Bug 785225 - Part 7: Bind SendCredentialsController to a Service instance; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Wed, 29 Aug 2012 14:43:40 -0700
changeset 111054 1c1410d01ab985a12edf62a8e533c935b1ea55f4
parent 111053 c7e070eafa940273e1cd987589bf5db0db9aaff7
child 111055 6d61599eebd30b5e55a1fc60b893b52a9eb8edc4
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersrnewman
bugs785225
milestone18.0a1
Bug 785225 - Part 7: Bind SendCredentialsController to a Service instance; r=rnewman
browser/base/content/sync/setup.js
services/sync/modules/jpakeclient.js
services/sync/modules/service.js
services/sync/tests/unit/test_sendcredentials_controller.js
--- a/browser/base/content/sync/setup.js
+++ b/browser/base/content/sync/setup.js
@@ -634,17 +634,18 @@ var gSyncSetup = {
   completePairing: function completePairing() {
     if (!this._jpakeclient) {
       // The channel was aborted while we were setting up the account
       // locally. XXX TODO should we do anything here, e.g. tell
       // the user on the last wizard page that it's ok, they just
       // have to pair again?
       return;
     }
-    let controller = new Weave.SendCredentialsController(this._jpakeclient);
+    let controller = new Weave.SendCredentialsController(this._jpakeclient,
+                                                         Weave.Service);
     this._jpakeclient.controller = controller;
   },
 
   startEasySetup: function () {
     // Don't do anything if we have a client already (e.g. we went to
     // Sync Options and just came back).
     if (this._jpakeclient)
       return;
--- a/services/sync/modules/jpakeclient.js
+++ b/services/sync/modules/jpakeclient.js
@@ -4,18 +4,17 @@
 
 const EXPORTED_SYMBOLS = ["JPAKEClient", "SendCredentialsController"];
 
 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
 
 Cu.import("resource://services-common/log4moz.js");
 Cu.import("resource://services-common/rest.js");
 Cu.import("resource://services-sync/constants.js");
-Cu.import("resource://services-sync/main.js");
-Cu.import("resource://services-sync/policies.js");
+Cu.import("resource://services-sync/identity.js");
 Cu.import("resource://services-sync/util.js");
 
 const REQUEST_TIMEOUT         = 60; // 1 minute
 const KEYEXCHANGE_VERSION     = 3;
 
 const JPAKE_SIGNERID_SENDER   = "sender";
 const JPAKE_SIGNERID_RECEIVER = "receiver";
 const JPAKE_LENGTH_SECRET     = 8;
@@ -691,25 +690,27 @@ JPAKEClient.prototype = {
  * context disappears. This object stays alive for the duration of the
  * transfer by being strong-ref'ed as an nsIObserver.
  *
  * Credentials are sent after the first sync has been completed
  * (successfully or not.)
  *
  * Usage:
  *
- *   jpakeclient.controller = new SendCredentialsController(jpakeclient);
+ *   jpakeclient.controller = new SendCredentialsController(jpakeclient,
+ *                                                          service);
  *
  */
-function SendCredentialsController(jpakeclient) {
+function SendCredentialsController(jpakeclient, service) {
   this._log = Log4Moz.repository.getLogger("Sync.SendCredentialsController");
   this._log.level = Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")];
 
   this._log.trace("Loading.");
   this.jpakeclient = jpakeclient;
+  this.service = service;
 
   // Register ourselves as observers the first Sync finishing (either
   // successfully or unsuccessfully, we don't care) or for removing
   // this device's sync configuration, in case that happens while we
   // haven't finished the first sync yet.
   Services.obs.addObserver(this, "weave:service:sync:finish", false);
   Services.obs.addObserver(this, "weave:service:sync:error",  false);
   Services.obs.addObserver(this, "weave:service:start-over",  false);
@@ -737,32 +738,32 @@ SendCredentialsController.prototype = {
         // This will call onAbort which will call unload().
         this.jpakeclient.abort();
         break;
     }
   },
 
   sendCredentials: function sendCredentials() {
     this._log.trace("Sending credentials.");
-    let credentials = {account:   Weave.Identity.account,
-                       password:  Weave.Identity.basicPassword,
-                       synckey:   Weave.Identity.syncKey,
-                       serverURL: Weave.Service.serverURL};
+    let credentials = {account:   Identity.account,
+                       password:  Identity.basicPassword,
+                       synckey:   Identity.syncKey,
+                       serverURL: this.service.serverURL};
     this.jpakeclient.sendAndComplete(credentials);
   },
 
   // JPAKEClient controller API
 
   onComplete: function onComplete() {
     this._log.debug("Exchange was completed successfully!");
     this.unload();
 
     // Schedule a Sync for soonish to fetch the data uploaded by the
     // device with which we just paired.
-    SyncScheduler.scheduleNextSync(SyncScheduler.activeInterval);
+    this.service.scheduler.scheduleNextSync(this.service.scheduler.activeInterval);
   },
 
   onAbort: function onAbort(error) {
     // It doesn't really matter why we aborted, but the channel is closed
     // for sure, so we won't be able to do anything with it.
     this._log.debug("Exchange was aborted with error: " + error);
     this.unload();
   },
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -335,17 +335,18 @@ WeaveSvc.prototype = {
       this.enabled = false;
       this._log.info("Could not load the Weave crypto component. Disabling " +
                       "Weave, since it will not work correctly.");
     }
 
     Svc.Obs.add("weave:service:setup-complete", this);
     Svc.Prefs.observe("engine.", this);
 
-    SyncScheduler.init();
+    this.scheduler = SyncScheduler;
+    this.scheduler.init();
 
     if (!this.enabled) {
       this._log.info("Firefox Sync disabled.");
     }
 
     this._updateCachedURLs();
 
     let status = this._checkSetup();
--- a/services/sync/tests/unit/test_sendcredentials_controller.js
+++ b/services/sync/tests/unit/test_sendcredentials_controller.js
@@ -43,34 +43,34 @@ function make_sendCredentials_test(topic
         // Signal the end of another sync. We shouldn't be registered anymore,
         // so we shouldn't re-enter this method (cf sendAndCompleteCalled above)
         Svc.Obs.notify(topic);
 
         SyncScheduler.setDefaults();
         Utils.nextTick(run_next_test);
       }
     };
-    jpakeclient.controller = new SendCredentialsController(jpakeclient);
+    jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
     Svc.Obs.notify(topic);
   };
 }
 
 add_test(make_sendCredentials_test("weave:service:sync:finish"));
 add_test(make_sendCredentials_test("weave:service:sync:error"));
 
 
 add_test(function test_abort() {
   _("Test aborting the J-PAKE exchange.");
 
   let jpakeclient = {
     sendAndComplete: function sendAndComplete() {
       do_throw("Shouldn't get here!");
     }
   };
-  jpakeclient.controller = new SendCredentialsController(jpakeclient);
+  jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
 
   // Verify that the controller unregisters itself when the exchange
   // was aborted.
   jpakeclient.controller.onAbort(JPAKE_ERROR_USERABORT);
   Svc.Obs.notify("weave:service:sync:finish");
   Utils.nextTick(run_next_test);
 });
 
@@ -83,17 +83,17 @@ add_test(function test_startOver() {
     abort: function abort() {
       abortCalled = true;
       this.controller.onAbort(JPAKE_ERROR_USERABORT);
     },
     sendAndComplete: function sendAndComplete() {
       do_throw("Shouldn't get here!");
     }
   };
-  jpakeclient.controller = new SendCredentialsController(jpakeclient);
+  jpakeclient.controller = new SendCredentialsController(jpakeclient, Service);
 
   Svc.Obs.notify("weave:service:start-over");
   do_check_true(abortCalled);
 
   // Ensure that the controller no longer does anything if a sync
   // finishes now or -- more likely -- errors out.
   Svc.Obs.notify("weave:service:sync:error");