Bug 1660361 - Send data in chunks from WebSocketChannelParent to WebSocketChannelChild r=valentin

Differential Revision: https://phabricator.services.mozilla.com/D88155
This commit is contained in:
Kershaw Chang 2020-08-28 08:04:15 +00:00
Родитель d5b3baf87c
Коммит 1bc13dd88d
6 изменённых файлов: 129 добавлений и 14 удалений

Просмотреть файл

@ -50,3 +50,4 @@ support-files = websocket_loadgroup_worker.js
support-files = webSocket_sharedWorker.js
[test_websocket_bigBlob.html]
support-files = file_websocket_bigBlob_wsh.py
[test_webSocket_longString.html]

Просмотреть файл

@ -0,0 +1,48 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
<title>WebSocket test - big blob on content side</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="websocket_helpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
var ws = new WebSocket("ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_basic", "test");
is(ws.readyState, 0, "Initial readyState is 0");
const longString = new Array(1024*1024).join('123456789ABCDEF');
ws.onopen = function(e) {
is(this, ws, "[onopen()] 'this' should point to the WebSocket.");
ws.send(longString);
};
ws.onclose = function(e) {
is(this, ws, "[onclose()] 'this' should point to the WebSocket.");
ok(e.wasClean, "Connection closed cleanly");
SimpleTest.executeSoon(SimpleTest.finish);
};
ws.onerror = function(e) {
is(this, ws, "[onerror()] 'this' should point to the WebSocket.");
ok(false, "onerror()] should not have been called!");
SimpleTest.executeSoon(SimpleTest.finish);
};
ws.onmessage = function(e) {
is(this, ws, "[onmessage()] 'this' should point to the WebSocket.");
// Do not use |is(e.data, longString, "...");| that results in a _very_ long line.
is(e.data.length, longString.length, "Length of received message");
ok(e.data === longString, "Content of received message");
this.close();
};
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>

Просмотреть файл

@ -53,8 +53,8 @@ child:
nsString aEffectiveURL, bool aEncrypted,
uint64_t aHttpChannelId);
async OnStop(nsresult aStatusCode);
async OnMessageAvailable(nsCString aMsg);
async OnBinaryMessageAvailable(nsCString aMsg);
async OnMessageAvailable(nsDependentCSubstring aMsg, bool aMoreData);
async OnBinaryMessageAvailable(nsDependentCSubstring aMsg, bool aMoreData);
async OnAcknowledge(uint32_t aSize);
async OnServerClose(uint16_t code, nsCString aReason);

Просмотреть файл

@ -294,11 +294,22 @@ class MessageEvent : public WebSocketEvent {
bool mBinary;
};
mozilla::ipc::IPCResult WebSocketChannelChild::RecvOnMessageAvailable(
const nsCString& aMsg) {
mEventQ->RunOrEnqueue(new EventTargetDispatcher(
this, new MessageEvent(aMsg, false), mTargetThread));
void WebSocketChannelChild::RecvOnMessageAvailableInternal(
const nsDependentCSubstring& aMsg, bool aMoreData, bool aBinary) {
if (aMoreData) {
mReceivedMsgBuffer.Append(aMsg);
return;
}
mReceivedMsgBuffer.Append(aMsg);
mEventQ->RunOrEnqueue(new EventTargetDispatcher(
this, new MessageEvent(mReceivedMsgBuffer, aBinary), mTargetThread));
mReceivedMsgBuffer.Truncate();
}
mozilla::ipc::IPCResult WebSocketChannelChild::RecvOnMessageAvailable(
const nsDependentCSubstring& aMsg, const bool& aMoreData) {
RecvOnMessageAvailableInternal(aMsg, aMoreData, false);
return IPC_OK();
}
@ -319,10 +330,8 @@ void WebSocketChannelChild::OnMessageAvailable(const nsCString& aMsg) {
}
mozilla::ipc::IPCResult WebSocketChannelChild::RecvOnBinaryMessageAvailable(
const nsCString& aMsg) {
mEventQ->RunOrEnqueue(new EventTargetDispatcher(
this, new MessageEvent(aMsg, true), mTargetThread));
const nsDependentCSubstring& aMsg, const bool& aMoreData) {
RecvOnMessageAvailableInternal(aMsg, aMoreData, true);
return IPC_OK();
}

Просмотреть файл

@ -57,8 +57,10 @@ class WebSocketChannelChild final : public BaseWebSocketChannel,
const bool& aSecure,
const uint64_t& aHttpChannelId);
mozilla::ipc::IPCResult RecvOnStop(const nsresult& aStatusCode);
mozilla::ipc::IPCResult RecvOnMessageAvailable(const nsCString& aMsg);
mozilla::ipc::IPCResult RecvOnBinaryMessageAvailable(const nsCString& aMsg);
mozilla::ipc::IPCResult RecvOnMessageAvailable(
const nsDependentCSubstring& aMsg, const bool& aMoreData);
mozilla::ipc::IPCResult RecvOnBinaryMessageAvailable(
const nsDependentCSubstring& aMsg, const bool& aMoreData);
mozilla::ipc::IPCResult RecvOnAcknowledge(const uint32_t& aSize);
mozilla::ipc::IPCResult RecvOnServerClose(const uint16_t& aCode,
const nsCString& aReason);
@ -80,8 +82,12 @@ class WebSocketChannelChild final : public BaseWebSocketChannel,
// This function tries to get a labeled event target for |mNeckoTarget|.
void SetupNeckoTarget();
void RecvOnMessageAvailableInternal(const nsDependentCSubstring& aMsg,
bool aMoreData, bool aBinary);
RefPtr<ChannelEventQueue> mEventQ;
nsString mEffectiveURL;
nsCString mReceivedMsgBuffer;
// This variable is protected by mutex.
enum { Opened, Closing, Closed } mIPCState;

Просмотреть файл

@ -217,13 +217,53 @@ WebSocketChannelParent::OnStop(nsISupports* aContext, nsresult aStatusCode) {
return NS_OK;
}
static bool SendOnMessageAvailableHelper(
const nsACString& aMsg,
const std::function<bool(const nsDependentCSubstring&, bool)>& aSendFunc) {
// To avoid the crash caused by too large IPC message, we have to split the
// data in small chunks and send them to child process. Note that the chunk
// size used here is the same as what we used for PHttpChannel.
static uint32_t const kCopyChunkSize = 128 * 1024;
uint32_t count = aMsg.Length();
if (count <= kCopyChunkSize) {
return aSendFunc(nsDependentCSubstring(aMsg), false);
}
uint32_t start = 0;
uint32_t toRead = std::min<uint32_t>(count, kCopyChunkSize);
while (count) {
nsDependentCSubstring data(Substring(aMsg, start, toRead));
if (!aSendFunc(data, count > kCopyChunkSize)) {
return false;
}
start += toRead;
count -= toRead;
toRead = std::min<uint32_t>(count, kCopyChunkSize);
}
return true;
}
NS_IMETHODIMP
WebSocketChannelParent::OnMessageAvailable(nsISupports* aContext,
const nsACString& aMsg) {
LOG(("WebSocketChannelParent::OnMessageAvailable() %p\n", this));
if (!CanRecv() || !SendOnMessageAvailable(nsCString(aMsg))) {
if (!CanRecv()) {
return NS_ERROR_FAILURE;
}
auto sendFunc = [self = UnsafePtr<WebSocketChannelParent>(this)](
const nsDependentCSubstring& aMsg, bool aMoreData) {
return self->SendOnMessageAvailable(aMsg, aMoreData);
};
if (!SendOnMessageAvailableHelper(aMsg, sendFunc)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
@ -231,9 +271,20 @@ NS_IMETHODIMP
WebSocketChannelParent::OnBinaryMessageAvailable(nsISupports* aContext,
const nsACString& aMsg) {
LOG(("WebSocketChannelParent::OnBinaryMessageAvailable() %p\n", this));
if (!CanRecv() || !SendOnBinaryMessageAvailable(nsCString(aMsg))) {
if (!CanRecv()) {
return NS_ERROR_FAILURE;
}
auto sendFunc = [self = UnsafePtr<WebSocketChannelParent>(this)](
const nsDependentCSubstring& aMsg, bool aMoreData) {
return self->SendOnBinaryMessageAvailable(aMsg, aMoreData);
};
if (!SendOnMessageAvailableHelper(aMsg, sendFunc)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}