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 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
8 ChromeUtils.defineESModuleGetters(lazy, {
9 CLIENT_NOT_CONFIGURED: "resource://services-sync/constants.sys.mjs",
10 FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
13 XPCOMUtils.defineLazyPreferenceGetter(
16 "services.sync.username"
20 * Sync's XPCOM service.
22 * It is named "Weave" for historical reasons.
24 * It's worth noting how Sync is lazily loaded. We register a timer that
25 * loads Sync a few seconds after app startup. This is so Sync does not
26 * adversely affect application start time.
28 * If Sync is not configured, no extra Sync code is loaded. If an
29 * external component (say the UI) needs to interact with Sync, it
30 * should use the promise-base function whenLoaded() - something like the
33 * // 1. Grab a handle to the Sync XPCOM service.
34 * let service = Cc["@mozilla.org/weave/service;1"]
35 * .getService(Components.interfaces.nsISupports)
38 * // 2. Use the .then method of the promise.
39 * service.whenLoaded().then(() => {
40 * // You are free to interact with "Weave." objects.
44 * And that's it! However, if you really want to avoid promises and do it
47 * // 1. Get a reference to the service as done in (1) above.
49 * // 2. Check if the service has been initialized.
50 * if (service.ready) {
51 * // You are free to interact with "Weave." objects.
55 * // 3. Install "ready" listener.
56 * Services.obs.addObserver(function onReady() {
57 * Services.obs.removeObserver(onReady, "weave:service:ready");
59 * // You are free to interact with "Weave." objects.
60 * }, "weave:service:ready", false);
62 * // 4. Trigger loading of Sync.
63 * service.ensureLoaded();
65 export function WeaveService() {
66 this.wrappedJSObject = this;
70 WeaveService.prototype = {
71 classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
73 QueryInterface: ChromeUtils.generateQI([
75 "nsISupportsWeakReference",
79 const { Weave } = ChromeUtils.importESModule(
80 "resource://services-sync/main.sys.mjs"
86 // Side-effect of accessing the service is that it is instantiated.
92 return Promise.resolve();
94 let onReadyPromise = new Promise(resolve => {
95 Services.obs.addObserver(function onReady() {
96 Services.obs.removeObserver(onReady, "weave:service:ready");
98 }, "weave:service:ready");
101 return onReadyPromise;
105 // Force Weave service to load if it hasn't triggered from overlays
106 this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
107 this.timer.initWithCallback(
110 let isConfigured = false;
111 // We only load more if it looks like Sync is configured.
113 // We have an associated FxAccount. So, do a more thorough check.
114 // This will import a number of modules and thus increase memory
115 // accordingly. We could potentially copy code performed by
116 // this check into this file if our above code is yielding too
117 // many false positives.
118 var { Weave } = ChromeUtils.importESModule(
119 "resource://services-sync/main.sys.mjs"
122 Weave.Status.checkSetup() != lazy.CLIENT_NOT_CONFIGURED;
130 Ci.nsITimer.TYPE_ONE_SHOT
135 * Whether Sync appears to be enabled.
137 * This returns true if we have an associated FxA account and Sync is enabled.
139 * It does *not* perform a robust check to see if the client is working.
140 * For that, you'll want to check Weave.Status.checkSetup().
144 !!lazy.syncUsername &&
145 Services.prefs.getBoolPref("identity.fxaccounts.enabled")
150 export function AboutWeaveLog() {}
151 AboutWeaveLog.prototype = {
152 classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
154 QueryInterface: ChromeUtils.generateQI([
156 "nsISupportsWeakReference",
163 newChannel(aURI, aLoadInfo) {
164 let dir = lazy.FileUtils.getDir("ProfD", ["weave", "logs"]);
166 dir.create(Ci.nsIFile.DIRECTORY_TYPE, lazy.FileUtils.PERMS_DIRECTORY);
168 if (ex.result != Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
171 // Ignore the exception due to a directory that already exists.
173 let uri = Services.io.newFileURI(dir);
174 let channel = Services.io.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
176 channel.originalURI = aURI;
178 // Ensure that the about page has the same privileges as a regular directory
179 // view. That way links to files can be opened. make sure we use the correct
180 // origin attributes when creating the principal for accessing the
181 // about:sync-log data.
182 let principal = Services.scriptSecurityManager.createContentPrincipal(
184 aLoadInfo.originAttributes
187 channel.owner = principal;