1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "TCPServerSocket.h"
8 #include "TCPServerSocketParent.h"
10 #include "TCPSocket.h"
11 #include "TCPSocketParent.h"
12 #include "mozilla/Unused.h"
13 #include "mozilla/dom/BrowserParent.h"
14 #include "mozilla/dom/TCPServerSocketEvent.h"
16 namespace mozilla::dom
{
18 NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent
, mServerSocket
)
19 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent
)
20 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent
)
22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent
)
23 NS_INTERFACE_MAP_ENTRY(nsISupports
)
26 void TCPServerSocketParent::ReleaseIPDLReference() {
32 void TCPServerSocketParent::AddIPDLReference() {
33 MOZ_ASSERT(!mIPCOpen
);
38 TCPServerSocketParent::TCPServerSocketParent(PNeckoParent
* neckoParent
,
41 bool aUseArrayBuffers
)
42 : mNeckoParent(neckoParent
), mIPCOpen(false) {
44 new TCPServerSocket(nullptr, aLocalPort
, aUseArrayBuffers
, aBacklog
);
45 mServerSocket
->SetServerBridgeParent(this);
48 TCPServerSocketParent::~TCPServerSocketParent() = default;
50 void TCPServerSocketParent::Init() {
51 NS_ENSURE_SUCCESS_VOID(mServerSocket
->Init());
54 nsresult
TCPServerSocketParent::SendCallbackAccept(TCPSocketParent
* socket
) {
58 rv
= socket
->GetHost(host
);
60 NS_ERROR("Failed to get host from nsITCPSocketParent");
61 return NS_ERROR_FAILURE
;
65 rv
= socket
->GetPort(&port
);
67 NS_ERROR("Failed to get port from nsITCPSocketParent");
68 return NS_ERROR_FAILURE
;
72 if (mNeckoParent
->SendPTCPSocketConstructor(socket
, host
, port
)) {
73 // Call |AddIPDLReference| after the consructor message is sent
74 // successfully, otherwise |socket| could be leaked.
75 socket
->AddIPDLReference();
77 mozilla::Unused
<< PTCPServerSocketParent::SendCallbackAccept(
80 NS_ERROR("Sending data from PTCPSocketParent was failed.");
83 NS_ERROR("The member value for NeckoParent is wrong.");
88 mozilla::ipc::IPCResult
TCPServerSocketParent::RecvClose() {
89 NS_ENSURE_TRUE(mServerSocket
, IPC_OK());
90 mServerSocket
->Close();
94 void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why
) {
96 mServerSocket
->Close();
97 mServerSocket
= nullptr;
99 mNeckoParent
= nullptr;
102 mozilla::ipc::IPCResult
TCPServerSocketParent::RecvRequestDelete() {
103 mozilla::Unused
<< Send__delete__(this);
107 void TCPServerSocketParent::OnConnect(TCPServerSocketEvent
* event
) {
108 RefPtr
<TCPSocket
> socket
= event
->Socket();
110 RefPtr
<TCPSocketParent
> socketParent
= new TCPSocketParent();
111 socketParent
->SetSocket(socket
);
113 socket
->SetSocketBridgeParent(socketParent
);
115 SendCallbackAccept(socketParent
);
118 } // namespace mozilla::dom