File: transport_context.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 4,903 bytes parent folder | download | duplicates (5)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/transport_context.h"

#include <sstream>
#include <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "components/webrtc/thread_wrapper.h"
#include "net/url_request/url_request_context_getter.h"
#include "remoting/base/logging.h"
#include "remoting/base/oauth_token_getter.h"
#include "remoting/protocol/chromium_port_allocator_factory.h"
#include "remoting/protocol/ice_config_fetcher.h"
#include "remoting/protocol/port_allocator_factory.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "third_party/webrtc/rtc_base/socket_address.h"

namespace remoting::protocol {

namespace {

// Use a cooldown period to prevent multiple service requests in case of a bug.
constexpr base::TimeDelta kIceConfigRequestCooldown = base::Minutes(2);

void PrintIceConfig(const IceConfig& ice_config) {
  std::stringstream ss;
  ss << "\nIceConfig: {\n";
  ss << "  stun: [\n";
  for (auto& stun_server : ice_config.stun_servers) {
    ss << "    " << stun_server.ToString() << ",\n";
  }
  ss << "  ]\n";
  ss << "  turn: [\n";
  for (auto& turn_server : ice_config.turn_servers) {
    ss << "    {\n";
    ss << "      username: " << turn_server.credentials.username << "\n";
    ss << "      password: " << turn_server.credentials.password << "\n";
    for (auto& port : turn_server.ports) {
      ss << "      port: " << port.address.ToString() << "\n";
    }
    ss << "    },\n";
  }
  ss << "  ]\n";
  ss << "  expiration time: " << ice_config.expiration_time << "\n";
  ss << "  max_bitrate_kbps: " << ice_config.max_bitrate_kbps << "\n";
  ss << "}";
  HOST_LOG << ss.str();
}

}  // namespace

// static
scoped_refptr<TransportContext> TransportContext::ForTests(TransportRole role) {
  webrtc::ThreadWrapper::EnsureForCurrentMessageLoop();
  return base::MakeRefCounted<TransportContext>(
      std::make_unique<protocol::ChromiumPortAllocatorFactory>(),
      webrtc::ThreadWrapper::current()->SocketServer(),
      /*ice_config_fetcher=*/nullptr, role);
}

TransportContext::TransportContext(
    std::unique_ptr<PortAllocatorFactory> port_allocator_factory,
    webrtc::SocketFactory* socket_factory,
    std::unique_ptr<IceConfigFetcher> ice_config_fetcher,
    TransportRole role)
    : port_allocator_factory_(std::move(port_allocator_factory)),
      socket_factory_(socket_factory),
      role_(role),
      ice_config_fetcher_(std::move(ice_config_fetcher)) {
  DCHECK(socket_factory_);
}

TransportContext::~TransportContext() = default;

void TransportContext::GetIceConfig(OnIceConfigCallback callback) {
  EnsureFreshIceConfig();

  // If there is a pending |ice_config_request_| then delay the callback until
  // the request is finished.
  if (ice_config_request_in_flight_) {
    pending_ice_config_callbacks_.push_back(std::move(callback));
  } else {
    HOST_LOG << "Using cached ICE Config.";
    PrintIceConfig(ice_config_);
    std::move(callback).Run(ice_config_);
  }
}

void TransportContext::EnsureFreshIceConfig() {
  // Check if request is already pending.
  if (ice_config_request_in_flight_) {
    HOST_LOG << "ICE Config request is already pending.";
    return;
  }

  if (last_request_completion_time_.is_max()) {
    HOST_LOG << "Skipping ICE Config request as refreshing is disabled";
    return;
  }

  if (base::Time::Now() >
      (last_request_completion_time_ + kIceConfigRequestCooldown)) {
    ice_config_request_in_flight_ = true;
    ice_config_fetcher_->GetIceConfig(
        base::BindOnce(&TransportContext::OnIceConfig, base::Unretained(this)));
  } else {
    HOST_LOG << "Skipping ICE Config request made during the cooldown period.";
  }
}

void TransportContext::OnIceConfig(std::optional<IceConfig> ice_config) {
  ice_config_ = ice_config.value_or(IceConfig());
  ice_config_request_in_flight_ = false;

  if (!ice_config_.is_null()) {
    // Only reset |last_request_completion_time_| if we received a valid config.
    // If we received an empty config, it could mean a problem in the backend,
    // a network issue, or some other error. Regardless of the specific error,
    // we should try to fetch a new config the next time one is requested.
    last_request_completion_time_ = base::Time::Now();
  }

  HOST_LOG << "Using newly requested ICE Config.";
  PrintIceConfig(ice_config_);

  auto& callback_list = pending_ice_config_callbacks_;
  while (!callback_list.empty()) {
    std::move(callback_list.front()).Run(ice_config_);
    callback_list.pop_front();
  }
}

int TransportContext::GetTurnMaxRateKbps() const {
  return ice_config_.max_bitrate_kbps;
}

}  // namespace remoting::protocol