Bug 1790115 - allow static theme resources in other extensions also in private browsing windows. r=mixedpuppy, a=RyanVM
authorLuca Greco <lgreco@mozilla.com>
Tue, 13 Sep 2022 23:42:19 +0000
changeset 703964 c73a6d4de77e7a72932107d76e4ba42c7d7c4f99
parent 703963 e47ddf4a282bdcc87545ef21a0671f96dbb5a7ac
child 703965 ac867a5225ddeb68087f263b947a364766396aa3
push id2954
push userryanvm@gmail.com
push dateThu, 29 Sep 2022 21:23:02 +0000
treeherdermozilla-release@387a1a22253e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy, RyanVM
bugs1790115
milestone105.0.2
Bug 1790115 - allow static theme resources in other extensions also in private browsing windows. r=mixedpuppy, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D156986
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/test/browser/browser_ext_themes_getCurrent_differentExt.js
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -3058,16 +3058,22 @@ class Extension extends ExtensionData {
         // since we are pushing the value into this.permissions.  EP will eventually save.
         lazy.ExtensionPermissions.add(this.id, {
           permissions: [PRIVATE_ALLOWED_PERMISSION],
           origins: [],
         });
         this.permissions.add(PRIVATE_ALLOWED_PERMISSION);
       }
 
+      // Allow other extensions to access static themes in private browsing windows
+      // (See Bug 1790115).
+      if (this.type === "theme") {
+        this.permissions.add(PRIVATE_ALLOWED_PERMISSION);
+      }
+
       // We only want to update the SVG_CONTEXT_PROPERTIES_PERMISSION during install and
       // upgrade/downgrade startups.
       if (INSTALL_AND_UPDATE_STARTUP_REASONS.has(this.startupReason)) {
         if (isMozillaExtension(this)) {
           // Add to EP so it is preserved after ADDON_INSTALL.  We don't wait on the add here
           // since we are pushing the value into this.permissions.  EP will eventually save.
           lazy.ExtensionPermissions.add(this.id, {
             permissions: [SVG_CONTEXT_PROPERTIES_PERMISSION],
--- a/toolkit/components/extensions/test/browser/browser_ext_themes_getCurrent_differentExt.js
+++ b/toolkit/components/extensions/test/browser/browser_ext_themes_getCurrent_differentExt.js
@@ -1,31 +1,31 @@
 "use strict";
 
 // This test checks whether browser.theme.getCurrent() works correctly when theme
 // does not originate from extension querying the theme.
+const THEME = {
+  manifest: {
+    theme: {
+      images: {
+        theme_frame: "image1.png",
+      },
+      colors: {
+        frame: ACCENT_COLOR,
+        tab_background_text: TEXT_COLOR,
+      },
+    },
+  },
+  files: {
+    "image1.png": BACKGROUND,
+  },
+};
 
 add_task(async function test_getcurrent() {
-  const theme = ExtensionTestUtils.loadExtension({
-    manifest: {
-      theme: {
-        images: {
-          theme_frame: "image1.png",
-        },
-        colors: {
-          frame: ACCENT_COLOR,
-          tab_background_text: TEXT_COLOR,
-        },
-      },
-    },
-    files: {
-      "image1.png": BACKGROUND,
-    },
-  });
-
+  const theme = ExtensionTestUtils.loadExtension(THEME);
   const extension = ExtensionTestUtils.loadExtension({
     background() {
       browser.theme.onUpdated.addListener(() => {
         browser.theme.getCurrent().then(theme => {
           browser.test.sendMessage("theme-updated", theme);
           if (!theme?.images) {
             return;
           }
@@ -76,8 +76,74 @@ add_task(async function test_getcurrent(
   Assert.equal(
     JSON.stringify({ colors: null, images: null, properties: null }),
     JSON.stringify(receivedTheme),
     "getCurrent returns empty theme"
   );
 
   await extension.unload();
 });
+
+add_task(async function test_getcurrent_privateBrowsing() {
+  const theme = ExtensionTestUtils.loadExtension(THEME);
+
+  const extension = ExtensionTestUtils.loadExtension({
+    incognitoOverride: "spanning",
+    manifest: {
+      sidebar_action: {
+        default_panel: "sidebar.html",
+      },
+    },
+    files: {
+      "sidebar.html": `<!DOCTYPE html>
+        <html>
+          <body>
+            Test Extension Sidebar
+            <script src="sidebar.js"></script>
+          </body>
+        </html>
+      `,
+      "sidebar.js": function() {
+        browser.theme.getCurrent().then(theme => {
+          if (!theme?.images) {
+            browser.test.fail(
+              `Missing expected images from theme.getCurrent result`
+            );
+            return;
+          }
+
+          // Try to access the theme_frame image
+          fetch(theme.images.theme_frame)
+            .then(() => {
+              browser.test.sendMessage("theme-image", { success: true });
+            })
+            .catch(e => {
+              browser.test.sendMessage("theme-image", {
+                success: false,
+                error: e.message,
+              });
+            });
+        });
+      },
+    },
+  });
+
+  await extension.startup();
+  await theme.startup();
+
+  const privateWin = await BrowserTestUtils.openNewBrowserWindow({
+    private: true,
+  });
+
+  const { ExtensionCommon } = ChromeUtils.import(
+    "resource://gre/modules/ExtensionCommon.jsm"
+  );
+  const { makeWidgetId } = ExtensionCommon;
+  privateWin.SidebarUI.show(`${makeWidgetId(extension.id)}-sidebar-action`);
+
+  let imageLoaded = extension.awaitMessage("theme-image");
+  Assert.deepEqual(await imageLoaded, { success: true }, "theme image loaded");
+
+  await extension.unload();
+  await theme.unload();
+
+  await BrowserTestUtils.closeWindow(privateWin);
+});