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-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"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-04-10 00:38:15 +03:00
|
|
|
#include "mozilla/dom/BrowserParent.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) {
|
|
|
|
mServerSocket =
|
|
|
|
new TCPServerSocket(nullptr, aLocalPort, aUseArrayBuffers, aBacklog);
|
|
|
|
mServerSocket->SetServerBridgeParent(this);
|
|
|
|
}
|
2013-07-29 21:36:43 +04:00
|
|
|
|
2020-03-09 18:18:20 +03:00
|
|
|
TCPServerSocketParent::~TCPServerSocketParent() = default;
|
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
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
nsresult TCPServerSocketParent::SendCallbackAccept(TCPSocketParent* socket) {
|
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)) {
|
2019-05-02 15:00:57 +03:00
|
|
|
// Call |AddIPDLReference| after the consructor message is sent
|
|
|
|
// successfully, otherwise |socket| could be leaked.
|
|
|
|
socket->AddIPDLReference();
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << PTCPServerSocketParent::SendCallbackAccept(socket);
|
2013-07-29 21:36:43 +04:00
|
|
|
} else {
|
|
|
|
NS_ERROR("Sending data from PTCPSocketParent was failed.");
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2013-07-29 21:36:43 +04:00
|
|
|
} else {
|
|
|
|
NS_ERROR("The member value for NeckoParent is wrong.");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult TCPServerSocketParent::RecvClose() {
|
2016-11-15 06:26:00 +03:00
|
|
|
NS_ENSURE_TRUE(mServerSocket, IPC_OK());
|
2013-07-29 21:36:43 +04:00
|
|
|
mServerSocket->Close();
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-07-29 21:36:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) {
|
|
|
|
if (mServerSocket) {
|
|
|
|
mServerSocket->Close();
|
|
|
|
mServerSocket = nullptr;
|
|
|
|
}
|
|
|
|
mNeckoParent = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult TCPServerSocketParent::RecvRequestDelete() {
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << Send__delete__(this);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-07-29 21:36:43 +04:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocket> socket = event->Socket();
|
2015-03-25 17:36:56 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocketParent> socketParent = new TCPSocketParent();
|
2015-03-25 17:36:56 +03:00
|
|
|
socketParent->SetSocket(socket);
|
|
|
|
|
|
|
|
socket->SetSocketBridgeParent(socketParent);
|
|
|
|
|
|
|
|
SendCallbackAccept(socketParent);
|
|
|
|
}
|
|
|
|
|
2013-07-29 21:36:43 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|