Bug 1488914 - Use about:home / about:privatebrowsing in windows.create r=rpl,dao
☠☠ backed out by 04fbac02c101 ☠ ☠
authorRob Wu <rob@robwu.nl>
Thu, 11 Oct 2018 14:14:50 +0000
changeset 499168 c844033d55eb91acb4190ee202361b1b95062e0c
parent 499155 8d8c2a0e01b424c7719f76f553ff2923f1ddce69
child 499169 bb2685bc742eec021c972c59cf74e564780e307d
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrpl, dao
bugs1488914
milestone64.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 1488914 - Use about:home / about:privatebrowsing in windows.create r=rpl,dao Use a homepage URL instead of a new tab URL by default in browser.windows.create. Differential Revision: https://phabricator.services.mozilla.com/D6030
browser/components/extensions/parent/ext-windows.js
browser/components/extensions/test/browser/browser_ext_tabs_create_url.js
--- a/browser/components/extensions/parent/ext-windows.js
+++ b/browser/components/extensions/parent/ext-windows.js
@@ -1,15 +1,14 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
-XPCOMUtils.defineLazyServiceGetter(this, "aboutNewTabService",
-                                   "@mozilla.org/browser/aboutnewtab-service;1",
-                                   "nsIAboutNewTabService");
+ChromeUtils.defineModuleGetter(this, "HomePage",
+                               "resource:///modules/HomePage.jsm");
 ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
                                "resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 var {
   promiseObserved,
 } = ExtensionUtils;
 
 /**
@@ -147,19 +146,16 @@ this.windows = class extends ExtensionAP
             let tab = tabTracker.getTab(createData.tabId);
 
             // Private browsing tabs can only be moved to private browsing
             // windows.
             let incognito = PrivateBrowsingUtils.isBrowserPrivate(tab.linkedBrowser);
             if (createData.incognito !== null && createData.incognito != incognito) {
               return Promise.reject({message: "`incognito` property must match the incognito state of tab"});
             }
-            if (createData.incognito && !PrivateBrowsingUtils.enabled) {
-              return Promise.reject({message: "`incognito` cannot be used if incognito mode is disabled"});
-            }
             createData.incognito = incognito;
 
             if (createData.cookieStoreId && createData.cookieStoreId !== getCookieStoreIdForTab(createData, tab)) {
               return Promise.reject({message: "`cookieStoreId` must match the tab's cookieStoreId"});
             }
 
             args.appendElement(tab);
           } else if (createData.url !== null) {
@@ -168,22 +164,24 @@ this.windows = class extends ExtensionAP
               for (let url of createData.url) {
                 array.appendElement(mkstr(url));
               }
               args.appendElement(array);
             } else {
               args.appendElement(mkstr(createData.url));
             }
           } else {
-            let url = aboutNewTabService.newTabURL;
+            let url = createData.incognito && !PrivateBrowsingUtils.permanentPrivateBrowsing ?
+              "about:privatebrowsing" : HomePage.get().split("|", 1)[0];
             args.appendElement(mkstr(url));
 
-            if (url === "about:newtab") {
-              // The extension principal cannot directly load about:newtab,
-              // so use the system principal instead.
+            if (url.startsWith("about:") &&
+                !context.checkLoadURL(url, {dontReportErrors: true})) {
+              // The extension principal cannot directly load about:-URLs,
+              // except for about:blank. So use the system principal instead.
               principal = Services.scriptSecurityManager.getSystemPrincipal();
             }
           }
 
           args.appendElement(null); // unused
           args.appendElement(null); // referrer
           args.appendElement(null); // postData
           args.appendElement(null); // allowThirdPartyFixup
@@ -208,16 +206,19 @@ this.windows = class extends ExtensionAP
             features.push("dialog=no", "all");
           } else {
             // All other types create "popup"-type windows by default.
             features.push("dialog", "resizable", "minimizable", "centerscreen", "titlebar", "close");
           }
 
           if (createData.incognito !== null) {
             if (createData.incognito) {
+              if (!PrivateBrowsingUtils.enabled) {
+                return Promise.reject({message: "`incognito` cannot be used if incognito mode is disabled"});
+              }
               features.push("private");
             } else {
               features.push("non-private");
             }
           }
 
           let {allowScriptsToClose, url} = createData;
           if (allowScriptsToClose === null) {
--- a/browser/components/extensions/test/browser/browser_ext_tabs_create_url.js
+++ b/browser/components/extensions/test/browser/browser_ext_tabs_create_url.js
@@ -1,12 +1,32 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
+async function runWithDisabledPrivateBrowsing(callback) {
+  const {
+    EnterprisePolicyTesting,
+    PoliciesPrefTracker,
+  } = ChromeUtils.import("resource://testing-common/EnterprisePolicyTesting.jsm", {});
+
+  PoliciesPrefTracker.start();
+  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
+    policies: {DisablePrivateBrowsing: true},
+  });
+
+  try {
+    await callback();
+  } finally {
+    await EnterprisePolicyTesting.setupPolicyEngineWithJson("");
+    EnterprisePolicyTesting.resetRunOnceState();
+    PoliciesPrefTracker.stop();
+  }
+}
+
 add_task(async function test_urlbar_focus() {
   // Disable preloaded new tab because the urlbar is automatically focused when
   // a preloaded new tab is opened, while this test is supposed to test that the
   // implementation of tabs.create automatically focuses the urlbar of new tabs.
   await SpecialPowers.pushPrefEnv({
     set: [["browser.newtab.preload", false]],
   });
 
@@ -55,8 +75,117 @@ add_task(async function test_urlbar_focu
   is(active.tagName, "html:input", "Input element focused");
   ok(active.classList.contains("urlbar-input"), "Urlbar focused");
 
   extension.sendMessage("remove", tab2.id);
   await extension.awaitMessage("result");
 
   await extension.unload();
 });
+
+add_task(async function default_url() {
+  const extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: ["tabs"],
+    },
+    background() {
+      function promiseNonBlankTab() {
+        return new Promise(resolve => {
+          browser.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) {
+            if (changeInfo.status === "complete" && tab.url !== "about:blank") {
+              browser.tabs.onUpdated.removeListener(listener);
+              resolve(tab);
+            }
+          });
+        });
+      }
+
+      browser.test.onMessage.addListener(async (msg, {incognito, expectedNewWindowUrl, expectedNewTabUrl}) => {
+        browser.test.assertEq("start", msg, `Start test, incognito=${incognito}`);
+
+        let tabPromise = promiseNonBlankTab();
+        let win;
+        try {
+          win = await browser.windows.create({incognito});
+          browser.test.assertEq(1, win.tabs.length, "Expected one tab in the new window.");
+        } catch (e) {
+          browser.test.assertEq(expectedNewWindowUrl, e.message, "Expected error");
+          browser.test.sendMessage("done");
+          return;
+        }
+        let tab = await tabPromise;
+        browser.test.assertEq(expectedNewWindowUrl, tab.url, "Expected default URL of new window");
+
+        tabPromise = promiseNonBlankTab();
+        await browser.tabs.create({windowId: win.id});
+        tab = await tabPromise;
+        browser.test.assertEq(expectedNewTabUrl, tab.url, "Expected default URL of new tab");
+
+        await browser.windows.remove(win.id);
+        browser.test.sendMessage("done");
+      });
+    },
+  });
+
+  await extension.startup();
+
+  extension.sendMessage("start", {
+    incognito: false,
+    expectedNewWindowUrl: "about:home",
+    expectedNewTabUrl: "about:newtab",
+  });
+  await extension.awaitMessage("done");
+  extension.sendMessage("start", {
+    incognito: true,
+    expectedNewWindowUrl: "about:privatebrowsing",
+    expectedNewTabUrl: "about:privatebrowsing",
+  });
+  await extension.awaitMessage("done");
+
+  info("Testing with multiple homepages.");
+  await SpecialPowers.pushPrefEnv({set: [["browser.startup.homepage", "about:robots|about:blank|about:home"]]});
+  extension.sendMessage("start", {
+    incognito: false,
+    expectedNewWindowUrl: "about:robots",
+    expectedNewTabUrl: "about:newtab",
+  });
+  await extension.awaitMessage("done");
+  extension.sendMessage("start", {
+    incognito: true,
+    expectedNewWindowUrl: "about:privatebrowsing",
+    expectedNewTabUrl: "about:privatebrowsing",
+  });
+  await extension.awaitMessage("done");
+  await SpecialPowers.popPrefEnv();
+
+  info("Testing with perma-private browsing mode.");
+  await SpecialPowers.pushPrefEnv({set: [["browser.privatebrowsing.autostart", true]]});
+  extension.sendMessage("start", {
+    incognito: false,
+    expectedNewWindowUrl: "about:home",
+    expectedNewTabUrl: "about:newtab",
+  });
+  await extension.awaitMessage("done");
+  extension.sendMessage("start", {
+    incognito: true,
+    expectedNewWindowUrl: "about:home",
+    expectedNewTabUrl: "about:newtab",
+  });
+  await extension.awaitMessage("done");
+  await SpecialPowers.popPrefEnv();
+
+  info("Testing with disabled private browsing mode.");
+  await runWithDisabledPrivateBrowsing(async () => {
+    extension.sendMessage("start", {
+      incognito: false,
+      expectedNewWindowUrl: "about:home",
+      expectedNewTabUrl: "about:newtab",
+    });
+    await extension.awaitMessage("done");
+    extension.sendMessage("start", {
+      incognito: true,
+      expectedNewWindowUrl: "`incognito` cannot be used if incognito mode is disabled",
+    });
+    await extension.awaitMessage("done");
+  });
+
+  await extension.unload();
+});