Permalink
Browse files

Bug 668622 - Move sync at startup logic (autoConnect, etc.) to SyncSc…

…heduler. r=philikon
  • Loading branch information...
1 parent 231b39e commit a6995c0348de67b635a4409d9e04b73e86481ab5 @emtwo emtwo committed Jul 21, 2011
@@ -151,9 +151,8 @@ BrowserGlue.prototype = {
}
delay = delay <= MAX_DELAY ? delay : MAX_DELAY;
- let syncTemp = {};
- Cu.import("resource://services-sync/service.js", syncTemp);
- syncTemp.Weave.Service.delayedAutoConnect(delay);
+ Cu.import("resource://services-sync/main.js");
+ Weave.SyncScheduler.delayedAutoConnect(delay);
},
#endif
@@ -52,6 +52,7 @@ let lazies = {
"identity.js": ["Identity", "ID"],
"jpakeclient.js": ["JPAKEClient"],
"notifications.js": ["Notifications", "Notification", "NotificationButton"],
+ "policies.js": ["SyncScheduler"],
"resource.js": ["Resource", "AsyncResource", "Auth",
"BasicAuthenticator", "NoOpAuthenticator"],
"service.js": ["Service"],
@@ -92,6 +92,7 @@ let SyncScheduler = {
Svc.Obs.add("weave:service:logout:finish", this);
Svc.Obs.add("weave:service:sync:error", this);
Svc.Obs.add("weave:service:backoff:interval", this);
+ Svc.Obs.add("weave:service:ready", this);
Svc.Obs.add("weave:engine:sync:applied", this);
Svc.Idle.addIdleObserver(this, Svc.Prefs.get("scheduler.idleTime"));
},
@@ -161,6 +162,14 @@ let SyncScheduler = {
Status.backoffInterval = interval;
Status.minimumNextSync = Date.now() + data;
break;
+ case "weave:service:ready":
+ // Applications can specify this preference if they want autoconnect
+ // to happen after a fixed delay.
+ let delay = Svc.Prefs.get("autoconnectDelay");
+ if (delay) {
+ this.delayedAutoConnect(delay);
+ }
+ break;
case "weave:engine:sync:applied":
let numItems = subject.applied;
this._log.trace("Engine " + data + " applied " + numItems + " items.");
@@ -330,6 +339,31 @@ let SyncScheduler = {
this.scheduleNextSync(interval);
},
+ /**
+ * Automatically start syncing after the given delay (in seconds).
+ *
+ * Applications can define the `services.sync.autoconnectDelay` preference
+ * to have this called automatically during start-up with the pref value as
+ * the argument. Alternatively, they can call it themselves to control when
+ * Sync should first start to sync.
+ */
+ delayedAutoConnect: function delayedAutoConnect(delay) {
+ if (Weave.Service._checkSetup() == STATUS_OK) {
+ Utils.namedTimer(this.autoConnect, delay * 1000, this, "_autoTimer");
+ }
+ },
+
+ autoConnect: function autoConnect() {
+ if (Weave.Service._checkSetup() == STATUS_OK && !Weave.Service._checkSync()) {
+ Utils.nextTick(Weave.Service.sync, Weave.Service);
+ }
+
+ // Once autoConnect is called we no longer need _autoTimer.
+ if (this._autoTimer) {
+ this._autoTimer.clear();
+ }
+ },
+
_syncErrors: 0,
/**
* Deal with sync errors appropriately
@@ -409,13 +409,6 @@ WeaveSvc.prototype = {
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED)
Svc.Obs.notify("weave:engine:start-tracking");
- // Applications can specify this preference if they want autoconnect
- // to happen after a fixed delay.
- let delay = Svc.Prefs.get("autoconnectDelay");
- if (delay) {
- this.delayedAutoConnect(delay);
- }
-
// Send an event now that Weave service is ready. We don't do this
// synchronously so that observers can import this module before
// registering an observer.
@@ -1043,31 +1036,6 @@ WeaveSvc.prototype = {
});
},
- /**
- * Automatically start syncing after the given delay (in seconds).
- *
- * Applications can define the `services.sync.autoconnectDelay` preference
- * to have this called automatically during start-up with the pref value as
- * the argument. Alternatively, they can call it themselves to control when
- * Sync should first start to sync.
- */
- delayedAutoConnect: function delayedAutoConnect(delay) {
- if (this._checkSetup() == STATUS_OK) {
- Utils.namedTimer(this._autoConnect, delay * 1000, this, "_autoTimer");
- }
- },
-
- _autoConnect: function _autoConnect() {
- if (this._checkSetup() == STATUS_OK && !this._checkSync()) {
- Utils.nextTick(this.sync, this);
- }
-
- // Once _autoConnect is called we no longer need _autoTimer.
- if (this._autoTimer) {
- this._autoTimer.clear();
- }
- },
-
persistLogin: function persistLogin() {
// Canceled master password prompt can prevent these from succeeding.
try {
@@ -343,7 +343,7 @@ add_test(function test_autoconnect() {
let server = sync_httpd_setup();
setUp();
- Service.delayedAutoConnect(0);
+ SyncScheduler.delayedAutoConnect(0);
});
add_test(function test_autoconnect_mp_locked() {
@@ -376,7 +376,7 @@ add_test(function test_autoconnect_mp_locked() {
});
});
- Service.delayedAutoConnect(0);
+ SyncScheduler.delayedAutoConnect(0);
});
let timer;
@@ -395,7 +395,7 @@ add_test(function test_no_autoconnect_during_wizard() {
// First wait >100ms (nsITimers can take up to that much time to fire, so
// we can account for the timer in delayedAutoconnect) and then two event
- // loop ticks (to account for the Utils.nextTick() in _autoConnect).
+ // loop ticks (to account for the Utils.nextTick() in autoConnect).
let ticks = 2;
function wait() {
if (ticks) {
@@ -410,7 +410,59 @@ add_test(function test_no_autoconnect_during_wizard() {
}
timer = Utils.namedTimer(wait, 150, {}, "timer");
- Service.delayedAutoConnect(0);
+ SyncScheduler.delayedAutoConnect(0);
+});
+
+add_test(function test_no_autoconnect_status_not_ok() {
+ let server = sync_httpd_setup();
+
+ // Ensure we don't actually try to sync (or log in for that matter).
+ function onLoginStart() {
+ do_throw("Should not get here!");
+ }
+ Svc.Obs.add("weave:service:login:start", onLoginStart);
+
+ // First wait >100ms (nsITimers can take up to that much time to fire, so
+ // we can account for the timer in delayedAutoconnect) and then two event
+ // loop ticks (to account for the Utils.nextTick() in autoConnect).
+ let ticks = 2;
+ function wait() {
+ if (ticks) {
+ ticks -= 1;
+ Utils.nextTick(wait);
+ return;
+ }
+ Svc.Obs.remove("weave:service:login:start", onLoginStart);
+
+ do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
+ do_check_eq(Status.login, LOGIN_FAILED_NO_USERNAME);
+
+ Service.startOver();
+ server.stop(run_next_test);
+ }
+ timer = Utils.namedTimer(wait, 150, {}, "timer");
+
+ SyncScheduler.delayedAutoConnect(0);
+});
+
+add_test(function test_autoconnectDelay_pref() {
+ Svc.Obs.add("weave:service:sync:finish", function onSyncFinish() {
+ Svc.Obs.remove("weave:service:sync:finish", onSyncFinish);
+
+ Service.startOver();
+ server.stop(run_next_test);
+ });
+
+ Svc.Prefs.set("autoconnectDelay", 1);
+
+ let server = sync_httpd_setup();
+ setUp();
+
+ Svc.Obs.notify("weave:service:ready");
+
+ // autoconnectDelay pref is multiplied by 1000.
+ do_check_eq(SyncScheduler._autoTimer.delay, 1000);
+ do_check_eq(Status.service, STATUS_OK);
});
add_test(function test_idle_adjustSyncInterval() {

0 comments on commit a6995c0

Please sign in to comment.