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 Cc = Components.classes;
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
10 Cu.import("resource://gre/modules/Services.jsm");
11 Cu.import("resource://gre/modules/FileUtils.jsm");
13 const SYNC_PREFS_BRANCH = "services.sync.";
17 * Sync's XPCOM service.
19 * It is named "Weave" for historical reasons.
21 * It's worth noting how Sync is lazily loaded. We register a timer that
22 * loads Sync a few seconds after app startup. This is so Sync does not
23 * adversely affect application start time.
25 * If Sync is not configured, no extra Sync code is loaded. If an
26 * external component (say the UI) needs to interact with Sync, it
27 * should do something like the following:
29 * // 1. Grab a handle to the Sync XPCOM service.
30 * let service = Cc["@mozilla.org/weave/service;1"]
31 * .getService(Components.interfaces.nsISupports)
34 * // 2. Check if the service has been initialized.
35 * if (service.ready) {
36 * // You are free to interact with "Weave." objects.
40 * // 3. Install "ready" listener.
41 * Services.obs.addObserver(function onReady() {
42 * Services.obs.removeObserver(onReady, "weave:service:ready");
44 * // You are free to interact with "Weave." objects.
45 * }, "weave:service:ready", false);
47 * // 4. Trigger loading of Sync.
48 * service.ensureLoaded();
50 function WeaveService() {
51 this.wrappedJSObject = this;
54 WeaveService.prototype = {
55 classID: Components.ID("{74b89fb0-f200-4ae8-a3ec-dd164117f6de}"),
57 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
58 Ci.nsISupportsWeakReference]),
60 ensureLoaded: function () {
61 Components.utils.import("resource://services-sync/main.js");
63 // Side-effect of accessing the service is that it is instantiated.
67 observe: function (subject, topic, data) {
70 let os = Cc["@mozilla.org/observer-service;1"].
71 getService(Ci.nsIObserverService);
72 os.addObserver(this, "final-ui-startup", true);
75 case "final-ui-startup":
76 // Force Weave service to load if it hasn't triggered from overlays
77 this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
78 this.timer.initWithCallback({
80 // We only load more if it looks like Sync is configured.
81 let prefs = Services.prefs.getBranch(SYNC_PREFS_BRANCH);
83 if (!prefs.prefHasUserValue("username")) {
87 // We have a username. So, do a more thorough check. This will
88 // import a number of modules and thus increase memory
89 // accordingly. We could potentially copy code performed by
90 // this check into this file if our above code is yielding too
91 // many false positives.
92 Components.utils.import("resource://services-sync/main.js");
93 if (Weave.Status.checkSetup() != Weave.CLIENT_NOT_CONFIGURED) {
97 }, 10000, Ci.nsITimer.TYPE_ONE_SHOT);
103 function AboutWeaveLog() {}
104 AboutWeaveLog.prototype = {
105 classID: Components.ID("{d28f8a0b-95da-48f4-b712-caf37097be41}"),
107 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule,
108 Ci.nsISupportsWeakReference]),
110 getURIFlags: function(aURI) {
114 newChannel: function(aURI) {
115 let dir = FileUtils.getDir("ProfD", ["weave", "logs"], true);
116 let uri = Services.io.newFileURI(dir);
117 let channel = Services.io.newChannelFromURI(uri);
118 channel.originalURI = aURI;
120 // Ensure that the about page has the same privileges as a regular directory
121 // view. That way links to files can be opened.
122 let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
123 .getService(Ci.nsIScriptSecurityManager);
124 let principal = ssm.getNoAppCodebasePrincipal(uri);
125 channel.owner = principal;
130 const components = [WeaveService, AboutWeaveLog];
131 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);