File: arc_dns_resolution_routine.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (134 lines) | stat: -rw-r--r-- 4,769 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
// Copyright 2021 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/arc_dns_resolution_routine.h"

#include <algorithm>
#include <utility>

#include "base/functional/bind.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics_util.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "net/dns/public/dns_protocol.h"

namespace ash {
namespace network_diagnostics {

namespace {

namespace mojom = ::chromeos::network_diagnostics::mojom;

// These hostnames were chosen because they need to be resolved for a
// successful ARC provisioning step.
constexpr char kHostname1[] = "www.googleapis.com";
constexpr char kHostname2[] = "android.clients.google.com";
constexpr char kHostname3[] = "android.googleapis.com";

}  // namespace

ArcDnsResolutionRoutine::ArcDnsResolutionRoutine(
    mojom::RoutineCallSource source)
    : NetworkDiagnosticsRoutine(source) {
  hostnames_to_resolve_dns_ = {kHostname1, kHostname2, kHostname3};
}

ArcDnsResolutionRoutine::~ArcDnsResolutionRoutine() = default;

mojom::RoutineType ArcDnsResolutionRoutine::Type() {
  return mojom::RoutineType::kArcDnsResolution;
}

void ArcDnsResolutionRoutine::Run() {
  AttemptNextQuery();
}

void ArcDnsResolutionRoutine::AttemptNextQuery() {
  // If no more hostnames to resolve, report success and analyze results.
  if (hostnames_to_resolve_dns_.empty()) {
    successfully_resolved_hostnames_ = true;
    AnalyzeResultsAndExecuteCallback();
    return;
  }

  std::string hostname = hostnames_to_resolve_dns_.back();
  hostnames_to_resolve_dns_.pop_back();

  // Call the DnsResolutionTest API from the instance of NetInstance.
  arc::mojom::NetInstance* net_instance = GetNetInstance();
  if (net_instance) {
    net_instance->DnsResolutionTest(
        "" /* default network */, hostname,
        base::BindOnce(&ArcDnsResolutionRoutine::OnQueryComplete,
                       weak_ptr_factory_.GetWeakPtr()));
  }
}

arc::mojom::NetInstance* ArcDnsResolutionRoutine::GetNetInstance() {
  // If |net_instance_| is not already set for testing purposes, get instance
  // of NetInstance service.
  if (net_instance_) {
    return net_instance_;
  }

  // Call the singleton for ArcServiceManager and check if it is null.
  auto* arc_service_manager = arc::ArcServiceManager::Get();
  if (!arc_service_manager) {
    failed_to_get_arc_service_manager_ = true;
    AnalyzeResultsAndExecuteCallback();
    return nullptr;
  }

  // Get an instance of the NetInstance service and check if it is null.
  auto* arc_bridge_service = arc_service_manager->arc_bridge_service();
  arc::mojom::NetInstance* net_instance =
      ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service->net(), DnsResolutionTest);
  if (!net_instance) {
    failed_to_get_net_instance_service_for_dns_resolution_test_ = true;
    AnalyzeResultsAndExecuteCallback();
    return nullptr;
  }
  return net_instance;
}

void ArcDnsResolutionRoutine::OnQueryComplete(
    arc::mojom::ArcDnsResolutionTestResultPtr result) {
  if (!result->is_successful ||
      result->response_code != net::dns_protocol::kRcodeNOERROR) {
    successfully_resolved_hostnames_ = false;
    AnalyzeResultsAndExecuteCallback();
    return;
  }

  max_latency_ = std::max(max_latency_, result->duration_ms);
  AttemptNextQuery();
}

void ArcDnsResolutionRoutine::AnalyzeResultsAndExecuteCallback() {
  if (!successfully_resolved_hostnames_) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcDnsResolutionProblem::kFailedDnsQueries);
  } else if (failed_to_get_arc_service_manager_) {
    set_verdict(mojom::RoutineVerdict::kNotRun);
    problems_.push_back(
        mojom::ArcDnsResolutionProblem::kFailedToGetArcServiceManager);
  } else if (failed_to_get_net_instance_service_for_dns_resolution_test_) {
    set_verdict(mojom::RoutineVerdict::kNotRun);
    problems_.push_back(mojom::ArcDnsResolutionProblem::
                            kFailedToGetNetInstanceForDnsResolutionTest);
  } else if (max_latency_ <= util::kDnsProblemLatencyMs &&
             max_latency_ > util::kDnsPotentialProblemLatencyMs) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcDnsResolutionProblem::kHighLatency);
  } else if (max_latency_ > util::kDnsProblemLatencyMs) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcDnsResolutionProblem::kVeryHighLatency);
  } else {
    set_verdict(mojom::RoutineVerdict::kNoProblem);
  }
  set_problems(mojom::RoutineProblems::NewArcDnsResolutionProblems(problems_));
  ExecuteCallback();
}

}  // namespace network_diagnostics
}  // namespace ash