summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/peerconnection/adapters/quic_transport_host.cc
blob: 5cddf17200a539aa2978cc0869b7deb3ccab94a4 (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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/peerconnection/adapters/quic_transport_host.h"

#include <utility>

#include "third_party/blink/renderer/modules/peerconnection/adapters/ice_transport_host.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/p2p_quic_transport_factory.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/quic_stream_host.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/quic_stream_proxy.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/quic_transport_proxy.h"
#include "third_party/blink/renderer/modules/peerconnection/adapters/web_rtc_cross_thread_copier.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"

namespace blink {

QuicTransportHost::QuicTransportHost(
    base::WeakPtr<QuicTransportProxy> proxy,
    std::unique_ptr<P2PQuicTransportFactory> quic_transport_factory)
    : quic_transport_factory_(std::move(quic_transport_factory)),
      proxy_(std::move(proxy)) {
  DETACH_FROM_THREAD(thread_checker_);
  DCHECK(quic_transport_factory_);
  DCHECK(proxy_);
}

QuicTransportHost::~QuicTransportHost() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  // If the TaskRunner this is getting initialized on is destroyed before
  // Initialize is called then |ice_transport_host_| may still be null.
  if (ice_transport_host_) {
    ice_transport_host_->DisconnectConsumer(this);
  }
}

void QuicTransportHost::Initialize(IceTransportHost* ice_transport_host,
                                   const P2PQuicTransportConfig& config) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  DCHECK(ice_transport_host);
  DCHECK(!ice_transport_host_);
  ice_transport_host_ = ice_transport_host;
  IceTransportAdapter* ice_transport_adapter =
      ice_transport_host_->ConnectConsumer(this);
  quic_transport_ = quic_transport_factory_->CreateQuicTransport(
      /*delegate=*/this, ice_transport_adapter->packet_transport(), config);
}

scoped_refptr<base::SingleThreadTaskRunner> QuicTransportHost::proxy_thread()
    const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  return ice_transport_host_->proxy_thread();
}

scoped_refptr<base::SingleThreadTaskRunner> QuicTransportHost::host_thread()
    const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  return ice_transport_host_->host_thread();
}

void QuicTransportHost::Start(
    std::vector<std::unique_ptr<rtc::SSLFingerprint>> remote_fingerprints) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  quic_transport_->Start(std::move(remote_fingerprints));
}

void QuicTransportHost::Stop() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  quic_transport_->Stop();
}

void QuicTransportHost::CreateStream(
    std::unique_ptr<QuicStreamHost> stream_host) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  P2PQuicStream* p2p_stream = quic_transport_->CreateStream();
  stream_host->Initialize(this, p2p_stream);
  stream_hosts_.insert(
      std::make_pair(stream_host.get(), std::move(stream_host)));
}

void QuicTransportHost::OnRemoveStream(QuicStreamHost* stream_host_to_remove) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  auto it = stream_hosts_.find(stream_host_to_remove);
  DCHECK(it != stream_hosts_.end());
  stream_hosts_.erase(it);
}

void QuicTransportHost::OnRemoteStopped() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  stream_hosts_.clear();
  PostCrossThreadTask(
      *proxy_thread(), FROM_HERE,
      CrossThreadBind(&QuicTransportProxy::OnRemoteStopped, proxy_));
}

void QuicTransportHost::OnConnectionFailed(const std::string& error_details,
                                           bool from_remote) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  stream_hosts_.clear();
  PostCrossThreadTask(*proxy_thread(), FROM_HERE,
                      CrossThreadBind(&QuicTransportProxy::OnConnectionFailed,
                                      proxy_, error_details, from_remote));
}

void QuicTransportHost::OnConnected() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  PostCrossThreadTask(
      *proxy_thread(), FROM_HERE,
      CrossThreadBind(&QuicTransportProxy::OnConnected, proxy_));
}

void QuicTransportHost::OnStream(P2PQuicStream* p2p_stream) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  DCHECK(p2p_stream);

  auto stream_proxy = std::make_unique<QuicStreamProxy>();
  auto stream_host = std::make_unique<QuicStreamHost>();
  stream_proxy->set_host(stream_host->AsWeakPtr());
  stream_host->set_proxy(stream_proxy->AsWeakPtr());

  stream_host->Initialize(this, p2p_stream);

  stream_hosts_.insert(
      std::make_pair(stream_host.get(), std::move(stream_host)));

  PostCrossThreadTask(*proxy_thread(), FROM_HERE,
                      CrossThreadBind(&QuicTransportProxy::OnStream, proxy_,
                                      WTF::Passed(std::move(stream_proxy))));
}

}  // namespace blink