summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/websocket/WebSocketConnectionChild.cpp
blob: 543eafebf12755371b43f6be192c882532af86b6 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* 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 "WebSocketLog.h"
#include "WebSocketConnectionChild.h"

#include "WebSocketConnection.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "nsISerializable.h"
#include "nsITLSSocketControl.h"
#include "nsITransportSecurityInfo.h"
#include "nsNetCID.h"
#include "nsSerializationHelper.h"
#include "nsSocketTransportService2.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS(WebSocketConnectionChild, nsIHttpUpgradeListener)

WebSocketConnectionChild::WebSocketConnectionChild() {
  LOG(("WebSocketConnectionChild ctor %p\n", this));
}

WebSocketConnectionChild::~WebSocketConnectionChild() {
  LOG(("WebSocketConnectionChild dtor %p\n", this));
}

void WebSocketConnectionChild::Init(uint32_t aListenerId) {
  nsresult rv;
  mSocketThread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  if (!mSocketThread) {
    return;
  }

  RefPtr<WebSocketConnectionChild> self = this;
  mSocketThread->Dispatch(NS_NewRunnableFunction(
      "WebSocketConnectionChild::Init", [self, aListenerId]() {
        mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc::
            BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread();
        if (!actorChild) {
          return;
        }

        Unused << actorChild->SendPWebSocketConnectionConstructor(self,
                                                                  aListenerId);
      }));
}

// nsIHttpUpgradeListener
NS_IMETHODIMP
WebSocketConnectionChild::OnTransportAvailable(
    nsISocketTransport* aTransport, nsIAsyncInputStream* aSocketIn,
    nsIAsyncOutputStream* aSocketOut) {
  LOG(("WebSocketConnectionChild::OnTransportAvailable %p\n", this));
  if (!OnSocketThread()) {
    nsCOMPtr<nsISocketTransport> transport = aTransport;
    nsCOMPtr<nsIAsyncInputStream> inputStream = aSocketIn;
    nsCOMPtr<nsIAsyncOutputStream> outputStream = aSocketOut;
    RefPtr<WebSocketConnectionChild> self = this;
    return mSocketThread->Dispatch(
        NS_NewRunnableFunction("WebSocketConnectionChild::OnTransportAvailable",
                               [self, transport, inputStream, outputStream]() {
                                 Unused << self->OnTransportAvailable(
                                     transport, inputStream, outputStream);
                               }),
        NS_DISPATCH_NORMAL);
  }

  LOG(("WebSocketConnectionChild::OnTransportAvailable %p\n", this));
  MOZ_ASSERT(OnSocketThread());
  MOZ_ASSERT(!mConnection, "already called");
  MOZ_ASSERT(aTransport);

  if (!CanSend()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsCOMPtr<nsITLSSocketControl> tlsSocketControl;
  aTransport->GetTlsSocketControl(getter_AddRefs(tlsSocketControl));
  nsCOMPtr<nsITransportSecurityInfo> securityInfo(
      do_QueryInterface(tlsSocketControl));

  RefPtr<WebSocketConnection> connection =
      new WebSocketConnection(aTransport, aSocketIn, aSocketOut);
  nsresult rv = connection->Init(this);
  if (NS_FAILED(rv)) {
    Unused << OnUpgradeFailed(rv);
    return NS_OK;
  }

  mConnection = std::move(connection);

  Unused << SendOnTransportAvailable(securityInfo);
  return NS_OK;
}

NS_IMETHODIMP
WebSocketConnectionChild::OnUpgradeFailed(nsresult aReason) {
  if (!OnSocketThread()) {
    return mSocketThread->Dispatch(NewRunnableMethod<nsresult>(
        "WebSocketConnectionChild::OnUpgradeFailed", this,
        &WebSocketConnectionChild::OnUpgradeFailed, aReason));
  }

  if (CanSend()) {
    Unused << SendOnUpgradeFailed(aReason);
  }
  return NS_OK;
}

NS_IMETHODIMP
WebSocketConnectionChild::OnWebSocketConnectionAvailable(
    WebSocketConnectionBase* aConnection) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

mozilla::ipc::IPCResult WebSocketConnectionChild::RecvWriteOutputData(
    nsTArray<uint8_t>&& aData) {
  LOG(("WebSocketConnectionChild::RecvWriteOutputData %p\n", this));

  if (!mConnection) {
    OnError(NS_ERROR_NOT_AVAILABLE);
    return IPC_OK();
  }

  mConnection->WriteOutputData(std::move(aData));
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionChild::RecvStartReading() {
  LOG(("WebSocketConnectionChild::RecvStartReading %p\n", this));

  if (!mConnection) {
    OnError(NS_ERROR_NOT_AVAILABLE);
    return IPC_OK();
  }

  mConnection->StartReading();
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionChild::RecvDrainSocketData() {
  LOG(("WebSocketConnectionChild::RecvDrainSocketData %p\n", this));

  if (!mConnection) {
    OnError(NS_ERROR_NOT_AVAILABLE);
    return IPC_OK();
  }

  mConnection->DrainSocketData();
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionChild::Recv__delete__() {
  LOG(("WebSocketConnectionChild::Recv__delete__ %p\n", this));

  if (!mConnection) {
    OnError(NS_ERROR_NOT_AVAILABLE);
    return IPC_OK();
  }

  mConnection->Close();
  mConnection = nullptr;
  return IPC_OK();
}

void WebSocketConnectionChild::OnError(nsresult aStatus) {
  LOG(("WebSocketConnectionChild::OnError %p\n", this));

  if (CanSend()) {
    Unused << SendOnError(aStatus);
  }
}

void WebSocketConnectionChild::OnTCPClosed() {
  LOG(("WebSocketConnectionChild::OnTCPClosed %p\n", this));

  if (CanSend()) {
    Unused << SendOnTCPClosed();
  }
}

nsresult WebSocketConnectionChild::OnDataReceived(uint8_t* aData,
                                                  uint32_t aCount) {
  LOG(("WebSocketConnectionChild::OnDataReceived %p\n", this));

  if (CanSend()) {
    nsTArray<uint8_t> data;
    data.AppendElements(aData, aCount);
    Unused << SendOnDataReceived(data);
  }

  return NS_OK;
}

void WebSocketConnectionChild::ActorDestroy(ActorDestroyReason aWhy) {
  LOG(("WebSocketConnectionChild::ActorDestroy %p\n", this));
  if (mConnection) {
    mConnection->Close();
    mConnection = nullptr;
  }
}

}  // namespace net
}  // namespace mozilla