1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/apps/src/AppsService.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict" 1.9 + 1.10 +function debug(s) { 1.11 + //dump("-*- AppsService.js: " + s + "\n"); 1.12 +} 1.13 + 1.14 +const Cc = Components.classes; 1.15 +const Ci = Components.interfaces; 1.16 +const Cu = Components.utils; 1.17 + 1.18 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.19 +Cu.import("resource://gre/modules/Services.jsm"); 1.20 + 1.21 +const APPS_SERVICE_CID = Components.ID("{05072afa-92fe-45bf-ae22-39b69c117058}"); 1.22 + 1.23 +function AppsService() 1.24 +{ 1.25 + debug("AppsService Constructor"); 1.26 + let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) 1.27 + .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; 1.28 + debug("inParent: " + inParent); 1.29 + Cu.import(inParent ? "resource://gre/modules/Webapps.jsm" : 1.30 + "resource://gre/modules/AppsServiceChild.jsm"); 1.31 +} 1.32 + 1.33 +AppsService.prototype = { 1.34 + 1.35 + getCSPByLocalId: function getCSPByLocalId(localId) { 1.36 + debug("GetCSPByLocalId( " + localId + " )"); 1.37 + return DOMApplicationRegistry.getCSPByLocalId(localId); 1.38 + }, 1.39 + 1.40 + getAppByManifestURL: function getAppByManifestURL(aManifestURL) { 1.41 + debug("GetAppByManifestURL( " + aManifestURL + " )"); 1.42 + return DOMApplicationRegistry.getAppByManifestURL(aManifestURL); 1.43 + }, 1.44 + 1.45 + getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) { 1.46 + debug("getAppLocalIdByManifestURL( " + aManifestURL + " )"); 1.47 + return DOMApplicationRegistry.getAppLocalIdByManifestURL(aManifestURL); 1.48 + }, 1.49 + 1.50 + getAppLocalIdByStoreId: function getAppLocalIdByStoreId(aStoreId) { 1.51 + debug("getAppLocalIdByStoreId( " + aStoreId + " )"); 1.52 + return DOMApplicationRegistry.getAppLocalIdByStoreId(aStoreId); 1.53 + }, 1.54 + 1.55 + getAppByLocalId: function getAppByLocalId(aLocalId) { 1.56 + debug("getAppByLocalId( " + aLocalId + " )"); 1.57 + return DOMApplicationRegistry.getAppByLocalId(aLocalId); 1.58 + }, 1.59 + 1.60 + getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) { 1.61 + debug("getManifestURLByLocalId( " + aLocalId + " )"); 1.62 + return DOMApplicationRegistry.getManifestURLByLocalId(aLocalId); 1.63 + }, 1.64 + 1.65 + getCoreAppsBasePath: function getCoreAppsBasePath() { 1.66 + debug("getCoreAppsBasePath()"); 1.67 + return DOMApplicationRegistry.getCoreAppsBasePath(); 1.68 + }, 1.69 + 1.70 + getWebAppsBasePath: function getWebAppsBasePath() { 1.71 + debug("getWebAppsBasePath()"); 1.72 + return DOMApplicationRegistry.getWebAppsBasePath(); 1.73 + }, 1.74 + 1.75 + getAppInfo: function getAppInfo(aAppId) { 1.76 + debug("getAppInfo()"); 1.77 + return DOMApplicationRegistry.getAppInfo(aAppId); 1.78 + }, 1.79 + 1.80 + getRedirect: function getRedirect(aLocalId, aURI) { 1.81 + debug("getRedirect for " + aLocalId + " " + aURI.spec); 1.82 + if (aLocalId == Ci.nsIScriptSecurityManager.NO_APP_ID || 1.83 + aLocalId == Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) { 1.84 + return null; 1.85 + } 1.86 + 1.87 + let app = DOMApplicationRegistry.getAppByLocalId(aLocalId); 1.88 + if (app && app.redirects) { 1.89 + let spec = aURI.spec; 1.90 + for (let i = 0; i < app.redirects.length; i++) { 1.91 + let redirect = app.redirects[i]; 1.92 + if (spec.startsWith(redirect.from)) { 1.93 + // Prepend the app origin to the redirection. We need that since 1.94 + // the origin of packaged apps is a uuid created at install time. 1.95 + let to = app.origin + redirect.to; 1.96 + // If we have a ? or a # in the current URL, add this part to the 1.97 + // redirection. 1.98 + let index = -1; 1.99 + index = spec.indexOf('?'); 1.100 + if (index == -1) { 1.101 + index = spec.indexOf('#'); 1.102 + } 1.103 + 1.104 + if (index != -1) { 1.105 + to += spec.substring(index); 1.106 + } 1.107 + debug('App specific redirection from ' + spec + ' to ' + to); 1.108 + return Services.io.newURI(to, null, null); 1.109 + } 1.110 + } 1.111 + } 1.112 + // No matching redirect. 1.113 + return null; 1.114 + }, 1.115 + 1.116 + classID : APPS_SERVICE_CID, 1.117 + QueryInterface : XPCOMUtils.generateQI([Ci.nsIAppsService]) 1.118 +} 1.119 + 1.120 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppsService])