2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
2012-08-30 01:43:40 +04:00
|
|
|
"ErrorHandler",
|
|
|
|
"SyncScheduler",
|
|
|
|
];
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2015-10-07 15:03:21 +03:00
|
|
|
var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2013-08-26 22:55:58 +04:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2012-08-30 01:43:41 +04:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2011-06-27 17:22:49 +04:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2012-08-30 01:43:41 +04:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2015-02-18 00:06:00 +03:00
|
|
|
Cu.import("resource://services-common/logmanager.js");
|
2016-02-03 03:21:09 +03:00
|
|
|
Cu.import("resource://services-common/async.js");
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2014-01-29 05:51:09 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Status",
|
|
|
|
"resource://services-sync/status.js");
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.SyncScheduler = function SyncScheduler(service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service = service;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
SyncScheduler.prototype = {
|
2013-08-26 22:55:58 +04:00
|
|
|
_log: Log.repository.getLogger("Sync.SyncScheduler"),
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2011-10-28 09:25:00 +04:00
|
|
|
_fatalLoginStatus: [LOGIN_FAILED_NO_USERNAME,
|
|
|
|
LOGIN_FAILED_NO_PASSWORD,
|
|
|
|
LOGIN_FAILED_NO_PASSPHRASE,
|
|
|
|
LOGIN_FAILED_INVALID_PASSPHRASE,
|
|
|
|
LOGIN_FAILED_LOGIN_REJECTED],
|
|
|
|
|
2011-06-29 05:48:55 +04:00
|
|
|
/**
|
|
|
|
* The nsITimer object that schedules the next sync. See scheduleNextSync().
|
|
|
|
*/
|
|
|
|
syncTimer: null,
|
|
|
|
|
|
|
|
setDefaults: function setDefaults() {
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.trace("Setting SyncScheduler policy values to defaults.");
|
2011-06-30 07:12:24 +04:00
|
|
|
|
2014-03-12 01:46:36 +04:00
|
|
|
let service = Cc["@mozilla.org/weave/service;1"]
|
|
|
|
.getService(Ci.nsISupports)
|
|
|
|
.wrappedJSObject;
|
|
|
|
|
|
|
|
let part = service.fxAccountsEnabled ? "fxa" : "sync11";
|
|
|
|
let prefSDInterval = "scheduler." + part + ".singleDeviceInterval";
|
|
|
|
this.singleDeviceInterval = Svc.Prefs.get(prefSDInterval) * 1000;
|
|
|
|
|
2011-06-30 07:12:24 +04:00
|
|
|
this.idleInterval = Svc.Prefs.get("scheduler.idleInterval") * 1000;
|
|
|
|
this.activeInterval = Svc.Prefs.get("scheduler.activeInterval") * 1000;
|
|
|
|
this.immediateInterval = Svc.Prefs.get("scheduler.immediateInterval") * 1000;
|
2013-09-04 03:11:46 +04:00
|
|
|
this.eolInterval = Svc.Prefs.get("scheduler.eolInterval") * 1000;
|
2011-06-30 07:12:24 +04:00
|
|
|
|
2011-06-29 05:48:55 +04:00
|
|
|
// A user is non-idle on startup by default.
|
|
|
|
this.idle = false;
|
|
|
|
|
|
|
|
this.hasIncomingItems = false;
|
|
|
|
|
2011-10-14 04:58:44 +04:00
|
|
|
this.clearSyncTriggers();
|
2011-06-29 05:48:55 +04:00
|
|
|
},
|
|
|
|
|
2011-10-14 04:58:44 +04:00
|
|
|
// nextSync is in milliseconds, but prefs can't hold that much
|
2015-09-23 12:40:53 +03:00
|
|
|
get nextSync() {
|
|
|
|
return Svc.Prefs.get("nextSync", 0) * 1000;
|
|
|
|
},
|
|
|
|
set nextSync(value) {
|
|
|
|
Svc.Prefs.set("nextSync", Math.floor(value / 1000));
|
|
|
|
},
|
2011-10-14 04:58:44 +04:00
|
|
|
|
2015-09-23 12:40:53 +03:00
|
|
|
get syncInterval() {
|
|
|
|
return Svc.Prefs.get("syncInterval", this.singleDeviceInterval);
|
|
|
|
},
|
|
|
|
set syncInterval(value) {
|
|
|
|
Svc.Prefs.set("syncInterval", value);
|
|
|
|
},
|
2011-10-14 04:58:44 +04:00
|
|
|
|
2015-09-23 12:40:53 +03:00
|
|
|
get syncThreshold() {
|
|
|
|
return Svc.Prefs.get("syncThreshold", SINGLE_USER_THRESHOLD);
|
|
|
|
},
|
|
|
|
set syncThreshold(value) {
|
|
|
|
Svc.Prefs.set("syncThreshold", value);
|
|
|
|
},
|
2011-10-14 04:58:44 +04:00
|
|
|
|
2015-09-23 12:40:53 +03:00
|
|
|
get globalScore() {
|
|
|
|
return Svc.Prefs.get("globalScore", 0);
|
|
|
|
},
|
|
|
|
set globalScore(value) {
|
|
|
|
Svc.Prefs.set("globalScore", value);
|
|
|
|
},
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2015-09-23 12:40:53 +03:00
|
|
|
get numClients() {
|
|
|
|
return Svc.Prefs.get("numClients", 0);
|
|
|
|
},
|
|
|
|
set numClients(value) {
|
|
|
|
Svc.Prefs.set("numClients", value);
|
|
|
|
},
|
2011-10-14 04:58:44 +04:00
|
|
|
|
2011-06-27 17:22:49 +04:00
|
|
|
init: function init() {
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log.level = Log.Level[Svc.Prefs.get("log.logger.service.main")];
|
2011-06-29 05:48:55 +04:00
|
|
|
this.setDefaults();
|
2011-06-27 17:22:49 +04:00
|
|
|
Svc.Obs.add("weave:engine:score:updated", this);
|
|
|
|
Svc.Obs.add("network:offline-status-changed", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:start", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:finish", this);
|
|
|
|
Svc.Obs.add("weave:engine:sync:finish", this);
|
2011-08-27 01:01:35 +04:00
|
|
|
Svc.Obs.add("weave:engine:sync:error", this);
|
2011-06-27 17:22:49 +04:00
|
|
|
Svc.Obs.add("weave:service:login:error", this);
|
|
|
|
Svc.Obs.add("weave:service:logout:finish", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:error", this);
|
|
|
|
Svc.Obs.add("weave:service:backoff:interval", this);
|
2011-07-21 23:20:55 +04:00
|
|
|
Svc.Obs.add("weave:service:ready", this);
|
2011-06-27 17:24:33 +04:00
|
|
|
Svc.Obs.add("weave:engine:sync:applied", this);
|
2011-07-21 23:21:34 +04:00
|
|
|
Svc.Obs.add("weave:service:setup-complete", this);
|
|
|
|
Svc.Obs.add("weave:service:start-over", this);
|
2014-04-10 06:08:19 +04:00
|
|
|
Svc.Obs.add("FxA:hawk:backoff:interval", this);
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2011-07-21 23:21:34 +04:00
|
|
|
if (Status.checkSetup() == STATUS_OK) {
|
2014-09-10 04:16:24 +04:00
|
|
|
Svc.Obs.add("wake_notification", this);
|
2011-07-21 23:21:34 +04:00
|
|
|
Svc.Idle.addIdleObserver(this, Svc.Prefs.get("scheduler.idleTime"));
|
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("Handling " + topic);
|
2011-06-27 17:22:49 +04:00
|
|
|
switch(topic) {
|
|
|
|
case "weave:engine:score:updated":
|
2011-09-06 20:17:26 +04:00
|
|
|
if (Status.login == LOGIN_SUCCEEDED) {
|
|
|
|
Utils.namedTimer(this.calculateScore, SCORE_UPDATE_DELAY, this,
|
|
|
|
"_scoreTimer");
|
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
break;
|
|
|
|
case "network:offline-status-changed":
|
|
|
|
// Whether online or offline, we'll reschedule syncs
|
|
|
|
this._log.trace("Network offline status change: " + data);
|
|
|
|
this.checkSyncStatus();
|
|
|
|
break;
|
|
|
|
case "weave:service:sync:start":
|
|
|
|
// Clear out any potentially pending syncs now that we're syncing
|
|
|
|
this.clearSyncTriggers();
|
|
|
|
|
|
|
|
// reset backoff info, if the server tells us to continue backing off,
|
|
|
|
// we'll handle that later
|
|
|
|
Status.resetBackoff();
|
|
|
|
|
|
|
|
this.globalScore = 0;
|
|
|
|
break;
|
|
|
|
case "weave:service:sync:finish":
|
2011-10-05 07:52:14 +04:00
|
|
|
this.nextSync = 0;
|
2011-06-27 17:24:33 +04:00
|
|
|
this.adjustSyncInterval();
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
if (Status.service == SYNC_FAILED_PARTIAL && this.requiresBackoff) {
|
|
|
|
this.requiresBackoff = false;
|
|
|
|
this.handleSyncError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-05 07:52:14 +04:00
|
|
|
let sync_interval;
|
2011-08-27 01:01:35 +04:00
|
|
|
this._syncErrors = 0;
|
2011-06-27 17:22:49 +04:00
|
|
|
if (Status.sync == NO_SYNC_NODE_FOUND) {
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.trace("Scheduling a sync at interval NO_SYNC_NODE_FOUND.");
|
2011-06-27 17:22:49 +04:00
|
|
|
sync_interval = NO_SYNC_NODE_INTERVAL;
|
|
|
|
}
|
|
|
|
this.scheduleNextSync(sync_interval);
|
|
|
|
break;
|
|
|
|
case "weave:engine:sync:finish":
|
2011-07-30 05:47:39 +04:00
|
|
|
if (data == "clients") {
|
2011-06-27 17:22:49 +04:00
|
|
|
// Update the client mode because it might change what we sync.
|
|
|
|
this.updateClientMode();
|
|
|
|
}
|
|
|
|
break;
|
2011-08-27 01:01:35 +04:00
|
|
|
case "weave:engine:sync:error":
|
2011-10-28 09:25:01 +04:00
|
|
|
// `subject` is the exception thrown by an engine's sync() method.
|
2011-08-27 01:01:35 +04:00
|
|
|
let exception = subject;
|
|
|
|
if (exception.status >= 500 && exception.status <= 504) {
|
|
|
|
this.requiresBackoff = true;
|
|
|
|
}
|
|
|
|
break;
|
2011-06-27 17:22:49 +04:00
|
|
|
case "weave:service:login:error":
|
|
|
|
this.clearSyncTriggers();
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2011-06-30 07:12:16 +04:00
|
|
|
if (Status.login == MASTER_PASSWORD_LOCKED) {
|
2011-10-28 09:25:00 +04:00
|
|
|
// Try again later, just as if we threw an error... only without the
|
|
|
|
// error count.
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.debug("Couldn't log in: master password is locked.");
|
|
|
|
this._log.trace("Scheduling a sync at MASTER_PASSWORD_LOCKED_RETRY_INTERVAL");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.scheduleAtInterval(MASTER_PASSWORD_LOCKED_RETRY_INTERVAL);
|
2011-10-28 09:25:00 +04:00
|
|
|
} else if (this._fatalLoginStatus.indexOf(Status.login) == -1) {
|
|
|
|
// Not a fatal login error, just an intermittent network or server
|
|
|
|
// issue. Keep on syncin'.
|
|
|
|
this.checkSyncStatus();
|
2011-06-30 07:12:16 +04:00
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
break;
|
|
|
|
case "weave:service:logout:finish":
|
|
|
|
// Start or cancel the sync timer depending on if
|
|
|
|
// logged in or logged out
|
|
|
|
this.checkSyncStatus();
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
2011-06-27 17:22:49 +04:00
|
|
|
case "weave:service:sync:error":
|
2011-07-21 23:21:34 +04:00
|
|
|
// There may be multiple clients but if the sync fails, client mode
|
|
|
|
// should still be updated so that the next sync has a correct interval.
|
|
|
|
this.updateClientMode();
|
2011-06-27 17:24:33 +04:00
|
|
|
this.adjustSyncInterval();
|
2011-10-05 07:52:14 +04:00
|
|
|
this.nextSync = 0;
|
2011-06-27 17:22:49 +04:00
|
|
|
this.handleSyncError();
|
|
|
|
break;
|
2014-04-10 06:08:19 +04:00
|
|
|
case "FxA:hawk:backoff:interval":
|
2011-06-27 17:22:49 +04:00
|
|
|
case "weave:service:backoff:interval":
|
2011-10-04 09:52:07 +04:00
|
|
|
let requested_interval = subject * 1000;
|
2012-12-22 23:43:56 +04:00
|
|
|
this._log.debug("Got backoff notification: " + requested_interval + "ms");
|
2011-10-04 09:52:07 +04:00
|
|
|
// Leave up to 25% more time for the back off.
|
|
|
|
let interval = requested_interval * (1 + Math.random() * 0.25);
|
2011-06-27 17:22:49 +04:00
|
|
|
Status.backoffInterval = interval;
|
2011-10-04 09:52:07 +04:00
|
|
|
Status.minimumNextSync = Date.now() + requested_interval;
|
2012-12-22 23:43:56 +04:00
|
|
|
this._log.debug("Fuzzed minimum next sync: " + Status.minimumNextSync);
|
2011-06-27 17:22:49 +04:00
|
|
|
break;
|
2011-07-21 23:20:55 +04:00
|
|
|
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;
|
2011-06-27 17:24:33 +04:00
|
|
|
case "weave:engine:sync:applied":
|
2012-03-07 02:27:56 +04:00
|
|
|
let numItems = subject.succeeded;
|
|
|
|
this._log.trace("Engine " + data + " successfully applied " + numItems +
|
|
|
|
" items.");
|
|
|
|
if (numItems) {
|
2011-06-27 17:24:33 +04:00
|
|
|
this.hasIncomingItems = true;
|
2012-03-07 02:27:56 +04:00
|
|
|
}
|
2011-06-27 17:24:33 +04:00
|
|
|
break;
|
2011-07-21 23:21:34 +04:00
|
|
|
case "weave:service:setup-complete":
|
2012-07-31 03:05:20 +04:00
|
|
|
Services.prefs.savePrefFile(null);
|
2011-07-21 23:21:34 +04:00
|
|
|
Svc.Idle.addIdleObserver(this, Svc.Prefs.get("scheduler.idleTime"));
|
2014-09-10 04:16:24 +04:00
|
|
|
Svc.Obs.add("wake_notification", this);
|
2011-07-21 23:21:34 +04:00
|
|
|
break;
|
|
|
|
case "weave:service:start-over":
|
2012-08-30 01:43:41 +04:00
|
|
|
this.setDefaults();
|
2011-09-23 02:44:06 +04:00
|
|
|
try {
|
|
|
|
Svc.Idle.removeIdleObserver(this, Svc.Prefs.get("scheduler.idleTime"));
|
2016-01-26 18:13:31 +03:00
|
|
|
} catch (ex) {
|
|
|
|
if (ex.result != Cr.NS_ERROR_FAILURE) {
|
|
|
|
throw ex;
|
|
|
|
}
|
2011-09-23 02:44:06 +04:00
|
|
|
// In all likelihood we didn't have an idle observer registered yet.
|
|
|
|
// It's all good.
|
|
|
|
}
|
2011-07-21 23:21:34 +04:00
|
|
|
break;
|
2011-06-27 17:24:33 +04:00
|
|
|
case "idle":
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.trace("We're idle.");
|
2011-06-27 17:24:33 +04:00
|
|
|
this.idle = true;
|
2011-06-29 05:48:55 +04:00
|
|
|
// Adjust the interval for future syncs. This won't actually have any
|
|
|
|
// effect until the next pending sync (which will happen soon since we
|
|
|
|
// were just active.)
|
|
|
|
this.adjustSyncInterval();
|
2011-06-27 17:24:33 +04:00
|
|
|
break;
|
2013-12-28 09:12:50 +04:00
|
|
|
case "active":
|
2011-10-05 07:04:13 +04:00
|
|
|
this._log.trace("Received notification that we're back from idle.");
|
2011-06-27 17:24:33 +04:00
|
|
|
this.idle = false;
|
2011-10-05 07:04:13 +04:00
|
|
|
Utils.namedTimer(function onBack() {
|
|
|
|
if (this.idle) {
|
|
|
|
this._log.trace("... and we're idle again. " +
|
|
|
|
"Ignoring spurious back notification.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.trace("Genuine return from idle. Syncing.");
|
|
|
|
// Trigger a sync if we have multiple clients.
|
|
|
|
if (this.numClients > 1) {
|
2011-10-05 07:52:14 +04:00
|
|
|
this.scheduleNextSync(0);
|
2011-10-05 07:04:13 +04:00
|
|
|
}
|
|
|
|
}, IDLE_OBSERVER_BACK_DELAY, this, "idleDebouncerTimer");
|
2011-06-27 17:24:33 +04:00
|
|
|
break;
|
2014-09-10 04:16:24 +04:00
|
|
|
case "wake_notification":
|
|
|
|
this._log.debug("Woke from sleep.");
|
|
|
|
Utils.nextTick(() => {
|
2015-11-12 02:54:55 +03:00
|
|
|
// Trigger a sync if we have multiple clients. We give it 5 seconds
|
|
|
|
// incase the network is still in the process of coming back up.
|
2014-09-10 04:16:24 +04:00
|
|
|
if (this.numClients > 1) {
|
2015-11-12 02:54:55 +03:00
|
|
|
this._log.debug("More than 1 client. Will sync in 5s.");
|
|
|
|
this.scheduleNextSync(5000);
|
2014-09-10 04:16:24 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2011-06-27 17:24:33 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
adjustSyncInterval: function adjustSyncInterval() {
|
2013-09-04 03:11:46 +04:00
|
|
|
if (Status.eol) {
|
|
|
|
this._log.debug("Server status is EOL; using eolInterval.");
|
|
|
|
this.syncInterval = this.eolInterval;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-27 17:24:33 +04:00
|
|
|
if (this.numClients <= 1) {
|
2011-06-30 07:12:24 +04:00
|
|
|
this._log.trace("Adjusting syncInterval to singleDeviceInterval.");
|
|
|
|
this.syncInterval = this.singleDeviceInterval;
|
2011-06-27 17:24:33 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-09-04 03:11:46 +04:00
|
|
|
|
2011-06-27 17:24:33 +04:00
|
|
|
// Only MULTI_DEVICE clients will enter this if statement
|
|
|
|
// since SINGLE_USER clients will be handled above.
|
|
|
|
if (this.idle) {
|
2011-06-30 07:12:24 +04:00
|
|
|
this._log.trace("Adjusting syncInterval to idleInterval.");
|
|
|
|
this.syncInterval = this.idleInterval;
|
2011-06-27 17:24:33 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasIncomingItems) {
|
2011-06-30 07:12:24 +04:00
|
|
|
this._log.trace("Adjusting syncInterval to immediateInterval.");
|
2011-06-27 17:24:33 +04:00
|
|
|
this.hasIncomingItems = false;
|
2011-06-30 07:12:24 +04:00
|
|
|
this.syncInterval = this.immediateInterval;
|
2011-06-27 17:24:33 +04:00
|
|
|
} else {
|
2011-06-30 07:12:24 +04:00
|
|
|
this._log.trace("Adjusting syncInterval to activeInterval.");
|
|
|
|
this.syncInterval = this.activeInterval;
|
2011-06-27 17:24:33 +04:00
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
calculateScore: function calculateScore() {
|
2012-08-30 01:43:41 +04:00
|
|
|
let engines = [this.service.clientsEngine].concat(this.service.engineManager.getEnabled());
|
2011-06-27 17:22:49 +04:00
|
|
|
for (let i = 0;i < engines.length;i++) {
|
|
|
|
this._log.trace(engines[i].name + ": score: " + engines[i].score);
|
|
|
|
this.globalScore += engines[i].score;
|
|
|
|
engines[i]._tracker.resetScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.trace("Global score updated: " + this.globalScore);
|
|
|
|
this.checkSyncStatus();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the locally stored clients list to figure out what mode to be in
|
|
|
|
*/
|
|
|
|
updateClientMode: function updateClientMode() {
|
|
|
|
// Nothing to do if it's the same amount
|
2012-08-30 01:43:41 +04:00
|
|
|
let numClients = this.service.clientsEngine.stats.numClients;
|
2011-06-27 17:24:33 +04:00
|
|
|
if (this.numClients == numClients)
|
2011-06-27 17:22:49 +04:00
|
|
|
return;
|
|
|
|
|
2011-06-27 17:24:33 +04:00
|
|
|
this._log.debug("Client count: " + this.numClients + " -> " + numClients);
|
|
|
|
this.numClients = numClients;
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2011-06-27 17:24:33 +04:00
|
|
|
if (numClients <= 1) {
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.trace("Adjusting syncThreshold to SINGLE_USER_THRESHOLD");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.syncThreshold = SINGLE_USER_THRESHOLD;
|
2011-06-30 07:12:16 +04:00
|
|
|
} else {
|
|
|
|
this._log.trace("Adjusting syncThreshold to MULTI_DEVICE_THRESHOLD");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.syncThreshold = MULTI_DEVICE_THRESHOLD;
|
|
|
|
}
|
2011-06-27 17:24:33 +04:00
|
|
|
this.adjustSyncInterval();
|
2011-06-27 17:22:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we should be syncing and schedule the next sync, if it's not scheduled
|
|
|
|
*/
|
|
|
|
checkSyncStatus: function checkSyncStatus() {
|
|
|
|
// Should we be syncing now, if not, cancel any sync timers and return
|
2011-07-14 02:20:07 +04:00
|
|
|
// if we're in backoff, we'll schedule the next sync.
|
|
|
|
let ignore = [kSyncBackoffNotMet, kSyncMasterPasswordLocked];
|
2012-08-30 01:43:41 +04:00
|
|
|
let skip = this.service._checkSync(ignore);
|
2011-06-27 17:22:49 +04:00
|
|
|
this._log.trace("_checkSync returned \"" + skip + "\".");
|
|
|
|
if (skip) {
|
|
|
|
this.clearSyncTriggers();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only set the wait time to 0 if we need to sync right away
|
|
|
|
let wait;
|
|
|
|
if (this.globalScore > this.syncThreshold) {
|
|
|
|
this._log.debug("Global Score threshold hit, triggering sync.");
|
|
|
|
wait = 0;
|
|
|
|
}
|
|
|
|
this.scheduleNextSync(wait);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call sync() if Master Password is not locked.
|
2011-08-27 01:01:35 +04:00
|
|
|
*
|
2011-06-27 17:22:49 +04:00
|
|
|
* Otherwise, reschedule a sync for later.
|
|
|
|
*/
|
|
|
|
syncIfMPUnlocked: function syncIfMPUnlocked() {
|
|
|
|
// No point if we got kicked out by the master password dialog.
|
|
|
|
if (Status.login == MASTER_PASSWORD_LOCKED &&
|
|
|
|
Utils.mpLocked()) {
|
|
|
|
this._log.debug("Not initiating sync: Login status is " + Status.login);
|
|
|
|
|
|
|
|
// If we're not syncing now, we need to schedule the next one.
|
2011-06-30 07:12:16 +04:00
|
|
|
this._log.trace("Scheduling a sync at MASTER_PASSWORD_LOCKED_RETRY_INTERVAL");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.scheduleAtInterval(MASTER_PASSWORD_LOCKED_RETRY_INTERVAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-30 01:43:41 +04:00
|
|
|
Utils.nextTick(this.service.sync, this.service);
|
2011-06-27 17:22:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a timer for the next sync
|
|
|
|
*/
|
|
|
|
scheduleNextSync: function scheduleNextSync(interval) {
|
2011-10-05 07:52:14 +04:00
|
|
|
// If no interval was specified, use the current sync interval.
|
|
|
|
if (interval == null) {
|
|
|
|
interval = this.syncInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the interval is set to no less than the backoff.
|
|
|
|
if (Status.backoffInterval && interval < Status.backoffInterval) {
|
2011-10-14 04:58:44 +04:00
|
|
|
this._log.trace("Requested interval " + interval +
|
2012-08-30 01:43:41 +04:00
|
|
|
" ms is smaller than the backoff interval. " +
|
2011-10-14 04:58:44 +04:00
|
|
|
"Using backoff interval " +
|
|
|
|
Status.backoffInterval + " ms instead.");
|
2011-10-05 07:52:14 +04:00
|
|
|
interval = Status.backoffInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.nextSync != 0) {
|
2011-10-14 04:58:44 +04:00
|
|
|
// There's already a sync scheduled. Don't reschedule if there's already
|
|
|
|
// a timer scheduled for sooner than requested.
|
2011-10-05 07:52:14 +04:00
|
|
|
let currentInterval = this.nextSync - Date.now();
|
2011-10-14 04:58:44 +04:00
|
|
|
this._log.trace("There's already a sync scheduled in " +
|
|
|
|
currentInterval + " ms.");
|
|
|
|
if (currentInterval < interval && this.syncTimer) {
|
|
|
|
this._log.trace("Ignoring scheduling request for next sync in " +
|
|
|
|
interval + " ms.");
|
2011-10-05 07:52:14 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
}
|
|
|
|
|
2011-10-14 04:58:44 +04:00
|
|
|
// Start the sync right away if we're already late.
|
2011-06-27 17:22:49 +04:00
|
|
|
if (interval <= 0) {
|
2011-10-14 04:58:44 +04:00
|
|
|
this._log.trace("Requested sync should happen right away.");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.syncIfMPUnlocked();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-14 04:58:44 +04:00
|
|
|
this._log.debug("Next sync in " + interval + " ms.");
|
2011-06-30 07:12:27 +04:00
|
|
|
Utils.namedTimer(this.syncIfMPUnlocked, interval, this, "syncTimer");
|
2011-06-27 17:22:49 +04:00
|
|
|
|
|
|
|
// Save the next sync time in-case sync is disabled (logout/offline/etc.)
|
|
|
|
this.nextSync = Date.now() + interval;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Incorporates the backoff/retry logic used in error handling and elective
|
|
|
|
* non-syncing.
|
|
|
|
*/
|
|
|
|
scheduleAtInterval: function scheduleAtInterval(minimumInterval) {
|
2012-03-23 02:49:50 +04:00
|
|
|
let interval = Utils.calculateBackoff(this._syncErrors,
|
|
|
|
MINIMUM_BACKOFF_INTERVAL,
|
|
|
|
Status.backoffInterval);
|
2011-10-14 04:58:44 +04:00
|
|
|
if (minimumInterval) {
|
2011-06-27 17:22:49 +04:00
|
|
|
interval = Math.max(minimumInterval, interval);
|
2011-10-14 04:58:44 +04:00
|
|
|
}
|
2011-06-27 17:22:49 +04:00
|
|
|
|
2011-10-14 04:58:44 +04:00
|
|
|
this._log.debug("Starting client-initiated backoff. Next sync in " +
|
|
|
|
interval + " ms.");
|
2011-06-27 17:22:49 +04:00
|
|
|
this.scheduleNextSync(interval);
|
|
|
|
},
|
|
|
|
|
2011-07-21 23:20:55 +04:00
|
|
|
/**
|
|
|
|
* 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) {
|
2012-08-30 01:43:41 +04:00
|
|
|
if (this.service._checkSetup() == STATUS_OK) {
|
2011-07-21 23:20:55 +04:00
|
|
|
Utils.namedTimer(this.autoConnect, delay * 1000, this, "_autoTimer");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
autoConnect: function autoConnect() {
|
2012-08-30 01:43:41 +04:00
|
|
|
if (this.service._checkSetup() == STATUS_OK && !this.service._checkSync()) {
|
2011-10-14 04:58:44 +04:00
|
|
|
// Schedule a sync based on when a previous sync was scheduled.
|
|
|
|
// scheduleNextSync() will do the right thing if that time lies in
|
|
|
|
// the past.
|
|
|
|
this.scheduleNextSync(this.nextSync - Date.now());
|
2011-07-21 23:20:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Once autoConnect is called we no longer need _autoTimer.
|
|
|
|
if (this._autoTimer) {
|
|
|
|
this._autoTimer.clear();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-06-27 17:22:49 +04:00
|
|
|
_syncErrors: 0,
|
|
|
|
/**
|
|
|
|
* Deal with sync errors appropriately
|
|
|
|
*/
|
|
|
|
handleSyncError: function handleSyncError() {
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("In handleSyncError. Error count: " + this._syncErrors);
|
2011-06-27 17:22:49 +04:00
|
|
|
this._syncErrors++;
|
|
|
|
|
|
|
|
// Do nothing on the first couple of failures, if we're not in
|
|
|
|
// backoff due to 5xx errors.
|
|
|
|
if (!Status.enforceBackoff) {
|
|
|
|
if (this._syncErrors < MAX_ERROR_COUNT_BEFORE_BACKOFF) {
|
|
|
|
this.scheduleNextSync();
|
|
|
|
return;
|
|
|
|
}
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.debug("Sync error count has exceeded " +
|
|
|
|
MAX_ERROR_COUNT_BEFORE_BACKOFF + "; enforcing backoff.");
|
2011-06-27 17:22:49 +04:00
|
|
|
Status.enforceBackoff = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scheduleAtInterval();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any timers/observers that might trigger a sync
|
|
|
|
*/
|
|
|
|
clearSyncTriggers: function clearSyncTriggers() {
|
2011-11-01 22:41:47 +04:00
|
|
|
this._log.debug("Clearing sync triggers and the global score.");
|
|
|
|
this.globalScore = this.nextSync = 0;
|
2011-06-27 17:22:49 +04:00
|
|
|
|
|
|
|
// Clear out any scheduled syncs
|
2011-06-29 05:48:55 +04:00
|
|
|
if (this.syncTimer)
|
|
|
|
this.syncTimer.clear();
|
2012-08-30 01:43:41 +04:00
|
|
|
},
|
2014-12-10 02:39:23 +03:00
|
|
|
|
2011-06-27 17:22:49 +04:00
|
|
|
};
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.ErrorHandler = function ErrorHandler(service) {
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service = service;
|
2012-08-30 01:43:40 +04:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
ErrorHandler.prototype = {
|
2013-09-04 03:11:46 +04:00
|
|
|
MINIMUM_ALERT_INTERVAL_MSEC: 604800000, // One week.
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
/**
|
|
|
|
* Flag that turns on error reporting for all errors, incl. network errors.
|
|
|
|
*/
|
|
|
|
dontIgnoreErrors: false,
|
|
|
|
|
2014-04-17 05:25:44 +04:00
|
|
|
/**
|
|
|
|
* Flag that indicates if we have already reported a prolonged failure.
|
|
|
|
* Once set, we don't report it again, meaning this error is only reported
|
|
|
|
* one per run.
|
|
|
|
*/
|
|
|
|
didReportProlongedError: false,
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
init: function init() {
|
2011-08-27 01:01:35 +04:00
|
|
|
Svc.Obs.add("weave:engine:sync:applied", this);
|
|
|
|
Svc.Obs.add("weave:engine:sync:error", this);
|
2011-08-27 01:01:35 +04:00
|
|
|
Svc.Obs.add("weave:service:login:error", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:error", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:finish", this);
|
|
|
|
|
|
|
|
this.initLogs();
|
|
|
|
},
|
|
|
|
|
|
|
|
initLogs: function initLogs() {
|
2013-08-26 22:55:58 +04:00
|
|
|
this._log = Log.repository.getLogger("Sync.ErrorHandler");
|
|
|
|
this._log.level = Log.Level[Svc.Prefs.get("log.logger.service.main")];
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2013-08-26 22:55:58 +04:00
|
|
|
let root = Log.repository.getLogger("Sync");
|
|
|
|
root.level = Log.Level[Svc.Prefs.get("log.rootLogger")];
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2015-02-18 00:06:00 +03:00
|
|
|
let logs = ["Sync", "FirefoxAccounts", "Hawk", "Common.TokenServerClient",
|
2015-08-06 07:47:31 +03:00
|
|
|
"Sync.SyncMigration", "browserwindow.syncui"];
|
2015-02-18 00:06:00 +03:00
|
|
|
|
|
|
|
this._logManager = new LogManager(Svc.Prefs, logs, "sync");
|
2011-08-27 01:01:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function observe(subject, topic, data) {
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("Handling " + topic);
|
2011-08-27 01:01:35 +04:00
|
|
|
switch(topic) {
|
2011-08-27 01:01:35 +04:00
|
|
|
case "weave:engine:sync:applied":
|
|
|
|
if (subject.newFailed) {
|
|
|
|
// An engine isn't able to apply one or more incoming records.
|
|
|
|
// We don't fail hard on this, but it usually indicates a bug,
|
|
|
|
// so for now treat it as sync error (c.f. Service._syncEngine())
|
|
|
|
Status.engines = [data, ENGINE_APPLY_FAIL];
|
|
|
|
this._log.debug(data + " failed to apply some records.");
|
|
|
|
}
|
|
|
|
break;
|
2016-02-03 03:21:09 +03:00
|
|
|
case "weave:engine:sync:error": {
|
2011-08-27 01:01:35 +04:00
|
|
|
let exception = subject; // exception thrown by engine's sync() method
|
|
|
|
let engine_name = data; // engine name that threw the exception
|
|
|
|
|
2016-01-12 04:21:28 +03:00
|
|
|
this.checkServerError(exception);
|
2011-08-27 01:01:35 +04:00
|
|
|
|
|
|
|
Status.engines = [engine_name, exception.failureCode || ENGINE_UNKNOWN_FAIL];
|
2016-02-03 03:21:09 +03:00
|
|
|
if (Async.isShutdownException(exception)) {
|
|
|
|
this._log.debug(engine_name + " was interrupted due to the application shutting down");
|
|
|
|
} else {
|
|
|
|
this._log.debug(engine_name + " failed", exception);
|
|
|
|
Services.telemetry.getKeyedHistogramById("WEAVE_ENGINE_SYNC_ERRORS")
|
|
|
|
.add(engine_name);
|
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
2016-02-03 03:21:09 +03:00
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
case "weave:service:login:error":
|
2015-05-18 10:34:22 +03:00
|
|
|
this._log.error("Sync encountered a login error");
|
|
|
|
this.resetFileLog();
|
2011-10-11 19:24:26 +04:00
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
if (this.shouldReportError()) {
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:login:error");
|
2011-08-31 02:38:31 +04:00
|
|
|
} else {
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:clear-error");
|
2011-08-27 01:01:35 +04:00
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
|
|
|
|
this.dontIgnoreErrors = false;
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
2016-02-03 03:21:09 +03:00
|
|
|
case "weave:service:sync:error": {
|
2011-08-27 01:01:35 +04:00
|
|
|
if (Status.sync == CREDENTIALS_CHANGED) {
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service.logout();
|
2011-08-27 01:01:35 +04:00
|
|
|
}
|
|
|
|
|
2016-02-03 03:21:09 +03:00
|
|
|
let exception = subject;
|
|
|
|
if (Async.isShutdownException(exception)) {
|
|
|
|
// If we are shutting down we just log the fact, attempt to flush
|
|
|
|
// the log file and get out of here!
|
|
|
|
this._log.error("Sync was interrupted due to the application shutting down");
|
|
|
|
this.resetFileLog();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a shutdown related exception...
|
|
|
|
this._log.error("Sync encountered an error", exception);
|
2015-05-18 10:34:22 +03:00
|
|
|
this.resetFileLog();
|
2011-10-11 19:24:26 +04:00
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
if (this.shouldReportError()) {
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:sync:error");
|
2011-08-27 01:01:35 +04:00
|
|
|
} else {
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:sync:finish");
|
2011-08-27 01:01:35 +04:00
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
|
|
|
|
this.dontIgnoreErrors = false;
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
2016-02-03 03:21:09 +03:00
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
case "weave:service:sync:finish":
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("Status.service is " + Status.service);
|
2011-10-28 09:25:01 +04:00
|
|
|
|
|
|
|
// Check both of these status codes: in the event of a failure in one
|
|
|
|
// engine, Status.service will be SYNC_FAILED_PARTIAL despite
|
|
|
|
// Status.sync being SYNC_SUCCEEDED.
|
|
|
|
// *facepalm*
|
|
|
|
if (Status.sync == SYNC_SUCCEEDED &&
|
|
|
|
Status.service == STATUS_OK) {
|
|
|
|
// Great. Let's clear our mid-sync 401 note.
|
|
|
|
this._log.trace("Clearing lastSyncReassigned.");
|
|
|
|
Svc.Prefs.reset("lastSyncReassigned");
|
|
|
|
}
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
if (Status.service == SYNC_FAILED_PARTIAL) {
|
2015-05-18 10:34:22 +03:00
|
|
|
this._log.error("Some engines did not sync correctly.");
|
|
|
|
this.resetFileLog();
|
2011-08-27 01:01:35 +04:00
|
|
|
|
|
|
|
if (this.shouldReportError()) {
|
2011-08-27 03:45:08 +04:00
|
|
|
this.dontIgnoreErrors = false;
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:sync:error");
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
} else {
|
2015-05-18 10:34:22 +03:00
|
|
|
this.resetFileLog();
|
2011-08-27 01:01:35 +04:00
|
|
|
}
|
2011-08-27 03:45:08 +04:00
|
|
|
this.dontIgnoreErrors = false;
|
2011-09-07 06:15:28 +04:00
|
|
|
this.notifyOnNextTick("weave:ui:sync:finish");
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-09-07 06:15:28 +04:00
|
|
|
notifyOnNextTick: function notifyOnNextTick(topic) {
|
2011-10-28 09:25:00 +04:00
|
|
|
Utils.nextTick(function() {
|
|
|
|
this._log.trace("Notifying " + topic +
|
|
|
|
". Status.login is " + Status.login +
|
|
|
|
". Status.sync is " + Status.sync);
|
|
|
|
Svc.Obs.notify(topic);
|
|
|
|
}, this);
|
2011-09-07 06:15:28 +04:00
|
|
|
},
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
/**
|
|
|
|
* Trigger a sync and don't muffle any errors, particularly network errors.
|
|
|
|
*/
|
|
|
|
syncAndReportErrors: function syncAndReportErrors() {
|
|
|
|
this._log.debug("Beginning user-triggered sync.");
|
|
|
|
|
|
|
|
this.dontIgnoreErrors = true;
|
2012-08-30 01:43:41 +04:00
|
|
|
Utils.nextTick(this.service.sync, this.service);
|
2011-08-27 01:01:35 +04:00
|
|
|
},
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
/**
|
|
|
|
* Generate a log file for the sync that just completed
|
|
|
|
* and refresh the input & output streams.
|
|
|
|
*/
|
2015-05-18 10:34:22 +03:00
|
|
|
resetFileLog: function resetFileLog() {
|
2015-05-18 10:34:22 +03:00
|
|
|
let onComplete = logType => {
|
2011-08-27 01:01:35 +04:00
|
|
|
Svc.Obs.notify("weave:service:reset-file-log");
|
2015-02-18 00:06:00 +03:00
|
|
|
this._log.trace("Notified: " + Date.now());
|
2015-05-18 10:34:22 +03:00
|
|
|
if (logType == this._logManager.ERROR_LOG_WRITTEN) {
|
|
|
|
Cu.reportError("Sync encountered an error - see about:sync-log for the log file.");
|
|
|
|
}
|
2015-02-18 00:06:00 +03:00
|
|
|
};
|
|
|
|
// Note we do not return the promise here - the caller doesn't need to wait
|
|
|
|
// for this to complete.
|
2015-05-18 10:34:22 +03:00
|
|
|
this._logManager.resetFileLog().then(onComplete, onComplete);
|
2011-08-27 01:01:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates server error codes to meaningful strings.
|
2011-08-27 01:01:35 +04:00
|
|
|
*
|
2011-08-27 01:01:35 +04:00
|
|
|
* @param code
|
|
|
|
* server error code as an integer
|
|
|
|
*/
|
|
|
|
errorStr: function errorStr(code) {
|
|
|
|
switch (code.toString()) {
|
|
|
|
case "1":
|
|
|
|
return "illegal-method";
|
|
|
|
case "2":
|
|
|
|
return "invalid-captcha";
|
|
|
|
case "3":
|
|
|
|
return "invalid-username";
|
|
|
|
case "4":
|
|
|
|
return "cannot-overwrite-resource";
|
|
|
|
case "5":
|
|
|
|
return "userid-mismatch";
|
|
|
|
case "6":
|
|
|
|
return "json-parse-failure";
|
|
|
|
case "7":
|
|
|
|
return "invalid-password";
|
|
|
|
case "8":
|
|
|
|
return "invalid-record";
|
|
|
|
case "9":
|
|
|
|
return "weak-password";
|
|
|
|
default:
|
|
|
|
return "generic-server-error";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-08-06 07:47:31 +03:00
|
|
|
// A function to indicate if Sync errors should be "reported" - which in this
|
|
|
|
// context really means "should be notify observers of an error" - but note
|
|
|
|
// that since bug 1180587, no one is going to surface an error to the user.
|
2011-08-27 01:01:35 +04:00
|
|
|
shouldReportError: function shouldReportError() {
|
|
|
|
if (Status.login == MASTER_PASSWORD_LOCKED) {
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("shouldReportError: false (master password locked).");
|
2011-08-27 01:01:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-27 03:45:08 +04:00
|
|
|
if (this.dontIgnoreErrors) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-05 08:12:51 +03:00
|
|
|
if (Status.login == LOGIN_FAILED_LOGIN_REJECTED) {
|
|
|
|
// An explicit LOGIN_REJECTED state is always reported (bug 1081158)
|
|
|
|
this._log.trace("shouldReportError: true (login was rejected)");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
let lastSync = Svc.Prefs.get("lastSync");
|
|
|
|
if (lastSync && ((Date.now() - Date.parse(lastSync)) >
|
|
|
|
Svc.Prefs.get("errorhandler.networkFailureReportTimeout") * 1000)) {
|
|
|
|
Status.sync = PROLONGED_SYNC_FAILURE;
|
2014-04-17 05:25:44 +04:00
|
|
|
if (this.didReportProlongedError) {
|
|
|
|
this._log.trace("shouldReportError: false (prolonged sync failure, but" +
|
|
|
|
" we've already reported it).");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._log.trace("shouldReportError: true (first prolonged sync failure).");
|
|
|
|
this.didReportProlongedError = true;
|
2011-08-27 01:01:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-30 01:43:41 +04:00
|
|
|
|
2011-10-28 09:25:01 +04:00
|
|
|
// We got a 401 mid-sync. Wait for the next sync before actually handling
|
|
|
|
// an error. This assumes that we'll get a 401 again on a login fetch in
|
|
|
|
// order to report the error.
|
2012-08-30 01:43:41 +04:00
|
|
|
if (!this.service.clusterURL) {
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.trace("shouldReportError: false (no cluster URL; " +
|
|
|
|
"possible node reassignment).");
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
|
2015-04-24 04:49:22 +03:00
|
|
|
|
|
|
|
let result = ([Status.login, Status.sync].indexOf(SERVER_MAINTENANCE) == -1 &&
|
|
|
|
[Status.login, Status.sync].indexOf(LOGIN_FAILED_NETWORK_ERROR) == -1);
|
|
|
|
this._log.trace("shouldReportError: ${result} due to login=${login}, sync=${sync}",
|
|
|
|
{result, login: Status.login, sync: Status.sync});
|
|
|
|
return result;
|
2011-08-27 01:01:35 +04:00
|
|
|
},
|
|
|
|
|
2013-09-04 03:11:46 +04:00
|
|
|
get currentAlertMode() {
|
|
|
|
return Svc.Prefs.get("errorhandler.alert.mode");
|
|
|
|
},
|
|
|
|
|
|
|
|
set currentAlertMode(str) {
|
|
|
|
return Svc.Prefs.set("errorhandler.alert.mode", str);
|
|
|
|
},
|
|
|
|
|
|
|
|
get earliestNextAlert() {
|
|
|
|
return Svc.Prefs.get("errorhandler.alert.earliestNext", 0) * 1000;
|
|
|
|
},
|
|
|
|
|
|
|
|
set earliestNextAlert(msec) {
|
|
|
|
return Svc.Prefs.set("errorhandler.alert.earliestNext", msec / 1000);
|
|
|
|
},
|
|
|
|
|
|
|
|
clearServerAlerts: function () {
|
|
|
|
// If we have any outstanding alerts, apparently they're no longer relevant.
|
|
|
|
Svc.Prefs.resetBranch("errorhandler.alert");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* X-Weave-Alert headers can include a JSON object:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "code": // One of "hard-eol", "soft-eol".
|
|
|
|
* "url": // For "Learn more" link.
|
|
|
|
* "message": // Logged in Sync logs.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
handleServerAlert: function (xwa) {
|
|
|
|
if (!xwa.code) {
|
|
|
|
this._log.warn("Got structured X-Weave-Alert, but no alert code.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (xwa.code) {
|
|
|
|
// Gently and occasionally notify the user that this service will be
|
|
|
|
// shutting down.
|
|
|
|
case "soft-eol":
|
|
|
|
// Fall through.
|
|
|
|
|
|
|
|
// Tell the user that this service has shut down, and drop our syncing
|
|
|
|
// frequency dramatically.
|
|
|
|
case "hard-eol":
|
|
|
|
// Note that both of these alerts should be subservient to future "sign
|
|
|
|
// in with your Firefox Account" storage alerts.
|
|
|
|
if ((this.currentAlertMode != xwa.code) ||
|
|
|
|
(this.earliestNextAlert < Date.now())) {
|
|
|
|
Utils.nextTick(function() {
|
|
|
|
Svc.Obs.notify("weave:eol", xwa);
|
|
|
|
}, this);
|
|
|
|
this._log.error("X-Weave-Alert: " + xwa.code + ": " + xwa.message);
|
|
|
|
this.earliestNextAlert = Date.now() + this.MINIMUM_ALERT_INTERVAL_MSEC;
|
|
|
|
this.currentAlertMode = xwa.code;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this._log.debug("Got unexpected X-Weave-Alert code: " + xwa.code);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
/**
|
|
|
|
* Handle HTTP response results or exceptions and set the appropriate
|
|
|
|
* Status.* bits.
|
2013-09-04 03:11:46 +04:00
|
|
|
*
|
|
|
|
* This method also looks for "side-channel" warnings.
|
2011-08-27 01:01:35 +04:00
|
|
|
*/
|
2016-01-12 04:21:28 +03:00
|
|
|
checkServerError: function (resp) {
|
2011-08-27 01:01:35 +04:00
|
|
|
switch (resp.status) {
|
2013-09-04 03:11:46 +04:00
|
|
|
case 200:
|
|
|
|
case 404:
|
|
|
|
case 513:
|
|
|
|
let xwa = resp.headers['x-weave-alert'];
|
|
|
|
|
|
|
|
// Only process machine-readable alerts.
|
|
|
|
if (!xwa || !xwa.startsWith("{")) {
|
|
|
|
this.clearServerAlerts();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
xwa = JSON.parse(xwa);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Malformed X-Weave-Alert from server: " + xwa);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleServerAlert(xwa);
|
|
|
|
break;
|
|
|
|
|
2011-08-27 01:01:35 +04:00
|
|
|
case 400:
|
|
|
|
if (resp == RESPONSE_OVER_QUOTA) {
|
|
|
|
Status.sync = OVER_QUOTA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 401:
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service.logout();
|
2011-10-28 09:25:01 +04:00
|
|
|
this._log.info("Got 401 response; resetting clusterURL.");
|
|
|
|
Svc.Prefs.reset("clusterURL");
|
|
|
|
|
|
|
|
let delay = 0;
|
|
|
|
if (Svc.Prefs.get("lastSyncReassigned")) {
|
|
|
|
// We got a 401 in the middle of the previous sync, and we just got
|
|
|
|
// another. Login must have succeeded in order for us to get here, so
|
|
|
|
// the password should be correct.
|
|
|
|
// This is likely to be an intermittent server issue, so back off and
|
|
|
|
// give it time to recover.
|
|
|
|
this._log.warn("Last sync also failed for 401. Delaying next sync.");
|
|
|
|
delay = MINIMUM_BACKOFF_INTERVAL;
|
|
|
|
} else {
|
|
|
|
this._log.debug("New mid-sync 401 failure. Making a note.");
|
|
|
|
Svc.Prefs.set("lastSyncReassigned", true);
|
|
|
|
}
|
|
|
|
this._log.info("Attempting to schedule another sync.");
|
2012-08-30 01:43:41 +04:00
|
|
|
this.service.scheduler.scheduleNextSync(delay);
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 500:
|
|
|
|
case 502:
|
|
|
|
case 503:
|
|
|
|
case 504:
|
|
|
|
Status.enforceBackoff = true;
|
|
|
|
if (resp.status == 503 && resp.headers["retry-after"]) {
|
2012-12-22 23:43:56 +04:00
|
|
|
let retryAfter = resp.headers["retry-after"];
|
|
|
|
this._log.debug("Got Retry-After: " + retryAfter);
|
2012-08-30 01:43:41 +04:00
|
|
|
if (this.service.isLoggedIn) {
|
2011-09-06 20:17:26 +04:00
|
|
|
Status.sync = SERVER_MAINTENANCE;
|
|
|
|
} else {
|
|
|
|
Status.login = SERVER_MAINTENANCE;
|
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
Svc.Obs.notify("weave:service:backoff:interval",
|
2012-12-22 23:43:56 +04:00
|
|
|
parseInt(retryAfter, 10));
|
2011-08-27 01:01:35 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resp.result) {
|
|
|
|
case Cr.NS_ERROR_UNKNOWN_HOST:
|
|
|
|
case Cr.NS_ERROR_CONNECTION_REFUSED:
|
|
|
|
case Cr.NS_ERROR_NET_TIMEOUT:
|
|
|
|
case Cr.NS_ERROR_NET_RESET:
|
|
|
|
case Cr.NS_ERROR_NET_INTERRUPT:
|
|
|
|
case Cr.NS_ERROR_PROXY_CONNECTION_REFUSED:
|
|
|
|
// The constant says it's about login, but in fact it just
|
|
|
|
// indicates general network error.
|
2012-08-30 01:43:41 +04:00
|
|
|
if (this.service.isLoggedIn) {
|
2011-09-06 20:17:26 +04:00
|
|
|
Status.sync = LOGIN_FAILED_NETWORK_ERROR;
|
|
|
|
} else {
|
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
|
|
|
}
|
2011-08-27 01:01:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|