Bug 1319452: Part 1 - Load background page in private browsing mode during permanent private browsing. r=billm
authorKris Maglione <maglione.k@gmail.com>
Tue, 22 Nov 2016 12:07:59 -0800
changeset 323910 926f21451281f50f9172366b534a684ab85f64eb
parent 323909 03acde1081c3280af1c70ef6d7f57243b720ce87
child 323911 1846a78b62ff79f695c81c741cb1f15c9c2cda28
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersbillm
bugs1319452
milestone53.0a1
Bug 1319452: Part 1 - Load background page in private browsing mode during permanent private browsing. r=billm MozReview-Commit-ID: KXA7xlq8Voc
toolkit/components/extensions/ext-backgroundPage.js
toolkit/components/extensions/test/xpcshell/test_ext_background_private_browsing.js
toolkit/components/extensions/test/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/ext-backgroundPage.js
+++ b/toolkit/components/extensions/ext-backgroundPage.js
@@ -2,16 +2,18 @@
 
 var {interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
                                   "resource://gre/modules/AddonManager.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
+                                  "resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 const {
   promiseDocumentLoaded,
   promiseEvent,
   promiseObserved,
 } = ExtensionUtils;
 
@@ -87,16 +89,22 @@ class BackgroundPageBase {
         }
       }
 
       this.extension.emit("startup");
     }.bind(this));
   }
 
   initParentWindow(chromeShell) {
+    if (PrivateBrowsingUtils.permanentPrivateBrowsing) {
+      let attrs = chromeShell.getOriginAttributes();
+      attrs.privateBrowsingId = 1;
+      chromeShell.setOriginAttributes(attrs);
+    }
+
     let system = Services.scriptSecurityManager.getSystemPrincipal();
     chromeShell.createAboutBlankContentViewer(system);
     chromeShell.useGlobalHistory = false;
     chromeShell.loadURI(XUL_URL, 0, null, null, null);
 
     return promiseObserved("chrome-document-global-created",
                            win => win.document == chromeShell.document);
   }
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_background_private_browsing.js
@@ -0,0 +1,40 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+Cu.import("resource://gre/modules/Preferences.jsm");
+
+function* testBackgroundPage(expected) {
+  let extension = ExtensionTestUtils.loadExtension({
+    async background() {
+      browser.test.assertEq(window, browser.extension.getBackgroundPage(),
+                            "Caller should be able to access itself as a background page");
+      browser.test.assertEq(window, await browser.runtime.getBackgroundPage(),
+                            "Caller should be able to access itself as a background page");
+
+      browser.test.sendMessage("incognito", browser.extension.inIncognitoContext);
+    },
+  });
+
+  yield extension.startup();
+
+  let incognito = yield extension.awaitMessage("incognito");
+  equal(incognito, expected.incognito, "Expected incognito value");
+
+  yield extension.unload();
+}
+
+add_task(function* test_background_incognito() {
+  do_print("Test background page incognito value with permanent private browsing disabled");
+
+  yield testBackgroundPage({incognito: false});
+
+  do_print("Test background page incognito value with permanent private browsing enabled");
+
+  Preferences.set("browser.privatebrowsing.autostart", true);
+  do_register_cleanup(() => {
+    Preferences.reset("browser.privatebrowsing.autostart");
+  });
+
+  yield testBackgroundPage({incognito: true});
+});
--- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini
@@ -14,16 +14,17 @@ tags = webextensions
 [test_ext_alarms_periodic.js]
 [test_ext_alarms_replaces.js]
 [test_ext_apimanager.js]
 [test_ext_api_permissions.js]
 [test_ext_background_generated_load_events.js]
 [test_ext_background_generated_reload.js]
 [test_ext_background_global_history.js]
 skip-if = os == "android" # Android does not use Places for history.
+[test_ext_background_private_browsing.js]
 [test_ext_background_runtime_connect_params.js]
 [test_ext_background_sub_windows.js]
 [test_ext_background_window_properties.js]
 skip-if = os == "android"
 [test_ext_contexts.js]
 [test_ext_downloads.js]
 [test_ext_downloads_download.js]
 skip-if = os == "android"