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

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: Fix histograms.xml Created 4 years, 9 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 "base/threading/thread_restrictions.h"
14 #include "components/prefs/scoped_user_pref_update.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 base::ThreadRestrictions::AssertIOAllowed();
Bernhard Bauer 2016/03/17 17:42:25 Doesn't base::WriteFile already do this? Basically
Greg Levin 2016/03/18 00:35:46 Got it, thanks. I wasn't sure how much they were
41 int bytes_written = base::WriteFile(file_path, data.data(), data.length());
42 if (bytes_written == -1)
43 LOG(ERROR) << "Write failed: " << file_path.value() << ", err = " << errno;
44 else
45 VLOG(1) << bytes_written << "bytes written to: " << file_path.value();
46
47 return (bytes_written != -1);
48 }
49
50 } // namespace
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // QuirksClient
54
55 QuirksClient::QuirksClient(int64_t product_id,
56 const RequestFinishedCallback& on_request_finished,
57 QuirksManager* manager)
58 : product_id_(product_id),
59 on_request_finished_(on_request_finished),
60 manager_(manager),
61 icc_path_(
62 manager->delegate()->GetDownloadDisplayProfileDirectory().Append(
63 IdToFileName(product_id))),
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(
75 kQuirksUrlFormat, IdToHexString(product_id_).c_str(), major_version);
76
77 VLOG(2) << "Preparing to download\n " << url << "\nto file "
78 << icc_path_.value();
79
80 url += "?key=";
81 url += manager_->delegate()->GetApiKey();
82
83 url_fetcher_ = manager_->CreateURLFetcher(url, this);
84 url_fetcher_->SetRequestContext(manager_->url_context_getter());
85 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE |
86 net::LOAD_DO_NOT_SAVE_COOKIES |
87 net::LOAD_DO_NOT_SEND_COOKIES |
88 net::LOAD_DO_NOT_SEND_AUTH_DATA);
89 url_fetcher_->Start();
90 }
91
92 void QuirksClient::OnURLFetchComplete(const net::URLFetcher* source) {
93 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
94 DCHECK_EQ(url_fetcher_.get(), source);
95
96 const int HTTP_INTERNAL_SERVER_ERROR_LAST =
97 net::HTTP_INTERNAL_SERVER_ERROR + 99;
98 const net::URLRequestStatus status = source->GetStatus();
99 const int response_code = source->GetResponseCode();
100 const bool server_error = !status.is_success() ||
101 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR &&
102 response_code <= HTTP_INTERNAL_SERVER_ERROR_LAST);
103
104 VLOG(2) << "QuirksClient::OnURLFetchComplete():"
105 << " status=" << status.status()
106 << ", response_code=" << response_code
107 << ", server_error=" << server_error;
108
109 if (response_code == net::HTTP_NOT_FOUND) {
110 VLOG(1) << IdToFileName(product_id_) << " not found on Quirks server.";
111 Shutdown(false);
112 return;
113 }
114
115 if (server_error) {
116 url_fetcher_.reset();
117 Retry();
118 return;
119 }
120
121 std::string response;
122 url_fetcher_->GetResponseAsString(&response);
123 VLOG(2) << "Quirks server response:\n" << response;
124
125 // Parse response data and write to file on file thread.
126 std::string data;
127 if (!ParseResult(response, &data)) {
128 Shutdown(false);
129 return;
130 }
131
132 base::PostTaskAndReplyWithResult(
133 manager_->blocking_pool(), FROM_HERE,
134 base::Bind(&WriteIccFile, icc_path_, data),
135 base::Bind(&QuirksClient::Shutdown, weak_ptr_factory_.GetWeakPtr()));
136 }
137
138 void QuirksClient::Shutdown(bool success) {
139 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
140 on_request_finished_.Run(success ? icc_path_ : base::FilePath(), true);
141 manager_->ClientFinished(this);
142 }
143
144 void QuirksClient::Retry() {
145 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
146 backoff_entry_.InformOfRequest(false);
147 const base::TimeDelta delay = backoff_entry_.GetTimeUntilRelease();
148
149 VLOG(1) << "Schedule next Quirks download attempt in " << delay.InSecondsF()
150 << " seconds (retry = " << backoff_entry_.failure_count() << ").";
151 request_scheduled_.Start(FROM_HERE, delay, this,
152 &QuirksClient::StartDownload);
153 }
154
155 bool QuirksClient::ParseResult(const std::string& result, std::string* data) {
156 std::string data64;
157 const base::DictionaryValue* dict;
158 scoped_ptr<base::Value> json = base::JSONReader::Read(result);
159 if (!json || !json->GetAsDictionary(&dict) ||
160 !dict->GetString("icc", &data64)) {
161 VLOG(1) << "Failed to parse JSON icc data";
162 return false;
163 }
164
165 if (!base::Base64Decode(data64, data)) {
166 VLOG(1) << "Failed to decode Base64 icc data";
167 return false;
168 }
169
170 return true;
171 }
172
173 } // namespace quirks
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698