2007-12-10 21:38:53 -08: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) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
2008-06-12 12:36:58 -07:00
|
|
|
* Myk Melez <myk@mozilla.org>
|
2007-12-10 21:38:53 -08: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-03-25 15:14:00 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Weave'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2008-06-11 10:38:25 -07:00
|
|
|
// The following constants determine when Weave will automatically sync data.
|
|
|
|
|
|
|
|
// An interval of one minute, initial threshold of 100, and step of 5 means
|
|
|
|
// that we'll try to sync each engine 21 times, once per minute, at
|
|
|
|
// consecutively lower thresholds (from 100 down to 5 in steps of 5 and then
|
|
|
|
// one more time with the threshold set to the minimum 1) before resetting
|
|
|
|
// the engine's threshold to the initial value and repeating the cycle
|
|
|
|
// until at some point the engine's score exceeds the threshold, at which point
|
|
|
|
// we'll sync it, reset its threshold to the initial value, rinse, and repeat.
|
|
|
|
|
|
|
|
// How long we wait between sync checks.
|
|
|
|
const SCHEDULED_SYNC_INTERVAL = 60 * 1000; // one minute
|
|
|
|
|
|
|
|
// INITIAL_THRESHOLD represents the value an engine's score has to exceed
|
|
|
|
// in order for us to sync it the first time we start up (and the first time
|
|
|
|
// we do a sync check after having synced the engine or reset the threshold).
|
|
|
|
const INITIAL_THRESHOLD = 100;
|
|
|
|
|
|
|
|
// THRESHOLD_DECREMENT_STEP is the amount by which we decrement an engine's
|
|
|
|
// threshold each time we do a sync check and don't sync that engine.
|
|
|
|
const THRESHOLD_DECREMENT_STEP = 5;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
2008-03-27 19:12:53 -07:00
|
|
|
Cu.import("resource://weave/wrap.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/crypto.js");
|
|
|
|
Cu.import("resource://weave/engines.js");
|
|
|
|
Cu.import("resource://weave/dav.js");
|
|
|
|
Cu.import("resource://weave/identity.js");
|
2008-03-07 01:56:36 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
2008-06-03 11:11:44 -07:00
|
|
|
Cu.import("resource://weave/engines/cookies.js");
|
2008-06-03 12:38:48 -07:00
|
|
|
Cu.import("resource://weave/engines/bookmarks.js");
|
2008-06-03 13:56:16 -07:00
|
|
|
Cu.import("resource://weave/engines/history.js");
|
2008-06-03 14:08:53 -07:00
|
|
|
Cu.import("resource://weave/engines/passwords.js");
|
2008-06-03 14:20:51 -07:00
|
|
|
Cu.import("resource://weave/engines/forms.js");
|
2008-06-03 14:45:53 -07:00
|
|
|
Cu.import("resource://weave/engines/tabs.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
Function.prototype.async = Async.sugar;
|
2008-03-25 15:14:00 -07:00
|
|
|
|
|
|
|
// for export
|
|
|
|
let Weave = {};
|
2008-03-31 07:20:09 -07:00
|
|
|
Cu.import("resource://weave/constants.js", Weave);
|
|
|
|
Cu.import("resource://weave/util.js", Weave);
|
|
|
|
Cu.import("resource://weave/async.js", Weave);
|
|
|
|
Cu.import("resource://weave/crypto.js", Weave);
|
2008-06-25 13:51:39 -07:00
|
|
|
Cu.import("resource://weave/notifications.js", Weave);
|
2008-03-31 07:20:09 -07:00
|
|
|
Cu.import("resource://weave/identity.js", Weave);
|
|
|
|
Cu.import("resource://weave/dav.js", Weave);
|
|
|
|
Cu.import("resource://weave/stores.js", Weave);
|
|
|
|
Cu.import("resource://weave/syncCores.js", Weave);
|
|
|
|
Cu.import("resource://weave/engines.js", Weave);
|
|
|
|
Cu.import("resource://weave/service.js", Weave);
|
2008-03-25 15:14:00 -07:00
|
|
|
Utils.lazy(Weave, 'Service', WeaveSvc);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Service singleton
|
|
|
|
* Main entry point into Weave's sync framework
|
|
|
|
*/
|
|
|
|
|
2008-06-17 19:54:09 -07:00
|
|
|
function WeaveSvc(engines) {
|
2008-05-23 12:08:03 -07:00
|
|
|
this._startupFinished = false;
|
2008-03-27 19:12:53 -07:00
|
|
|
this._initLogs();
|
|
|
|
this._log.info("Weave Sync Service Initializing");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
// Create Weave identities (for logging in, and for encryption)
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.set('WeaveID', new Identity('Mozilla Services Password', this.username));
|
|
|
|
ID.set('WeaveCryptoID',
|
|
|
|
new Identity('Mozilla Services Encryption Passphrase', this.username));
|
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
// Set up aliases for other modules to use our IDs
|
|
|
|
ID.setAlias('WeaveID', 'DAV:default');
|
|
|
|
ID.setAlias('WeaveCryptoID', 'Engine:PBE:default');
|
|
|
|
|
2008-06-17 19:54:09 -07:00
|
|
|
if (typeof engines == "undefined")
|
|
|
|
engines = [
|
|
|
|
new BookmarksEngine(),
|
|
|
|
new HistoryEngine(),
|
|
|
|
new CookieEngine(),
|
|
|
|
new PasswordEngine(),
|
|
|
|
new FormEngine(),
|
|
|
|
new TabEngine()
|
|
|
|
];
|
|
|
|
|
|
|
|
// Register engines
|
|
|
|
for (let i = 0; i < engines.length; i++)
|
|
|
|
Engines.register(engines[i]);
|
2008-04-14 18:53:35 -07:00
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
// Other misc startup
|
2008-03-27 19:12:53 -07:00
|
|
|
Utils.prefs.addObserver("", this, false);
|
2008-06-23 16:10:31 -07:00
|
|
|
this._os.addObserver(this, "quit-application", true);
|
2008-03-27 19:12:53 -07:00
|
|
|
|
|
|
|
if (!this.enabled) {
|
|
|
|
this._log.info("Weave Sync disabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-03-25 15:14:00 -07:00
|
|
|
WeaveSvc.prototype = {
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-27 19:12:53 -07:00
|
|
|
_notify: Wrap.notify,
|
|
|
|
_lock: Wrap.lock,
|
|
|
|
_localLock: Wrap.localLock,
|
|
|
|
_osPrefix: "weave:service:",
|
2008-06-12 12:36:58 -07:00
|
|
|
_loggedIn: false,
|
2008-06-30 14:00:06 -07:00
|
|
|
_syncInProgress: false,
|
2008-02-13 14:30:44 -08:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
__os: null,
|
|
|
|
get _os() {
|
|
|
|
if (!this.__os)
|
|
|
|
this.__os = Cc["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Ci.nsIObserverService);
|
|
|
|
return this.__os;
|
|
|
|
},
|
|
|
|
|
|
|
|
__dirSvc: null,
|
|
|
|
get _dirSvc() {
|
|
|
|
if (!this.__dirSvc)
|
|
|
|
this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties);
|
|
|
|
return this.__dirSvc;
|
|
|
|
},
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
__json: null,
|
|
|
|
get _json() {
|
|
|
|
if (!this.__json)
|
|
|
|
this.__json = Cc["@mozilla.org/dom/json;1"].
|
|
|
|
createInstance(Ci.nsIJSON);
|
|
|
|
return this.__json;
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// Timer object for automagically syncing
|
|
|
|
_scheduleTimer: null,
|
|
|
|
|
|
|
|
get username() {
|
2008-03-19 15:17:04 -07:00
|
|
|
return Utils.prefs.getCharPref("username");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
set username(value) {
|
2007-12-20 12:18:41 -08:00
|
|
|
if (value)
|
2008-03-19 15:17:04 -07:00
|
|
|
Utils.prefs.setCharPref("username", value);
|
2007-12-20 12:18:41 -08:00
|
|
|
else
|
2008-03-19 15:17:04 -07:00
|
|
|
Utils.prefs.clearUserPref("username");
|
2007-12-20 12:18:41 -08:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// fixme - need to loop over all Identity objects - needs some rethinking...
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.get('WeaveID').username = value;
|
|
|
|
ID.get('WeaveCryptoID').username = value;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
get password() { return ID.get('WeaveID').password; },
|
|
|
|
set password(value) { ID.get('WeaveID').password = value; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
get passphrase() { return ID.get('WeaveCryptoID').password; },
|
|
|
|
set passphrase(value) { ID.get('WeaveCryptoID').password = value; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-06 14:18:50 -07:00
|
|
|
get userPath() { return ID.get('WeaveID').username; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
get isLoggedIn() this._loggedIn,
|
|
|
|
get enabled() Utils.prefs.getBoolPref("enabled"),
|
2008-02-13 16:07:11 -08:00
|
|
|
|
|
|
|
get schedule() {
|
|
|
|
if (!this.enabled)
|
|
|
|
return 0; // manual/off
|
2008-03-19 15:17:04 -07:00
|
|
|
return Utils.prefs.getIntPref("schedule");
|
2008-02-13 16:07:11 -08:00
|
|
|
},
|
2008-06-15 08:21:44 -07:00
|
|
|
|
2008-05-23 12:08:03 -07:00
|
|
|
onWindowOpened: function Weave__onWindowOpened() {
|
2008-05-23 12:22:08 -07:00
|
|
|
if (!this._startupFinished) {
|
|
|
|
if (Utils.prefs.getBoolPref("autoconnect") &&
|
2008-06-30 17:55:48 -07:00
|
|
|
this.username && this.username != 'nobody')
|
|
|
|
this._initialLoginAndSync.async(this);
|
2008-05-23 12:08:03 -07:00
|
|
|
this._startupFinished = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-30 17:55:48 -07:00
|
|
|
_initialLoginAndSync: function Weave__initialLoginAndSync() {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
// Any exceptions thrown by the login process will be propagated
|
|
|
|
// out here, so there's no need to check to see if the login was
|
|
|
|
// successful; if it wasn't, this coroutine will just abort.
|
|
|
|
|
|
|
|
yield this.login(self.cb);
|
|
|
|
yield this.sync(self.cb);
|
|
|
|
},
|
|
|
|
|
2008-02-13 16:07:11 -08:00
|
|
|
_setSchedule: function Weave__setSchedule(schedule) {
|
|
|
|
switch (this.schedule) {
|
2007-12-10 21:38:53 -08:00
|
|
|
case 0:
|
2008-02-13 16:07:11 -08:00
|
|
|
this._disableSchedule();
|
2007-12-10 21:38:53 -08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
this._enableSchedule();
|
|
|
|
break;
|
|
|
|
default:
|
2008-02-13 16:07:11 -08:00
|
|
|
this._log.info("Invalid Weave scheduler setting: " + schedule);
|
2007-12-10 21:38:53 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_enableSchedule: function WeaveSvc__enableSchedule() {
|
2008-02-13 16:07:11 -08:00
|
|
|
if (this._scheduleTimer) {
|
|
|
|
this._scheduleTimer.cancel();
|
|
|
|
this._scheduleTimer = null;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
this._scheduleTimer = Cc["@mozilla.org/timer;1"].
|
|
|
|
createInstance(Ci.nsITimer);
|
2008-02-13 14:30:44 -08:00
|
|
|
let listener = new Utils.EventListener(Utils.bind2(this, this._onSchedule));
|
2008-06-11 10:38:25 -07:00
|
|
|
this._scheduleTimer.initWithCallback(listener, SCHEDULED_SYNC_INTERVAL,
|
2007-12-10 21:38:53 -08:00
|
|
|
this._scheduleTimer.TYPE_REPEATING_SLACK);
|
2008-02-13 16:07:11 -08:00
|
|
|
this._log.info("Weave scheduler enabled");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_disableSchedule: function WeaveSvc__disableSchedule() {
|
2008-02-13 16:07:11 -08:00
|
|
|
if (this._scheduleTimer) {
|
|
|
|
this._scheduleTimer.cancel();
|
|
|
|
this._scheduleTimer = null;
|
|
|
|
}
|
|
|
|
this._log.info("Weave scheduler disabled");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_onSchedule: function WeaveSvc__onSchedule() {
|
2008-02-13 16:07:11 -08:00
|
|
|
if (this.enabled) {
|
2008-07-07 22:30:32 -07:00
|
|
|
if (DAV.locked) {
|
2008-06-26 16:37:38 -07:00
|
|
|
this._log.info("Skipping scheduled sync; local operation in progress")
|
|
|
|
} else {
|
|
|
|
this._log.info("Running scheduled sync");
|
2008-06-30 14:00:06 -07:00
|
|
|
this._notify("sync-as-needed", this._lock(this._syncAsNeeded)).async(this);
|
2008-06-26 16:37:38 -07:00
|
|
|
}
|
2008-02-13 16:07:11 -08:00
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_initLogs: function WeaveSvc__initLogs() {
|
2007-12-10 21:38:53 -08:00
|
|
|
this._log = Log4Moz.Service.getLogger("Service.Main");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.service.main")];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let formatter = new Log4Moz.BasicFormatter();
|
2007-12-10 21:38:53 -08:00
|
|
|
let root = Log4Moz.Service.rootLogger;
|
2008-03-19 15:17:04 -07:00
|
|
|
root.level = Log4Moz.Level[Utils.prefs.getCharPref("log.rootLogger")];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let capp = new Log4Moz.ConsoleAppender(formatter);
|
2008-03-19 15:17:04 -07:00
|
|
|
capp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.console")];
|
2007-12-10 21:38:53 -08:00
|
|
|
root.addAppender(capp);
|
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let dapp = new Log4Moz.DumpAppender(formatter);
|
2008-03-19 15:17:04 -07:00
|
|
|
dapp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.dump")];
|
2007-12-10 21:38:53 -08:00
|
|
|
root.addAppender(dapp);
|
|
|
|
|
2007-12-19 19:49:28 -08:00
|
|
|
let brief = this._dirSvc.get("ProfD", Ci.nsIFile);
|
|
|
|
brief.QueryInterface(Ci.nsILocalFile);
|
|
|
|
brief.append("weave");
|
|
|
|
brief.append("logs");
|
|
|
|
brief.append("brief-log.txt");
|
|
|
|
if (!brief.exists())
|
|
|
|
brief.create(brief.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
|
|
|
|
let verbose = brief.parent.clone();
|
|
|
|
verbose.append("verbose-log.txt");
|
|
|
|
if (!verbose.exists())
|
|
|
|
verbose.create(verbose.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
this._briefApp = new Log4Moz.RotatingFileAppender(brief, formatter);
|
2008-03-28 19:36:11 -07:00
|
|
|
this._briefApp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.briefLog")];
|
|
|
|
root.addAppender(this._briefApp);
|
2008-05-29 18:15:50 -07:00
|
|
|
this._debugApp = new Log4Moz.RotatingFileAppender(verbose, formatter);
|
2008-03-28 19:36:11 -07:00
|
|
|
this._debugApp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.debugLog")];
|
|
|
|
root.addAppender(this._debugApp);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-28 19:36:11 -07:00
|
|
|
clearLogs: function WeaveSvc_clearLogs() {
|
|
|
|
this._briefApp.clear();
|
|
|
|
this._debugApp.clear();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_uploadVersion: function WeaveSvc__uploadVersion() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.MKCOL("meta", self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
let ret = yield;
|
|
|
|
if (!ret)
|
|
|
|
throw "Could not create meta information directory";
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.PUT("meta/version", STORAGE_FORMAT_VERSION, self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
ret = yield;
|
|
|
|
Utils.ensureStatus(ret.status, "Could not upload server version file");
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-03-19 18:42:12 -07:00
|
|
|
// force a server wipe when the version is lower than ours (or there is none)
|
2008-06-30 14:00:06 -07:00
|
|
|
_versionCheck: function WeaveSvc__versionCheck() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-03-19 18:42:12 -07:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.GET("meta/version", self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
let ret = yield;
|
|
|
|
|
|
|
|
if (!Utils.checkStatus(ret.status)) {
|
|
|
|
this._log.info("Server has no version file. Wiping server data.");
|
2008-06-30 14:00:06 -07:00
|
|
|
yield this._serverWipe.async(this, self.cb);
|
|
|
|
yield this._uploadVersion.async(this, self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
|
|
|
|
} else if (ret.responseText < STORAGE_FORMAT_VERSION) {
|
|
|
|
this._log.info("Server version too low. Wiping server data.");
|
2008-06-30 14:00:06 -07:00
|
|
|
yield this._serverWipe.async(this, self.cb);
|
|
|
|
yield this._uploadVersion.async(this, self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
|
|
|
|
} else if (ret.responseText > STORAGE_FORMAT_VERSION) {
|
|
|
|
// FIXME: should we do something here?
|
2007-12-14 18:07:25 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
_checkUserDir: function WeaveSvc__checkUserDir() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-05-15 18:08:13 -07:00
|
|
|
let prefix = DAV.defaultPrefix;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
this._log.trace("Checking user directory exists");
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-04-01 23:43:14 -07:00
|
|
|
try {
|
2008-05-15 18:08:13 -07:00
|
|
|
DAV.defaultPrefix = '';
|
2008-04-01 23:43:14 -07:00
|
|
|
DAV.MKCOL("user/" + this.userPath, self.cb);
|
|
|
|
let ret = yield;
|
|
|
|
if (!ret)
|
|
|
|
throw "Could not create user directory";
|
|
|
|
}
|
2008-05-15 18:08:13 -07:00
|
|
|
catch (e) { throw e; }
|
|
|
|
finally { DAV.defaultPrefix = prefix; }
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 18:09:30 -07:00
|
|
|
// Retrieves the keypair for the given Identity object and inserts
|
|
|
|
// its information into the Identity object. If no Identity object
|
|
|
|
// is supplied, the 'WeaveCryptoID' identity is used.
|
|
|
|
//
|
|
|
|
// If 'createIfNecessary' is true (the default), then this function
|
|
|
|
// will create a new keypair if none currently exists.
|
|
|
|
//
|
|
|
|
// This coroutine assumes the DAV singleton's prefix is set to the
|
|
|
|
// proper user-specific directory.
|
|
|
|
//
|
|
|
|
// If the password associated with the Identity cannot be used to
|
|
|
|
// decrypt the private key, an exception is raised.
|
2008-06-30 16:50:19 -07:00
|
|
|
_getKeypair : function WeaveSvc__getKeypair(id, createIfNecessary) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-05-23 12:08:03 -07:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
if ("none" == Utils.prefs.getCharPref("encryption"))
|
|
|
|
return;
|
|
|
|
|
2008-06-30 16:50:19 -07:00
|
|
|
if (typeof(id) == "undefined")
|
|
|
|
id = ID.get('WeaveCryptoID');
|
|
|
|
|
|
|
|
if (typeof(createIfNecessary) == "undefined")
|
|
|
|
createIfNecessary = true;
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
this._log.trace("Retrieving keypair from server");
|
|
|
|
|
2008-06-30 16:50:19 -07:00
|
|
|
let statuses = [[200, 300]];
|
|
|
|
if (createIfNecessary)
|
|
|
|
statuses.push(404);
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
// XXX this kind of replaces _keyCheck
|
|
|
|
// seems like key generation should only happen during setup?
|
|
|
|
DAV.GET("private/privkey", self.cb);
|
|
|
|
let privkeyResp = yield;
|
|
|
|
Utils.ensureStatus(privkeyResp.status,
|
2008-06-30 16:50:19 -07:00
|
|
|
"Could not get private key from server", statuses);
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
DAV.GET("public/pubkey", self.cb);
|
|
|
|
let pubkeyResp = yield;
|
|
|
|
Utils.ensureStatus(pubkeyResp.status,
|
2008-06-30 16:50:19 -07:00
|
|
|
"Could not get public key from server", statuses);
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
if (privkeyResp.status == 404 || pubkeyResp.status == 404) {
|
2008-06-30 14:00:06 -07:00
|
|
|
yield this._generateKeys.async(this, self.cb);
|
|
|
|
return;
|
2008-03-31 07:20:09 -07:00
|
|
|
}
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
let privkeyData = this._json.decode(privkeyResp.responseText);
|
|
|
|
let pubkeyData = this._json.decode(pubkeyResp.responseText);
|
|
|
|
|
|
|
|
if (!privkeyData || !pubkeyData)
|
|
|
|
throw "Bad keypair JSON";
|
|
|
|
if (privkeyData.version != 1 || pubkeyData.version != 1)
|
|
|
|
throw "Unexpected keypair data version";
|
|
|
|
if (privkeyData.algorithm != "RSA" || pubkeyData.algorithm != "RSA")
|
|
|
|
throw "Only RSA keys currently supported";
|
|
|
|
|
|
|
|
|
|
|
|
id.keypairAlg = privkeyData.algorithm;
|
|
|
|
id.privkey = privkeyData.privkey;
|
|
|
|
id.privkeyWrapIV = privkeyData.privkeyIV;
|
|
|
|
id.passphraseSalt = privkeyData.privkeySalt;
|
|
|
|
|
|
|
|
id.pubkey = pubkeyData.pubkey;
|
|
|
|
|
2008-06-30 16:50:19 -07:00
|
|
|
let isValid = yield Crypto.isPassphraseValid.async(Crypto, self.cb, id);
|
|
|
|
if (!isValid)
|
|
|
|
throw new Error("Passphrase is not valid.");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_generateKeys: function WeaveSvc__generateKeys() {
|
2008-03-07 04:20:55 -08:00
|
|
|
let self = yield;
|
|
|
|
|
2008-03-19 18:42:12 -07:00
|
|
|
this._log.debug("Generating new RSA key");
|
2008-04-14 18:53:35 -07:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
// RSAkeygen will set the needed |id| properties.
|
2008-04-14 18:53:35 -07:00
|
|
|
let id = ID.get('WeaveCryptoID');
|
|
|
|
Crypto.RSAkeygen.async(Crypto, self.cb, id);
|
2008-06-23 16:23:57 -07:00
|
|
|
yield;
|
2008-03-19 18:42:12 -07:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.MKCOL("private/", self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
let ret = yield;
|
|
|
|
if (!ret)
|
|
|
|
throw "Could not create private key directory";
|
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.MKCOL("public/", self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
ret = yield;
|
|
|
|
if (!ret)
|
|
|
|
throw "Could not create public key directory";
|
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
let privkeyData = { version : 1,
|
|
|
|
algorithm : id.keypairAlg,
|
|
|
|
privkey : id.privkey,
|
|
|
|
privkeyIV : id.privkeyWrapIV,
|
|
|
|
privkeySalt : id.passphraseSalt
|
|
|
|
};
|
|
|
|
let data = this._json.encode(privkeyData);
|
|
|
|
|
|
|
|
DAV.PUT("private/privkey", data, self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
ret = yield;
|
|
|
|
Utils.ensureStatus(ret.status, "Could not upload private key");
|
2008-03-07 04:20:55 -08:00
|
|
|
|
2008-06-23 16:23:57 -07:00
|
|
|
|
|
|
|
let pubkeyData = { version : 1,
|
|
|
|
algorithm : id.keypairAlg,
|
2008-06-30 14:00:06 -07:00
|
|
|
pubkey : id.pubkey
|
2008-06-23 16:23:57 -07:00
|
|
|
};
|
|
|
|
data = this._json.encode(pubkeyData);
|
|
|
|
|
|
|
|
DAV.PUT("public/pubkey", data, self.cb);
|
2008-03-19 18:42:12 -07:00
|
|
|
ret = yield;
|
|
|
|
Utils.ensureStatus(ret.status, "Could not upload public key");
|
2008-03-07 04:20:55 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:10:31 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
observe: function WeaveSvc__observe(subject, topic, data) {
|
2008-06-23 16:10:31 -07:00
|
|
|
switch (topic) {
|
|
|
|
case "nsPref:changed":
|
|
|
|
switch (data) {
|
|
|
|
case "enabled": // this works because this.schedule is 0 when disabled
|
|
|
|
case "schedule":
|
|
|
|
this._setSchedule(this.schedule);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "quit-application":
|
|
|
|
this._onQuitApplication();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_onQuitApplication: function WeaveSvc__onQuitApplication() {
|
2008-06-23 16:10:31 -07:00
|
|
|
if (!this.enabled ||
|
|
|
|
!Utils.prefs.getBoolPref("syncOnQuit.enabled") ||
|
|
|
|
!this._loggedIn)
|
2008-02-13 16:07:11 -08:00
|
|
|
return;
|
|
|
|
|
2008-06-23 16:10:31 -07:00
|
|
|
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
|
|
|
|
getService(Ci.nsIWindowWatcher);
|
|
|
|
|
|
|
|
// This window has to be modal to prevent the application from quitting
|
|
|
|
// until the sync finishes and the window closes.
|
|
|
|
let window = ww.openWindow(null,
|
|
|
|
"chrome://weave/content/status.xul",
|
|
|
|
"Weave:status",
|
|
|
|
"chrome,centerscreen,modal",
|
|
|
|
null);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// These are global (for all engines)
|
|
|
|
|
2008-06-30 16:50:19 -07:00
|
|
|
verifyPassphrase: function WeaveSvc_verifyPassphrase(username, password,
|
|
|
|
passphrase) {
|
|
|
|
this._localLock(this._notify("verify-passphrase",
|
|
|
|
this._verifyPassphrase,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
passphrase)).async(this, null);
|
|
|
|
},
|
|
|
|
|
|
|
|
_verifyPassphrase: function WeaveSvc__verifyPassphrase(username, password,
|
|
|
|
passphrase) {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
this._log.debug("Verifying passphrase");
|
|
|
|
|
|
|
|
yield this._verifyLogin.async(this, self.cb, username, password);
|
|
|
|
let id = new Identity('Passphrase Verification', username);
|
|
|
|
id.setTempPassword(passphrase);
|
|
|
|
// XXX: We're not checking the version of the server here, in part because
|
|
|
|
// we have no idea what to do if the version is different than we expect
|
|
|
|
// it to be.
|
|
|
|
yield this._getKeypair.async(this, self.cb, id, false);
|
|
|
|
let isValid = yield Crypto.isPassphraseValid.async(Crypto, self.cb, id);
|
|
|
|
if (!isValid)
|
|
|
|
throw new Error("Passphrase is not valid.");
|
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
verifyLogin: function WeaveSvc_verifyLogin(username, password) {
|
2008-06-30 15:54:15 -07:00
|
|
|
this._log.debug("Verifying login for user " + username);
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
this._localLock(this._notify("verify-login", this._verifyLogin,
|
|
|
|
username, password)).async(this, null);
|
2008-03-30 08:40:23 -07:00
|
|
|
},
|
2008-06-25 00:22:53 -07:00
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_verifyLogin: function WeaveSvc__verifyLogin(username, password) {
|
2008-03-30 08:40:23 -07:00
|
|
|
let self = yield;
|
|
|
|
|
2008-06-30 23:41:47 -07:00
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
|
|
|
|
2008-06-25 00:22:53 -07:00
|
|
|
DAV.baseURL = Utils.prefs.getCharPref("serverURL");
|
|
|
|
DAV.defaultPrefix = "user/" + username;
|
2008-04-10 21:38:15 -07:00
|
|
|
|
2008-06-26 12:27:54 -07:00
|
|
|
this._log.info("Using server URL: " + DAV.baseURL + DAV.defaultPrefix);
|
|
|
|
let status = yield DAV.checkLogin.async(DAV, self.cb, username, password);
|
|
|
|
if (status == 404) {
|
|
|
|
// create user directory (for self-hosted webdav shares)
|
|
|
|
yield this._checkUserDir.async(this, self.cb);
|
|
|
|
status = yield DAV.checkLogin.async(DAV, self.cb, username, password);
|
|
|
|
}
|
2008-06-30 15:54:15 -07:00
|
|
|
|
2008-06-26 12:27:54 -07:00
|
|
|
Utils.ensureStatus(status, "Login verification failed");
|
2008-06-25 00:22:53 -07:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
login: function WeaveSvc_login(onComplete) {
|
2008-06-25 00:22:53 -07:00
|
|
|
this._localLock(this._notify("login", this._login)).async(this, onComplete);
|
|
|
|
},
|
2008-06-30 14:00:06 -07:00
|
|
|
_login: function WeaveSvc__login() {
|
2008-06-25 00:22:53 -07:00
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
this._log.debug("Logging in user " + this.username);
|
2008-03-30 08:40:23 -07:00
|
|
|
|
|
|
|
if (!this.username)
|
|
|
|
throw "No username set, login failed";
|
|
|
|
if (!this.password)
|
|
|
|
throw "No password given or found in password manager";
|
|
|
|
|
2008-06-30 15:54:15 -07:00
|
|
|
yield this._verifyLogin.async(this, self.cb, this.username,
|
|
|
|
this.password);
|
2008-06-26 12:27:54 -07:00
|
|
|
yield this._versionCheck.async(this, self.cb);
|
|
|
|
yield this._getKeypair.async(this, self.cb);
|
2008-04-10 21:38:15 -07:00
|
|
|
|
2008-06-12 12:36:58 -07:00
|
|
|
this._setSchedule(this.schedule);
|
|
|
|
|
2008-06-26 12:27:54 -07:00
|
|
|
this._loggedIn = true;
|
2008-03-30 08:40:23 -07:00
|
|
|
self.done(true);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
logout: function WeaveSvc_logout() {
|
2007-12-10 21:38:53 -08:00
|
|
|
this._log.info("Logging out");
|
2008-06-12 12:36:58 -07:00
|
|
|
this._disableSchedule();
|
2008-04-10 21:38:15 -07:00
|
|
|
this._loggedIn = false;
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.get('WeaveID').setTempPassword(null); // clear cached password
|
|
|
|
ID.get('WeaveCryptoID').setTempPassword(null); // and passphrase
|
2008-03-30 08:40:23 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:logout:success", "");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-30 08:40:23 -07:00
|
|
|
resetLock: function WeaveSvc_resetLock(onComplete) {
|
|
|
|
this._notify("reset-server-lock", this._resetLock).async(this, onComplete);
|
2008-03-27 19:12:53 -07:00
|
|
|
},
|
2008-03-30 08:40:23 -07:00
|
|
|
_resetLock: function WeaveSvc__resetLock() {
|
2008-03-27 19:12:53 -07:00
|
|
|
let self = yield;
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.forceUnlock.async(DAV, self.cb);
|
2008-03-30 08:40:23 -07:00
|
|
|
yield;
|
|
|
|
},
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-03-30 08:40:23 -07:00
|
|
|
serverWipe: function WeaveSvc_serverWipe(onComplete) {
|
2008-03-31 07:20:09 -07:00
|
|
|
let cb = function WeaveSvc_serverWipeCb() {
|
|
|
|
let self = yield;
|
|
|
|
this._serverWipe.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
this.logout();
|
|
|
|
self.done();
|
|
|
|
};
|
2008-06-23 16:22:05 -07:00
|
|
|
this._notify("server-wipe", this._lock(cb)).async(this, onComplete);
|
2008-03-30 08:40:23 -07:00
|
|
|
},
|
|
|
|
_serverWipe: function WeaveSvc__serverWipe() {
|
|
|
|
let self = yield;
|
2008-03-27 19:12:53 -07:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.listFiles.async(DAV, self.cb);
|
2008-03-30 08:40:23 -07:00
|
|
|
let names = yield;
|
|
|
|
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
2008-06-10 19:12:04 -07:00
|
|
|
if (names[i].match(/\.htaccess$/))
|
|
|
|
continue;
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.DELETE(names[i], self.cb);
|
2008-03-30 08:40:23 -07:00
|
|
|
let resp = yield;
|
|
|
|
this._log.debug(resp.status);
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// These are per-engine
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
sync: function WeaveSvc_sync(onComplete) {
|
2008-06-23 16:22:05 -07:00
|
|
|
this._notify("sync", this._lock(this._sync)).async(this, onComplete);
|
2008-03-27 19:12:53 -07:00
|
|
|
},
|
2008-06-11 10:38:25 -07:00
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_sync: function WeaveSvc__sync() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-06-29 07:00:53 -07:00
|
|
|
yield this._versionCheck.async(this, self.cb);
|
|
|
|
yield this._getKeypair.async(this, self.cb);
|
2008-03-31 07:20:09 -07:00
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
let engines = Engines.getAll();
|
|
|
|
for (let i = 0; i < engines.length; i++) {
|
2008-06-11 10:38:25 -07:00
|
|
|
if (!engines[i].enabled)
|
|
|
|
continue;
|
2008-06-29 07:00:53 -07:00
|
|
|
yield this._notify(engines[i].name + "-engine:sync",
|
|
|
|
this._syncEngine, engines[i]).async(this, self.cb);
|
2008-06-11 10:38:25 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// The values that engine scores must meet or exceed before we sync them
|
|
|
|
// as needed. These are engine-specific, as different kinds of data change
|
|
|
|
// at different rates, so we store them in a hash indexed by engine name.
|
|
|
|
_syncThresholds: {},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_syncAsNeeded: function WeaveSvc__syncAsNeeded() {
|
2008-06-11 10:38:25 -07:00
|
|
|
let self = yield;
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
yield this._versionCheck.async(this, self.cb);
|
|
|
|
yield this._getKeypair.async(this, self.cb);
|
2008-06-11 10:38:25 -07:00
|
|
|
|
|
|
|
let engines = Engines.getAll();
|
|
|
|
for each (let engine in engines) {
|
|
|
|
if (!engine.enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(engine.name in this._syncThresholds))
|
|
|
|
this._syncThresholds[engine.name] = INITIAL_THRESHOLD;
|
|
|
|
|
2008-06-11 20:07:35 -07:00
|
|
|
let score = engine._tracker.score;
|
|
|
|
if (score >= this._syncThresholds[engine.name]) {
|
|
|
|
this._log.debug(engine.name + " score " + score +
|
2008-06-11 14:16:03 -07:00
|
|
|
" reaches threshold " +
|
2008-06-11 10:38:25 -07:00
|
|
|
this._syncThresholds[engine.name] + "; syncing");
|
|
|
|
this._notify(engine.name + "-engine:sync",
|
|
|
|
this._syncEngine, engine).async(this, self.cb);
|
2008-04-15 17:21:34 -07:00
|
|
|
yield;
|
2008-06-11 10:38:25 -07:00
|
|
|
|
|
|
|
// Reset the engine's threshold to the initial value.
|
|
|
|
// Note: we do this after syncing the engine so that we'll try again
|
|
|
|
// next time around if syncing fails for some reason. The upside
|
|
|
|
// of this approach is that we'll sync again as soon as possible;
|
|
|
|
// but the downside is that if the error is caused by the server being
|
|
|
|
// overloaded, we'll contribute to the problem by trying to sync
|
|
|
|
// repeatedly at the maximum rate.
|
|
|
|
this._syncThresholds[engine.name] = INITIAL_THRESHOLD;
|
2008-04-15 17:21:34 -07:00
|
|
|
}
|
2008-06-11 10:38:25 -07:00
|
|
|
else {
|
2008-06-11 20:07:35 -07:00
|
|
|
this._log.debug(engine.name + " score " + score +
|
2008-06-11 14:16:03 -07:00
|
|
|
" does not reach threshold " +
|
2008-06-11 10:38:25 -07:00
|
|
|
this._syncThresholds[engine.name] + "; not syncing");
|
|
|
|
|
2008-06-11 13:50:47 -07:00
|
|
|
// Decrement the threshold by the standard amount, and if this puts it
|
|
|
|
// at or below zero, then set it to 1, the lowest possible value, where
|
|
|
|
// it'll stay until there's something to sync (whereupon we'll sync it,
|
|
|
|
// reset the threshold to the initial value, and start over again).
|
|
|
|
this._syncThresholds[engine.name] -= THRESHOLD_DECREMENT_STEP;
|
|
|
|
if (this._syncThresholds[engine.name] <= 0)
|
|
|
|
this._syncThresholds[engine.name] = 1;
|
2008-04-15 17:21:34 -07:00
|
|
|
}
|
2008-04-01 14:46:29 -07:00
|
|
|
}
|
2008-03-30 08:40:23 -07:00
|
|
|
},
|
2008-06-11 10:38:25 -07:00
|
|
|
|
2008-03-30 08:40:23 -07:00
|
|
|
_syncEngine: function WeaveSvc__syncEngine(engine) {
|
|
|
|
let self = yield;
|
2008-05-23 23:58:53 -07:00
|
|
|
try {
|
2008-06-29 07:00:53 -07:00
|
|
|
yield engine.sync(self.cb);
|
2008-06-10 13:45:37 -07:00
|
|
|
engine._tracker.resetScore();
|
2008-05-23 23:58:53 -07:00
|
|
|
} catch(e) {
|
2008-05-30 17:40:08 -07:00
|
|
|
this._log.error(Utils.exceptionStr(e));
|
2008-06-25 04:43:07 -07:00
|
|
|
this._log.error(Utils.stackTrace(e));
|
2008-05-23 23:58:53 -07:00
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
resetServer: function WeaveSvc_resetServer(onComplete) {
|
2008-06-23 16:22:05 -07:00
|
|
|
this._notify("reset-server",
|
|
|
|
this._lock(this._resetServer)).async(this, onComplete);
|
2008-03-27 19:12:53 -07:00
|
|
|
},
|
2008-06-30 14:00:06 -07:00
|
|
|
_resetServer: function WeaveSvc__resetServer() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-03-31 07:20:09 -07:00
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
let engines = Engines.getAll();
|
|
|
|
for (let i = 0; i < engines.length; i++) {
|
2008-04-15 17:21:34 -07:00
|
|
|
if (!engines[i].enabled)
|
|
|
|
continue;
|
2008-04-14 18:53:35 -07:00
|
|
|
engines[i].resetServer(self.cb);
|
|
|
|
yield;
|
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
resetClient: function WeaveSvc_resetClient(onComplete) {
|
2008-03-30 08:40:23 -07:00
|
|
|
this._localLock(this._notify("reset-client",
|
|
|
|
this._resetClient)).async(this, onComplete);
|
2008-03-07 04:20:55 -08:00
|
|
|
},
|
2008-06-30 14:00:06 -07:00
|
|
|
_resetClient: function WeaveSvc__resetClient() {
|
2008-03-25 23:01:34 -07:00
|
|
|
let self = yield;
|
2008-04-14 18:53:35 -07:00
|
|
|
let engines = Engines.getAll();
|
|
|
|
for (let i = 0; i < engines.length; i++) {
|
2008-04-15 17:21:34 -07:00
|
|
|
if (!engines[i].enabled)
|
|
|
|
continue;
|
2008-04-14 18:53:35 -07:00
|
|
|
engines[i].resetClient(self.cb);
|
|
|
|
yield;
|
|
|
|
}
|
2008-03-25 23:01:34 -07:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
shareData: function WeaveSvc_shareData(dataType,
|
2008-06-30 22:30:04 -07:00
|
|
|
isShareEnabled,
|
|
|
|
onComplete,
|
|
|
|
guid,
|
|
|
|
username) {
|
2008-06-06 17:33:44 -07:00
|
|
|
/* Shares data of the specified datatype (which must correspond to
|
|
|
|
one of the registered engines) with the user specified by username.
|
|
|
|
The data node indicated by guid will be shared, along with all its
|
|
|
|
children, if it has any. onComplete is a function that will be called
|
|
|
|
when sharing is done; it takes an argument that will be true or false
|
|
|
|
to indicate whether sharing succeeded or failed.
|
|
|
|
Implementation, as well as the interpretation of what 'guid' means,
|
2008-06-30 22:30:04 -07:00
|
|
|
is left up to the engine for the specific dataType.
|
|
|
|
|
|
|
|
isShareEnabled: true to start sharing, false to stop sharing.*/
|
2008-06-10 19:12:04 -07:00
|
|
|
|
2008-06-06 17:33:44 -07:00
|
|
|
let messageName = "share-" + dataType;
|
2008-06-06 19:22:23 -07:00
|
|
|
/* so for instance, if dataType is "bookmarks" then a message
|
|
|
|
"share-bookmarks" will be sent out to any observers who are listening
|
|
|
|
for it. As far as I know, there aren't currently any listeners for
|
|
|
|
"share-bookmarks" but we'll send it out just in case. */
|
2008-06-11 11:54:44 -07:00
|
|
|
|
2008-06-30 22:30:04 -07:00
|
|
|
let self = this;
|
|
|
|
let saved_dataType = dataType;
|
|
|
|
let saved_onComplete = onComplete;
|
|
|
|
let saved_guid = guid;
|
|
|
|
let saved_username = username;
|
|
|
|
let saved_isShareEnabled = isShareEnabled;
|
|
|
|
let successMsg = "weave:service:global:success";
|
|
|
|
let errorMsg = "weave:service:global:error";
|
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
|
|
|
|
let observer = {
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
if (!Weave.DAV.locked) {
|
|
|
|
self._notify(messageName, self._lock(self._shareData,
|
|
|
|
saved_dataType,
|
|
|
|
saved_isShareEnabled,
|
|
|
|
saved_guid,
|
|
|
|
saved_username)).async(self,
|
|
|
|
saved_onComplete);
|
|
|
|
os.removeObserver(observer, successMsg);
|
|
|
|
os.removeObserver(observer, errorMsg);
|
|
|
|
}
|
|
|
|
},
|
2008-07-01 09:58:00 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver])
|
2008-06-30 22:30:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Weave.DAV.locked) {
|
|
|
|
/* then we have to wait until it's not locked. */
|
2008-07-01 09:58:00 -07:00
|
|
|
dump( "DAV is locked, gonna set up observer to do it later.\n");
|
2008-06-30 22:30:04 -07:00
|
|
|
os.addObserver( observer, successMsg, true );
|
|
|
|
os.addObserver( observer, errorMsg, true );
|
2008-06-24 12:22:32 -07:00
|
|
|
} else {
|
2008-06-30 22:30:04 -07:00
|
|
|
// Just do it right now!
|
2008-07-01 09:58:00 -07:00
|
|
|
dump( "DAV not locked, doing it now.\n");
|
2008-06-30 22:30:04 -07:00
|
|
|
observer.observe();
|
2008-06-09 18:44:13 -07:00
|
|
|
}
|
2008-06-23 18:23:08 -07:00
|
|
|
},
|
|
|
|
|
2008-06-30 22:30:04 -07:00
|
|
|
_shareData: function WeaveSvc__shareData(dataType,
|
|
|
|
isShareEnabled,
|
|
|
|
guid,
|
|
|
|
username) {
|
2008-06-24 12:22:32 -07:00
|
|
|
let self = yield;
|
|
|
|
let ret;
|
|
|
|
if (Engines.get(dataType).enabled) {
|
2008-06-30 22:30:04 -07:00
|
|
|
if (isShareEnabled) {
|
|
|
|
Engines.get(dataType).share(self.cb, guid, username);
|
|
|
|
} else {
|
|
|
|
Engines.get(dataType).stopSharing(self.cb, guid, username);
|
|
|
|
}
|
2008-06-24 12:22:32 -07:00
|
|
|
ret = yield;
|
|
|
|
} else {
|
2008-06-30 22:30:04 -07:00
|
|
|
this._log.warn( "Can't share disabled data type: " + dataType );
|
2008-06-24 12:22:32 -07:00
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
self.done(ret);
|
2008-03-25 23:01:34 -07:00
|
|
|
}
|
2008-06-30 22:30:04 -07:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
};
|