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 213 214 215 216 217 218 219
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/net/network_diagnostics/video_conferencing_routine.h"
#include <optional>
#include <string>
#include <utility>
#include "base/logging.h"
#include "base/time/time.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics_util.h"
#include "chrome/browser/ash/net/network_diagnostics/udp_prober.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/net_errors.h"
#include "services/network/public/mojom/network_context.mojom.h"
namespace ash {
namespace network_diagnostics {
namespace {
namespace mojom = ::chromeos::network_diagnostics::mojom;
const char kDefaultStunServer[] = "stun.l.google.com";
} // namespace
// TODO(crbug/1227877): Move support details to the UI.
const char kSupportDetails[] = "https://support.google.com/a/answer/1279090";
const base::TimeDelta kTimeoutAfterHostResolution = base::Seconds(10);
VideoConferencingRoutine::VideoConferencingRoutine(
mojom::RoutineCallSource source)
: NetworkDiagnosticsRoutine(source),
stun_server_hostname_(kDefaultStunServer),
udp_prober_getter_callback_(base::BindRepeating(
&VideoConferencingRoutine::CreateAndExecuteUdpProber)),
tls_prober_getter_callback_(base::BindRepeating(
&VideoConferencingRoutine::CreateAndExecuteTlsProber)),
udp_ports_(util::GetUdpPortsForGoogleStunServer()),
tcp_ports_(util::GetTcpPortsForGoogleStunServer()),
media_hostnames_(util::GetDefaultMediaUrls()) {}
VideoConferencingRoutine::VideoConferencingRoutine(
mojom::RoutineCallSource source,
const std::string& stun_server_hostname)
: NetworkDiagnosticsRoutine(source),
stun_server_hostname_(stun_server_hostname),
udp_prober_getter_callback_(base::BindRepeating(
&VideoConferencingRoutine::CreateAndExecuteUdpProber)),
tls_prober_getter_callback_(base::BindRepeating(
&VideoConferencingRoutine::CreateAndExecuteTlsProber)),
udp_ports_(util::GetUdpPortsForCustomStunServer()),
tcp_ports_(util::GetTcpPortsForCustomStunServer()),
media_hostnames_(util::GetDefaultMediaUrls()) {}
VideoConferencingRoutine::~VideoConferencingRoutine() = default;
mojom::RoutineType VideoConferencingRoutine::Type() {
return mojom::RoutineType::kVideoConferencing;
}
void VideoConferencingRoutine::Run() {
ProbeStunServerOverUdp();
}
void VideoConferencingRoutine::AnalyzeResultsAndExecuteCallback() {
std::optional<std::string> support_details = kSupportDetails;
set_verdict(mojom::RoutineVerdict::kProblem);
if (!open_udp_port_found_) {
problems_.push_back(mojom::VideoConferencingProblem::kUdpFailure);
}
if (!open_tcp_port_found_) {
problems_.push_back(mojom::VideoConferencingProblem::kTcpFailure);
}
if (!media_hostnames_reachable_) {
problems_.push_back(mojom::VideoConferencingProblem::kMediaFailure);
}
if (problems_.empty()) {
set_verdict(mojom::RoutineVerdict::kNoProblem);
support_details = std::nullopt;
}
set_problems(mojom::RoutineProblems::NewVideoConferencingProblems(problems_));
ExecuteCallback();
}
void VideoConferencingRoutine::ProbeStunServerOverUdp() {
if (udp_ports_.empty()) {
ProbeStunServerOverTcp();
return;
}
int port = udp_ports_.back();
udp_ports_.pop_back();
AttemptUdpProbe(net::HostPortPair(stun_server_hostname_, port));
}
void VideoConferencingRoutine::ProbeStunServerOverTcp() {
if (tcp_ports_.empty()) {
ProbeMediaHostnames();
return;
}
int port = tcp_ports_.back();
tcp_ports_.pop_back();
AttemptTcpProbe(net::HostPortPair(stun_server_hostname_, port));
}
void VideoConferencingRoutine::ProbeMediaHostnames() {
if (media_hostnames_.empty()) {
AnalyzeResultsAndExecuteCallback();
return;
}
auto host_pair = net::HostPortPair::FromURL(media_hostnames_.back());
media_hostnames_.pop_back();
AttemptTlsProbe(host_pair);
}
network::mojom::NetworkContext* VideoConferencingRoutine::GetNetworkContext() {
Profile* profile = util::GetUserProfile();
DCHECK(profile);
return profile->GetDefaultStoragePartition()->GetNetworkContext();
}
std::unique_ptr<UdpProber> VideoConferencingRoutine::CreateAndExecuteUdpProber(
network::NetworkContextGetter network_context_getter,
net::HostPortPair host_port_pair,
base::span<const uint8_t> data,
net::NetworkTrafficAnnotationTag tag,
base::TimeDelta timeout_after_host_resolution,
UdpProber::UdpProbeCompleteCallback callback) {
return UdpProber::Start(std::move(network_context_getter), host_port_pair,
std::move(data), tag, timeout_after_host_resolution,
std::move(callback));
}
std::unique_ptr<TlsProber> VideoConferencingRoutine::CreateAndExecuteTlsProber(
network::NetworkContextGetter network_context_getter,
net::HostPortPair host_port_pair,
bool negotiate_tls,
TlsProber::TlsProbeCompleteCallback callback) {
return std::make_unique<TlsProber>(std::move(network_context_getter),
host_port_pair, negotiate_tls,
std::move(callback));
}
void VideoConferencingRoutine::AttemptUdpProbe(
net::HostPortPair host_port_pair) {
// Store the instance of UdpProber.
udp_prober_ = udp_prober_getter_callback_.Run(
base::BindRepeating(&VideoConferencingRoutine::GetNetworkContext),
host_port_pair, util::GetStunHeader(),
util::GetStunNetworkAnnotationTag(), kTimeoutAfterHostResolution,
base::BindOnce(&VideoConferencingRoutine::OnUdpProbeComplete,
weak_ptr()));
}
void VideoConferencingRoutine::AttemptTcpProbe(
net::HostPortPair host_port_pair) {
tls_prober_ = tls_prober_getter_callback_.Run(
base::BindRepeating(&VideoConferencingRoutine::GetNetworkContext),
host_port_pair,
/*negotiate_tls=*/false,
base::BindOnce(&VideoConferencingRoutine::OnTcpProbeComplete,
weak_ptr()));
}
void VideoConferencingRoutine::AttemptTlsProbe(
net::HostPortPair host_port_pair) {
// Store the instance of TlsProber.
tls_prober_ = tls_prober_getter_callback_.Run(
base::BindRepeating(&VideoConferencingRoutine::GetNetworkContext),
host_port_pair,
/*negotiate_tls=*/true,
base::BindOnce(&VideoConferencingRoutine::OnTlsProbeComplete,
weak_ptr()));
}
void VideoConferencingRoutine::OnUdpProbeComplete(
int result,
UdpProber::ProbeExitEnum probe_exit_enum) {
if (result == net::OK) {
open_udp_port_found_ = true;
// Only one open UDP port needs to be detected.
ProbeStunServerOverTcp();
return;
}
ProbeStunServerOverUdp();
}
void VideoConferencingRoutine::OnTcpProbeComplete(
int result,
TlsProber::ProbeExitEnum probe_exit_enum) {
if (result == net::OK) {
open_tcp_port_found_ = true;
// Only one open TCP port needs to be detected.
ProbeMediaHostnames();
return;
}
ProbeStunServerOverTcp();
}
void VideoConferencingRoutine::OnTlsProbeComplete(
int result,
TlsProber::ProbeExitEnum probe_exit_enum) {
if (result != net::OK) {
media_hostnames_reachable_ = false;
// All media hostnames must be reachable.
AnalyzeResultsAndExecuteCallback();
return;
}
ProbeMediaHostnames();
}
} // namespace network_diagnostics
} // namespace ash
|