Commit cd6bb388 authored by Jonathan Kingston's avatar Jonathan Kingston
Browse files

Bug 1395659 - Rejecting contextual identity APIs when containers are disabled. r=kmag

MozReview-Commit-ID: LCiI74SN12y

--HG--
extra : rebase_source : 86c883e610e406e115ca9558b5f4128db426a8e5
parent 23b1fbea
Loading
Loading
Loading
Loading
+18 −22
Original line number Diff line number Diff line
@@ -76,6 +76,12 @@ const convertIdentity = identity => {
  return result;
};

const checkAPIEnabled = () => {
  if (!containersEnabled) {
    throw new ExtensionError("Contextual identities are currently disabled");
  }
};

const convertIdentityFromObserver = wrappedIdentity => {
  let identity = wrappedIdentity.wrappedJSObject;
  let iconUrl, colorCode;
@@ -127,11 +133,10 @@ this.contextualIdentities = class extends ExtensionAPI {
    let self = {
      contextualIdentities: {
        async get(cookieStoreId) {
          checkAPIEnabled();
          let containerId = getContainerForCookieStoreId(cookieStoreId);
          if (!containerId) {
            return Promise.reject({
              message: `Invalid contextual identitiy: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Invalid contextual identitiy: ${cookieStoreId}`);
          }

          let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
@@ -139,6 +144,7 @@ this.contextualIdentities = class extends ExtensionAPI {
        },

        async query(details) {
          checkAPIEnabled();
          let identities = [];
          ContextualIdentityService.getPublicIdentities().forEach(identity => {
            if (details.name &&
@@ -163,19 +169,16 @@ this.contextualIdentities = class extends ExtensionAPI {
          return convertIdentity(identity);
        },

        update(cookieStoreId, details) {
        async update(cookieStoreId, details) {
          checkAPIEnabled();
          let containerId = getContainerForCookieStoreId(cookieStoreId);
          if (!containerId) {
            return Promise.reject({
              message: `Invalid contextual identitiy: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Invalid contextual identitiy: ${cookieStoreId}`);
          }

          let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
          if (!identity) {
            return Promise.reject({
              message: `Invalid contextual identitiy: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Invalid contextual identitiy: ${cookieStoreId}`);
          }

          if (details.name !== null) {
@@ -193,36 +196,29 @@ this.contextualIdentities = class extends ExtensionAPI {
          if (!ContextualIdentityService.update(identity.userContextId,
                                                identity.name, identity.icon,
                                                identity.color)) {
            return Promise.reject({
              message: `Contextual identitiy failed to update: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Contextual identitiy failed to update: ${cookieStoreId}`);
          }

          return convertIdentity(identity);
        },

        async remove(cookieStoreId) {
          checkAPIEnabled();
          let containerId = getContainerForCookieStoreId(cookieStoreId);
          if (!containerId) {
            return Promise.reject({
              message: `Invalid contextual identitiy: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Invalid contextual identitiy: ${cookieStoreId}`);
          }

          let identity = ContextualIdentityService.getPublicIdentityFromId(containerId);
          if (!identity) {
            return Promise.reject({
              message: `Invalid contextual identitiy: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Invalid contextual identitiy: ${cookieStoreId}`);
          }

          // We have to create the identity object before removing it.
          let convertedIdentity = convertIdentity(identity);

          if (!ContextualIdentityService.remove(identity.userContextId)) {
            return Promise.reject({
              message: `Contextual identitiy failed to remove: ${cookieStoreId}`,
            });
            throw new ExtensionError(`Contextual identitiy failed to remove: ${cookieStoreId}`);
          }

          return convertedIdentity;
+33 −0
Original line number Diff line number Diff line
@@ -139,6 +139,30 @@ add_task(async function test_contextualIdentity_with_permissions() {
    browser.test.assertEq("Personal", ci.name, "identity.name is correct");
    browser.test.assertEq("firefox-container-1", ci.cookieStoreId, "identity.cookieStoreId is correct");

    function listenForMessage(messageName, stateChangeBool) {
      return new Promise((resolve) => {
        browser.test.onMessage.addListener(function listener(msg) {
          browser.test.log(`Got message from background: ${msg}`);
          if (msg === messageName + "-response") {
            browser.test.onMessage.removeListener(listener);
            resolve();
          }
        });
        browser.test.log(`Sending message to background: ${messageName} ${stateChangeBool}`);
        browser.test.sendMessage(messageName, stateChangeBool);
      });
    }

    await listenForMessage("containers-state-change", false);

    browser.test.assertRejects(
      browser.contextualIdentities.query({}),
      "Contextual identities are currently disabled",
      "Throws when containers are disabled"
    );

    await listenForMessage("containers-state-change", true);

    let cis = await browser.contextualIdentities.query({});
    browser.test.assertEq(4, cis.length, "by default we should have 4 containers");

@@ -202,6 +226,7 @@ add_task(async function test_contextualIdentity_with_permissions() {

    browser.test.notifyPass("contextualIdentities");
  }

  function makeExtension(id) {
    return ExtensionTestUtils.loadExtension({
      useAddonManager: "temporary",
@@ -216,6 +241,14 @@ add_task(async function test_contextualIdentity_with_permissions() {
  }

  let extension = makeExtension("containers-test@mozilla.org");

  extension.onMessage("containers-state-change", (stateBool) => {
    Components.utils.reportError(`Got message "containers-state-change", ${stateBool}`);
    Services.prefs.setBoolPref(CONTAINERS_PREF, stateBool);
    Components.utils.reportError("Changed pref");
    extension.sendMessage("containers-state-change-response");
  });

  await extension.startup();
  await extension.awaitFinish("contextualIdentities");
  equal(Services.prefs.getBoolPref(CONTAINERS_PREF), true, "Pref should now be enabled, whatever it's initial state");