File: translate_url_fetcher.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 (138 lines) | stat: -rw-r--r-- 5,196 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
// Copyright 2014 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/translate/core/browser/translate_url_fetcher.h"

#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/variations/net/variations_http_headers.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/simple_url_loader.h"

namespace translate {

namespace {

// Retry parameter for fetching.
const int kMaxRetry = 16;

}  // namespace

TranslateURLFetcher::TranslateURLFetcher()
    : state_(IDLE), retry_count_(0), max_retry_on_5xx_(0) {}

TranslateURLFetcher::~TranslateURLFetcher() = default;

bool TranslateURLFetcher::Request(const GURL& url,
                                  TranslateURLFetcher::Callback callback,
                                  bool is_incognito) {
  // This function is not supposed to be called if the previous operation is not
  // finished.
  if (state_ == REQUESTING) {
    NOTREACHED();
  }

  if (retry_count_ >= kMaxRetry)
    return false;
  retry_count_++;

  state_ = REQUESTING;
  url_ = url;
  callback_ = std::move(callback);

  // If the TranslateDownloadManager's request context getter is nullptr then
  // shutdown is in progress. Abort the request, which can't proceed with a
  // null url_loader_factory.
  network::mojom::URLLoaderFactory* url_loader_factory =
      TranslateDownloadManager::GetInstance()->url_loader_factory().get();
  if (!url_loader_factory)
    return false;

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("translate_url_fetcher", R"(
        semantics {
          sender: "Translate"
          description:
            "Chrome can provide translations for the web sites visited by the "
            "user. If this feature is enabled, Chrome sends network requests "
            "to download the list of supported languages and a library to "
            "perform translations."
          trigger:
            "When Chrome starts, it downloads the list of supported languages "
            "for translation. The first time Chrome decides to offer "
            "translation of a web site, it triggers a popup to ask "
            "if user wants a translation and if user approves, "
            "translation library is downloaded. The library is cached for a "
            "day and is not fetched if it is available and fresh."
          data:
            "Current locale is sent to fetch the list of supported languages. "
            "Translation library that is obtained via this interface would "
            "perform actual translation, and it will send words and phrases in "
            "the site to the server to translate it, but this request doesn't "
            "send any words."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "Users can enable/disable this feature by toggling 'Offer to "
            "translate pages that aren't in a language you read.' in Chrome "
            "settings under Languages. The list of supported languages is "
            "downloaded regardless of the settings."
          chrome_policy {
            TranslateEnabled {
              TranslateEnabled: false
            }
          }
        })");

  // Create and initialize URL loader.
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url_;
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  if (!extra_request_header_.empty())
    resource_request->headers.AddHeaderFromString(extra_request_header_);

  simple_loader_ =
      variations::CreateSimpleURLLoaderWithVariationsHeaderUnknownSignedIn(
          std::move(resource_request),
          is_incognito ? variations::InIncognito::kYes
                       : variations::InIncognito::kNo,
          traffic_annotation);
  // Set retry parameter for HTTP status code 5xx. This doesn't work against
  // 106 (net::ERR_INTERNET_DISCONNECTED) and so on.
  // TranslateLanguageList handles network status, and implements retry.
  if (max_retry_on_5xx_) {
    simple_loader_->SetRetryOptions(
        max_retry_on_5xx_, network::SimpleURLLoader::RetryMode::RETRY_ON_5XX);
  }

  simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      url_loader_factory,
      base::BindOnce(&TranslateURLFetcher::OnSimpleLoaderComplete,
                     base::Unretained(this)));
  return true;
}

void TranslateURLFetcher::OnSimpleLoaderComplete(
    std::unique_ptr<std::string> response_body) {
  std::string data;
  if (response_body) {
    DCHECK_EQ(net::OK, simple_loader_->NetError());
    data = std::move(*response_body);
    state_ = COMPLETED;
  } else {
    state_ = FAILED;
  }

  simple_loader_.reset();

  std::move(callback_).Run(state_ == COMPLETED, data);
}

}  // namespace translate