File: update_check_helper.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (97 lines) | stat: -rw-r--r-- 3,768 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
// 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 "components/safety_check/update_check_helper.h"

#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/safety_check/url_constants.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"

namespace safety_check {

namespace {

// Request timeout of 5 seconds to not interrupt the completion of Safety check.
// The user can always start a new Safety check if a request times out.
constexpr base::TimeDelta kConnectionTimeout = base::Seconds(5);

// Maximum number of retries for sending the request.
constexpr int kMaxRetries = 2;

const net::NetworkTrafficAnnotationTag kTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("safety_check_update_connectivity",
                                        R"(
      semantics {
        sender: "Safety Check Browser Updates Check"
        description:
          "If during the updates check part of the Safety check the version "
          "updater returns a generic error status, this request is used to "
          "determine whether it is caused by connectivity issues."
        trigger:
          "When the user started the Safety check in settings and the browser "
          "updates check fails."
        data:
          "No data is sent with the request."
        destination: GOOGLE_OWNED_SERVICE
      }
      policy {
        cookies_allowed: NO
        setting:
          "No user-visible setting for this feature because this is part of "
          "user-triggered Safety check, which explicitly includes an update "
          "check."
        policy_exception_justification:
          "Not implemented, considered not required."
      })");

}  // namespace

UpdateCheckHelper::UpdateCheckHelper(
    scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
    : url_loader_factory_(url_loader_factory) {}

UpdateCheckHelper::~UpdateCheckHelper() = default;

void UpdateCheckHelper::CheckConnectivity(
    ConnectivityCheckCallback connection_check_callback) {
  result_callback_ = std::move(connection_check_callback);

  // Create a request with no data or cookies.
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = GURL(kConnectivityCheckUrl);
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  resource_request->load_flags = net::LOAD_DISABLE_CACHE;
  resource_request->redirect_mode = ::network::mojom::RedirectMode::kError;

  url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
                                                 kTrafficAnnotation);
  url_loader_->SetTimeoutDuration(kConnectionTimeout);
  url_loader_->SetRetryOptions(
      kMaxRetries,
      network::SimpleURLLoader::RetryMode::RETRY_ON_5XX |
          network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);
  url_loader_->DownloadHeadersOnly(
      url_loader_factory_.get(),
      base::BindOnce(&UpdateCheckHelper::OnURLLoadComplete,
                     base::Unretained(this)));
}

UpdateCheckHelper::UpdateCheckHelper() = default;

void UpdateCheckHelper::OnURLLoadComplete(
    scoped_refptr<net::HttpResponseHeaders> headers) {
  DCHECK(url_loader_);
  bool connected = headers && headers->response_code() == net::HTTP_NO_CONTENT;
  url_loader_.reset();
  std::move(result_callback_).Run(connected);
}

}  // namespace safety_check