2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-07-29 17:36:43 +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/. */
|
|
|
|
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2015-03-25 14:36:56 +00:00
|
|
|
#include "TCPServerSocket.h"
|
2013-07-29 17:36:43 +00:00
|
|
|
#include "TCPServerSocketParent.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "TCPSocketParent.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/AppProcessChecker.h"
|
2014-10-30 02:03:54 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/TabParent.h"
|
2013-07-29 17:36:43 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket)
|
2013-07-29 17:36:43 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPServerSocketParent::ReleaseIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIPCOpen);
|
|
|
|
mIPCOpen = false;
|
|
|
|
this->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPServerSocketParent::AddIPDLReference()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIPCOpen);
|
|
|
|
mIPCOpen = true;
|
|
|
|
this->AddRef();
|
|
|
|
}
|
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
TCPServerSocketParent::TCPServerSocketParent(PNeckoParent* neckoParent,
|
|
|
|
uint16_t aLocalPort,
|
|
|
|
uint16_t aBacklog,
|
|
|
|
bool aUseArrayBuffers)
|
|
|
|
: mNeckoParent(neckoParent)
|
|
|
|
, mIPCOpen(false)
|
2013-07-29 17:36:43 +00:00
|
|
|
{
|
2015-03-25 14:36:56 +00:00
|
|
|
mServerSocket = new TCPServerSocket(nullptr, aLocalPort, aUseArrayBuffers, aBacklog);
|
|
|
|
mServerSocket->SetServerBridgeParent(this);
|
|
|
|
}
|
2013-07-29 17:36:43 +00:00
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
TCPServerSocketParent::~TCPServerSocketParent()
|
|
|
|
{
|
|
|
|
}
|
2013-07-29 17:36:43 +00:00
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
void
|
|
|
|
TCPServerSocketParent::Init()
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS_VOID(mServerSocket->Init());
|
2013-07-29 17:36:43 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 02:03:54 +00:00
|
|
|
uint32_t
|
|
|
|
TCPServerSocketParent::GetAppId()
|
|
|
|
{
|
|
|
|
const PContentParent *content = Manager()->Manager();
|
2015-10-08 00:15:56 +00:00
|
|
|
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
|
|
|
|
TabParent *tab = TabParent::GetFrom(browser);
|
|
|
|
return tab->OwnAppId();
|
|
|
|
} else {
|
|
|
|
return nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
2014-10-30 02:03:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-29 08:12:17 +00:00
|
|
|
bool
|
|
|
|
TCPServerSocketParent::GetInBrowser()
|
|
|
|
{
|
|
|
|
const PContentParent *content = Manager()->Manager();
|
2015-10-08 00:15:56 +00:00
|
|
|
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
|
|
|
|
TabParent *tab = TabParent::GetFrom(browser);
|
|
|
|
return tab->IsBrowserElement();
|
|
|
|
} else {
|
|
|
|
return false;
|
2014-12-29 08:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
nsresult
|
|
|
|
TCPServerSocketParent::SendCallbackAccept(TCPSocketParent *socket)
|
2013-07-29 17:36:43 +00:00
|
|
|
{
|
2015-03-25 14:36:56 +00:00
|
|
|
socket->AddIPDLReference();
|
2013-07-29 17:36:43 +00:00
|
|
|
|
2013-12-19 03:21:12 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsString host;
|
|
|
|
rv = socket->GetHost(host);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Failed to get host from nsITCPSocketParent");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t port;
|
|
|
|
rv = socket->GetPort(&port);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("Failed to get port from nsITCPSocketParent");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:36:43 +00:00
|
|
|
if (mNeckoParent) {
|
2015-03-25 14:36:56 +00:00
|
|
|
if (mNeckoParent->SendPTCPSocketConstructor(socket, host, port)) {
|
2015-11-02 05:53:26 +00:00
|
|
|
mozilla::Unused << PTCPServerSocketParent::SendCallbackAccept(socket);
|
2013-07-29 17:36:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ERROR("Sending data from PTCPSocketParent was failed.");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ERROR("The member value for NeckoParent is wrong.");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPServerSocketParent::RecvClose()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mServerSocket, true);
|
|
|
|
mServerSocket->Close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPServerSocketParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
if (mServerSocket) {
|
|
|
|
mServerSocket->Close();
|
|
|
|
mServerSocket = nullptr;
|
|
|
|
}
|
|
|
|
mNeckoParent = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPServerSocketParent::RecvRequestDelete()
|
|
|
|
{
|
2015-11-02 05:53:26 +00:00
|
|
|
mozilla::Unused << Send__delete__(this);
|
2013-07-29 17:36:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 14:36:56 +00:00
|
|
|
void
|
|
|
|
TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<TCPSocket> socket = event->Socket();
|
2015-03-25 14:36:56 +00:00
|
|
|
socket->SetAppIdAndBrowser(GetAppId(), GetInBrowser());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<TCPSocketParent> socketParent = new TCPSocketParent();
|
2015-03-25 14:36:56 +00:00
|
|
|
socketParent->SetSocket(socket);
|
|
|
|
|
|
|
|
socket->SetSocketBridgeParent(socketParent);
|
|
|
|
|
|
|
|
SendCallbackAccept(socketParent);
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:36:43 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|