Bug 763171 - Move display synced tab logic to nsBrowserGlue.js; r=gavin
authorGregory Szorc <gps@mozilla.com>
Fri, 29 Jun 2012 15:30:45 -0700
changeset 98696 9d0bf5dddfc4b97b89f95f134e226da6a7b46d5e
parent 98695 dbb9abec1fea45b038efc103ea5a668fce91fa97
child 98697 ea8c4810d3d5a5a2d60452143024ded37bc318fa
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersgavin
bugs763171
milestone16.0a1
Bug 763171 - Move display synced tab logic to nsBrowserGlue.js; r=gavin
browser/base/content/browser-syncui.js
browser/components/nsBrowserGlue.js
--- a/browser/base/content/browser-syncui.js
+++ b/browser/base/content/browser-syncui.js
@@ -11,17 +11,16 @@ let gSyncUI = {
          "weave:service:login:start",
          "weave:service:login:finish",
          "weave:service:logout:finish",
          "weave:service:start-over",
          "weave:ui:login:error",
          "weave:ui:sync:error",
          "weave:ui:sync:finish",
          "weave:ui:clear-error",
-         "weave:engine:clients:display-uri",
   ],
 
   _unloaded: false,
 
   init: function SUI_init() {
     // Proceed to set up the UI if Sync has already started up.
     // Otherwise we'll do it when Sync is firing up.
     if (Weave.Status.ready) {
@@ -392,31 +391,16 @@ let gSyncUI = {
       title = this._stringBundle.GetStringFromName("error.sync.no_node_found.title");
       Weave.Notifications.removeAll(title);
       this._wasDelayed = false;
     }
 
     this.updateUI();
   },
 
-  /**
-   * Observer called when display URI command is received.
-   */
-  onDisplayURI: function onDisplayURI(data) {
-    if (!gBrowser) {
-      return;
-    }
-
-    try {
-      gBrowser.addTab(data.wrappedJSObject.object.uri);
-    } catch (ex) {
-      Cu.reportError("Error displaying tab received by Sync: " + ex);
-    }
-  },
-
   observe: function SUI_observe(subject, topic, data) {
     if (this._unloaded) {
       Cu.reportError("SyncUI observer called after unload: " + topic);
       return;
     }
 
     switch (topic) {
       case "weave:service:sync:start":
@@ -456,19 +440,16 @@ let gSyncUI = {
         this.initUI();
         break;
       case "weave:notification:added":
         this.initNotifications();
         break;
       case "weave:ui:clear-error":
         this.clearError();
         break;
-      case "weave:engine:clients:display-uri":
-        this.onDisplayURI(subject);
-        break;
     }
   },
 
   QueryInterface: XPCOMUtils.generateQI([
     Ci.nsIObserver,
     Ci.nsISupportsWeakReference
   ])
 };
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -177,16 +177,19 @@ BrowserGlue.prototype = {
       case "browser-lastwindow-close-granted":
         this._setPrefToSaveSession();
         break;
 #endif
 #ifdef MOZ_SERVICES_SYNC
       case "weave:service:ready":
         this._setSyncAutoconnectDelay();
         break;
+      case "weave:engine:clients:display-uri":
+        this._onDisplaySyncURI(subject);
+        break;
 #endif
       case "session-save":
         this._setPrefToSaveSession(true);
         subject.QueryInterface(Ci.nsISupportsPRBool);
         subject.data = true;
         break;
       case "places-init-complete":
         if (!this._migrationImportsDefaultBookmarks)
@@ -260,16 +263,17 @@ BrowserGlue.prototype = {
     os.addObserver(this, "quit-application-requested", false);
     os.addObserver(this, "quit-application-granted", false);
 #ifdef OBSERVE_LASTWINDOW_CLOSE_TOPICS
     os.addObserver(this, "browser-lastwindow-close-requested", false);
     os.addObserver(this, "browser-lastwindow-close-granted", false);
 #endif
 #ifdef MOZ_SERVICES_SYNC
     os.addObserver(this, "weave:service:ready", false);
+    os.addObserver(this, "weave:engine:clients:display-uri", false);
 #endif
     os.addObserver(this, "session-save", false);
     os.addObserver(this, "places-init-complete", false);
     this._isPlacesInitObserver = true;
     os.addObserver(this, "places-database-locked", false);
     this._isPlacesLockedObserver = true;
     os.addObserver(this, "distribution-customization-complete", false);
     os.addObserver(this, "places-shutdown", false);
@@ -287,16 +291,17 @@ BrowserGlue.prototype = {
     os.removeObserver(this, "quit-application-requested");
     os.removeObserver(this, "quit-application-granted");
 #ifdef OBSERVE_LASTWINDOW_CLOSE_TOPICS
     os.removeObserver(this, "browser-lastwindow-close-requested");
     os.removeObserver(this, "browser-lastwindow-close-granted");
 #endif
 #ifdef MOZ_SERVICES_SYNC
     os.removeObserver(this, "weave:service:ready", false);
+    os.removeObserver(this, "weave:engine:clients:display-uri", false);
 #endif
     os.removeObserver(this, "session-save");
     if (this._isIdleObserver)
       this._idleService.removeIdleObserver(this, BOOKMARKS_BACKUP_IDLE_TIME);
     if (this._isPlacesInitObserver)
       os.removeObserver(this, "places-init-complete");
     if (this._isPlacesLockedObserver)
       os.removeObserver(this, "places-database-locked");
@@ -1501,16 +1506,39 @@ BrowserGlue.prototype = {
       let win = windowList.getNext();
       if (isFullBrowserWindow(win))
         return win;
     }
     return null;
 #endif
   },
 
+#ifdef MOZ_SERVICES_SYNC
+  /**
+   * Called as an observer when Sync's "display URI" notification is fired.
+   *
+   * We open the received URI in a background tab.
+   *
+   * Eventually, this will likely be replaced by a more robust tab syncing
+   * feature. This functionality is considered somewhat evil by UX because it
+   * opens a new tab automatically without any prompting. However, it is a
+   * lesser evil than sending a tab to a specific device (from e.g. Fennec)
+   * and having nothing happen on the receiving end.
+   */
+  _onDisplaySyncURI: function _onDisplaySyncURI(data) {
+    try {
+      let tabbrowser = this.getMostRecentBrowserWindow().gBrowser;
+
+      // The payload is wrapped weirdly because of how Sync does notifications.
+      tabbrowser.addTab(data.wrappedJSObject.object.uri);
+    } catch (ex) {
+      Cu.reportError("Error displaying tab received by Sync: " + ex);
+    }
+  },
+#endif
 
   // for XPCOM
   classID:          Components.ID("{eab9012e-5f74-4cbc-b2b5-a590235513cc}"),
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
                                          Ci.nsISupportsWeakReference,
                                          Ci.nsIBrowserGlue]),