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>
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
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");
|
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);
|
|
|
|
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-03-27 19:12:53 -07:00
|
|
|
function WeaveSvc() {
|
|
|
|
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');
|
|
|
|
|
|
|
|
// Register built-in engines
|
|
|
|
Engines.register(new BookmarksEngine());
|
|
|
|
Engines.register(new HistoryEngine());
|
|
|
|
Engines.register(new CookieEngine());
|
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);
|
|
|
|
|
|
|
|
if (!this.enabled) {
|
|
|
|
this._log.info("Weave Sync disabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._setSchedule(this.schedule);
|
|
|
|
}
|
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-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;
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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-04-14 18:53:35 -07:00
|
|
|
get userPath() { return ID.get('WeaveID').userHash; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
get currentUser() {
|
2008-04-10 21:38:15 -07:00
|
|
|
if (this._loggedIn)
|
2007-12-10 21:38:53 -08:00
|
|
|
return this.username;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2008-02-13 16:07:11 -08:00
|
|
|
get enabled() {
|
2008-03-19 15:17:04 -07:00
|
|
|
return 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
|
|
|
},
|
|
|
|
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_enableSchedule: function WeaveSync__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));
|
2007-12-10 21:38:53 -08:00
|
|
|
this._scheduleTimer.initWithCallback(listener, 1800000, // 30 min
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_disableSchedule: function WeaveSync__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
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_onSchedule: function WeaveSync__onSchedule() {
|
2008-02-13 16:07:11 -08:00
|
|
|
if (this.enabled) {
|
|
|
|
this._log.info("Running scheduled sync");
|
|
|
|
this.sync();
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
_initLogs: function WeaveSync__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
|
|
|
|
|
|
|
let formatter = Log4Moz.Service.newFormatter("basic");
|
|
|
|
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
|
|
|
|
|
|
|
let capp = Log4Moz.Service.newAppender("console", 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);
|
|
|
|
|
|
|
|
let dapp = Log4Moz.Service.newAppender("dump", 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-03-28 19:36:11 -07:00
|
|
|
this._briefApp = Log4Moz.Service.newFileAppender("rotating", brief, formatter);
|
|
|
|
this._briefApp.level = Log4Moz.Level[Utils.prefs.getCharPref("log.appender.briefLog")];
|
|
|
|
root.addAppender(this._briefApp);
|
|
|
|
this._debugApp = Log4Moz.Service.newFileAppender("rotating", verbose, formatter);
|
|
|
|
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-03-19 18:42:12 -07:00
|
|
|
_uploadVersion: function WeaveSync__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)
|
|
|
|
_versionCheck: function WeaveSync__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.");
|
|
|
|
this._serverWipe.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
this._uploadVersion.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
} else if (ret.responseText < STORAGE_FORMAT_VERSION) {
|
|
|
|
this._log.info("Server version too low. Wiping server data.");
|
|
|
|
this._serverWipe.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
this._uploadVersion.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
} 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;
|
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-03-31 07:20:09 -07:00
|
|
|
let serverURL = Utils.prefs.getCharPref("serverURL");
|
|
|
|
if (serverURL[serverURL.length-1] != '/')
|
|
|
|
serverURL = serverURL + '/';
|
|
|
|
|
2008-04-01 23:43:14 -07:00
|
|
|
try {
|
|
|
|
DAV.baseURL = serverURL;
|
|
|
|
DAV.MKCOL("user/" + this.userPath, self.cb);
|
|
|
|
let ret = yield;
|
|
|
|
if (!ret)
|
|
|
|
throw "Could not create user directory";
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
DAV.baseURL = serverURL + "user/" + this.userPath + "/";
|
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
this._log.info("Using server URL: " + DAV.baseURL);
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
_keyCheck: function WeaveSvc__keyCheck() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
DAV.GET("private/privkey", self.cb);
|
|
|
|
let keyResp = yield;
|
|
|
|
Utils.ensureStatus(keyResp.status,
|
|
|
|
"Could not get private key from server", [[200,300],404]);
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
if (keyResp.status != 404) {
|
2008-04-14 18:53:35 -07:00
|
|
|
let id = ID.get('WeaveCryptoID');
|
|
|
|
id.privkey = keyResp.responseText;
|
|
|
|
Crypto.RSAkeydecrypt.async(Crypto, self.cb, id);
|
|
|
|
id.pubkey = yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-03-31 07:20:09 -07:00
|
|
|
} else {
|
|
|
|
this._generateKeys.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-19 18:42:12 -07:00
|
|
|
_generateKeys: function WeaveSync__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
|
|
|
|
|
|
|
let id = ID.get('WeaveCryptoID');
|
|
|
|
Crypto.RSAkeygen.async(Crypto, self.cb, id);
|
2008-03-19 18:42:12 -07:00
|
|
|
let [privkey, pubkey] = yield;
|
2008-03-07 04:20:55 -08:00
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
id.privkey = privkey;
|
|
|
|
id.pubkey = pubkey;
|
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-03-31 07:20:09 -07:00
|
|
|
DAV.PUT("private/privkey", privkey, 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-03-31 07:20:09 -07:00
|
|
|
DAV.PUT("public/pubkey", pubkey, 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
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
observe: function WeaveSync__observe(subject, topic, data) {
|
2008-02-13 16:07:11 -08:00
|
|
|
if (topic != "nsPref:changed")
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (data) {
|
|
|
|
case "enabled": // this works because this.schedule is 0 when disabled
|
|
|
|
case "schedule":
|
|
|
|
this._setSchedule(this.schedule);
|
2007-12-10 21:38:53 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// These are global (for all engines)
|
|
|
|
|
2008-03-27 19:12:53 -07:00
|
|
|
login: function WeaveSync_login(onComplete, password, passphrase) {
|
2008-03-30 08:40:23 -07:00
|
|
|
this._localLock(this._notify("login", this._login,
|
|
|
|
password, passphrase)).async(this, onComplete);
|
|
|
|
},
|
|
|
|
_login: function WeaveSync__login(password, passphrase) {
|
|
|
|
let self = yield;
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// cache password & passphrase
|
2008-03-30 08:40:23 -07:00
|
|
|
// if null, we'll try to get them from the pw manager below
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.get('WeaveID').setTempPassword(password);
|
|
|
|
ID.get('WeaveCryptoID').setTempPassword(passphrase);
|
2008-04-10 21:38:15 -07:00
|
|
|
|
2008-03-30 08:40:23 -07:00
|
|
|
this._log.debug("Logging in");
|
|
|
|
|
|
|
|
if (!this.username)
|
|
|
|
throw "No username set, login failed";
|
|
|
|
if (!this.password)
|
|
|
|
throw "No password given or found in password manager";
|
|
|
|
|
2008-04-01 23:43:14 -07:00
|
|
|
let serverURL = Utils.prefs.getCharPref("serverURL");
|
|
|
|
if (serverURL[serverURL.length-1] != '/')
|
|
|
|
serverURL = serverURL + '/';
|
|
|
|
DAV.baseURL = serverURL + "user/" + this.userPath + "/";
|
2008-03-30 08:40:23 -07:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
DAV.checkLogin.async(DAV, self.cb, this.username, this.password);
|
2008-03-30 08:40:23 -07:00
|
|
|
let success = yield;
|
2008-04-01 23:43:14 -07:00
|
|
|
if (!success) {
|
|
|
|
try {
|
|
|
|
this._checkUserDir.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
} catch (e) { /* FIXME: tmp workaround for services.m.c */ }
|
2008-04-10 21:38:15 -07:00
|
|
|
DAV.checkLogin.async(DAV, self.cb, this.username, this.password);
|
2008-04-01 23:43:14 -07:00
|
|
|
let success = yield;
|
|
|
|
if (!success)
|
|
|
|
throw "Login failed";
|
|
|
|
}
|
2008-03-30 08:40:23 -07:00
|
|
|
|
|
|
|
this._versionCheck.async(this, self.cb);
|
|
|
|
yield;
|
2008-03-31 07:20:09 -07:00
|
|
|
this._keyCheck.async(this, self.cb);
|
|
|
|
yield;
|
2008-03-30 08:40:23 -07:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
this._loggedIn = true;
|
|
|
|
|
2008-03-30 08:40:23 -07:00
|
|
|
self.done(true);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
logout: function WeaveSync_logout() {
|
2007-12-10 21:38:53 -08:00
|
|
|
this._log.info("Logging out");
|
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();
|
|
|
|
};
|
|
|
|
this._lock(this._notify("server-wipe", 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-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-03-27 19:12:53 -07:00
|
|
|
sync: function WeaveSync_sync(onComplete) {
|
2008-03-30 08:40:23 -07:00
|
|
|
this._lock(this._notify("sync", this._sync)).async(this, onComplete);
|
2008-03-27 19:12:53 -07:00
|
|
|
},
|
2007-12-14 18:07:25 -08:00
|
|
|
_sync: function WeaveSync__sync() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
if (!this._loggedIn)
|
2008-03-31 07:20:09 -07:00
|
|
|
throw "Can't sync: Not logged in";
|
|
|
|
|
|
|
|
this._versionCheck.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
this._keyCheck.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
let engines = Engines.getAll();
|
|
|
|
for (let i = 0; i < engines.length; i++) {
|
|
|
|
if (engines[i].enabled) {
|
|
|
|
this._notify(engines[i].name + "-engine:sync",
|
|
|
|
this._syncEngine, engines[i]).async(this, self.cb);
|
|
|
|
engines[i].resetServer(self.cb);
|
|
|
|
yield;
|
|
|
|
if (engines[i].name == "bookmarks") { // FIXME
|
|
|
|
Engines.get("bookmarks").syncMounts(self.cb);
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
}
|
2008-04-01 14:46:29 -07:00
|
|
|
}
|
2008-03-30 08:40:23 -07:00
|
|
|
},
|
|
|
|
_syncEngine: function WeaveSvc__syncEngine(engine) {
|
|
|
|
let self = yield;
|
|
|
|
engine.sync(self.cb);
|
2008-03-27 19:12:53 -07:00
|
|
|
yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2008-03-27 19:12:53 -07:00
|
|
|
resetServer: function WeaveSync_resetServer(onComplete) {
|
2008-03-30 08:40:23 -07:00
|
|
|
this._lock(this._notify("reset-server",
|
|
|
|
this._resetServer)).async(this, onComplete);
|
2008-03-27 19:12:53 -07:00
|
|
|
},
|
2007-12-14 18:07:25 -08:00
|
|
|
_resetServer: function WeaveSync__resetServer() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2008-03-31 07:20:09 -07:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
if (!this._loggedIn)
|
2008-03-31 07:20:09 -07:00
|
|
|
throw "Can't reset server: Not logged in";
|
|
|
|
|
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-03-27 19:12:53 -07:00
|
|
|
resetClient: function WeaveSync_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-03-27 19:12:53 -07:00
|
|
|
_resetClient: function WeaveSync__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-03-27 19:12:53 -07:00
|
|
|
shareBookmarks: function WeaveSync_shareBookmarks(onComplete, username) {
|
2008-03-30 08:40:23 -07:00
|
|
|
this._lock(this._notify("share-bookmarks",
|
|
|
|
this._shareBookmarks,
|
|
|
|
username)).async(this, onComplete);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
2008-03-27 19:12:53 -07:00
|
|
|
_shareBookmarks: function WeaveSync__shareBookmarks(username) {
|
|
|
|
let self = yield;
|
2008-04-15 17:21:34 -07:00
|
|
|
if (Engines.get("bookmarks").enabled)
|
|
|
|
return;
|
2008-04-14 18:53:35 -07:00
|
|
|
Engines.get("bookmarks").share(self.cb, username);
|
2008-03-28 03:25:51 -07:00
|
|
|
let ret = yield;
|
|
|
|
self.done(ret);
|
2008-03-25 23:01:34 -07:00
|
|
|
}
|
2008-03-27 19:12:53 -07:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
};
|