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