michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict" michael@0: michael@0: function debug(s) { michael@0: //dump("-*- AppsService.js: " + s + "\n"); michael@0: } michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const APPS_SERVICE_CID = Components.ID("{05072afa-92fe-45bf-ae22-39b69c117058}"); michael@0: michael@0: function AppsService() michael@0: { michael@0: debug("AppsService Constructor"); michael@0: let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) michael@0: .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; michael@0: debug("inParent: " + inParent); michael@0: Cu.import(inParent ? "resource://gre/modules/Webapps.jsm" : michael@0: "resource://gre/modules/AppsServiceChild.jsm"); michael@0: } michael@0: michael@0: AppsService.prototype = { michael@0: michael@0: getCSPByLocalId: function getCSPByLocalId(localId) { michael@0: debug("GetCSPByLocalId( " + localId + " )"); michael@0: return DOMApplicationRegistry.getCSPByLocalId(localId); michael@0: }, michael@0: michael@0: getAppByManifestURL: function getAppByManifestURL(aManifestURL) { michael@0: debug("GetAppByManifestURL( " + aManifestURL + " )"); michael@0: return DOMApplicationRegistry.getAppByManifestURL(aManifestURL); michael@0: }, michael@0: michael@0: getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) { michael@0: debug("getAppLocalIdByManifestURL( " + aManifestURL + " )"); michael@0: return DOMApplicationRegistry.getAppLocalIdByManifestURL(aManifestURL); michael@0: }, michael@0: michael@0: getAppLocalIdByStoreId: function getAppLocalIdByStoreId(aStoreId) { michael@0: debug("getAppLocalIdByStoreId( " + aStoreId + " )"); michael@0: return DOMApplicationRegistry.getAppLocalIdByStoreId(aStoreId); michael@0: }, michael@0: michael@0: getAppByLocalId: function getAppByLocalId(aLocalId) { michael@0: debug("getAppByLocalId( " + aLocalId + " )"); michael@0: return DOMApplicationRegistry.getAppByLocalId(aLocalId); michael@0: }, michael@0: michael@0: getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) { michael@0: debug("getManifestURLByLocalId( " + aLocalId + " )"); michael@0: return DOMApplicationRegistry.getManifestURLByLocalId(aLocalId); michael@0: }, michael@0: michael@0: getCoreAppsBasePath: function getCoreAppsBasePath() { michael@0: debug("getCoreAppsBasePath()"); michael@0: return DOMApplicationRegistry.getCoreAppsBasePath(); michael@0: }, michael@0: michael@0: getWebAppsBasePath: function getWebAppsBasePath() { michael@0: debug("getWebAppsBasePath()"); michael@0: return DOMApplicationRegistry.getWebAppsBasePath(); michael@0: }, michael@0: michael@0: getAppInfo: function getAppInfo(aAppId) { michael@0: debug("getAppInfo()"); michael@0: return DOMApplicationRegistry.getAppInfo(aAppId); michael@0: }, michael@0: michael@0: getRedirect: function getRedirect(aLocalId, aURI) { michael@0: debug("getRedirect for " + aLocalId + " " + aURI.spec); michael@0: if (aLocalId == Ci.nsIScriptSecurityManager.NO_APP_ID || michael@0: aLocalId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) { michael@0: return null; michael@0: } michael@0: michael@0: let app = DOMApplicationRegistry.getAppByLocalId(aLocalId); michael@0: if (app && app.redirects) { michael@0: let spec = aURI.spec; michael@0: for (let i = 0; i < app.redirects.length; i++) { michael@0: let redirect = app.redirects[i]; michael@0: if (spec.startsWith(redirect.from)) { michael@0: // Prepend the app origin to the redirection. We need that since michael@0: // the origin of packaged apps is a uuid created at install time. michael@0: let to = app.origin + redirect.to; michael@0: // If we have a ? or a # in the current URL, add this part to the michael@0: // redirection. michael@0: let index = -1; michael@0: index = spec.indexOf('?'); michael@0: if (index == -1) { michael@0: index = spec.indexOf('#'); michael@0: } michael@0: michael@0: if (index != -1) { michael@0: to += spec.substring(index); michael@0: } michael@0: debug('App specific redirection from ' + spec + ' to ' + to); michael@0: return Services.io.newURI(to, null, null); michael@0: } michael@0: } michael@0: } michael@0: // No matching redirect. michael@0: return null; michael@0: }, michael@0: michael@0: classID : APPS_SERVICE_CID, michael@0: QueryInterface : XPCOMUtils.generateQI([Ci.nsIAppsService]) michael@0: } michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppsService])