Bug 1331968: Implement the moz: protocol handler to redirect to a fixed website. r=gijs
authorDave Townsend <dtownsend@oxymoronical.com>
Wed, 18 Jan 2017 14:10:46 -0800
changeset 358471 24e88a2d883fff700725490665bce10aeb303551
parent 358470 dbef9f0ee2d5400cc4a01bd8974f389763f2735e
child 358472 487a4e43eb9d1f04a5d8e3dd183fe38dbe105e1f
child 358510 287d743f3b70f8a6d3e66264f2549f6cfc915eeb
push id10621
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 16:02:43 +0000
treeherdermozilla-aurora@dca7b42e6c67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs
bugs1331968
milestone53.0a1
Bug 1331968: Implement the moz: protocol handler to redirect to a fixed website. r=gijs The protocol handler is intentionally simple. It works for the entering into the urlbar case but not a lot else. Included some basic tests. MozReview-Commit-ID: 4FQ1irdt3Nj
browser/installer/package-manifest.in
caps/tests/mochitest/browser_checkloaduri.js
mobile/android/installer/package-manifest.in
toolkit/components/moz.build
toolkit/components/mozprotocol/moz.build
toolkit/components/mozprotocol/mozProtocolHandler.js
toolkit/components/mozprotocol/mozProtocolHandler.manifest
toolkit/components/mozprotocol/tests/browser.ini
toolkit/components/mozprotocol/tests/browser_mozprotocol.js
toolkit/components/mozprotocol/tests/mozprotocol.html
--- a/browser/installer/package-manifest.in
+++ b/browser/installer/package-manifest.in
@@ -439,16 +439,18 @@
 @RESPATH@/components/nsLivemarkService.js
 @RESPATH@/components/nsTaggingService.js
 @RESPATH@/components/UnifiedComplete.js
 @RESPATH@/components/nsPlacesExpiration.js
 @RESPATH@/components/PageIconProtocolHandler.js
 @RESPATH@/components/PlacesCategoriesStarter.js
 @RESPATH@/components/ColorAnalyzer.js
 @RESPATH@/components/PageThumbsProtocol.js
+@RESPATH@/components/mozProtocolHandler.js
+@RESPATH@/components/mozProtocolHandler.manifest
 @RESPATH@/components/nsDefaultCLH.manifest
 @RESPATH@/components/nsDefaultCLH.js
 @RESPATH@/components/nsContentPrefService.manifest
 @RESPATH@/components/nsContentPrefService.js
 @RESPATH@/components/nsContentDispatchChooser.manifest
 @RESPATH@/components/nsContentDispatchChooser.js
 @RESPATH@/components/nsHandlerService.manifest
 @RESPATH@/components/nsHandlerService.js
--- a/caps/tests/mochitest/browser_checkloaduri.js
+++ b/caps/tests/mochitest/browser_checkloaduri.js
@@ -16,58 +16,62 @@ const URLs = new Map([
     ["feed:chrome://foo/content/bar.xul", false, false, false],
     ["view-source:http://www.example2.com", false, false, true],
     ["view-source:https://www.example2.com", false, false, true],
     ["view-source:feed:http://www.example2.com", false, false, true],
     ["feed:view-source:http://www.example2.com", false, false, false],
     ["data:text/html,Hi", true, false, true],
     ["view-source:data:text/html,Hi", false, false, true],
     ["javascript:alert('hi')", true, false, true],
+    ["moz://a", false, false, true],
   ]],
   ["feed:http://www.example.com", [
     ["http://www.example2.com", true, true, true],
     ["feed:http://www.example2.com", true, true, true],
     ["https://www.example2.com", true, true, true],
     ["feed:https://www.example2.com", true, true, true],
     ["chrome://foo/content/bar.xul", false, false, true],
     ["feed:chrome://foo/content/bar.xul", false, false, false],
     ["view-source:http://www.example2.com", false, false, true],
     ["view-source:https://www.example2.com", false, false, true],
     ["view-source:feed:http://www.example2.com", false, false, true],
     ["feed:view-source:http://www.example2.com", false, false, false],
     ["data:text/html,Hi", true, false, true],
     ["view-source:data:text/html,Hi", false, false, true],
     ["javascript:alert('hi')", true, false, true],
+    ["moz://a", false, false, true],
   ]],
   ["view-source:http://www.example.com", [
     ["http://www.example2.com", true, true, true],
     ["feed:http://www.example2.com", false, false, true],
     ["https://www.example2.com", true, true, true],
     ["feed:https://www.example2.com", false, false, true],
     ["chrome://foo/content/bar.xul", false, false, true],
     ["feed:chrome://foo/content/bar.xul", false, false, false],
     ["view-source:http://www.example2.com", true, true, true],
     ["view-source:https://www.example2.com", true, true, true],
     ["view-source:feed:http://www.example2.com", false, false, true],
     ["feed:view-source:http://www.example2.com", false, false, false],
     ["data:text/html,Hi", true, false, true],
     ["view-source:data:text/html,Hi", true, false, true],
     ["javascript:alert('hi')", true, false, true],
+    ["moz://a", false, false, true],
   ]],
   ["about:foo", [
     ["about:foo?", true, true, true],
     ["about:foo?bar", true, true, true],
     ["about:foo#", true, true, true],
     ["about:foo#bar", true, true, true],
     ["about:foo?#", true, true, true],
     ["about:foo?bar#baz", true, true, true],
     ["about:bar", false, false, true],
     ["about:bar?foo#baz", false, false, true],
     ["about:bar?foo", false, false, true],
     ["http://www.example.com/", true, true, true],
+    ["moz://a", false, false, true],
   ]],
 ]);
 
 function testURL(source, target, canLoad, canLoadWithoutInherit, canCreate, flags) {
   let threw = false;
   let targetURI;
   try {
     targetURI = makeURI(target);
--- a/mobile/android/installer/package-manifest.in
+++ b/mobile/android/installer/package-manifest.in
@@ -270,16 +270,18 @@
 @BINPATH@/components/PushComponents.js
 #endif
 @BINPATH@/components/BrowserElementParent.manifest
 @BINPATH@/components/BrowserElementParent.js
 @BINPATH@/components/FeedProcessor.manifest
 @BINPATH@/components/FeedProcessor.js
 @BINPATH@/components/WellKnownOpportunisticUtils.js
 @BINPATH@/components/WellKnownOpportunisticUtils.manifest
+@BINPATH@/components/mozProtocolHandler.js
+@BINPATH@/components/mozProtocolHandler.manifest
 @BINPATH@/components/nsDNSServiceDiscovery.manifest
 @BINPATH@/components/nsDNSServiceDiscovery.js
 @BINPATH@/components/toolkitsearch.manifest
 @BINPATH@/components/nsSearchService.js
 @BINPATH@/components/nsSidebar.js
 @BINPATH@/components/passwordmgr.manifest
 @BINPATH@/components/nsLoginInfo.js
 @BINPATH@/components/nsLoginManager.js
--- a/toolkit/components/moz.build
+++ b/toolkit/components/moz.build
@@ -30,16 +30,17 @@ DIRS += [
     'finalizationwitness',
     'formautofill',
     'find',
     'gfx',
     'jsdownloads',
     'lz4',
     'mediasniffer',
     'microformats',
+    'mozprotocol',
     'osfile',
     'parentalcontrols',
     'passwordmgr',
     'perf',
     'perfmonitoring',
     'places',
     'privatebrowsing',
     'processsingleton',
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/moz.build
@@ -0,0 +1,14 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+EXTRA_COMPONENTS += [
+    'mozProtocolHandler.js',
+    'mozProtocolHandler.manifest',
+]
+
+BROWSER_CHROME_MANIFESTS += [
+    'tests/browser.ini'
+]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/mozProtocolHandler.js
@@ -0,0 +1,48 @@
+/* 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/. */
+"use strict";
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/NetUtil.jsm");
+
+function mozProtocolHandler() {
+  XPCOMUtils.defineLazyPreferenceGetter(this, "urlToLoad", "toolkit.mozprotocol.url",
+                                        "https://www.mozilla.org/protocol");
+}
+
+mozProtocolHandler.prototype = {
+  scheme: "moz",
+  defaultPort: -1,
+  protocolFlags: Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD,
+
+  newURI(spec, charset, base) {
+    let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
+    if (base) {
+      uri.spec = base.resolve(spec);
+    } else {
+      uri.spec = spec;
+    }
+    return uri;
+  },
+
+  newChannel2(uri, loadInfo) {
+    let realURL = NetUtil.newURI(this.urlToLoad);
+    let channel = Services.io.newChannelFromURIWithLoadInfo(realURL, loadInfo)
+    channel.loadFlags |= Ci.nsIChannel.LOAD_REPLACE;
+    return channel;
+  },
+
+  newChannel(uri) {
+    return this.newChannel(uri, null);
+  },
+
+  classID: Components.ID("{47a45e5f-691e-4799-8686-14f8d3fc0f8c}"),
+
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([mozProtocolHandler]);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/mozProtocolHandler.manifest
@@ -0,0 +1,2 @@
+component {47a45e5f-691e-4799-8686-14f8d3fc0f8c} mozProtocolHandler.js
+contract @mozilla.org/network/protocol;1?name=moz {47a45e5f-691e-4799-8686-14f8d3fc0f8c}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/tests/browser.ini
@@ -0,0 +1,5 @@
+[DEFAULT]
+
+[browser_mozprotocol.js]
+support-files = 
+  mozprotocol.html
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/tests/browser_mozprotocol.js
@@ -0,0 +1,14 @@
+// Check that entering moz://a into the address bar directs us to a new url
+add_task(function*() {
+  let path = getRootDirectory(gTestPath).substring("chrome://mochitests/content/".length);
+  yield SpecialPowers.pushPrefEnv({
+    set: [["toolkit.mozprotocol.url", `https://example.com/${path}mozprotocol.html`]],
+  });
+
+  yield BrowserTestUtils.withNewTab("about:blank", function*() {
+    gBrowser.loadURI("moz://a");
+    yield BrowserTestUtils.waitForLocationChange(gBrowser,
+      `https://example.com/${path}mozprotocol.html`);
+    ok(true, "Made it to the expected page");
+  });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/mozprotocol/tests/mozprotocol.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+
+<html>
+<body>
+<p><a id="link" href="moz://a">Test</a></p>
+</body>
+</html>