Bumping manifests a=b2g-bump
[gecko.git] / dom / apps / AppsService.js
blob63483a58be638073b68a9572f8768b9b7b0cb19f
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 "use strict"
7 function debug(s) {
8   //dump("-*- AppsService.js: " + s + "\n");
11 const Cc = Components.classes;
12 const Ci = Components.interfaces;
13 const Cu = Components.utils;
15 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
16 Cu.import("resource://gre/modules/Services.jsm");
17 Cu.import("resource://gre/modules/Promise.jsm");
18 try {
19   if (Services.prefs.getBoolPref("dom.apps.customization.enabled")) {
20     Cu.import("resource://gre/modules/UserCustomizations.jsm");
21   }
22 } catch(e) {}
24 const APPS_SERVICE_CID = Components.ID("{05072afa-92fe-45bf-ae22-39b69c117058}");
26 function AppsService()
28   debug("AppsService Constructor");
29   this.inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
30                     .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
31   debug("inParent: " + this.inParent);
32   Cu.import(this.inParent ? "resource://gre/modules/Webapps.jsm" :
33                             "resource://gre/modules/AppsServiceChild.jsm");
36 AppsService.prototype = {
38   getManifestCSPByLocalId: function getCSPByLocalId(localId) {
39     debug("GetManifestCSPByLocalId( " + localId + " )");
40     return DOMApplicationRegistry.getManifestCSPByLocalId(localId);
41   },
43   getDefaultCSPByLocalId: function getCSPByLocalId(localId) {
44     debug("GetDefaultCSPByLocalId( " + localId + " )");
45     return DOMApplicationRegistry.getDefaultCSPByLocalId(localId);
46   },
48   getAppByManifestURL: function getAppByManifestURL(aManifestURL) {
49     debug("GetAppByManifestURL( " + aManifestURL + " )");
50     return DOMApplicationRegistry.getAppByManifestURL(aManifestURL);
51   },
53   getManifestFor: function getManifestFor(aManifestURL) {
54     debug("getManifestFor(" + aManifestURL + ")");
55     if (this.inParent) {
56       return DOMApplicationRegistry.getManifestFor(aManifestURL);
57     } else {
58       return Promise.reject(
59         new Error("Calling getManifestFor() from child is not supported"));
60     }
61   },
63   getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) {
64     debug("getAppLocalIdByManifestURL( " + aManifestURL + " )");
65     return DOMApplicationRegistry.getAppLocalIdByManifestURL(aManifestURL);
66   },
68   getAppLocalIdByStoreId: function getAppLocalIdByStoreId(aStoreId) {
69     debug("getAppLocalIdByStoreId( " + aStoreId + " )");
70     return DOMApplicationRegistry.getAppLocalIdByStoreId(aStoreId);
71   },
73   getAppByLocalId: function getAppByLocalId(aLocalId) {
74     debug("getAppByLocalId( " + aLocalId + " )");
75     return DOMApplicationRegistry.getAppByLocalId(aLocalId);
76   },
78   getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
79     debug("getManifestURLByLocalId( " + aLocalId + " )");
80     return DOMApplicationRegistry.getManifestURLByLocalId(aLocalId);
81   },
83   getCoreAppsBasePath: function getCoreAppsBasePath() {
84     debug("getCoreAppsBasePath()");
85     return DOMApplicationRegistry.getCoreAppsBasePath();
86   },
88   getWebAppsBasePath: function getWebAppsBasePath() {
89     debug("getWebAppsBasePath()");
90     return DOMApplicationRegistry.getWebAppsBasePath();
91   },
93   getAppInfo: function getAppInfo(aAppId) {
94     debug("getAppInfo()");
95     return DOMApplicationRegistry.getAppInfo(aAppId);
96   },
98   getRedirect: function getRedirect(aLocalId, aURI) {
99     debug("getRedirect for " + aLocalId + " " + aURI.spec);
100     if (aLocalId == Ci.nsIScriptSecurityManager.NO_APP_ID ||
101         aLocalId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) {
102       return null;
103     }
105     let app = DOMApplicationRegistry.getAppByLocalId(aLocalId);
106     if (app && app.redirects) {
107       let spec = aURI.spec;
108       for (let i = 0; i < app.redirects.length; i++) {
109         let redirect = app.redirects[i];
110         if (spec.startsWith(redirect.from)) {
111           // Prepend the app origin to the redirection. We need that since
112           // the origin of packaged apps is a uuid created at install time.
113           let to = app.origin + redirect.to;
114           // If we have a ? or a # in the current URL, add this part to the
115           // redirection.
116           let index = -1;
117           index = spec.indexOf('?');
118           if (index == -1) {
119             index = spec.indexOf('#');
120           }
122           if (index != -1) {
123             to += spec.substring(index);
124           }
125           debug('App specific redirection from ' + spec + ' to ' + to);
126           return Services.io.newURI(to, null, null);
127         }
128       }
129     }
130     // No matching redirect.
131     return null;
132   },
134   classID : APPS_SERVICE_CID,
135   QueryInterface : XPCOMUtils.generateQI([Ci.nsIAppsService])
138 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppsService])