File: arc_http_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 (139 lines) | stat: -rw-r--r-- 4,893 bytes parent folder | download | duplicates (6)
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
// 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_http_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/http/http_status_code.h"
#include "url/gurl.h"

namespace ash {
namespace network_diagnostics {

namespace {

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

constexpr int kTotalHostsToQuery = 3;
// The length of a random eight letter prefix.
constexpr int kHostPrefixLength = 8;
constexpr char kHttpScheme[] = "http://";

// Requests taking longer than 1000 ms are problematic.
constexpr int kProblemLatencyMs = 1000;
// Requests lasting between 500 ms and 1000 ms are potentially problematic.
constexpr int kPotentialProblemLatencyMs = 500;

}  // namespace

ArcHttpRoutine::ArcHttpRoutine(mojom::RoutineCallSource source)
    : NetworkDiagnosticsRoutine(source),
      hostnames_to_request_http_(
          util::GetRandomHostsWithSchemeAndGenerate204Path(kTotalHostsToQuery,
                                                           kHostPrefixLength,
                                                           kHttpScheme)) {}

ArcHttpRoutine::~ArcHttpRoutine() = default;

mojom::RoutineType ArcHttpRoutine::Type() {
  return mojom::RoutineType::kArcHttp;
}

void ArcHttpRoutine::Run() {
  AttemptNextRequest();
}

void ArcHttpRoutine::AttemptNextRequest() {
  // If no more hostnames to request, report success and analyze results.
  if (hostnames_to_request_http_.empty()) {
    successfully_requested_targets_ = true;
    AnalyzeResultsAndExecuteCallback();
    return;
  }

  auto url = GURL(hostnames_to_request_http_.back());
  hostnames_to_request_http_.pop_back();

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

arc::mojom::NetInstance* ArcHttpRoutine::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(), HttpTest);
  if (!net_instance) {
    failed_to_get_net_instance_service_for_http_test_ = true;
    AnalyzeResultsAndExecuteCallback();
    return nullptr;
  }
  return net_instance;
}

void ArcHttpRoutine::OnRequestComplete(
    arc::mojom::ArcHttpTestResultPtr result) {
  if (!result->is_successful ||
      // We generated path to a page that returns 204 response.
      result->status_code != net::HttpStatusCode::HTTP_NO_CONTENT) {
    successfully_requested_targets_ = false;
    AnalyzeResultsAndExecuteCallback();
    return;
  }

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

void ArcHttpRoutine::AnalyzeResultsAndExecuteCallback() {
  if (!successfully_requested_targets_) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcHttpProblem::kFailedHttpRequests);
  } else if (failed_to_get_arc_service_manager_) {
    set_verdict(mojom::RoutineVerdict::kNotRun);
    problems_.push_back(mojom::ArcHttpProblem::kFailedToGetArcServiceManager);
  } else if (failed_to_get_net_instance_service_for_http_test_) {
    set_verdict(mojom::RoutineVerdict::kNotRun);
    problems_.push_back(
        mojom::ArcHttpProblem::kFailedToGetNetInstanceForHttpTest);
  } else if (max_latency_ <= kProblemLatencyMs &&
             max_latency_ > kPotentialProblemLatencyMs) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcHttpProblem::kHighLatency);
  } else if (max_latency_ > kProblemLatencyMs) {
    set_verdict(mojom::RoutineVerdict::kProblem);
    problems_.push_back(mojom::ArcHttpProblem::kVeryHighLatency);
  } else {
    set_verdict(mojom::RoutineVerdict::kNoProblem);
  }
  set_problems(mojom::RoutineProblems::NewArcHttpProblems(problems_));
  ExecuteCallback();
}

}  // namespace network_diagnostics
}  // namespace ash