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/. */
|
2008-11-07 04:37:48 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"FileUtils",
|
|
|
|
"resource://gre/modules/FileUtils.jsm"
|
|
|
|
);
|
2019-07-05 11:58:22 +03:00
|
|
|
|
2018-01-03 23:22:43 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
this,
|
|
|
|
"syncUsername",
|
|
|
|
"services.sync.username"
|
|
|
|
);
|
2013-01-30 19:05:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05 09:09:54 +04:00
|
|
|
* should use the promise-base function whenLoaded() - something like the
|
|
|
|
* following:
|
2013-01-30 19:05:12 +04: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-05 09:09:54 +04: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 19:05:12 +04: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 18:28:10 +04:00
|
|
|
function WeaveService() {
|
|
|
|
this.wrappedJSObject = this;
|
2013-01-30 19:05:12 +04:00
|
|
|
this.ready = false;
|
2010-06-23 18:28:10 +04:00
|
|
|
}
|
2008-11-07 04:37:48 +03:00
|
|
|
WeaveService.prototype = {
|
|
|
|
classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
|
|
|
|
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
]),
|
2008-11-07 04:37:48 +03:00
|
|
|
|
2019-09-25 09:51:18 +03:00
|
|
|
get Weave() {
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
|
2019-09-25 09:51:18 +03:00
|
|
|
return Weave;
|
|
|
|
},
|
2013-01-30 19:05:12 +04:00
|
|
|
|
2019-09-25 09:51:18 +03:00
|
|
|
ensureLoaded() {
|
2013-01-30 19:05:12 +04:00
|
|
|
// Side-effect of accessing the service is that it is instantiated.
|
2019-09-25 09:51:18 +03:00
|
|
|
this.Weave.Service;
|
2013-01-30 19:05:12 +04:00
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
whenLoaded() {
|
2014-02-05 09:09:54 +04:00
|
|
|
if (this.ready) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-05-03 01:27:49 +03:00
|
|
|
let onReadyPromise = new Promise(resolve => {
|
|
|
|
Services.obs.addObserver(function onReady() {
|
|
|
|
Services.obs.removeObserver(onReady, "weave:service:ready");
|
|
|
|
resolve();
|
|
|
|
}, "weave:service:ready");
|
|
|
|
});
|
2014-02-05 09:09:54 +04:00
|
|
|
this.ensureLoaded();
|
2017-05-03 01:27:49 +03:00
|
|
|
return onReadyPromise;
|
2014-02-05 09:09:54 +04:00
|
|
|
},
|
|
|
|
|
2017-05-13 00:01:25 +03:00
|
|
|
init() {
|
|
|
|
// 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: () => {
|
|
|
|
let isConfigured = false;
|
|
|
|
// We only load more if it looks like Sync is configured.
|
|
|
|
if (this.enabled) {
|
|
|
|
// We have an associated FxAccount. 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.
|
2019-01-17 21:18:31 +03:00
|
|
|
var { Weave } = ChromeUtils.import(
|
|
|
|
"resource://services-sync/main.js"
|
|
|
|
);
|
2017-05-13 00:01:25 +03:00
|
|
|
isConfigured =
|
|
|
|
Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED;
|
|
|
|
}
|
|
|
|
let getHistogramById = Services.telemetry.getHistogramById;
|
|
|
|
getHistogramById("WEAVE_CONFIGURED").add(isConfigured);
|
|
|
|
if (isConfigured) {
|
|
|
|
this.ensureLoaded();
|
|
|
|
}
|
2019-07-05 11:58:22 +03:00
|
|
|
},
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2017-05-13 00:01:25 +03:00
|
|
|
10000,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2014-02-05 20:08:14 +04:00
|
|
|
/**
|
|
|
|
* Whether Sync appears to be enabled.
|
|
|
|
*
|
2018-02-15 06:24:44 +03:00
|
|
|
* This returns true if we have an associated FxA account and Sync is enabled.
|
2014-02-05 20:08:14 +04:00
|
|
|
*
|
|
|
|
* 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() {
|
2018-02-15 06:24:44 +03:00
|
|
|
return (
|
|
|
|
!!syncUsername &&
|
|
|
|
Services.prefs.getBoolPref("identity.fxaccounts.enabled")
|
|
|
|
);
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2008-11-07 04:37:48 +03:00
|
|
|
};
|
|
|
|
|
2010-05-08 02:24:51 +04:00
|
|
|
function AboutWeaveLog() {}
|
|
|
|
AboutWeaveLog.prototype = {
|
|
|
|
classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
|
|
|
|
|
2018-04-23 06:55:06 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
|
|
|
Ci.nsIAboutModule,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
]),
|
2010-05-08 02:24:51 +04:00
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
getURIFlags(aURI) {
|
2010-05-08 02:24:51 +04:00
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
2017-01-10 20:09:02 +03:00
|
|
|
newChannel(aURI, aLoadInfo) {
|
2011-06-13 22:40:08 +04:00
|
|
|
let dir = FileUtils.getDir("ProfD", ["weave", "logs"], true);
|
|
|
|
let uri = Services.io.newFileURI(dir);
|
2015-02-18 06:54:12 +03:00
|
|
|
let channel = Services.io.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
|
|
|
|
|
2011-06-13 22:40:08 +04:00
|
|
|
channel.originalURI = aURI;
|
|
|
|
|
2012-08-14 23:37:04 +04:00
|
|
|
// Ensure that the about page has the same privileges as a regular directory
|
2016-03-21 23:44:00 +03:00
|
|
|
// view. That way links to files can be opened. make sure we use the correct
|
|
|
|
// origin attributes when creating the principal for accessing the
|
|
|
|
// about:sync-log data.
|
2019-07-08 19:37:45 +03:00
|
|
|
let principal = Services.scriptSecurityManager.createContentPrincipal(
|
2017-11-24 17:46:33 +03:00
|
|
|
uri,
|
|
|
|
aLoadInfo.originAttributes
|
|
|
|
);
|
2015-08-18 10:01:42 +03:00
|
|
|
|
2012-08-14 23:37:04 +04:00
|
|
|
channel.owner = principal;
|
2011-06-13 22:40:08 +04:00
|
|
|
return channel;
|
2018-08-31 08:59:17 +03:00
|
|
|
},
|
2010-05-08 02:24:51 +04:00
|
|
|
};
|
|
|
|
|
2019-01-30 07:44:18 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["WeaveService", "AboutWeaveLog"];
|