Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/quirks/quirks_manager.h" | 5 #include "components/quirks/quirks_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 QuirksManager* manager_ = nullptr; | 27 QuirksManager* manager_ = nullptr; |
| 28 | 28 |
| 29 const char kIccExtension[] = ".icc"; | 29 const char kIccExtension[] = ".icc"; |
| 30 | 30 |
| 31 // How often we query Quirks Server. | 31 // How often we query Quirks Server. |
| 32 const int kDaysBetweenServerChecks = 30; | 32 const int kDaysBetweenServerChecks = 30; |
| 33 | 33 |
| 34 // Check if file exists, VLOG results. | 34 // Check if QuirksClient has already downloaded icc file from server. |
| 35 bool CheckAndLogFile(const base::FilePath& path) { | 35 base::FilePath CheckForIccFile(base::FilePath path) { |
|
oshima
2016/10/24 17:36:32
nit: const &
| |
| 36 const bool exists = base::PathExists(path); | 36 const bool exists = base::PathExists(path); |
| 37 VLOG(1) << (exists ? "File" : "No File") << " found at " << path.value(); | 37 VLOG(1) << (exists ? "File" : "No File") << " found at " << path.value(); |
| 38 // TODO(glevin): If file exists, do we want to implement a hash to verify that | 38 // TODO(glevin): If file exists, do we want to implement a hash to verify that |
| 39 // the file hasn't been corrupted or tampered with? | 39 // the file hasn't been corrupted or tampered with? |
| 40 return exists; | 40 return exists ? path : base::FilePath(); |
| 41 } | |
| 42 | |
| 43 base::FilePath CheckForIccFile(base::FilePath built_in_path, | |
| 44 base::FilePath download_path, | |
| 45 bool quirks_enabled) { | |
| 46 // First, look for icc file in old read-only location. If there, we don't use | |
| 47 // the Quirks server. | |
| 48 if (CheckAndLogFile(built_in_path)) | |
| 49 return built_in_path; | |
| 50 | |
| 51 // If experimental Quirks flag isn't set, no other icc file is available. | |
| 52 if (!quirks_enabled) { | |
| 53 VLOG(1) << "Quirks Client disabled, no built-in icc file available."; | |
| 54 return base::FilePath(); | |
| 55 } | |
| 56 | |
| 57 // Check if QuirksClient has already downloaded icc file from server. | |
| 58 if (CheckAndLogFile(download_path)) | |
| 59 return download_path; | |
| 60 | |
| 61 return base::FilePath(); | |
| 62 } | 41 } |
| 63 | 42 |
| 64 } // namespace | 43 } // namespace |
| 65 | 44 |
| 66 std::string IdToHexString(int64_t product_id) { | 45 std::string IdToHexString(int64_t product_id) { |
| 67 return base::StringPrintf("%08" PRIx64, product_id); | 46 return base::StringPrintf("%08" PRIx64, product_id); |
| 68 } | 47 } |
| 69 | 48 |
| 70 std::string IdToFileName(int64_t product_id) { | 49 std::string IdToFileName(int64_t product_id) { |
| 71 return IdToHexString(product_id).append(kIccExtension); | 50 return IdToHexString(product_id).append(kIccExtension); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 | 109 |
| 131 for (const std::unique_ptr<QuirksClient>& client : clients_) | 110 for (const std::unique_ptr<QuirksClient>& client : clients_) |
| 132 client->StartDownload(); | 111 client->StartDownload(); |
| 133 } | 112 } |
| 134 | 113 |
| 135 void QuirksManager::RequestIccProfilePath( | 114 void QuirksManager::RequestIccProfilePath( |
| 136 int64_t product_id, | 115 int64_t product_id, |
| 137 const RequestFinishedCallback& on_request_finished) { | 116 const RequestFinishedCallback& on_request_finished) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 | 118 |
| 119 if (!QuirksEnabled()) { | |
| 120 VLOG(1) << "Quirks Client disabled."; | |
| 121 on_request_finished.Run(base::FilePath(), false); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 140 if (!product_id) { | 125 if (!product_id) { |
| 141 VLOG(1) << "Could not determine display information (product id = 0)"; | 126 VLOG(1) << "Could not determine display information (product id = 0)"; |
| 142 on_request_finished.Run(base::FilePath(), false); | 127 on_request_finished.Run(base::FilePath(), false); |
| 143 return; | 128 return; |
| 144 } | 129 } |
| 145 | 130 |
| 146 std::string name = IdToFileName(product_id); | 131 std::string name = IdToFileName(product_id); |
| 147 base::PostTaskAndReplyWithResult( | 132 base::PostTaskAndReplyWithResult( |
| 148 blocking_pool_.get(), FROM_HERE, | 133 blocking_pool_.get(), FROM_HERE, |
| 149 base::Bind(&CheckForIccFile, | 134 base::Bind(&CheckForIccFile, |
| 150 delegate_->GetBuiltInDisplayProfileDirectory().Append(name), | 135 delegate_->GetDisplayProfileDirectory().Append(name)), |
| 151 delegate_->GetDownloadDisplayProfileDirectory().Append(name), | |
| 152 QuirksEnabled()), | |
| 153 base::Bind(&QuirksManager::OnIccFilePathRequestCompleted, | 136 base::Bind(&QuirksManager::OnIccFilePathRequestCompleted, |
| 154 weak_ptr_factory_.GetWeakPtr(), product_id, | 137 weak_ptr_factory_.GetWeakPtr(), product_id, |
| 155 on_request_finished)); | 138 on_request_finished)); |
| 156 } | 139 } |
| 157 | 140 |
| 158 void QuirksManager::ClientFinished(QuirksClient* client) { | 141 void QuirksManager::ClientFinished(QuirksClient* client) { |
| 159 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 SetLastServerCheck(client->product_id(), base::Time::Now()); | 143 SetLastServerCheck(client->product_id(), base::Time::Now()); |
| 161 auto it = std::find_if(clients_.begin(), clients_.end(), | 144 auto it = std::find_if(clients_.begin(), clients_.end(), |
| 162 [client](const std::unique_ptr<QuirksClient>& c) { | 145 [client](const std::unique_ptr<QuirksClient>& c) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 174 | 157 |
| 175 return net::URLFetcher::Create(url, net::URLFetcher::GET, delegate); | 158 return net::URLFetcher::Create(url, net::URLFetcher::GET, delegate); |
| 176 } | 159 } |
| 177 | 160 |
| 178 void QuirksManager::OnIccFilePathRequestCompleted( | 161 void QuirksManager::OnIccFilePathRequestCompleted( |
| 179 int64_t product_id, | 162 int64_t product_id, |
| 180 const RequestFinishedCallback& on_request_finished, | 163 const RequestFinishedCallback& on_request_finished, |
| 181 base::FilePath path) { | 164 base::FilePath path) { |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
| 183 | 166 |
| 184 // If we found a file or client is disabled, inform requester. | 167 // If we found a file, just inform requester. |
| 185 if (!path.empty() || !QuirksEnabled()) { | 168 if (!path.empty()) { |
| 186 on_request_finished.Run(path, false); | 169 on_request_finished.Run(path, false); |
| 187 // TODO(glevin): If Quirks files are ever modified on the server, we'll need | 170 // TODO(glevin): If Quirks files are ever modified on the server, we'll need |
| 188 // to modify this logic to check for updates. See crbug.com/595024. | 171 // to modify this logic to check for updates. See crbug.com/595024. |
| 189 return; | 172 return; |
| 190 } | 173 } |
| 191 | 174 |
| 192 double last_check = 0.0; | 175 double last_check = 0.0; |
| 193 local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck) | 176 local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck) |
| 194 ->GetDouble(IdToHexString(product_id), &last_check); | 177 ->GetDouble(IdToHexString(product_id), &last_check); |
| 195 | 178 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 224 } | 207 } |
| 225 | 208 |
| 226 void QuirksManager::SetLastServerCheck(int64_t product_id, | 209 void QuirksManager::SetLastServerCheck(int64_t product_id, |
| 227 const base::Time& last_check) { | 210 const base::Time& last_check) { |
| 228 DCHECK(thread_checker_.CalledOnValidThread()); | 211 DCHECK(thread_checker_.CalledOnValidThread()); |
| 229 DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck); | 212 DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck); |
| 230 dict->SetDouble(IdToHexString(product_id), last_check.ToDoubleT()); | 213 dict->SetDouble(IdToHexString(product_id), last_check.ToDoubleT()); |
| 231 } | 214 } |
| 232 | 215 |
| 233 } // namespace quirks | 216 } // namespace quirks |
| OLD | NEW |