Bug 1371543 - Support Screenshot page action with UITour draft
authorFischer.json <fischer.json@gmail.com>
Wed, 30 Aug 2017 11:25:59 +0800
changeset 655598 56eb24f0df94e487747feb8541be243fb6a1bb27
parent 655385 db7f19e26e571ae1dd309f5d2f387b06ba670c30
child 728895 1f403f189c13acdc84c50955db467a67ad26b913
push id76940
push userbmo:fliu@mozilla.com
push dateWed, 30 Aug 2017 08:23:03 +0000
bugs1371543, 1393668
milestone57.0a1
Bug 1371543 - Support Screenshot page action with UITour This patch - supports Screenshot page action with UITour - adds https://screenshots.firefox.com into the uitour permission list for the bug 1393668 MozReview-Commit-ID: K4v0LU2WSyl
browser/app/permissions
browser/components/uitour/UITour-lib.js
browser/components/uitour/UITour.jsm
browser/components/uitour/test/browser_UITour_availableTargets.js
--- a/browser/app/permissions
+++ b/browser/app/permissions
@@ -3,16 +3,17 @@
 # * matchtype \t type \t permission \t host
 # * "origin" should be used for matchtype, "host" is supported for legacy reasons
 # * type is a string that identifies the type of permission (e.g. "cookie")
 # * permission is an integer between 1 and 15
 # See nsPermissionManager.cpp for more...
 
 # UITour
 origin	uitour	1	https://www.mozilla.org
+origin	uitour	1	https://screenshots.firefox.com
 origin	uitour	1	https://support.mozilla.org
 origin	uitour	1	https://addons.mozilla.org
 origin	uitour	1	https://discovery.addons.mozilla.org
 origin	uitour	1	about:home
 origin	uitour	1	about:newtab
 
 # XPInstall
 origin	install	1	https://addons.mozilla.org
--- a/browser/components/uitour/UITour-lib.js
+++ b/browser/components/uitour/UITour-lib.js
@@ -115,16 +115,17 @@ if (typeof Mozilla == "undefined") {
    * <li>pageAction-bookmark
    * <li>pageAction-copyURL
    * <li>pageAction-emailLink
    * <li>pageAction-sendToDevice
    * <li>pocket
    * <li>privateWindow
    * <li>quit
    * <li>readerMode-urlBar
+   * <li>screenshots
    * <li>search
    * <li>searchIcon
    * <li>searchPrefsLink
    * <li>selectedTabIcon
    * <li>trackingProtection
    * <li>urlbar
    * <li>webide
    * </ul>
--- a/browser/components/uitour/UITour.jsm
+++ b/browser/components/uitour/UITour.jsm
@@ -252,16 +252,22 @@ this.UITour = {
                aDocument.getElementById("pageAction-panel-emailLink");
       },
     }],
     ["pageAction-sendToDevice", {
       query: (aDocument) => {
         return aDocument.getElementById("pageAction-urlbar-sendToDevice") ||
                aDocument.getElementById("pageAction-panel-sendToDevice");
       },
+    }],
+    ["screenshots", {
+      query: (aDocument) => {
+        return aDocument.getElementById("pageAction-urlbar-screenshots") ||
+               aDocument.getElementById("pageAction-panel-screenshots");
+      },
     }]
   ]),
 
   init() {
     log.debug("Initializing UITour");
     // Lazy getter is initialized here so it can be replicated any time
     // in a test.
     delete this.seenPageIDs;
--- a/browser/components/uitour/test/browser_UITour_availableTargets.js
+++ b/browser/components/uitour/test/browser_UITour_availableTargets.js
@@ -24,89 +24,97 @@ function getExpectedTargets() {
     "pageAction-bookmark",
     "pageAction-copyURL",
     "pageAction-emailLink",
     "pageAction-sendToDevice",
       ...(hasPocket ? ["pocket"] : []),
     "privateWindow",
       ...(hasQuit ? ["quit"] : []),
     "readerMode-urlBar",
+    "screenshots",
     "search",
     "searchIcon",
     "trackingProtection",
     "urlbar",
   ];
 }
 
 add_task(setup_UITourTest);
 
 add_UITour_task(async function test_availableTargets() {
+  await ensureScreenshotsEnabled();
   let data = await getConfigurationPromise("availableTargets");
   let expecteds = getExpectedTargets();
   ok_targets(data, expecteds);
   ok(UITour.availableTargetsCache.has(window),
      "Targets should now be cached");
 });
 
 add_UITour_task(async function test_availableTargets_changeWidgets() {
+  await ensureScreenshotsEnabled();
   CustomizableUI.addWidgetToArea("bookmarks-menu-button", CustomizableUI.AREA_NAVBAR, 0);
   ok(!UITour.availableTargetsCache.has(window),
      "Targets should be evicted from cache after widget change");
   let data = await getConfigurationPromise("availableTargets");
   let expecteds = getExpectedTargets();
   expecteds = ["bookmarks", ...expecteds];
   ok_targets(data, expecteds);
 
   ok(UITour.availableTargetsCache.has(window),
      "Targets should now be cached again");
   CustomizableUI.reset();
   ok(!UITour.availableTargetsCache.has(window),
      "Targets should not be cached after reset");
 });
 
 add_UITour_task(async function test_availableTargets_exceptionFromGetTarget() {
+  await ensureScreenshotsEnabled();
   // The query function for the "search" target will throw if it's not found.
   // Make sure the callback still fires with the other available targets.
   CustomizableUI.removeWidgetFromArea("search-container");
   let data = await getConfigurationPromise("availableTargets");
   let expecteds = getExpectedTargets();
   // Default minus "search" and "searchIcon"
   expecteds = expecteds.filter(target => target != "search" && target != "searchIcon");
   ok_targets(data, expecteds);
 
   CustomizableUI.reset();
 });
 
 add_UITour_task(async function test_availableTargets_removeUrlbarPageActionsAll() {
+  await ensureScreenshotsEnabled();
   pageActionsHelper.setActionsUrlbarState(false);
   UITour.clearAvailableTargetsCache();
   let data = await getConfigurationPromise("availableTargets");
   let expecteds = getExpectedTargets();
   ok_targets(data, expecteds);
   let expectedActions = [
     [ "pocket", "pageAction-panel-pocket" ],
+    [ "screenshots", "pageAction-panel-screenshots" ],
     [ "pageAction-bookmark", "pageAction-panel-bookmark" ],
     [ "pageAction-copyURL", "pageAction-panel-copyURL" ],
     [ "pageAction-emailLink", "pageAction-panel-emailLink" ],
     [ "pageAction-sendToDevice", "pageAction-panel-sendToDevice" ],
   ];
   for (let [ targetName, expectedNodeId ] of expectedActions) {
     await assertTargetNode(targetName, expectedNodeId);
   }
   pageActionsHelper.restoreActionsUrlbarState();
 });
 
 add_UITour_task(async function test_availableTargets_addUrlbarPageActionsAll() {
+  await ensureScreenshotsEnabled();
   pageActionsHelper.setActionsUrlbarState(true);
   UITour.clearAvailableTargetsCache();
   let data = await getConfigurationPromise("availableTargets");
   let expecteds = getExpectedTargets();
   ok_targets(data, expecteds);
   let expectedActions = [
     [ "pocket", "pocket-button-box" ],
+    [ "screenshots", "pageAction-urlbar-screenshots" ],
     [ "pageAction-bookmark", "star-button-box" ],
     [ "pageAction-copyURL", "pageAction-urlbar-copyURL" ],
     [ "pageAction-emailLink", "pageAction-urlbar-emailLink" ],
     [ "pageAction-sendToDevice", "pageAction-urlbar-sendToDevice" ],
   ];
   for (let [ targetName, expectedNodeId ] of expectedActions) {
     await assertTargetNode(targetName, expectedNodeId);
   }
@@ -148,8 +156,34 @@ var pageActionsHelper = {
       return;
     }
     for (let [ action, originalState] of this._originalStates) {
       action.shownInUrlbar = originalState;
     }
     this._originalStates = null;
   }
 };
+
+function ensureScreenshotsEnabled() {
+  let prefsToEnsure = [];
+  if (Services.prefs.getBoolPref("extensions.screenshots.disabled", false)) {
+    prefsToEnsure.push([ "extensions.screenshots.disabled", false ]);
+  }
+  if (Services.prefs.getBoolPref("extensions.screenshots.system-disabled", false)) {
+    prefsToEnsure.push([ "extensions.screenshots.system-disabled", false ]);
+  }
+  return new Promise((resolve, reject) => {
+    if (prefsToEnsure.length <= 0) {
+     resolve();
+     return;
+    }
+    let listener = {
+      onWidgetAfterCreation(widgetid) {
+        if (widgetid == "screenshots_mozilla_org-browser-action") {
+          CustomizableUI.removeListener(listener);
+          resolve();
+        }
+      }
+    };
+    CustomizableUI.addListener(listener);
+    SpecialPowers.pushPrefEnv({ set: prefsToEnsure });
+  });
+}