WebrtcTCPSocketParent.cpp (3247B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 ft=cpp : */ 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 "WebrtcTCPSocketParent.h" 8 9 #include "WebrtcTCPSocket.h" 10 #include "WebrtcTCPSocketLog.h" 11 #include "mozilla/net/NeckoParent.h" 12 13 using namespace mozilla::dom; 14 using namespace mozilla::ipc; 15 16 namespace mozilla::net { 17 18 mozilla::ipc::IPCResult WebrtcTCPSocketParent::RecvAsyncOpen( 19 const nsACString& aHost, const int& aPort, const nsACString& aLocalAddress, 20 const int& aLocalPort, const bool& aUseTls, 21 const Maybe<WebrtcProxyConfig>& aProxyConfig) { 22 LOG(("WebrtcTCPSocketParent::RecvAsyncOpen %p to %s:%d\n", this, 23 PromiseFlatCString(aHost).get(), aPort)); 24 25 MOZ_ASSERT(mChannel, "webrtc TCP socket should be non-null"); 26 if (!mChannel) { 27 return IPC_FAIL(this, "Called with null channel."); 28 } 29 30 mChannel->Open(aHost, aPort, aLocalAddress, aLocalPort, aUseTls, 31 aProxyConfig); 32 33 return IPC_OK(); 34 } 35 36 mozilla::ipc::IPCResult WebrtcTCPSocketParent::RecvWrite( 37 nsTArray<uint8_t>&& aWriteData) { 38 LOG(("WebrtcTCPSocketParent::RecvWrite %p for %zu\n", this, 39 aWriteData.Length())); 40 41 // Need to check this here in case there are Writes in the queue after OnClose 42 if (mChannel) { 43 mChannel->Write(std::move(aWriteData)); 44 } 45 46 return IPC_OK(); 47 } 48 49 mozilla::ipc::IPCResult WebrtcTCPSocketParent::RecvClose() { 50 LOG(("WebrtcTCPSocketParent::RecvClose %p\n", this)); 51 52 CleanupChannel(); 53 54 IProtocol* mgr = Manager(); 55 if (!Send__delete__(this)) { 56 return IPC_FAIL_NO_REASON(mgr); 57 } 58 59 return IPC_OK(); 60 } 61 62 void WebrtcTCPSocketParent::ActorDestroy(ActorDestroyReason aWhy) { 63 LOG(("WebrtcTCPSocketParent::ActorDestroy %p for %d\n", this, aWhy)); 64 65 CleanupChannel(); 66 } 67 68 WebrtcTCPSocketParent::WebrtcTCPSocketParent(const Maybe<dom::TabId>& aTabId) { 69 MOZ_COUNT_CTOR(WebrtcTCPSocketParent); 70 71 LOG(("WebrtcTCPSocketParent::WebrtcTCPSocketParent %p\n", this)); 72 73 mChannel = new WebrtcTCPSocket(this); 74 if (aTabId.isSome()) { 75 mChannel->SetTabId(*aTabId); 76 } 77 } 78 79 WebrtcTCPSocketParent::~WebrtcTCPSocketParent() { 80 MOZ_COUNT_DTOR(WebrtcTCPSocketParent); 81 82 LOG(("WebrtcTCPSocketParent::~WebrtcTCPSocketParent %p\n", this)); 83 84 CleanupChannel(); 85 } 86 87 // WebrtcTCPSocketCallback 88 void WebrtcTCPSocketParent::OnClose(nsresult aReason) { 89 LOG(("WebrtcTCPSocketParent::OnClose %p\n", this)); 90 91 if (mChannel) { 92 (void)SendOnClose(aReason); 93 } 94 95 CleanupChannel(); 96 } 97 98 void WebrtcTCPSocketParent::OnRead(nsTArray<uint8_t>&& aReadData) { 99 LOG(("WebrtcTCPSocketParent::OnRead %p %zu\n", this, aReadData.Length())); 100 101 if (mChannel && !SendOnRead(std::move(aReadData))) { 102 CleanupChannel(); 103 } 104 } 105 106 void WebrtcTCPSocketParent::OnConnected(const nsACString& aProxyType) { 107 LOG(("WebrtcTCPSocketParent::OnConnected %p\n", this)); 108 109 if (mChannel && !SendOnConnected(aProxyType)) { 110 CleanupChannel(); 111 } 112 } 113 114 void WebrtcTCPSocketParent::CleanupChannel() { 115 if (mChannel) { 116 mChannel->Close(); 117 mChannel = nullptr; 118 } 119 } 120 121 } // namespace mozilla::net