Bug 1369107 - 3. Don't use GeckoAppShell.getContext for plugin code; r=snorp
authorJim Chen <nchen@mozilla.com>
Fri, 02 Jun 2017 16:13:41 -0400
changeset 410277 5c6b25ec2b198c6f8b75ca9c930dfc75a5286529
parent 410276 685f48a31f21eaeb7df0529b392d2899f0a5c01f
child 410278 49051e18e2760f6a34658b178f52f63fb0e8396e
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1369107
milestone55.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 1369107 - 3. Don't use GeckoAppShell.getContext for plugin code; r=snorp Use getApplicationContext or GeckoApp.getPluginContext instead of getContext so we can get rid of ContextGetter. MozReview-Commit-ID: B8tTPeZ5gvt
dom/plugins/base/nsNPAPIPlugin.cpp
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
widget/android/AndroidBridge.cpp
widget/android/fennec/FennecJNIWrappers.cpp
widget/android/fennec/FennecJNIWrappers.h
--- a/dom/plugins/base/nsNPAPIPlugin.cpp
+++ b/dom/plugins/base/nsNPAPIPlugin.cpp
@@ -96,16 +96,17 @@ using mozilla::plugins::PluginModuleCont
 #include "mozilla/a11y/Compatibility.h"
 #endif
 #endif
 
 #ifdef MOZ_WIDGET_ANDROID
 #include <android/log.h>
 #include "android_npapi.h"
 #include "ANPBase.h"
+#include "FennecJNIWrappers.h"
 #include "GeneratedJNIWrappers.h"
 #undef LOG
 #define LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "GeckoPlugins" , ## args)
 #endif
 
 #include "nsIAudioChannelAgent.h"
 #include "AudioChannelService.h"
 
@@ -2077,20 +2078,23 @@ NPError
 
     case kSupportedDrawingModel_ANPGetValue: {
       LOG("get supported drawing model");
       return NPERR_GENERIC_ERROR;
     }
 
     case kJavaContext_ANPGetValue: {
       LOG("get java context");
-      auto ret = java::GeckoAppShell::GetContext();
-      if (!ret)
+
+      auto ret = npp && jni::IsFennec()
+          ? java::GeckoApp::GetPluginContext()
+          : java::GeckoAppShell::GetApplicationContext();
+      if (!ret) {
         return NPERR_GENERIC_ERROR;
-
+      }
       *static_cast<jobject*>(result) = ret.Forget();
       return NPERR_NO_ERROR;
     }
 
     case kAudioTrackInterfaceV1_ANPGetValue: {
       LOG("get audio interface v1");
       ANPAudioTrackInterfaceV1 *i = (ANPAudioTrackInterfaceV1 *) result;
       InitAudioTrackInterfaceV1(i);
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -1074,16 +1074,21 @@ public abstract class GeckoApp extends G
                 }
             });
         }
     }
 
     @WrapForJNI(calledFrom = "ui", dispatchTo = "gecko")
     private static native void onFullScreenPluginHidden(View view);
 
+    @WrapForJNI(calledFrom = "gecko")
+    private static Context getPluginContext() {
+        return GeckoActivityMonitor.getInstance().getCurrentActivity();
+    }
+
     private void showSetImageResult(final boolean success, final int message, final String path) {
         ThreadUtils.postToUiThread(new Runnable() {
             @Override
             public void run() {
                 if (!success) {
                     SnackbarBuilder.builder(GeckoApp.this)
                             .message(message)
                             .duration(Snackbar.LENGTH_LONG)
--- a/widget/android/AndroidBridge.cpp
+++ b/widget/android/AndroidBridge.cpp
@@ -685,42 +685,18 @@ AndroidBridge::GetCurrentNetworkInformat
 }
 
 jobject
 AndroidBridge::GetGlobalContextRef() {
     if (sGlobalContext) {
         return sGlobalContext;
     }
 
-    JNIEnv* const env = GetEnvForThread();
-    AutoLocalJNIFrame jniFrame(env, 4);
-
-    auto context = GeckoAppShell::GetContext();
-    if (!context) {
-        ALOG_BRIDGE("%s: Could not GetContext()", __FUNCTION__);
-        return 0;
-    }
-    jclass contextClass = env->FindClass("android/content/Context");
-    if (!contextClass) {
-        ALOG_BRIDGE("%s: Could not find Context class.", __FUNCTION__);
-        return 0;
-    }
-    jmethodID mid = env->GetMethodID(contextClass, "getApplicationContext",
-                                     "()Landroid/content/Context;");
-    if (!mid) {
-        ALOG_BRIDGE("%s: Could not find getApplicationContext.", __FUNCTION__);
-        return 0;
-    }
-    jobject appContext = env->CallObjectMethod(context.Get(), mid);
-    if (!appContext) {
-        ALOG_BRIDGE("%s: getApplicationContext failed.", __FUNCTION__);
-        return 0;
-    }
-
-    sGlobalContext = env->NewGlobalRef(appContext);
+    auto context = GeckoAppShell::GetApplicationContext();
+    sGlobalContext = Object::GlobalRef(context).Forget();
     MOZ_ASSERT(sGlobalContext);
     return sGlobalContext;
 }
 
 /* Implementation file */
 NS_IMPL_ISUPPORTS(nsAndroidBridge,
                   nsIAndroidEventDispatcher,
                   nsIAndroidBridge,
--- a/widget/android/fennec/FennecJNIWrappers.cpp
+++ b/widget/android/fennec/FennecJNIWrappers.cpp
@@ -47,16 +47,24 @@ const char GeckoApp::name[] =
 constexpr char GeckoApp::AddPluginView_t::name[];
 constexpr char GeckoApp::AddPluginView_t::signature[];
 
 auto GeckoApp::AddPluginView(mozilla::jni::Object::Param a0) -> void
 {
     return mozilla::jni::Method<AddPluginView_t>::Call(GeckoApp::Context(), nullptr, a0);
 }
 
+constexpr char GeckoApp::GetPluginContext_t::name[];
+constexpr char GeckoApp::GetPluginContext_t::signature[];
+
+auto GeckoApp::GetPluginContext() -> mozilla::jni::Object::LocalRef
+{
+    return mozilla::jni::Method<GetPluginContext_t>::Call(GeckoApp::Context(), nullptr);
+}
+
 constexpr char GeckoApp::LaunchOrBringToFront_t::name[];
 constexpr char GeckoApp::LaunchOrBringToFront_t::signature[];
 
 auto GeckoApp::LaunchOrBringToFront() -> void
 {
     return mozilla::jni::Method<LaunchOrBringToFront_t>::Call(GeckoApp::Context(), nullptr);
 }
 
--- a/widget/android/fennec/FennecJNIWrappers.h
+++ b/widget/android/fennec/FennecJNIWrappers.h
@@ -151,16 +151,35 @@ public:
         static const mozilla::jni::CallingThread callingThread =
                 mozilla::jni::CallingThread::GECKO;
         static const mozilla::jni::DispatchTarget dispatchTarget =
                 mozilla::jni::DispatchTarget::CURRENT;
     };
 
     static auto AddPluginView(mozilla::jni::Object::Param) -> void;
 
+    struct GetPluginContext_t {
+        typedef GeckoApp Owner;
+        typedef mozilla::jni::Object::LocalRef ReturnType;
+        typedef mozilla::jni::Object::Param SetterType;
+        typedef mozilla::jni::Args<> Args;
+        static constexpr char name[] = "getPluginContext";
+        static constexpr char signature[] =
+                "()Landroid/content/Context;";
+        static const bool isStatic = true;
+        static const mozilla::jni::ExceptionMode exceptionMode =
+                mozilla::jni::ExceptionMode::ABORT;
+        static const mozilla::jni::CallingThread callingThread =
+                mozilla::jni::CallingThread::GECKO;
+        static const mozilla::jni::DispatchTarget dispatchTarget =
+                mozilla::jni::DispatchTarget::CURRENT;
+    };
+
+    static auto GetPluginContext() -> mozilla::jni::Object::LocalRef;
+
     struct LaunchOrBringToFront_t {
         typedef GeckoApp Owner;
         typedef void ReturnType;
         typedef void SetterType;
         typedef mozilla::jni::Args<> Args;
         static constexpr char name[] = "launchOrBringToFront";
         static constexpr char signature[] =
                 "()V";