Bug 1439615 - [1.0] Move nsILoadURIDelegate handling to content module. r=jchen
authorEugen Sawin <esawin@mozilla.com>
Tue, 27 Feb 2018 23:04:05 +0100
changeset 405765 120334ac0e535522a7544773b06345ca94b0c64b
parent 405764 dcadcf486b78e82cb0cbf716be72805a489fc9a4
child 405766 f248db9bdb5c5fae6bc7f1fcede2eff6dc4808e8
push id100292
push useresawin@mozilla.com
push dateWed, 28 Feb 2018 20:04:20 +0000
treeherdermozilla-inbound@f248db9bdb5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1439615
milestone60.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 1439615 - [1.0] Move nsILoadURIDelegate handling to content module. r=jchen
mobile/android/chrome/geckoview/GeckoViewNavigationContent.js
mobile/android/chrome/geckoview/jar.mn
mobile/android/modules/geckoview/GeckoViewNavigation.jsm
new file mode 100644
--- /dev/null
+++ b/mobile/android/chrome/geckoview/GeckoViewNavigationContent.js
@@ -0,0 +1,62 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* 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/. */
+
+ChromeUtils.import("resource://gre/modules/GeckoViewContentModule.jsm");
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  Services: "resource://gre/modules/Services.jsm",
+});
+
+XPCOMUtils.defineLazyGetter(this, "dump", () =>
+    ChromeUtils.import("resource://gre/modules/AndroidLog.jsm",
+                       {}).AndroidLog.d.bind(null, "ViewNavigationContent"));
+
+function debug(aMsg) {
+  // dump(aMsg);
+}
+
+class GeckoViewNavigationContent extends GeckoViewContentModule {
+  register() {
+    debug("register");
+
+    docShell.loadURIDelegate = this;
+  }
+
+  unregister() {
+    debug("unregister");
+
+    docShell.loadURIDelegate = null;
+  }
+
+  // nsILoadURIDelegate.
+  loadURI(aUri, aWhere, aFlags, aTriggeringPrincipal) {
+    debug("loadURI " + (aUri && aUri.spec) + " " + aWhere + " " + aFlags);
+
+    let message = {
+      type: "GeckoView:OnLoadUri",
+      uri: aUri ? aUri.displaySpec : "",
+      where: aWhere,
+      flags: aFlags
+    };
+
+    debug("dispatch " + JSON.stringify(message));
+
+    let handled = undefined;
+    this.eventDispatcher.sendRequestForResult(message).then(response => {
+      handled = response;
+    }, () => {
+      // There was an error or listener was not registered in GeckoSession, treat as unhandled.
+      handled = false;
+    });
+    Services.tm.spinEventLoopUntil(() => handled !== undefined);
+
+    if (!handled) {
+      throw Cr.NS_ERROR_ABORT;
+    }
+  }
+}
+
+var navigationListener = new GeckoViewNavigationContent("GeckoViewNavigation", this);
--- a/mobile/android/chrome/geckoview/jar.mn
+++ b/mobile/android/chrome/geckoview/jar.mn
@@ -3,9 +3,10 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 geckoview.jar:
 % content geckoview %content/
 
   content/geckoview.xul
   content/geckoview.js
   content/GeckoViewContent.js
+  content/GeckoViewNavigationContent.js
   content/GeckoViewScrollContent.js
--- a/mobile/android/modules/geckoview/GeckoViewNavigation.jsm
+++ b/mobile/android/modules/geckoview/GeckoViewNavigation.jsm
@@ -26,18 +26,18 @@ function debug(aMsg) {
 // Handles GeckoView:GoBack and :GoForward requests dispatched by
 // GeckoView.goBack and .goForward.
 // Dispatches GeckoView:LocationChange to the GeckoView on location change when
 // active.
 // Implements nsIBrowserDOMWindow.
 // Implements nsILoadURIDelegate.
 class GeckoViewNavigation extends GeckoViewModule {
   init() {
+    this._frameScriptLoaded = false;
     this.window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow = this;
-    this.browser.docShell.loadURIDelegate = this;
 
     this.eventDispatcher.registerListener(this, [
       "GeckoView:GoBack",
       "GeckoView:GoForward",
       "GeckoView:LoadUri",
       "GeckoView:Reload",
       "GeckoView:Stop"
     ]);
@@ -66,46 +66,16 @@ class GeckoViewNavigation extends GeckoV
     }
   }
 
   // Message manager event handler.
   receiveMessage(aMsg) {
     debug("receiveMessage " + aMsg.name);
   }
 
-  handleLoadUri(aUri, aOpener, aWhere, aFlags, aTriggeringPrincipal) {
-    debug("handleOpenURI: aUri=" + (aUri && aUri.spec) +
-          " aWhere=" + aWhere +
-          " aFlags=" + aFlags);
-
-    if (!this.isRegistered) {
-      return false;
-    }
-
-    let message = {
-      type: "GeckoView:OnLoadUri",
-      uri: aUri ? aUri.displaySpec : "",
-      where: aWhere,
-      flags: aFlags
-    };
-
-    debug("dispatch " + JSON.stringify(message));
-
-    let handled = undefined;
-    this.eventDispatcher.sendRequestForResult(message).then(response => {
-      handled = response;
-    }, () => {
-      // There was an error or listener was not registered in GeckoSession, treat as unhandled.
-      handled = false;
-    });
-    Services.tm.spinEventLoopUntil(() => handled !== undefined);
-
-    return handled;
-  }
-
   waitAndSetOpener(aSessionId, aOpener) {
     if (!aSessionId) {
       return Promise.resolve(null);
     }
 
     return new Promise(resolve => {
       const handler = {
         observe(aSubject, aTopic, aData) {
@@ -145,28 +115,16 @@ class GeckoViewNavigation extends GeckoV
       browser = null;
     });
 
     // Wait indefinitely for app to respond with a browser or null
     Services.tm.spinEventLoopUntil(() => browser !== undefined);
     return browser;
   }
 
-  // nsILoadURIDelegate.
-  loadURI(aUri, aWhere, aFlags, aTriggeringPrincipal) {
-    debug("loadURI " + aUri + " " + aWhere + " " + aFlags + " " +
-          aTriggeringPrincipal);
-
-    const handled = this.handleLoadUri(aUri, null, aWhere, aFlags,
-                                       aTriggeringPrincipal);
-    if (!handled) {
-      throw Cr.NS_ERROR_ABORT;
-    }
-  }
-
   // nsIBrowserDOMWindow.
   createContentWindow(aUri, aOpener, aWhere, aFlags, aTriggeringPrincipal) {
     debug("createContentWindow: aUri=" + (aUri && aUri.spec) +
           " aWhere=" + aWhere +
           " aFlags=" + aFlags);
 
     const browser = this.handleNewSession(aUri, aOpener, aWhere, aFlags,
                                           aTriggeringPrincipal);
@@ -212,16 +170,22 @@ class GeckoViewNavigation extends GeckoV
   canClose() {
     debug("canClose");
     return false;
   }
 
   register() {
     debug("register");
 
+    if (!this._frameScriptLoaded) {
+      this.messageManager.loadFrameScript(
+        "chrome://geckoview/content/GeckoViewNavigationContent.js", true);
+      this._frameScriptLoaded = true;
+    }
+
     let flags = Ci.nsIWebProgress.NOTIFY_LOCATION;
     this.progressFilter =
       Cc["@mozilla.org/appshell/component/browser-status-filter;1"]
       .createInstance(Ci.nsIWebProgress);
     this.progressFilter.addProgressListener(this, flags);
     this.browser.addProgressListener(this.progressFilter, flags);
   }