From 579004be29a7f9b04c2186114b6cfe5f105b0b37 Mon Sep 17 00:00:00 2001 From: Drew Willcoxon Date: Tue, 5 May 2015 17:23:59 -0700 Subject: [PATCH] Bug 1155521 - Migrate Pocket add-on and social provider users to the new Pocket button (part 2, migration). r=jaws --- browser/base/content/browser.xul | 4 +-- .../customizableui/CustomizableUI.jsm | 31 +++++++++++++++++- .../customizableui/CustomizableWidgets.jsm | 32 +++++++++++++++++++ ...owser_1042100_default_placements_update.js | 21 ++++++++++++ browser/modules/BrowserUITelemetry.jsm | 1 + 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/browser/base/content/browser.xul b/browser/base/content/browser.xul index 9326772f55bc..e74ed89a98a3 100644 --- a/browser/base/content/browser.xul +++ b/browser/base/content/browser.xul @@ -730,9 +730,9 @@ fullscreentoolbar="true" mode="icons" customizable="true" iconsize="small" #ifdef MOZ_DEV_EDITION - defaultset="urlbar-container,search-container,developer-button,bookmarks-menu-button,downloads-button,home-button,loop-button" + defaultset="urlbar-container,search-container,developer-button,bookmarks-menu-button,pocket-button,downloads-button,home-button,loop-button" #else - defaultset="urlbar-container,search-container,bookmarks-menu-button,downloads-button,home-button,loop-button" + defaultset="urlbar-container,search-container,bookmarks-menu-button,pocket-button,downloads-button,home-button,loop-button" #endif customizationtarget="nav-bar-customization-target" overflowable="true" diff --git a/browser/components/customizableui/CustomizableUI.jsm b/browser/components/customizableui/CustomizableUI.jsm index 33c90788a641..1fc05b847cbe 100644 --- a/browser/components/customizableui/CustomizableUI.jsm +++ b/browser/components/customizableui/CustomizableUI.jsm @@ -208,6 +208,15 @@ let CustomizableUIInternal = { "loop-button", ]; + // Insert the Pocket button after the bookmarks button if it's present. + for (let widgetDefinition of CustomizableWidgets) { + if (widgetDefinition.id == "pocket-button") { + let idx = navbarPlacements.indexOf("bookmarks-menu-button") + 1; + navbarPlacements.splice(idx, 0, widgetDefinition.id); + break; + } + } + if (Services.prefs.getBoolPref(kPrefWebIDEInNavbar)) { navbarPlacements.push("webide-button"); } @@ -288,7 +297,9 @@ let CustomizableUIInternal = { }, _introduceNewBuiltinWidgets: function() { - if (!gSavedState || gSavedState.currentVersion >= kVersion) { + // We should still enter even if gSavedState.currentVersion >= kVersion + // because the per-widget pref facility is independent of versioning. + if (!gSavedState) { return; } @@ -2106,6 +2117,14 @@ let CustomizableUIInternal = { // opened - so we know there's no build areas to handle. Also, builtin // widgets are expected to be (mostly) static, so shouldn't affect the // current placement settings. + + // This allows a widget to be both built-in by default but also able to be + // destroyed based on criteria that may not be available when the widget is + // created -- for example, because some other feature in the browser + // supersedes the widget. + let conditionalDestroyPromise = aData.conditionalDestroyPromise || null; + delete aData.conditionalDestroyPromise; + let widget = this.normalizeWidget(aData, CustomizableUI.SOURCE_BUILTIN); if (!widget) { ERROR("Error creating builtin widget: " + aData.id); @@ -2114,6 +2133,16 @@ let CustomizableUIInternal = { LOG("Creating built-in widget with id: " + widget.id); gPalette.set(widget.id, widget); + + if (conditionalDestroyPromise) { + conditionalDestroyPromise.then(shouldDestroy => { + if (shouldDestroy) { + this.destroyWidget(widget.id); + } + }, err => { + Cu.reportError(err); + }); + } }, // Returns true if the area will eventually lazily restore (but hasn't yet). diff --git a/browser/components/customizableui/CustomizableWidgets.jsm b/browser/components/customizableui/CustomizableWidgets.jsm index ad8a64380126..3d60789926d2 100644 --- a/browser/components/customizableui/CustomizableWidgets.jsm +++ b/browser/components/customizableui/CustomizableWidgets.jsm @@ -26,6 +26,10 @@ XPCOMUtils.defineLazyModuleGetter(this, "CharsetMenu", "resource://gre/modules/CharsetMenu.jsm"); XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", "resource://gre/modules/PrivateBrowsingUtils.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", + "resource://gre/modules/AddonManager.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "SocialService", + "resource://gre/modules/SocialService.jsm"); XPCOMUtils.defineLazyGetter(this, "CharsetBundle", function() { const kCharsetBundle = "chrome://global/locale/charsetMenu.properties"; @@ -1081,16 +1085,44 @@ if (Services.prefs.getBoolPref("browser.pocket.enabled")) { if (isEnabledForLocale) { let pocketButton = { id: "pocket-button", + defaultArea: CustomizableUI.AREA_NAVBAR, + introducedInVersion: "pref", type: "view", viewId: "PanelUI-pocketView", label: PocketBundle.GetStringFromName("pocket-button.label"), tooltiptext: PocketBundle.GetStringFromName("pocket-button.tooltiptext"), onViewShowing: Pocket.onPanelViewShowing, onViewHiding: Pocket.onPanelViewHiding, + + // If the user has the "classic" Pocket add-on installed, use that instead + // and destroy the widget. + conditionalDestroyPromise: new Promise(resolve => { + AddonManager.getAddonByID("isreaditlater@ideashower.com", addon => { + resolve(addon && addon.isActive); + }); + }), }; CustomizableWidgets.push(pocketButton); CustomizableUI.addListener(pocketButton); + + // Uninstall the Pocket social provider if it exists, but only if we haven't + // already uninstalled it in this manner. That way the user can reinstall + // it if they prefer it without its being uninstalled every time they start + // the browser. + let origin = "https://getpocket.com"; + SocialService.getProvider(origin, provider => { + if (provider) { + let pref = "social.backup.getpocket-com"; + if (!Services.prefs.prefHasUserValue(pref)) { + let str = Cc["@mozilla.org/supports-string;1"]. + createInstance(Ci.nsISupportsString); + str.data = JSON.stringify(provider.manifest); + Services.prefs.setComplexValue(pref, Ci.nsISupportsString, str); + SocialService.uninstallProvider(origin, () => {}); + } + } + }); } } diff --git a/browser/components/customizableui/test/browser_1042100_default_placements_update.js b/browser/components/customizableui/test/browser_1042100_default_placements_update.js index 33c65535436f..129dbd75457f 100644 --- a/browser/components/customizableui/test/browser_1042100_default_placements_update.js +++ b/browser/components/customizableui/test/browser_1042100_default_placements_update.js @@ -71,6 +71,27 @@ function test() { ok(haveNavbarPlacements, "Should have placements for nav-bar"); if (haveNavbarPlacements) { let placements = [...gFuturePlacements.get(CustomizableUI.AREA_NAVBAR)]; + + // Ignore widgets that are placed using the pref facility and not the + // versioned facility. They're independent of kVersion and the saved + // state's current version, so they may be present in the placements. + for (let i = 0; i < placements.length; ) { + if (placements[i] == testWidgetNew.id) { + i++; + continue; + } + let pref = "browser.toolbarbuttons.introduced." + placements[i]; + let introduced = false; + try { + introduced = Services.prefs.getBoolPref(pref); + } catch (ex) {} + if (!introduced) { + i++; + continue; + } + placements.splice(i, 1); + } + is(placements.length, 1, "Should have 1 newly placed widget in nav-bar"); is(placements[0], testWidgetNew.id, "Should have our test widget to be placed in nav-bar"); } diff --git a/browser/modules/BrowserUITelemetry.jsm b/browser/modules/BrowserUITelemetry.jsm index 8611b49bedb2..a34b2b33bb19 100644 --- a/browser/modules/BrowserUITelemetry.jsm +++ b/browser/modules/BrowserUITelemetry.jsm @@ -49,6 +49,7 @@ XPCOMUtils.defineLazyGetter(this, "DEFAULT_AREA_PLACEMENTS", function() { "urlbar-container", "search-container", "bookmarks-menu-button", + "pocket-button", "downloads-button", "home-button", "social-share-button",