Bug 1104435 part 2 - Make AnimationPlayer derive from nsISupports; r=smaug
[gecko.git] / webapprt / WebappRT.jsm
blobd4c5f186f9046c60a3fd3e55b2b5cb3dd3797f0f
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 file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 this.EXPORTED_SYMBOLS = ["WebappRT"];
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
12 Cu.import("resource://gre/modules/Services.jsm");
13 Cu.import("resource://gre/modules/Promise.jsm");
14 Cu.import("resource://gre/modules/AppsUtils.jsm");
16 XPCOMUtils.defineLazyModuleGetter(this, "OS",
17   "resource://gre/modules/osfile.jsm");
19 XPCOMUtils.defineLazyModuleGetter(this, "Task",
20   "resource://gre/modules/Task.jsm");
22 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
23   'resource://gre/modules/NativeApp.jsm');
25 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
26                                   "@mozilla.org/AppsService;1",
27                                   "nsIAppsService");
29 this.WebappRT = {
30   _configPromise: null,
32   get configPromise() {
33     if (!this._configPromise) {
34       this._configPromise = Task.spawn(function*() {
35         let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
36                                       "webapp.json");
38         WebappRT.config = yield AppsUtils.loadJSONAsync(webappJson);
39       });
40     }
42     return this._configPromise;
43   },
45   get launchURI() {
46     return this.localeManifest.fullLaunchPath();
47   },
49   get localeManifest() {
50     return new ManifestHelper(this.config.app.manifest,
51                               this.config.app.origin,
52                               this.config.app.manifestURL);
53   },
55   get appID() {
56     let manifestURL = this.config.app.manifestURL;
57     if (!manifestURL) {
58       return Ci.nsIScriptSecurityManager.NO_APP_ID;
59     }
61     return appsService.getAppLocalIdByManifestURL(manifestURL);
62   },
64   isUpdatePending: Task.async(function*() {
65     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
66                                   "update", "webapp.json");
68     if (!(yield OS.File.exists(webappJson))) {
69       return false;
70     }
72     return true;
73   }),
75   applyUpdate: Task.async(function*() {
76     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
77                                   "update", "webapp.json");
78     let config = yield AppsUtils.loadJSONAsync(webappJson);
80     let nativeApp = new NativeApp(config.app, config.app.manifest,
81                                   config.app.categories,
82                                   config.registryDir);
83     try {
84       yield nativeApp.applyUpdate(config.app);
85     } catch (ex) {
86       return false;
87     }
89     // The update has been applied successfully, the new config file
90     // is the config file that was in the update directory.
91     this.config = config;
92     this._configPromise = Promise.resolve();
94     return true;
95   }),
97   startUpdateService: function() {
98     let manifestURL = this.config.app.manifestURL;
99     // We used to install apps without storing their manifest URL.
100     // Now we can't update them.
101     if (!manifestURL) {
102       return;
103     }
105     // Check for updates once a day.
106     let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
107                        getService(Ci.nsIUpdateTimerManager);
108     timerManager.registerTimer("updateTimer", () => {
109       let window = Services.wm.getMostRecentWindow("webapprt:webapp");
110       window.navigator.mozApps.mgmt.getAll().onsuccess = function() {
111         let thisApp = null;
112         for (let app of this.result) {
113           if (app.manifestURL == manifestURL) {
114             thisApp = app;
115             break;
116           }
117         }
119         // This shouldn't happen if the app is installed.
120         if (!thisApp) {
121           Cu.reportError("Couldn't find the app in the webapps registry");
122           return;
123         }
125         thisApp.ondownloadavailable = () => {
126           // Download available, download it!
127           thisApp.download();
128         };
130         thisApp.ondownloadsuccess = () => {
131           // Update downloaded, apply it!
132           window.navigator.mozApps.mgmt.applyDownload(thisApp);
133         };
135         thisApp.ondownloadapplied = () => {
136           // Application updated, nothing to do.
137         };
139         thisApp.checkForUpdate();
140       }
141     }, Services.prefs.getIntPref("webapprt.app_update_interval"));
142   },