Skip to content

Commit e0173ff

Browse files
committed
Bug 1215092 - WebSocketEventService and WebSocket discovering - part 2 - Unique Serial number for WebSocketChannel in IPC, r=michal
1 parent 4d8e79e commit e0173ff

13 files changed

+84
-16
lines changed

netwerk/ipc/NeckoChild.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ NeckoChild::DeallocPWyciwygChannelChild(PWyciwygChannelChild* channel)
145145

146146
PWebSocketChild*
147147
NeckoChild::AllocPWebSocketChild(const PBrowserOrId& browser,
148-
const SerializedLoadContext& aSerialized)
148+
const SerializedLoadContext& aSerialized,
149+
const uint32_t& aSerial)
149150
{
150151
NS_NOTREACHED("AllocPWebSocketChild should not be called");
151152
return nullptr;

netwerk/ipc/NeckoChild.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class NeckoChild :
4040
virtual bool DeallocPFTPChannelChild(PFTPChannelChild*) override;
4141
virtual PWebSocketChild*
4242
AllocPWebSocketChild(const PBrowserOrId&,
43-
const SerializedLoadContext&) override;
43+
const SerializedLoadContext&,
44+
const uint32_t&) override;
4445
virtual bool DeallocPWebSocketChild(PWebSocketChild*) override;
4546
virtual PTCPSocketChild* AllocPTCPSocketChild(const nsString& host,
4647
const uint16_t& port) override;

netwerk/ipc/NeckoParent.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ NeckoParent::DeallocPWyciwygChannelParent(PWyciwygChannelParent* channel)
320320

321321
PWebSocketParent*
322322
NeckoParent::AllocPWebSocketParent(const PBrowserOrId& browser,
323-
const SerializedLoadContext& serialized)
323+
const SerializedLoadContext& serialized,
324+
const uint32_t& aSerial)
324325
{
325326
nsCOMPtr<nsILoadContext> loadContext;
326327
const char *error = CreateChannelLoadContext(browser, Manager(),
@@ -335,7 +336,8 @@ NeckoParent::AllocPWebSocketParent(const PBrowserOrId& browser,
335336
RefPtr<TabParent> tabParent = TabParent::GetFrom(browser.get_PBrowserParent());
336337
PBOverrideStatus overrideStatus = PBOverrideStatusFromLoadContext(serialized);
337338
WebSocketChannelParent* p = new WebSocketChannelParent(tabParent, loadContext,
338-
overrideStatus);
339+
overrideStatus,
340+
aSerial);
339341
p->AddRef();
340342
return p;
341343
}

netwerk/ipc/NeckoParent.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ class NeckoParent
124124
virtual bool DeallocPFTPChannelParent(PFTPChannelParent*) override;
125125
virtual PWebSocketParent*
126126
AllocPWebSocketParent(const PBrowserOrId& browser,
127-
const SerializedLoadContext& aSerialized) override;
127+
const SerializedLoadContext& aSerialized,
128+
const uint32_t& aSerial) override;
128129
virtual bool DeallocPWebSocketParent(PWebSocketParent*) override;
129130
virtual PTCPSocketParent* AllocPTCPSocketParent(const nsString& host,
130131
const uint16_t& port) override;

netwerk/ipc/PNecko.ipdl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ parent:
6868
PFTPChannel(PBrowserOrId browser, SerializedLoadContext loadContext,
6969
FTPChannelCreationArgs args);
7070

71-
PWebSocket(PBrowserOrId browser, SerializedLoadContext loadContext);
71+
PWebSocket(PBrowserOrId browser, SerializedLoadContext loadContext,
72+
uint32_t aSerialID);
7273
PTCPServerSocket(uint16_t localPort, uint16_t backlog, bool useArrayBuffers);
7374
PUDPSocket(Principal principal, nsCString filter);
7475

netwerk/protocol/websocket/BaseWebSocketChannel.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,25 @@
1414
#include "nsStandardURL.h"
1515
#include "LoadInfo.h"
1616
#include "nsIDOMNode.h"
17+
#include "mozilla/dom/ContentChild.h"
18+
19+
using mozilla::dom::ContentChild;
1720

1821
PRLogModuleInfo *webSocketLog = nullptr;
1922

2023
namespace mozilla {
2124
namespace net {
2225

26+
static uint64_t gNextWebSocketID = 0;
27+
28+
// We use only 53 bits for the WebSocket serial ID so that it can be converted
29+
// to and from a JS value without loss of precision. The upper bits of the
30+
// WebSocket serial ID hold the process ID. The lower bits identify the
31+
// WebSocket.
32+
static const uint64_t kWebSocketIDTotalBits = 53;
33+
static const uint64_t kWebSocketIDProcessBits = 22;
34+
static const uint64_t kWebSocketIDWebSocketBits = kWebSocketIDTotalBits - kWebSocketIDProcessBits;
35+
2336
BaseWebSocketChannel::BaseWebSocketChannel()
2437
: mWasOpened(0)
2538
, mClientSetPingInterval(0)
@@ -31,6 +44,24 @@ BaseWebSocketChannel::BaseWebSocketChannel()
3144
{
3245
if (!webSocketLog)
3346
webSocketLog = PR_NewLogModule("nsWebSocket");
47+
48+
// Generation of a unique serial ID.
49+
uint64_t processID = 0;
50+
if (XRE_IsContentProcess()) {
51+
ContentChild* cc = ContentChild::GetSingleton();
52+
processID = cc->GetID();
53+
}
54+
55+
uint64_t processBits = processID & ((uint64_t(1) << kWebSocketIDProcessBits) - 1);
56+
57+
// Make sure no actual webSocket ends up with mWebSocketID == 0 but less then
58+
// what the kWebSocketIDProcessBits allows.
59+
if (++gNextWebSocketID >= (uint64_t(1) << kWebSocketIDWebSocketBits)) {
60+
gNextWebSocketID = 1;
61+
}
62+
63+
uint64_t webSocketBits = gNextWebSocketID & ((uint64_t(1) << kWebSocketIDWebSocketBits) - 1);
64+
mSerial = (processBits << kWebSocketIDWebSocketBits) | webSocketBits;
3465
}
3566

3667
//-----------------------------------------------------------------------------
@@ -197,6 +228,24 @@ BaseWebSocketChannel::InitLoadInfo(nsIDOMNode* aLoadingNode,
197228
return NS_OK;
198229
}
199230

231+
NS_IMETHODIMP
232+
BaseWebSocketChannel::GetSerial(uint32_t* aSerial)
233+
{
234+
if (!aSerial) {
235+
return NS_ERROR_FAILURE;
236+
}
237+
238+
*aSerial = mSerial;
239+
return NS_OK;
240+
}
241+
242+
NS_IMETHODIMP
243+
BaseWebSocketChannel::SetSerial(uint32_t aSerial)
244+
{
245+
mSerial = aSerial;
246+
return NS_OK;
247+
}
248+
200249
//-----------------------------------------------------------------------------
201250
// BaseWebSocketChannel::nsIProtocolHandler
202251
//-----------------------------------------------------------------------------

netwerk/protocol/websocket/BaseWebSocketChannel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class BaseWebSocketChannel : public nsIWebSocketChannel,
5555
NS_IMETHOD InitLoadInfo(nsIDOMNode* aLoadingNode, nsIPrincipal* aLoadingPrincipal,
5656
nsIPrincipal* aTriggeringPrincipal, uint32_t aSecurityFlags,
5757
uint32_t aContentPolicyType) override;
58+
NS_IMETHOD GetSerial(uint32_t* aSerial) override;
59+
NS_IMETHOD SetSerial(uint32_t aSerial) override;
5860

5961
// Off main thread URI access.
6062
virtual void GetEffectiveURL(nsAString& aEffectiveURL) const = 0;
@@ -98,6 +100,8 @@ class BaseWebSocketChannel : public nsIWebSocketChannel,
98100

99101
uint32_t mPingInterval; /* milliseconds */
100102
uint32_t mPingResponseTimeout; /* milliseconds */
103+
104+
uint32_t mSerial;
101105
};
102106

103107
} // namespace net

netwerk/protocol/websocket/WebSocketChannel.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,6 @@ NS_IMPL_ISUPPORTS(OutboundEnqueuer, nsIRunnable)
11381138
// WebSocketChannel
11391139
//-----------------------------------------------------------------------------
11401140

1141-
uint32_t WebSocketChannel::sSerialSeed = 0;
1142-
11431141
WebSocketChannel::WebSocketChannel() :
11441142
mPort(0),
11451143
mCloseTimeout(20000),
@@ -1194,8 +1192,6 @@ WebSocketChannel::WebSocketChannel() :
11941192
if (NS_FAILED(rv))
11951193
LOG(("Failed to initiate dashboard service."));
11961194

1197-
mSerial = sSerialSeed++;
1198-
11991195
mService = WebSocketEventService::GetOrCreate();
12001196
}
12011197

netwerk/protocol/websocket/WebSocketChannel.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ class WebSocketChannel : public BaseWebSocketChannel,
294294
bool mPrivateBrowsing;
295295

296296
nsCOMPtr<nsIDashboardEventNotifier> mConnectionLogService;
297-
uint32_t mSerial;
298-
static uint32_t sSerialSeed;
299297

300298
// These members are used for network per-app metering (bug 855949)
301299
// Currently, they are only available on gonk.

netwerk/protocol/websocket/WebSocketChannelChild.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
489489
NS_ENSURE_SUCCESS(rv, rv);
490490

491491
gNeckoChild->SendPWebSocketConstructor(this, tabChild,
492-
IPC::SerializedLoadContext(this));
492+
IPC::SerializedLoadContext(this),
493+
mSerial);
493494
if (!SendAsyncOpen(uri, nsCString(aOrigin), aInnerWindowID, mProtocol,
494495
mEncrypted, mPingInterval, mClientSetPingInterval,
495496
mPingResponseTimeout, mClientSetPingTimeout, loadInfoArgs)) {

netwerk/protocol/websocket/WebSocketChannelParent.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ NS_IMPL_ISUPPORTS(WebSocketChannelParent,
2626

2727
WebSocketChannelParent::WebSocketChannelParent(nsIAuthPromptProvider* aAuthProvider,
2828
nsILoadContext* aLoadContext,
29-
PBOverrideStatus aOverrideStatus)
29+
PBOverrideStatus aOverrideStatus,
30+
uint32_t aSerial)
3031
: mAuthProvider(aAuthProvider)
3132
, mLoadContext(aLoadContext)
3233
, mIPCOpen(true)
34+
, mSerial(aSerial)
3335
{
3436
// Websocket channels can't have a private browsing override
3537
MOZ_ASSERT_IF(!aLoadContext, aOverrideStatus == kPBOverride_Unset);
@@ -95,6 +97,11 @@ WebSocketChannelParent::RecvAsyncOpen(const URIParams& aURI,
9597
if (NS_FAILED(rv))
9698
goto fail;
9799

100+
rv = mChannel->SetSerial(mSerial);
101+
if (NS_WARN_IF(NS_FAILED(rv))) {
102+
goto fail;
103+
}
104+
98105
rv = LoadInfoArgsToLoadInfo(aLoadInfoArgs, getter_AddRefs(loadInfo));
99106
if (NS_FAILED(rv))
100107
goto fail;

netwerk/protocol/websocket/WebSocketChannelParent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class WebSocketChannelParent : public PWebSocketParent,
3535

3636
WebSocketChannelParent(nsIAuthPromptProvider* aAuthProvider,
3737
nsILoadContext* aLoadContext,
38-
PBOverrideStatus aOverrideStatus);
38+
PBOverrideStatus aOverrideStatus,
39+
uint32_t aSerial);
3940

4041
private:
4142
bool RecvAsyncOpen(const URIParams& aURI,
@@ -66,6 +67,7 @@ class WebSocketChannelParent : public PWebSocketParent,
6667
nsCOMPtr<nsILoadContext> mLoadContext;
6768
bool mIPCOpen;
6869

70+
uint32_t mSerial;
6971
};
7072

7173
} // namespace net

netwerk/protocol/websocket/nsIWebSocketChannel.idl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface nsIPrincipal;
2222
* We are also making it scriptable for now, but this may change once we have
2323
* WebSockets for Workers.
2424
*/
25-
[scriptable, uuid(352e0c08-f14c-40c8-ad06-2f8bb5d9d9e0)]
25+
[scriptable, uuid(1bfc252b-ad46-413c-860b-2079bf1399c7)]
2626
interface nsIWebSocketChannel : nsISupports
2727
{
2828
/**
@@ -223,4 +223,9 @@ interface nsIWebSocketChannel : nsISupports
223223
*/
224224
attribute unsigned long pingTimeout;
225225

226+
/**
227+
* Unique ID for this channel. It's not readonly because when the channel is
228+
* created via IPC, the serial number is received from the child process.
229+
*/
230+
attribute unsigned long serial;
226231
};

0 commit comments

Comments
 (0)