OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/precache/content/precache_manager.h" | 5 #include "components/precache/content/precache_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 using content::BrowserThread; | 30 using content::BrowserThread; |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 const char kPrecacheFieldTrialName[] = "Precache"; | 34 const char kPrecacheFieldTrialName[] = "Precache"; |
35 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; | 35 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; |
36 const char kPrecacheFieldTrialControlGroup[] = "Control"; | 36 const char kPrecacheFieldTrialControlGroup[] = "Control"; |
37 const char kConfigURLParam[] = "config_url"; | 37 const char kConfigURLParam[] = "config_url"; |
38 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; | 38 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; |
| 39 const char kGlobalRankingParam[] = "global_ranking"; |
39 const size_t kNumTopHosts = 100; | 40 const size_t kNumTopHosts = 100; |
40 | 41 |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 namespace precache { | 44 namespace precache { |
44 | 45 |
45 size_t NumTopHosts() { | 46 size_t NumTopHosts() { |
46 return kNumTopHosts; | 47 return kNumTopHosts; |
47 } | 48 } |
48 | 49 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 BrowserThread::PostTaskAndReplyWithResult( | 132 BrowserThread::PostTaskAndReplyWithResult( |
132 BrowserThread::DB, | 133 BrowserThread::DB, |
133 FROM_HERE, | 134 FROM_HERE, |
134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, | 135 base::Bind(&PrecacheDatabase::GetUnfinishedWork, |
135 base::Unretained(precache_database_.get())), | 136 base::Unretained(precache_database_.get())), |
136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); | 137 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); |
137 } | 138 } |
138 | 139 |
139 void PrecacheManager::OnGetUnfinishedWorkDone( | 140 void PrecacheManager::OnGetUnfinishedWorkDone( |
140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 141 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
141 if (!unfinished_work->has_start_time() || | 142 // Reset progress on a prefetch that has taken too long to complete. |
142 base::Time::Now() - base::Time::FromInternalValue( | 143 if (unfinished_work->has_start_time() && |
143 unfinished_work->start_time()) > base::TimeDelta::FromHours(6)) { | 144 base::Time::Now() - |
| 145 base::Time::FromInternalValue(unfinished_work->start_time()) > |
| 146 base::TimeDelta::FromHours(6)) { |
144 PrecacheFetcher::RecordCompletionStatistics( | 147 PrecacheFetcher::RecordCompletionStatistics( |
145 *unfinished_work, unfinished_work->top_host_size(), | 148 *unfinished_work, unfinished_work->top_host_size(), |
146 unfinished_work->resource_size()); | 149 unfinished_work->resource_size()); |
147 unfinished_work.reset(new PrecacheUnfinishedWork()); | 150 unfinished_work.reset(new PrecacheUnfinishedWork); |
| 151 } |
| 152 // If this prefetch is new, set the start time. |
| 153 if (!unfinished_work->has_start_time()) |
148 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); | 154 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); |
149 } | |
150 unfinished_work_ = std::move(unfinished_work); | 155 unfinished_work_ = std::move(unfinished_work); |
151 bool needs_top_hosts = unfinished_work_->top_host_size() == 0; | 156 bool needs_top_hosts = unfinished_work_->top_host_size() == 0; |
152 | 157 |
153 if (IsInExperimentGroup()) { | 158 if (IsInExperimentGroup()) { |
154 BrowserThread::PostTask( | 159 BrowserThread::PostTask( |
155 BrowserThread::DB, FROM_HERE, | 160 BrowserThread::DB, FROM_HERE, |
156 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, | 161 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, |
157 base::Unretained(precache_database_.get()), | 162 base::Unretained(precache_database_.get()), |
158 base::Time::Now())); | 163 base::Time::Now())); |
159 | 164 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 precache_completion_callback_.Reset(); | 318 precache_completion_callback_.Reset(); |
314 } | 319 } |
315 | 320 |
316 is_precaching_ = false; | 321 is_precaching_ = false; |
317 } | 322 } |
318 | 323 |
319 void PrecacheManager::OnHostsReceived( | 324 void PrecacheManager::OnHostsReceived( |
320 const history::TopHostsList& host_counts) { | 325 const history::TopHostsList& host_counts) { |
321 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 326 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
322 | 327 |
323 std::vector<std::string> hosts; | 328 for (const auto& host_count : host_counts) { |
324 for (const auto& host_count : host_counts) | 329 TopHost* top_host = unfinished_work_->add_top_host(); |
325 unfinished_work_->add_top_host()->set_hostname(host_count.first); | 330 top_host->set_hostname(host_count.first); |
| 331 top_host->set_visits(host_count.second); |
| 332 } |
326 InitializeAndStartFetcher(); | 333 InitializeAndStartFetcher(); |
327 } | 334 } |
328 | 335 |
329 void PrecacheManager::InitializeAndStartFetcher() { | 336 void PrecacheManager::InitializeAndStartFetcher() { |
330 if (!is_precaching_) { | 337 if (!is_precaching_) { |
331 // Don't start precaching if it was canceled while waiting for the list of | 338 // Don't start precaching if it was canceled while waiting for the list of |
332 // hosts. | 339 // hosts. |
333 return; | 340 return; |
334 } | 341 } |
335 // Start precaching. | 342 // Start precaching. |
336 precache_fetcher_.reset(new PrecacheFetcher( | 343 precache_fetcher_.reset(new PrecacheFetcher( |
337 content::BrowserContext::GetDefaultStoragePartition(browser_context_) | 344 content::BrowserContext::GetDefaultStoragePartition(browser_context_) |
338 ->GetURLRequestContext(), | 345 ->GetURLRequestContext(), |
339 GURL(variations::GetVariationParamValue(kPrecacheFieldTrialName, | 346 GURL(variations::GetVariationParamValue(kPrecacheFieldTrialName, |
340 kConfigURLParam)), | 347 kConfigURLParam)), |
341 variations::GetVariationParamValue(kPrecacheFieldTrialName, | 348 variations::GetVariationParamValue(kPrecacheFieldTrialName, |
342 kManifestURLPrefixParam), | 349 kManifestURLPrefixParam), |
| 350 !variations::GetVariationParamValue(kPrecacheFieldTrialName, |
| 351 kGlobalRankingParam) |
| 352 .empty(), |
343 std::move(unfinished_work_), | 353 std::move(unfinished_work_), |
344 metrics::HashName( | 354 metrics::HashName( |
345 base::FieldTrialList::FindFullName(kPrecacheFieldTrialName)), | 355 base::FieldTrialList::FindFullName(kPrecacheFieldTrialName)), |
346 precache_database_->GetWeakPtr(), | 356 precache_database_->GetWeakPtr(), |
347 content::BrowserThread::GetTaskRunnerForThread( | 357 content::BrowserThread::GetTaskRunnerForThread( |
348 content::BrowserThread::DB), | 358 content::BrowserThread::DB), |
349 this)); | 359 this)); |
350 precache_fetcher_->Start(); | 360 precache_fetcher_->Start(); |
351 } | 361 } |
352 | 362 |
353 void PrecacheManager::OnHostsReceivedThenDone( | 363 void PrecacheManager::OnHostsReceivedThenDone( |
354 const history::TopHostsList& host_counts) { | 364 const history::TopHostsList& host_counts) { |
355 OnDone(); | 365 OnDone(); |
356 } | 366 } |
357 | 367 |
358 } // namespace precache | 368 } // namespace precache |
OLD | NEW |