Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: components/quirks/quirks_client.cc

Issue 1528963002: Quirks Client for downloading and providing display profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sixth round of review fixes, sorting out thread issues Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/quirks/quirks_client.h"
6
7 #include "base/base64.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_reader.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/task_runner_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/prefs/scoped_user_pref_update.h"
14 #include "components/quirks/quirks_manager.h"
15 #include "components/version_info/version_info.h"
16 #include "net/base/load_flags.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_context_getter.h"
20
21 namespace quirks {
22
23 namespace {
24
25 const char kQuirksUrlFormat[] =
26 "https://qa-quirksserver-pa.sandbox.googleapis.com/v2/display/%s/clients"
27 "/chromeos/M%d";
28
29 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
30 1, // Initial errors before applying backoff
31 10000, // 10 seconds.
32 2, // Factor by which the waiting time will be multiplied.
33 0, // Random fuzzing percentage.
34 1000 * 3600 * 6, // Max wait between requests = 6 hours.
35 -1, // Don't discard entry.
36 true, // Use initial delay after first error.
37 };
38
39 bool WriteIccFile(const base::FilePath file_path, const std::string& data) {
40 int bytes_written = base::WriteFile(file_path, data.data(), data.length());
41 if (bytes_written == -1)
42 VLOG(1) << "Failed to write: " << file_path.value() << ", err = " << errno;
43 else
44 VLOG(1) << bytes_written << "bytes written to: " << file_path.value();
45
46 return (bytes_written != -1);
47 }
48
49 } // namespace
50
51 ////////////////////////////////////////////////////////////////////////////////
52 // QuirksClient
53
54 QuirksClient::QuirksClient(int64_t product_id,
55 const DownloadFinishedCallback& on_download_finished,
56 QuirksManager* manager)
57 : product_id_(product_id),
58 on_download_finished_(on_download_finished),
59 manager_(manager),
60 icc_path_(
61 manager->delegate()->GetDownloadDisplayProfileDirectory().Append(
62 IdToFileName(product_id))),
63 request_reason_(FIRST),
64 backoff_entry_(&kDefaultBackoffPolicy),
65 weak_ptr_factory_(this) {}
66
67 QuirksClient::~QuirksClient() {}
68
69 void QuirksClient::StartDownload() {
70 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
71
72 // URL of icc file on Quirks Server.
73 int major_version = atoi(version_info::GetVersionNumber().c_str());
74 std::string url = base::StringPrintf(kQuirksUrlFormat,
75 IdToHexString(product_id_).c_str(),
76 major_version);
77
78 VLOG(2) << "Preparing to download\n " << url << "\nto file "
79 << icc_path_.value();
80
81 url += "?key=";
82 url += manager_->delegate()->GetApiKey();
83
84 url_fetcher_ = manager_->CreateURLFetcher(url, this);
85 url_fetcher_->SetRequestContext(manager_->url_context_getter());
86 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
87 net::LOAD_DO_NOT_SAVE_COOKIES |
88 net::LOAD_DO_NOT_SEND_COOKIES |
89 net::LOAD_DO_NOT_SEND_AUTH_DATA);
90 url_fetcher_->Start();
91 }
92
93 void QuirksClient::OnURLFetchComplete(const net::URLFetcher* source) {
94 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
95 DCHECK_EQ(url_fetcher_.get(), source);
96
97 const int HTTP_INTERNAL_SERVER_ERROR_LAST =
98 net::HTTP_INTERNAL_SERVER_ERROR + 99;
99 const net::URLRequestStatus status = source->GetStatus();
100 const int response_code = source->GetResponseCode();
101 const bool server_error = !status.is_success() ||
102 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR &&
103 response_code <= HTTP_INTERNAL_SERVER_ERROR_LAST);
104
105 VLOG(2) << "QuirksClient::OnURLFetchComplete():"
106 << " status=" << status.status()
107 << ", response_code=" << response_code
108 << ", server_error=" << server_error;
109
110 manager_->RecordReasonUmaStat(request_reason_);
111
112 if (response_code == net::HTTP_NOT_FOUND) {
113 VLOG(1) << IdToFileName(product_id_) << " not found on Quirks server.";
114 manager_->RecordFileFoundUmaStat(false);
115 Shutdown(false);
116 return;
117 }
118
119 if (server_error) {
120 url_fetcher_.reset();
121 Retry();
122 return;
123 }
124
125 manager_->RecordFileFoundUmaStat(true);
126 std::string response;
127 url_fetcher_->GetResponseAsString(&response);
128 VLOG(2) << "Quirks server response:\n" << response;
129
130 // Parse response data and write to file on file thread.
131 std::string data;
132 if (!ParseResult(response, &data)) {
133 Shutdown(false);
134 return;
135 }
136
137 base::PostTaskAndReplyWithResult(
138 manager_->blocking_pool(), FROM_HERE,
139 base::Bind(&WriteIccFile, icc_path_, data),
140 base::Bind(&QuirksClient::Shutdown, weak_ptr_factory_.GetWeakPtr()));
141 }
142
143 void QuirksClient::Shutdown(bool success) {
144 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
145 if (!on_download_finished_.is_null())
146 on_download_finished_.Run(success ? icc_path_ : base::FilePath());
147
148 manager_->ClientFinished(this);
149 }
150
151 void QuirksClient::Retry() {
152 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
153 backoff_entry_.InformOfRequest(false);
154 const base::TimeDelta delay = backoff_entry_.GetTimeUntilRelease();
155
156 VLOG(1) << "Schedule next Quirks download attempt in " << delay.InSecondsF()
157 << " seconds (retry = " << backoff_entry_.failure_count() << ").";
158 request_scheduled_.Start(FROM_HERE, delay, this,
159 &QuirksClient::StartDownload);
160 }
161
162 bool QuirksClient::ParseResult(const std::string& result, std::string* data) {
163 std::string data64;
164 const base::DictionaryValue* dict;
165 scoped_ptr<base::Value> json = base::JSONReader::Read(result);
166 if (!json || !json->GetAsDictionary(&dict) ||
167 !dict->GetString("icc", &data64)) {
168 VLOG(1) << "Failed to parse JSON icc data";
169 return false;
170 }
171
172 if (!base::Base64Decode(data64, data)) {
173 VLOG(1) << "Failed to decode Base64 icc data";
174 return false;
175 }
176
177 return true;
178 }
179
180 } // namespace quirks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698