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>
|
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");
|
2008-06-03 14:45:53 -07:00
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
Cu.import("resource://weave/async.js");
|
|
|
|
Cu.import("resource://weave/engines.js");
|
|
|
|
Cu.import("resource://weave/stores.js");
|
|
|
|
Cu.import("resource://weave/trackers.js");
|
2008-06-25 13:51:32 -07:00
|
|
|
Cu.import("resource://weave/constants.js");
|
2009-02-04 19:51:20 -08:00
|
|
|
Cu.import("resource://weave/type_records/tabs.js");
|
2009-02-09 20:23:42 -08:00
|
|
|
Cu.import("resource://weave/engines/clientData.js");
|
2008-06-03 14:45:53 -07:00
|
|
|
|
|
|
|
Function.prototype.async = Async.sugar;
|
|
|
|
|
2009-01-08 16:57:35 -08:00
|
|
|
function TabEngine() {
|
|
|
|
this._init();
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
TabEngine.prototype = {
|
2009-01-08 16:57:35 -08:00
|
|
|
__proto__: SyncEngine.prototype,
|
|
|
|
name: "tabs",
|
|
|
|
displayName: "Tabs",
|
|
|
|
logName: "Tabs",
|
|
|
|
_storeObj: TabStore,
|
|
|
|
_trackerObj: TabTracker,
|
2009-02-04 19:51:20 -08:00
|
|
|
_recordObj: TabSetRecord,
|
|
|
|
|
|
|
|
// 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];
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
2009-02-04 19:51:20 -08:00
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
};
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
function TabStore() {
|
2009-01-08 16:57:35 -08:00
|
|
|
this._TabStore_init();
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
TabStore.prototype = {
|
2009-02-04 19:51:20 -08:00
|
|
|
__proto__: Store.prototype,
|
2009-01-08 16:57:35 -08:00
|
|
|
_logName: "Tabs.Store",
|
2009-02-04 19:51:20 -08:00
|
|
|
_filePath: "weave/meta/tabSets.json",
|
|
|
|
_remoteClients: {},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
_TabStore_init: function TabStore__init() {
|
2009-02-06 11:19:50 -08:00
|
|
|
dump("Initializing TabStore!!\n");
|
2009-02-04 19:51:20 -08:00
|
|
|
this._init();
|
|
|
|
this._readFromFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
get _localClientGUID() {
|
2009-02-09 20:23:42 -08:00
|
|
|
return Clients.clientID;
|
2008-06-03 14:45:53 -07:00
|
|
|
},
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
get _localClientName() {
|
2009-02-09 20:23:42 -08:00
|
|
|
return Clients.clientName;
|
2009-02-04 19:51:20 -08:00
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-09 21:16:18 -08:00
|
|
|
get _fennecTabs() {
|
|
|
|
let wm = Cc["@mozilla.org/appshell/window-mediator;1"]
|
|
|
|
.getService(Ci.nsIWindowMediator);
|
|
|
|
let browserWindow = wm.getMostRecentWindow("navigator:browser");
|
|
|
|
return browserWindow.Browser._tabs;
|
|
|
|
},
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
_writeToFile: function TabStore_writeToFile() {
|
|
|
|
// use JSON service to serialize the records...
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("Writing out to file...");
|
2009-02-04 19:51:20 -08:00
|
|
|
let file = Utils.getProfileFile(
|
|
|
|
{path: this._filePath, autoCreate: true});
|
|
|
|
let jsonObj = {};
|
|
|
|
for (let id in this._remoteClients) {
|
|
|
|
jsonObj[id] = this._remoteClients[id].toJson();
|
|
|
|
}
|
|
|
|
let [fos] = Utils.open(file, ">");
|
|
|
|
fos.writeString(this._json.encode(jsonObj));
|
|
|
|
fos.close();
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
_readFromFile: function TabStore_readFromFile() {
|
|
|
|
// use JSON service to un-serialize the records...
|
|
|
|
// call on initialization.
|
|
|
|
// Put stuff into remoteClients.
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("Reading in from file...");
|
2009-02-04 19:51:20 -08:00
|
|
|
let file = Utils.getProfileFile(this._filePath);
|
|
|
|
if (!file.exists())
|
2008-06-30 15:13:07 -07:00
|
|
|
return;
|
2009-02-04 19:51:20 -08:00
|
|
|
try {
|
|
|
|
let [is] = Utils.open(file, "<");
|
|
|
|
let json = Utils.readStream(is);
|
|
|
|
is.close();
|
|
|
|
let jsonObj = this._json.decode(json);
|
|
|
|
for (let id in jsonObj) {
|
|
|
|
this._remoteClients[id] = new TabSetRecord();
|
|
|
|
this._remoteClients[id].fromJson(jsonObj[id]);
|
|
|
|
this._remoteClients[id].id = id;
|
2009-02-06 11:19:50 -08:00
|
|
|
}
|
2009-02-04 19:51:20 -08:00
|
|
|
} catch (e) {
|
|
|
|
this._log.warn("Failed to load saved tabs file" + e);
|
|
|
|
}
|
|
|
|
},
|
2008-06-30 15:13:07 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
get _sessionStore() {
|
|
|
|
let sessionStore = Cc["@mozilla.org/browser/sessionstore;1"].
|
|
|
|
getService(Ci.nsISessionStore);
|
|
|
|
this.__defineGetter__("_sessionStore", function() { return sessionStore;});
|
|
|
|
return this._sessionStore;
|
2008-06-03 14:45:53 -07:00
|
|
|
},
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
get _json() {
|
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
this.__defineGetter__("_json", function() {return json;});
|
|
|
|
return this._json;
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-09 21:16:18 -08:00
|
|
|
_addTabToRecord: function( tab, record ) {
|
|
|
|
// TODO contentDocument not defined for fennec's tab objects
|
|
|
|
let title = tab.contentDocument.title.innerHtml; // will this work?
|
|
|
|
this._log.debug("Wrapping a tab with title " + title);
|
|
|
|
let urlHistory = [];
|
|
|
|
let entries = tab.entries.slice(tab.entries.length - 10);
|
|
|
|
for (let entry in entries) {
|
|
|
|
urlHistory.push( entry.url );
|
|
|
|
}
|
|
|
|
record.addTab(title, urlHistory);
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-09 21:16:18 -08:00
|
|
|
_createLocalClientTabSetRecord: function TabStore__createLocalTabSet() {
|
|
|
|
// Test for existence of sessionStore. If it doesn't exist, then
|
|
|
|
// use get _fennecTabs instead.
|
2009-02-04 19:51:20 -08:00
|
|
|
let record = new TabSetRecord();
|
|
|
|
record.setClientName( this._localClientName );
|
|
|
|
|
2009-02-09 21:16:18 -08:00
|
|
|
if (Cc["@mozilla.org/browser/sessionstore;1"]) {
|
|
|
|
let session = this._json.decode(this._sessionStore.getBrowserState());
|
|
|
|
for (let i = 0; i < session.windows.length; i++) {
|
|
|
|
let window = session.windows[i];
|
|
|
|
/* For some reason, session store uses one-based array index references,
|
|
|
|
(f.e. in the "selectedWindow" and each tab's "index" properties), so we
|
|
|
|
convert them to and from JavaScript's zero-based indexes as needed. */
|
|
|
|
let windowID = i + 1;
|
|
|
|
|
|
|
|
for (let j = 0; j < window.tabs.length; j++) {
|
|
|
|
this._addTabToRecord(window.tabs[j], record);
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
2009-02-09 21:16:18 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for each ( let tab in this._fennecTabs) {
|
|
|
|
this._addTabToRecord(tab, record);
|
2009-02-04 19:51:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return record;
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
itemExists: function TabStore_itemExists(id) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("ItemExists called.");
|
2009-02-04 19:51:20 -08:00
|
|
|
if (id == this._localClientGUID) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("It's me.");
|
2009-02-04 19:51:20 -08:00
|
|
|
return true;
|
|
|
|
} else if (this._remoteClients[id]) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("It's somebody else.");
|
2009-02-04 19:51:20 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("It doesn't exist!");
|
2009-02-04 19:51:20 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
createRecord: function TabStore_createRecord(id) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("CreateRecord called for id " + id );
|
2009-02-04 19:51:20 -08:00
|
|
|
let record;
|
|
|
|
if (id == this._localClientGUID) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("That's Me!");
|
2009-02-04 19:51:20 -08:00
|
|
|
record = this._createLocalClientTabSetRecord();
|
|
|
|
} else {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("That's Somebody Else.");
|
2009-02-04 19:51:20 -08:00
|
|
|
record = this._remoteClients[id];
|
|
|
|
}
|
|
|
|
record.id = id;
|
|
|
|
return record;
|
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
changeItemId: function TabStore_changeItemId(oldId, newId) {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("changeItemId called.");
|
2009-02-04 19:51:20 -08:00
|
|
|
if (this._remoteClients[oldId]) {
|
|
|
|
let record = this._remoteClients[oldId];
|
|
|
|
record.id = newId;
|
|
|
|
delete this._remoteClients[oldId];
|
|
|
|
this._remoteClients[newId] = record;
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
2009-02-04 19:51:20 -08:00
|
|
|
},
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-09 20:23:42 -08:00
|
|
|
getAllIDs: function TabStore_getAllIds() {
|
2009-02-06 11:19:50 -08:00
|
|
|
this._log.debug("getAllIds called.");
|
2009-02-04 19:51:20 -08:00
|
|
|
let items = {};
|
|
|
|
items[ this._localClientGUID ] = true;
|
|
|
|
for (let id in this._remoteClients) {
|
|
|
|
items[id] = true;
|
|
|
|
}
|
2008-06-03 14:45:53 -07:00
|
|
|
return items;
|
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function TabStore_wipe() {
|
2009-02-04 19:51:20 -08:00
|
|
|
this._log.debug("Wipe called. Clearing cache of remote client tabs.");
|
|
|
|
this._remoteClients = {};
|
|
|
|
this._writeToFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
create: function TabStore_create(record) {
|
|
|
|
if (record.id == this._localClientGUID)
|
|
|
|
return; // can't happen?
|
|
|
|
this._log.debug("Create called. Adding remote client record for ");
|
|
|
|
this._log.debug(record.getClientName());
|
|
|
|
this._remoteClients[record.id] = record;
|
|
|
|
this._writeToFile();
|
|
|
|
// TODO writing to file after every change is inefficient. How do we
|
|
|
|
// make sure to do it (or at least flush it) only after sync is done?
|
|
|
|
// override syncFinished
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function TabStore_update(record) {
|
|
|
|
if (record.id == this._localClientGUID)
|
|
|
|
return; // can't happen?
|
|
|
|
this._log.debug("Update called. Updating remote client record for");
|
|
|
|
this._log.debug(record.getClientName());
|
|
|
|
this._remoteClients[record.id] = record;
|
|
|
|
this._writeToFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function TabStore_remove(record) {
|
|
|
|
if (record.id == this._localClientGUID)
|
|
|
|
return; // can't happen?
|
|
|
|
this._log.debug("Remove called. Deleting record with id " + record.id);
|
|
|
|
delete this._remoteClients[record.id];
|
|
|
|
this._writeToFile();
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
2009-02-04 19:51:20 -08:00
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
};
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
function TabTracker() {
|
2009-02-06 17:50:12 -08:00
|
|
|
this._TabTracker_init();
|
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
|
|
|
_logName: "TabTracker",
|
2009-02-04 19:51:20 -08:00
|
|
|
file: "tab_tracker",
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-06 17:50:12 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
|
|
|
|
|
|
|
_TabTracker_init: function TabTracker__init() {
|
|
|
|
this._init();
|
2008-06-03 14:45:53 -07:00
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
// Register me as an observer!! Listen for tabs opening and closing:
|
2009-02-06 17:50:12 -08:00
|
|
|
// TODO We need to also register with any windows that are ALREDY
|
2009-02-09 21:16:18 -08:00
|
|
|
// open. On Fennec maybe try to get this from getBrowser(), which is
|
|
|
|
// defined differently but should still exist...
|
2009-02-06 17:50:12 -08:00
|
|
|
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Ci.nsIWindowWatcher);
|
|
|
|
dump("Initialized TabTracker\n");
|
|
|
|
ww.registerNotification(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function TabTracker_observe(aSubject, aTopic, aData) {
|
|
|
|
dump("TabTracker spotted window open/close...\n");
|
|
|
|
let window = aSubject.QueryInterface(Ci.nsIDOMWindow);
|
|
|
|
// TODO: Not all windows have tabContainers. Fennec windows don't,
|
|
|
|
// for instance.
|
2009-02-09 20:23:42 -08:00
|
|
|
if (! window.getBrowser)
|
|
|
|
return;
|
|
|
|
let browser = window.getBrowser();
|
|
|
|
if (! browser.tabContainer)
|
|
|
|
return;
|
|
|
|
let container = browser.tabContainer;
|
2009-02-06 17:50:12 -08:00
|
|
|
if (aTopic == "domwindowopened") {
|
|
|
|
container.addEventListener("TabOpen", this.onTabChanged, false);
|
|
|
|
container.addEventListener("TabClose", this.onTabChanged, false);
|
|
|
|
} else if (aTopic == "domwindowclosed") {
|
|
|
|
container.removeEventListener("TabOpen", this.onTabChanged, false);
|
|
|
|
container.removeEventListener("TabClose", this.onTabChanged, false);
|
|
|
|
}
|
2008-06-03 14:45:53 -07:00
|
|
|
},
|
|
|
|
|
2009-02-04 19:51:20 -08:00
|
|
|
onTabChanged: function TabTracker_onTabChanged(event) {
|
|
|
|
this._score += 10; // meh? meh.
|
2008-06-03 14:45:53 -07:00
|
|
|
}
|
|
|
|
}
|