2012-07-11 08:38:36 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-01-31 13:35:13 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
2012-07-11 08:38:36 -07:00
|
|
|
|
|
|
|
function AppProtocolHandler() {
|
2012-12-22 05:56:21 -08:00
|
|
|
this._appInfo = [];
|
|
|
|
this._runningInParent = Cc["@mozilla.org/xre/runtime;1"]
|
|
|
|
.getService(Ci.nsIXULRuntime)
|
|
|
|
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
2012-07-11 08:38:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AppProtocolHandler.prototype = {
|
|
|
|
classID: Components.ID("{b7ad6144-d344-4687-b2d0-b6b9dce1f07f}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]),
|
|
|
|
|
|
|
|
scheme: "app",
|
|
|
|
defaultPort: -1,
|
2012-09-25 12:33:01 -07:00
|
|
|
// Don't allow loading from other protocols, and only from app:// if webapps is granted
|
|
|
|
protocolFlags: Ci.nsIProtocolHandler.URI_NOAUTH |
|
|
|
|
Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD |
|
|
|
|
Ci.nsIProtocolHandler.URI_CROSS_ORIGIN_NEEDS_WEBAPPS_PERM,
|
2012-07-11 08:38:36 -07:00
|
|
|
|
2012-12-22 05:56:21 -08:00
|
|
|
getAppInfo: function app_phGetAppInfo(aId) {
|
2012-08-29 14:20:03 -07:00
|
|
|
|
2012-12-22 05:56:21 -08:00
|
|
|
if (!this._appInfo[aId]) {
|
2013-01-31 13:35:13 -08:00
|
|
|
this._appInfo[aId] = appsService.getAppInfo(aId);
|
2012-07-11 08:38:36 -07:00
|
|
|
}
|
2012-12-22 05:56:21 -08:00
|
|
|
return this._appInfo[aId];
|
2012-07-11 08:38:36 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
newURI: function app_phNewURI(aSpec, aOriginCharset, aBaseURI) {
|
|
|
|
let uri = Cc["@mozilla.org/network/standard-url;1"]
|
|
|
|
.createInstance(Ci.nsIStandardURL);
|
|
|
|
uri.init(Ci.nsIStandardURL.URLTYPE_STANDARD, -1, aSpec, aOriginCharset,
|
|
|
|
aBaseURI);
|
|
|
|
return uri.QueryInterface(Ci.nsIURI);
|
|
|
|
},
|
|
|
|
|
|
|
|
newChannel: function app_phNewChannel(aURI) {
|
|
|
|
// We map app://ABCDEF/path/to/file.ext to
|
|
|
|
// jar:file:///path/to/profile/webapps/ABCDEF/application.zip!/path/to/file.ext
|
2013-02-27 12:09:12 -08:00
|
|
|
let noScheme = aURI.spec.substring(6);
|
|
|
|
let firstSlash = noScheme.indexOf("/");
|
2012-07-11 08:38:36 -07:00
|
|
|
|
2013-02-27 12:09:12 -08:00
|
|
|
let appId = noScheme;
|
|
|
|
let fileSpec = aURI.path;
|
2013-02-27 11:31:02 -08:00
|
|
|
|
2013-02-27 12:09:12 -08:00
|
|
|
if (firstSlash) {
|
|
|
|
appId = noScheme.substring(0, firstSlash);
|
2013-02-27 11:31:02 -08:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:09:12 -08:00
|
|
|
// Build a jar channel and masquerade as an app:// URI.
|
|
|
|
let appInfo = this.getAppInfo(appId);
|
|
|
|
let uri;
|
2012-12-22 05:56:21 -08:00
|
|
|
if (this._runningInParent || appInfo.isCoreApp) {
|
|
|
|
// In-parent and CoreApps can directly access files, so use jar:file://
|
|
|
|
uri = "jar:file://" + appInfo.basePath + appId + "/application.zip!" + fileSpec;
|
|
|
|
} else {
|
|
|
|
// non-CoreApps in child need to ask parent for file handle, use jar:ipcfile://
|
|
|
|
uri = "jar:remoteopenfile://" + appInfo.basePath + appId + "/application.zip!" + fileSpec;
|
|
|
|
}
|
2012-07-11 08:38:36 -07:00
|
|
|
let channel = Services.io.newChannel(uri, null, null);
|
|
|
|
channel.QueryInterface(Ci.nsIJARChannel).setAppURI(aURI);
|
|
|
|
channel.QueryInterface(Ci.nsIChannel).originalURI = aURI;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
},
|
|
|
|
|
|
|
|
allowPort: function app_phAllowPort(aPort, aScheme) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AppProtocolHandler]);
|