Bug 1478776 - Part 10: Add internal VisualViewport resize/scroll events. r=botond,nika

The VisualViewport events are all nice and shiny, but unfortunately not quite
what is needed for the session store.

Firstly, the spec wants the "scroll" event to be fired only when the *relative*
offset between visual and layout viewport changes. The session store however
records the absolute offset and as such is interested in when *that* changes.

Secondly, again as per the spec the events don't bubble, and with the default
DOMEventTargetHelper implementation they don't escape the VisualViewport during
capturing, either. This means that any event listener must be added directly on
the VisualViewport itself in order to capture any events.

This might have been intended because the events use the same names as the
normal "scroll"/"resize" events, and as such you cannot specify separate event
listeners for VisualViewport and non-VisualViewport "scroll" events if both
events end up being dispatched to the same element (you can only try to filter
after the fact by looking at the originalTarget of the event).

At the same time, the VisualViewport is attached to the inner Window, and so
each time you navigate, you also get a different VisualViewport object.
All of this might be totally fine from the perspective of a page script, because
in that case you won't care anyway about what happens when the current page goes
away.

From the session store perspective on the other hand (especially Fennec's non-
e10s session store design), this is rather unfortunate because we don't want to
have to keep registering event listeners
a) manually for each subframe
b) each time the page navigates

The event target chain problem could be solved by letting the scroll events
escape the VisualViewport during the capturing phase (which the spec doesn't say
anything about), but this would mean that any scroll listener attached to a
window/browser/... that uses capturing will now catch both layout and visual
viewport scroll events.

In some cases this might even be beneficial, but in others (e.g. bug 1498812
comment 21) I'd like to specifically decide which kind of scroll event to
capture. Having to look at event.originalTarget to distinguish the two kinds
might be defensible in test code, but in case this distinction would be needed
in production code as well, given the existence of a C++-based filtering helper
in nsSessionStoreUtils for another use case where (scroll) events need to be
filtered, JS-based scroll event filtering might be a bad idea.

Additionally, in any case this wouldn't solve the fundamental conflict between
the spec and the session store about *when* the "scroll" event should be fired
in the first place.

Hence I'd like to introduce a separate set of events with distinct event names,
which will be dispatched according to the requirements of our internal users
(i.e. currently the session store). To avoid potential web compatibility issues
down the road, for now these events will be dispatched only to event listeners
registered in the system group (allowing *all* Chrome event listeners cannot be
done because checking the Chrome status of each event target might be too
expensive for frequently dispatched events).

Differential Revision: https://phabricator.services.mozilla.com/D14046

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jan Henning 2018-12-20 22:14:42 +00:00
parent fb0460d033
commit 7fb92b8c44
12 changed files with 82 additions and 31 deletions

View File

@ -38,6 +38,24 @@ JSObject* VisualViewport::WrapObject(JSContext* aCx,
return VisualViewport_Binding::Wrap(aCx, this, aGivenProto);
}
/* virtual */
void VisualViewport::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
EventMessage msg = aVisitor.mEvent->mMessage;
aVisitor.mCanHandle = true;
EventTarget* parentTarget = nullptr;
// Only our special internal events are allowed to escape the
// Visual Viewport and be dispatched further up the DOM tree.
if (msg == eMozVisualScroll || msg == eMozVisualResize) {
if (nsPIDOMWindowInner* win = GetOwner()) {
if (nsIDocument* doc = win->GetExtantDoc()) {
parentTarget = doc;
}
}
}
aVisitor.SetParentTarget(parentTarget, false);
}
CSSSize VisualViewport::VisualViewportSize() const {
CSSSize size = CSSSize(0, 0);
@ -163,6 +181,11 @@ void VisualViewport::FireResizeEvent() {
mResizeEvent->Revoke();
mResizeEvent = nullptr;
VVP_LOG("%p, FireResizeEvent, fire mozvisualresize\n", this);
WidgetEvent mozEvent(true, eMozVisualResize);
mozEvent.mFlags.mOnlySystemGroupDispatch = true;
EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent);
VVP_LOG("%p, FireResizeEvent, fire VisualViewport resize\n", this);
WidgetEvent event(true, eResize);
event.mFlags.mBubbles = false;
@ -172,27 +195,29 @@ void VisualViewport::FireResizeEvent() {
/* ================= Scroll event handling ================= */
void VisualViewport::PostScrollEvent(const nsPoint& aPrevRelativeOffset) {
VVP_LOG("%p: PostScrollEvent, prevRelativeOffset %s\n", this,
ToString(aPrevRelativeOffset).c_str());
void VisualViewport::PostScrollEvent(const nsPoint& aPrevVisualOffset,
const nsPoint& aPrevLayoutOffset) {
VVP_LOG("%p: PostScrollEvent, prevRelativeOffset=%s\n", this,
ToString(aPrevVisualOffset - aPrevLayoutOffset).c_str());
if (mScrollEvent) {
return;
}
// The event constructor will register itself with the refresh driver.
if (nsPresContext* presContext = GetPresContext()) {
mScrollEvent =
new VisualViewportScrollEvent(this, presContext, aPrevRelativeOffset);
mScrollEvent = new VisualViewportScrollEvent(
this, presContext, aPrevVisualOffset, aPrevLayoutOffset);
VVP_LOG("%p: PostScrollEvent, created new event\n", this);
}
}
VisualViewport::VisualViewportScrollEvent::VisualViewportScrollEvent(
VisualViewport* aViewport, nsPresContext* aPresContext,
const nsPoint& aPrevRelativeOffset)
const nsPoint& aPrevVisualOffset, const nsPoint& aPrevLayoutOffset)
: Runnable("VisualViewport::VisualViewportScrollEvent"),
mViewport(aViewport),
mPrevRelativeOffset(aPrevRelativeOffset) {
mPrevVisualOffset(aPrevVisualOffset),
mPrevLayoutOffset(aPrevLayoutOffset) {
aPresContext->RefreshDriver()->PostVisualViewportScrollEvent(this);
}
@ -206,17 +231,27 @@ VisualViewport::VisualViewportScrollEvent::Run() {
void VisualViewport::FireScrollEvent() {
MOZ_ASSERT(mScrollEvent);
nsPoint prevRelativeOffset = mScrollEvent->PrevRelativeOffset();
nsPoint prevVisualOffset = mScrollEvent->PrevVisualOffset();
nsPoint prevLayoutOffset = mScrollEvent->PrevLayoutOffset();
mScrollEvent->Revoke();
mScrollEvent = nullptr;
nsIPresShell* presShell = GetPresShell();
// Check whether the relative visual viewport offset actually changed - maybe
// both visual and layout viewport scrolled together and there was no change
// after all.
if (presShell) {
if (nsIPresShell* presShell = GetPresShell()) {
if (presShell->GetVisualViewportOffset() != prevVisualOffset) {
// The internal event will be fired whenever the visual viewport's
// *absolute* offset changed, i.e. relative to the page.
VVP_LOG("%p: FireScrollEvent, fire mozvisualscroll\n", this);
WidgetEvent mozEvent(true, eMozVisualScroll);
mozEvent.mFlags.mOnlySystemGroupDispatch = true;
EventDispatcher::Dispatch(this, GetPresContext(), &mozEvent);
}
// Check whether the relative visual viewport offset actually changed -
// maybe both visual and layout viewport scrolled together and there was no
// change after all.
nsPoint curRelativeOffset =
presShell->GetVisualViewportOffsetRelativeToLayoutViewport();
nsPoint prevRelativeOffset = prevVisualOffset - prevLayoutOffset;
VVP_LOG(
"%p: FireScrollEvent, curRelativeOffset %s, "
"prevRelativeOffset %s\n",

View File

@ -33,9 +33,11 @@ class VisualViewport final : public mozilla::DOMEventTargetHelper {
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
void PostResizeEvent();
void PostScrollEvent(const nsPoint& aPrevRelativeOffset);
void PostScrollEvent(const nsPoint& aPrevVisualOffset,
const nsPoint& aPrevLayoutOffset);
// These two events are modelled after the ScrollEvent class in
// nsGfxScrollFrame.h.
@ -55,9 +57,11 @@ class VisualViewport final : public mozilla::DOMEventTargetHelper {
NS_DECL_NSIRUNNABLE
VisualViewportScrollEvent(VisualViewport* aViewport,
nsPresContext* aPresContext,
const nsPoint& aPrevRelativeOffset);
const nsPoint& aPrevVisualOffset,
const nsPoint& aPrevLayoutOffset);
void Revoke() { mViewport = nullptr; }
nsPoint PrevRelativeOffset() const { return mPrevRelativeOffset; }
nsPoint PrevVisualOffset() const { return mPrevVisualOffset; }
nsPoint PrevLayoutOffset() const { return mPrevLayoutOffset; }
private:
VisualViewport* mViewport;
@ -70,7 +74,8 @@ class VisualViewport final : public mozilla::DOMEventTargetHelper {
// Hopefully, at this point both visual and layout viewport positions have
// been updated, so that we're able to tell whether the relative offset did
// in fact change or not.
const nsPoint mPrevRelativeOffset;
const nsPoint mPrevVisualOffset;
const nsPoint mPrevLayoutOffset;
};
private:

View File

@ -265,6 +265,10 @@ FORWARDED_EVENT(load, eLoad, EventNameType_All, eBasicEventClass)
FORWARDED_EVENT(resize, eResize, EventNameType_All, eBasicEventClass)
FORWARDED_EVENT(scroll, eScroll, (EventNameType_HTMLXUL | EventNameType_SVGSVG),
eBasicEventClass)
NON_IDL_EVENT(mozvisualresize, eMozVisualResize, EventNameType_None,
eBasicEventClass)
NON_IDL_EVENT(mozvisualscroll, eMozVisualScroll, EventNameType_None,
eBasicEventClass)
WINDOW_EVENT(afterprint, eAfterPrint,
EventNameType_XUL | EventNameType_HTMLBodyOrFramesetOnly,

View File

@ -60,7 +60,7 @@ function* test(testDriver) {
// Our internal visual viewport scroll event on the other hand only cares
// about the absolute offset of the visual viewport and should therefore
// definitively fire.
todo(visScrEvtInternal.count > 0, "Got some mozvisualscroll events");
ok(visScrEvtInternal.count > 0, "Got some mozvisualscroll events");
}
waitUntilApzStable()

View File

@ -60,7 +60,7 @@ function* test(testDriver) {
visResEvt.unregister();
ok(visResEvt.count > 0, "Got some visual viewport resize events");
visResEvtInternal.unregister();
todo(visResEvtInternal.count > 0, "Got some mozvisualresize events");
ok(visResEvtInternal.count > 0, "Got some mozvisualresize events");
// We're pinch-zooming somewhere in the middle of the page, so the visual
// viewport's coordinates change, too.
@ -69,7 +69,7 @@ function* test(testDriver) {
visScrEvt.unregister();
ok(visScrEvt.count > 0, "Got some visual viewport scroll events");
visScrEvtInternal.unregister();
todo(visScrEvtInternal.count > 0, "Got some mozvisualscroll events");
ok(visScrEvtInternal.count > 0, "Got some mozvisualscroll events");
// Our internal events shouldn't leak to normal content.
visResEvtContent.unregister();

View File

@ -176,7 +176,7 @@ static ScreenMargin ScrollFrame(nsIContent* aContent,
if (nsCOMPtr<nsIPresShell> shell = GetPresShell(aContent)) {
shell->SetVisualViewportOffset(
CSSPoint::ToAppUnits(aRequest.GetScrollOffset()),
shell->GetVisualViewportOffsetRelativeToLayoutViewport());
shell->GetLayoutViewportOffset());
}
}
}

View File

@ -10042,13 +10042,15 @@ void nsIPresShell::SetVisualViewportSize(nscoord aWidth, nscoord aHeight) {
}
}
void nsIPresShell::SetVisualViewportOffset(const nsPoint& aScrollOffset,
const nsPoint& aPrevRelativeOffset) {
void nsIPresShell::SetVisualViewportOffset(
const nsPoint& aScrollOffset, const nsPoint& aPrevLayoutScrollPos) {
if (mVisualViewportOffset != aScrollOffset) {
nsPoint prevOffset = mVisualViewportOffset;
mVisualViewportOffset = aScrollOffset;
if (auto* window = nsGlobalWindowInner::Cast(mDocument->GetInnerWindow())) {
window->VisualViewport()->PostScrollEvent(aPrevRelativeOffset);
window->VisualViewport()->PostScrollEvent(prevOffset,
aPrevLayoutScrollPos);
}
}
}

View File

@ -1648,7 +1648,7 @@ class nsIPresShell : public nsStubDocumentObserver {
}
void SetVisualViewportOffset(const nsPoint& aScrollOffset,
const nsPoint& aPrevRelativeOffset);
const nsPoint& aPrevLayoutScrollPos);
nsPoint GetVisualViewportOffset() const { return mVisualViewportOffset; }

View File

@ -2676,9 +2676,6 @@ void ScrollFrameHelper::ScrollToImpl(nsPoint aPt, const nsRect& aRange,
if (dist.x >= horzAllowance || dist.y >= vertAllowance) {
needFrameVisibilityUpdate = true;
}
nsPoint prevVVRelativeOffset =
presContext->PresShell()
->GetVisualViewportOffsetRelativeToLayoutViewport();
// notify the listeners.
for (uint32_t i = 0; i < mListeners.Length(); i++) {
@ -2740,7 +2737,7 @@ void ScrollFrameHelper::ScrollToImpl(nsPoint aPt, const nsRect& aRange,
// offset (e.g. by using nsIDOMWindowUtils.getVisualViewportOffset()
// in chrome JS code) before it's updated by the next APZ repaint,
// we could get incorrect results.
presContext->PresShell()->SetVisualViewportOffset(pt, prevVVRelativeOffset);
presContext->PresShell()->SetVisualViewportOffset(pt, curPos);
}
ScrollVisual();
@ -2866,7 +2863,8 @@ void ScrollFrameHelper::ScrollToImpl(nsPoint aPt, const nsRect& aRange,
if (mIsRoot) {
if (auto* window = nsGlobalWindowInner::Cast(
mOuter->PresContext()->Document()->GetInnerWindow())) {
window->VisualViewport()->PostScrollEvent(prevVVRelativeOffset);
window->VisualViewport()->PostScrollEvent(
presContext->PresShell()->GetVisualViewportOffset(), curPos);
}
}

View File

@ -457,7 +457,8 @@ class WidgetEvent : public WidgetEventTime {
mFlags.mBubbles = true;
break;
default:
if (mMessage == eResize || mMessage == eEditorInput) {
if (mMessage == eResize || mMessage == eMozVisualResize ||
mMessage == eMozVisualScroll || mMessage == eEditorInput) {
mFlags.mCancelable = false;
} else {
mFlags.mCancelable = true;

View File

@ -55,6 +55,8 @@ NS_EVENT_MESSAGE(eAccessKeyNotFound)
NS_EVENT_MESSAGE(eResize)
NS_EVENT_MESSAGE(eScroll)
NS_EVENT_MESSAGE(eMozVisualResize)
NS_EVENT_MESSAGE(eMozVisualScroll)
// Application installation
NS_EVENT_MESSAGE(eInstall)

View File

@ -1881,6 +1881,10 @@ STATIC_ATOMS = [
# MediaDevices device change event
Atom("ondevicechange", "ondevicechange"),
# Internal Visual Viewport events
Atom("onmozvisualresize", "onmozvisualresize"),
Atom("onmozvisualscroll", "onmozvisualscroll"),
# WebExtensions
Atom("moz_extension", "moz-extension"),
Atom("all_urlsPermission", "<all_urls>"),