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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/rand_util.h" | |
15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
16 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
17 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
18 #include "components/prefs/scoped_user_pref_update.h" | 17 #include "components/prefs/scoped_user_pref_update.h" |
19 #include "components/quirks/pref_names.h" | 18 #include "components/quirks/pref_names.h" |
20 #include "components/quirks/quirks_client.h" | 19 #include "components/quirks/quirks_client.h" |
21 #include "components/quirks/switches.h" | 20 #include "components/quirks/switches.h" |
22 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
23 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
24 #include "url/gurl.h" | 23 #include "url/gurl.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
41 // TODO(glevin): If file exists, do we want to implement a hash to verify that | 40 // TODO(glevin): If file exists, do we want to implement a hash to verify that |
42 // the file hasn't been corrupted or tampered with? | 41 // the file hasn't been corrupted or tampered with? |
43 return exists; | 42 return exists; |
44 } | 43 } |
45 | 44 |
46 base::FilePath CheckForIccFile(base::FilePath built_in_path, | 45 base::FilePath CheckForIccFile(base::FilePath built_in_path, |
47 base::FilePath download_path, | 46 base::FilePath download_path, |
48 bool quirks_enabled) { | 47 bool quirks_enabled) { |
49 // First, look for icc file in old read-only location. If there, we don't use | 48 // First, look for icc file in old read-only location. If there, we don't use |
50 // the Quirks server. | 49 // the Quirks server. |
51 // TODO(glevin): Awaiting final decision on how to handle old read-only files. | 50 // TODO(glevin): Awaiting final decision on how to handle old read-only files. |
Greg Levin
2016/07/08 23:18:42
TODO: Remove this TODO
Greg Levin
2016/07/10 15:30:10
Done.
| |
52 if (CheckAndLogFile(built_in_path)) | 51 if (CheckAndLogFile(built_in_path)) |
53 return built_in_path; | 52 return built_in_path; |
54 | 53 |
55 // If experimental Quirks flag isn't set, no other icc file is available. | 54 // If experimental Quirks flag isn't set, no other icc file is available. |
56 if (!quirks_enabled) { | 55 if (!quirks_enabled) { |
57 VLOG(1) << "Quirks Client disabled, no built-in icc file available."; | 56 VLOG(1) << "Quirks Client disabled, no built-in icc file available."; |
58 return base::FilePath(); | 57 return base::FilePath(); |
59 } | 58 } |
60 | 59 |
61 // Check if QuirksClient has already downloaded icc file from server. | 60 // Check if QuirksClient has already downloaded icc file from server. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 on_request_finished.Run(path, false); | 183 on_request_finished.Run(path, false); |
185 // TODO(glevin): If Quirks files are ever modified on the server, we'll need | 184 // TODO(glevin): If Quirks files are ever modified on the server, we'll need |
186 // to modify this logic to check for updates. See crbug.com/595024. | 185 // to modify this logic to check for updates. See crbug.com/595024. |
187 return; | 186 return; |
188 } | 187 } |
189 | 188 |
190 double last_check = 0.0; | 189 double last_check = 0.0; |
191 local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck) | 190 local_state_->GetDictionary(prefs::kQuirksClientLastServerCheck) |
192 ->GetDouble(IdToHexString(product_id), &last_check); | 191 ->GetDouble(IdToHexString(product_id), &last_check); |
193 | 192 |
194 // If never checked server before, need to check for new device. | |
195 if (last_check == 0.0) { | |
196 delegate_->GetDaysSinceOobe(base::Bind( | |
197 &QuirksManager::OnDaysSinceOobeReceived, weak_ptr_factory_.GetWeakPtr(), | |
198 product_id, on_request_finished)); | |
199 return; | |
200 } | |
201 | |
202 const base::TimeDelta time_since = | 193 const base::TimeDelta time_since = |
203 base::Time::Now() - base::Time::FromDoubleT(last_check); | 194 base::Time::Now() - base::Time::FromDoubleT(last_check); |
204 | 195 |
205 // Don't need server check if we've checked within last 30 days. | 196 // Don't need server check if we've checked within last 30 days. |
206 if (time_since < base::TimeDelta::FromDays(kDaysBetweenServerChecks)) { | 197 if (time_since < base::TimeDelta::FromDays(kDaysBetweenServerChecks)) { |
207 VLOG(2) << time_since.InDays() | 198 VLOG(2) << time_since.InDays() |
208 << " days since last Quirks Server check for display " | 199 << " days since last Quirks Server check for display " |
209 << IdToHexString(product_id); | 200 << IdToHexString(product_id); |
210 on_request_finished.Run(base::FilePath(), false); | 201 on_request_finished.Run(base::FilePath(), false); |
211 return; | 202 return; |
212 } | 203 } |
213 | 204 |
214 CreateClient(product_id, on_request_finished); | 205 // Create and start a client to download file. |
215 } | |
216 | |
217 void QuirksManager::OnDaysSinceOobeReceived( | |
218 int64_t product_id, | |
219 const RequestFinishedCallback& on_request_finished, | |
220 int days_since_oobe) { | |
221 DCHECK(thread_checker_.CalledOnValidThread()); | |
222 // On newer devices, we want to check server immediately (after OOBE/login). | |
223 if (days_since_oobe <= kDaysBetweenServerChecks) { | |
224 CreateClient(product_id, on_request_finished); | |
225 return; | |
226 } | |
227 | |
228 // Otherwise, for the first check on an older device, we want to stagger | |
229 // it over 30 days, so artificially set last check accordingly. | |
230 // TODO(glevin): I believe that it makes sense to remove this random delay | |
231 // in the next Chrome release. | |
232 const int rand_days = base::RandInt(0, kDaysBetweenServerChecks); | |
233 const base::Time fake_last_check = | |
234 base::Time::Now() - base::TimeDelta::FromDays(rand_days); | |
235 SetLastServerCheck(product_id, fake_last_check); | |
236 VLOG(2) << "Delaying first Quirks Server check by " | |
237 << kDaysBetweenServerChecks - rand_days << " days."; | |
238 | |
239 on_request_finished.Run(base::FilePath(), false); | |
240 } | |
241 | |
242 void QuirksManager::CreateClient( | |
243 int64_t product_id, | |
244 const RequestFinishedCallback& on_request_finished) { | |
245 DCHECK(thread_checker_.CalledOnValidThread()); | |
246 QuirksClient* client = | 206 QuirksClient* client = |
247 new QuirksClient(product_id, on_request_finished, this); | 207 new QuirksClient(product_id, on_request_finished, this); |
248 clients_.insert(base::WrapUnique(client)); | 208 clients_.insert(base::WrapUnique(client)); |
249 if (!waiting_for_login_) | 209 if (!waiting_for_login_) |
250 client->StartDownload(); | 210 client->StartDownload(); |
251 else | 211 else |
252 VLOG(2) << "Quirks Client created; waiting for login to begin download."; | 212 VLOG(2) << "Quirks Client created; waiting for login to begin download."; |
253 } | 213 } |
254 | 214 |
255 bool QuirksManager::QuirksEnabled() { | 215 bool QuirksManager::QuirksEnabled() { |
(...skipping 10 matching lines...) Expand all Loading... | |
266 } | 226 } |
267 | 227 |
268 void QuirksManager::SetLastServerCheck(int64_t product_id, | 228 void QuirksManager::SetLastServerCheck(int64_t product_id, |
269 const base::Time& last_check) { | 229 const base::Time& last_check) { |
270 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
271 DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck); | 231 DictionaryPrefUpdate dict(local_state_, prefs::kQuirksClientLastServerCheck); |
272 dict->SetDouble(IdToHexString(product_id), last_check.ToDoubleT()); | 232 dict->SetDouble(IdToHexString(product_id), last_check.ToDoubleT()); |
273 } | 233 } |
274 | 234 |
275 } // namespace quirks | 235 } // namespace quirks |
OLD | NEW |