2012-09-24 18:53:49 +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/. */
|
|
|
|
|
2013-07-29 17:36:43 +00:00
|
|
|
#include <algorithm>
|
2012-09-24 18:53:49 +00:00
|
|
|
#include "TCPSocketChild.h"
|
2013-07-29 17:36:43 +00:00
|
|
|
#include "mozilla/unused.h"
|
2012-09-24 18:53:49 +00:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
|
|
|
#include "mozilla/dom/PBrowserChild.h"
|
|
|
|
#include "mozilla/dom/TabChild.h"
|
|
|
|
#include "nsIDOMTCPSocket.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-04-23 15:12:53 +00:00
|
|
|
#include "jswrapper.h"
|
2012-09-24 18:53:49 +00:00
|
|
|
|
|
|
|
using mozilla::net::gNeckoChild;
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
bool
|
2013-05-11 02:39:45 +00:00
|
|
|
DeserializeArrayBuffer(JS::Handle<JSObject*> aObj,
|
2013-04-19 09:25:33 +00:00
|
|
|
const InfallibleTArray<uint8_t>& aBuffer,
|
2013-05-11 02:39:45 +00:00
|
|
|
JS::MutableHandle<JS::Value> aVal)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
2013-05-30 14:54:46 +00:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
2012-09-24 18:53:49 +00:00
|
|
|
JSAutoCompartment ac(cx, aObj);
|
|
|
|
|
2013-05-04 07:52:57 +00:00
|
|
|
JS::Rooted<JSObject*> obj(cx, JS_NewArrayBuffer(cx, aBuffer.Length()));
|
2012-09-24 18:53:49 +00:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
2012-11-14 17:56:26 +00:00
|
|
|
uint8_t* data = JS_GetArrayBufferData(obj);
|
2012-09-24 18:53:49 +00:00
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
memcpy(data, aBuffer.Elements(), aBuffer.Length());
|
2013-05-11 02:39:45 +00:00
|
|
|
aVal.set(OBJECT_TO_JSVAL(obj));
|
2012-09-24 18:53:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace IPC
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(TCPSocketChildBase, mSocket)
|
2012-09-24 18:53:49 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketChildBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketChildBase)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketChildBase)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITCPSocketChild)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
TCPSocketChildBase::TCPSocketChildBase()
|
|
|
|
: mIPCOpen(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketChildBase::~TCPSocketChildBase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-27 20:38:33 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketChild::Release(void)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
|
|
|
nsrefcnt refcnt = TCPSocketChildBase::Release();
|
|
|
|
if (refcnt == 1 && mIPCOpen) {
|
|
|
|
PTCPSocketChild::SendRequestDelete();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return refcnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketChild::TCPSocketChild()
|
2013-07-29 17:36:43 +00:00
|
|
|
: mWindowObj(nullptr)
|
2013-12-19 03:21:12 +00:00
|
|
|
, mHost()
|
|
|
|
, mPort(0)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-19 03:21:12 +00:00
|
|
|
void TCPSocketChild::Init(const nsString& aHost, const uint16_t& aPort) {
|
|
|
|
mHost = aHost;
|
|
|
|
mPort = aPort;
|
|
|
|
}
|
|
|
|
|
2012-09-24 18:53:49 +00:00
|
|
|
NS_IMETHODIMP
|
2013-11-14 07:59:55 +00:00
|
|
|
TCPSocketChild::SendOpen(nsITCPSocketInternal* aSocket,
|
|
|
|
const nsAString& aHost, uint16_t aPort,
|
|
|
|
bool aUseSSL, const nsAString& aBinaryType,
|
2014-01-09 17:39:36 +00:00
|
|
|
nsIDOMWindow* aWindow, JS::Handle<JS::Value> aWindowObj,
|
2013-11-14 07:59:55 +00:00
|
|
|
JSContext* aCx)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
|
|
|
mSocket = aSocket;
|
2013-07-29 17:36:43 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(aWindowObj.isObject());
|
|
|
|
mWindowObj = js::CheckedUnwrap(&aWindowObj.toObject());
|
|
|
|
if (!mWindowObj) {
|
2013-04-23 15:12:53 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-24 18:53:49 +00:00
|
|
|
AddIPDLReference();
|
2013-12-19 03:21:12 +00:00
|
|
|
gNeckoChild->SendPTCPSocketConstructor(this, nsString(aHost), aPort);
|
2013-11-14 07:59:55 +00:00
|
|
|
PTCPSocketChild::SendOpen(nsString(aHost), aPort,
|
|
|
|
aUseSSL, nsString(aBinaryType));
|
2012-09-24 18:53:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocketChildBase::ReleaseIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIPCOpen);
|
|
|
|
mIPCOpen = false;
|
|
|
|
this->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocketChildBase::AddIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIPCOpen);
|
|
|
|
mIPCOpen = true;
|
|
|
|
this->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketChild::~TCPSocketChild()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-14 08:00:28 +00:00
|
|
|
bool
|
|
|
|
TCPSocketChild::RecvUpdateBufferedAmount(const uint32_t& aBuffered,
|
|
|
|
const uint32_t& aTrackingNumber)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(mSocket->UpdateBufferedAmount(aBuffered, aTrackingNumber))) {
|
|
|
|
NS_ERROR("Shouldn't fail!");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-24 18:53:49 +00:00
|
|
|
bool
|
|
|
|
TCPSocketChild::RecvCallback(const nsString& aType,
|
|
|
|
const CallbackData& aData,
|
2013-11-14 08:00:28 +00:00
|
|
|
const nsString& aReadyState)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
2013-11-14 08:00:28 +00:00
|
|
|
if (NS_FAILED(mSocket->UpdateReadyState(aReadyState)))
|
2012-09-24 18:53:49 +00:00
|
|
|
NS_ERROR("Shouldn't fail!");
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
if (aData.type() == CallbackData::Tvoid_t) {
|
|
|
|
rv = mSocket->CallListenerVoid(aType);
|
|
|
|
|
2013-05-02 07:51:54 +00:00
|
|
|
} else if (aData.type() == CallbackData::TTCPError) {
|
|
|
|
const TCPError& err(aData.get_TCPError());
|
|
|
|
rv = mSocket->CallListenerError(aType, err.name());
|
2012-09-24 18:53:49 +00:00
|
|
|
|
|
|
|
} else if (aData.type() == CallbackData::TSendableData) {
|
|
|
|
const SendableData& data = aData.get_SendableData();
|
|
|
|
|
|
|
|
if (data.type() == SendableData::TArrayOfuint8_t) {
|
2013-05-11 02:39:45 +00:00
|
|
|
JSContext* cx = nsContentUtils::GetSafeJSContext();
|
2013-08-26 23:38:08 +00:00
|
|
|
JSAutoRequest ar(cx);
|
2013-05-11 02:39:45 +00:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2013-07-29 17:36:43 +00:00
|
|
|
JS::Rooted<JSObject*> window(cx, mWindowObj);
|
|
|
|
bool ok = IPC::DeserializeArrayBuffer(window, data.get_ArrayOfuint8_t(), &val);
|
2013-04-23 16:05:06 +00:00
|
|
|
NS_ENSURE_TRUE(ok, true);
|
2012-09-24 18:53:49 +00:00
|
|
|
rv = mSocket->CallListenerArrayBuffer(aType, val);
|
|
|
|
|
|
|
|
} else if (data.type() == SendableData::TnsString) {
|
|
|
|
rv = mSocket->CallListenerData(aType, data.get_nsString());
|
|
|
|
|
|
|
|
} else {
|
2013-06-29 01:38:30 +00:00
|
|
|
MOZ_CRASH("Invalid callback data type!");
|
2012-09-24 18:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-06-29 01:38:30 +00:00
|
|
|
MOZ_CRASH("Invalid callback type!");
|
2012-09-24 18:53:49 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-31 19:09:17 +00:00
|
|
|
NS_IMETHODIMP
|
2013-11-14 07:59:55 +00:00
|
|
|
TCPSocketChild::SendStartTLS()
|
2013-08-31 19:09:17 +00:00
|
|
|
{
|
2013-11-14 07:59:55 +00:00
|
|
|
PTCPSocketChild::SendStartTLS();
|
2013-08-31 19:09:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-24 18:53:49 +00:00
|
|
|
NS_IMETHODIMP
|
2013-11-14 07:59:55 +00:00
|
|
|
TCPSocketChild::SendSuspend()
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
2013-11-14 07:59:55 +00:00
|
|
|
PTCPSocketChild::SendSuspend();
|
2012-09-24 18:53:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-11-14 07:59:55 +00:00
|
|
|
TCPSocketChild::SendResume()
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
2013-11-14 07:59:55 +00:00
|
|
|
PTCPSocketChild::SendResume();
|
2012-09-24 18:53:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-11-14 07:59:55 +00:00
|
|
|
TCPSocketChild::SendClose()
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
2013-11-14 07:59:55 +00:00
|
|
|
PTCPSocketChild::SendClose();
|
2012-09-24 18:53:49 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 17:39:36 +00:00
|
|
|
TCPSocketChild::SendSend(JS::Handle<JS::Value> aData,
|
2013-11-14 07:59:55 +00:00
|
|
|
uint32_t aByteOffset,
|
|
|
|
uint32_t aByteLength,
|
2013-11-14 08:00:28 +00:00
|
|
|
uint32_t aTrackingNumber,
|
2013-11-14 07:59:55 +00:00
|
|
|
JSContext* aCx)
|
2012-09-24 18:53:49 +00:00
|
|
|
{
|
|
|
|
if (aData.isString()) {
|
|
|
|
JSString* jsstr = aData.toString();
|
2014-07-05 15:30:54 +00:00
|
|
|
nsAutoJSString str;
|
2012-09-24 18:53:49 +00:00
|
|
|
bool ok = str.init(aCx, jsstr);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
|
2013-11-14 08:00:28 +00:00
|
|
|
SendData(str, aTrackingNumber);
|
2012-09-24 18:53:49 +00:00
|
|
|
} else {
|
|
|
|
NS_ENSURE_TRUE(aData.isObject(), NS_ERROR_FAILURE);
|
2013-05-04 07:52:57 +00:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aData.toObject());
|
2013-04-19 09:25:33 +00:00
|
|
|
NS_ENSURE_TRUE(JS_IsArrayBufferObject(obj), NS_ERROR_FAILURE);
|
|
|
|
uint32_t buflen = JS_GetArrayBufferByteLength(obj);
|
|
|
|
aByteOffset = std::min(buflen, aByteOffset);
|
|
|
|
uint32_t nbytes = std::min(buflen - aByteOffset, aByteLength);
|
|
|
|
uint8_t* data = JS_GetArrayBufferData(obj);
|
2012-09-24 18:53:49 +00:00
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
FallibleTArray<uint8_t> fallibleArr;
|
2013-11-15 17:28:00 +00:00
|
|
|
if (!fallibleArr.InsertElementsAt(0, data + aByteOffset, nbytes)) {
|
2012-09-24 18:53:49 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
InfallibleTArray<uint8_t> arr;
|
|
|
|
arr.SwapElements(fallibleArr);
|
2013-11-14 08:00:28 +00:00
|
|
|
SendData(arr, aTrackingNumber);
|
2012-09-24 18:53:49 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:36:43 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketChild::SetSocketAndWindow(nsITCPSocketInternal *aSocket,
|
2014-01-09 17:39:36 +00:00
|
|
|
JS::Handle<JS::Value> aWindowObj,
|
2013-11-14 07:59:55 +00:00
|
|
|
JSContext* aCx)
|
2013-07-29 17:36:43 +00:00
|
|
|
{
|
|
|
|
mSocket = aSocket;
|
|
|
|
MOZ_ASSERT(aWindowObj.isObject());
|
|
|
|
mWindowObj = js::CheckedUnwrap(&aWindowObj.toObject());
|
|
|
|
if (!mWindowObj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-19 03:21:12 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketChild::GetHost(nsAString& aHost)
|
|
|
|
{
|
|
|
|
aHost = mHost;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketChild::GetPort(uint16_t* aPort)
|
|
|
|
{
|
|
|
|
*aPort = mPort;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:36:43 +00:00
|
|
|
bool
|
|
|
|
TCPSocketChild::RecvRequestDelete()
|
|
|
|
{
|
|
|
|
mozilla::unused << Send__delete__(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-24 18:53:49 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|