Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_client/quirks_client.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "base/task_runner_util.h" | |
| 15 #include "base/thread_task_runner_handle.h" | |
| 16 #include "chromeos/chromeos_paths.h" | |
| 17 #include "components/prefs/scoped_user_pref_update.h" | |
| 18 #include "components/quirks_client/quirks_client_manager.h" | |
| 19 #include "components/quirks_client/switches.h" | |
| 20 #include "net/base/load_flags.h" | |
| 21 #include "net/http/http_status_code.h" | |
| 22 #include "net/url_request/url_fetcher.h" | |
| 23 #include "net/url_request/url_request_context_getter.h" | |
| 24 | |
| 25 namespace quirks_client { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 const char kQuirksUrlFormat[] = | |
| 30 "https://qa-quirksserver-pa.sandbox.googleapis.com/v2/display/%08" PRIx64 | |
| 31 "/clients/chromeos/M%d"; | |
| 32 | |
| 33 // Sleep between Quirks retries (will be multiplied by squared retry number). | |
| 34 const unsigned kRetrySleepSeconds = 10; | |
| 35 | |
| 36 // Retry is infinite with increasing intervals. When calculated delay becomes | |
| 37 // longer than maximum (kMaxRetrySleepSeconds) it is set to the maximum. | |
| 38 const unsigned kMaxRetrySleepSeconds = 6 * 3600; // 6 hours | |
| 39 | |
| 40 // Access to singleton QuirksClientManager for static and nonmember funcitons. | |
| 41 QuirksClientManager* GetManager() { | |
| 42 return QuirksClientManager::Get(); | |
| 43 } | |
| 44 | |
| 45 // Check if file exists, VLOG results. | |
| 46 bool CheckAndLogFile(const base::FilePath& path) { | |
| 47 const bool exists = base::PathExists(path); | |
| 48 VLOG(1) << (exists ? "File" : "No File") << " found at " << path.value(); | |
| 49 // TODO(glevin): If file exists, do we want to implement a hash to verify that | |
| 50 // the file hasn't been corrupted or tampered with? | |
| 51 return exists; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 std::string IdToHexString(int64_t product_id) { | |
| 57 return base::StringPrintf("%08" PRIx64, product_id); | |
| 58 } | |
| 59 | |
| 60 // Must be called on file thread. | |
| 61 base::FilePath RequestIccProfilePath( | |
| 62 int64_t product_id, | |
| 63 const DownloadFinishedCallback& on_download_finished) { | |
| 64 std::string file_name = IdToHexString(product_id) + ".icc"; | |
| 65 | |
| 66 // First, look for icc file in old read-only location. If there, we don't use | |
| 67 // the Quirks server. | |
| 68 // TODO(glevin): Awaiting final decision on how to handle old read-only files. | |
| 69 base::FilePath path; | |
| 70 CHECK( | |
| 71 PathService::Get(chromeos::DIR_DEVICE_COLOR_CALIBRATION_PROFILES, &path)); | |
| 72 path = path.Append(file_name); | |
| 73 if (CheckAndLogFile(path)) | |
| 74 return path; | |
| 75 | |
| 76 // If Quirks Client is disabled, no other icc file is available. | |
| 77 if (!QuirksClient::IsEnabled()) { | |
| 78 VLOG(1) << "Quirks Client disabled, no built-in icc file available."; | |
| 79 return base::FilePath(); | |
| 80 } | |
| 81 | |
| 82 if (!GetManager()) { | |
|
Bernhard Bauer
2016/02/12 14:51:21
Wait, you call this method on a background thread?
Greg Levin
2016/02/16 03:14:46
Keeping the manager on one thread is tricky, as it
| |
| 83 VLOG(1) << "Quirks Client Manager not initialized; can't start Client."; | |
| 84 return base::FilePath(); | |
| 85 } | |
| 86 | |
| 87 // Check if QuirksClient has already downloaded icc file from server. | |
| 88 path = | |
| 89 GetManager()->delegate()->GetDisplayProfileDirectory().Append(file_name); | |
| 90 if (CheckAndLogFile(path)) | |
| 91 return path; | |
| 92 | |
| 93 // TODO(glevin): Eventually we'll want to check the server for updates to | |
| 94 // files, so we'll still want to get down here even if we find the icc file. | |
| 95 | |
| 96 GetManager()->RunClient(product_id, on_download_finished); | |
| 97 | |
| 98 return base::FilePath(); | |
| 99 } | |
| 100 | |
| 101 //////////////////////////////////////////////////////////////////////////////// | |
| 102 // QuirksClient | |
| 103 | |
| 104 QuirksClient::QuirksClient(int64_t product_id, | |
| 105 const DownloadFinishedCallback& on_download_finished, | |
| 106 QuirksClientManager* manager) | |
| 107 : product_id_(product_id), | |
| 108 on_download_finished_(on_download_finished), | |
| 109 manager_(manager), | |
| 110 icc_path_(manager->delegate()->GetDisplayProfileDirectory().Append( | |
| 111 IdToHexString(product_id) + ".icc")), | |
| 112 request_reason_(FIRST), | |
| 113 retries_(0), | |
| 114 weak_ptr_factory_(this) {} | |
| 115 | |
| 116 QuirksClient::~QuirksClient() {} | |
| 117 | |
| 118 void QuirksClient::StartDownload() { | |
| 119 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 120 | |
| 121 // URL of icc file on Quirks Server. | |
| 122 // TODO(glevin): Replace |44| with actual version. This is fine for now, as | |
| 123 // the Quirks Server is not currently using this value. | |
| 124 std::string url = base::StringPrintf(kQuirksUrlFormat, product_id_, 44); | |
| 125 | |
| 126 VLOG(2) << "Preparing to download\n " << url << "\nto file " | |
| 127 << icc_path_.value(); | |
| 128 | |
| 129 url += "?key="; | |
| 130 url += manager_->delegate()->GetApiKey(); | |
| 131 | |
| 132 url_fetcher_ = manager_->CreateURLFetcher(url, this); | |
| 133 url_fetcher_->SetRequestContext(manager_->url_context_getter()); | |
| 134 url_fetcher_->SetLoadFlags(net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | |
| 135 net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 136 net::LOAD_DO_NOT_SEND_COOKIES | | |
| 137 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
| 138 url_fetcher_->Start(); | |
| 139 } | |
| 140 | |
| 141 // static | |
| 142 bool QuirksClient::IsEnabled() { | |
| 143 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 144 switches::kEnableDisplayQuirksClient); | |
| 145 } | |
| 146 | |
| 147 void QuirksClient::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 148 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 149 DCHECK_EQ(url_fetcher_.get(), source); | |
| 150 | |
| 151 const int HTTP_INTERNAL_SERVER_ERROR_LAST = | |
| 152 net::HTTP_INTERNAL_SERVER_ERROR + 99; | |
| 153 const net::URLRequestStatus status = source->GetStatus(); | |
| 154 const int response_code = source->GetResponseCode(); | |
| 155 const bool server_error = !status.is_success() || | |
| 156 (response_code >= net::HTTP_INTERNAL_SERVER_ERROR && | |
| 157 response_code <= HTTP_INTERNAL_SERVER_ERROR_LAST); | |
| 158 | |
| 159 VLOG(2) << "QuirksClient::OnURLFetchComplete():" | |
| 160 << " status=" << status.status() | |
| 161 << ", response_code=" << response_code | |
| 162 << ", server_error=" << server_error; | |
| 163 | |
| 164 manager_->RecordReasonUmaStat(request_reason_); | |
| 165 | |
| 166 if (response_code == net::HTTP_NOT_FOUND) { | |
| 167 VLOG(1) << IdToHexString(product_id_) << ".icc not found on Quirks server."; | |
| 168 manager_->RecordFileFoundUmaStat(false); | |
| 169 Shutdown(false); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 if (server_error) { | |
| 174 url_fetcher_.reset(); | |
| 175 Retry(); | |
| 176 return; | |
| 177 } | |
| 178 | |
| 179 manager_->RecordFileFoundUmaStat(true); | |
| 180 std::string response; | |
| 181 url_fetcher_->GetResponseAsString(&response); | |
| 182 VLOG(2) << "Quirks server response:\n" << response; | |
| 183 | |
| 184 // Parse response data and write to file on file thread. | |
| 185 std::string data; | |
| 186 if (!ParseResult(response, &data)) | |
|
Bernhard Bauer
2016/02/12 14:51:21
So if we can't parse the result, we'll just never
Greg Levin
2016/02/15 21:53:52
Done (added Shutdown()).
| |
| 187 return; | |
| 188 | |
| 189 base::PostTaskAndReplyWithResult( | |
| 190 manager_->blocking_pool(), FROM_HERE, | |
| 191 base::Bind(&WriteIccFile, icc_path_, data), | |
| 192 base::Bind(&QuirksClient::Shutdown, weak_ptr_factory_.GetWeakPtr())); | |
| 193 } | |
| 194 | |
| 195 void QuirksClient::Shutdown(bool success) { | |
| 196 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 197 if (!on_download_finished_.is_null()) | |
| 198 on_download_finished_.Run(success ? icc_path_ : base::FilePath()); | |
| 199 | |
| 200 manager_->SetLastServerCheck(product_id_, base::Time::Now()); | |
| 201 manager_->DeleteClient(this); | |
| 202 } | |
| 203 | |
| 204 void QuirksClient::Retry() { | |
| 205 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 206 ++retries_; | |
| 207 | |
| 208 const unsigned delay_seconds = | |
| 209 std::min(retries_ * retries_ * kRetrySleepSeconds, kMaxRetrySleepSeconds); | |
| 210 const base::TimeDelta delay = base::TimeDelta::FromSeconds(delay_seconds); | |
| 211 | |
| 212 VLOG(1) << "Schedule next Quirks download attempt in " << delay.InSecondsF() | |
| 213 << " seconds (retry = " << retries_ << ")."; | |
| 214 request_scheduled_.Start(FROM_HERE, delay, this, | |
| 215 &QuirksClient::StartDownload); | |
| 216 } | |
| 217 | |
| 218 // static | |
| 219 bool QuirksClient::WriteIccFile(const base::FilePath file_path, | |
| 220 const std::string& data) { | |
| 221 DCHECK(GetManager() && | |
| 222 GetManager()->blocking_pool()->RunsTasksOnCurrentThread()); | |
| 223 int bytes_written = base::WriteFile(file_path, data.data(), data.length()); | |
| 224 if (bytes_written == -1) | |
| 225 VLOG(1) << "Failed to write: " << file_path.value() << ", err = " << errno; | |
| 226 else | |
| 227 VLOG(1) << bytes_written << "bytes written to: " << file_path.value(); | |
| 228 | |
| 229 return (bytes_written != -1); | |
| 230 } | |
| 231 | |
| 232 bool QuirksClient::ParseResult(const std::string& result, std::string* data) { | |
| 233 std::string data64; | |
| 234 const base::DictionaryValue* dict; | |
| 235 scoped_ptr<base::Value> json = base::JSONReader::Read(result); | |
| 236 if (!json || !json->GetAsDictionary(&dict) || | |
| 237 !dict->GetString("icc", &data64)) { | |
| 238 VLOG(1) << "Failed to parse JSON icc data"; | |
| 239 return false; | |
| 240 } | |
| 241 | |
| 242 if (!base::Base64Decode(data64, data)) { | |
| 243 VLOG(1) << "Failed to decode Base64 icc data"; | |
| 244 return false; | |
| 245 } | |
| 246 | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 250 } // namespace chromeos | |
| OLD | NEW |