Bug 1322576 - [2.4] Add GeckoView settings management. r=snorp,jchen
authorEugen Sawin <esawin@mozilla.com>
Tue, 14 Feb 2017 19:51:43 +0100
changeset 373328 dd8000832878e2f2f4f066a295c81b02b94f1c0d
parent 373327 7552fa4a2858a2ca6a51640dcc85133a9537085c
child 373329 162492fcf4a9ecad41d71ff55c8a9341f44411bc
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, jchen
bugs1322576
milestone54.0a1
Bug 1322576 - [2.4] Add GeckoView settings management. r=snorp,jchen
mobile/android/base/moz.build
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoView.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoViewSettings.java
mobile/android/modules/GeckoViewModule.jsm
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -254,16 +254,17 @@ gvjar.sources += [geckoview_source_dir +
     'GeckoProfile.java',
     'GeckoProfileDirectories.java',
     'GeckoScreenOrientation.java',
     'GeckoSharedPrefs.java',
     'GeckoThread.java',
     'GeckoView.java',
     'GeckoViewChrome.java',
     'GeckoViewFragment.java',
+    'GeckoViewSettings.java',
     'gfx/BitmapUtils.java',
     'gfx/BufferedImage.java',
     'gfx/BufferedImageGLInfo.java',
     'gfx/DynamicToolbarAnimator.java',
     'gfx/FloatSize.java',
     'gfx/FullScreenState.java',
     'gfx/GeckoLayerClient.java',
     'gfx/ImmutableViewportMetrics.java',
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoView.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoView.java
@@ -46,16 +46,18 @@ public class GeckoView extends LayerView
     private final EventDispatcher eventDispatcher = new EventDispatcher();
 
     private ChromeDelegate mChromeDelegate;
     /* package */ ContentListener mContentListener;
     /* package */ NavigationListener mNavigationListener;
     /* package */ ProgressListener mProgressListener;
     private InputConnectionListener mInputConnectionListener;
 
+    private GeckoViewSettings mSettings;
+
     protected boolean onAttachedToWindowCalled;
     protected String chromeURI;
     protected int screenId = 0; // default to the primary screen
 
     @WrapForJNI(dispatchTo = "proxy")
     protected static final class Window extends JNIObject {
         @WrapForJNI(skip = true)
         /* package */ Window() {}
@@ -186,16 +188,18 @@ public class GeckoView extends LayerView
             GeckoAppShell.setContextGetter(this);
         }
 
         // Perform common initialization for Fennec/GeckoView.
         GeckoAppShell.setLayerView(this);
 
         initializeView();
         listener.registerListeners();
+
+        mSettings = new GeckoViewSettings(getEventDispatcher());
     }
 
     @Override
     protected Parcelable onSaveInstanceState()
     {
         final Parcelable superState = super.onSaveInstanceState();
         stateSaved = true;
         return new StateBinder(superState, this.window);
@@ -322,16 +326,20 @@ public class GeckoView extends LayerView
 
     /**
     * Go forward in history.
     */
     public void goForward() {
         eventDispatcher.dispatch("GeckoView:GoForward", null);
     }
 
+    public GeckoViewSettings getSettings() {
+        return mSettings;
+    }
+
     @Override
     public Handler getHandler() {
         if (mInputConnectionListener != null) {
             return mInputConnectionListener.getHandler(super.getHandler());
         }
         return super.getHandler();
     }
 
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoViewSettings.java
@@ -0,0 +1,62 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * vim: ts=4 sw=4 expandtab:
+ * 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.util.GeckoBundle;
+
+import android.util.Log;
+
+public final class GeckoViewSettings {
+    private static final String LOGTAG = "GeckoViewSettings";
+    private static final boolean DEBUG = false;
+
+    private static class Key<T> {
+        private final String text;
+
+        public Key(final String text) {
+            this.text = text;
+        }
+    }
+
+    public static final Key<Boolean> USE_TRACKING_PROTECTION =
+        new Key<Boolean>("useTrackingProtection");
+
+    private final EventDispatcher mEventDispatcher;
+    private final GeckoBundle mBundle;
+
+    /* package */ GeckoViewSettings(EventDispatcher eventDispatcher) {
+        mEventDispatcher = eventDispatcher;
+        mBundle = new GeckoBundle();
+
+        setBoolean(USE_TRACKING_PROTECTION, false);
+    }
+
+    public void setBoolean(Key<Boolean> key, boolean value) {
+        synchronized (mBundle) {
+            final Object old = mBundle.get(key.text);
+            if (old != null && old.equals(value)) {
+                return;
+            }
+            mBundle.putBoolean(key.text, value);
+        }
+        dispatchUpdate();
+    }
+
+    public Object getBoolean(Key<Boolean> key) {
+        synchronized (mBundle) {
+            return mBundle.getBoolean(key.text);
+        }
+    }
+
+    /* package */ GeckoBundle asBundle() {
+        return mBundle;
+    }
+
+    private void dispatchUpdate() {
+        mEventDispatcher.dispatch("GeckoView:UpdateSettings", null);
+    }
+}
--- a/mobile/android/modules/GeckoViewModule.jsm
+++ b/mobile/android/modules/GeckoViewModule.jsm
@@ -3,23 +3,44 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 this.EXPORTED_SYMBOLS = ["GeckoViewModule"];
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
+var dump = Cu.import("resource://gre/modules/AndroidLog.jsm", {})
+           .AndroidLog.d.bind(null, "ViewModule");
+
+function debug(msg) {
+  // dump(msg);
+}
+
 class GeckoViewModule {
   constructor(window, browser, eventDispatcher) {
     this.window = window;
     this.browser = browser;
     this.eventDispatcher = eventDispatcher;
 
+    this.eventDispatcher.registerListener(
+      () => this.onSettingsUpdate(),
+      "GeckoView:UpdateSettings");
+
     this.init();
+    this.onSettingsUpdate();
   }
 
+  // Override this with module initialization.
   init() {}
 
+  // Called when settings have changed. Access settings via this.settings.
+  onSettingsUpdate() {}
+
+  get settings() {
+    let view = this.window.arguments[0].QueryInterface(Ci.nsIAndroidView);
+    return view.settings;
+  }
+
   get messageManager() {
     return this.browser.messageManager;
   }
 }