|
| 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 |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +const Cc = Components.classes; |
| 8 | +const Ci = Components.interfaces; |
| 9 | +const Cu = Components.utils; |
| 10 | + |
| 11 | +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 12 | +Cu.import("resource://gre/modules/Services.jsm"); |
| 13 | + |
| 14 | +XPCOMUtils.defineLazyGetter(this, "cpmm", function() { |
| 15 | + return Cc["@mozilla.org/childprocessmessagemanager;1"] |
| 16 | + .getService(Ci.nsIFrameMessageManager) |
| 17 | + .QueryInterface(Ci.nsISyncMessageSender); |
| 18 | +}); |
| 19 | + |
| 20 | +function AppProtocolHandler() { |
| 21 | + this._basePath = null; |
| 22 | +} |
| 23 | + |
| 24 | +AppProtocolHandler.prototype = { |
| 25 | + classID: Components.ID("{b7ad6144-d344-4687-b2d0-b6b9dce1f07f}"), |
| 26 | + QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler]), |
| 27 | + |
| 28 | + scheme: "app", |
| 29 | + defaultPort: -1, |
| 30 | + // Using the same flags as the JAR protocol handler. |
| 31 | + protocolFlags2: Ci.nsIProtocolHandler.URI_NORELATIVE | |
| 32 | + Ci.nsIProtocolHandler.URI_NOAUTH | |
| 33 | + Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE, |
| 34 | + |
| 35 | + get basePath() { |
| 36 | + if (!this._basePath) { |
| 37 | + this._basePath = cpmm.sendSyncMessage("Webapps:GetBasePath", { })[0] + "/"; |
| 38 | + } |
| 39 | + |
| 40 | + return this._basePath; |
| 41 | + }, |
| 42 | + |
| 43 | + newURI: function app_phNewURI(aSpec, aOriginCharset, aBaseURI) { |
| 44 | + let uri = Cc["@mozilla.org/network/standard-url;1"] |
| 45 | + .createInstance(Ci.nsIStandardURL); |
| 46 | + uri.init(Ci.nsIStandardURL.URLTYPE_STANDARD, -1, aSpec, aOriginCharset, |
| 47 | + aBaseURI); |
| 48 | + return uri.QueryInterface(Ci.nsIURI); |
| 49 | + }, |
| 50 | + |
| 51 | + newChannel: function app_phNewChannel(aURI) { |
| 52 | + // We map app://ABCDEF/path/to/file.ext to |
| 53 | + // jar:file:///path/to/profile/webapps/ABCDEF/application.zip!/path/to/file.ext |
| 54 | + let noScheme = aURI.spec.substring(6); |
| 55 | + let firstSlash = noScheme.indexOf("/"); |
| 56 | + |
| 57 | + let appId = noScheme; |
| 58 | + let fileSpec = aURI.path; |
| 59 | + |
| 60 | + if (firstSlash) { |
| 61 | + appId = noScheme.substring(0, firstSlash); |
| 62 | + } |
| 63 | + |
| 64 | + // Simulates default behavior of http servers: |
| 65 | + // Adds index.html if the file spec ends in / in /#anchor |
| 66 | + let lastSlash = fileSpec.lastIndexOf("/"); |
| 67 | + if (lastSlash == fileSpec.length - 1) { |
| 68 | + fileSpec += "index.html"; |
| 69 | + } else if (fileSpec[lastSlash + 1] == '#') { |
| 70 | + let anchor = fileSpec.substring(lastSlash + 1); |
| 71 | + fileSpec = fileSpec.substring(0, lastSlash) + "/index.html" + anchor; |
| 72 | + } |
| 73 | + |
| 74 | + // Build a jar channel and masquerade as an app:// URI. |
| 75 | + let uri = "jar:file://" + this.basePath + appId + "/application.zip!" + fileSpec; |
| 76 | + let channel = Services.io.newChannel(uri, null, null); |
| 77 | + channel.QueryInterface(Ci.nsIJARChannel).setAppURI(aURI); |
| 78 | + channel.QueryInterface(Ci.nsIChannel).originalURI = aURI; |
| 79 | + |
| 80 | + return channel; |
| 81 | + }, |
| 82 | + |
| 83 | + allowPort: function app_phAllowPort(aPort, aScheme) { |
| 84 | + return false; |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +const NSGetFactory = XPCOMUtils.generateNSGetFactory([AppProtocolHandler]); |
0 commit comments