services/sync/modules/rest.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
     7 Cu.import("resource://gre/modules/Log.jsm");
     8 Cu.import("resource://services-common/rest.js");
     9 Cu.import("resource://services-sync/util.js");
    10 Cu.import("resource://services-sync/constants.js");
    12 this.EXPORTED_SYMBOLS = ["SyncStorageRequest"];
    14 const STORAGE_REQUEST_TIMEOUT = 5 * 60; // 5 minutes
    16 /**
    17  * RESTRequest variant for use against a Sync storage server.
    18  */
    19 this.SyncStorageRequest = function SyncStorageRequest(uri) {
    20   RESTRequest.call(this, uri);
    22   this.authenticator = null;
    23 }
    24 SyncStorageRequest.prototype = {
    26   __proto__: RESTRequest.prototype,
    28   _logName: "Sync.StorageRequest",
    30   /**
    31    * The string to use as the base User-Agent in Sync requests.
    32    * These strings will look something like
    33    * 
    34    *   Firefox/4.0 FxSync/1.8.0.20100101.mobile
    35    * 
    36    * or
    37    * 
    38    *   Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
    39    */
    40   userAgent:
    41     Services.appinfo.name + "/" + Services.appinfo.version +  // Product.
    42     " FxSync/" + WEAVE_VERSION + "." +                        // Sync.
    43     Services.appinfo.appBuildID + ".",                        // Build.
    45   /**
    46    * Wait 5 minutes before killing a request.
    47    */
    48   timeout: STORAGE_REQUEST_TIMEOUT,
    50   dispatch: function dispatch(method, data, onComplete, onProgress) {
    51     // Compose a UA string fragment from the various available identifiers.
    52     if (Svc.Prefs.get("sendVersionInfo", true)) {
    53       let ua = this.userAgent + Svc.Prefs.get("client.type", "desktop");
    54       this.setHeader("user-agent", ua);
    55     }
    57     if (this.authenticator) {
    58       this.authenticator(this);
    59     } else {
    60       this._log.debug("No authenticator found.");
    61     }
    63     return RESTRequest.prototype.dispatch.apply(this, arguments);
    64   },
    66   onStartRequest: function onStartRequest(channel) {
    67     RESTRequest.prototype.onStartRequest.call(this, channel);
    68     if (this.status == this.ABORTED) {
    69       return;
    70     }
    72     let headers = this.response.headers;
    73     // Save the latest server timestamp when possible.
    74     if (headers["x-weave-timestamp"]) {
    75       SyncStorageRequest.serverTime = parseFloat(headers["x-weave-timestamp"]);
    76     }
    78     // This is a server-side safety valve to allow slowing down
    79     // clients without hurting performance.
    80     if (headers["x-weave-backoff"]) {
    81       Svc.Obs.notify("weave:service:backoff:interval",
    82                      parseInt(headers["x-weave-backoff"], 10));
    83     }
    85     if (this.response.success && headers["x-weave-quota-remaining"]) {
    86       Svc.Obs.notify("weave:service:quota:remaining",
    87                      parseInt(headers["x-weave-quota-remaining"], 10));
    88     }
    89   }
    90 };

mercurial