Bug 570573 - Clean up uses of switch (Svc.AppInfo.ID) for app-specific hacks (Part 2) [r=mconnor]

Weave.Service.onStartup now triggers autoconnect after a fixed delay specified in a preference. If that preference is absent, nothing happens and apps are responsible for triggering autoconnect in a weave:service:ready observer themselves. Provide such observers for Firefox and Fennec.
This commit is contained in:
Philipp von Weitershausen 2010-06-10 17:04:49 -07:00
parent eb4e7c1045
commit 2a116ee59d
3 changed files with 35 additions and 27 deletions

View File

@ -294,30 +294,17 @@ WeaveSvc.prototype = {
this._updateCachedURLs();
// Send an event now that Weave service is ready
Svc.Obs.notify("weave:service:ready");
// Wait a little before checking how long to wait to autoconnect
if (this._checkSetup() == STATUS_OK && Svc.Prefs.get("autoconnect")) {
Utils.delay(function() {
// Figure out how many seconds to delay autoconnect based on the app
let wait = 3;
switch (Svc.AppInfo.ID) {
case FIREFOX_ID:
// Add one second delay for each busy tab in every window
let enum = Svc.WinMediator.getEnumerator("navigator:browser");
while (enum.hasMoreElements()) {
Array.forEach(enum.getNext().gBrowser.mTabs, function(tab) {
wait += tab.hasAttribute("busy");
});
}
break;
}
this._log.debug("Autoconnecting in " + wait + " seconds");
Utils.delay(this._autoConnect, wait * 1000, this, "_autoTimer");
}, 2000, this, "_autoTimer");
// 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 will definitely have access to the
// 'Weave' namespace.
Utils.delay(function() Svc.Obs.notify("weave:service:ready"), 0);
},
_checkSetup: function WeaveSvc__checkSetup() {
@ -676,6 +663,15 @@ WeaveSvc.prototype = {
Svc.Obs.notify("weave:service:start-over");
},
delayedAutoConnect: function delayedAutoConnect(delay) {
if (this._loggedIn)
return;
if (this._checkSetup() == STATUS_OK && Svc.Prefs.get("autoconnect")) {
Utils.delay(this._autoConnect, delay * 1000, this, "_autoTimer");
}
},
_autoConnect: let (attempts = 0) function _autoConnect() {
let reason =
Utils.mpLocked() ? "master password still locked"

View File

@ -30,19 +30,28 @@ function run_test() {
Weave.Service.serverURL = "http://localhost:8080/";
Weave.Service.clusterURL = "http://localhost:8080/";
_("Initial state is ok.");
do_check_eq(Status.service, STATUS_OK);
_("Try logging in. It wont' work because we're not configured yet.");
do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
Weave.Service.login();
do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
do_check_eq(Status.login, LOGIN_FAILED_NO_USERNAME);
do_check_false(Weave.Service.isLoggedIn);
_("Try again with username and password set.");
Weave.Service.username = "johndoe";
Weave.Service.password = "ilovejane";
// We need a non-empty passphrase for login to work. Even the
// setup wizard just sets this to 'foo' if there isn't a
// passphrase yet.
Weave.Service.login();
do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
do_check_eq(Status.login, LOGIN_FAILED_NO_PASSPHRASE);
do_check_false(Weave.Service.isLoggedIn);
_("Success if passphrase is set.");
Weave.Service.passphrase = "foo";
Weave.Service.login();
do_check_eq(Status.service, STATUS_OK);
do_check_eq(Status.login, LOGIN_SUCCEEDED);
do_check_true(Weave.Service.isLoggedIn);
} finally {

View File

@ -1,6 +1,7 @@
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/identity.js");
Cu.import("resource://weave/ext/Observers.js");
Cu.import("resource://weave/ext/Sync.js");
function run_test() {
_("When imported, Weave.Service.onStartup is called");
@ -29,6 +30,8 @@ function run_test() {
do_check_eq(ID.get('WeaveCryptoID').username, "johndoe");
_("Observers are notified of startup");
// Synchronize with Weave.Service.onStartup's async notification
Sync.sleep(0);
do_check_true(observerCalled);
} finally {