Bug 1207090 - Expose TCPSocket to chrome contexts. r=bz

This commit is contained in:
Josh Matthews 2015-09-22 08:45:00 -04:00
parent 8a56a13543
commit e202227bd2
14 changed files with 119 additions and 52 deletions

View File

@ -123,7 +123,7 @@ void
TCPServerSocket::FireEvent(const nsAString& aType, TCPSocket* aSocket)
{
AutoJSAPI api;
api.Init(GetOwner());
api.Init(GetOwnerGlobal());
TCPServerSocketEventInit init;
init.mBubbles = false;
@ -144,7 +144,7 @@ TCPServerSocket::FireEvent(const nsAString& aType, TCPSocket* aSocket)
NS_IMETHODIMP
TCPServerSocket::OnSocketAccepted(nsIServerSocket* aServer, nsISocketTransport* aTransport)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
nsCOMPtr<nsIGlobalObject> global = GetOwnerGlobal();
nsRefPtr<TCPSocket> socket = TCPSocket::CreateAcceptedSocket(global, aTransport, mUseArrayBuffers);
if (mServerBridgeParent) {
socket->SetAppIdAndBrowser(mServerBridgeParent->GetAppId(),
@ -175,7 +175,7 @@ TCPServerSocket::OnStopListening(nsIServerSocket* aServer, nsresult aStatus)
nsresult
TCPServerSocket::AcceptChildSocket(TCPSocketChild* aSocketChild)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
nsCOMPtr<nsIGlobalObject> global = GetOwnerGlobal();
NS_ENSURE_TRUE(global, NS_ERROR_FAILURE);
nsRefPtr<TCPSocket> socket = TCPSocket::CreateAcceptedSocket(global, aSocketChild, mUseArrayBuffers);
NS_ENSURE_TRUE(socket, NS_ERROR_FAILURE);

View File

@ -493,7 +493,7 @@ TCPSocket::FireEvent(const nsAString& aType)
}
AutoJSAPI api;
if (NS_WARN_IF(!api.Init(GetOwner()))) {
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
return NS_ERROR_FAILURE;
}
JS::Rooted<JS::Value> val(api.cx());
@ -505,7 +505,7 @@ TCPSocket::FireDataArrayEvent(const nsAString& aType,
const InfallibleTArray<uint8_t>& buffer)
{
AutoJSAPI api;
if (NS_WARN_IF(!api.Init(GetOwner()))) {
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
return NS_ERROR_FAILURE;
}
JSContext* cx = api.cx();
@ -523,7 +523,7 @@ TCPSocket::FireDataStringEvent(const nsAString& aType,
const nsACString& aString)
{
AutoJSAPI api;
if (NS_WARN_IF(!api.Init(GetOwner()))) {
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
return NS_ERROR_FAILURE;
}
JSContext* cx = api.cx();
@ -1034,7 +1034,7 @@ TCPSocket::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext, nsIInput
}
AutoJSAPI api;
if (!api.Init(GetOwner())) {
if (!api.Init(GetOwnerGlobal())) {
return NS_ERROR_FAILURE;
}
JSContext* cx = api.cx();
@ -1057,7 +1057,7 @@ TCPSocket::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext, nsIInput
}
AutoJSAPI api;
if (!api.Init(GetOwner())) {
if (!api.Init(GetOwnerGlobal())) {
return NS_ERROR_FAILURE;
}
JSContext* cx = api.cx();
@ -1189,3 +1189,16 @@ TCPSocket::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aD
return NS_OK;
}
/* static */
bool
TCPSocket::ShouldTCPSocketExist(JSContext* aCx, JSObject* aGlobal)
{
if (nsContentUtils::IsSystemPrincipal(nsContentUtils::ObjectPrincipal(aGlobal))) {
return true;
}
const char* const perms[] = { "tcp-socket", nullptr };
return Preferences::GetBool("dom.mozTCPSocket.enabled") &&
CheckAnyPermissions(aCx, aGlobal, perms);
}

View File

@ -88,13 +88,10 @@ public:
NS_DECL_NSIOBSERVER
NS_DECL_NSITCPSOCKETCALLBACK
nsPIDOMWindow* GetParentObject() const
{
return GetOwner();
}
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
static bool ShouldTCPSocketExist(JSContext* aCx, JSObject* aGlobal);
void GetHost(nsAString& aHost);
uint32_t Port();
bool Ssl();

View File

@ -9,6 +9,7 @@ DIRS += ['interfaces']
if CONFIG['MOZ_B2G_RIL']:
XPCSHELL_TESTS_MANIFESTS += ['tests/unit_stats/xpcshell.ini']
MOCHITEST_CHROME_MANIFESTS += ['tests/chrome.ini']
MOCHITEST_MANIFESTS += ['tests/mochitest.ini']
EXPORTS.mozilla.dom += [

View File

@ -0,0 +1,7 @@
[DEFAULT]
support-files =
tcpsocket_test.jsm
test_tcpsocket_client_and_server_basics.js
add_task.js
[test_tcpsocket_jsm.html]

View File

@ -0,0 +1,13 @@
this.EXPORTED_SYMBOLS = ['createSocket', 'createServer', 'enablePrefsAndPermissions'];
this.createSocket = function(host, port, options) {
return new TCPSocket(host, port, options);
}
this.createServer = function(port, options, backlog) {
return new TCPServerSocket(port, options, backlog);
}
this.enablePrefsAndPermissions = function() {
return false;
}

View File

@ -13,6 +13,19 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1084245
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="add_task.js"></script>
<script type="application/javascript">
function createServer(port, options, backlog) {
return new TCPServerSocket(port, options, backlog);
}
function createSocket(host, port, options) {
return new TCPSocket(host, port, options);
}
function enablePrefsAndPermissions() {
return true;
}
</script>
<script type="application/javascript;version=1.7" src="test_tcpsocket_client_and_server_basics.js"></script>
</head>
<body>

View File

@ -153,18 +153,20 @@ function defer() {
function* test_basics() {
// Enable our use of TCPSocket
let prefDeferred = defer();
SpecialPowers.pushPrefEnv(
{ set: [ ['dom.mozTCPSocket.enabled', true] ] },
prefDeferred.resolve);
yield prefDeferred.promise;
if (enablePrefsAndPermissions()) {
// Enable our use of TCPSocket
let prefDeferred = defer();
SpecialPowers.pushPrefEnv(
{ set: [ ['dom.mozTCPSocket.enabled', true] ] },
prefDeferred.resolve);
yield prefDeferred.promise;
let permDeferred = defer();
SpecialPowers.pushPermissions(
[ { type: 'tcp-socket', allow: true, context: document } ],
permDeferred.resolve);
yield permDeferred.promise;
let permDeferred = defer();
SpecialPowers.pushPermissions(
[ { type: 'tcp-socket', allow: true, context: document } ],
permDeferred.resolve);
yield permDeferred.promise;
}
// See bug 903830; in e10s mode we never get to find out the localPort if we
// let it pick a free port by choosing 0. This is the same port the xpcshell
@ -172,15 +174,15 @@ function* test_basics() {
let serverPort = 8085;
// - Start up a listening socket.
let listeningServer = new TCPServerSocket(serverPort,
{ binaryType: 'arraybuffer' },
SERVER_BACKLOG);
let listeningServer = createServer(serverPort,
{ binaryType: 'arraybuffer' },
SERVER_BACKLOG);
let connectedPromise = waitForConnection(listeningServer);
// -- Open a connection to the server
let clientSocket = new TCPSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
let clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
let clientQueue = listenForEventsOnSocket(clientSocket, 'client');
// (the client connects)
@ -286,8 +288,8 @@ function* test_basics() {
// -- Re-establish connection
connectedPromise = waitForConnection(listeningServer);
clientSocket = new TCPSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientQueue = listenForEventsOnSocket(clientSocket, 'client');
is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
@ -313,8 +315,8 @@ function* test_basics() {
// -- Re-establish connection
connectedPromise = waitForConnection(listeningServer);
clientSocket = new TCPSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientQueue = listenForEventsOnSocket(clientSocket, 'client');
is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
@ -347,8 +349,8 @@ function* test_basics() {
// -- Re-establish connection
connectedPromise = waitForConnection(listeningServer);
clientSocket = new TCPSocket('127.0.0.1', serverPort,
{ binaryType: 'string' });
clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'string' });
clientQueue = listenForEventsOnSocket(clientSocket, 'client');
is((yield clientQueue.waitForEvent()).type, 'open', 'got open event');
@ -375,8 +377,8 @@ function* test_basics() {
listeningServer.close();
// - try and connect, get an error
clientSocket = new TCPSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });
clientQueue = listenForEventsOnSocket(clientSocket, 'client');
is((yield clientQueue.waitForEvent()).type, 'error', 'fail to connect');
is(clientSocket.readyState, 'closed',

View File

@ -0,0 +1,25 @@
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<meta charset="utf-8">
<title>Test for 1207090</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
<script type="application/javascript">
Components.utils.import("chrome://mochitests/content/chrome/dom/network/tests/tcpsocket_test.jsm");
</script>
<script type="application/javascript" src="add_task.js"></script>
<script type="application/javascript;version=1.7" src="test_tcpsocket_client_and_server_basics.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1207090">Mozilla Bug 1207090</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<div id="container"></div>
</body>
</html>

View File

@ -14,9 +14,8 @@ dictionary ServerSocketOptions {
};
[Constructor(unsigned short port, optional ServerSocketOptions options, optional unsigned short backlog = 0),
Pref="dom.mozTCPSocket.enabled",
CheckAnyPermissions="tcp-socket",
Exposed=Window]
Func="mozilla::dom::TCPSocket::ShouldTCPSocketExist",
Exposed=(Window,System)]
interface TCPServerSocket : EventTarget {
/**
* The port of this server socket object.

View File

@ -4,9 +4,8 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
[Constructor(DOMString type, optional TCPServerSocketEventInit eventInitDict),
Pref="dom.mozTCPSocket.enabled",
CheckAnyPermissions="tcp-socket",
Exposed=Window]
Func="mozilla::dom::TCPSocket::ShouldTCPSocketExist",
Exposed=(Window,System)]
interface TCPServerSocketEvent : Event {
readonly attribute TCPSocket socket;
};

View File

@ -40,9 +40,8 @@ interface LegacyMozTCPSocket {
};
[Constructor(DOMString host, unsigned short port, optional SocketOptions options),
Pref="dom.mozTCPSocket.enabled",
CheckAnyPermissions="tcp-socket",
Exposed=Window]
Func="mozilla::dom::TCPSocket::ShouldTCPSocketExist",
Exposed=(Window,System)]
interface TCPSocket : EventTarget {
/**
* Upgrade an insecure connection to use TLS. Throws if the ready state is not OPEN.

View File

@ -9,9 +9,9 @@
* - if there's an error connecting to the host
*/
[Pref="dom.mozTCPSocket.enabled",
CheckAnyPermissions="tcp-socket",
Constructor(DOMString type, optional TCPSocketErrorEventInit eventInitDict)]
[Func="mozilla::dom::TCPSocket::ShouldTCPSocketExist",
Constructor(DOMString type, optional TCPSocketErrorEventInit eventInitDict),
Exposed=(Window,System)]
interface TCPSocketErrorEvent : Event {
readonly attribute DOMString name;
readonly attribute DOMString message;

View File

@ -10,9 +10,8 @@
*/
[Constructor(DOMString type, optional TCPSocketEventInit eventInitDict),
Pref="dom.mozTCPSocket.enabled",
CheckAnyPermissions="tcp-socket",
Exposed=Window]
Func="mozilla::dom::TCPSocket::ShouldTCPSocketExist",
Exposed=(Window,System)]
interface TCPSocketEvent : Event {
/**
* If the event is a "data" event, data will be the bytes read from the network;