2012-05-21 04:12:37 -07: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/. */
|
2008-11-06 17:37:48 -08:00
|
|
|
|
2009-05-26 14:18:45 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2011-06-13 11:40:08 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/FileUtils.jsm");
|
2014-02-04 21:09:54 -08:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-12-16 20:45:03 -08:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-06 17:37:48 -08:00
|
|
|
|
2013-01-30 07:05:12 -08:00
|
|
|
const SYNC_PREFS_BRANCH = "services.sync.";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync's XPCOM service.
|
|
|
|
*
|
|
|
|
* It is named "Weave" for historical reasons.
|
|
|
|
*
|
|
|
|
* It's worth noting how Sync is lazily loaded. We register a timer that
|
|
|
|
* loads Sync a few seconds after app startup. This is so Sync does not
|
|
|
|
* adversely affect application start time.
|
|
|
|
*
|
|
|
|
* If Sync is not configured, no extra Sync code is loaded. If an
|
|
|
|
* external component (say the UI) needs to interact with Sync, it
|
2014-02-04 21:09:54 -08:00
|
|
|
* should use the promise-base function whenLoaded() - something like the
|
|
|
|
* following:
|
2013-01-30 07:05:12 -08:00
|
|
|
*
|
|
|
|
* // 1. Grab a handle to the Sync XPCOM service.
|
|
|
|
* let service = Cc["@mozilla.org/weave/service;1"]
|
|
|
|
* .getService(Components.interfaces.nsISupports)
|
|
|
|
* .wrappedJSObject;
|
|
|
|
*
|
2014-02-04 21:09:54 -08:00
|
|
|
* // 2. Use the .then method of the promise.
|
|
|
|
* service.whenLoaded().then(() => {
|
|
|
|
* // You are free to interact with "Weave." objects.
|
|
|
|
* return;
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* And that's it! However, if you really want to avoid promises and do it
|
|
|
|
* old-school, then
|
|
|
|
*
|
|
|
|
* // 1. Get a reference to the service as done in (1) above.
|
|
|
|
*
|
2013-01-30 07:05:12 -08:00
|
|
|
* // 2. Check if the service has been initialized.
|
|
|
|
* if (service.ready) {
|
|
|
|
* // You are free to interact with "Weave." objects.
|
|
|
|
* return;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // 3. Install "ready" listener.
|
|
|
|
* Services.obs.addObserver(function onReady() {
|
|
|
|
* Services.obs.removeObserver(onReady, "weave:service:ready");
|
|
|
|
*
|
|
|
|
* // You are free to interact with "Weave." objects.
|
|
|
|
* }, "weave:service:ready", false);
|
|
|
|
*
|
|
|
|
* // 4. Trigger loading of Sync.
|
|
|
|
* service.ensureLoaded();
|
|
|
|
*/
|
2010-06-23 07:28:10 -07:00
|
|
|
function WeaveService() {
|
|
|
|
this.wrappedJSObject = this;
|
2013-01-30 07:05:12 -08:00
|
|
|
this.ready = false;
|
2010-06-23 07:28:10 -07:00
|
|
|
}
|
2008-11-06 17:37:48 -08:00
|
|
|
WeaveService.prototype = {
|
|
|
|
classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
|
|
|
|
|
2009-05-26 14:18:45 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2008-11-06 17:37:48 -08:00
|
|
|
|
2013-01-30 07:05:12 -08:00
|
|
|
ensureLoaded: function () {
|
2014-12-05 16:46:16 -08:00
|
|
|
// If we are loaded and not using FxA, load the migration module.
|
|
|
|
if (!this.fxAccountsEnabled) {
|
|
|
|
Cu.import("resource://services-sync/FxaMigrator.jsm");
|
|
|
|
}
|
|
|
|
|
2013-01-30 07:05:12 -08:00
|
|
|
Components.utils.import("resource://services-sync/main.js");
|
|
|
|
|
|
|
|
// Side-effect of accessing the service is that it is instantiated.
|
|
|
|
Weave.Service;
|
|
|
|
},
|
|
|
|
|
2014-02-04 21:09:54 -08:00
|
|
|
whenLoaded: function() {
|
|
|
|
if (this.ready) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
Services.obs.addObserver(function onReady() {
|
|
|
|
Services.obs.removeObserver(onReady, "weave:service:ready");
|
|
|
|
deferred.resolve();
|
|
|
|
}, "weave:service:ready", false);
|
|
|
|
this.ensureLoaded();
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2014-02-05 08:08:14 -08:00
|
|
|
/**
|
|
|
|
* Whether Firefox Accounts is enabled.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-12-18 21:38:35 -08:00
|
|
|
get fxAccountsEnabled() {
|
2014-01-14 04:13:08 -08:00
|
|
|
try {
|
2014-03-23 17:44:43 -07:00
|
|
|
// Old sync guarantees '@' will never appear in the username while FxA
|
|
|
|
// uses the FxA email address - so '@' is the flag we use.
|
|
|
|
let username = Services.prefs.getCharPref(SYNC_PREFS_BRANCH + "username");
|
2015-04-29 08:32:05 -07:00
|
|
|
return !username || username.includes('@');
|
2014-01-14 04:13:08 -08:00
|
|
|
} catch (_) {
|
2014-03-23 17:44:43 -07:00
|
|
|
return true; // No username == only allow FxA to be configured.
|
2014-01-14 04:13:08 -08:00
|
|
|
}
|
2013-12-18 21:38:35 -08:00
|
|
|
},
|
|
|
|
|
2014-02-05 08:08:14 -08:00
|
|
|
/**
|
|
|
|
* Whether Sync appears to be enabled.
|
|
|
|
*
|
|
|
|
* This returns true if all the Sync preferences for storing account
|
|
|
|
* and server configuration are populated.
|
|
|
|
*
|
|
|
|
* It does *not* perform a robust check to see if the client is working.
|
|
|
|
* For that, you'll want to check Weave.Status.checkSetup().
|
|
|
|
*/
|
|
|
|
get enabled() {
|
|
|
|
let prefs = Services.prefs.getBranch(SYNC_PREFS_BRANCH);
|
|
|
|
return prefs.prefHasUserValue("username") &&
|
|
|
|
prefs.prefHasUserValue("clusterURL");
|
|
|
|
},
|
|
|
|
|
2013-01-30 07:05:12 -08:00
|
|
|
observe: function (subject, topic, data) {
|
2008-11-06 17:37:48 -08:00
|
|
|
switch (topic) {
|
|
|
|
case "app-startup":
|
2009-05-26 14:18:45 -07:00
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
2010-08-12 08:03:58 -07:00
|
|
|
getService(Ci.nsIObserverService);
|
2009-10-22 12:31:45 -07:00
|
|
|
os.addObserver(this, "final-ui-startup", true);
|
2008-11-06 17:37:48 -08:00
|
|
|
break;
|
2009-10-22 17:03:51 -07:00
|
|
|
|
2009-10-22 12:31:45 -07:00
|
|
|
case "final-ui-startup":
|
2009-12-15 14:21:12 -08:00
|
|
|
// Force Weave service to load if it hasn't triggered from overlays
|
|
|
|
this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this.timer.initWithCallback({
|
|
|
|
notify: function() {
|
2014-05-26 17:39:51 -07:00
|
|
|
let isConfigured = false;
|
2014-01-28 17:51:09 -08:00
|
|
|
// We only load more if it looks like Sync is configured.
|
|
|
|
let prefs = Services.prefs.getBranch(SYNC_PREFS_BRANCH);
|
2014-05-26 17:39:51 -07:00
|
|
|
if (prefs.prefHasUserValue("username")) {
|
|
|
|
// We have a username. So, do a more thorough check. This will
|
|
|
|
// import a number of modules and thus increase memory
|
|
|
|
// accordingly. We could potentially copy code performed by
|
|
|
|
// this check into this file if our above code is yielding too
|
|
|
|
// many false positives.
|
|
|
|
Components.utils.import("resource://services-sync/main.js");
|
|
|
|
isConfigured = Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED;
|
2014-01-28 17:51:09 -08:00
|
|
|
}
|
2014-05-26 17:39:51 -07:00
|
|
|
let getHistogramById = Services.telemetry.getHistogramById;
|
|
|
|
getHistogramById("WEAVE_CONFIGURED").add(isConfigured);
|
|
|
|
if (isConfigured) {
|
|
|
|
getHistogramById("WEAVE_CONFIGURED_MASTER_PASSWORD").add(Utils.mpEnabled());
|
2014-01-28 17:51:09 -08:00
|
|
|
this.ensureLoaded();
|
2013-12-18 21:38:35 -08:00
|
|
|
}
|
2013-02-06 10:32:00 -08:00
|
|
|
}.bind(this)
|
2013-12-18 21:38:35 -08:00
|
|
|
}, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
|
2013-12-16 20:45:03 -08:00
|
|
|
break;
|
2008-11-06 17:37:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-05-07 15:24:51 -07:00
|
|
|
function AboutWeaveLog() {}
|
|
|
|
AboutWeaveLog.prototype = {
|
|
|
|
classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
|
|
|
|
|
|
|
getURIFlags: function(aURI) {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
2015-02-17 19:54:12 -08:00
|
|
|
newChannel: function(aURI, aLoadInfo) {
|
2011-06-13 11:40:08 -07:00
|
|
|
let dir = FileUtils.getDir("ProfD", ["weave", "logs"], true);
|
|
|
|
let uri = Services.io.newFileURI(dir);
|
2015-02-17 19:54:12 -08:00
|
|
|
let channel = Services.io.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
|
|
|
|
|
2011-06-13 11:40:08 -07:00
|
|
|
channel.originalURI = aURI;
|
|
|
|
|
2012-08-14 12:37:04 -07:00
|
|
|
// Ensure that the about page has the same privileges as a regular directory
|
|
|
|
// view. That way links to files can be opened.
|
|
|
|
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
2015-08-25 11:05:43 -07:00
|
|
|
let principal = ssm.getNoAppCodebasePrincipal(uri);
|
2012-08-14 12:37:04 -07:00
|
|
|
channel.owner = principal;
|
2011-06-13 11:40:08 -07:00
|
|
|
return channel;
|
2010-05-07 15:24:51 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-13 11:40:08 -07:00
|
|
|
const components = [WeaveService, AboutWeaveLog];
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
|