1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const { XPCOMUtils } = ChromeUtils.import(
6 "resource://gre/modules/XPCOMUtils.jsm"
8 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
9 ChromeUtils.defineModuleGetter(
12 "resource://gre/modules/FileUtils.jsm"
15 XPCOMUtils.defineLazyPreferenceGetter(
18 "services.sync.username"
22 * Sync's XPCOM service.
24 * It is named "Weave" for historical reasons.
26 * It's worth noting how Sync is lazily loaded. We register a timer that
27 * loads Sync a few seconds after app startup. This is so Sync does not
28 * adversely affect application start time.
30 * If Sync is not configured, no extra Sync code is loaded. If an
31 * external component (say the UI) needs to interact with Sync, it
32 * should use the promise-base function whenLoaded() - something like the
35 * // 1. Grab a handle to the Sync XPCOM service.
36 * let service = Cc["@mozilla.org/weave/service;1"]
37 * .getService(Components.interfaces.nsISupports)
40 * // 2. Use the .then method of the promise.
41 * service.whenLoaded().then(() => {
42 * // You are free to interact with "Weave." objects.
46 * And that's it! However, if you really want to avoid promises and do it
49 * // 1. Get a reference to the service as done in (1) above.
51 * // 2. Check if the service has been initialized.
52 * if (service.ready) {
53 * // You are free to interact with "Weave." objects.
57 * // 3. Install "ready" listener.
58 * Services.obs.addObserver(function onReady() {
59 * Services.obs.removeObserver(onReady, "weave:service:ready");
61 * // You are free to interact with "Weave." objects.
62 * }, "weave:service:ready", false);
64 * // 4. Trigger loading of Sync.
65 * service.ensureLoaded();
67 function WeaveService() {
68 this.wrappedJSObject = this;
71 WeaveService.prototype = {
72 classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
74 QueryInterface: ChromeUtils.generateQI([
76 "nsISupportsWeakReference",
80 const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
85 // Side-effect of accessing the service is that it is instantiated.
91 return Promise.resolve();
93 let onReadyPromise = new Promise(resolve => {
94 Services.obs.addObserver(function onReady() {
95 Services.obs.removeObserver(onReady, "weave:service:ready");
97 }, "weave:service:ready");
100 return onReadyPromise;
104 // Force Weave service to load if it hasn't triggered from overlays
105 this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
106 this.timer.initWithCallback(
109 let isConfigured = false;
110 // We only load more if it looks like Sync is configured.
112 // We have an associated FxAccount. So, do a more thorough check.
113 // This will import a number of modules and thus increase memory
114 // accordingly. We could potentially copy code performed by
115 // this check into this file if our above code is yielding too
116 // many false positives.
117 var { Weave } = ChromeUtils.import(
118 "resource://services-sync/main.js"
121 Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED;
129 Ci.nsITimer.TYPE_ONE_SHOT
134 * Whether Sync appears to be enabled.
136 * This returns true if we have an associated FxA account and Sync is enabled.
138 * It does *not* perform a robust check to see if the client is working.
139 * For that, you'll want to check Weave.Status.checkSetup().
144 Services.prefs.getBoolPref("identity.fxaccounts.enabled")
149 function AboutWeaveLog() {}
150 AboutWeaveLog.prototype = {
151 classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
153 QueryInterface: ChromeUtils.generateQI([
155 "nsISupportsWeakReference",
162 newChannel(aURI, aLoadInfo) {
163 let dir = FileUtils.getDir("ProfD", ["weave", "logs"], true);
164 let uri = Services.io.newFileURI(dir);
165 let channel = Services.io.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
167 channel.originalURI = aURI;
169 // Ensure that the about page has the same privileges as a regular directory
170 // view. That way links to files can be opened. make sure we use the correct
171 // origin attributes when creating the principal for accessing the
172 // about:sync-log data.
173 let principal = Services.scriptSecurityManager.createContentPrincipal(
175 aLoadInfo.originAttributes
178 channel.owner = principal;
183 var EXPORTED_SYMBOLS = ["WeaveService", "AboutWeaveLog"];