2012-09-24 11:53:49 -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/. */
|
|
|
|
|
|
|
|
#include "TCPSocketParent.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIDOMTCPSocket.h"
|
|
|
|
#include "mozilla/unused.h"
|
2012-12-22 03:53:38 -08:00
|
|
|
#include "mozilla/AppProcessChecker.h"
|
2013-08-20 23:49:42 -07:00
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
#include "mozilla/net/PNeckoParent.h"
|
2013-08-28 23:15:08 -07:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-08-14 18:47:15 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-08-28 23:15:08 -07:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2014-09-23 12:19:19 -07:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2013-09-23 14:30:40 -07:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2012-09-24 11:53:49 -07:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
|
|
//Defined in TCPSocketChild.cpp
|
|
|
|
extern bool
|
2013-05-10 19:39:45 -07:00
|
|
|
DeserializeArrayBuffer(JS::Handle<JSObject*> aObj,
|
2013-04-19 02:25:33 -07:00
|
|
|
const InfallibleTArray<uint8_t>& aBuffer,
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::MutableHandle<JS::Value> aVal);
|
2012-09-24 11:53:49 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
static void
|
|
|
|
FireInteralError(mozilla::net::PTCPSocketParent* aActor, uint32_t aLineNo)
|
|
|
|
{
|
|
|
|
mozilla::unused <<
|
|
|
|
aActor->SendCallback(NS_LITERAL_STRING("onerror"),
|
2013-05-02 00:51:54 -07:00
|
|
|
TCPError(NS_LITERAL_STRING("InvalidStateError")),
|
2013-11-14 00:00:28 -08:00
|
|
|
NS_LITERAL_STRING("connecting"));
|
2012-09-24 11:53:49 -07:00
|
|
|
}
|
|
|
|
|
2014-09-23 12:19:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(TCPSocketParentBase)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TCPSocketParentBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocket)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIntermediary)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TCPSocketParentBase)
|
|
|
|
tmp->mIntermediaryObj = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocket)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIntermediary)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(TCPSocketParentBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mIntermediaryObj)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2012-09-24 11:53:49 -07:00
|
|
|
|
2013-07-29 10:36:43 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketParentBase)
|
2012-09-24 11:53:49 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITCPSocketParent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-09-23 12:19:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketParentBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketParentBase)
|
|
|
|
|
2013-07-29 10:36:43 -07:00
|
|
|
TCPSocketParentBase::TCPSocketParentBase()
|
|
|
|
: mIPCOpen(false)
|
|
|
|
{
|
2014-08-22 16:15:36 -07:00
|
|
|
mObserver = new mozilla::net::OfflineObserver(this);
|
2014-09-23 12:19:19 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-07-29 10:36:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketParentBase::~TCPSocketParentBase()
|
|
|
|
{
|
2014-08-22 16:15:36 -07:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->RemoveObserver();
|
|
|
|
}
|
2014-09-23 12:19:19 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2013-07-29 10:36:43 -07:00
|
|
|
}
|
|
|
|
|
2014-08-22 16:15:36 -07:00
|
|
|
nsresult
|
|
|
|
TCPSocketParent::OfflineNotification(nsISupports *aSubject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppOfflineInfo> info(do_QueryInterface(aSubject));
|
|
|
|
if (!info) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t targetAppId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
|
|
|
info->GetAppId(&targetAppId);
|
|
|
|
|
|
|
|
// Obtain App ID
|
|
|
|
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
|
|
|
const PContentParent *content = Manager()->Manager();
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
|
|
|
|
if (browsers.Length() > 0) {
|
|
|
|
TabParent *tab = static_cast<TabParent*>(browsers[0]);
|
|
|
|
appId = tab->OwnAppId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appId != targetAppId) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the app is offline, close the socket
|
|
|
|
if (mSocket && NS_IsAppOffline(appId)) {
|
|
|
|
mSocket->Close();
|
|
|
|
mSocket = nullptr;
|
|
|
|
mIntermediaryObj = nullptr;
|
|
|
|
mIntermediary = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-29 10:36:43 -07:00
|
|
|
void
|
|
|
|
TCPSocketParentBase::ReleaseIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIPCOpen);
|
|
|
|
mIPCOpen = false;
|
|
|
|
this->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocketParentBase::AddIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIPCOpen);
|
|
|
|
mIPCOpen = true;
|
|
|
|
this->AddRef();
|
|
|
|
}
|
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketParent::Release(void)
|
2013-07-29 10:36:43 -07:00
|
|
|
{
|
|
|
|
nsrefcnt refcnt = TCPSocketParentBase::Release();
|
|
|
|
if (refcnt == 1 && mIPCOpen) {
|
|
|
|
mozilla::unused << PTCPSocketParent::SendRequestDelete();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return refcnt;
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:53:49 -07:00
|
|
|
bool
|
2013-07-29 10:36:43 -07:00
|
|
|
TCPSocketParent::RecvOpen(const nsString& aHost, const uint16_t& aPort, const bool& aUseSSL,
|
2013-08-20 23:49:42 -07:00
|
|
|
const nsString& aBinaryType)
|
2012-09-24 11:53:49 -07:00
|
|
|
{
|
2013-07-29 10:36:43 -07:00
|
|
|
// We don't have browser actors in xpcshell, and hence can't run automated
|
|
|
|
// tests without this loophole.
|
2013-08-20 23:49:42 -07:00
|
|
|
if (net::UsingNeckoIPCSecurity() &&
|
|
|
|
!AssertAppProcessPermission(Manager()->Manager(), "tcp-socket")) {
|
2013-07-29 10:36:43 -07:00
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-28 23:15:08 -07:00
|
|
|
// Obtain App ID
|
2014-08-22 16:15:36 -07:00
|
|
|
uint32_t appId = nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
2014-08-26 16:34:46 -07:00
|
|
|
const PContentParent *content = Manager()->Manager();
|
|
|
|
const InfallibleTArray<PBrowserParent*>& browsers = content->ManagedPBrowserParent();
|
|
|
|
if (browsers.Length() > 0) {
|
|
|
|
TabParent *tab = static_cast<TabParent*>(browsers[0]);
|
|
|
|
appId = tab->OwnAppId();
|
2013-08-28 23:15:08 -07:00
|
|
|
}
|
|
|
|
|
2014-08-22 16:15:36 -07:00
|
|
|
if (NS_IsAppOffline(appId)) {
|
|
|
|
NS_ERROR("Can't open socket because app is offline");
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:53:49 -07:00
|
|
|
nsresult rv;
|
|
|
|
mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-28 23:15:08 -07:00
|
|
|
rv = mIntermediary->Open(this, aHost, aPort, aUseSSL, aBinaryType, appId,
|
|
|
|
getter_AddRefs(mSocket));
|
2012-09-24 11:53:49 -07:00
|
|
|
if (NS_FAILED(rv) || !mSocket) {
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
TCPSocketParent::InitJS(JS::Handle<JS::Value> aIntermediary, JSContext* aCx)
|
2012-09-24 11:53:49 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIntermediary.isObject());
|
|
|
|
mIntermediaryObj = &aIntermediary.toObject();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:09:17 -07:00
|
|
|
bool
|
|
|
|
TCPSocketParent::RecvStartTLS()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mSocket, true);
|
|
|
|
nsresult rv = mSocket->UpgradeToSecure();
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:53:49 -07:00
|
|
|
bool
|
|
|
|
TCPSocketParent::RecvSuspend()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mSocket, true);
|
|
|
|
nsresult rv = mSocket->Suspend();
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPSocketParent::RecvResume()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mSocket, true);
|
|
|
|
nsresult rv = mSocket->Resume();
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-11-14 00:00:28 -08:00
|
|
|
TCPSocketParent::RecvData(const SendableData& aData,
|
|
|
|
const uint32_t& aTrackingNumber)
|
2012-09-24 11:53:49 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mIntermediary, true);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
switch (aData.type()) {
|
|
|
|
case SendableData::TArrayOfuint8_t: {
|
2013-05-10 19:39:45 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-08-26 16:38:08 -07:00
|
|
|
JSAutoRequest ar(cx);
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
JS::Rooted<JSObject*> obj(cx, mIntermediaryObj);
|
|
|
|
IPC::DeserializeArrayBuffer(obj, aData.get_ArrayOfuint8_t(), &val);
|
2013-11-14 00:00:28 -08:00
|
|
|
rv = mIntermediary->OnRecvSendArrayBuffer(val, aTrackingNumber);
|
2012-09-24 11:53:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SendableData::TnsString:
|
2013-11-14 00:00:28 -08:00
|
|
|
rv = mIntermediary->OnRecvSendString(aData.get_nsString(), aTrackingNumber);
|
2012-09-24 11:53:49 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("unexpected SendableData type");
|
2012-09-24 11:53:49 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPSocketParent::RecvClose()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mSocket, true);
|
|
|
|
nsresult rv = mSocket->Close();
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
TCPSocketParent::SendEvent(const nsAString& aType, JS::Handle<JS::Value> aDataVal,
|
2013-11-13 23:59:55 -08:00
|
|
|
const nsAString& aReadyState, JSContext* aCx)
|
2012-09-24 11:53:49 -07:00
|
|
|
{
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
NS_WARNING("Dropping callback due to no IPC connection");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CallbackData data;
|
|
|
|
if (aDataVal.isString()) {
|
|
|
|
JSString* jsstr = aDataVal.toString();
|
2014-07-05 08:30:54 -07:00
|
|
|
nsAutoJSString str;
|
2012-09-24 11:53:49 -07:00
|
|
|
if (!str.init(aCx, jsstr)) {
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2013-05-02 00:51:54 -07:00
|
|
|
data = SendableData(str);
|
2012-09-24 11:53:49 -07:00
|
|
|
|
|
|
|
} else if (aDataVal.isUndefined() || aDataVal.isNull()) {
|
|
|
|
data = mozilla::void_t();
|
|
|
|
|
|
|
|
} else if (aDataVal.isObject()) {
|
2014-01-31 01:55:20 -08:00
|
|
|
JS::Rooted<JSObject *> obj(aCx, &aDataVal.toObject());
|
2013-04-19 02:25:33 -07:00
|
|
|
if (JS_IsArrayBufferObject(obj)) {
|
|
|
|
uint32_t nbytes = JS_GetArrayBufferByteLength(obj);
|
|
|
|
uint8_t* buffer = JS_GetArrayBufferData(obj);
|
2012-09-24 11:53:49 -07:00
|
|
|
if (!buffer) {
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
FallibleTArray<uint8_t> fallibleArr;
|
|
|
|
if (!fallibleArr.InsertElementsAt(0, buffer, nbytes)) {
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
InfallibleTArray<uint8_t> arr;
|
|
|
|
arr.SwapElements(fallibleArr);
|
|
|
|
data = SendableData(arr);
|
|
|
|
|
|
|
|
} else {
|
2014-07-05 08:30:54 -07:00
|
|
|
nsAutoJSString name;
|
2012-09-24 11:53:49 -07:00
|
|
|
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2013-07-26 02:00:38 -07:00
|
|
|
if (!JS_GetProperty(aCx, obj, "name", &val)) {
|
2013-05-02 00:51:54 -07:00
|
|
|
NS_ERROR("No name property on supposed error object");
|
2014-04-27 19:41:01 -07:00
|
|
|
} else if (val.isString()) {
|
2014-04-27 19:45:46 -07:00
|
|
|
if (!name.init(aCx, val.toString())) {
|
2012-09-24 11:53:49 -07:00
|
|
|
NS_WARNING("couldn't initialize string");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 00:51:54 -07:00
|
|
|
data = TCPError(name);
|
2012-09-24 11:53:49 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Unexpected JS value encountered");
|
|
|
|
FireInteralError(this, __LINE__);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mozilla::unused <<
|
|
|
|
PTCPSocketParent::SendCallback(nsString(aType), data,
|
2013-11-14 00:00:28 -08:00
|
|
|
nsString(aReadyState));
|
2012-09-24 11:53:49 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-29 10:36:43 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketParent::SetSocketAndIntermediary(nsIDOMTCPSocket *socket,
|
|
|
|
nsITCPSocketIntermediary *intermediary,
|
|
|
|
JSContext* cx)
|
|
|
|
{
|
|
|
|
mSocket = socket;
|
|
|
|
mIntermediary = intermediary;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-14 00:00:28 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketParent::SendUpdateBufferedAmount(uint32_t aBufferedAmount,
|
|
|
|
uint32_t aTrackingNumber)
|
|
|
|
{
|
|
|
|
mozilla::unused << PTCPSocketParent::SendUpdateBufferedAmount(aBufferedAmount,
|
|
|
|
aTrackingNumber);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-18 19:21:12 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketParent::GetHost(nsAString& aHost)
|
|
|
|
{
|
|
|
|
if (!mSocket) {
|
|
|
|
NS_ERROR("No internal socket instance mSocket!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return mSocket->GetHost(aHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocketParent::GetPort(uint16_t* aPort)
|
|
|
|
{
|
|
|
|
if (!mSocket) {
|
|
|
|
NS_ERROR("No internal socket instance mSocket!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return mSocket->GetPort(aPort);
|
|
|
|
}
|
|
|
|
|
2012-09-24 11:53:49 -07:00
|
|
|
void
|
|
|
|
TCPSocketParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close();
|
|
|
|
}
|
|
|
|
mSocket = nullptr;
|
|
|
|
mIntermediaryObj = nullptr;
|
|
|
|
mIntermediary = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPSocketParent::RecvRequestDelete()
|
|
|
|
{
|
|
|
|
mozilla::unused << Send__delete__(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|