2011-05-04 13:36:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
#include "WebSocketLog.h"
|
|
|
|
#include "WebSocketChannelParent.h"
|
|
|
|
#include "nsIAuthPromptProvider.h"
|
2012-08-23 02:13:54 +00:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2012-08-23 19:33:46 +00:00
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2015-02-17 18:09:58 +00:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2013-10-18 20:57:55 +00:00
|
|
|
#include "SerializedLoadContext.h"
|
2014-08-23 03:05:56 +00:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
2014-04-17 18:53:58 +00:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2012-08-23 02:13:54 +00:00
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(WebSocketChannelParent,
|
|
|
|
nsIWebSocketListener,
|
|
|
|
nsIInterfaceRequestor)
|
2011-05-04 13:36:23 +00:00
|
|
|
|
2012-12-29 09:02:16 +00:00
|
|
|
WebSocketChannelParent::WebSocketChannelParent(nsIAuthPromptProvider* aAuthProvider,
|
|
|
|
nsILoadContext* aLoadContext,
|
2015-10-28 19:10:42 +00:00
|
|
|
PBOverrideStatus aOverrideStatus,
|
|
|
|
uint32_t aSerial)
|
2011-05-04 13:36:23 +00:00
|
|
|
: mAuthProvider(aAuthProvider)
|
2012-12-29 09:02:16 +00:00
|
|
|
, mLoadContext(aLoadContext)
|
2011-05-04 13:36:23 +00:00
|
|
|
, mIPCOpen(true)
|
2015-10-28 19:10:42 +00:00
|
|
|
, mSerial(aSerial)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
2012-12-29 09:02:16 +00:00
|
|
|
// Websocket channels can't have a private browsing override
|
|
|
|
MOZ_ASSERT_IF(!aLoadContext, aOverrideStatus == kPBOverride_Unset);
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
2012-07-22 22:35:33 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::PWebSocketChannelParent
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2011-05-04 13:36:23 +00:00
|
|
|
WebSocketChannelParent::RecvDeleteSelf()
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvDeleteSelf() %p\n", this));
|
2012-07-30 14:20:58 +00:00
|
|
|
mChannel = nullptr;
|
|
|
|
mAuthProvider = nullptr;
|
2016-12-13 20:09:21 +00:00
|
|
|
IProtocol* mgr = Manager();
|
2016-11-15 03:26:00 +00:00
|
|
|
if (mIPCOpen && !Send__delete__(this)) {
|
2016-12-13 20:09:21 +00:00
|
|
|
return IPC_FAIL_NO_REASON(mgr);
|
2016-11-15 03:26:00 +00:00
|
|
|
}
|
|
|
|
return IPC_OK();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2016-06-07 09:46:03 +00:00
|
|
|
WebSocketChannelParent::RecvAsyncOpen(const OptionalURIParams& aURI,
|
2011-05-04 13:36:23 +00:00
|
|
|
const nsCString& aOrigin,
|
2015-10-26 15:29:28 +00:00
|
|
|
const uint64_t& aInnerWindowID,
|
2011-05-04 13:36:23 +00:00
|
|
|
const nsCString& aProtocol,
|
2013-03-28 22:52:16 +00:00
|
|
|
const bool& aSecure,
|
|
|
|
const uint32_t& aPingInterval,
|
|
|
|
const bool& aClientSetPingInterval,
|
|
|
|
const uint32_t& aPingTimeout,
|
2015-02-17 18:09:58 +00:00
|
|
|
const bool& aClientSetPingTimeout,
|
2016-06-07 09:46:03 +00:00
|
|
|
const OptionalLoadInfoArgs& aLoadInfoArgs,
|
|
|
|
const OptionalTransportProvider& aTransportProvider,
|
|
|
|
const nsCString& aNegotiatedExtensions)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvAsyncOpen() %p\n", this));
|
2012-08-23 19:33:46 +00:00
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
nsresult rv;
|
2012-08-23 19:33:46 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2015-02-17 18:09:58 +00:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo;
|
2016-08-23 11:20:00 +00:00
|
|
|
|
|
|
|
rv = LoadInfoArgsToLoadInfo(aLoadInfoArgs, getter_AddRefs(loadInfo));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
if (aSecure) {
|
|
|
|
mChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=wss", &rv);
|
|
|
|
} else {
|
|
|
|
mChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=ws", &rv);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 23:55:17 +00:00
|
|
|
goto fail;
|
2011-05-04 13:36:23 +00:00
|
|
|
|
2015-10-28 19:10:42 +00:00
|
|
|
rv = mChannel->SetSerial(mSerial);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:09:58 +00:00
|
|
|
rv = mChannel->SetLoadInfo(loadInfo);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
rv = mChannel->SetNotificationCallbacks(this);
|
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 23:55:17 +00:00
|
|
|
goto fail;
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
rv = mChannel->SetProtocol(aProtocol);
|
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 23:55:17 +00:00
|
|
|
goto fail;
|
2011-05-04 13:36:23 +00:00
|
|
|
|
2016-06-07 09:46:03 +00:00
|
|
|
if (aTransportProvider.type() != OptionalTransportProvider::Tvoid_t) {
|
|
|
|
RefPtr<TransportProviderParent> provider =
|
|
|
|
static_cast<TransportProviderParent*>(
|
|
|
|
aTransportProvider.get_PTransportProviderParent());
|
|
|
|
rv = mChannel->SetServerParameters(provider, aNegotiatedExtensions);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uri = DeserializeURI(aURI);
|
|
|
|
if (!uri) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-08-23 19:33:46 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 22:52:16 +00:00
|
|
|
// only use ping values from child if they were overridden by client code.
|
|
|
|
if (aClientSetPingInterval) {
|
2013-04-05 20:52:12 +00:00
|
|
|
// IDL allows setting in seconds, so must be multiple of 1000 ms
|
|
|
|
MOZ_ASSERT(aPingInterval >= 1000 && !(aPingInterval % 1000));
|
2016-11-23 10:00:42 +00:00
|
|
|
DebugOnly<nsresult> rv = mChannel->SetPingInterval(aPingInterval / 1000);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2013-03-28 22:52:16 +00:00
|
|
|
}
|
|
|
|
if (aClientSetPingTimeout) {
|
2013-04-05 20:52:12 +00:00
|
|
|
MOZ_ASSERT(aPingTimeout >= 1000 && !(aPingTimeout % 1000));
|
2016-11-23 10:00:42 +00:00
|
|
|
DebugOnly<nsresult> rv = mChannel->SetPingTimeout(aPingTimeout / 1000);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2013-03-28 22:52:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 15:29:28 +00:00
|
|
|
rv = mChannel->AsyncOpen(uri, aOrigin, aInnerWindowID, this, nullptr);
|
2011-05-04 13:36:23 +00:00
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 23:55:17 +00:00
|
|
|
goto fail;
|
2011-05-04 13:36:23 +00:00
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2011-08-02 23:55:17 +00:00
|
|
|
|
|
|
|
fail:
|
2012-07-30 14:20:58 +00:00
|
|
|
mChannel = nullptr;
|
2016-11-15 03:26:00 +00:00
|
|
|
if (!SendOnStop(rv)) {
|
|
|
|
return IPC_FAIL_NO_REASON(this);
|
|
|
|
}
|
|
|
|
return IPC_OK();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2012-08-22 15:56:38 +00:00
|
|
|
WebSocketChannelParent::RecvClose(const uint16_t& code, const nsCString& reason)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvClose() %p\n", this));
|
|
|
|
if (mChannel) {
|
2011-08-03 19:15:25 +00:00
|
|
|
nsresult rv = mChannel->Close(code, reason);
|
2016-11-15 03:26:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, IPC_OK());
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
2014-08-23 03:05:56 +00:00
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2011-05-04 13:36:23 +00:00
|
|
|
WebSocketChannelParent::RecvSendMsg(const nsCString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendMsg() %p\n", this));
|
|
|
|
if (mChannel) {
|
|
|
|
nsresult rv = mChannel->SendMsg(aMsg);
|
2016-11-15 03:26:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, IPC_OK());
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2011-05-04 13:36:23 +00:00
|
|
|
WebSocketChannelParent::RecvSendBinaryMsg(const nsCString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendBinaryMsg() %p\n", this));
|
|
|
|
if (mChannel) {
|
|
|
|
nsresult rv = mChannel->SendBinaryMsg(aMsg);
|
2016-11-15 03:26:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, IPC_OK());
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 03:26:00 +00:00
|
|
|
mozilla::ipc::IPCResult
|
2017-03-09 13:10:24 +00:00
|
|
|
WebSocketChannelParent::RecvSendBinaryStream(const IPCStream& aStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t& aLength)
|
2011-12-15 23:20:17 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendBinaryStream() %p\n", this));
|
|
|
|
if (mChannel) {
|
2017-03-09 13:10:24 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aStream);
|
2012-08-23 02:13:54 +00:00
|
|
|
if (!stream) {
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2012-08-23 02:13:54 +00:00
|
|
|
}
|
|
|
|
nsresult rv = mChannel->SendBinaryStream(stream, aLength);
|
2016-11-15 03:26:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, IPC_OK());
|
2011-12-15 23:20:17 +00:00
|
|
|
}
|
2016-11-15 03:26:00 +00:00
|
|
|
return IPC_OK();
|
2011-12-15 23:20:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 22:35:33 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::nsIRequestObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-04 13:36:23 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketChannelParent::OnStart(nsISupports *aContext)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnStart() %p\n", this));
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString protocol, extensions;
|
2014-04-17 18:53:58 +00:00
|
|
|
nsString effectiveURL;
|
|
|
|
bool encrypted = false;
|
2011-05-04 13:36:23 +00:00
|
|
|
if (mChannel) {
|
2016-11-23 10:00:42 +00:00
|
|
|
DebugOnly<nsresult> rv = mChannel->GetProtocol(protocol);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
rv = mChannel->GetExtensions(extensions);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2014-04-17 18:53:58 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketChannel> channel;
|
2014-04-17 18:53:58 +00:00
|
|
|
channel = static_cast<WebSocketChannel*>(mChannel.get());
|
|
|
|
MOZ_ASSERT(channel);
|
|
|
|
|
|
|
|
channel->GetEffectiveURL(effectiveURL);
|
|
|
|
encrypted = channel->IsEncrypted();
|
2011-05-04 13:36:23 +00:00
|
|
|
}
|
2014-04-17 18:53:58 +00:00
|
|
|
if (!mIPCOpen || !SendOnStart(protocol, extensions, effectiveURL, encrypted)) {
|
2011-05-04 13:36:23 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketChannelParent::OnStop(nsISupports *aContext, nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnStop() %p\n", this));
|
|
|
|
if (!mIPCOpen || !SendOnStop(aStatusCode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketChannelParent::OnMessageAvailable(nsISupports *aContext, const nsACString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnMessageAvailable() %p\n", this));
|
|
|
|
if (!mIPCOpen || !SendOnMessageAvailable(nsCString(aMsg))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketChannelParent::OnBinaryMessageAvailable(nsISupports *aContext, const nsACString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnBinaryMessageAvailable() %p\n", this));
|
|
|
|
if (!mIPCOpen || !SendOnBinaryMessageAvailable(nsCString(aMsg))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
WebSocketChannelParent::OnAcknowledge(nsISupports *aContext, uint32_t aSize)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnAcknowledge() %p\n", this));
|
|
|
|
if (!mIPCOpen || !SendOnAcknowledge(aSize)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-08-03 19:15:25 +00:00
|
|
|
WebSocketChannelParent::OnServerClose(nsISupports *aContext,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t code, const nsACString & reason)
|
2011-05-04 13:36:23 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnServerClose() %p\n", this));
|
2011-08-03 19:15:25 +00:00
|
|
|
if (!mIPCOpen || !SendOnServerClose(code, nsCString(reason))) {
|
2011-05-04 13:36:23 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketChannelParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::ActorDestroy() %p\n", this));
|
2018-05-23 12:22:00 +00:00
|
|
|
|
|
|
|
// Make sure we close the channel if the content process dies without going
|
|
|
|
// through a clean shutdown.
|
|
|
|
if (mChannel) {
|
|
|
|
Unused << mChannel->Close(nsIWebSocketChannel::CLOSE_GOING_AWAY,
|
|
|
|
NS_LITERAL_CSTRING("Child was killed"));
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
mIPCOpen = false;
|
|
|
|
}
|
|
|
|
|
2012-07-22 22:35:33 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::nsIInterfaceRequestor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-06 20:14:07 +00:00
|
|
|
WebSocketChannelParent::GetInterface(const nsIID & iid, void **result)
|
2012-07-22 22:35:33 +00:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::GetInterface() %p\n", this));
|
|
|
|
if (mAuthProvider && iid.Equals(NS_GET_IID(nsIAuthPromptProvider)))
|
|
|
|
return mAuthProvider->GetAuthPrompt(nsIAuthPromptProvider::PROMPT_NORMAL,
|
|
|
|
iid, result);
|
|
|
|
|
|
|
|
// Only support nsILoadContext if child channel's callbacks did too
|
2012-08-07 04:47:48 +00:00
|
|
|
if (iid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) {
|
2015-04-19 17:22:35 +00:00
|
|
|
nsCOMPtr<nsILoadContext> copy = mLoadContext;
|
|
|
|
copy.forget(result);
|
2012-08-07 04:47:48 +00:00
|
|
|
return NS_OK;
|
2012-07-22 22:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QueryInterface(iid, result);
|
|
|
|
}
|
|
|
|
|
2011-05-04 13:36:23 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|