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

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: Refactor to move some functionality from client to manager 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 "net/base/load_flags.h"
16 #include "net/http/http_status_code.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h"
19
20 namespace quirks {
21
22 namespace {
23
24 const char kQuirksUrlFormat[] =
25 "https://qa-quirksserver-pa.sandbox.googleapis.com/v2/display/%s/clients"
26 "/chromeos/M%d";
27
28 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
29 1, // Initial errors before applying backoff
30 10000, // 10 seconds.
31 2, // Factor by which the waiting time will be multiplied.
32 0, // Random fuzzing percentage.
33 1000 * 3600 * 6, // Max wait between requests = 6 hours.
34 -1, // Don't discard entry.
35 true, // Use initial delay after first error.
36 };
37
38 // Access to singleton QuirksManager for static and nonmember funcitons.
39 QuirksManager* GetManager() {
40 return QuirksManager::Get();
41 }
stevenjb 2016/02/16 20:07:51 Not needed (see comment below)
Greg Levin 2016/02/17 23:01:51 Deleted!
42
43 } // namespace
44
45 ////////////////////////////////////////////////////////////////////////////////
46 // QuirksClient
47
48 QuirksClient::QuirksClient(int64_t product_id,
49 const DownloadFinishedCallback& on_download_finished,
50 QuirksManager* manager)
51 : product_id_(product_id),
52 on_download_finished_(on_download_finished),
53 manager_(manager),
54 icc_path_(manager->delegate()->GetDisplayProfileDirectory().Append(
55 IdToHexString(product_id) + ".icc")),
stevenjb 2016/02/16 20:07:51 .icc should be defined as a const char[] in the an
Greg Levin 2016/02/17 23:01:51 Done... created IdToFileName() to go with IdToHexS
56 request_reason_(FIRST),
57 backoff_entry_(&kDefaultBackoffPolicy),
58 weak_ptr_factory_(this) {}
59
60 QuirksClient::~QuirksClient() {}
61
62 void QuirksClient::StartDownload() {
63 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
64
65 // URL of icc file on Quirks Server.
66 // TODO(glevin): Replace |44| with actual version. This is fine for now, as
67 // the Quirks Server is not currently using this value.
stevenjb 2016/02/16 20:07:51 We should fix this now or do something else in the
Greg Levin 2016/02/17 23:01:51 Done.
68 std::string url = base::StringPrintf(kQuirksUrlFormat,
69 IdToHexString(product_id_).c_str(), 44);
70
71 VLOG(2) << "Preparing to download\n " << url << "\nto file "
72 << icc_path_.value();
73
74 url += "?key=";
75 url += manager_->delegate()->GetApiKey();
76
77 url_fetcher_ = manager_->CreateURLFetcher(url, this);
78 url_fetcher_->SetRequestContext(manager_->url_context_getter());
79 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
80 net::LOAD_DO_NOT_SAVE_COOKIES |
81 net::LOAD_DO_NOT_SEND_COOKIES |
82 net::LOAD_DO_NOT_SEND_AUTH_DATA);
83 url_fetcher_->Start();
84 }
85
86 void QuirksClient::OnURLFetchComplete(const net::URLFetcher* source) {
87 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
88 DCHECK_EQ(url_fetcher_.get(), source);
89
90 const int HTTP_INTERNAL_SERVER_ERROR_LAST =
91 net::HTTP_INTERNAL_SERVER_ERROR + 99;
stevenjb 2016/02/16 20:07:51 Where does 99 come from?
Greg Levin 2016/02/17 23:01:51 It was originally copied from here: https://code.g
stevenjb 2016/02/17 23:38:55 I see. Ugh, OK.
92 const net::URLRequestStatus status = source->GetStatus();
93 const int response_code = source->GetResponseCode();
94 const bool server_error = !status.is_success() ||
95 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR &&
96 response_code <= HTTP_INTERNAL_SERVER_ERROR_LAST);
97
98 VLOG(2) << "QuirksClient::OnURLFetchComplete():"
99 << " status=" << status.status()
100 << ", response_code=" << response_code
101 << ", server_error=" << server_error;
102
103 manager_->RecordReasonUmaStat(request_reason_);
104
105 if (response_code == net::HTTP_NOT_FOUND) {
106 VLOG(1) << IdToHexString(product_id_) << ".icc not found on Quirks server.";
107 manager_->RecordFileFoundUmaStat(false);
108 Shutdown(false);
109 return;
110 }
111
112 if (server_error) {
113 url_fetcher_.reset();
114 Retry();
115 return;
116 }
117
118 manager_->RecordFileFoundUmaStat(true);
119 std::string response;
120 url_fetcher_->GetResponseAsString(&response);
121 VLOG(2) << "Quirks server response:\n" << response;
122
123 // Parse response data and write to file on file thread.
124 std::string data;
125 if (!ParseResult(response, &data)) {
126 Shutdown(false);
127 return;
128 }
129
130 base::PostTaskAndReplyWithResult(
131 manager_->blocking_pool(), FROM_HERE,
132 base::Bind(&WriteIccFile, icc_path_, data),
133 base::Bind(&QuirksClient::Shutdown, weak_ptr_factory_.GetWeakPtr()));
134 }
135
136 void QuirksClient::Shutdown(bool success) {
137 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
138 if (!on_download_finished_.is_null())
139 on_download_finished_.Run(success ? icc_path_ : base::FilePath());
140
141 manager_->SetLastServerCheck(product_id_, base::Time::Now());
stevenjb 2016/02/16 20:07:51 Maybe make this part of ClientFinished?
Greg Levin 2016/02/17 23:01:51 Done.
142 manager_->ClientFinished(this);
143 }
144
145 void QuirksClient::Retry() {
146 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
147 backoff_entry_.InformOfRequest(false);
148 const base::TimeDelta delay = backoff_entry_.GetTimeUntilRelease();
149
150 VLOG(1) << "Schedule next Quirks download attempt in " << delay.InSecondsF()
151 << " seconds (retry = " << backoff_entry_.failure_count() << ").";
152 request_scheduled_.Start(FROM_HERE, delay, this,
153 &QuirksClient::StartDownload);
154 }
155
156 // static
157 // TODO(glevin): would like to make this non-static and get rid of GetManager(),
158 // but was having trouble working out the syntax for Bind'ing it above, since
159 // "weak_ptrs can only bind to methods without return values".
stevenjb 2016/02/16 20:07:51 This shouldn't need to be a static member at all,
Greg Levin 2016/02/17 23:01:51 Done.
160 bool QuirksClient::WriteIccFile(const base::FilePath file_path,
161 const std::string& data) {
162 DCHECK(GetManager() &&
163 GetManager()->blocking_pool()->RunsTasksOnCurrentThread());
stevenjb 2016/02/16 20:07:51 Why do we need this DCHECK? If it is important tha
Greg Levin 2016/02/17 23:01:51 Done.
164 int bytes_written = base::WriteFile(file_path, data.data(), data.length());
165 if (bytes_written == -1)
166 VLOG(1) << "Failed to write: " << file_path.value() << ", err = " << errno;
167 else
168 VLOG(1) << bytes_written << "bytes written to: " << file_path.value();
169
170 return (bytes_written != -1);
171 }
172
173 bool QuirksClient::ParseResult(const std::string& result, std::string* data) {
174 std::string data64;
175 const base::DictionaryValue* dict;
176 scoped_ptr<base::Value> json = base::JSONReader::Read(result);
177 if (!json || !json->GetAsDictionary(&dict) ||
178 !dict->GetString("icc", &data64)) {
179 VLOG(1) << "Failed to parse JSON icc data";
180 return false;
181 }
182
183 if (!base::Base64Decode(data64, data)) {
184 VLOG(1) << "Failed to decode Base64 icc data";
185 return false;
186 }
187
188 return true;
189 }
190
191 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698