2015-05-03 22:32:37 +03: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 21:36:43 +04: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 05:17:00 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2015-03-25 17:36:56 +03:00
|
|
|
#include "TCPServerSocket.h"
|
2013-07-29 21:36:43 +04:00
|
|
|
#include "TCPServerSocketParent.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "TCPSocketParent.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/AppProcessChecker.h"
|
2014-10-30 05:03:54 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/TabParent.h"
|
2013-07-29 21:36:43 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket)
|
2013-07-29 21:36:43 +04: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 17:36:56 +03:00
|
|
|
TCPServerSocketParent::TCPServerSocketParent(PNeckoParent* neckoParent,
|
|
|
|
uint16_t aLocalPort,
|
|
|
|
uint16_t aBacklog,
|
|
|
|
bool aUseArrayBuffers)
|
|
|
|
: mNeckoParent(neckoParent)
|
|
|
|
, mIPCOpen(false)
|
2013-07-29 21:36:43 +04:00
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
mServerSocket = new TCPServerSocket(nullptr, aLocalPort, aUseArrayBuffers, aBacklog);
|
|
|
|
mServerSocket->SetServerBridgeParent(this);
|
|
|
|
}
|
2013-07-29 21:36:43 +04:00
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
TCPServerSocketParent::~TCPServerSocketParent()
|
|
|
|
{
|
|
|
|
}
|
2013-07-29 21:36:43 +04:00
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPServerSocketParent::Init()
|
|
|
|
{
|
|
|
|
NS_ENSURE_SUCCESS_VOID(mServerSocket->Init());
|
2013-07-29 21:36:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-30 05:03:54 +03:00
|
|
|
uint32_t
|
|
|
|
TCPServerSocketParent::GetAppId()
|
|
|
|
{
|
|
|
|
const PContentParent *content = Manager()->Manager();
|
2015-10-08 03:15:56 +03:00
|
|
|
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
|
|
|
|
TabParent *tab = TabParent::GetFrom(browser);
|
|
|
|
return tab->OwnAppId();
|
|
|
|
} else {
|
|
|
|
return nsIScriptSecurityManager::UNKNOWN_APP_ID;
|
2014-10-30 05:03:54 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-29 11:12:17 +03:00
|
|
|
bool
|
|
|
|
TCPServerSocketParent::GetInBrowser()
|
|
|
|
{
|
|
|
|
const PContentParent *content = Manager()->Manager();
|
2015-10-08 03:15:56 +03:00
|
|
|
if (PBrowserParent* browser = LoneManagedOrNull(content->ManagedPBrowserParent())) {
|
|
|
|
TabParent *tab = TabParent::GetFrom(browser);
|
|
|
|
return tab->IsBrowserElement();
|
|
|
|
} else {
|
|
|
|
return false;
|
2014-12-29 11:12:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
nsresult
|
|
|
|
TCPServerSocketParent::SendCallbackAccept(TCPSocketParent *socket)
|
2013-07-29 21:36:43 +04:00
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
socket->AddIPDLReference();
|
2013-07-29 21:36:43 +04:00
|
|
|
|
2013-12-19 07:21:12 +04: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 21:36:43 +04:00
|
|
|
if (mNeckoParent) {
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mNeckoParent->SendPTCPSocketConstructor(socket, host, port)) {
|
|
|
|
mozilla::unused << PTCPServerSocketParent::SendCallbackAccept(socket);
|
2013-07-29 21:36:43 +04: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()
|
|
|
|
{
|
|
|
|
mozilla::unused << Send__delete__(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event)
|
|
|
|
{
|
|
|
|
nsRefPtr<TCPSocket> socket = event->Socket();
|
|
|
|
socket->SetAppIdAndBrowser(GetAppId(), GetInBrowser());
|
|
|
|
|
|
|
|
nsRefPtr<TCPSocketParent> socketParent = new TCPSocketParent();
|
|
|
|
socketParent->SetSocket(socket);
|
|
|
|
|
|
|
|
socket->SetSocketBridgeParent(socketParent);
|
|
|
|
|
|
|
|
SendCallbackAccept(socketParent);
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:36:43 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|