summaryrefslogtreecommitdiffstats
path: root/application/basilisk/base/content/sync/aboutSyncTabs.js
blob: 08986f73f65c82f876773b815e0f2554ba1734f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var Cu = Components.utils;

Cu.import("resource://services-common/utils.js");
Cu.import("resource://services-sync/main.js");
Cu.import("resource:///modules/PlacesUIUtils.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm", this);
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "Promise",
                                  "resource://gre/modules/Promise.jsm");

var RemoteTabViewer = {
  _tabsList: null,

  init: function () {
    Services.obs.addObserver(this, "weave:service:login:finish", false);
    Services.obs.addObserver(this, "weave:engine:sync:finish", false);

    Services.obs.addObserver(this, "cloudsync:tabs:update", false);

    this._tabsList = document.getElementById("tabsList");

    this.buildList(true);
  },

  uninit: function () {
    Services.obs.removeObserver(this, "weave:service:login:finish");
    Services.obs.removeObserver(this, "weave:engine:sync:finish");

    Services.obs.removeObserver(this, "cloudsync:tabs:update");
  },

  createItem: function (attrs) {
    let item = document.createElement("richlistitem");

    // Copy the attributes from the argument into the item.
    for (let attr in attrs) {
      item.setAttribute(attr, attrs[attr]);
    }

    if (attrs["type"] == "tab") {
      item.label = attrs.title != "" ? attrs.title : attrs.url;
    }

    return item;
  },

  filterTabs: function (event) {
    let val = event.target.value.toLowerCase();
    let numTabs = this._tabsList.getRowCount();
    let clientTabs = 0;
    let currentClient = null;

    for (let i = 0; i < numTabs; i++) {
      let item = this._tabsList.getItemAtIndex(i);
      let hide = false;
      if (item.getAttribute("type") == "tab") {
        if (!item.getAttribute("url").toLowerCase().includes(val) &&
            !item.getAttribute("title").toLowerCase().includes(val)) {
          hide = true;
        } else {
          clientTabs++;
        }
      }
      else if (item.getAttribute("type") == "client") {
        if (currentClient) {
          if (clientTabs == 0) {
            currentClient.hidden = true;
          }
        }
        currentClient = item;
        clientTabs = 0;
      }
      item.hidden = hide;
    }
    if (clientTabs == 0) {
      currentClient.hidden = true;
    }
  },

  openSelected: function () {
    let items = this._tabsList.selectedItems;
    let urls = [];
    for (let i = 0; i < items.length; i++) {
      if (items[i].getAttribute("type") == "tab") {
        urls.push(items[i].getAttribute("url"));
        let index = this._tabsList.getIndexOfItem(items[i]);
        this._tabsList.removeItemAt(index);
      }
    }
    if (urls.length) {
      getTopWin().gBrowser.loadTabs(urls);
      this._tabsList.clearSelection();
    }
  },

  bookmarkSingleTab: function () {
    let item = this._tabsList.selectedItems[0];
    let uri = Weave.Utils.makeURI(item.getAttribute("url"));
    let title = item.getAttribute("title");
    PlacesUIUtils.showBookmarkDialog({ action: "add"
                                     , type: "bookmark"
                                     , uri: uri
                                     , title: title
                                     , hiddenRows: [ "description"
                                                   , "location"
                                                   , "loadInSidebar"
                                                   , "keyword" ]
                                     }, window.top);
  },

  bookmarkSelectedTabs: function () {
    let items = this._tabsList.selectedItems;
    let URIs = [];
    for (let i = 0; i < items.length; i++) {
      if (items[i].getAttribute("type") == "tab") {
        let uri = Weave.Utils.makeURI(items[i].getAttribute("url"));
        if (!uri) {
          continue;
        }

        URIs.push(uri);
      }
    }
    if (URIs.length) {
      PlacesUIUtils.showBookmarkDialog({ action: "add"
                                       , type: "folder"
                                       , URIList: URIs
                                       , hiddenRows: [ "description" ]
                                       }, window.top);
    }
  },

  getIcon: function (iconUri, defaultIcon) {
    try {
      let iconURI = Weave.Utils.makeURI(iconUri);
      return PlacesUtils.favicons.getFaviconLinkForIcon(iconURI).spec;
    } catch (ex) {
      // Do nothing.
    }

    // Just give the provided default icon or the system's default.
    return defaultIcon || PlacesUtils.favicons.defaultFavicon.spec;
  },

  _waitingForBuildList: false,

  _buildListRequested: false,

  buildList: function (forceSync) {
    if (this._waitingForBuildList) {
      this._buildListRequested = true;
      return;
    }

    this._waitingForBuildList = true;
    this._buildListRequested = false;

    this._clearTabList();

    if (Weave.Service.isLoggedIn) {
      this._refetchTabs(forceSync);
      this._generateWeaveTabList();
    } else {
      // XXXzpao We should say something about not being logged in & not having data
      //        or tell the appropriate condition. (bug 583344)
    }

    let complete = () => {
      this._waitingForBuildList = false;
      if (this._buildListRequested) {
        CommonUtils.nextTick(this.buildList, this);
      }
    }

    complete();
  },

  _clearTabList: function () {
    let list = this._tabsList;

    // Clear out existing richlistitems.
    let count = list.getRowCount();
    if (count > 0) {
      for (let i = count - 1; i >= 0; i--) {
        list.removeItemAt(i);
      }
    }
  },

  _generateWeaveTabList: function () {
    let engine = Weave.Service.engineManager.get("tabs");
    let list = this._tabsList;

    let seenURLs = new Set();
    let localURLs = engine.getOpenURLs();

    for (let [, client] of Object.entries(engine.getAllClients())) {
      // Create the client node, but don't add it in-case we don't show any tabs
      let appendClient = true;

      client.tabs.forEach(function({title, urlHistory, icon}) {
        let url = urlHistory[0];
        if (!url || localURLs.has(url) || seenURLs.has(url)) {
          return;
        }
        seenURLs.add(url);

        if (appendClient) {
          let attrs = {
            type: "client",
            clientName: client.clientName,
            class: Weave.Service.clientsEngine.isMobile(client.id) ? "mobile" : "desktop"
          };
          let clientEnt = this.createItem(attrs);
          list.appendChild(clientEnt);
          appendClient = false;
          clientEnt.disabled = true;
        }
        let attrs = {
          type:  "tab",
          title: title || url,
          url:   url,
          icon:  this.getIcon(icon),
        }
        let tab = this.createItem(attrs);
        list.appendChild(tab);
      }, this);
    }
  },

  _generateCloudSyncTabList: function () {
    let updateTabList = function (remoteTabs) {
      let list = this._tabsList;

      for (let client of remoteTabs) {
        let clientAttrs = {
          type: "client",
          clientName: client.name,
        };

        let clientEnt = this.createItem(clientAttrs);
        list.appendChild(clientEnt);

        for (let tab of client.tabs) {
          let tabAttrs = {
            type: "tab",
            title: tab.title,
            url: tab.url,
            icon: this.getIcon(tab.icon),
          };
          let tabEnt = this.createItem(tabAttrs);
          list.appendChild(tabEnt);
        }
      }
    }.bind(this);

    return CloudSync().tabs.getRemoteTabs()
                           .then(updateTabList, Promise.reject.bind(Promise));
  },

  adjustContextMenu: function (event) {
    let mode = "all";
    switch (this._tabsList.selectedItems.length) {
      case 0:
        break;
      case 1:
        mode = "single"
        break;
      default:
        mode = "multiple";
        break;
    }

    let menu = document.getElementById("tabListContext");
    let el = menu.firstChild;
    while (el) {
      let showFor = el.getAttribute("showFor");
      if (showFor) {
        el.hidden = showFor != mode && showFor != "all";
      }

      el = el.nextSibling;
    }
  },

  _refetchTabs: function (force) {
    if (!force) {
      // Don't bother refetching tabs if we already did so recently
      let lastFetch = 0;
      try {
        lastFetch = Services.prefs.getIntPref("services.sync.lastTabFetch");
      }
      catch (e) {
        /* Just use the default value of 0 */
      }

      let now = Math.floor(Date.now() / 1000);
      if (now - lastFetch < 30) {
        return false;
      }
    }

    // Ask Sync to just do the tabs engine if it can.
    Weave.Service.sync(["tabs"]);
    Services.prefs.setIntPref("services.sync.lastTabFetch",
                              Math.floor(Date.now() / 1000));

    return true;
  },

  observe: function (subject, topic, data) {
    switch (topic) {
      case "weave:service:login:finish":
        // A login has finished, which means that a Sync is about to start and
        // we will eventually get to the "tabs" engine - but try and force the
        // tab engine to sync first by passing |true| for the forceSync param.
        this.buildList(true);
        break;
      case "weave:engine:sync:finish":
        if (data == "tabs") {
          // The tabs engine just finished, so re-build the list without
          // forcing a new sync of the tabs engine.
          this.buildList(false);
        }
        break;
      case "cloudsync:tabs:update":
        this.buildList(false);
        break;
    }
  },

  handleClick: function (event) {
    if (event.target.getAttribute("type") != "tab") {
      return;
    }

    if (event.button == 1) {
      let url = event.target.getAttribute("url");
      openUILink(url, event);
      let index = this._tabsList.getIndexOfItem(event.target);
      this._tabsList.removeItemAt(index);
    }
  }
}