2011-05-04 06:36:23 -07: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 04:12:37 -07: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 06:36:23 -07:00
|
|
|
|
|
|
|
#include "WebSocketLog.h"
|
|
|
|
#include "WebSocketChannelParent.h"
|
|
|
|
#include "nsIAuthPromptProvider.h"
|
2012-08-22 19:13:54 -07:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2013-10-18 13:57:55 -07:00
|
|
|
#include "SerializedLoadContext.h"
|
2012-08-22 19:13:54 -07:00
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2011-05-04 06:36:23 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS2(WebSocketChannelParent,
|
2011-05-04 06:36:23 -07:00
|
|
|
nsIWebSocketListener,
|
|
|
|
nsIInterfaceRequestor)
|
|
|
|
|
2012-12-29 01:02:16 -08:00
|
|
|
WebSocketChannelParent::WebSocketChannelParent(nsIAuthPromptProvider* aAuthProvider,
|
|
|
|
nsILoadContext* aLoadContext,
|
|
|
|
PBOverrideStatus aOverrideStatus)
|
2011-05-04 06:36:23 -07:00
|
|
|
: mAuthProvider(aAuthProvider)
|
2012-12-29 01:02:16 -08:00
|
|
|
, mLoadContext(aLoadContext)
|
2011-05-04 06:36:23 -07:00
|
|
|
, mIPCOpen(true)
|
|
|
|
{
|
2012-12-29 01:02:16 -08:00
|
|
|
// Websocket channels can't have a private browsing override
|
|
|
|
MOZ_ASSERT_IF(!aLoadContext, aOverrideStatus == kPBOverride_Unset);
|
2011-05-04 06:36:23 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!webSocketLog)
|
|
|
|
webSocketLog = PR_NewLogModule("nsWebSocket");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-07-22 15:35:33 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::PWebSocketChannelParent
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-05-04 06:36:23 -07:00
|
|
|
bool
|
|
|
|
WebSocketChannelParent::RecvDeleteSelf()
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvDeleteSelf() %p\n", this));
|
2012-07-30 07:20:58 -07:00
|
|
|
mChannel = nullptr;
|
|
|
|
mAuthProvider = nullptr;
|
2011-05-04 06:36:23 -07:00
|
|
|
return mIPCOpen ? Send__delete__(this) : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-23 12:33:46 -07:00
|
|
|
WebSocketChannelParent::RecvAsyncOpen(const URIParams& aURI,
|
2011-05-04 06:36:23 -07:00
|
|
|
const nsCString& aOrigin,
|
|
|
|
const nsCString& aProtocol,
|
2013-03-28 15:52:16 -07:00
|
|
|
const bool& aSecure,
|
|
|
|
const uint32_t& aPingInterval,
|
|
|
|
const bool& aClientSetPingInterval,
|
|
|
|
const uint32_t& aPingTimeout,
|
|
|
|
const bool& aClientSetPingTimeout)
|
2011-05-04 06:36:23 -07:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvAsyncOpen() %p\n", this));
|
2012-08-23 12:33:46 -07:00
|
|
|
|
2011-05-04 06:36:23 -07:00
|
|
|
nsresult rv;
|
2012-08-23 12:33:46 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
2011-05-04 06:36:23 -07: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 16:55:17 -07:00
|
|
|
goto fail;
|
2011-05-04 06:36:23 -07:00
|
|
|
|
|
|
|
rv = mChannel->SetNotificationCallbacks(this);
|
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 16:55:17 -07:00
|
|
|
goto fail;
|
2011-05-04 06:36:23 -07:00
|
|
|
|
|
|
|
rv = mChannel->SetProtocol(aProtocol);
|
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 16:55:17 -07:00
|
|
|
goto fail;
|
2011-05-04 06:36:23 -07:00
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
uri = DeserializeURI(aURI);
|
|
|
|
if (!uri) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-03-28 15:52:16 -07:00
|
|
|
// only use ping values from child if they were overridden by client code.
|
|
|
|
if (aClientSetPingInterval) {
|
2013-04-05 13:52:12 -07:00
|
|
|
// IDL allows setting in seconds, so must be multiple of 1000 ms
|
|
|
|
MOZ_ASSERT(aPingInterval >= 1000 && !(aPingInterval % 1000));
|
|
|
|
mChannel->SetPingInterval(aPingInterval / 1000);
|
2013-03-28 15:52:16 -07:00
|
|
|
}
|
|
|
|
if (aClientSetPingTimeout) {
|
2013-04-05 13:52:12 -07:00
|
|
|
MOZ_ASSERT(aPingTimeout >= 1000 && !(aPingTimeout % 1000));
|
|
|
|
mChannel->SetPingTimeout(aPingTimeout / 1000);
|
2013-03-28 15:52:16 -07:00
|
|
|
}
|
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
rv = mChannel->AsyncOpen(uri, aOrigin, this, nullptr);
|
2011-05-04 06:36:23 -07:00
|
|
|
if (NS_FAILED(rv))
|
2011-08-02 16:55:17 -07:00
|
|
|
goto fail;
|
2011-05-04 06:36:23 -07:00
|
|
|
|
|
|
|
return true;
|
2011-08-02 16:55:17 -07:00
|
|
|
|
|
|
|
fail:
|
2012-07-30 07:20:58 -07:00
|
|
|
mChannel = nullptr;
|
2011-08-02 16:55:17 -07:00
|
|
|
return SendOnStop(rv);
|
2011-05-04 06:36:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
WebSocketChannelParent::RecvClose(const uint16_t& code, const nsCString& reason)
|
2011-05-04 06:36:23 -07:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvClose() %p\n", this));
|
|
|
|
if (mChannel) {
|
2011-08-03 12:15:25 -07:00
|
|
|
nsresult rv = mChannel->Close(code, reason);
|
2011-05-04 06:36:23 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebSocketChannelParent::RecvSendMsg(const nsCString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendMsg() %p\n", this));
|
|
|
|
if (mChannel) {
|
|
|
|
nsresult rv = mChannel->SendMsg(aMsg);
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebSocketChannelParent::RecvSendBinaryMsg(const nsCString& aMsg)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendBinaryMsg() %p\n", this));
|
|
|
|
if (mChannel) {
|
|
|
|
nsresult rv = mChannel->SendBinaryMsg(aMsg);
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:20:17 -08:00
|
|
|
bool
|
2012-08-22 19:13:54 -07:00
|
|
|
WebSocketChannelParent::RecvSendBinaryStream(const InputStreamParams& aStream,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t& aLength)
|
2011-12-15 15:20:17 -08:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::RecvSendBinaryStream() %p\n", this));
|
|
|
|
if (mChannel) {
|
2012-08-22 19:13:54 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeInputStream(aStream);
|
|
|
|
if (!stream) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsresult rv = mChannel->SendBinaryStream(stream, aLength);
|
2011-12-15 15:20:17 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-22 15:35:33 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::nsIRequestObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-05-04 06:36:23 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketChannelParent::OnStart(nsISupports *aContext)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnStart() %p\n", this));
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString protocol, extensions;
|
2011-05-04 06:36:23 -07:00
|
|
|
if (mChannel) {
|
|
|
|
mChannel->GetProtocol(protocol);
|
2011-08-03 20:46:13 -07:00
|
|
|
mChannel->GetExtensions(extensions);
|
2011-05-04 06:36:23 -07:00
|
|
|
}
|
2011-08-03 20:46:13 -07:00
|
|
|
if (!mIPCOpen || !SendOnStart(protocol, extensions)) {
|
2011-05-04 06:36:23 -07: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 08:56:38 -07:00
|
|
|
WebSocketChannelParent::OnAcknowledge(nsISupports *aContext, uint32_t aSize)
|
2011-05-04 06:36:23 -07:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnAcknowledge() %p\n", this));
|
|
|
|
if (!mIPCOpen || !SendOnAcknowledge(aSize)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-08-03 12:15:25 -07:00
|
|
|
WebSocketChannelParent::OnServerClose(nsISupports *aContext,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t code, const nsACString & reason)
|
2011-05-04 06:36:23 -07:00
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::OnServerClose() %p\n", this));
|
2011-08-03 12:15:25 -07:00
|
|
|
if (!mIPCOpen || !SendOnServerClose(code, nsCString(reason))) {
|
2011-05-04 06:36:23 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketChannelParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
LOG(("WebSocketChannelParent::ActorDestroy() %p\n", this));
|
|
|
|
mIPCOpen = false;
|
|
|
|
}
|
|
|
|
|
2012-07-22 15:35:33 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// WebSocketChannelParent::nsIInterfaceRequestor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-06 13:14:07 -07:00
|
|
|
WebSocketChannelParent::GetInterface(const nsIID & iid, void **result)
|
2012-07-22 15:35:33 -07: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-06 21:47:48 -07:00
|
|
|
if (iid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) {
|
|
|
|
NS_ADDREF(mLoadContext);
|
|
|
|
*result = static_cast<nsILoadContext*>(mLoadContext);
|
|
|
|
return NS_OK;
|
2012-07-22 15:35:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return QueryInterface(iid, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-04 06:36:23 -07:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|