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/. */
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");
19 if (Services.prefs.getBoolPref("dom.apps.customization.enabled")) {
20 Cu.import("resource://gre/modules/UserCustomizations.jsm");
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);
43 getDefaultCSPByLocalId: function getCSPByLocalId(localId) {
44 debug("GetDefaultCSPByLocalId( " + localId + " )");
45 return DOMApplicationRegistry.getDefaultCSPByLocalId(localId);
48 getAppByManifestURL: function getAppByManifestURL(aManifestURL) {
49 debug("GetAppByManifestURL( " + aManifestURL + " )");
50 return DOMApplicationRegistry.getAppByManifestURL(aManifestURL);
53 getManifestFor: function getManifestFor(aManifestURL) {
54 debug("getManifestFor(" + aManifestURL + ")");
56 return DOMApplicationRegistry.getManifestFor(aManifestURL);
58 return Promise.reject(
59 new Error("Calling getManifestFor() from child is not supported"));
63 getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) {
64 debug("getAppLocalIdByManifestURL( " + aManifestURL + " )");
65 return DOMApplicationRegistry.getAppLocalIdByManifestURL(aManifestURL);
68 getAppLocalIdByStoreId: function getAppLocalIdByStoreId(aStoreId) {
69 debug("getAppLocalIdByStoreId( " + aStoreId + " )");
70 return DOMApplicationRegistry.getAppLocalIdByStoreId(aStoreId);
73 getAppByLocalId: function getAppByLocalId(aLocalId) {
74 debug("getAppByLocalId( " + aLocalId + " )");
75 return DOMApplicationRegistry.getAppByLocalId(aLocalId);
78 getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
79 debug("getManifestURLByLocalId( " + aLocalId + " )");
80 return DOMApplicationRegistry.getManifestURLByLocalId(aLocalId);
83 getCoreAppsBasePath: function getCoreAppsBasePath() {
84 debug("getCoreAppsBasePath()");
85 return DOMApplicationRegistry.getCoreAppsBasePath();
88 getWebAppsBasePath: function getWebAppsBasePath() {
89 debug("getWebAppsBasePath()");
90 return DOMApplicationRegistry.getWebAppsBasePath();
93 getAppInfo: function getAppInfo(aAppId) {
94 debug("getAppInfo()");
95 return DOMApplicationRegistry.getAppInfo(aAppId);
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) {
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
117 index = spec.indexOf('?');
119 index = spec.indexOf('#');
123 to += spec.substring(index);
125 debug('App specific redirection from ' + spec + ' to ' + to);
126 return Services.io.newURI(to, null, null);
130 // No matching redirect.
134 classID : APPS_SERVICE_CID,
135 QueryInterface : XPCOMUtils.generateQI([Ci.nsIAppsService])
138 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppsService])