summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/ipc/WebrtcTCPSocketParent.cpp
blob: 0df8962757d5000bcd33985df5e2dae821f65e9b (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 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 "WebrtcTCPSocketParent.h"

#include "mozilla/net/NeckoParent.h"

#include "WebrtcTCPSocket.h"
#include "WebrtcTCPSocketLog.h"

using namespace mozilla::dom;
using namespace mozilla::ipc;

namespace mozilla::net {

mozilla::ipc::IPCResult WebrtcTCPSocketParent::RecvAsyncOpen(
    const nsACString& aHost, const int& aPort, const nsACString& aLocalAddress,
    const int& aLocalPort, const bool& aUseTls,
    const Maybe<WebrtcProxyConfig>& aProxyConfig) {
  LOG(("WebrtcTCPSocketParent::RecvAsyncOpen %p to %s:%d\n", this,
       PromiseFlatCString(aHost).get(), aPort));

  MOZ_ASSERT(mChannel, "webrtc TCP socket should be non-null");
  if (!mChannel) {
    return IPC_FAIL(this, "Called with null channel.");
  }

  mChannel->Open(aHost, aPort, aLocalAddress, aLocalPort, aUseTls,
                 aProxyConfig);

  return IPC_OK();
}

mozilla::ipc::IPCResult WebrtcTCPSocketParent::RecvWrite(
    nsTArray<uint8_t>&& aWriteData) {
  LOG(("WebrtcTCPSocketParent::RecvWrite %p for %zu\n", this,
       aWriteData.Length()));

  // Need to check this here in case there are Writes in the queue after OnClose
  if (mChannel) {
    mChannel->Write(std::move(aWriteData));
  }

  return IPC_OK();
}

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

  CleanupChannel();

  IProtocol* mgr = Manager();
  if (!Send__delete__(this)) {
    return IPC_FAIL_NO_REASON(mgr);
  }

  return IPC_OK();
}

void WebrtcTCPSocketParent::ActorDestroy(ActorDestroyReason aWhy) {
  LOG(("WebrtcTCPSocketParent::ActorDestroy %p for %d\n", this, aWhy));

  CleanupChannel();
}

WebrtcTCPSocketParent::WebrtcTCPSocketParent(const Maybe<dom::TabId>& aTabId) {
  MOZ_COUNT_CTOR(WebrtcTCPSocketParent);

  LOG(("WebrtcTCPSocketParent::WebrtcTCPSocketParent %p\n", this));

  mChannel = new WebrtcTCPSocket(this);
  if (aTabId.isSome()) {
    mChannel->SetTabId(*aTabId);
  }
}

WebrtcTCPSocketParent::~WebrtcTCPSocketParent() {
  MOZ_COUNT_DTOR(WebrtcTCPSocketParent);

  LOG(("WebrtcTCPSocketParent::~WebrtcTCPSocketParent %p\n", this));

  CleanupChannel();
}

// WebrtcTCPSocketCallback
void WebrtcTCPSocketParent::OnClose(nsresult aReason) {
  LOG(("WebrtcTCPSocketParent::OnClose %p\n", this));

  if (mChannel) {
    Unused << SendOnClose(aReason);
  }

  CleanupChannel();
}

void WebrtcTCPSocketParent::OnRead(nsTArray<uint8_t>&& aReadData) {
  LOG(("WebrtcTCPSocketParent::OnRead %p %zu\n", this, aReadData.Length()));

  if (mChannel && !SendOnRead(std::move(aReadData))) {
    CleanupChannel();
  }
}

void WebrtcTCPSocketParent::OnConnected(const nsACString& aProxyType) {
  LOG(("WebrtcTCPSocketParent::OnConnected %p\n", this));

  if (mChannel && !SendOnConnected(aProxyType)) {
    CleanupChannel();
  }
}

void WebrtcTCPSocketParent::CleanupChannel() {
  if (mChannel) {
    mChannel->Close();
    mChannel = nullptr;
  }
}

}  // namespace mozilla::net