Bug 1522053 - Copy global permissions into new browsers r=johannh
authorDale Harvey <dale@arandomurl.com>
Thu, 28 Feb 2019 20:17:18 +0000
changeset 519698 56f40b43c0d4c3331e867f2a2cbd00ef3bb28591
parent 519697 ec57fe6f4e173478cfe52b69481db7a2f8145adc
child 519699 4ee6e4e37189ee6b1753df35f0fd64e629740c5b
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1522053
milestone67.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 1522053 - Copy global permissions into new browsers r=johannh Differential Revision: https://phabricator.services.mozilla.com/D21279
browser/base/content/test/permissions/browser_autoplay_blocked.js
browser/modules/SitePermissions.jsm
--- a/browser/base/content/test/permissions/browser_autoplay_blocked.js
+++ b/browser/base/content/test/permissions/browser_autoplay_blocked.js
@@ -1,14 +1,17 @@
 /*
  * Test that a blocked request to autoplay media is shown to the user
  */
 
 const AUTOPLAY_PAGE  = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.com") + "browser_autoplay_blocked.html";
 
+const AUTOPLAY_PREF = "media.autoplay.default";
+const AUTOPLAY_PERM = "autoplay-media";
+
 function openIdentityPopup() {
   let promise = BrowserTestUtils.waitForEvent(gIdentityHandler._identityPopup, "popupshown");
   gIdentityHandler._identityBox.click();
   return promise;
 }
 
 function closeIdentityPopup() {
   let promise = BrowserTestUtils.waitForEvent(gIdentityHandler._identityPopup, "popuphidden");
@@ -16,45 +19,56 @@ function closeIdentityPopup() {
   return promise;
 }
 
 function autoplayBlockedIcon() {
   return document.querySelector("#blocked-permissions-container " +
                                 ".blocked-permission-icon.autoplay-media-icon");
 }
 
+async function blockedIconShown(browser) {
+  // May need to wait for `GloballyAutoplayBlocked` event before showing icon.
+  if (BrowserTestUtils.is_hidden(autoplayBlockedIcon())) {
+    await BrowserTestUtils.waitForEvent(browser, "GloballyAutoplayBlocked");
+  }
+  ok(!BrowserTestUtils.is_hidden(autoplayBlockedIcon()), "Blocked icon is shown");
+}
+
+add_task(async function setup() {
+  registerCleanupFunction(() => {
+    Services.perms.removeAll();
+    Services.prefs.clearUserPref(AUTOPLAY_PREF);
+  });
+});
+
 add_task(async function testMainViewVisible() {
-  Services.prefs.setIntPref("media.autoplay.default", Ci.nsIAutoplay.ALLOWED);
+  Services.prefs.setIntPref(AUTOPLAY_PREF, Ci.nsIAutoplay.ALLOWED);
 
   await BrowserTestUtils.withNewTab(AUTOPLAY_PAGE, async function() {
     let permissionsList = document.getElementById("identity-popup-permission-list");
     let emptyLabel = permissionsList.nextElementSibling.nextElementSibling;
 
     ok(BrowserTestUtils.is_hidden(autoplayBlockedIcon()), "Blocked icon not shown");
 
     await openIdentityPopup();
     ok(!BrowserTestUtils.is_hidden(emptyLabel), "List of permissions is empty");
     await closeIdentityPopup();
   });
 
-  Services.prefs.setIntPref("media.autoplay.default", Ci.nsIAutoplay.BLOCKED);
+  Services.prefs.setIntPref(AUTOPLAY_PREF, Ci.nsIAutoplay.BLOCKED);
 
   await BrowserTestUtils.withNewTab(AUTOPLAY_PAGE, async function(browser) {
     let permissionsList = document.getElementById("identity-popup-permission-list");
     let emptyLabel = permissionsList.nextElementSibling.nextElementSibling;
 
-    if (BrowserTestUtils.is_hidden(autoplayBlockedIcon())) {
-      // The block icon would be showed after tab receives `GloballyAutoplayBlocked` event.
-      await BrowserTestUtils.waitForEvent(browser, "GloballyAutoplayBlocked");
-    }
-    ok(!BrowserTestUtils.is_hidden(autoplayBlockedIcon()), "Blocked icon is shown");
+    await blockedIconShown(browser);
 
     await openIdentityPopup();
     ok(BrowserTestUtils.is_hidden(emptyLabel), "List of permissions is not empty");
-    let labelText = SitePermissions.getPermissionLabel("autoplay-media");
+    let labelText = SitePermissions.getPermissionLabel(AUTOPLAY_PERM);
     let labels = permissionsList.querySelectorAll(".identity-popup-permission-label");
     is(labels.length, 1, "One permission visible in main view");
     is(labels[0].textContent, labelText, "Correct value");
 
     let menulist = document.getElementById("identity-popup-popup-menulist");
     Assert.equal(menulist.label, "Block");
 
     await EventUtils.synthesizeMouseAtCenter(menulist, { type: "mousedown" });
@@ -65,15 +79,41 @@ add_task(async function testMainViewVisi
     let menuitem = menulist.getElementsByTagName("menuitem")[0];
     Assert.equal(menuitem.getAttribute("label"), "Allow");
 
     menuitem.click();
     menulist.menupopup.hidePopup();
     await closeIdentityPopup();
 
     let uri = Services.io.newURI(AUTOPLAY_PAGE);
-    let state = SitePermissions.get(uri, "autoplay-media").state;
+    let state = SitePermissions.get(uri, AUTOPLAY_PERM).state;
     Assert.equal(state, SitePermissions.ALLOW);
   });
 
   Services.perms.removeAll();
-  Services.prefs.clearUserPref("media.autoplay.default");
 });
+
+add_task(async function testGloballyBlockedOnNewWindow() {
+  Services.prefs.setIntPref(AUTOPLAY_PREF, Ci.nsIAutoplay.BLOCKED);
+
+  let uri = Services.io.newURI(AUTOPLAY_PAGE);
+
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, uri.spec);
+  await blockedIconShown(tab.linkedBrowser);
+
+  Assert.deepEqual(SitePermissions.get(uri, AUTOPLAY_PERM, tab.linkedBrowser), {
+    state: SitePermissions.BLOCK,
+    scope: SitePermissions.SCOPE_PERSISTENT,
+  });
+
+  let promiseWin = BrowserTestUtils.waitForNewWindow();
+  gBrowser.replaceTabWithWindow(tab);
+  let win = await promiseWin;
+  tab = win.gBrowser.selectedTab;
+
+  Assert.deepEqual(SitePermissions.get(uri, AUTOPLAY_PERM, tab.linkedBrowser), {
+    state: SitePermissions.BLOCK,
+    scope: SitePermissions.SCOPE_PERSISTENT,
+  });
+
+  SitePermissions.remove(uri, AUTOPLAY_PERM, tab.linkedBrowser);
+  await BrowserTestUtils.closeWindow(win);
+});
--- a/browser/modules/SitePermissions.jsm
+++ b/browser/modules/SitePermissions.jsm
@@ -187,16 +187,25 @@ const GloballyBlockedPermissions = {
           id,
           state: SitePermissions.BLOCK,
           scope: SitePermissions.SCOPE_GLOBAL,
         });
       }
     }
     return permissions;
   },
+
+  // Copies the globally blocked permission state of one browser
+  // into a new entry for the other browser.
+  copy(browser, newBrowser) {
+    let entry = this._stateByBrowser.get(browser);
+    if (entry) {
+      this._stateByBrowser.set(newBrowser, entry);
+    }
+  },
 };
 
 /**
  * A module to manage permanent and temporary permissions
  * by URI and browser.
  *
  * Some methods have the side effect of dispatching a "PermissionStateChange"
  * event on changes to temporary permissions, as mentioned in the respective docs.
@@ -589,16 +598,17 @@ var SitePermissions = {
    *
    * @param {Browser} browser
    *        The browser object to copy from.
    * @param {Browser} newBrowser
    *        The browser object to copy to.
    */
   copyTemporaryPermissions(browser, newBrowser) {
     TemporaryPermissions.copy(browser, newBrowser);
+    GloballyBlockedPermissions.copy(browser, newBrowser);
   },
 
   /**
    * Returns the localized label for the permission with the given ID, to be
    * used in a UI for managing permissions.
    *
    * @param {string} permissionID
    *        The permission to get the label for.