2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-06 17:20:36 -07:00
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
|
|
|
Cu.import("resource://services-common/rest.js");
|
2011-07-06 17:20:36 -07:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
|
2012-04-05 23:26:06 -07:00
|
|
|
const EXPORTED_SYMBOLS = ["SyncStorageRequest"];
|
2011-07-06 17:20:36 -07:00
|
|
|
|
|
|
|
const STORAGE_REQUEST_TIMEOUT = 5 * 60; // 5 minutes
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RESTRequest variant for use against a Sync storage server.
|
|
|
|
*/
|
|
|
|
function SyncStorageRequest(uri) {
|
|
|
|
RESTRequest.call(this, uri);
|
|
|
|
}
|
|
|
|
SyncStorageRequest.prototype = {
|
|
|
|
|
|
|
|
__proto__: RESTRequest.prototype,
|
|
|
|
|
|
|
|
_logName: "Sync.StorageRequest",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The string to use as the base User-Agent in Sync requests.
|
|
|
|
* These strings will look something like
|
|
|
|
*
|
|
|
|
* Firefox/4.0 FxSync/1.8.0.20100101.mobile
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
|
|
|
|
*/
|
|
|
|
userAgent:
|
|
|
|
Services.appinfo.name + "/" + Services.appinfo.version + // Product.
|
|
|
|
" FxSync/" + WEAVE_VERSION + "." + // Sync.
|
|
|
|
Services.appinfo.appBuildID + ".", // Build.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait 5 minutes before killing a request.
|
|
|
|
*/
|
|
|
|
timeout: STORAGE_REQUEST_TIMEOUT,
|
|
|
|
|
|
|
|
dispatch: function dispatch(method, data, onComplete, onProgress) {
|
|
|
|
// Compose a UA string fragment from the various available identifiers.
|
|
|
|
if (Svc.Prefs.get("sendVersionInfo", true)) {
|
|
|
|
let ua = this.userAgent + Svc.Prefs.get("client.type", "desktop");
|
|
|
|
this.setHeader("user-agent", ua);
|
|
|
|
}
|
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
let authenticator = Identity.getRESTRequestAuthenticator();
|
|
|
|
if (authenticator) {
|
|
|
|
authenticator(this);
|
2011-07-06 17:20:36 -07:00
|
|
|
} else {
|
2012-03-22 15:49:50 -07:00
|
|
|
this._log.debug("No authenticator found.");
|
2011-07-06 17:20:36 -07:00
|
|
|
}
|
|
|
|
|
2011-07-14 12:11:29 -07:00
|
|
|
return RESTRequest.prototype.dispatch.apply(this, arguments);
|
2011-07-06 17:20:36 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
onStartRequest: function onStartRequest(channel) {
|
|
|
|
RESTRequest.prototype.onStartRequest.call(this, channel);
|
2011-07-14 12:11:29 -07:00
|
|
|
if (this.status == this.ABORTED) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-06 17:20:36 -07:00
|
|
|
|
|
|
|
let headers = this.response.headers;
|
|
|
|
// Save the latest server timestamp when possible.
|
|
|
|
if (headers["x-weave-timestamp"]) {
|
|
|
|
SyncStorageRequest.serverTime = parseFloat(headers["x-weave-timestamp"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a server-side safety valve to allow slowing down
|
|
|
|
// clients without hurting performance.
|
|
|
|
if (headers["x-weave-backoff"]) {
|
|
|
|
Svc.Obs.notify("weave:service:backoff:interval",
|
|
|
|
parseInt(headers["x-weave-backoff"], 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.response.success && headers["x-weave-quota-remaining"]) {
|
|
|
|
Svc.Obs.notify("weave:service:quota:remaining",
|
|
|
|
parseInt(headers["x-weave-quota-remaining"], 10));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|