summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/websocket/WebSocketConnectionParent.cpp
blob: 62bbdaede1aba8bf542ac3ea5bc4a4046e75e3a2 (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
/* -*- 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 "WebSocketConnectionParent.h"

#include "nsIHttpChannelInternal.h"
#include "nsITransportSecurityInfo.h"
#include "nsSerializationHelper.h"
#include "nsThreadUtils.h"
#include "WebSocketConnectionListener.h"

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS0(WebSocketConnectionParent)

WebSocketConnectionParent::WebSocketConnectionParent(
    nsIHttpUpgradeListener* aListener)
    : mUpgradeListener(aListener), mBackgroundThread(GetCurrentEventTarget()) {
  LOG(("WebSocketConnectionParent ctor %p\n", this));
  MOZ_ASSERT(mUpgradeListener);
}

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

mozilla::ipc::IPCResult WebSocketConnectionParent::RecvOnTransportAvailable(
    nsITransportSecurityInfo* aSecurityInfo) {
  LOG(("WebSocketConnectionParent::RecvOnTransportAvailable %p\n", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (aSecurityInfo) {
    MutexAutoLock lock(mMutex);
    mSecurityInfo = aSecurityInfo;
  }

  if (mUpgradeListener) {
    Unused << mUpgradeListener->OnWebSocketConnectionAvailable(this);
    mUpgradeListener = nullptr;
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionParent::RecvOnError(
    const nsresult& aStatus) {
  LOG(("WebSocketConnectionParent::RecvOnError %p\n", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (mListener) {
    mListener->OnError(aStatus);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionParent::RecvOnUpgradeFailed(
    const nsresult& aReason) {
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (mUpgradeListener) {
    Unused << mUpgradeListener->OnUpgradeFailed(aReason);
    mUpgradeListener = nullptr;
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionParent::RecvOnTCPClosed() {
  LOG(("WebSocketConnectionParent::RecvOnTCPClosed %p\n", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (mListener) {
    mListener->OnTCPClosed();
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult WebSocketConnectionParent::RecvOnDataReceived(
    nsTArray<uint8_t>&& aData) {
  LOG(("WebSocketConnectionParent::RecvOnDataReceived %p\n", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (mListener) {
    uint8_t* buffer = const_cast<uint8_t*>(aData.Elements());
    nsresult rv = mListener->OnDataReceived(buffer, aData.Length());
    if (NS_FAILED(rv)) {
      mListener->OnError(rv);
    }
  }
  return IPC_OK();
}

void WebSocketConnectionParent::ActorDestroy(ActorDestroyReason aWhy) {
  LOG(("WebSocketConnectionParent::ActorDestroy %p aWhy=%d\n", this, aWhy));
  if (!mClosed) {
    // Treat this as an error when IPC is closed before
    // WebSocketConnectionParent::Close() is called.
    RefPtr<WebSocketConnectionListener> listener;
    listener.swap(mListener);
    if (listener) {
      listener->OnError(NS_ERROR_FAILURE);
    }
  }
};

nsresult WebSocketConnectionParent::Init(
    WebSocketConnectionListener* aListener) {
  NS_ENSURE_ARG_POINTER(aListener);

  mListener = aListener;
  return NS_OK;
}

void WebSocketConnectionParent::GetIoTarget(nsIEventTarget** aTarget) {
  nsCOMPtr<nsIEventTarget> target = mBackgroundThread;
  return target.forget(aTarget);
}

void WebSocketConnectionParent::Close() {
  LOG(("WebSocketConnectionParent::Close %p\n", this));

  mClosed = true;

  RefPtr<WebSocketConnectionParent> self = this;
  auto task = [self{std::move(self)}]() {
    Unused << self->Send__delete__(self);
    self->mListener = nullptr;
  };

  if (mBackgroundThread->IsOnCurrentThread()) {
    task();
  } else {
    mBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "WebSocketConnectionParent::Close", std::move(task)));
  }
}

nsresult WebSocketConnectionParent::WriteOutputData(
    const uint8_t* aHdrBuf, uint32_t aHdrBufLength, const uint8_t* aPayloadBuf,
    uint32_t aPayloadBufLength) {
  LOG(("WebSocketConnectionParent::WriteOutputData %p", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

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

  nsTArray<uint8_t> data;
  data.AppendElements(aHdrBuf, aHdrBufLength);
  data.AppendElements(aPayloadBuf, aPayloadBufLength);
  return SendWriteOutputData(data) ? NS_OK : NS_ERROR_FAILURE;
}

nsresult WebSocketConnectionParent::StartReading() {
  LOG(("WebSocketConnectionParent::StartReading %p\n", this));

  RefPtr<WebSocketConnectionParent> self = this;
  auto task = [self{std::move(self)}]() {
    if (!self->CanSend()) {
      if (self->mListener) {
        self->mListener->OnError(NS_ERROR_NOT_AVAILABLE);
      }
      return;
    }

    Unused << self->SendStartReading();
  };

  if (mBackgroundThread->IsOnCurrentThread()) {
    task();
  } else {
    mBackgroundThread->Dispatch(NS_NewRunnableFunction(
        "WebSocketConnectionParent::SendStartReading", std::move(task)));
  }

  return NS_OK;
}

void WebSocketConnectionParent::DrainSocketData() {
  LOG(("WebSocketConnectionParent::DrainSocketData %p\n", this));
  MOZ_ASSERT(mBackgroundThread->IsOnCurrentThread());

  if (!CanSend()) {
    if (mListener) {
      mListener->OnError(NS_ERROR_NOT_AVAILABLE);
    }
    return;
  }

  Unused << SendDrainSocketData();
}

nsresult WebSocketConnectionParent::GetSecurityInfo(
    nsITransportSecurityInfo** aSecurityInfo) {
  LOG(("WebSocketConnectionParent::GetSecurityInfo() %p\n", this));
  MOZ_ASSERT(NS_IsMainThread());

  NS_ENSURE_ARG_POINTER(aSecurityInfo);

  MutexAutoLock lock(mMutex);
  NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
  return NS_OK;
}

}  // namespace net
}  // namespace mozilla