Bug 1354738 - Don't force Sync initialization on context menu open. r?markh draft
authorEdouard Oger <eoger@fastmail.com>
Mon, 17 Apr 2017 16:52:35 -0400
changeset 563827 061857325c4f94c3ed02d27b8c5dd0f8fb6a43f7
parent 563802 a374c35469935a874fefe64d3e07003fc5bc8884
child 624591 fe9b8b623c86f04b57f4bfc75c41e0e34e7a64ea
push id54435
push userbmo:eoger@fastmail.com
push dateMon, 17 Apr 2017 22:09:21 +0000
reviewersmarkh
bugs1354738
milestone55.0a1
Bug 1354738 - Don't force Sync initialization on context menu open. r?markh MozReview-Commit-ID: JBwGTW9nLSl
browser/base/content/browser-fxaccounts.js
browser/base/content/test/general/browser_contextmenu.js
browser/base/content/test/general/browser_visibleTabs_contextMenu.js
--- a/browser/base/content/browser-fxaccounts.js
+++ b/browser/base/content/browser-fxaccounts.js
@@ -60,20 +60,17 @@ var gFxAccounts = {
     return this.strings = Services.strings.createBundle(
       "chrome://browser/locale/accounts.properties"
     );
   },
 
   get loginFailed() {
     // Referencing Weave.Service will implicitly initialize sync, and we don't
     // want to force that - so first check if it is ready.
-    let service = Cc["@mozilla.org/weave/service;1"]
-                  .getService(Components.interfaces.nsISupports)
-                  .wrappedJSObject;
-    if (!service.ready) {
+    if (!this.weaveService.ready) {
       return false;
     }
     // LOGIN_FAILED_LOGIN_REJECTED explicitly means "you must log back in".
     // All other login failures are assumed to be transient and should go
     // away by themselves, so aren't reflected here.
     return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED;
   },
 
@@ -371,29 +368,31 @@ var gFxAccounts = {
       const allDevicesLabel = this.strings.GetStringFromName("sendTabToAllDevices.menuitem");
       addTargetDevice("", allDevicesLabel);
     }
 
     devicesPopup.appendChild(fragment);
   },
 
   updateTabContextMenu(aPopupMenu, aTargetTab) {
-    if (!this.sendTabToDeviceEnabled) {
+    if (!this.sendTabToDeviceEnabled ||
+        !this.weaveService.ready) {
       return;
     }
 
     const targetURI = aTargetTab.linkedBrowser.currentURI.spec;
     const showSendTab = this.remoteClients.length > 0 && this.isSendableURI(targetURI);
 
     ["context_sendTabToDevice", "context_sendTabToDevice_separator"]
     .forEach(id => { document.getElementById(id).hidden = !showSendTab });
   },
 
   initPageContextMenu(contextMenu) {
-    if (!this.sendTabToDeviceEnabled) {
+    if (!this.sendTabToDeviceEnabled ||
+        !this.weaveService.ready) {
       return;
     }
 
     const remoteClientPresent = this.remoteClients.length > 0;
     // showSendLink and showSendPage are mutually exclusive
     let showSendLink = remoteClientPresent
                        && (contextMenu.onSaveableLink || contextMenu.onPlainTextLink);
     const showSendPage = !showSendLink && remoteClientPresent
@@ -417,8 +416,15 @@ var gFxAccounts = {
 };
 
 XPCOMUtils.defineLazyGetter(gFxAccounts, "FxAccountsCommon", function() {
   return Cu.import("resource://gre/modules/FxAccountsCommon.js", {});
 });
 
 XPCOMUtils.defineLazyModuleGetter(this, "EnsureFxAccountsWebChannel",
   "resource://gre/modules/FxAccountsWebChannel.jsm");
+
+
+XPCOMUtils.defineLazyGetter(gFxAccounts, "weaveService", function() {
+  return Components.classes["@mozilla.org/weave/service;1"]
+                   .getService(Components.interfaces.nsISupports)
+                   .wrappedJSObject;
+});
--- a/browser/base/content/test/general/browser_contextmenu.js
+++ b/browser/base/content/test/general/browser_contextmenu.js
@@ -889,16 +889,17 @@ add_task(function* test_input_spell_fals
 });
 
 const remoteClientsFixture = [ { id: 1, name: "Foo"}, { id: 2, name: "Bar"} ];
 
 add_task(function* test_plaintext_sendpagetodevice() {
   if (!gFxAccounts.sendTabToDeviceEnabled) {
     return;
   }
+  yield ensureSyncReady();
   const oldGetter = setupRemoteClientsFixture(remoteClientsFixture);
 
   let plainTextItemsWithSendPage =
                     ["context-navigation",   null,
                       ["context-back",         false,
                         "context-forward",      false,
                         "context-reload",       true,
                         "context-bookmarkpage", true], null,
@@ -926,16 +927,17 @@ add_task(function* test_plaintext_sendpa
 
   restoreRemoteClients(oldGetter);
 });
 
 add_task(function* test_link_sendlinktodevice() {
   if (!gFxAccounts.sendTabToDeviceEnabled) {
     return;
   }
+  yield ensureSyncReady();
   const oldGetter = setupRemoteClientsFixture(remoteClientsFixture);
 
   yield test_contextmenu("#test-link",
     ["context-openlinkintab", true,
      ...(hasContainers ? ["context-openlinkinusercontext-menu", true] : []),
      // We need a blank entry here because the containers submenu is
      // dynamically generated with no ids.
      ...(hasContainers ? ["", null] : []),
@@ -983,8 +985,15 @@ function* selectText(selector) {
     let div = doc.createRange();
     let element = doc.querySelector(contentSelector);
     Assert.ok(element, "Found element to select text from");
     div.setStartBefore(element);
     div.setEndAfter(element);
     win.getSelection().addRange(div);
   });
 }
+
+function ensureSyncReady() {
+  let service = Cc["@mozilla.org/weave/service;1"]
+                  .getService(Components.interfaces.nsISupports)
+                  .wrappedJSObject;
+  return service.whenLoaded();
+}
--- a/browser/base/content/test/general/browser_visibleTabs_contextMenu.js
+++ b/browser/base/content/test/general/browser_visibleTabs_contextMenu.js
@@ -16,16 +16,17 @@ add_task(function* test() {
   is(document.getElementById("context_closeTab").disabled, false, "Close Tab is enabled");
   is(document.getElementById("context_reloadAllTabs").disabled, false, "Reload All Tabs is enabled");
 
 
   if (gFxAccounts.sendTabToDeviceEnabled) {
     const origIsSendableURI = gFxAccounts.isSendableURI;
     gFxAccounts.isSendableURI = () => true;
     // Check the send tab to device menu item
+    yield ensureSyncReady();
     const oldGetter = setupRemoteClientsFixture(remoteClientsFixture);
     yield updateTabContextMenu(origTab, function* () {
       yield openMenuItemSubmenu("context_sendTabToDevice");
     });
     is(document.getElementById("context_sendTabToDevice").hidden, false, "Send tab to device is shown");
     let targets = document.getElementById("context_sendTabToDevicePopupMenu").childNodes;
     is(targets[0].getAttribute("label"), "Foo", "Foo target is present");
     is(targets[1].getAttribute("label"), "Bar", "Bar target is present");
@@ -71,8 +72,15 @@ add_task(function* test() {
   // Close Tabs To The End should now be enabled
   updateTabContextMenu(origTab);
   is(document.getElementById("context_closeTabsToTheEnd").disabled, false, "Close Tabs To The End is enabled");
 
   gBrowser.removeTab(testTab);
   gBrowser.removeTab(pinned);
 });
 
+function ensureSyncReady() {
+  let service = Cc["@mozilla.org/weave/service;1"]
+                  .getService(Components.interfaces.nsISupports)
+                  .wrappedJSObject;
+  return service.whenLoaded();
+}
+