Bug 1494713 - Expose `EventDispatcher::GetGlobalObject()` and `nsWindow::GetEventDispatcher()`. r=snorp
authorLina Cambridge <lina@yakshaving.ninja>
Fri, 16 Nov 2018 17:30:26 +0000
changeset 503233 155387956608096a66495a3da674e238efb49912
parent 503232 3ab98d066db05f049c52d634bc5d6c3b66763810
child 503234 6644b6e14bde969270ccb646bb501a11221e3041
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1494713
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1494713 - Expose `EventDispatcher::GetGlobalObject()` and `nsWindow::GetEventDispatcher()`. r=snorp Differential Revision: https://phabricator.services.mozilla.com/D11288
widget/android/EventDispatcher.cpp
widget/android/EventDispatcher.h
widget/android/moz.build
widget/android/nsWindow.h
--- a/widget/android/EventDispatcher.cpp
+++ b/widget/android/EventDispatcher.cpp
@@ -644,31 +644,27 @@ class NativeCallbackDelegateSupport fina
     public java::EventDispatcher::NativeCallbackDelegate
             ::Natives<NativeCallbackDelegateSupport>
 {
     using CallbackDelegate = java::EventDispatcher::NativeCallbackDelegate;
     using Base = CallbackDelegate::Natives<NativeCallbackDelegateSupport>;
 
     const nsCOMPtr<nsIAndroidEventCallback> mCallback;
     const nsCOMPtr<nsIAndroidEventFinalizer> mFinalizer;
-    const nsCOMPtr<nsPIDOMWindowOuter> mWindow;
+    const nsCOMPtr<nsIGlobalObject> mGlobalObject;
 
     void Call(jni::Object::Param aData,
               nsresult (nsIAndroidEventCallback::*aCall)(JS::HandleValue))
     {
         MOZ_ASSERT(NS_IsMainThread());
 
         // Use either the attached window's realm or a default realm.
 
         dom::AutoJSAPI jsapi;
-        if (mWindow) {
-            NS_ENSURE_TRUE_VOID(jsapi.Init(mWindow->GetCurrentInnerWindow()));
-        } else {
-            NS_ENSURE_TRUE_VOID(jsapi.Init(xpc::PrivilegedJunkScope()));
-        }
+        NS_ENSURE_TRUE_VOID(jsapi.Init(mGlobalObject));
 
         JS::RootedValue data(jsapi.cx());
         nsresult rv = UnboxData(NS_LITERAL_STRING("callback"), jsapi.cx(),
                                 aData, &data, /* BundleOnly */ false);
         NS_ENSURE_SUCCESS_VOID(rv);
 
         dom::AutoNoJSAPI nojsapi;
         rv = (mCallback->*aCall)(data);
@@ -690,20 +686,20 @@ public:
 
     static void Finalize(const CallbackDelegate::LocalRef& aInstance)
     {
         DisposeNative(aInstance);
     }
 
     NativeCallbackDelegateSupport(nsIAndroidEventCallback* callback,
                                   nsIAndroidEventFinalizer* finalizer,
-                                  nsPIDOMWindowOuter* domWindow)
+                                  nsIGlobalObject* globalObject)
         : mCallback(callback)
         , mFinalizer(finalizer)
-        , mWindow(domWindow)
+        , mGlobalObject(globalObject)
     {}
 
     ~NativeCallbackDelegateSupport()
     {
         if (mFinalizer) {
             mFinalizer->OnFinalize();
         }
     }
@@ -745,16 +741,25 @@ public:
 NS_IMPL_ISUPPORTS(FinalizingCallbackDelegate, nsIAndroidEventCallback)
 
 } // namespace detail
 
 using namespace detail;
 
 NS_IMPL_ISUPPORTS(EventDispatcher, nsIAndroidEventDispatcher)
 
+nsIGlobalObject*
+EventDispatcher::GetGlobalObject()
+{
+  if (mDOMWindow) {
+    return nsGlobalWindowInner::Cast(mDOMWindow->GetCurrentInnerWindow());
+  }
+  return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
+}
+
 nsresult
 EventDispatcher::DispatchOnGecko(ListenersList* list, const nsAString& aEvent,
                                  JS::HandleValue aData,
                                  nsIAndroidEventCallback* aCallback)
 {
     MOZ_ASSERT(NS_IsMainThread());
     dom::AutoNoJSAPI nojsapi;
 
@@ -797,17 +802,17 @@ EventDispatcher::WrapCallback(nsIAndroid
                 jni::GetGeckoThreadEnv());
     }
 
     java::EventDispatcher::NativeCallbackDelegate::LocalRef
             callback = java::EventDispatcher::NativeCallbackDelegate::New();
     NativeCallbackDelegateSupport::AttachNative(
             callback,
             MakeUnique<NativeCallbackDelegateSupport>(
-                    aCallback, aFinalizer, mDOMWindow));
+                    aCallback, aFinalizer, GetGlobalObject()));
     return callback;
 }
 
 NS_IMETHODIMP
 EventDispatcher::Dispatch(JS::HandleValue aEvent, JS::HandleValue aData,
                           nsIAndroidEventCallback* aCallback,
                           nsIAndroidEventFinalizer* aFinalizer, JSContext* aCx)
 {
@@ -855,23 +860,18 @@ EventDispatcher::Dispatch(const char16_t
                           java::GeckoBundle::Param aData,
                           nsIAndroidEventCallback* aCallback)
 {
     nsDependentString event(aEvent);
 
     ListenersList* list = mListenersMap.Get(event);
     if (list) {
         dom::AutoJSAPI jsapi;
-        if (mDOMWindow) {
-            NS_ENSURE_TRUE(jsapi.Init(mDOMWindow->GetCurrentInnerWindow()),
-                           NS_ERROR_FAILURE);
-        } else {
-            NS_ENSURE_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()),
-                           NS_ERROR_FAILURE);
-        }
+        NS_ENSURE_TRUE(jsapi.Init(GetGlobalObject()),
+                       NS_ERROR_FAILURE);
         JS::RootedValue data(jsapi.cx());
         nsresult rv = UnboxData(/* Event */ nullptr, jsapi.cx(), aData, &data,
                                 /* BundleOnly */ true);
         NS_ENSURE_SUCCESS(rv, rv);
         return DispatchOnGecko(list, event, data, aCallback);
     }
 
     java::EventDispatcher::LocalRef dispatcher(mDispatcher);
@@ -1063,21 +1063,17 @@ EventDispatcher::DispatchToGecko(jni::St
     ListenersList* list = mListenersMap.Get(event);
     if (!list || list->listeners.IsEmpty()) {
         return;
     }
 
     // Use the same compartment as the attached window if possible, otherwise
     // use a default compartment.
     dom::AutoJSAPI jsapi;
-    if (mDOMWindow) {
-        NS_ENSURE_TRUE_VOID(jsapi.Init(mDOMWindow->GetCurrentInnerWindow()));
-    } else {
-        NS_ENSURE_TRUE_VOID(jsapi.Init(xpc::PrivilegedJunkScope()));
-    }
+    NS_ENSURE_TRUE_VOID(jsapi.Init(GetGlobalObject()));
 
     JS::RootedValue data(jsapi.cx());
     nsresult rv = UnboxData(aEvent, jsapi.cx(), aData, &data,
                             /* BundleOnly */ true);
     NS_ENSURE_SUCCESS_VOID(rv);
 
     nsCOMPtr<nsIAndroidEventCallback> callback;
     if (aCallback) {
--- a/widget/android/EventDispatcher.h
+++ b/widget/android/EventDispatcher.h
@@ -49,16 +49,18 @@ public:
     void DispatchToGecko(jni::String::Param aEvent,
                          jni::Object::Param aData,
                          jni::Object::Param aCallback);
 
     static nsresult UnboxBundle(JSContext* aCx,
                                 jni::Object::Param aData,
                                 JS::MutableHandleValue aOut);
 
+    nsIGlobalObject* GetGlobalObject();
+
     using NativesBase::DisposeNative;
 private:
     java::EventDispatcher::WeakRef mDispatcher;
     nsCOMPtr<nsPIDOMWindowOuter> mDOMWindow;
 
     virtual ~EventDispatcher() {}
 
     struct ListenersList {
--- a/widget/android/moz.build
+++ b/widget/android/moz.build
@@ -29,16 +29,18 @@ EXPORTS += [
 EXPORTS += [
     '!GeneratedJNINatives.h',
     '!GeneratedJNIWrappers.h',
 ]
 
 EXPORTS.mozilla.widget += [
     'AndroidCompositorWidget.h',
     'AndroidUiThread.h',
+    'EventDispatcher.h',
+    'nsWindow.h',
 ]
 
 SOURCES += [
     '!GeneratedJNIWrappers.cpp',
 ]
 
 UNIFIED_SOURCES += [
     'AndroidAlerts.cpp',
--- a/widget/android/nsWindow.h
+++ b/widget/android/nsWindow.h
@@ -215,16 +215,23 @@ public:
     void OnSizeChanged(const mozilla::gfx::IntSize& aSize);
 
     void InitEvent(mozilla::WidgetGUIEvent& event,
                    LayoutDeviceIntPoint* aPoint = 0);
 
     void UpdateOverscrollVelocity(const float aX, const float aY);
     void UpdateOverscrollOffset(const float aX, const float aY);
 
+    mozilla::widget::EventDispatcher* GetEventDispatcher() const {
+        if (mAndroidView) {
+            return mAndroidView->mEventDispatcher;
+        }
+        return nullptr;
+    }
+
     //
     // nsIWidget
     //
 
     using nsBaseWidget::Create; // for Create signature not overridden here
     virtual MOZ_MUST_USE nsresult Create(nsIWidget* aParent,
                                          nsNativeWidget aNativeParent,
                                          const LayoutDeviceIntRect& aRect,