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/quirks_manager.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/format_macros.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/rand_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/thread_task_runner_handle.h" | |
| 14 #include "chromeos/chromeos_paths.h" | |
| 15 #include "components/prefs/pref_registry_simple.h" | |
| 16 #include "components/prefs/scoped_user_pref_update.h" | |
| 17 #include "components/quirks/pref_names.h" | |
| 18 #include "components/quirks/switches.h" | |
| 19 #include "net/url_request/url_fetcher.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace quirks { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 QuirksManager* g_manager = nullptr; | |
| 28 | |
| 29 // How often we query Quirks Server. | |
| 30 const int kDaysBetweenServerChecks = 30; | |
| 31 | |
| 32 // Check if file exists, VLOG results. | |
| 33 bool CheckAndLogFile(const base::FilePath& path) { | |
| 34 const bool exists = base::PathExists(path); | |
| 35 VLOG(1) << (exists ? "File" : "No File") << " found at " << path.value(); | |
| 36 // TODO(glevin): If file exists, do we want to implement a hash to verify that | |
| 37 // the file hasn't been corrupted or tampered with? | |
| 38 return exists; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 std::string IdToHexString(int64_t product_id) { | |
| 44 return base::StringPrintf("%08" PRIx64, product_id); | |
| 45 } | |
| 46 | |
| 47 // Must be called on file thread. | |
|
stevenjb
2016/02/16 20:07:51
Requiring this to be called on the file thread is
Bernhard Bauer
2016/02/17 17:27:40
Also, CompleteRequestIccFilePath() could create th
Greg Levin
2016/02/17 23:01:51
Ok, this is just my incomplete understanding of th
stevenjb
2016/02/17 23:38:55
Perhaps we need to discuss this in a meeting, howe
Bernhard Bauer
2016/02/18 12:17:07
You are correct that this doesn't really work with
Greg Levin
2016/03/02 00:00:26
Done. Based on stevenjb's input, I refactored QM s
| |
| 48 base::FilePath RequestIccProfilePath( | |
| 49 int64_t product_id, | |
| 50 const DownloadFinishedCallback& on_download_finished) { | |
| 51 if (g_manager) | |
| 52 return g_manager->RequestIccFilePath(product_id, on_download_finished); | |
| 53 | |
| 54 VLOG(1) << "Quirks Client Manager not initialized; can't get icc file."; | |
| 55 return base::FilePath(); | |
| 56 } | |
| 57 | |
| 58 //////////////////////////////////////////////////////////////////////////////// | |
| 59 // QuirksManager | |
| 60 | |
| 61 QuirksManager::QuirksManager( | |
| 62 Delegate* delegate, | |
| 63 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 64 PrefService* local_state, | |
| 65 scoped_refptr<net::URLRequestContextGetter> url_context_getter) | |
| 66 : delegate_(delegate), | |
| 67 blocking_pool_(blocking_pool), | |
| 68 local_state_(local_state), | |
| 69 url_context_getter_(url_context_getter), | |
| 70 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 71 is_new_device_(false), | |
| 72 weak_ptr_factory_(this) {} | |
| 73 | |
| 74 QuirksManager::~QuirksManager() { | |
| 75 clients_.clear(); | |
| 76 g_manager = nullptr; | |
| 77 } | |
| 78 | |
| 79 // static | |
| 80 void QuirksManager::Initialize( | |
| 81 scoped_ptr<Delegate> delegate, | |
| 82 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 83 PrefService* local_state, | |
| 84 scoped_refptr<net::URLRequestContextGetter> url_context_getter) { | |
| 85 g_manager = new QuirksManager(delegate.release(), blocking_pool, local_state, | |
| 86 url_context_getter); | |
| 87 } | |
| 88 | |
| 89 // static | |
| 90 void QuirksManager::Shutdown() { | |
| 91 delete g_manager; | |
| 92 } | |
| 93 | |
| 94 // static | |
| 95 QuirksManager* QuirksManager::Get() { | |
| 96 DCHECK(g_manager); | |
| 97 return g_manager; | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 void QuirksManager::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 102 registry->RegisterDictionaryPref(prefs::kQuirksClientLastServerCheck); | |
| 103 } | |
| 104 | |
| 105 base::FilePath QuirksManager::RequestIccFilePath( | |
| 106 int64_t product_id, | |
| 107 const DownloadFinishedCallback& on_download_finished) { | |
| 108 DCHECK(blocking_pool()->RunsTasksOnCurrentThread()); | |
| 109 std::string file_name = IdToHexString(product_id) + ".icc"; | |
| 110 | |
| 111 // First, look for icc file in old read-only location. If there, we don't use | |
| 112 // the Quirks server. | |
| 113 // TODO(glevin): Awaiting final decision on how to handle old read-only files. | |
| 114 base::FilePath path; | |
| 115 CHECK( | |
| 116 PathService::Get(chromeos::DIR_DEVICE_COLOR_CALIBRATION_PROFILES, &path)); | |
|
stevenjb
2016/02/16 20:07:51
nit: Rather than adding a src/chromeos dependency
Greg Levin
2016/02/17 23:01:51
Moved it to delegate instead (as per suggestion in
| |
| 117 path = path.Append(file_name); | |
| 118 if (CheckAndLogFile(path)) | |
| 119 return path; | |
| 120 | |
| 121 // If experimental Quirks flag isn't set, no other icc file is available. | |
| 122 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 123 switches::kEnableDisplayQuirksClient)) { | |
| 124 VLOG(1) << "Quirks Client disabled, no built-in icc file available."; | |
| 125 return base::FilePath(); | |
| 126 } | |
| 127 | |
| 128 // Check if QuirksClient has already downloaded icc file from server. | |
| 129 path = delegate_->GetDisplayProfileDirectory().Append(file_name); | |
| 130 if (CheckAndLogFile(path)) | |
| 131 return path; | |
| 132 | |
| 133 // TODO(glevin): Eventually we'll want to check the server for updates to | |
| 134 // files, so we'll still want to get down here even if we find the icc file. | |
| 135 | |
| 136 // Record this on file thread for future use on ui thread. | |
| 137 is_new_device_ = (delegate_->GetDaysSinceOobe() <= kDaysBetweenServerChecks); | |
| 138 | |
| 139 task_runner_->PostTask(FROM_HERE, | |
| 140 base::Bind(&QuirksManager::RunClientOnUIThread, | |
| 141 weak_ptr_factory_.GetWeakPtr(), product_id, | |
| 142 on_download_finished)); | |
| 143 | |
| 144 return base::FilePath(); | |
| 145 } | |
| 146 | |
| 147 void QuirksManager::ClientFinished(QuirksClient* client) { | |
| 148 auto it = std::find_if(clients_.begin(), clients_.end(), | |
| 149 [client](const scoped_ptr<QuirksClient>& c) { | |
| 150 return c.get() == client; | |
| 151 }); | |
| 152 if (it != clients_.end()) | |
|
Bernhard Bauer
2016/02/17 17:27:40
I think I would DCHECK this instead of silently fa
Greg Levin
2016/02/17 23:01:51
Done.
| |
| 153 clients_.erase(it); | |
| 154 } | |
| 155 | |
| 156 bool QuirksManager::NeedToCheckServer(int64_t product_id) { | |
| 157 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 158 double last_check = 0.0; | |
| 159 local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck) | |
| 160 ->GetDouble(IdToHexString(product_id), &last_check); | |
| 161 | |
| 162 if (last_check != 0.0) { | |
| 163 const base::TimeDelta time_since = | |
| 164 base::Time::Now() - base::Time::FromDoubleT(last_check); | |
| 165 | |
| 166 // Don't need server check if we've checked within last 30 days. | |
| 167 if (time_since < base::TimeDelta::FromDays(kDaysBetweenServerChecks)) { | |
| 168 VLOG(2) << time_since.InDays() | |
| 169 << " days since last Quirks Server check for display " | |
| 170 << IdToHexString(product_id); | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 // On new devices, we want to check server immediately. | |
| 178 if (is_new_device_) | |
| 179 return true; | |
| 180 | |
| 181 // Otherwise, for the first check on an older device, we want to stagger | |
| 182 // it over 30 days, so artificially set last check accordingly. | |
| 183 const int rand_days = base::RandInt(0, kDaysBetweenServerChecks); | |
| 184 const base::Time fake_last_check = | |
| 185 base::Time::Now() - base::TimeDelta::FromDays(rand_days); | |
| 186 SetLastServerCheck(product_id, fake_last_check); | |
| 187 VLOG(2) << "Delaying first Quirks Server check by " | |
| 188 << kDaysBetweenServerChecks - rand_days << " days."; | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 void QuirksManager::SetLastServerCheck(int64_t product_id, | |
| 193 const base::Time& last_check) { | |
| 194 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 195 DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck); | |
| 196 dict->SetDouble(IdToHexString(product_id), last_check.ToDoubleT()); | |
| 197 } | |
| 198 | |
| 199 // TODO(glevin): Add code to record UMA stats here. Also need to set | |
| 200 // request_reason_ in QuirksClient. | |
| 201 void QuirksManager::RecordReasonUmaStat(QuirksClient::RequestReason reason) {} | |
| 202 | |
| 203 void QuirksManager::RecordFileFoundUmaStat(bool success) {} | |
| 204 | |
| 205 scoped_ptr<net::URLFetcher> QuirksManager::CreateURLFetcher( | |
| 206 const std::string& url, | |
| 207 net::URLFetcherDelegate* delegate) { | |
| 208 if (!fake_quirks_fetcher_creator_.is_null()) | |
| 209 return fake_quirks_fetcher_creator_.Run(GURL(url), delegate); | |
| 210 | |
| 211 return net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, delegate); | |
| 212 } | |
| 213 | |
| 214 // Initializes QuirksClient object on the ui thread, where most of its work | |
| 215 // needs to be done. | |
| 216 void QuirksManager::RunClientOnUIThread( | |
| 217 int64_t product_id, | |
| 218 const DownloadFinishedCallback& on_download_finished) { | |
| 219 DCHECK(base::ThreadTaskRunnerHandle::IsSet()); | |
| 220 if (!ValidateOnUiThread()) { | |
| 221 VLOG(1) << "Quirks Manager not correctly initialized."; | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 if (!NeedToCheckServer(product_id)) | |
| 226 return; | |
| 227 | |
| 228 QuirksClient* client = | |
| 229 new QuirksClient(product_id, on_download_finished, this); | |
| 230 clients_.insert(make_scoped_ptr(client)); | |
| 231 client->StartDownload(); | |
| 232 } | |
| 233 | |
| 234 bool QuirksManager::ValidateOnUiThread() { | |
| 235 return base::ThreadTaskRunnerHandle::IsSet() && | |
| 236 local_state_->CalledOnValidThread() && | |
| 237 task_runner_->RunsTasksOnCurrentThread(); | |
| 238 } | |
| 239 | |
| 240 } // namespace quirks | |
| OLD | NEW |