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(socket
);
79 NS_ERROR("Sending data from PTCPSocketParent was failed.");
82 NS_ERROR("The member value for NeckoParent is wrong.");
87 mozilla::ipc::IPCResult
TCPServerSocketParent::RecvClose() {
88 NS_ENSURE_TRUE(mServerSocket
, IPC_OK());
89 mServerSocket
->Close();
93 void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why
) {
95 mServerSocket
->Close();
96 mServerSocket
= nullptr;
98 mNeckoParent
= nullptr;
101 mozilla::ipc::IPCResult
TCPServerSocketParent::RecvRequestDelete() {
102 mozilla::Unused
<< Send__delete__(this);
106 void TCPServerSocketParent::OnConnect(TCPServerSocketEvent
* event
) {
107 RefPtr
<TCPSocket
> socket
= event
->Socket();
109 RefPtr
<TCPSocketParent
> socketParent
= new TCPSocketParent();
110 socketParent
->SetSocket(socket
);
112 socket
->SetSocketBridgeParent(socketParent
);
114 SendCallbackAccept(socketParent
);
117 } // namespace mozilla::dom