summaryrefslogtreecommitdiffstats
path: root/dom/network/TCPSocketChild.cpp
blob: 9e1dce27692a0fde5e739deeadb1c02c20192756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <algorithm>
#include "TCPSocketChild.h"
#include "mozilla/Unused.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/net/NeckoChild.h"
#include "mozilla/dom/PBrowserChild.h"
#include "mozilla/dom/TabChild.h"
#include "nsITCPSocketCallback.h"
#include "TCPSocket.h"
#include "nsContentUtils.h"
#include "jsapi.h"
#include "jsfriendapi.h"

using mozilla::net::gNeckoChild;

namespace IPC {

bool
DeserializeArrayBuffer(JSContext* cx,
                       const InfallibleTArray<uint8_t>& aBuffer,
                       JS::MutableHandle<JS::Value> aVal)
{
  mozilla::UniquePtr<uint8_t[], JS::FreePolicy> data(js_pod_malloc<uint8_t>(aBuffer.Length()));
  if (!data)
      return false;
  memcpy(data.get(), aBuffer.Elements(), aBuffer.Length());

  JSObject* obj = JS_NewArrayBufferWithContents(cx, aBuffer.Length(), data.get());
  if (!obj)
      return false;
  // If JS_NewArrayBufferWithContents returns non-null, the ownership of
  // the data is transfered to obj, so we release the ownership here.
  mozilla::Unused << data.release();

  aVal.setObject(*obj);
  return true;
}

} // namespace IPC

namespace mozilla {
namespace dom {

NS_IMPL_CYCLE_COLLECTION_CLASS(TCPSocketChildBase)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TCPSocketChildBase)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocket)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TCPSocketChildBase)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocket)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(TCPSocketChildBase)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPSocketChildBase)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPSocketChildBase)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPSocketChildBase)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

TCPSocketChildBase::TCPSocketChildBase()
: mIPCOpen(false)
{
  mozilla::HoldJSObjects(this);
}

TCPSocketChildBase::~TCPSocketChildBase()
{
  mozilla::DropJSObjects(this);
}

NS_IMETHODIMP_(MozExternalRefCountType) TCPSocketChild::Release(void)
{
  nsrefcnt refcnt = TCPSocketChildBase::Release();
  if (refcnt == 1 && mIPCOpen) {
    PTCPSocketChild::SendRequestDelete();
    return 1;
  }
  return refcnt;
}

TCPSocketChild::TCPSocketChild(const nsAString& aHost, const uint16_t& aPort)
: mHost(aHost)
, mPort(aPort)
{
}

void
TCPSocketChild::SendOpen(nsITCPSocketCallback* aSocket, bool aUseSSL, bool aUseArrayBuffers)
{
  mSocket = aSocket;

  AddIPDLReference();
  gNeckoChild->SendPTCPSocketConstructor(this, mHost, mPort);
  MOZ_ASSERT(mFilterName.IsEmpty()); // Currently nobody should use this
  PTCPSocketChild::SendOpen(mHost, mPort, aUseSSL, aUseArrayBuffers);
}

void
TCPSocketChild::SendWindowlessOpenBind(nsITCPSocketCallback* aSocket,
                                       const nsACString& aRemoteHost, uint16_t aRemotePort,
                                       const nsACString& aLocalHost, uint16_t aLocalPort,
                                       bool aUseSSL)
{
  mSocket = aSocket;
  AddIPDLReference();
  gNeckoChild->SendPTCPSocketConstructor(this,
                                         NS_ConvertUTF8toUTF16(aRemoteHost),
                                         aRemotePort);
  PTCPSocketChild::SendOpenBind(nsCString(aRemoteHost), aRemotePort,
                                nsCString(aLocalHost), aLocalPort,
                                aUseSSL, true, mFilterName);
}

void
TCPSocketChildBase::ReleaseIPDLReference()
{
  MOZ_ASSERT(mIPCOpen);
  mIPCOpen = false;
  mSocket = nullptr;
  this->Release();
}

void
TCPSocketChildBase::AddIPDLReference()
{
  MOZ_ASSERT(!mIPCOpen);
  mIPCOpen = true;
  this->AddRef();
}

TCPSocketChild::~TCPSocketChild()
{
}

bool
TCPSocketChild::RecvUpdateBufferedAmount(const uint32_t& aBuffered,
                                         const uint32_t& aTrackingNumber)
{
  mSocket->UpdateBufferedAmount(aBuffered, aTrackingNumber);
  return true;
}

bool
TCPSocketChild::RecvCallback(const nsString& aType,
                             const CallbackData& aData,
                             const uint32_t& aReadyState)
{
  mSocket->UpdateReadyState(aReadyState);

  if (aData.type() == CallbackData::Tvoid_t) {
    mSocket->FireEvent(aType);

  } else if (aData.type() == CallbackData::TTCPError) {
    const TCPError& err(aData.get_TCPError());
    mSocket->FireErrorEvent(err.name(), err.message());

  } else if (aData.type() == CallbackData::TSendableData) {
    const SendableData& data = aData.get_SendableData();

    if (data.type() == SendableData::TArrayOfuint8_t) {
      mSocket->FireDataArrayEvent(aType, data.get_ArrayOfuint8_t());
    } else if (data.type() == SendableData::TnsCString) {
      mSocket->FireDataStringEvent(aType, data.get_nsCString());
    } else {
      MOZ_CRASH("Invalid callback data type!");
    }
  } else {
    MOZ_CRASH("Invalid callback type!");
  }
  return true;
}

void
TCPSocketChild::SendSend(const nsACString& aData, uint32_t aTrackingNumber)
{
  SendData(nsCString(aData), aTrackingNumber);
}

nsresult
TCPSocketChild::SendSend(const ArrayBuffer& aData,
                         uint32_t aByteOffset,
                         uint32_t aByteLength,
                         uint32_t aTrackingNumber)
{
  uint32_t buflen = aData.Length();
  uint32_t offset = std::min(buflen, aByteOffset);
  uint32_t nbytes = std::min(buflen - aByteOffset, aByteLength);
  FallibleTArray<uint8_t> fallibleArr;
  if (!fallibleArr.InsertElementsAt(0, aData.Data() + offset, nbytes, fallible)) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  InfallibleTArray<uint8_t> arr;
  arr.SwapElements(fallibleArr);
  SendData(arr, aTrackingNumber);
  return NS_OK;
}

NS_IMETHODIMP
TCPSocketChild::SendSendArray(nsTArray<uint8_t>& aArray, uint32_t aTrackingNumber)
{
  SendData(aArray, aTrackingNumber);
  return NS_OK;
}

void
TCPSocketChild::SetSocket(TCPSocket* aSocket)
{
  mSocket = aSocket;
}

void
TCPSocketChild::GetHost(nsAString& aHost)
{
  aHost = mHost;
}

void
TCPSocketChild::GetPort(uint16_t* aPort)
{
  *aPort = mPort;
}

nsresult
TCPSocketChild::SetFilterName(const nsACString& aFilterName)
{
  if (!mFilterName.IsEmpty()) {
    // filter name can only be set once.
    return NS_ERROR_FAILURE;
  }
  mFilterName = aFilterName;
  return NS_OK;
}

bool
TCPSocketChild::RecvRequestDelete()
{
  mozilla::Unused << Send__delete__(this);
  return true;
}

} // namespace dom
} // namespace mozilla