Merge autoland to mozilla-central. a=merge
[gecko.git] / dom / network / TCPServerSocketParent.cpp
blobc73ee7e0ff41d21def12ef0af409c65d9b8d5b37
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(
78 WrapNotNull(socket));
79 } else {
80 NS_ERROR("Sending data from PTCPSocketParent was failed.");
82 } else {
83 NS_ERROR("The member value for NeckoParent is wrong.");
85 return NS_OK;
88 mozilla::ipc::IPCResult TCPServerSocketParent::RecvClose() {
89 NS_ENSURE_TRUE(mServerSocket, IPC_OK());
90 mServerSocket->Close();
91 return IPC_OK();
94 void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) {
95 if (mServerSocket) {
96 mServerSocket->Close();
97 mServerSocket = nullptr;
99 mNeckoParent = nullptr;
102 mozilla::ipc::IPCResult TCPServerSocketParent::RecvRequestDelete() {
103 mozilla::Unused << Send__delete__(this);
104 return IPC_OK();
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