2015-12-01 05:07:21 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["SyncedTabs"];
|
2015-12-01 05:07:21 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
|
|
|
|
const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
|
|
|
|
const { Preferences } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Preferences.jsm"
|
|
|
|
);
|
2015-12-01 05:07:21 +03:00
|
|
|
|
|
|
|
// The Sync XPCOM service
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "weaveXPCService", function() {
|
|
|
|
return Cc["@mozilla.org/weave/service;1"].getService(
|
|
|
|
Ci.nsISupports
|
|
|
|
).wrappedJSObject;
|
|
|
|
});
|
|
|
|
|
|
|
|
// from MDN...
|
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
|
}
|
|
|
|
|
|
|
|
// A topic we fire whenever we have new tabs available. This might be due
|
|
|
|
// to a request made by this module to refresh the tab list, or as the result
|
|
|
|
// of a regularly scheduled sync. The intent is that consumers just listen
|
|
|
|
// for this notification and update their UI in response.
|
|
|
|
const TOPIC_TABS_CHANGED = "services.sync.tabs.changed";
|
|
|
|
|
|
|
|
// The interval, in seconds, before which we consider the existing list
|
|
|
|
// of tabs "fresh enough" and don't force a new sync.
|
|
|
|
const TABS_FRESH_ENOUGH_INTERVAL = 30;
|
|
|
|
|
2017-12-19 01:48:31 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "log", function() {
|
|
|
|
let log = Log.repository.getLogger("Sync.RemoteTabs");
|
|
|
|
log.manageLevelFromPref("services.sync.log.logger.tabs");
|
|
|
|
return log;
|
|
|
|
});
|
2015-12-01 05:07:21 +03:00
|
|
|
|
|
|
|
// A private singleton that does the work.
|
|
|
|
let SyncedTabsInternal = {
|
|
|
|
/* Make a "tab" record. Returns a promise */
|
2017-05-03 02:29:33 +03:00
|
|
|
async _makeTab(client, tab, url, showRemoteIcons) {
|
2016-02-22 07:43:46 +03:00
|
|
|
let icon;
|
|
|
|
if (showRemoteIcons) {
|
|
|
|
icon = tab.icon;
|
|
|
|
}
|
2015-12-01 05:07:21 +03:00
|
|
|
if (!icon) {
|
2018-04-16 19:24:06 +03:00
|
|
|
// By not specifying a size the favicon service will pick the default,
|
|
|
|
// that is usually set through setDefaultIconURIPreferredSize by the
|
|
|
|
// first browser window. Commonly it's 16px at current dpi.
|
|
|
|
icon = "page-icon:" + url;
|
2015-12-01 05:07:21 +03:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: "tab",
|
|
|
|
title: tab.title || url,
|
|
|
|
url,
|
|
|
|
icon,
|
|
|
|
client: client.id,
|
|
|
|
lastUsed: tab.lastUsed,
|
|
|
|
};
|
2017-05-03 02:29:33 +03:00
|
|
|
},
|
2015-12-01 05:07:21 +03:00
|
|
|
|
|
|
|
/* Make a "client" record. Returns a promise for consistency with _makeTab */
|
2017-05-03 02:29:33 +03:00
|
|
|
async _makeClient(client) {
|
2015-12-01 05:07:21 +03:00
|
|
|
return {
|
|
|
|
id: client.id,
|
|
|
|
type: "client",
|
2016-04-11 21:10:40 +03:00
|
|
|
name: Weave.Service.clientsEngine.getClientName(client.id),
|
2018-03-28 09:38:30 +03:00
|
|
|
clientType: Weave.Service.clientsEngine.getClientType(client.id),
|
2016-05-19 01:45:29 +03:00
|
|
|
lastModified: client.lastModified * 1000, // sec to ms
|
2018-08-31 08:59:17 +03:00
|
|
|
tabs: [],
|
2015-12-01 05:07:21 +03:00
|
|
|
};
|
2017-05-03 02:29:33 +03:00
|
|
|
},
|
2015-12-01 05:07:21 +03:00
|
|
|
|
|
|
|
_tabMatchesFilter(tab, filter) {
|
|
|
|
let reFilter = new RegExp(escapeRegExp(filter), "i");
|
2019-10-22 18:22:48 +03:00
|
|
|
return reFilter.test(tab.url) || reFilter.test(tab.title);
|
2015-12-01 05:07:21 +03:00
|
|
|
},
|
|
|
|
|
2017-05-03 02:29:33 +03:00
|
|
|
async getTabClients(filter) {
|
2015-12-01 05:07:21 +03:00
|
|
|
log.info("Generating tab list with filter", filter);
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
// If Sync isn't ready, don't try and get anything.
|
|
|
|
if (!weaveXPCService.ready) {
|
|
|
|
log.debug("Sync isn't yet ready, so returning an empty tab list");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:43:46 +03:00
|
|
|
// A boolean that controls whether we should show the icon from the remote tab.
|
|
|
|
const showRemoteIcons = Preferences.get(
|
|
|
|
"services.sync.syncedTabs.showRemoteIcons",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2015-12-01 05:07:21 +03:00
|
|
|
let engine = Weave.Service.engineManager.get("tabs");
|
|
|
|
|
|
|
|
let ntabs = 0;
|
|
|
|
|
2017-01-13 02:02:08 +03:00
|
|
|
for (let client of Object.values(engine.getAllClients())) {
|
2016-04-01 00:36:40 +03:00
|
|
|
if (!Weave.Service.clientsEngine.remoteClientExists(client.id)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-03 02:29:33 +03:00
|
|
|
let clientRepr = await this._makeClient(client);
|
2015-12-01 05:07:21 +03:00
|
|
|
log.debug("Processing client", clientRepr);
|
|
|
|
|
|
|
|
for (let tab of client.tabs) {
|
|
|
|
let url = tab.urlHistory[0];
|
2017-08-11 07:26:29 +03:00
|
|
|
log.trace("remote tab", url);
|
2017-05-13 02:54:54 +03:00
|
|
|
|
|
|
|
if (!url) {
|
2015-12-01 05:07:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-05-03 02:29:33 +03:00
|
|
|
let tabRepr = await this._makeTab(client, tab, url, showRemoteIcons);
|
2015-12-01 05:07:21 +03:00
|
|
|
if (filter && !this._tabMatchesFilter(tabRepr, filter)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
clientRepr.tabs.push(tabRepr);
|
|
|
|
}
|
|
|
|
// We return all clients, even those without tabs - the consumer should
|
|
|
|
// filter it if they care.
|
|
|
|
ntabs += clientRepr.tabs.length;
|
|
|
|
result.push(clientRepr);
|
|
|
|
}
|
|
|
|
log.info(`Final tab list has ${result.length} clients with ${ntabs} tabs.`);
|
|
|
|
return result;
|
2017-05-03 02:29:33 +03:00
|
|
|
},
|
2015-12-01 05:07:21 +03:00
|
|
|
|
2017-06-06 01:50:07 +03:00
|
|
|
async syncTabs(force) {
|
2015-12-01 05:07:21 +03:00
|
|
|
if (!force) {
|
|
|
|
// Don't bother refetching tabs if we already did so recently
|
|
|
|
let lastFetch = Preferences.get("services.sync.lastTabFetch", 0);
|
|
|
|
let now = Math.floor(Date.now() / 1000);
|
|
|
|
if (now - lastFetch < TABS_FRESH_ENOUGH_INTERVAL) {
|
|
|
|
log.info("_refetchTabs was done recently, do not doing it again");
|
2017-06-06 01:50:07 +03:00
|
|
|
return false;
|
2015-12-01 05:07:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Sync isn't configured don't try and sync, else we will get reports
|
|
|
|
// of a login failure.
|
|
|
|
if (Weave.Status.checkSetup() == Weave.CLIENT_NOT_CONFIGURED) {
|
|
|
|
log.info("Sync client is not configured, so not attempting a tab sync");
|
2017-06-06 01:50:07 +03:00
|
|
|
return false;
|
2015-12-01 05:07:21 +03:00
|
|
|
}
|
|
|
|
// Ask Sync to just do the tabs engine if it can.
|
2017-06-06 01:50:07 +03:00
|
|
|
try {
|
|
|
|
log.info("Doing a tab sync.");
|
2017-11-16 01:10:45 +03:00
|
|
|
await Weave.Service.sync({ why: "tabs", engines: ["tabs"] });
|
2017-06-06 01:50:07 +03:00
|
|
|
return true;
|
|
|
|
} catch (ex) {
|
|
|
|
log.error("Sync failed", ex);
|
|
|
|
throw ex;
|
|
|
|
}
|
2015-12-01 05:07:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
log.trace(`observed topic=${topic}, data=${data}, subject=${subject}`);
|
|
|
|
switch (topic) {
|
|
|
|
case "weave:engine:sync:finish":
|
|
|
|
if (data != "tabs") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The tabs engine just finished syncing
|
|
|
|
// Set our lastTabFetch pref here so it tracks both explicit sync calls
|
|
|
|
// and normally scheduled ones.
|
|
|
|
Preferences.set(
|
|
|
|
"services.sync.lastTabFetch",
|
|
|
|
Math.floor(Date.now() / 1000)
|
|
|
|
);
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, TOPIC_TABS_CHANGED);
|
2015-12-01 05:07:21 +03:00
|
|
|
break;
|
|
|
|
case "weave:service:start-over":
|
|
|
|
// start-over needs to notify so consumers find no tabs.
|
|
|
|
Preferences.reset("services.sync.lastTabFetch");
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, TOPIC_TABS_CHANGED);
|
2015-12-01 05:07:21 +03:00
|
|
|
break;
|
2016-03-03 04:20:41 +03:00
|
|
|
case "nsPref:changed":
|
2017-04-14 22:51:39 +03:00
|
|
|
Services.obs.notifyObservers(null, TOPIC_TABS_CHANGED);
|
2016-03-03 04:20:41 +03:00
|
|
|
break;
|
2015-12-01 05:07:21 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns true if Sync is configured to Sync tabs, false otherwise
|
|
|
|
get isConfiguredToSyncTabs() {
|
|
|
|
if (!weaveXPCService.ready) {
|
|
|
|
log.debug("Sync isn't yet ready; assuming tab engine is enabled");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let engine = Weave.Service.engineManager.get("tabs");
|
|
|
|
return engine && engine.enabled;
|
|
|
|
},
|
|
|
|
|
|
|
|
get hasSyncedThisSession() {
|
|
|
|
let engine = Weave.Service.engineManager.get("tabs");
|
|
|
|
return engine && engine.hasSyncedThisSession;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(SyncedTabsInternal, "weave:engine:sync:finish");
|
|
|
|
Services.obs.addObserver(SyncedTabsInternal, "weave:service:start-over");
|
2016-03-03 04:20:41 +03:00
|
|
|
// Observe the pref the indicates the state of the tabs engine has changed.
|
|
|
|
// This will force consumers to re-evaluate the state of sync and update
|
|
|
|
// accordingly.
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.prefs.addObserver("services.sync.engine.tabs", SyncedTabsInternal);
|
2015-12-01 05:07:21 +03:00
|
|
|
|
|
|
|
// The public interface.
|
2018-02-23 22:50:01 +03:00
|
|
|
var SyncedTabs = {
|
2015-12-01 05:07:21 +03:00
|
|
|
// A mock-point for tests.
|
|
|
|
_internal: SyncedTabsInternal,
|
|
|
|
|
|
|
|
// We make the topic for the observer notification public.
|
|
|
|
TOPIC_TABS_CHANGED,
|
|
|
|
|
|
|
|
// Returns true if Sync is configured to Sync tabs, false otherwise
|
|
|
|
get isConfiguredToSyncTabs() {
|
|
|
|
return this._internal.isConfiguredToSyncTabs;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns true if a tab sync has completed once this session. If this
|
|
|
|
// returns false, then getting back no clients/tabs possibly just means we
|
|
|
|
// are waiting for that first sync to complete.
|
|
|
|
get hasSyncedThisSession() {
|
|
|
|
return this._internal.hasSyncedThisSession;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return a promise that resolves with an array of client records, each with
|
|
|
|
// a .tabs array. Note that part of the contract for this module is that the
|
|
|
|
// returned objects are not shared between invocations, so callers are free
|
|
|
|
// to mutate the returned objects (eg, sort, truncate) however they see fit.
|
|
|
|
getTabClients(query) {
|
|
|
|
return this._internal.getTabClients(query);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Starts a background request to start syncing tabs. Returns a promise that
|
|
|
|
// resolves when the sync is complete, but there's no resolved value -
|
|
|
|
// callers should be listening for TOPIC_TABS_CHANGED.
|
|
|
|
// If |force| is true we always sync. If false, we only sync if the most
|
|
|
|
// recent sync wasn't "recently".
|
|
|
|
syncTabs(force) {
|
|
|
|
return this._internal.syncTabs(force);
|
|
|
|
},
|
2016-03-08 01:13:41 +03:00
|
|
|
|
2017-02-07 00:31:31 +03:00
|
|
|
sortTabClientsByLastUsed(clients) {
|
|
|
|
// First sort the list of tabs for each client. Note that
|
2016-03-08 01:13:41 +03:00
|
|
|
// this module promises that the objects it returns are never
|
|
|
|
// shared, so we are free to mutate those objects directly.
|
|
|
|
for (let client of clients) {
|
|
|
|
let tabs = client.tabs;
|
|
|
|
tabs.sort((a, b) => b.lastUsed - a.lastUsed);
|
|
|
|
}
|
|
|
|
// Now sort the clients - the clients are sorted in the order of the
|
|
|
|
// most recent tab for that client (ie, it is important the tabs for
|
|
|
|
// each client are already sorted.)
|
|
|
|
clients.sort((a, b) => {
|
|
|
|
if (a.tabs.length == 0) {
|
|
|
|
return 1; // b comes first.
|
|
|
|
}
|
|
|
|
if (b.tabs.length == 0) {
|
|
|
|
return -1; // a comes first.
|
|
|
|
}
|
|
|
|
return b.tabs[0].lastUsed - a.tabs[0].lastUsed;
|
|
|
|
});
|
|
|
|
},
|
2015-12-01 05:07:21 +03:00
|
|
|
};
|