Bug 1453143 - [3.2] Add support for pref-based mutable and static runtime settings. r=jchen
authorEugen Sawin <esawin@mozilla.com>
Tue, 10 Apr 2018 23:14:32 +0200
changeset 466847 a3b3732bb802af9d1364dcd5d8b901cc3a24ef0f
parent 466846 4d56446fd614f8b4e2beb8466bfe1a13b170bd81
child 466848 88aab44edada90cbf1d4a353a3e74d5ec3a9b6ab
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1453143
milestone61.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 1453143 - [3.2] Add support for pref-based mutable and static runtime settings. r=jchen
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java
@@ -11,16 +11,17 @@ import android.os.Parcelable;
 import android.content.Context;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.util.Log;
 
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoThread;
+import org.mozilla.gecko.PrefsHelper;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 
 public final class GeckoRuntime implements Parcelable {
     private static final String LOGTAG = "GeckoRuntime";
     private static final boolean DEBUG = false;
 
@@ -90,16 +91,19 @@ public final class GeckoRuntime implemen
             if (!GeckoThread.launch()) {
                 Log.d(LOGTAG, "init failed (GeckoThread already launched)");
                 return false;
             }
             mSettings = settings;
 
             // Bug 1453062 -- the EventDispatcher should really live here (or in GeckoThread)
             EventDispatcher.getInstance().registerUiThreadListener(mEventListener, "Gecko:Exited");
+
+            mSettings.runtime = this;
+            mSettings.flush();
             return true;
         }
         Log.d(LOGTAG, "init failed (could not initiate GeckoThread)");
         return false;
     }
 
     /**
      * Create a new runtime with default settings and attach it to the given
@@ -181,16 +185,20 @@ public final class GeckoRuntime implemen
     public @Nullable Delegate getDelegate() {
         return mDelegate;
     }
 
     public GeckoRuntimeSettings getSettings() {
         return mSettings;
     }
 
+    /* package */ void setPref(final String name, final Object value) {
+        PrefsHelper.setPref(name, value, /* flush */ false);
+    }
+
     @Override // Parcelable
     public int describeContents() {
         return 0;
     }
 
     @Override // Parcelable
     public void writeToParcel(Parcel out, int flags) {
         out.writeParcelable(mSettings, flags);
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntimeSettings.java
@@ -62,29 +62,76 @@ public final class GeckoRuntimeSettings 
          * @param extras The Gecko intent extras.
          */
         public @NonNull Builder extras(final @NonNull Bundle extras) {
             mSettings.mExtras = extras;
             return this;
         }
     }
 
+    /* package */ GeckoRuntime runtime;
     /* package */ boolean mUseContentProcess;
     /* package */ String[] mArgs;
     /* package */ Bundle mExtras;
+    /* package */ int prefCount;
+
+    private class Pref<T> {
+        public final String name;
+        public final T defaultValue;
+        private T value;
+
+        public Pref(final String name, final T defaultValue) {
+            GeckoRuntimeSettings.this.prefCount++;
+
+            this.name = name;
+            this.defaultValue = defaultValue;
+            value = defaultValue;
+        }
+
+        public void set(T newValue) {
+            value = newValue;
+            flush();
+        }
+
+        public T get() {
+            return value;
+        }
+
+        public void flush() {
+            if (GeckoRuntimeSettings.this.runtime != null) {
+                GeckoRuntimeSettings.this.runtime.setPref(name, value);
+            }
+        }
+    }
+
+    private final Pref<?>[] mPrefs = new Pref<?>[] {};
 
     /* package */ GeckoRuntimeSettings() {
-        mArgs = new String[0];
-        mExtras = new Bundle();
+        this(null);
     }
 
-    /* package */ GeckoRuntimeSettings(final @NonNull GeckoRuntimeSettings settings) {
-        mUseContentProcess = settings.getUseContentProcessHint();
-        mArgs = settings.getArguments().clone();
-        mExtras = new Bundle(settings.getExtras());
+    /* package */ GeckoRuntimeSettings(final @Nullable GeckoRuntimeSettings settings) {
+        if (BuildConfig.DEBUG && prefCount != mPrefs.length) {
+            throw new AssertionError("Add new pref to prefs list");
+        }
+
+        if (settings == null) {
+            mArgs = new String[0];
+            mExtras = new Bundle();
+        } else {
+            mUseContentProcess = settings.getUseContentProcessHint();
+            mArgs = settings.getArguments().clone();
+            mExtras = new Bundle(settings.getExtras());
+        }
+    }
+
+    /* package */ void flush() {
+        for (final Pref<?> pref: mPrefs) {
+            pref.flush();
+        }
     }
 
     /**
      * Get the content process hint flag.
      *
      * @return The content process hint flag.
      */
     public boolean getUseContentProcessHint() {