Bug 1734063 [wpt PR 31107] - [GridNG] Fix rounding of distributed free space to flexi...
[gecko.git] / services / sync / Weave.jsm
blob3cf28e6a4b26bc0b2d23102a924872ee196ce42c
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"
7 );
8 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
9 ChromeUtils.defineModuleGetter(
10   this,
11   "FileUtils",
12   "resource://gre/modules/FileUtils.jsm"
15 XPCOMUtils.defineLazyPreferenceGetter(
16   this,
17   "syncUsername",
18   "services.sync.username"
21 /**
22  * Sync's XPCOM service.
23  *
24  * It is named "Weave" for historical reasons.
25  *
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.
29  *
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
33  * following:
34  *
35  * // 1. Grab a handle to the Sync XPCOM service.
36  * let service = Cc["@mozilla.org/weave/service;1"]
37  *                 .getService(Components.interfaces.nsISupports)
38  *                 .wrappedJSObject;
39  *
40  * // 2. Use the .then method of the promise.
41  * service.whenLoaded().then(() => {
42  *   // You are free to interact with "Weave." objects.
43  *   return;
44  * });
45  *
46  * And that's it!  However, if you really want to avoid promises and do it
47  * old-school, then
48  *
49  * // 1. Get a reference to the service as done in (1) above.
50  *
51  * // 2. Check if the service has been initialized.
52  * if (service.ready) {
53  *   // You are free to interact with "Weave." objects.
54  *   return;
55  * }
56  *
57  * // 3. Install "ready" listener.
58  * Services.obs.addObserver(function onReady() {
59  *   Services.obs.removeObserver(onReady, "weave:service:ready");
60  *
61  *   // You are free to interact with "Weave." objects.
62  * }, "weave:service:ready", false);
63  *
64  * // 4. Trigger loading of Sync.
65  * service.ensureLoaded();
66  */
67 function WeaveService() {
68   this.wrappedJSObject = this;
69   this.ready = false;
71 WeaveService.prototype = {
72   classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
74   QueryInterface: ChromeUtils.generateQI([
75     "nsIObserver",
76     "nsISupportsWeakReference",
77   ]),
79   get Weave() {
80     const { Weave } = ChromeUtils.import("resource://services-sync/main.js");
81     return Weave;
82   },
84   ensureLoaded() {
85     // Side-effect of accessing the service is that it is instantiated.
86     this.Weave.Service;
87   },
89   whenLoaded() {
90     if (this.ready) {
91       return Promise.resolve();
92     }
93     let onReadyPromise = new Promise(resolve => {
94       Services.obs.addObserver(function onReady() {
95         Services.obs.removeObserver(onReady, "weave:service:ready");
96         resolve();
97       }, "weave:service:ready");
98     });
99     this.ensureLoaded();
100     return onReadyPromise;
101   },
103   init() {
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(
107       {
108         notify: () => {
109           let isConfigured = false;
110           // We only load more if it looks like Sync is configured.
111           if (this.enabled) {
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"
119             );
120             isConfigured =
121               Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED;
122           }
123           if (isConfigured) {
124             this.ensureLoaded();
125           }
126         },
127       },
128       10000,
129       Ci.nsITimer.TYPE_ONE_SHOT
130     );
131   },
133   /**
134    * Whether Sync appears to be enabled.
135    *
136    * This returns true if we have an associated FxA account and Sync is enabled.
137    *
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().
140    */
141   get enabled() {
142     return (
143       !!syncUsername &&
144       Services.prefs.getBoolPref("identity.fxaccounts.enabled")
145     );
146   },
149 function AboutWeaveLog() {}
150 AboutWeaveLog.prototype = {
151   classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
153   QueryInterface: ChromeUtils.generateQI([
154     "nsIAboutModule",
155     "nsISupportsWeakReference",
156   ]),
158   getURIFlags(aURI) {
159     return 0;
160   },
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(
174       uri,
175       aLoadInfo.originAttributes
176     );
178     channel.owner = principal;
179     return channel;
180   },
183 var EXPORTED_SYMBOLS = ["WeaveService", "AboutWeaveLog"];