2008-06-26 14:49:01 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Myk Melez <myk@mozilla.org>
|
2009-02-04 19:51:20 -08:00
|
|
|
* Jono DiCarlo <jdicarlo@mozilla.com>
|
2010-11-11 11:00:41 -08:00
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2008-06-26 14:49:01 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['TabEngine'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-02-06 17:50:12 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
|
|
|
Cu.import("resource://services-sync/engines/clients.js");
|
|
|
|
Cu.import("resource://services-sync/stores.js");
|
|
|
|
Cu.import("resource://services-sync/trackers.js");
|
|
|
|
Cu.import("resource://services-sync/type_records/tabs.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-07-19 23:07:45 -07:00
|
|
|
Cu.import("resource://services-sync/ext/Preferences.js");
|
|
|
|
|
|
|
|
// It is safer to inspect the private browsing preferences rather than
|
|
|
|
// the flags of nsIPrivateBrowsingService. The user may have turned on
|
|
|
|
// "Never remember history" in the same session, or Firefox was started
|
|
|
|
// with the -private command line argument. In both cases, the
|
|
|
|
// "autoStarted" flag of nsIPrivateBrowsingService will be wrong.
|
|
|
|
const PBPrefs = new Preferences("browser.privatebrowsing.");
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-01-08 16:57:35 -08:00
|
|
|
function TabEngine() {
|
2010-02-11 15:29:15 -08:00
|
|
|
SyncEngine.call(this, "Tabs");
|
2010-02-11 15:25:31 -08:00
|
|
|
|
|
|
|
// Reset the client on every startup so that we fetch recent tabs
|
|
|
|
this._resetClient();
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
TabEngine.prototype = {
|
2009-01-08 16:57:35 -08:00
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
_storeObj: TabStore,
|
|
|
|
_trackerObj: TabTracker,
|
2009-02-04 19:51:20 -08:00
|
|
|
_recordObj: TabSetRecord,
|
|
|
|
|
2010-11-11 11:00:41 -08:00
|
|
|
getChangedIDs: function getChangedIDs() {
|
|
|
|
// No need for a proper timestamp (no conflict resolution needed).
|
|
|
|
let changedIDs = {};
|
|
|
|
if (this._tracker.modified)
|
|
|
|
changedIDs[Clients.localID] = 0;
|
|
|
|
return changedIDs;
|
|
|
|
},
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
// API for use by Weave UI code to give user choices of tabs to open:
|
|
|
|
getAllClients: function TabEngine_getAllClients() {
|
|
|
|
return this._store._remoteClients;
|
|
|
|
},
|
|
|
|
|
|
|
|
getClientById: function TabEngine_getClientById(id) {
|
|
|
|
return this._store._remoteClients[id];
|
2009-02-26 22:36:14 -08:00
|
|
|
},
|
2009-02-04 19:51:20 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
_resetClient: function TabEngine__resetClient() {
|
2009-12-02 14:44:17 -08:00
|
|
|
SyncEngine.prototype._resetClient.call(this);
|
2009-02-26 22:36:14 -08:00
|
|
|
this._store.wipe();
|
2010-11-11 11:00:41 -08:00
|
|
|
this._tracker.modified = true;
|
2009-03-12 15:54:26 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/* The intent is not to show tabs in the menu if they're already
|
|
|
|
* open locally. There are a couple ways to interpret this: for
|
|
|
|
* instance, we could do it by removing a tab from the list when
|
|
|
|
* you open it -- but then if you close it, you can't get back to
|
|
|
|
* it. So the way I'm doing it here is to not show a tab in the menu
|
|
|
|
* if you have a tab open to the same URL, even though this means
|
|
|
|
* that as soon as you navigate anywhere, the original tab will
|
|
|
|
* reappear in the menu.
|
|
|
|
*/
|
|
|
|
locallyOpenTabMatchesURL: function TabEngine_localTabMatches(url) {
|
2009-12-01 11:36:56 -08:00
|
|
|
return this._store.getAllTabs().some(function(tab) {
|
|
|
|
return tab.urlHistory[0] == url;
|
|
|
|
});
|
2009-02-26 22:36:14 -08:00
|
|
|
}
|
2008-06-03 14:45:53 -07:00
|
|
|
};
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function TabStore(name) {
|
|
|
|
Store.call(this, name);
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
TabStore.prototype = {
|
2009-02-04 19:51:20 -08:00
|
|
|
__proto__: Store.prototype,
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-11-25 15:22:45 -08:00
|
|
|
itemExists: function TabStore_itemExists(id) {
|
2010-03-16 16:39:08 -07:00
|
|
|
return id == Clients.localID;
|
2009-03-17 17:57:53 -07:00
|
|
|
},
|
|
|
|
|
2009-12-02 14:46:02 -08:00
|
|
|
getAllTabs: function getAllTabs(filter) {
|
2009-12-03 14:54:23 -08:00
|
|
|
let filteredUrls = new RegExp(Svc.Prefs.get("engine.tabs.filteredUrls"), "i");
|
|
|
|
|
2009-11-25 15:22:45 -08:00
|
|
|
let allTabs = [];
|
2010-03-16 15:14:32 -07:00
|
|
|
|
|
|
|
let currentState = JSON.parse(Svc.Session.getBrowserState());
|
|
|
|
currentState.windows.forEach(function(window) {
|
|
|
|
window.tabs.forEach(function(tab) {
|
|
|
|
// Make sure there are history entries to look at.
|
|
|
|
if (!tab.entries.length)
|
|
|
|
return;
|
|
|
|
// Until we store full or partial history, just grab the current entry.
|
|
|
|
// index is 1 based, so make sure we adjust.
|
|
|
|
let entry = tab.entries[tab.index - 1];
|
|
|
|
|
|
|
|
// Filter out some urls if necessary. SessionStore can return empty
|
|
|
|
// tabs in some cases - easiest thing is to just ignore them for now.
|
|
|
|
if (!entry.url || filter && filteredUrls.test(entry.url))
|
2009-12-02 14:46:02 -08:00
|
|
|
return;
|
|
|
|
|
2010-03-16 15:14:32 -07:00
|
|
|
// weaveLastUsed will only be set if the tab was ever selected (or
|
|
|
|
// opened after Weave was running). So it might not ever be set.
|
|
|
|
// I think it's also possible that attributes[.image] might not be set
|
|
|
|
// so handle that as well.
|
2009-12-01 11:36:56 -08:00
|
|
|
allTabs.push({
|
2010-03-16 15:14:32 -07:00
|
|
|
title: entry.title || "",
|
|
|
|
urlHistory: [entry.url],
|
|
|
|
icon: tab.attributes && tab.attributes.image || "",
|
|
|
|
lastUsed: tab.extData && tab.extData.weaveLastUsed || 0
|
2009-12-01 11:36:56 -08:00
|
|
|
});
|
2009-11-25 15:22:45 -08:00
|
|
|
});
|
2010-03-16 15:14:32 -07:00
|
|
|
});
|
|
|
|
|
2009-12-01 11:36:56 -08:00
|
|
|
return allTabs;
|
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
createRecord: function createRecord(id, collection) {
|
|
|
|
let record = new TabSetRecord(collection, id);
|
2010-03-16 16:39:08 -07:00
|
|
|
record.clientName = Clients.localName;
|
2009-03-18 12:40:27 -07:00
|
|
|
|
2010-07-19 23:07:45 -07:00
|
|
|
// Don't provide any tabs to compare against and ignore the update later.
|
|
|
|
if (Svc.Private.privateBrowsingEnabled && !PBPrefs.get("autostart")) {
|
|
|
|
record.tabs = [];
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2009-12-01 11:36:56 -08:00
|
|
|
// Sort tabs in descending-used order to grab the most recently used
|
2010-02-17 18:24:22 -08:00
|
|
|
let tabs = this.getAllTabs(true).sort(function(a, b) {
|
2009-12-01 11:36:56 -08:00
|
|
|
return b.lastUsed - a.lastUsed;
|
2010-02-17 18:24:22 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Figure out how many tabs we can pack into a payload. Starting with a 28KB
|
|
|
|
// payload, we can estimate various overheads from encryption/JSON/WBO.
|
|
|
|
let size = JSON.stringify(tabs).length;
|
|
|
|
let origLength = tabs.length;
|
|
|
|
const MAX_TAB_SIZE = 20000;
|
|
|
|
if (size > MAX_TAB_SIZE) {
|
|
|
|
// Estimate a little more than the direct fraction to maximize packing
|
|
|
|
let cutoff = Math.ceil(tabs.length * MAX_TAB_SIZE / size);
|
|
|
|
tabs = tabs.slice(0, cutoff + 1);
|
|
|
|
|
|
|
|
// Keep dropping off the last entry until the data fits
|
|
|
|
while (JSON.stringify(tabs).length > MAX_TAB_SIZE)
|
|
|
|
tabs.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.trace("Created tabs " + tabs.length + " of " + origLength);
|
|
|
|
tabs.forEach(function(tab) {
|
2010-01-21 09:59:27 -08:00
|
|
|
this._log.trace("Wrapping tab: " + JSON.stringify(tab));
|
2009-11-25 15:22:45 -08:00
|
|
|
}, this);
|
2009-03-18 12:40:27 -07:00
|
|
|
|
2010-02-17 18:24:22 -08:00
|
|
|
record.tabs = tabs;
|
2009-02-04 19:51:20 -08:00
|
|
|
return record;
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-09 20:23:42 -08:00
|
|
|
getAllIDs: function TabStore_getAllIds() {
|
2010-07-19 23:07:45 -07:00
|
|
|
// Don't report any tabs if we're in private browsing for first syncs.
|
2009-11-25 15:17:39 -08:00
|
|
|
let ids = {};
|
2010-07-19 23:07:45 -07:00
|
|
|
if (Svc.Private.privateBrowsingEnabled && !PBPrefs.get("autostart"))
|
|
|
|
return ids;
|
|
|
|
|
2010-03-16 16:39:08 -07:00
|
|
|
ids[Clients.localID] = true;
|
2009-11-25 15:17:39 -08:00
|
|
|
return ids;
|
2008-06-03 14:45:53 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function TabStore_wipe() {
|
2009-02-04 19:51:20 -08:00
|
|
|
this._remoteClients = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function TabStore_create(record) {
|
2009-11-25 15:22:45 -08:00
|
|
|
this._log.debug("Adding remote tabs from " + record.clientName);
|
|
|
|
this._remoteClients[record.id] = record.cleartext;
|
2009-12-17 12:20:07 -08:00
|
|
|
|
|
|
|
// Lose some precision, but that's good enough (seconds)
|
|
|
|
let roundModify = Math.floor(record.modified / 1000);
|
|
|
|
let notifyState = Svc.Prefs.get("notifyTabState");
|
|
|
|
// If there's no existing pref, save this first modified time
|
|
|
|
if (notifyState == null)
|
|
|
|
Svc.Prefs.set("notifyTabState", roundModify);
|
|
|
|
// Don't change notifyState if it's already 0 (don't notify)
|
|
|
|
else if (notifyState == 0)
|
|
|
|
return;
|
|
|
|
// We must have gotten a new tab that isn't the same as last time
|
|
|
|
else if (notifyState != roundModify)
|
|
|
|
Svc.Prefs.set("notifyTabState", 0);
|
2010-07-19 23:07:45 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function update(record) {
|
|
|
|
this._log.trace("Ignoring tab updates as local ones win");
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-16 16:49:56 -07:00
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function TabTracker(name) {
|
|
|
|
Tracker.call(this, name);
|
2010-08-06 08:30:58 -07:00
|
|
|
Svc.Obs.add("weave:engine:start-tracking", this);
|
|
|
|
Svc.Obs.add("weave:engine:stop-tracking", this);
|
2010-07-19 23:07:45 -07:00
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
// Make sure "this" pointer is always set correctly for event listeners
|
|
|
|
this.onTab = Utils.bind2(this, this.onTab);
|
2010-08-06 08:30:58 -07:00
|
|
|
this._unregisterListeners = Utils.bind2(this, this._unregisterListeners);
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
TabTracker.prototype = {
|
2009-02-04 19:51:20 -08:00
|
|
|
__proto__: Tracker.prototype,
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-06 17:50:12 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
|
|
|
|
2010-11-11 11:00:41 -08:00
|
|
|
loadChangedIDs: function loadChangedIDs() {
|
|
|
|
// Don't read changed IDs from disk at start up.
|
|
|
|
},
|
|
|
|
|
|
|
|
clearChangedIDs: function clearChangedIDs() {
|
|
|
|
this.modified = false;
|
|
|
|
},
|
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
_topics: ["pageshow", "TabOpen", "TabClose", "TabSelect"],
|
|
|
|
_registerListenersForWindow: function registerListenersFW(window) {
|
|
|
|
this._log.trace("Registering tab listeners in window");
|
|
|
|
for each (let topic in this._topics) {
|
|
|
|
window.addEventListener(topic, this.onTab, false);
|
|
|
|
}
|
|
|
|
window.addEventListener("unload", this._unregisterListeners, false);
|
|
|
|
},
|
2009-03-17 17:57:53 -07:00
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
_unregisterListeners: function unregisterListeners(event) {
|
|
|
|
this._unregisterListenersForWindow(event.target);
|
|
|
|
},
|
2009-06-22 15:53:42 -07:00
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
_unregisterListenersForWindow: function unregisterListenersFW(window) {
|
|
|
|
this._log.trace("Removing tab listeners in window");
|
|
|
|
window.removeEventListener("unload", this._unregisterListeners, false);
|
|
|
|
for each (let topic in this._topics) {
|
|
|
|
window.removeEventListener(topic, this.onTab, false);
|
|
|
|
}
|
2009-03-17 17:57:53 -07:00
|
|
|
},
|
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
_enabled: false,
|
2009-03-17 17:57:53 -07:00
|
|
|
observe: function TabTracker_observe(aSubject, aTopic, aData) {
|
2010-08-06 08:30:58 -07:00
|
|
|
switch (aTopic) {
|
|
|
|
case "weave:engine:start-tracking":
|
|
|
|
if (!this._enabled) {
|
|
|
|
Svc.Obs.add("private-browsing", this);
|
|
|
|
Svc.Obs.add("domwindowopened", this);
|
|
|
|
let wins = Svc.WinMediator.getEnumerator("navigator:browser");
|
|
|
|
while (wins.hasMoreElements())
|
|
|
|
this._registerListenersForWindow(wins.getNext());
|
|
|
|
this._enabled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "weave:engine:stop-tracking":
|
|
|
|
if (this._enabled) {
|
|
|
|
Svc.Obs.remove("private-browsing", this);
|
|
|
|
Svc.Obs.remove("domwindowopened", this);
|
|
|
|
let wins = Svc.WinMediator.getEnumerator("navigator:browser");
|
|
|
|
while (wins.hasMoreElements())
|
|
|
|
this._unregisterListenersForWindow(wins.getNext());
|
|
|
|
this._enabled = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case "domwindowopened":
|
|
|
|
// Add tab listeners now that a window has opened
|
|
|
|
let self = this;
|
|
|
|
aSubject.addEventListener("load", function onLoad(event) {
|
|
|
|
aSubject.removeEventListener("load", onLoad, false);
|
|
|
|
// Only register after the window is done loading to avoid unloads
|
|
|
|
self._registerListenersForWindow(aSubject);
|
|
|
|
}, false);
|
|
|
|
break;
|
|
|
|
case "private-browsing":
|
|
|
|
if (aData == "enter" && !PBPrefs.get("autostart"))
|
2010-11-11 11:00:41 -08:00
|
|
|
this.modified = false;
|
2010-01-06 09:54:15 -08:00
|
|
|
}
|
2008-06-03 14:45:53 -07:00
|
|
|
},
|
|
|
|
|
2009-11-25 15:14:56 -08:00
|
|
|
onTab: function onTab(event) {
|
2010-07-19 23:07:45 -07:00
|
|
|
if (Svc.Private.privateBrowsingEnabled && !PBPrefs.get("autostart")) {
|
|
|
|
this._log.trace("Ignoring tab event from private browsing.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-21 16:35:51 -07:00
|
|
|
this._log.trace("onTab event: " + event.type);
|
2010-11-11 11:00:41 -08:00
|
|
|
this.modified = true;
|
2009-11-12 14:48:54 -08:00
|
|
|
|
2010-04-21 16:35:51 -07:00
|
|
|
// For pageshow events, only give a partial score bump (~.1)
|
|
|
|
let chance = .1;
|
|
|
|
|
|
|
|
// For regular Tab events, do a full score bump and remember when it changed
|
|
|
|
if (event.type != "pageshow") {
|
|
|
|
chance = 1;
|
|
|
|
|
|
|
|
// Store a timestamp in the tab to track when it was last used
|
|
|
|
Svc.Session.setTabValue(event.originalTarget, "weaveLastUsed",
|
|
|
|
Math.floor(Date.now() / 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only increase the score by whole numbers, so use random for partial score
|
|
|
|
if (Math.random() < chance)
|
|
|
|
this.score++;
|
2009-02-11 19:14:25 -08:00
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|