Skip to content

Commit 3fe7c66

Browse files
author
Fabrice Desré
committed
Bug 769350 - Implement trusted/certified app scheme support [r=vingtetun]
1 parent 994e810 commit 3fe7c66

File tree

9 files changed

+144
-3
lines changed

9 files changed

+144
-3
lines changed

b2g/installer/package-manifest.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@
473473
@BINPATH@/components/SystemMessageManager.js
474474
@BINPATH@/components/SystemMessageManager.manifest
475475

476+
@BINPATH@/components/AppProtocolHandler.js
477+
@BINPATH@/components/AppProtocolHandler.manifest
478+
476479
; Modules
477480
@BINPATH@/modules/*
478481

dom/apps/src/Webapps.jsm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ let DOMApplicationRegistry = {
5151
"Webapps:GetSelf",
5252
"Webapps:GetInstalled", "Webapps:GetNotInstalled",
5353
"Webapps:Launch", "Webapps:GetAll",
54-
"Webapps:InstallPackage"];
54+
"Webapps:InstallPackage", "Webapps:GetBasePath"];
5555

5656
this.messages.forEach((function(msgName) {
5757
ppmm.addMessageListener(msgName, this);
@@ -188,6 +188,9 @@ let DOMApplicationRegistry = {
188188
case "Webapps:InstallPackage":
189189
this.installPackage(msg);
190190
break;
191+
case "Webapps:GetBasePath":
192+
return FileUtils.getFile(DIRECTORY_NAME, ["webapps"], true).path;
193+
break;
191194
}
192195
},
193196

modules/libjar/nsIJARChannel.idl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ interface nsIJARChannel : nsIChannel
1515
* channel.
1616
*/
1717
readonly attribute boolean isUnsafe;
18+
19+
/**
20+
* Forces the uri to be a app:// uri.
21+
*/
22+
void setAppURI(in nsIURI uri);
1823
};

modules/libjar/nsJARChannel.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ nsJARInputThunk::IsNonBlocking(bool *nonBlocking)
186186

187187

188188
nsJARChannel::nsJARChannel()
189-
: mContentLength(-1)
189+
: mAppURI(nsnull)
190+
, mContentLength(-1)
190191
, mLoadFlags(LOAD_NORMAL)
191192
, mStatus(NS_OK)
192193
, mIsPending(false)
@@ -476,7 +477,12 @@ nsJARChannel::SetOriginalURI(nsIURI *aURI)
476477
NS_IMETHODIMP
477478
nsJARChannel::GetURI(nsIURI **aURI)
478479
{
479-
NS_IF_ADDREF(*aURI = mJarURI);
480+
if (mAppURI) {
481+
NS_IF_ADDREF(*aURI = mAppURI);
482+
} else {
483+
NS_IF_ADDREF(*aURI = mJarURI);
484+
}
485+
480486
return NS_OK;
481487
}
482488

@@ -758,6 +764,20 @@ nsJARChannel::GetIsUnsafe(bool *isUnsafe)
758764
return NS_OK;
759765
}
760766

767+
NS_IMETHODIMP
768+
nsJARChannel::SetAppURI(nsIURI *aURI) {
769+
NS_ENSURE_ARG_POINTER(aURI);
770+
771+
nsCAutoString scheme;
772+
aURI->GetScheme(scheme);
773+
if (!scheme.EqualsLiteral("app")) {
774+
return NS_ERROR_INVALID_ARG;
775+
}
776+
777+
mAppURI = aURI;
778+
return NS_OK;
779+
}
780+
761781
//-----------------------------------------------------------------------------
762782
// nsIDownloadObserver
763783
//-----------------------------------------------------------------------------

modules/libjar/nsJARChannel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class nsJARChannel : public nsIJARChannel
5555

5656
nsCOMPtr<nsIJARURI> mJarURI;
5757
nsCOMPtr<nsIURI> mOriginalURI;
58+
nsCOMPtr<nsIURI> mAppURI;
5859
nsCOMPtr<nsISupports> mOwner;
5960
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
6061
nsCOMPtr<nsISupports> mSecurityInfo;

netwerk/protocol/Makefile.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include $(DEPTH)/config/autoconf.mk
1212

1313
PARALLEL_DIRS = \
1414
about \
15+
app \
1516
data \
1617
device \
1718
file \
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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]);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AppProtocolHander.js
2+
component {b7ad6144-d344-4687-b2d0-b6b9dce1f07f} AppProtocolHandler.js
3+
contract @mozilla.org/network/protocol;1?name=app {b7ad6144-d344-4687-b2d0-b6b9dce1f07f}

netwerk/protocol/app/Makefile.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
DEPTH = ../../..
6+
topsrcdir = @top_srcdir@
7+
srcdir = @srcdir@
8+
VPATH = @srcdir@
9+
10+
include $(DEPTH)/config/autoconf.mk
11+
12+
EXTRA_COMPONENTS = \
13+
AppProtocolHandler.js \
14+
AppProtocolHandler.manifest \
15+
$(NULL)
16+
17+
include $(topsrcdir)/config/rules.mk

0 commit comments

Comments
 (0)