Bug 1085774 - Part 0: cleanup in tabs engine. r=trivial

This commit is contained in:
Richard Newman 2014-11-30 19:34:58 -08:00
parent 50973f3c7a
commit fa9c41719f

View File

@ -2,13 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
this.EXPORTED_SYMBOLS = ['TabEngine', 'TabSetRecord']; this.EXPORTED_SYMBOLS = ["TabEngine", "TabSetRecord"];
const Cc = Components.classes; const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const Ci = Components.interfaces;
const Cu = Components.utils;
const TABS_TTL = 604800; // 7 days const TABS_TTL = 604800; // 7 days.
Cu.import("resource://gre/modules/Preferences.jsm"); Cu.import("resource://gre/modules/Preferences.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm");
@ -27,7 +25,7 @@ this.TabSetRecord = function TabSetRecord(collection, id) {
TabSetRecord.prototype = { TabSetRecord.prototype = {
__proto__: CryptoWrapper.prototype, __proto__: CryptoWrapper.prototype,
_logName: "Sync.Record.Tabs", _logName: "Sync.Record.Tabs",
ttl: TABS_TTL ttl: TABS_TTL,
}; };
Utils.deferGetSet(TabSetRecord, "cleartext", ["clientName", "tabs"]); Utils.deferGetSet(TabSetRecord, "cleartext", ["clientName", "tabs"]);
@ -36,7 +34,7 @@ Utils.deferGetSet(TabSetRecord, "cleartext", ["clientName", "tabs"]);
this.TabEngine = function TabEngine(service) { this.TabEngine = function TabEngine(service) {
SyncEngine.call(this, "Tabs", service); SyncEngine.call(this, "Tabs", service);
// Reset the client on every startup so that we fetch recent tabs // Reset the client on every startup so that we fetch recent tabs.
this._resetClient(); this._resetClient();
} }
TabEngine.prototype = { TabEngine.prototype = {
@ -47,7 +45,7 @@ TabEngine.prototype = {
syncPriority: 3, syncPriority: 3,
getChangedIDs: function getChangedIDs() { getChangedIDs: function () {
// No need for a proper timestamp (no conflict resolution needed). // No need for a proper timestamp (no conflict resolution needed).
let changedIDs = {}; let changedIDs = {};
if (this._tracker.modified) if (this._tracker.modified)
@ -55,22 +53,22 @@ TabEngine.prototype = {
return changedIDs; return changedIDs;
}, },
// API for use by Weave UI code to give user choices of tabs to open: // API for use by Sync UI code to give user choices of tabs to open.
getAllClients: function TabEngine_getAllClients() { getAllClients: function () {
return this._store._remoteClients; return this._store._remoteClients;
}, },
getClientById: function TabEngine_getClientById(id) { getClientById: function (id) {
return this._store._remoteClients[id]; return this._store._remoteClients[id];
}, },
_resetClient: function TabEngine__resetClient() { _resetClient: function () {
SyncEngine.prototype._resetClient.call(this); SyncEngine.prototype._resetClient.call(this);
this._store.wipe(); this._store.wipe();
this._tracker.modified = true; this._tracker.modified = true;
}, },
removeClientData: function removeClientData() { removeClientData: function () {
let url = this.engineURL + "/" + this.service.clientsEngine.localID; let url = this.engineURL + "/" + this.service.clientsEngine.localID;
this.service.resource(url).delete(); this.service.resource(url).delete();
}, },
@ -94,7 +92,7 @@ function TabStore(name, engine) {
TabStore.prototype = { TabStore.prototype = {
__proto__: Store.prototype, __proto__: Store.prototype,
itemExists: function TabStore_itemExists(id) { itemExists: function (id) {
return id == this.engine.service.clientsEngine.localID; return id == this.engine.service.clientsEngine.localID;
}, },
@ -155,7 +153,7 @@ TabStore.prototype = {
return allTabs; return allTabs;
}, },
createRecord: function createRecord(id, collection) { createRecord: function (id, collection) {
let record = new TabSetRecord(collection, id); let record = new TabSetRecord(collection, id);
record.clientName = this.engine.service.clientsEngine.localName; record.clientName = this.engine.service.clientsEngine.localName;
@ -188,7 +186,7 @@ TabStore.prototype = {
return record; return record;
}, },
getAllIDs: function TabStore_getAllIds() { getAllIDs: function () {
// Don't report any tabs if all windows are in private browsing for // Don't report any tabs if all windows are in private browsing for
// first syncs. // first syncs.
let ids = {}; let ids = {};
@ -214,31 +212,38 @@ TabStore.prototype = {
return ids; return ids;
}, },
wipe: function TabStore_wipe() { wipe: function () {
this._remoteClients = {}; this._remoteClients = {};
}, },
create: function TabStore_create(record) { create: function (record) {
this._log.debug("Adding remote tabs from " + record.clientName); this._log.debug("Adding remote tabs from " + record.clientName);
this._remoteClients[record.id] = record.cleartext; this._remoteClients[record.id] = record.cleartext;
// Lose some precision, but that's good enough (seconds) // Lose some precision, but that's good enough (seconds).
let roundModify = Math.floor(record.modified / 1000); let roundModify = Math.floor(record.modified / 1000);
let notifyState = Svc.Prefs.get("notifyTabState"); let notifyState = Svc.Prefs.get("notifyTabState");
// If there's no existing pref, save this first modified time
if (notifyState == null) // If there's no existing pref, save this first modified time.
if (notifyState == null) {
Svc.Prefs.set("notifyTabState", roundModify); Svc.Prefs.set("notifyTabState", roundModify);
// Don't change notifyState if it's already 0 (don't notify)
else if (notifyState == 0)
return; return;
// We must have gotten a new tab that isn't the same as last time }
else if (notifyState != roundModify)
// Don't change notifyState if it's already 0 (don't notify).
if (notifyState == 0) {
return;
}
// We must have gotten a new tab that isn't the same as last time.
if (notifyState != roundModify) {
Svc.Prefs.set("notifyTabState", 0); Svc.Prefs.set("notifyTabState", 0);
}
}, },
update: function update(record) { update: function (record) {
this._log.trace("Ignoring tab updates as local ones win"); this._log.trace("Ignoring tab updates as local ones win");
} },
}; };
@ -247,7 +252,7 @@ function TabTracker(name, engine) {
Svc.Obs.add("weave:engine:start-tracking", this); Svc.Obs.add("weave:engine:start-tracking", this);
Svc.Obs.add("weave:engine:stop-tracking", this); Svc.Obs.add("weave:engine:stop-tracking", this);
// Make sure "this" pointer is always set correctly for event listeners // Make sure "this" pointer is always set correctly for event listeners.
this.onTab = Utils.bind2(this, this.onTab); this.onTab = Utils.bind2(this, this.onTab);
this._unregisterListeners = Utils.bind2(this, this._unregisterListeners); this._unregisterListeners = Utils.bind2(this, this._unregisterListeners);
} }
@ -256,16 +261,17 @@ TabTracker.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
loadChangedIDs: function loadChangedIDs() { loadChangedIDs: function () {
// Don't read changed IDs from disk at start up. // Don't read changed IDs from disk at start up.
}, },
clearChangedIDs: function clearChangedIDs() { clearChangedIDs: function () {
this.modified = false; this.modified = false;
}, },
_topics: ["pageshow", "TabOpen", "TabClose", "TabSelect"], _topics: ["pageshow", "TabOpen", "TabClose", "TabSelect"],
_registerListenersForWindow: function registerListenersFW(window) {
_registerListenersForWindow: function (window) {
this._log.trace("Registering tab listeners in window"); this._log.trace("Registering tab listeners in window");
for each (let topic in this._topics) { for each (let topic in this._topics) {
window.addEventListener(topic, this.onTab, false); window.addEventListener(topic, this.onTab, false);
@ -273,11 +279,11 @@ TabTracker.prototype = {
window.addEventListener("unload", this._unregisterListeners, false); window.addEventListener("unload", this._unregisterListeners, false);
}, },
_unregisterListeners: function unregisterListeners(event) { _unregisterListeners: function (event) {
this._unregisterListenersForWindow(event.target); this._unregisterListenersForWindow(event.target);
}, },
_unregisterListenersForWindow: function unregisterListenersFW(window) { _unregisterListenersForWindow: function (window) {
this._log.trace("Removing tab listeners in window"); this._log.trace("Removing tab listeners in window");
window.removeEventListener("unload", this._unregisterListeners, false); window.removeEventListener("unload", this._unregisterListeners, false);
for each (let topic in this._topics) { for each (let topic in this._topics) {
@ -318,7 +324,7 @@ TabTracker.prototype = {
} }
}, },
onTab: function onTab(event) { onTab: function (event) {
if (event.originalTarget.linkedBrowser) { if (event.originalTarget.linkedBrowser) {
let browser = event.originalTarget.linkedBrowser; let browser = event.originalTarget.linkedBrowser;
if (PrivateBrowsingUtils.isBrowserPrivate(browser) && if (PrivateBrowsingUtils.isBrowserPrivate(browser) &&
@ -334,7 +340,8 @@ TabTracker.prototype = {
// For page shows, bump the score 10% of the time, emulating a partial // For page shows, bump the score 10% of the time, emulating a partial
// score. We don't want to sync too frequently. For all other page // score. We don't want to sync too frequently. For all other page
// events, always bump the score. // events, always bump the score.
if (event.type != "pageshow" || Math.random() < .1) if (event.type != "pageshow" || Math.random() < .1) {
this.score += SCORE_INCREMENT_SMALL; this.score += SCORE_INCREMENT_SMALL;
}
}, },
} };