Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / dom / network / TCPSocketChild.cpp
blob2420171df3b4becf557e8a00c4ade089eb9472c2
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 <algorithm>
8 #include "TCPSocketChild.h"
9 #include "mozilla/HoldDropJSObjects.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/net/NeckoChild.h"
13 #include "mozilla/dom/PBrowserChild.h"
14 #include "mozilla/dom/BrowserChild.h"
15 #include "nsITCPSocketCallback.h"
16 #include "TCPSocket.h"
17 #include "nsContentUtils.h"
18 #include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
19 #include "js/RootingAPI.h" // JS::MutableHandle
20 #include "js/Utility.h" // js::ArrayBufferContentsArena, JS::FreePolicy, js_pod_arena_malloc
21 #include "js/Value.h" // JS::Value
23 using mozilla::net::gNeckoChild;
25 namespace IPC {
27 bool DeserializeArrayBuffer(JSContext* cx, const nsTArray<uint8_t>& aBuffer,
28 JS::MutableHandle<JS::Value> aVal) {
29 mozilla::UniquePtr<uint8_t[], JS::FreePolicy> data(
30 js_pod_arena_malloc<uint8_t>(js::ArrayBufferContentsArena,
31 aBuffer.Length()));
32 if (!data) return false;
33 memcpy(data.get(), aBuffer.Elements(), aBuffer.Length());
35 JSObject* obj =
36 JS::NewArrayBufferWithContents(cx, aBuffer.Length(), std::move(data));
37 if (!obj) return false;
39 aVal.setObject(*obj);
40 return true;
43 } // namespace IPC
45 namespace mozilla::dom {
47 NS_IMPL_CYCLE_COLLECTION_CLASS(TCPSocketChildBase)
49 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TCPSocketChildBase)
50 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocket)
51 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
53 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TCPSocketChildBase)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocket)
55 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
57 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(TCPSocketChildBase)
58 NS_IMPL_CYCLE_COLLECTION_TRACE_END
60 NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketChildBase)
61 NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketChildBase)
63 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketChildBase)
64 NS_INTERFACE_MAP_ENTRY(nsISupports)
65 NS_INTERFACE_MAP_END
67 TCPSocketChildBase::TCPSocketChildBase() : mIPCOpen(false) {
68 mozilla::HoldJSObjects(this);
71 TCPSocketChildBase::~TCPSocketChildBase() { mozilla::DropJSObjects(this); }
73 NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketChild::Release(void) {
74 nsrefcnt refcnt = TCPSocketChildBase::Release();
75 if (refcnt == 1 && mIPCOpen) {
76 PTCPSocketChild::SendRequestDelete();
77 return 1;
79 return refcnt;
82 TCPSocketChild::TCPSocketChild(const nsAString& aHost, const uint16_t& aPort,
83 nsISerialEventTarget* aTarget)
84 : mHost(aHost), mPort(aPort), mIPCEventTarget(aTarget) {}
86 void TCPSocketChild::SendOpen(nsITCPSocketCallback* aSocket, bool aUseSSL,
87 bool aUseArrayBuffers) {
88 mSocket = aSocket;
90 AddIPDLReference();
91 gNeckoChild->SendPTCPSocketConstructor(this, mHost, mPort);
92 PTCPSocketChild::SendOpen(mHost, mPort, aUseSSL, aUseArrayBuffers);
95 void TCPSocketChildBase::ReleaseIPDLReference() {
96 MOZ_ASSERT(mIPCOpen);
97 mIPCOpen = false;
98 mSocket = nullptr;
99 this->Release();
102 void TCPSocketChildBase::AddIPDLReference() {
103 MOZ_ASSERT(!mIPCOpen);
104 mIPCOpen = true;
105 this->AddRef();
108 TCPSocketChild::~TCPSocketChild() = default;
110 mozilla::ipc::IPCResult TCPSocketChild::RecvUpdateBufferedAmount(
111 const uint32_t& aBuffered, const uint32_t& aTrackingNumber) {
112 mSocket->UpdateBufferedAmount(aBuffered, aTrackingNumber);
113 return IPC_OK();
116 mozilla::ipc::IPCResult TCPSocketChild::RecvCallback(
117 const nsString& aType, const CallbackData& aData,
118 const uint32_t& aReadyState) {
119 mSocket->UpdateReadyState(aReadyState);
121 if (aData.type() == CallbackData::Tvoid_t) {
122 mSocket->FireEvent(aType);
124 } else if (aData.type() == CallbackData::TTCPError) {
125 const TCPError& err(aData.get_TCPError());
126 mSocket->FireErrorEvent(err.name(), err.message(), err.errorCode());
128 } else if (aData.type() == CallbackData::TSendableData) {
129 const SendableData& data = aData.get_SendableData();
131 if (data.type() == SendableData::TArrayOfuint8_t) {
132 mSocket->FireDataArrayEvent(aType, data.get_ArrayOfuint8_t());
133 } else if (data.type() == SendableData::TnsCString) {
134 mSocket->FireDataStringEvent(aType, data.get_nsCString());
135 } else {
136 MOZ_CRASH("Invalid callback data type!");
138 } else {
139 MOZ_CRASH("Invalid callback type!");
141 return IPC_OK();
144 void TCPSocketChild::SendSend(const nsACString& aData) {
145 SendData(nsCString(aData));
148 void TCPSocketChild::SendSend(nsTArray<uint8_t>&& aData) {
149 SendData(SendableData{std::move(aData)});
152 void TCPSocketChild::SetSocket(TCPSocket* aSocket) { mSocket = aSocket; }
154 void TCPSocketChild::GetHost(nsAString& aHost) { aHost = mHost; }
156 void TCPSocketChild::GetPort(uint16_t* aPort) const { *aPort = mPort; }
158 mozilla::ipc::IPCResult TCPSocketChild::RecvRequestDelete() {
159 mozilla::Unused << Send__delete__(this);
160 return IPC_OK();
163 } // namespace mozilla::dom