|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 const Cu = Components.utils; |
|
8 const Cc = Components.classes; |
|
9 const Ci = Components.interfaces; |
|
10 |
|
11 // This module exposes a subset of the functionnalities of the parent DOM |
|
12 // Registry to content processes, to be be used from the AppsService component. |
|
13 |
|
14 this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"]; |
|
15 |
|
16 Cu.import("resource://gre/modules/AppsUtils.jsm"); |
|
17 Cu.import("resource://gre/modules/Services.jsm"); |
|
18 |
|
19 function debug(s) { |
|
20 //dump("-*- AppsServiceChild.jsm: " + s + "\n"); |
|
21 } |
|
22 |
|
23 this.DOMApplicationRegistry = { |
|
24 init: function init() { |
|
25 debug("init"); |
|
26 this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] |
|
27 .getService(Ci.nsISyncMessageSender); |
|
28 |
|
29 ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) { |
|
30 this.cpmm.addMessageListener(aMsgName, this); |
|
31 }).bind(this)); |
|
32 |
|
33 // We need to prime the cache with the list of apps. |
|
34 // XXX shoud we do this async and block callers if it's not yet there? |
|
35 this.webapps = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0]; |
|
36 |
|
37 // We need a fast mapping from localId -> app, so we add an index. |
|
38 this.localIdIndex = { }; |
|
39 for (let id in this.webapps) { |
|
40 let app = this.webapps[id]; |
|
41 this.localIdIndex[app.localId] = app; |
|
42 } |
|
43 |
|
44 Services.obs.addObserver(this, "xpcom-shutdown", false); |
|
45 }, |
|
46 |
|
47 observe: function(aSubject, aTopic, aData) { |
|
48 // cpmm.addMessageListener causes the DOMApplicationRegistry object to live |
|
49 // forever if we don't clean up properly. |
|
50 this.webapps = null; |
|
51 ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) { |
|
52 this.cpmm.removeMessageListener(aMsgName, this); |
|
53 }).bind(this)); |
|
54 }, |
|
55 |
|
56 receiveMessage: function receiveMessage(aMessage) { |
|
57 debug("Received " + aMessage.name + " message."); |
|
58 let msg = aMessage.json; |
|
59 switch (aMessage.name) { |
|
60 case "Webapps:AddApp": |
|
61 this.webapps[msg.id] = msg.app; |
|
62 this.localIdIndex[msg.app.localId] = msg.app; |
|
63 break; |
|
64 case "Webapps:RemoveApp": |
|
65 delete this.localIdIndex[this.webapps[msg.id].localId]; |
|
66 delete this.webapps[msg.id]; |
|
67 break; |
|
68 } |
|
69 }, |
|
70 |
|
71 getAppByManifestURL: function getAppByManifestURL(aManifestURL) { |
|
72 debug("getAppByManifestURL " + aManifestURL); |
|
73 return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL); |
|
74 }, |
|
75 |
|
76 getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) { |
|
77 debug("getAppLocalIdByManifestURL " + aManifestURL); |
|
78 return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL); |
|
79 }, |
|
80 |
|
81 getCSPByLocalId: function(aLocalId) { |
|
82 debug("getCSPByLocalId:" + aLocalId); |
|
83 return AppsUtils.getCSPByLocalId(this.webapps, aLocalId); |
|
84 }, |
|
85 |
|
86 getAppLocalIdByStoreId: function(aStoreId) { |
|
87 debug("getAppLocalIdByStoreId:" + aStoreId); |
|
88 return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId); |
|
89 }, |
|
90 |
|
91 getAppByLocalId: function getAppByLocalId(aLocalId) { |
|
92 debug("getAppByLocalId " + aLocalId); |
|
93 let app = this.localIdIndex[aLocalId]; |
|
94 if (!app) { |
|
95 debug("Ouch, No app!"); |
|
96 return null; |
|
97 } |
|
98 |
|
99 return new mozIApplication(app); |
|
100 }, |
|
101 |
|
102 getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) { |
|
103 debug("getManifestURLByLocalId " + aLocalId); |
|
104 return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId); |
|
105 }, |
|
106 |
|
107 getCoreAppsBasePath: function getCoreAppsBasePath() { |
|
108 debug("getCoreAppsBasePath() not yet supported on child!"); |
|
109 return null; |
|
110 }, |
|
111 |
|
112 getWebAppsBasePath: function getWebAppsBasePath() { |
|
113 debug("getWebAppsBasePath() not yet supported on child!"); |
|
114 return null; |
|
115 }, |
|
116 |
|
117 getAppInfo: function getAppInfo(aAppId) { |
|
118 return AppsUtils.getAppInfo(this.webapps, aAppId); |
|
119 } |
|
120 } |
|
121 |
|
122 DOMApplicationRegistry.init(); |