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
|
// Copyright 2016 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/quirks/quirks_client.h"
#include "base/base64.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/quirks/quirks_manager.h"
#include "components/version_info/version_info.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace quirks {
namespace {
const char kQuirksUrlFormat[] =
"https://chromeosquirksserver-pa.googleapis.com/v2/display/%s/clients"
"/chromeos/M%d?";
const int kMaxServerFailures = 10;
const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
1, // Initial errors before applying backoff
10000, // 10 seconds.
2, // Factor by which the waiting time will be multiplied.
0, // Random fuzzing percentage.
1000 * 3600 * 6, // Max wait between requests = 6 hours.
-1, // Don't discard entry.
true, // Use initial delay after first error.
};
bool WriteIccFile(const base::FilePath file_path, const std::string& data) {
if (!base::WriteFile(file_path, data)) {
PLOG(ERROR) << "Write failed: " << file_path.value();
return false;
}
VLOG(1) << data.size() << "bytes written to: " << file_path.value();
return true;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// QuirksClient
QuirksClient::QuirksClient(int64_t product_id,
std::string display_name,
std::string api_key,
RequestFinishedCallback on_request_finished,
QuirksManager* manager)
: product_id_(product_id),
display_name_(std::move(display_name)),
api_key_(std::move(api_key)),
on_request_finished_(std::move(on_request_finished)),
manager_(manager),
icc_path_(
manager_->display_profile_path().Append(IdToFileName(product_id))),
backoff_entry_(&kDefaultBackoffPolicy) {}
QuirksClient::~QuirksClient() = default;
void QuirksClient::StartDownload() {
DCHECK(thread_checker_.CalledOnValidThread());
// URL of icc file on Quirks Server.
int major_version = version_info::GetMajorVersionNumberAsInt();
std::string url = base::StringPrintf(
kQuirksUrlFormat, IdToHexString(product_id_).c_str(), major_version);
if (!display_name_.empty()) {
url += "display_name=" + base::EscapeQueryParamValue(display_name_, true) +
"&";
}
VLOG(2) << "Preparing to download\n " << url << "\nto file "
<< icc_path_.value();
url += "key=" + api_key_;
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL(url);
resource_request->load_flags =
net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE;
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("quirks_display_fetcher", R"(
semantics {
sender: "Quirks"
description: "Download custom display calibration file."
trigger:
"Chrome OS attempts to download monitor calibration files on"
"first device login, and then once every 30 days."
data: "ICC files to calibrate and improve the quality of a display."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
chrome_policy {
DeviceQuirksDownloadEnabled {
DeviceQuirksDownloadEnabled: false
}
}
}
)");
url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation);
url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
manager_->url_loader_factory(),
base::BindOnce(&QuirksClient::OnDownloadComplete,
base::Unretained(this)));
}
void QuirksClient::OnDownloadComplete(
std::unique_ptr<std::string> response_body) {
DCHECK(thread_checker_.CalledOnValidThread());
// Take ownership of the loader in this scope.
std::unique_ptr<network::SimpleURLLoader> url_loader = std::move(url_loader_);
int response_code = 0;
if (url_loader->ResponseInfo() && url_loader->ResponseInfo()->headers)
response_code = url_loader->ResponseInfo()->headers->response_code();
VLOG(2) << "QuirksClient::OnURLFetchComplete():"
<< " net_error=" << url_loader->NetError()
<< ", response_code=" << response_code;
if (response_code == net::HTTP_NOT_FOUND) {
VLOG(1) << IdToFileName(product_id_) << " not found on Quirks server.";
Shutdown(false);
return;
}
if (url_loader->NetError() != net::OK) {
if (backoff_entry_.failure_count() >= kMaxServerFailures) {
// After 10 retires (5+ hours), give up, and try again in a month.
VLOG(1) << "Too many retries; Quirks Client shutting down.";
Shutdown(false);
return;
}
Retry();
return;
}
DCHECK(response_body); // Guaranteed to be valid if NetError() is net::OK.
VLOG(2) << "Quirks server response:\n" << *response_body;
// Parse response data and write to file on file thread.
std::string data;
if (!ParseResult(*response_body, &data)) {
Shutdown(false);
return;
}
manager_->task_runner()->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&WriteIccFile, icc_path_, data),
base::BindOnce(&QuirksClient::Shutdown, weak_ptr_factory_.GetWeakPtr()));
}
void QuirksClient::Shutdown(bool success) {
DCHECK(thread_checker_.CalledOnValidThread());
std::move(on_request_finished_)
.Run(success ? icc_path_ : base::FilePath(), true);
manager_->ClientFinished(this);
}
void QuirksClient::Retry() {
DCHECK(thread_checker_.CalledOnValidThread());
backoff_entry_.InformOfRequest(false);
const base::TimeDelta delay = backoff_entry_.GetTimeUntilRelease();
VLOG(1) << "Schedule next Quirks download attempt in " << delay.InSecondsF()
<< " seconds (retry = " << backoff_entry_.failure_count() << ").";
request_scheduled_.Start(FROM_HERE, delay, this,
&QuirksClient::StartDownload);
}
bool QuirksClient::ParseResult(const std::string& result, std::string* data) {
std::optional<base::Value::Dict> maybe_json =
base::JSONReader::ReadDict(result);
if (!maybe_json) {
VLOG(1) << "Failed to parse JSON icc data";
return false;
}
std::string* data64 = maybe_json->FindString("icc");
if (!data64) {
VLOG(1) << "Missing icc data";
return false;
}
if (!base::Base64Decode(*data64, data)) {
VLOG(1) << "Failed to decode Base64 icc data";
return false;
}
return true;
}
} // namespace quirks
|