зеркало из https://github.com/mozilla/pjs.git
use the wrapper notifier for login(), change observers to the slightly different observer topics; allow server url to not have a trailing slash (add one automatically); dial down dav.js verbosity; add serverWipe service method; change 'reset server data' button in prefs pane to do serverWipe instead of resetServer; allow for wrappers to have extra args both saved in the closure (at wrap creation time) as well as passed in later (via .async())
This commit is contained in:
Родитель
bd57ab6275
Коммит
6726d4d1c1
|
@ -79,6 +79,8 @@ DAVCollection.prototype = {
|
|||
return this._baseURL;
|
||||
},
|
||||
set baseURL(value) {
|
||||
if (value[value.length-1] != '/')
|
||||
value = value + '/';
|
||||
this._baseURL = value;
|
||||
},
|
||||
|
||||
|
@ -286,7 +288,7 @@ DAVCollection.prototype = {
|
|||
yield;
|
||||
}
|
||||
|
||||
this._log.info("Logging in");
|
||||
this._log.debug("Logging in");
|
||||
|
||||
let URI = Utils.makeURI(this._baseURL);
|
||||
this._auth = "Basic " + btoa(username + ":" + password);
|
||||
|
@ -307,7 +309,7 @@ DAVCollection.prototype = {
|
|||
},
|
||||
|
||||
logout: function DC_logout() {
|
||||
this._log.debug("Logging out (forgetting auth header)");
|
||||
this._log.trace("Logging out (forgetting auth header)");
|
||||
this._loggedIn = false;
|
||||
this.__auth = null;
|
||||
},
|
||||
|
@ -318,7 +320,7 @@ DAVCollection.prototype = {
|
|||
let self = yield;
|
||||
let ret = null;
|
||||
|
||||
this._log.info("Getting active lock token");
|
||||
this._log.debug("Getting active lock token");
|
||||
this.PROPFIND("",
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
|
||||
"<D:propfind xmlns:D='DAV:'>" +
|
||||
|
@ -334,12 +336,13 @@ DAVCollection.prototype = {
|
|||
|
||||
let tokens = Utils.xpath(resp.responseXML, '//D:locktoken/D:href');
|
||||
let token = tokens.iterateNext();
|
||||
ret = token.textContent;
|
||||
if (token)
|
||||
ret = token.textContent;
|
||||
|
||||
if (ret)
|
||||
this._log.debug("Found an active lock token");
|
||||
this._log.trace("Found an active lock token");
|
||||
else
|
||||
this._log.debug("No active lock token found");
|
||||
this._log.trace("No active lock token found");
|
||||
self.done(ret);
|
||||
},
|
||||
|
||||
|
@ -416,25 +419,25 @@ DAVCollection.prototype = {
|
|||
let self = yield;
|
||||
let unlocked = true;
|
||||
|
||||
this._log.info("Forcibly releasing any server locks");
|
||||
this._log.debug("Forcibly releasing any server locks");
|
||||
|
||||
this._getActiveLock.async(this, self.cb);
|
||||
this._token = yield;
|
||||
|
||||
if (!this._token) {
|
||||
this._log.info("No server lock found");
|
||||
this._log.debug("No server lock found");
|
||||
self.done(true);
|
||||
yield;
|
||||
}
|
||||
|
||||
this._log.info("Server lock found, unlocking");
|
||||
this._log.trace("Server lock found, unlocking");
|
||||
this.unlock.async(this, self.cb);
|
||||
unlocked = yield;
|
||||
|
||||
if (unlocked)
|
||||
this._log.debug("Lock released");
|
||||
this._log.trace("Lock released");
|
||||
else
|
||||
this._log.debug("No lock released");
|
||||
this._log.trace("No lock released");
|
||||
self.done(unlocked);
|
||||
},
|
||||
|
||||
|
|
|
@ -363,92 +363,6 @@ WeaveSvc.prototype = {
|
|||
Utils.ensureStatus(ret.status, "Could not upload public key");
|
||||
},
|
||||
|
||||
_login: function WeaveSync__login(password, passphrase) {
|
||||
let self = yield;
|
||||
|
||||
try {
|
||||
if (this._dav.locked)
|
||||
throw "Login failed: could not acquire lock";
|
||||
this._dav.allowLock = false;
|
||||
|
||||
// cache password & passphrase
|
||||
// if null, we'll try to get them from the pw manager below
|
||||
this._mozId.setTempPassword(password);
|
||||
this._cryptoId.setTempPassword(passphrase);
|
||||
|
||||
this._log.debug("Logging in");
|
||||
this._os.notifyObservers(null, "weave:service-login:start", "");
|
||||
|
||||
if (!this.username)
|
||||
throw "No username set, login failed";
|
||||
if (!this.password)
|
||||
throw "No password given or found in password manager";
|
||||
|
||||
let serverURL = Utils.prefs.getCharPref("serverURL");
|
||||
this._dav.baseURL = serverURL + "user/" + this.userPath + "/";
|
||||
this._log.info("Using server URL: " + this._dav.baseURL);
|
||||
|
||||
this._dav.login.async(this._dav, self.cb, this.username, this.password);
|
||||
let success = yield;
|
||||
|
||||
if (!success) {
|
||||
// FIXME: we should actually limit this to when we get a 404
|
||||
this._createUserDir.async(this, self.cb, serverURL);
|
||||
yield;
|
||||
}
|
||||
|
||||
this._versionCheck.async(this, self.cb);
|
||||
yield;
|
||||
|
||||
this._dav.GET("private/privkey", self.cb);
|
||||
let keyResp = yield;
|
||||
Utils.ensureStatus(keyResp.status,
|
||||
"Could not get private key from server", [[200,300],404]);
|
||||
|
||||
if (keyResp.status != 404) {
|
||||
this._cryptoId.privkey = keyResp.responseText;
|
||||
Crypto.RSAkeydecrypt.async(Crypto, self.cb, this._cryptoId);
|
||||
this._cryptoId.pubkey = yield;
|
||||
|
||||
} else {
|
||||
this._generateKeys.async(this, self.cb);
|
||||
yield;
|
||||
}
|
||||
|
||||
this._passphrase = null;
|
||||
this._dav.allowLock = true;
|
||||
this._os.notifyObservers(null, "weave:service-login:success", "");
|
||||
self.done(true);
|
||||
|
||||
} catch (e) {
|
||||
this._dav.allowLock = true;
|
||||
this._os.notifyObservers(null, "weave:service-login:error", "");
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
|
||||
_serverWipe: function WeaveSync__serverWipe() {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_serverWipe() {
|
||||
let innerSelf = yield;
|
||||
|
||||
this._dav.listFiles.async(this._dav, self.cb);
|
||||
let names = yield;
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
this._dav.DELETE(names[i], self.cb);
|
||||
let resp = yield;
|
||||
this._log.debug(resp.status);
|
||||
}
|
||||
};
|
||||
|
||||
// NOTE: doesn't lock because it's called from _login() which
|
||||
// already holds the lock
|
||||
this._notify("server-wipe", cb).async(this, self.cb);
|
||||
yield;
|
||||
},
|
||||
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupports]),
|
||||
|
||||
// nsIObserver
|
||||
|
@ -468,7 +382,59 @@ WeaveSvc.prototype = {
|
|||
// These are global (for all engines)
|
||||
|
||||
login: function WeaveSync_login(onComplete, password, passphrase) {
|
||||
this._login.async(this, onComplete, password, passphrase);
|
||||
this._localLock(this._notify("login", this._login,
|
||||
password, passphrase)).async(this, onComplete);
|
||||
},
|
||||
_login: function WeaveSync__login(password, passphrase) {
|
||||
let self = yield;
|
||||
|
||||
// cache password & passphrase
|
||||
// if null, we'll try to get them from the pw manager below
|
||||
this._mozId.setTempPassword(password);
|
||||
this._cryptoId.setTempPassword(passphrase);
|
||||
|
||||
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";
|
||||
|
||||
let serverURL = Utils.prefs.getCharPref("serverURL");
|
||||
if (serverURL[serverURL.length-1] != '/')
|
||||
serverURL = serverURL + '/';
|
||||
this._dav.baseURL = serverURL + "user/" + this.userPath + "/";
|
||||
this._log.info("Using server URL: " + this._dav.baseURL);
|
||||
|
||||
this._dav.login.async(this._dav, self.cb, this.username, this.password);
|
||||
let success = yield;
|
||||
|
||||
if (!success) {
|
||||
// FIXME: we should actually limit this to when we get a 404
|
||||
this._createUserDir.async(this, self.cb, serverURL);
|
||||
yield;
|
||||
}
|
||||
|
||||
this._versionCheck.async(this, self.cb);
|
||||
yield;
|
||||
|
||||
this._dav.GET("private/privkey", self.cb);
|
||||
let keyResp = yield;
|
||||
Utils.ensureStatus(keyResp.status,
|
||||
"Could not get private key from server", [[200,300],404]);
|
||||
|
||||
if (keyResp.status != 404) {
|
||||
this._cryptoId.privkey = keyResp.responseText;
|
||||
Crypto.RSAkeydecrypt.async(Crypto, self.cb, this._cryptoId);
|
||||
this._cryptoId.pubkey = yield;
|
||||
|
||||
} else {
|
||||
this._generateKeys.async(this, self.cb);
|
||||
yield;
|
||||
}
|
||||
|
||||
this._passphrase = null;
|
||||
self.done(true);
|
||||
},
|
||||
|
||||
logout: function WeaveSync_logout() {
|
||||
|
@ -476,111 +442,96 @@ WeaveSvc.prototype = {
|
|||
this._dav.logout();
|
||||
this._mozId.setTempPassword(null); // clear cached password
|
||||
this._cryptoId.setTempPassword(null); // and passphrase
|
||||
this._os.notifyObservers(null, "weave:service-logout:success", "");
|
||||
this._os.notifyObservers(null, "weave:service:logout:success", "");
|
||||
},
|
||||
|
||||
resetLock: function WeaveSync_resetLock(onComplete) {
|
||||
this._resetLock.async(this, onComplete);
|
||||
resetLock: function WeaveSvc_resetLock(onComplete) {
|
||||
this._notify("reset-server-lock", this._resetLock).async(this, onComplete);
|
||||
},
|
||||
_resetLock: function WeaveSync__resetLock() {
|
||||
_resetLock: function WeaveSvc__resetLock() {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_resetLock() {
|
||||
let innerSelf = yield;
|
||||
this._dav.forceUnlock.async(this._dav, innerSelf.cb);
|
||||
yield;
|
||||
};
|
||||
|
||||
this._notify("reset-server-lock", cb).async(this, self.cb);
|
||||
this._dav.forceUnlock.async(this._dav, self.cb);
|
||||
yield;
|
||||
},
|
||||
|
||||
serverWipe: function WeaveSvc_serverWipe(onComplete) {
|
||||
this._lock(this._notify("server-wipe",
|
||||
this._serverWipe)).async(this, onComplete);
|
||||
},
|
||||
_serverWipe: function WeaveSvc__serverWipe() {
|
||||
let self = yield;
|
||||
|
||||
this._dav.listFiles.async(this._dav, self.cb);
|
||||
let names = yield;
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
this._dav.DELETE(names[i], self.cb);
|
||||
let resp = yield;
|
||||
this._log.debug(resp.status);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
// These are per-engine
|
||||
|
||||
sync: function WeaveSync_sync(onComplete) {
|
||||
this._sync.async(this, onComplete);
|
||||
this._lock(this._notify("sync", this._sync)).async(this, onComplete);
|
||||
},
|
||||
_sync: function WeaveSync__sync() {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_sync() {
|
||||
let innerSelf = yield;
|
||||
|
||||
let engineCb = function WeaveSvc_syncEngine(engine) {
|
||||
let innerInnerSelf = yield;
|
||||
engine.sync(innerInnerSelf.cb);
|
||||
yield;
|
||||
};
|
||||
|
||||
if (Utils.prefs.getBoolPref("bookmarks")) {
|
||||
this._notify(this._bmkEngine.name + ":sync",
|
||||
engineCb, this._bmkEngine).async(this, innerSelf.cb);
|
||||
yield;
|
||||
this._bmkEngine.syncMounts(innerSelf.cb);
|
||||
yield;
|
||||
}
|
||||
if (Utils.prefs.getBoolPref("history")) {
|
||||
this._notify(this._histEngine.name + ":sync",
|
||||
engineCb, this._histEngine).async(this, innerSelf.cb);
|
||||
yield;
|
||||
}
|
||||
};
|
||||
|
||||
this._lock(this._notify("sync", cb)).async(this, self.cb);
|
||||
if (Utils.prefs.getBoolPref("bookmarks")) {
|
||||
this._notify(this._bmkEngine.name + ":sync",
|
||||
this._syncEngine, this._bmkEngine).async(this, self.cb);
|
||||
yield;
|
||||
this._bmkEngine.syncMounts(self.cb); // FIXME
|
||||
yield;
|
||||
}
|
||||
if (Utils.prefs.getBoolPref("history")) {
|
||||
this._notify(this._histEngine.name + ":sync",
|
||||
this._syncEngine, this._histEngine).async(this, self.cb);
|
||||
yield;
|
||||
}
|
||||
},
|
||||
_syncEngine: function WeaveSvc__syncEngine(engine) {
|
||||
let self = yield;
|
||||
engine.sync(self.cb);
|
||||
yield;
|
||||
},
|
||||
|
||||
resetServer: function WeaveSync_resetServer(onComplete) {
|
||||
this._resetServer.async(this, onComplete);
|
||||
this._lock(this._notify("reset-server",
|
||||
this._resetServer)).async(this, onComplete);
|
||||
},
|
||||
_resetServer: function WeaveSync__resetServer() {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_resetServer() {
|
||||
let innerSelf = yield;
|
||||
this._bmkEngine.resetServer(innerSelf.cb);
|
||||
yield;
|
||||
this._histEngine.resetServer(innerSelf.cb);
|
||||
yield;
|
||||
};
|
||||
|
||||
this._lock(this._notify("reset-server",cb)).async(this, self.cb);
|
||||
this._bmkEngine.resetServer(self.cb);
|
||||
yield;
|
||||
this._histEngine.resetServer(self.cb);
|
||||
yield;
|
||||
},
|
||||
|
||||
resetClient: function WeaveSync_resetClient(onComplete) {
|
||||
this._resetClient.async(this, onComplete);
|
||||
this._localLock(this._notify("reset-client",
|
||||
this._resetClient)).async(this, onComplete);
|
||||
},
|
||||
_resetClient: function WeaveSync__resetClient() {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_resetClient() {
|
||||
let innerSelf = yield;
|
||||
this._bmkEngine.resetClient(innerSelf.cb);
|
||||
yield;
|
||||
this._histEngine.resetClient(innerSelf.cb);
|
||||
yield;
|
||||
};
|
||||
|
||||
this._localLock(this._notify("reset-client", cb)).async(this, self.cb);
|
||||
this._bmkEngine.resetClient(self.cb);
|
||||
yield;
|
||||
this._histEngine.resetClient(self.cb);
|
||||
yield;
|
||||
},
|
||||
|
||||
shareBookmarks: function WeaveSync_shareBookmarks(onComplete, username) {
|
||||
this._shareBookmarks.async(this, onComplete, username);
|
||||
this._lock(this._notify("share-bookmarks",
|
||||
this._shareBookmarks,
|
||||
username)).async(this, onComplete);
|
||||
},
|
||||
_shareBookmarks: function WeaveSync__shareBookmarks(username) {
|
||||
let self = yield;
|
||||
|
||||
let cb = function Weave_shareBookmarks() {
|
||||
let innerSelf = yield;
|
||||
this._bmkEngine.share(innerSelf.cb, username);
|
||||
let ret = yield;
|
||||
innerSelf.done(ret);
|
||||
};
|
||||
|
||||
this._lock(this._notify("share-bookmarks", cb)).async(this, self.cb);
|
||||
this._bmkEngine.share(self.cb, username);
|
||||
let ret = yield;
|
||||
self.done(ret);
|
||||
}
|
||||
|
|
|
@ -72,18 +72,20 @@ let Wrap = {
|
|||
// ...
|
||||
// }
|
||||
// };
|
||||
notify: function Weave_notify(name, method /* , arg1, arg2, ... */) {
|
||||
notify: function Weave_notify(name, method /* , arg1, arg2, ..., argN */) {
|
||||
let savedName = name;
|
||||
let savedMethod = method;
|
||||
let args = Array.prototype.slice.call(arguments, 2);
|
||||
let savedArgs = Array.prototype.slice.call(arguments, 2);
|
||||
|
||||
return function WeaveNotifyWrapper() {
|
||||
return function WeaveNotifyWrapper(/* argN+1, argN+2, ... */) {
|
||||
let self = yield;
|
||||
let ret;
|
||||
let args = Array.prototype.slice.call(arguments);
|
||||
|
||||
try {
|
||||
this._os.notifyObservers(null, this._osPrefix + savedName + ":start", "");
|
||||
|
||||
args = savedArgs.concat(args);
|
||||
args.unshift(this, savedMethod, self.cb);
|
||||
Async.run.apply(Async, args);
|
||||
ret = yield;
|
||||
|
@ -101,13 +103,14 @@ let Wrap = {
|
|||
|
||||
// NOTE: see notify, this works the same way. they can be
|
||||
// chained together as well.
|
||||
lock: function WeaveSync_lock(method /* , arg1, arg2, ... */) {
|
||||
lock: function WeaveSync_lock(method /* , arg1, arg2, ..., argN */) {
|
||||
let savedMethod = method;
|
||||
let args = Array.prototype.slice.call(arguments, 1);
|
||||
let savedArgs = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
return function WeaveLockWrapper() {
|
||||
return function WeaveLockWrapper( /* argN+1, argN+2, ... */) {
|
||||
let self = yield;
|
||||
let ret;
|
||||
let args = Array.prototype.slice.call(arguments);
|
||||
|
||||
this._dav.lock.async(this._dav, self.cb);
|
||||
let locked = yield;
|
||||
|
@ -115,6 +118,7 @@ let Wrap = {
|
|||
throw "Could not acquire lock";
|
||||
|
||||
try {
|
||||
args = savedArgs.concat(args);
|
||||
args.unshift(this, savedMethod, self.cb);
|
||||
Async.run.apply(Async, args);
|
||||
ret = yield;
|
||||
|
@ -133,19 +137,21 @@ let Wrap = {
|
|||
|
||||
// NOTE: see notify, this works the same way. they can be
|
||||
// chained together as well.
|
||||
localLock: function WeaveSync_localLock(method /* , arg1, arg2, ... */) {
|
||||
localLock: function WeaveSync_localLock(method /* , arg1, arg2, ..., argN */) {
|
||||
let savedMethod = method;
|
||||
let args = Array.prototype.slice.call(arguments, 1);
|
||||
let savedArgs = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
return function WeaveLocalLockWrapper() {
|
||||
return function WeaveLocalLockWrapper(/* argN+1, argN+2, ... */) {
|
||||
let self = yield;
|
||||
let ret;
|
||||
let args = Array.prototype.slice.call(arguments);
|
||||
|
||||
if (this._dav.locked)
|
||||
throw "Could not acquire lock";
|
||||
this._dav.allowLock = false;
|
||||
|
||||
try {
|
||||
args = savedArgs.concat(args);
|
||||
args.unshift(this, savedMethod, self.cb);
|
||||
Async.run.apply(Async, args);
|
||||
ret = yield;
|
||||
|
|
Загрузка…
Ссылка в новой задаче