Skip to content

Commit 5e75741

Browse files
author
Christoph Kerschbaumer
committed
Bug 1099296 - Attach LoadInfo to remaining callers of ioService and ProtocolHandlers - websocket changes (r=jduell)
1 parent 877495c commit 5e75741

12 files changed

+168
-15
lines changed

docshell/base/LoadInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace mozilla {
2020
namespace net {
2121
class HttpChannelParent;
2222
class FTPChannelParent;
23+
class WebSocketChannelParent;
2324
}
2425

2526
/**
@@ -51,6 +52,7 @@ class MOZ_EXPORT LoadInfo MOZ_FINAL : public nsILoadInfo
5152

5253
friend class net::HttpChannelParent;
5354
friend class net::FTPChannelParent;
55+
friend class net::WebSocketChannelParent;
5456

5557
~LoadInfo();
5658

dom/base/WebSocket.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,15 +1574,11 @@ WebSocketImpl::InitializeConnection()
15741574
// are not thread-safe.
15751575
mOriginDocument = nullptr;
15761576

1577-
nsCOMPtr<nsILoadInfo> loadInfo =
1578-
new LoadInfo(doc ?
1579-
doc->NodePrincipal() : mPrincipal.get(),
1580-
mPrincipal,
1581-
doc,
1582-
nsILoadInfo::SEC_NORMAL,
1583-
nsIContentPolicy::TYPE_WEBSOCKET);
1584-
rv = wsChannel->SetLoadInfo(loadInfo);
1585-
NS_ENSURE_SUCCESS(rv, rv);
1577+
wsChannel->InitLoadInfo(doc ? doc->AsDOMNode() : nullptr,
1578+
doc ? doc->NodePrincipal() : mPrincipal.get(),
1579+
mPrincipal,
1580+
nsILoadInfo::SEC_NORMAL,
1581+
nsIContentPolicy::TYPE_WEBSOCKET);
15861582

15871583
if (!mRequestedProtocolList.IsEmpty()) {
15881584
rv = wsChannel->SetProtocol(mRequestedProtocolList);

netwerk/ipc/NeckoChannelParams.ipdlh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,18 @@ struct RtspChannelConnectArgs
121121
uint32_t channelId;
122122
};
123123

124+
//-----------------------------------------------------------------------------
125+
// WS IPDL structs
126+
//-----------------------------------------------------------------------------
127+
128+
struct WebSocketLoadInfoArgs
129+
{
130+
PrincipalInfo requestingPrincipalInfo;
131+
PrincipalInfo triggeringPrincipalInfo;
132+
uint32_t securityFlags;
133+
uint32_t contentPolicyType;
134+
uint32_t innerWindowID;
135+
};
136+
124137
} // namespace ipc
125138
} // namespace mozilla

netwerk/protocol/websocket/BaseWebSocketChannel.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ BaseWebSocketChannel::SetPingTimeout(uint32_t aSeconds)
182182
return NS_OK;
183183
}
184184

185+
NS_IMETHODIMP
186+
BaseWebSocketChannel::InitLoadInfo(nsIDOMNode* aLoadingNode,
187+
nsIPrincipal* aLoadingPrincipal,
188+
nsIPrincipal* aTriggeringPrincipal,
189+
uint32_t aSecurityFlags,
190+
uint32_t aContentPolicyType)
191+
{
192+
nsCOMPtr<nsINode> node = do_QueryInterface(aLoadingNode);
193+
mLoadInfo = new LoadInfo(aLoadingPrincipal, aTriggeringPrincipal,
194+
node, aSecurityFlags, aContentPolicyType);
195+
return NS_OK;
196+
}
197+
185198
//-----------------------------------------------------------------------------
186199
// BaseWebSocketChannel::nsIProtocolHandler
187200
//-----------------------------------------------------------------------------

netwerk/protocol/websocket/BaseWebSocketChannel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class BaseWebSocketChannel : public nsIWebSocketChannel,
5252
NS_IMETHOD SetPingInterval(uint32_t aSeconds) MOZ_OVERRIDE;
5353
NS_IMETHOD GetPingTimeout(uint32_t *aSeconds) MOZ_OVERRIDE;
5454
NS_IMETHOD SetPingTimeout(uint32_t aSeconds) MOZ_OVERRIDE;
55+
NS_IMETHOD InitLoadInfo(nsIDOMNode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
56+
nsIPrincipal* aTriggeringPrincipal, uint32_t aSecurityFlags,
57+
uint32_t aContentPolicyType) MOZ_OVERRIDE;
5558

5659
// Off main thread URI access.
5760
virtual void GetEffectiveURL(nsAString& aEffectiveURL) const = 0;

netwerk/protocol/websocket/PWebSocket.ipdl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include protocol PNecko;
99
include protocol PBrowser;
1010
include InputStreamParams;
1111
include URIParams;
12+
include NeckoChannelParams;
1213

1314
include protocol PBlob; //FIXME: bug #792908
1415

@@ -31,7 +32,8 @@ parent:
3132
uint32_t aPingInterval,
3233
bool aClientSetPingInterval,
3334
uint32_t aPingTimeout,
34-
bool aClientSetPingTimeout);
35+
bool aClientSetPingTimeout,
36+
WebSocketLoadInfoArgs aLoadInfoArgs);
3537
Close(uint16_t code, nsCString reason);
3638
SendMsg(nsCString aMsg);
3739
SendBinaryMsg(nsCString aMsg);

netwerk/protocol/websocket/WebSocketChannelChild.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "nsNetUtil.h"
1414
#include "mozilla/ipc/InputStreamUtils.h"
1515
#include "mozilla/ipc/URIUtils.h"
16+
#include "mozilla/ipc/BackgroundUtils.h"
1617
#include "mozilla/net/ChannelEventQueue.h"
1718
#include "SerializedLoadContext.h"
1819

@@ -452,6 +453,25 @@ WebSocketChannelChild::OnServerClose(const uint16_t& aCode,
452453
}
453454
}
454455

456+
// helper function to assign loadInfo to wsArgs
457+
void
458+
propagateLoadInfo(nsILoadInfo *aLoadInfo,
459+
WebSocketLoadInfoArgs& wsArgs)
460+
{
461+
mozilla::ipc::PrincipalInfo requestingPrincipalInfo;
462+
mozilla::ipc::PrincipalInfo triggeringPrincipalInfo;
463+
464+
mozilla::ipc::PrincipalToPrincipalInfo(aLoadInfo->LoadingPrincipal(),
465+
&requestingPrincipalInfo);
466+
wsArgs.requestingPrincipalInfo() = requestingPrincipalInfo;
467+
mozilla::ipc::PrincipalToPrincipalInfo(aLoadInfo->TriggeringPrincipal(),
468+
&triggeringPrincipalInfo);
469+
wsArgs.triggeringPrincipalInfo() = triggeringPrincipalInfo;
470+
wsArgs.securityFlags() = aLoadInfo->GetSecurityFlags();
471+
wsArgs.contentPolicyType() = aLoadInfo->GetContentPolicyType();
472+
wsArgs.innerWindowID() = aLoadInfo->GetInnerWindowID();
473+
}
474+
455475
NS_IMETHODIMP
456476
WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
457477
const nsACString &aOrigin,
@@ -482,12 +502,16 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
482502
// Corresponding release in DeallocPWebSocket
483503
AddIPDLReference();
484504

505+
WebSocketLoadInfoArgs wsArgs;
506+
propagateLoadInfo(mLoadInfo, wsArgs);
507+
485508
gNeckoChild->SendPWebSocketConstructor(this, tabChild,
486509
IPC::SerializedLoadContext(this));
487510
if (!SendAsyncOpen(uri, nsCString(aOrigin), mProtocol, mEncrypted,
488511
mPingInterval, mClientSetPingInterval,
489-
mPingResponseTimeout, mClientSetPingTimeout))
512+
mPingResponseTimeout, mClientSetPingTimeout, wsArgs)) {
490513
return NS_ERROR_UNEXPECTED;
514+
}
491515

492516
mOriginalURI = aURI;
493517
mURI = mOriginalURI;

netwerk/protocol/websocket/WebSocketChannelParent.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "nsIAuthPromptProvider.h"
1010
#include "mozilla/ipc/InputStreamUtils.h"
1111
#include "mozilla/ipc/URIUtils.h"
12+
#include "mozilla/ipc/BackgroundUtils.h"
1213
#include "SerializedLoadContext.h"
1314
#include "nsIOService.h"
1415
#include "mozilla/net/NeckoCommon.h"
@@ -66,13 +67,15 @@ WebSocketChannelParent::RecvAsyncOpen(const URIParams& aURI,
6667
const uint32_t& aPingInterval,
6768
const bool& aClientSetPingInterval,
6869
const uint32_t& aPingTimeout,
69-
const bool& aClientSetPingTimeout)
70+
const bool& aClientSetPingTimeout,
71+
const WebSocketLoadInfoArgs& aLoadInfoArgs)
7072
{
7173
LOG(("WebSocketChannelParent::RecvAsyncOpen() %p\n", this));
7274

7375
nsresult rv;
7476
nsCOMPtr<nsIURI> uri;
75-
77+
nsCOMPtr<nsIPrincipal> requestingPrincipal, triggeringPrincipal;
78+
nsCOMPtr<nsILoadInfo> loadInfo;
7679

7780
bool appOffline = false;
7881
uint32_t appId = GetAppId();
@@ -94,6 +97,28 @@ WebSocketChannelParent::RecvAsyncOpen(const URIParams& aURI,
9497
if (NS_FAILED(rv))
9598
goto fail;
9699

100+
requestingPrincipal =
101+
mozilla::ipc::PrincipalInfoToPrincipal(aLoadInfoArgs.requestingPrincipalInfo(), &rv);
102+
if (NS_FAILED(rv)) {
103+
goto fail;
104+
}
105+
106+
triggeringPrincipal =
107+
mozilla::ipc::PrincipalInfoToPrincipal(aLoadInfoArgs.triggeringPrincipalInfo(), &rv);
108+
if (NS_FAILED(rv)) {
109+
goto fail;
110+
}
111+
112+
loadInfo = new LoadInfo(requestingPrincipal,
113+
triggeringPrincipal,
114+
aLoadInfoArgs.securityFlags(),
115+
aLoadInfoArgs.contentPolicyType(),
116+
aLoadInfoArgs.innerWindowID());
117+
rv = mChannel->SetLoadInfo(loadInfo);
118+
if (NS_FAILED(rv)) {
119+
goto fail;
120+
}
121+
97122
rv = mChannel->SetNotificationCallbacks(this);
98123
if (NS_FAILED(rv))
99124
goto fail;

netwerk/protocol/websocket/WebSocketChannelParent.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class WebSocketChannelParent : public PWebSocketParent,
4545
const uint32_t& aPingInterval,
4646
const bool& aClientSetPingInterval,
4747
const uint32_t& aPingTimeout,
48-
const bool& aClientSetPingTimeout) MOZ_OVERRIDE;
48+
const bool& aClientSetPingTimeout,
49+
const WebSocketLoadInfoArgs& aLoadInfoArgs) MOZ_OVERRIDE;
4950
bool RecvClose(const uint16_t & code, const nsCString & reason) MOZ_OVERRIDE;
5051
bool RecvSendMsg(const nsCString& aMsg) MOZ_OVERRIDE;
5152
bool RecvSendBinaryMsg(const nsCString& aMsg) MOZ_OVERRIDE;

netwerk/protocol/websocket/nsIWebSocketChannel.idl

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface nsILoadGroup;
1010
interface nsIWebSocketListener;
1111
interface nsIInputStream;
1212
interface nsILoadInfo;
13+
interface nsIDOMNode;
14+
interface nsIPrincipal;
1315

1416
#include "nsISupports.idl"
1517

@@ -20,7 +22,7 @@ interface nsILoadInfo;
2022
* We are also making it scriptable for now, but this may change once we have
2123
* WebSockets for Workers.
2224
*/
23-
[scriptable, uuid(3fb248c2-c0fc-4516-9c58-505a92ad8c64)]
25+
[scriptable, uuid(21217c03-2ff7-4f9c-9d10-6d3d94a7d843)]
2426
interface nsIWebSocketChannel : nsISupports
2527
{
2628
/**
@@ -67,6 +69,63 @@ interface nsIWebSocketChannel : nsISupports
6769
*/
6870
readonly attribute ACString extensions;
6971

72+
/**
73+
* Init the WebSocketChannel with LoadInfo arguments.
74+
* @param aLoadingNode
75+
* The loadingDocument of the channel.
76+
* The element or document where the result of this request will be
77+
* used. This is the document/element that will get access to the
78+
* result of this request. For example for an image load, it's the
79+
* document in which the image will be loaded. And for a CSS
80+
* stylesheet it's the document whose rendering will be affected by
81+
* the stylesheet.
82+
* For loads that are not related to any document, such as loads coming
83+
* from addons or internal browser features, use null here.
84+
* @param aLoadingPrincipal
85+
* The loadingPrincipal of the channel.
86+
* The principal of the document where the result of this request will
87+
* be used.
88+
* This defaults to the principal of aLoadingNode, so when aLoadingNode
89+
* is passed then aLoadingPrincipal can be left as null. However, for
90+
* loads where aLoadingNode is null this argument must be passed.
91+
* For example for loads from a WebWorker, pass the principal
92+
* of that worker. For loads from an addon or from internal browser
93+
* features, pass the system principal.
94+
* If aLoadingNode is null and the URI being loaded isn't coming from
95+
* a webpage, the principal should almost always be the systemPrincipal.
96+
* One exception to this is for loads from WebWorkers since they don't
97+
* have any nodes to be passed as aLoadingNode.
98+
* Please note, aLoadingPrincipal is *not* the principal of the
99+
* resource being loaded. But rather the principal of the context
100+
* where the resource will be used.
101+
* @param aTriggeringPrincipal
102+
* The triggeringPrincipal of the load.
103+
* The triggeringPrincipal is the principal of the resource that caused
104+
* this particular URL to be loaded.
105+
* For WebSockets, the loadingPrincipal and the triggeringPrincipal
106+
* are *always* identical.
107+
* @param aSecurityFlags
108+
* The securityFlags of the channel.
109+
* Any of the securityflags defined in nsILoadInfo.idl
110+
* @param aContentPolicyType
111+
* The contentPolicyType of the channel.
112+
* Any of the content types defined in nsIContentPolicy.idl
113+
* @return reference to the new nsIChannel object
114+
*
115+
*
116+
* Keep in mind that URIs coming from a webpage should *never* use the
117+
* systemPrincipal as the loadingPrincipal.
118+
*
119+
* Please note, if you provide both a loadingNode and a loadingPrincipal,
120+
* then loadingPrincipal must be equal to loadingNode->NodePrincipal().
121+
* But less error prone is to just supply a loadingNode.
122+
*/
123+
void initLoadInfo(in nsIDOMNode aLoadingNode,
124+
in nsIPrincipal aLoadingPrincipal,
125+
in nsIPrincipal aTriggeringPrincipal,
126+
in unsigned long aSecurityFlags,
127+
in unsigned long aContentPolicyType);
128+
70129
/**
71130
* Asynchronously open the websocket connection. Received messages are fed
72131
* to the socket listener as they arrive. The socket listener's methods

netwerk/test/unit/test_dns_proxy_bypass.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
Cu.import("resource://gre/modules/Services.jsm");
6+
57
var ioService = Cc["@mozilla.org/network/io-service;1"].
68
getService(Ci.nsIIOService);
79

@@ -61,6 +63,13 @@ function run_test() {
6163
prefs.setBoolPref("network.proxy.socks_remote_dns", true);
6264
var chan = Cc["@mozilla.org/network/protocol;1?name=ws"].
6365
createInstance(Components.interfaces.nsIWebSocketChannel);
66+
67+
chan.initLoadInfo(null, // aLoadingNode
68+
Services.scriptSecurityManager.getSystemPrincipal(),
69+
null, // aTriggeringPrincipal
70+
Ci.nsILoadInfo.SEC_NORMAL,
71+
Ci.nsIContentPolicy.TYPE_WEBSOCKET);
72+
6473
var uri = ioService.newURI(url, null, null);
6574
chan.asyncOpen(uri, url, listener, null);
6675
do_test_pending();

netwerk/test/unit/test_websocket_offline.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ function run_test() {
3535
try {
3636
chan = Cc["@mozilla.org/network/protocol;1?name=ws"].
3737
createInstance(Components.interfaces.nsIWebSocketChannel);
38+
chan.initLoadInfo(null, // aLoadingNode
39+
Services.scriptSecurityManager.getSystemPrincipal(),
40+
null, // aTriggeringPrincipal
41+
Ci.nsILoadInfo.SEC_NORMAL,
42+
Ci.nsIContentPolicy.TYPE_WEBSOCKET);
43+
3844
var uri = Services.io.newURI(url, null, null);
3945
chan.asyncOpen(uri, url, listener, null);
4046
do_test_pending();

0 commit comments

Comments
 (0)