TCPServerSocketParent.cpp (3444B)
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/. */ 6 7 #include "TCPServerSocketParent.h" 8 9 #include "TCPServerSocket.h" 10 #include "TCPSocket.h" 11 #include "TCPSocketParent.h" 12 #include "mozilla/dom/BrowserParent.h" 13 #include "mozilla/dom/TCPServerSocketEvent.h" 14 #include "nsJSUtils.h" 15 16 namespace mozilla::dom { 17 18 NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket) 19 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent) 20 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent) 21 22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent) 23 NS_INTERFACE_MAP_ENTRY(nsISupports) 24 NS_INTERFACE_MAP_END 25 26 void TCPServerSocketParent::ReleaseIPDLReference() { 27 MOZ_ASSERT(mIPCOpen); 28 mIPCOpen = false; 29 this->Release(); 30 } 31 32 void TCPServerSocketParent::AddIPDLReference() { 33 MOZ_ASSERT(!mIPCOpen); 34 mIPCOpen = true; 35 this->AddRef(); 36 } 37 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); 46 } 47 48 TCPServerSocketParent::~TCPServerSocketParent() = default; 49 50 void TCPServerSocketParent::Init() { 51 NS_ENSURE_SUCCESS_VOID(mServerSocket->Init()); 52 } 53 54 nsresult TCPServerSocketParent::SendCallbackAccept(TCPSocketParent* socket) { 55 nsresult rv; 56 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; 62 } 63 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; 69 } 70 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(); 76 77 (void)PTCPServerSocketParent::SendCallbackAccept(WrapNotNull(socket)); 78 } else { 79 NS_ERROR("Sending data from PTCPSocketParent was failed."); 80 } 81 } else { 82 NS_ERROR("The member value for NeckoParent is wrong."); 83 } 84 return NS_OK; 85 } 86 87 mozilla::ipc::IPCResult TCPServerSocketParent::RecvClose() { 88 NS_ENSURE_TRUE(mServerSocket, IPC_OK()); 89 mServerSocket->Close(); 90 return IPC_OK(); 91 } 92 93 void TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) { 94 if (mServerSocket) { 95 mServerSocket->Close(); 96 mServerSocket = nullptr; 97 } 98 mNeckoParent = nullptr; 99 } 100 101 mozilla::ipc::IPCResult TCPServerSocketParent::RecvRequestDelete() { 102 (void)Send__delete__(this); 103 return IPC_OK(); 104 } 105 106 void TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event) { 107 RefPtr<TCPSocket> socket = event->Socket(); 108 109 RefPtr<TCPSocketParent> socketParent = new TCPSocketParent(); 110 socketParent->SetSocket(socket); 111 112 socket->SetSocketBridgeParent(socketParent); 113 114 SendCallbackAccept(socketParent); 115 } 116 117 } // namespace mozilla::dom