|
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 function debug(s) { |
|
8 //dump("-*- AppsService.js: " + s + "\n"); |
|
9 } |
|
10 |
|
11 const Cc = Components.classes; |
|
12 const Ci = Components.interfaces; |
|
13 const Cu = Components.utils; |
|
14 |
|
15 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
16 Cu.import("resource://gre/modules/Services.jsm"); |
|
17 |
|
18 const APPS_SERVICE_CID = Components.ID("{05072afa-92fe-45bf-ae22-39b69c117058}"); |
|
19 |
|
20 function AppsService() |
|
21 { |
|
22 debug("AppsService Constructor"); |
|
23 let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) |
|
24 .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; |
|
25 debug("inParent: " + inParent); |
|
26 Cu.import(inParent ? "resource://gre/modules/Webapps.jsm" : |
|
27 "resource://gre/modules/AppsServiceChild.jsm"); |
|
28 } |
|
29 |
|
30 AppsService.prototype = { |
|
31 |
|
32 getCSPByLocalId: function getCSPByLocalId(localId) { |
|
33 debug("GetCSPByLocalId( " + localId + " )"); |
|
34 return DOMApplicationRegistry.getCSPByLocalId(localId); |
|
35 }, |
|
36 |
|
37 getAppByManifestURL: function getAppByManifestURL(aManifestURL) { |
|
38 debug("GetAppByManifestURL( " + aManifestURL + " )"); |
|
39 return DOMApplicationRegistry.getAppByManifestURL(aManifestURL); |
|
40 }, |
|
41 |
|
42 getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) { |
|
43 debug("getAppLocalIdByManifestURL( " + aManifestURL + " )"); |
|
44 return DOMApplicationRegistry.getAppLocalIdByManifestURL(aManifestURL); |
|
45 }, |
|
46 |
|
47 getAppLocalIdByStoreId: function getAppLocalIdByStoreId(aStoreId) { |
|
48 debug("getAppLocalIdByStoreId( " + aStoreId + " )"); |
|
49 return DOMApplicationRegistry.getAppLocalIdByStoreId(aStoreId); |
|
50 }, |
|
51 |
|
52 getAppByLocalId: function getAppByLocalId(aLocalId) { |
|
53 debug("getAppByLocalId( " + aLocalId + " )"); |
|
54 return DOMApplicationRegistry.getAppByLocalId(aLocalId); |
|
55 }, |
|
56 |
|
57 getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) { |
|
58 debug("getManifestURLByLocalId( " + aLocalId + " )"); |
|
59 return DOMApplicationRegistry.getManifestURLByLocalId(aLocalId); |
|
60 }, |
|
61 |
|
62 getCoreAppsBasePath: function getCoreAppsBasePath() { |
|
63 debug("getCoreAppsBasePath()"); |
|
64 return DOMApplicationRegistry.getCoreAppsBasePath(); |
|
65 }, |
|
66 |
|
67 getWebAppsBasePath: function getWebAppsBasePath() { |
|
68 debug("getWebAppsBasePath()"); |
|
69 return DOMApplicationRegistry.getWebAppsBasePath(); |
|
70 }, |
|
71 |
|
72 getAppInfo: function getAppInfo(aAppId) { |
|
73 debug("getAppInfo()"); |
|
74 return DOMApplicationRegistry.getAppInfo(aAppId); |
|
75 }, |
|
76 |
|
77 getRedirect: function getRedirect(aLocalId, aURI) { |
|
78 debug("getRedirect for " + aLocalId + " " + aURI.spec); |
|
79 if (aLocalId == Ci.nsIScriptSecurityManager.NO_APP_ID || |
|
80 aLocalId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) { |
|
81 return null; |
|
82 } |
|
83 |
|
84 let app = DOMApplicationRegistry.getAppByLocalId(aLocalId); |
|
85 if (app && app.redirects) { |
|
86 let spec = aURI.spec; |
|
87 for (let i = 0; i < app.redirects.length; i++) { |
|
88 let redirect = app.redirects[i]; |
|
89 if (spec.startsWith(redirect.from)) { |
|
90 // Prepend the app origin to the redirection. We need that since |
|
91 // the origin of packaged apps is a uuid created at install time. |
|
92 let to = app.origin + redirect.to; |
|
93 // If we have a ? or a # in the current URL, add this part to the |
|
94 // redirection. |
|
95 let index = -1; |
|
96 index = spec.indexOf('?'); |
|
97 if (index == -1) { |
|
98 index = spec.indexOf('#'); |
|
99 } |
|
100 |
|
101 if (index != -1) { |
|
102 to += spec.substring(index); |
|
103 } |
|
104 debug('App specific redirection from ' + spec + ' to ' + to); |
|
105 return Services.io.newURI(to, null, null); |
|
106 } |
|
107 } |
|
108 } |
|
109 // No matching redirect. |
|
110 return null; |
|
111 }, |
|
112 |
|
113 classID : APPS_SERVICE_CID, |
|
114 QueryInterface : XPCOMUtils.generateQI([Ci.nsIAppsService]) |
|
115 } |
|
116 |
|
117 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppsService]) |