tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_permissions_dialog_default_perm.js (4396B)


      1 "use strict";
      2 
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 const PERMISSIONS_URL =
      8  "chrome://browser/content/preferences/dialogs/permissions.xhtml";
      9 
     10 let sitePermissionsDialog;
     11 
     12 let principal = Services.scriptSecurityManager.createContentPrincipal(
     13  Services.io.newURI("http://www.example.com"),
     14  {}
     15 );
     16 let pbPrincipal = Services.scriptSecurityManager.principalWithOA(principal, {
     17  privateBrowsingId: 1,
     18 });
     19 let principalB = Services.scriptSecurityManager.createContentPrincipal(
     20  Services.io.newURI("https://example.org"),
     21  {}
     22 );
     23 
     24 /**
     25 * Replaces the default permissions defined in browser/app/permissions with our
     26 * own test-only permissions and instructs the permission manager to import
     27 * them.
     28 */
     29 async function addDefaultTestPermissions() {
     30  // create a file in the temp directory with the defaults.
     31  let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
     32  file.append("test_default_permissions");
     33 
     34  await IOUtils.writeUTF8(
     35    file.path,
     36    `origin\tinstall\t1\t${principal.origin}\norigin\tinstall\t1\t${pbPrincipal.origin}\n`
     37  );
     38 
     39  // Change the default permission file path.
     40  await SpecialPowers.pushPrefEnv({
     41    set: [
     42      ["permissions.manager.defaultsUrl", Services.io.newFileURI(file).spec],
     43    ],
     44  });
     45 
     46  // Call the permission manager to reload default permissions from file.
     47  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
     48 
     49  registerCleanupFunction(async () => {
     50    // Clean up temporary default permission file.
     51    await IOUtils.remove(file.path);
     52 
     53    // Restore non-test default permissions.
     54    await SpecialPowers.popPrefEnv();
     55    Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
     56  });
     57 }
     58 
     59 async function openPermissionsDialog() {
     60  let dialogOpened = promiseLoadSubDialog(PERMISSIONS_URL);
     61 
     62  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     63    let doc = content.document;
     64    let settingsButton = doc.getElementById("addonExceptions");
     65    settingsButton.click();
     66  });
     67 
     68  sitePermissionsDialog = await dialogOpened;
     69  await sitePermissionsDialog.document.mozSubdialogReady;
     70 }
     71 
     72 add_setup(async function () {
     73  await addDefaultTestPermissions();
     74 });
     75 
     76 /**
     77 * Tests that default (persistent) private browsing permissions can be removed.
     78 */
     79 add_task(async function removeAll() {
     80  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     81  await openPermissionsDialog();
     82 
     83  let doc = sitePermissionsDialog.document;
     84  let richlistbox = doc.getElementById("permissionsBox");
     85 
     86  // First item in the richlistbox contains column headers.
     87  Assert.equal(
     88    richlistbox.itemCount,
     89    2,
     90    "Should have the two default permission entries initially."
     91  );
     92 
     93  info("Adding a new non-default install permission");
     94  PermissionTestUtils.add(principalB, "install", Services.perms.ALLOW_ACTION);
     95 
     96  info("Waiting for the permission to appear in the list.");
     97  await BrowserTestUtils.waitForMutationCondition(
     98    richlistbox,
     99    { childList: true },
    100    () => richlistbox.itemCount == 3
    101  );
    102 
    103  info("Clicking remove all.");
    104  doc.getElementById("removeAllPermissions").click();
    105 
    106  info("Waiting for all list items to be cleared.");
    107  await BrowserTestUtils.waitForMutationCondition(
    108    richlistbox,
    109    { childList: true },
    110    () => richlistbox.itemCount == 0
    111  );
    112 
    113  let dialogClosePromise = BrowserTestUtils.waitForEvent(
    114    sitePermissionsDialog,
    115    "dialogclosing",
    116    true
    117  );
    118 
    119  info("Accepting dialog to apply the changes.");
    120  doc.querySelector("dialog").getButton("accept").click();
    121 
    122  info("Waiting for dialog to close.");
    123  await dialogClosePromise;
    124 
    125  info("Waiting for all permissions to be removed.");
    126  await TestUtils.waitForCondition(
    127    () =>
    128      PermissionTestUtils.getPermissionObject(principal, "install") == null &&
    129      PermissionTestUtils.getPermissionObject(pbPrincipal, "install") == null &&
    130      PermissionTestUtils.getPermissionObject(principalB, "install") == null
    131  );
    132 
    133  info("Opening the permissions dialog again.");
    134  await openPermissionsDialog();
    135 
    136  Assert.equal(
    137    richlistbox.itemCount,
    138    0,
    139    "Permission list should still be empty."
    140  );
    141 
    142  // Cleanup
    143  BrowserTestUtils.removeTab(gBrowser.selectedTab);
    144  Services.perms.removeAll();
    145 });