Bug 787273 - Part 3: Don't rely on Identity in SyncStorageRequest; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Fri, 14 Sep 2012 16:02:32 -0700
changeset 111079 c6a07b5b3142d2ec680467d1bc584a1edd7352b8
parent 111078 b57329caf8e876aaa20728196f88604513137070
child 111080 6117227b889d83db35f64a605b1adcb16f95f38b
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs787273
milestone18.0a1
Bug 787273 - Part 3: Don't rely on Identity in SyncStorageRequest; r=rnewman The Identity singleton is going away. This refactors SyncStorageRequest to not use it. Behavior now works like Resource. Instances are obtained from the Service singleton and have authentication functionality attached.
services/sync/modules/rest.js
services/sync/modules/service.js
services/sync/tests/unit/test_syncstoragerequest.js
--- a/services/sync/modules/rest.js
+++ b/services/sync/modules/rest.js
@@ -2,28 +2,29 @@
  * 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/. */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://services-common/log4moz.js");
 Cu.import("resource://services-common/rest.js");
 Cu.import("resource://services-sync/util.js");
-Cu.import("resource://services-sync/identity.js");
 Cu.import("resource://services-sync/constants.js");
 
 const EXPORTED_SYMBOLS = ["SyncStorageRequest"];
 
 const STORAGE_REQUEST_TIMEOUT = 5 * 60; // 5 minutes
 
 /**
  * RESTRequest variant for use against a Sync storage server.
  */
 function SyncStorageRequest(uri) {
   RESTRequest.call(this, uri);
+
+  this.authenticator = null;
 }
 SyncStorageRequest.prototype = {
 
   __proto__: RESTRequest.prototype,
 
   _logName: "Sync.StorageRequest",
 
   /**
@@ -48,19 +49,18 @@ SyncStorageRequest.prototype = {
 
   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);
     }
 
-    let authenticator = Identity.getRESTRequestAuthenticator();
-    if (authenticator) {
-      authenticator(this);
+    if (this.authenticator) {
+      this.authenticator(this);
     } else {
       this._log.debug("No authenticator found.");
     }
 
     return RESTRequest.prototype.dispatch.apply(this, arguments);
   },
 
   onStartRequest: function onStartRequest(channel) {
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -477,16 +477,26 @@ WeaveSvc.prototype = {
   resource: function resource(url) {
     let res = new Resource(url);
     res.authenticator = this._identity.getResourceAuthenticator();
 
     return res;
   },
 
   /**
+   * Obtain a SyncStorageRequest instance with authentication credentials.
+   */
+  getStorageRequest: function getStorageRequest(url) {
+    let request = new SyncStorageRequest(url);
+    request.authenticator = this._identity.getRESTRequestAuthenticator();
+
+    return request;
+  },
+
+  /**
    * Perform the info fetch as part of a login or key fetch.
    */
   _fetchInfo: function _fetchInfo(url) {
     let infoURL = url || this.infoURL;
 
     this._log.trace("In _fetchInfo: " + infoURL);
     let info;
     try {
@@ -1425,17 +1435,17 @@ WeaveSvc.prototype = {
   getStorageInfo: function getStorageInfo(type, callback) {
     if (STORAGE_INFO_TYPES.indexOf(type) == -1) {
       throw "Invalid value for 'type': " + type;
     }
 
     let info_type = "info/" + type;
     this._log.trace("Retrieving '" + info_type + "'...");
     let url = this.userBaseURL + info_type;
-    return new SyncStorageRequest(url).get(function onComplete(error) {
+    return this.getStorageRequest(url).get(function onComplete(error) {
       // Note: 'this' is the request.
       if (error) {
         this._log.debug("Failed to retrieve '" + info_type + "': " +
                         Utils.exceptionStr(error));
         return callback(error);
       }
       if (this.response.status != 200) {
         this._log.debug("Failed to retrieve '" + info_type +
--- a/services/sync/tests/unit/test_syncstoragerequest.js
+++ b/services/sync/tests/unit/test_syncstoragerequest.js
@@ -1,16 +1,16 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
+Cu.import("resource://services-common/log4moz.js");
+Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/rest.js");
+Cu.import("resource://services-sync/service.js");
 Cu.import("resource://services-sync/util.js");
-Cu.import("resource://services-sync/identity.js");
-Cu.import("resource://services-sync/constants.js");
-Cu.import("resource://services-common/log4moz.js");
 
 const STORAGE_REQUEST_RESOURCE_URL = TEST_SERVER_URL + "resource";
 
 function run_test() {
   Log4Moz.repository.getLogger("Sync.RESTRequest").level = Log4Moz.Level.Trace;
   initTestLogging();
 
   run_next_test();
@@ -54,17 +54,17 @@ add_test(function test_user_agent_mobile
 });
 
 add_test(function test_auth() {
   let handler = httpd_handler(200, "OK");
   let server = httpd_setup({"/resource": handler});
 
   setBasicCredentials("johndoe", "ilovejane", "XXXXXXXXX");
 
-  let request = new SyncStorageRequest(STORAGE_REQUEST_RESOURCE_URL);
+  let request = Service.getStorageRequest(STORAGE_REQUEST_RESOURCE_URL);
   request.get(function (error) {
     do_check_eq(error, null);
     do_check_eq(this.response.status, 200);
     do_check_true(basic_auth_matches(handler.request, "johndoe", "ilovejane"));
 
     Svc.Prefs.reset("");
 
     server.stop(run_next_test);