Change Resource.get() semantics and support X-Weave-Alert (bug #478330)

--HG--
extra : rebase_source : e0eb1e34f85ddd041005d780590640005dc0b434
This commit is contained in:
Anant Narayanan 2009-08-26 00:05:57 -07:00
parent dfe8e7e513
commit 371e639357
3 changed files with 69 additions and 11 deletions

View File

@ -218,7 +218,10 @@ SyncEngine.prototype = {
__proto__: Engine.prototype,
_recordObj: CryptoWrapper,
_alerts: "",
get alerts() { return this._alerts; },
get baseURL() {
let url = Svc.Prefs.get("clusterURL");
if (!url)
@ -341,8 +344,15 @@ SyncEngine.prototype = {
Sync.sleep(0);
});
newitems.get();
let resp = newitems.get();
try {
// we only need to store the latest alert
this._alerts = resp.getHeader("X-Weave-Alert");
} catch (e) {
// no alert headers were present
this._alerts = "";
}
if (this.lastSync < this._lastSyncTmp)
this.lastSync = this._lastSyncTmp;

View File

@ -50,6 +50,23 @@ Cu.import("resource://weave/constants.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/auth.js");
// = RequestResponse =
//
// This object is returned on a successful
// HTTP response (codes 200-300) and encapsulates
// returned headers and the actual response text
function RequestResponse(channel, response) {
this._channel = channel;
this._response = response;
}
RequestResponse.prototype = {
get response() { return this._response },
get status() { return this._channel.responseStatus },
getHeader: function ReqRe_getHeader(which) {
return this._channel.getResponseHeader(which);
}
};
// = RequestException =
//
// This function raises an exception through the call
@ -293,7 +310,7 @@ Resource.prototype = {
this._log.debug(action + " request failed (" + channel.responseStatus + ")");
if (this._data)
this._log.debug("Error response: " + this._data);
throw new RequestException(this, action, channel);
return new RequestResponse(channel, this._data);
} else {
this._log.debug(action + " request successful (" + channel.responseStatus + ")");
@ -313,7 +330,7 @@ Resource.prototype = {
}
}
return this._data;
return new RequestResponse(channel, this._data);
},
// ** {{{ Resource.get }}} **

View File

@ -172,6 +172,10 @@ WeaveSvc.prototype = {
// Timer object for automagically syncing
_syncTimer: null,
// Current alerts
_alerts: "[]",
get alerts() { return JSON.parse(this._alerts); },
get username() {
return Svc.Prefs.get("username", "");
},
@ -532,17 +536,38 @@ WeaveSvc.prototype = {
};
// login may fail because of cluster change
let response;
try {
res.get();
response = res.get();
} catch (e) {}
try {
switch (res.lastChannel.responseStatus) {
if (response) {
switch (response.status) {
case 200:
if (passphrase && !this.verifyPassphrase(username, password, passphrase)) {
if (passphrase &&
!this.verifyPassphrase(username, password, passphrase)) {
this._setSyncFailure(LOGIN_FAILED_INVALID_PASSPHRASE);
return false;
}
// check for alerts
try {
let alerts = response.getHeader("X-Weave-Alert");
if (this._alerts != alerts) {
this._alerts = alerts;
Svc.Observer.notifyObservers(
null, "weave:service:alerts:changed", ""
);
}
} catch (e) {
// no alert headers were present
if (this.alerts.length != 0) {
this._alerts = "";
Svc.Observer.notifyObservers(
null, "weave:service:alerts:changed", ""
);
}
}
return true;
case 401:
if (this.updateCluster(username))
@ -554,9 +579,9 @@ WeaveSvc.prototype = {
default:
throw "unexpected HTTP response: " + res.lastChannel.responseStatus;
}
} catch (e) {
} else {
// if we get here, we have either a busted channel or a network error
this._log.debug("verifyLogin failed: " + e)
this._log.debug("verifyLogin failed: network error");
this._setSyncFailure(LOGIN_FAILED_NETWORK_ERROR);
throw e;
}
@ -1222,6 +1247,12 @@ WeaveSvc.prototype = {
_syncEngine: function WeaveSvc__syncEngine(engine) {
try {
engine.sync();
if (this._alerts != engine.alerts) {
this._alerts = engine.alerts;
Svc.Observer.notifyObservers(
null, "weave:service:alerts:changed", ""
);
}
return true;
}
catch(e) {