Bug 1219016 - Store application context in GeckoAppShell; r=snorp
authorJim Chen <nchen@mozilla.com>
Wed, 11 Nov 2015 21:28:59 -0500
changeset 308419 9dbbf81894ca3903dad663333987fe01ddfac63b
parent 308418 f2bffacca04c239a253cf6906cd98210a7381285
child 308420 d42f4c84b136ebebe71536ca87135f3d87e0b16f
push id7470
push users.kaspari@gmail.com
push dateThu, 12 Nov 2015 12:51:02 +0000
reviewerssnorp
bugs1219016
milestone45.0a1
Bug 1219016 - Store application context in GeckoAppShell; r=snorp This patch adds separate setter and getter for the application context in GeckoAppShell. The existing getContext method is misused for both application and activity context, so new methods are added to improve consistency.
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoAppShell.java
mobile/android/base/GeckoView.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -1275,16 +1275,17 @@ public abstract class GeckoApp
 
         // GeckoAppShell is tightly coupled to us, rather than
         // the app context, because various parts of Fennec (e.g.,
         // GeckoScreenOrientation) use GAS to access the Activity in
         // the guise of fetching a Context.
         // When that's fixed, `this` can change to
         // `(GeckoApplication) getApplication()` here.
         GeckoAppShell.setContextGetter(this);
+        GeckoAppShell.setApplicationContext(getApplicationContext());
         GeckoAppShell.setGeckoInterface(this);
 
         Tabs.getInstance().attachToContext(this);
         try {
             Favicons.initializeWithContext(this);
         } catch (Exception e) {
             Log.e(LOGTAG, "Exception starting favicon cache. Corrupt resources?", e);
         }
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -2040,27 +2040,36 @@ public class GeckoAppShell
             Log.w(LOGTAG, "Couldn't find plugin class " + className, cnfe);
             return null;
         } catch (android.content.pm.PackageManager.NameNotFoundException nnfe) {
             Log.w(LOGTAG, "Couldn't find package.", nnfe);
             return null;
         }
     }
 
+    private static Context sApplicationContext;
     private static ContextGetter sContextGetter;
 
     @WrapForJNI(allowMultithread = true)
     public static Context getContext() {
         return sContextGetter.getContext();
     }
 
     public static void setContextGetter(ContextGetter cg) {
         sContextGetter = cg;
     }
 
+    public static Context getApplicationContext() {
+        return sApplicationContext;
+    }
+
+    public static void setApplicationContext(final Context context) {
+        sApplicationContext = context;
+    }
+
     public static SharedPreferences getSharedPreferences() {
         if (sContextGetter == null) {
             throw new IllegalStateException("No ContextGetter; cannot fetch prefs.");
         }
         return sContextGetter.getSharedPreferences();
     }
 
     public interface AppStateListener {
--- a/mobile/android/base/GeckoView.java
+++ b/mobile/android/base/GeckoView.java
@@ -134,16 +134,20 @@ public class GeckoView extends LayerView
         String url = a.getString(R.styleable.GeckoView_url);
         boolean doInit = a.getBoolean(R.styleable.GeckoView_doinit, true);
         a.recycle();
         init(context, url, doInit);
     }
 
     private void init(Context context, String url, boolean doInit) {
 
+        if (GeckoAppShell.getApplicationContext() == null) {
+            GeckoAppShell.setApplicationContext(context.getApplicationContext());
+        }
+
         // Set the GeckoInterface if the context is an activity and the GeckoInterface
         // has not already been set
         if (context instanceof Activity && getGeckoInterface() == null) {
             setGeckoInterface(new BaseGeckoInterface(context));
             GeckoAppShell.setContextGetter(this);
         }
 
         // Perform common initialization for Fennec/GeckoView.