services/sync/modules/rest.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/modules/rest.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
     1.9 +
    1.10 +Cu.import("resource://gre/modules/Log.jsm");
    1.11 +Cu.import("resource://services-common/rest.js");
    1.12 +Cu.import("resource://services-sync/util.js");
    1.13 +Cu.import("resource://services-sync/constants.js");
    1.14 +
    1.15 +this.EXPORTED_SYMBOLS = ["SyncStorageRequest"];
    1.16 +
    1.17 +const STORAGE_REQUEST_TIMEOUT = 5 * 60; // 5 minutes
    1.18 +
    1.19 +/**
    1.20 + * RESTRequest variant for use against a Sync storage server.
    1.21 + */
    1.22 +this.SyncStorageRequest = function SyncStorageRequest(uri) {
    1.23 +  RESTRequest.call(this, uri);
    1.24 +
    1.25 +  this.authenticator = null;
    1.26 +}
    1.27 +SyncStorageRequest.prototype = {
    1.28 +
    1.29 +  __proto__: RESTRequest.prototype,
    1.30 +
    1.31 +  _logName: "Sync.StorageRequest",
    1.32 +
    1.33 +  /**
    1.34 +   * The string to use as the base User-Agent in Sync requests.
    1.35 +   * These strings will look something like
    1.36 +   * 
    1.37 +   *   Firefox/4.0 FxSync/1.8.0.20100101.mobile
    1.38 +   * 
    1.39 +   * or
    1.40 +   * 
    1.41 +   *   Firefox Aurora/5.0a1 FxSync/1.9.0.20110409.desktop
    1.42 +   */
    1.43 +  userAgent:
    1.44 +    Services.appinfo.name + "/" + Services.appinfo.version +  // Product.
    1.45 +    " FxSync/" + WEAVE_VERSION + "." +                        // Sync.
    1.46 +    Services.appinfo.appBuildID + ".",                        // Build.
    1.47 +
    1.48 +  /**
    1.49 +   * Wait 5 minutes before killing a request.
    1.50 +   */
    1.51 +  timeout: STORAGE_REQUEST_TIMEOUT,
    1.52 +
    1.53 +  dispatch: function dispatch(method, data, onComplete, onProgress) {
    1.54 +    // Compose a UA string fragment from the various available identifiers.
    1.55 +    if (Svc.Prefs.get("sendVersionInfo", true)) {
    1.56 +      let ua = this.userAgent + Svc.Prefs.get("client.type", "desktop");
    1.57 +      this.setHeader("user-agent", ua);
    1.58 +    }
    1.59 +
    1.60 +    if (this.authenticator) {
    1.61 +      this.authenticator(this);
    1.62 +    } else {
    1.63 +      this._log.debug("No authenticator found.");
    1.64 +    }
    1.65 +
    1.66 +    return RESTRequest.prototype.dispatch.apply(this, arguments);
    1.67 +  },
    1.68 +
    1.69 +  onStartRequest: function onStartRequest(channel) {
    1.70 +    RESTRequest.prototype.onStartRequest.call(this, channel);
    1.71 +    if (this.status == this.ABORTED) {
    1.72 +      return;
    1.73 +    }
    1.74 +
    1.75 +    let headers = this.response.headers;
    1.76 +    // Save the latest server timestamp when possible.
    1.77 +    if (headers["x-weave-timestamp"]) {
    1.78 +      SyncStorageRequest.serverTime = parseFloat(headers["x-weave-timestamp"]);
    1.79 +    }
    1.80 +
    1.81 +    // This is a server-side safety valve to allow slowing down
    1.82 +    // clients without hurting performance.
    1.83 +    if (headers["x-weave-backoff"]) {
    1.84 +      Svc.Obs.notify("weave:service:backoff:interval",
    1.85 +                     parseInt(headers["x-weave-backoff"], 10));
    1.86 +    }
    1.87 +
    1.88 +    if (this.response.success && headers["x-weave-quota-remaining"]) {
    1.89 +      Svc.Obs.notify("weave:service:quota:remaining",
    1.90 +                     parseInt(headers["x-weave-quota-remaining"], 10));
    1.91 +    }
    1.92 +  }
    1.93 +};

mercurial