Bug 1294071 - Move dispatchMemoryPressure out of GeckoAppShell; r=snorp
authorJim Chen <nchen@mozilla.com>
Thu, 18 Aug 2016 18:04:11 -0400
changeset 353695 11c088af8ed917102fda4b0a85203645f8c272cf
parent 353694 d4035843d2aacd9a8eebeeba4bbab8a55537658b
child 353696 418eb82a09f157f7ffea2f3d8b7778f91b9d40a3
push id1324
push usermtabara@mozilla.com
push dateMon, 16 Jan 2017 13:07:44 +0000
treeherdermozilla-release@a01c49833940 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1294071
milestone51.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 1294071 - Move dispatchMemoryPressure out of GeckoAppShell; r=snorp Move dispatchMemoryPressure out of GeckoAppShell and AndroidJNI.cpp
mobile/android/base/java/org/mozilla/gecko/MemoryMonitor.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
mozglue/android/jni-stubs.inc
widget/android/AndroidJNI.cpp
widget/android/GeneratedJNINatives.h
widget/android/GeneratedJNIWrappers.cpp
widget/android/GeneratedJNIWrappers.h
widget/android/MemoryMonitor.h
widget/android/nsAppShell.cpp
--- a/mobile/android/base/java/org/mozilla/gecko/MemoryMonitor.java
+++ b/mobile/android/base/java/org/mozilla/gecko/MemoryMonitor.java
@@ -1,15 +1,16 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import org.mozilla.gecko.annotation.WrapForJNI;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.favicons.Favicons;
 import org.mozilla.gecko.home.ImageLoader;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.content.BroadcastReceiver;
 import android.content.ComponentCallbacks2;
@@ -136,16 +137,19 @@ class MemoryMonitor extends BroadcastRec
                 label = "default";
             }
             GeckoAppShell.notifyObservers("Memory:Dump", label);
         } else if (ACTION_FORCE_PRESSURE.equals(intent.getAction())) {
             increaseMemoryPressure(MEMORY_PRESSURE_HIGH);
         }
     }
 
+    @WrapForJNI(calledFrom = "ui")
+    private static native void dispatchMemoryPressure();
+
     private boolean increaseMemoryPressure(int level) {
         int oldLevel;
         synchronized (this) {
             // bump up our level if we're not already higher
             if (mMemoryPressure > level) {
                 return false;
             }
             oldLevel = mMemoryPressure;
@@ -167,17 +171,17 @@ class MemoryMonitor extends BroadcastRec
             // we did on the last memory pressure increase.
             return false;
         }
 
         // TODO hook in memory-reduction stuff for different levels here
         if (level >= MEMORY_PRESSURE_MEDIUM) {
             //Only send medium or higher events because that's all that is used right now
             if (GeckoThread.isRunning()) {
-                GeckoAppShell.dispatchMemoryPressure();
+                dispatchMemoryPressure();
             }
 
             Favicons.clearMemCache();
             ImageLoader.clearLruCache();
         }
         return true;
     }
 
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoAppShell.java
@@ -234,17 +234,16 @@ public class GeckoAppShell
     static public final int LINK_TYPE_2G = 5;
     static public final int LINK_TYPE_3G = 6;
     static public final int LINK_TYPE_4G = 7;
 
     /* The Android-side API: API methods that Android calls */
 
     // helper methods
     public static native void onSurfaceTextureFrameAvailable(Object surfaceTexture, int id);
-    public static native void dispatchMemoryPressure();
 
     private static native void reportJavaCrash(String stackTrace);
 
     @WrapForJNI(dispatchTo = "gecko")
     public static native void notifyUriVisited(String uri);
 
     public static native void invalidateAndScheduleComposite();
 
--- a/mozglue/android/jni-stubs.inc
+++ b/mozglue/android/jni-stubs.inc
@@ -15,35 +15,16 @@ Java_org_mozilla_gecko_GeckoAppShell_onS
 #endif
 
 #ifdef JNI_BINDINGS
   xul_dlsym("Java_org_mozilla_gecko_GeckoAppShell_onSurfaceTextureFrameAvailable", &f_Java_org_mozilla_gecko_GeckoAppShell_onSurfaceTextureFrameAvailable);
 #endif
 
 #ifdef JNI_STUBS
 
-typedef void (*Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure_t)(JNIEnv *, jclass);
-static Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure_t f_Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure;
-extern "C" NS_EXPORT void MOZ_JNICALL
-Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure(JNIEnv * arg0, jclass arg1) {
-    if (!f_Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure) {
-        arg0->ThrowNew(arg0->FindClass("java/lang/UnsupportedOperationException"),
-                       "JNI Function called before it was loaded");
-        return ;
-    }
-     f_Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure(arg0, arg1);
-}
-#endif
-
-#ifdef JNI_BINDINGS
-  xul_dlsym("Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure", &f_Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure);
-#endif
-
-#ifdef JNI_STUBS
-
 typedef void (*Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash_t)(JNIEnv *, jclass, jstring);
 static Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash_t f_Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash;
 extern "C" NS_EXPORT void MOZ_JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash(JNIEnv * arg0, jclass arg1, jstring arg2) {
     if (!f_Java_org_mozilla_gecko_GeckoAppShell_reportJavaCrash) {
         arg0->ThrowNew(arg0->FindClass("java/lang/UnsupportedOperationException"),
                        "JNI Function called before it was loaded");
         return ;
--- a/widget/android/AndroidJNI.cpp
+++ b/widget/android/AndroidJNI.cpp
@@ -31,17 +31,16 @@
 
 #include "mozilla/unused.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/UniquePtr.h"
 
 #include "mozilla/layers/APZCTreeManager.h"
 #include "nsPluginInstanceOwner.h"
 #include "AndroidSurfaceTexture.h"
-#include "nsMemoryPressure.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace mozilla::layers;
 using namespace mozilla::widget;
 
 /* Forward declare all the JNI methods as extern "C" */
 
@@ -120,15 +119,9 @@ Java_org_mozilla_gecko_GeckoAppShell_onS
   if (!st) {
     __android_log_print(ANDROID_LOG_ERROR, "GeckoJNI", "Failed to find AndroidSurfaceTexture with id %d", id);
     return;
   }
 
   st->NotifyFrameAvailable();
 }
 
-NS_EXPORT void JNICALL
-Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure(JNIEnv* jenv, jclass)
-{
-    NS_DispatchMemoryPressure(MemPressure_New);
 }
-
-}
--- a/widget/android/GeneratedJNINatives.h
+++ b/widget/android/GeneratedJNINatives.h
@@ -345,16 +345,31 @@ const JNINativeMethod GeckoView::Window:
             ::template Wrap<&Impl::Open>),
 
     mozilla::jni::MakeNativeMethod<GeckoView::Window::Reattach_t>(
             mozilla::jni::NativeStub<GeckoView::Window::Reattach_t, Impl>
             ::template Wrap<&Impl::Reattach>)
 };
 
 template<class Impl>
+class MemoryMonitor::Natives : public mozilla::jni::NativeImpl<MemoryMonitor, Impl>
+{
+public:
+    static const JNINativeMethod methods[1];
+};
+
+template<class Impl>
+const JNINativeMethod MemoryMonitor::Natives<Impl>::methods[] = {
+
+    mozilla::jni::MakeNativeMethod<MemoryMonitor::DispatchMemoryPressure_t>(
+            mozilla::jni::NativeStub<MemoryMonitor::DispatchMemoryPressure_t, Impl>
+            ::template Wrap<&Impl::DispatchMemoryPressure>)
+};
+
+template<class Impl>
 class PrefsHelper::Natives : public mozilla::jni::NativeImpl<PrefsHelper, Impl>
 {
 public:
     static const JNINativeMethod methods[4];
 };
 
 template<class Impl>
 const JNINativeMethod PrefsHelper::Natives<Impl>::methods[] = {
--- a/widget/android/GeneratedJNIWrappers.cpp
+++ b/widget/android/GeneratedJNIWrappers.cpp
@@ -1139,16 +1139,22 @@ constexpr char GeckoView::Window::LoadUr
 constexpr char GeckoView::Window::LoadUri_t::signature[];
 
 constexpr char GeckoView::Window::Open_t::name[];
 constexpr char GeckoView::Window::Open_t::signature[];
 
 constexpr char GeckoView::Window::Reattach_t::name[];
 constexpr char GeckoView::Window::Reattach_t::signature[];
 
+const char MemoryMonitor::name[] =
+        "org/mozilla/gecko/MemoryMonitor";
+
+constexpr char MemoryMonitor::DispatchMemoryPressure_t::name[];
+constexpr char MemoryMonitor::DispatchMemoryPressure_t::signature[];
+
 const char PrefsHelper::name[] =
         "org/mozilla/gecko/PrefsHelper";
 
 constexpr char PrefsHelper::CallPrefHandler_t::name[];
 constexpr char PrefsHelper::CallPrefHandler_t::signature[];
 
 auto PrefsHelper::CallPrefHandler(mozilla::jni::Object::Param a0, int32_t a1, mozilla::jni::String::Param a2, bool a3, int32_t a4, mozilla::jni::String::Param a5) -> void
 {
--- a/widget/android/GeneratedJNIWrappers.h
+++ b/widget/android/GeneratedJNIWrappers.h
@@ -3566,16 +3566,46 @@ public:
     };
 
     static const mozilla::jni::CallingThread callingThread =
             mozilla::jni::CallingThread::ANY;
 
     template<class Impl> class Natives;
 };
 
+class MemoryMonitor : public mozilla::jni::ObjectBase<MemoryMonitor>
+{
+public:
+    static const char name[];
+
+    explicit MemoryMonitor(const Context& ctx) : ObjectBase<MemoryMonitor>(ctx) {}
+
+    struct DispatchMemoryPressure_t {
+        typedef MemoryMonitor Owner;
+        typedef void ReturnType;
+        typedef void SetterType;
+        typedef mozilla::jni::Args<> Args;
+        static constexpr char name[] = "dispatchMemoryPressure";
+        static constexpr char signature[] =
+                "()V";
+        static const bool isStatic = true;
+        static const mozilla::jni::ExceptionMode exceptionMode =
+                mozilla::jni::ExceptionMode::ABORT;
+        static const mozilla::jni::CallingThread callingThread =
+                mozilla::jni::CallingThread::UI;
+        static const mozilla::jni::DispatchTarget dispatchTarget =
+                mozilla::jni::DispatchTarget::CURRENT;
+    };
+
+    static const mozilla::jni::CallingThread callingThread =
+            mozilla::jni::CallingThread::UI;
+
+    template<class Impl> class Natives;
+};
+
 class PrefsHelper : public mozilla::jni::ObjectBase<PrefsHelper>
 {
 public:
     static const char name[];
 
     explicit PrefsHelper(const Context& ctx) : ObjectBase<PrefsHelper>(ctx) {}
 
     struct CallPrefHandler_t {
new file mode 100644
--- /dev/null
+++ b/widget/android/MemoryMonitor.h
@@ -0,0 +1,27 @@
+/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef MemoryMonitor_h
+#define MemoryMonitor_h
+
+#include "GeneratedJNINatives.h"
+#include "nsMemoryPressure.h"
+
+namespace mozilla {
+
+class MemoryMonitor final
+    : public java::MemoryMonitor::Natives<MemoryMonitor>
+{
+public:
+    static void
+    DispatchMemoryPressure()
+    {
+        NS_DispatchMemoryPressure(MemoryPressureState::MemPressure_New);
+    }
+};
+
+} // namespace mozilla
+
+#endif // MemoryMonitor_h
--- a/widget/android/nsAppShell.cpp
+++ b/widget/android/nsAppShell.cpp
@@ -53,16 +53,17 @@
 #include "mozilla/Logging.h"
 #endif
 
 #include "AndroidAlerts.h"
 #include "ANRReporter.h"
 #include "GeckoBatteryManager.h"
 #include "GeckoNetworkManager.h"
 #include "GeckoScreenOrientation.h"
+#include "MemoryMonitor.h"
 #include "PrefsHelper.h"
 #include "Telemetry.h"
 #include "ThumbnailHelper.h"
 
 #ifdef DEBUG_ANDROID_EVENTS
 #define EVLOG(args...)  ALOG(args)
 #else
 #define EVLOG(args...) do { } while (0)
@@ -342,16 +343,17 @@ nsAppShell::nsAppShell()
         // Initialize JNI and Set the corresponding state in GeckoThread.
         AndroidBridge::ConstructBridge();
         GeckoAppShellSupport::Init();
         GeckoThreadSupport::Init();
         mozilla::ANRReporter::Init();
         mozilla::GeckoBatteryManager::Init();
         mozilla::GeckoNetworkManager::Init();
         mozilla::GeckoScreenOrientation::Init();
+        mozilla::MemoryMonitor::Init();
         mozilla::PrefsHelper::Init();
         mozilla::widget::Telemetry::Init();
         mozilla::ThumbnailHelper::Init();
         nsWindow::InitNatives();
 
         java::GeckoThread::SetState(java::GeckoThread::State::JNI_READY());
     }