Bug 1787269 [wpt PR 35624] - Further refine STP download regexs, a=testonly
[gecko.git] / dom / network / TCPServerSocketParent.cpp
blobcff64e91599d907eba952497ebdb453f9719f823
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"
9 #include "nsJSUtils.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)
24 NS_INTERFACE_MAP_END
26 void TCPServerSocketParent::ReleaseIPDLReference() {
27 MOZ_ASSERT(mIPCOpen);
28 mIPCOpen = false;
29 this->Release();
32 void TCPServerSocketParent::AddIPDLReference() {
33 MOZ_ASSERT(!mIPCOpen);
34 mIPCOpen = true;
35 this->AddRef();
38 TCPServerSocketParent::TCPServerSocketParent(PNeckoParent* neckoParent,
39 uint16_t aLocalPort,
40 uint16_t aBacklog,
41 bool aUseArrayBuffers)
42 : mNeckoParent(neckoParent), mIPCOpen(false) {
43 mServerSocket =
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) {
55 nsresult rv;
57 nsString host;
58 rv = socket->GetHost(host);
59 if (NS_FAILED(rv)) {
60 NS_ERROR("Failed to get host from nsITCPSocketParent");
61 return NS_ERROR_FAILURE;
64 uint16_t port;
65 rv = socket->GetPort(&port);
66 if (NS_FAILED(rv)) {
67 NS_ERROR("Failed to get port from nsITCPSocketParent");
68 return NS_ERROR_FAILURE;
71 if (mNeckoParent) {
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);
78 } else {
79 NS_ERROR("Sending data from PTCPSocketParent was failed.");
81 } else {
82 NS_ERROR("The member value for NeckoParent is wrong.");
84 return NS_OK;
87 mozilla::ipc::IPCResult TCPServerSocketParent::RecvClose() {
88 NS_ENSURE_TRUE(mServerSocket, IPC_OK());
89 mServerSocket->Close();
90 return IPC_OK();
93 void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) {
94 if (mServerSocket) {
95 mServerSocket->Close();
96 mServerSocket = nullptr;
98 mNeckoParent = nullptr;
101 mozilla::ipc::IPCResult TCPServerSocketParent::RecvRequestDelete() {
102 mozilla::Unused << Send__delete__(this);
103 return IPC_OK();
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