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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 BrowserThread::PostTaskAndReplyWithResult( | 131 BrowserThread::PostTaskAndReplyWithResult( |
132 BrowserThread::DB, | 132 BrowserThread::DB, |
133 FROM_HERE, | 133 FROM_HERE, |
134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, | 134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, |
135 base::Unretained(precache_database_.get())), | 135 base::Unretained(precache_database_.get())), |
136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); | 136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); |
137 } | 137 } |
138 | 138 |
139 void PrecacheManager::OnGetUnfinishedWorkDone( | 139 void PrecacheManager::OnGetUnfinishedWorkDone( |
140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
141 if (!unfinished_work->has_start_time() || | 141 // Reset progress on a prefetch that has taken too long to complete. |
142 base::Time::Now() - base::Time::FromInternalValue( | 142 if (unfinished_work->has_start_time() && |
143 unfinished_work->start_time()) > base::TimeDelta::FromHours(6)) { | 143 base::Time::Now() - |
| 144 base::Time::FromInternalValue(unfinished_work->start_time()) > |
| 145 base::TimeDelta::FromHours(6)) { |
144 PrecacheFetcher::RecordCompletionStatistics( | 146 PrecacheFetcher::RecordCompletionStatistics( |
145 *unfinished_work, unfinished_work->top_host_size(), | 147 *unfinished_work, unfinished_work->top_host_size(), |
146 unfinished_work->resource_size()); | 148 unfinished_work->resource_size()); |
147 unfinished_work.reset(new PrecacheUnfinishedWork()); | 149 unfinished_work.reset(new PrecacheUnfinishedWork); |
| 150 } |
| 151 // If this prefetch is new, set the start time. |
| 152 if (!unfinished_work->has_start_time()) |
148 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); | 153 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); |
149 } | |
150 unfinished_work_ = std::move(unfinished_work); | 154 unfinished_work_ = std::move(unfinished_work); |
151 bool needs_top_hosts = unfinished_work_->top_host_size() == 0; | 155 bool needs_top_hosts = unfinished_work_->top_host_size() == 0; |
152 | 156 |
153 if (IsInExperimentGroup()) { | 157 if (IsInExperimentGroup()) { |
154 BrowserThread::PostTask( | 158 BrowserThread::PostTask( |
155 BrowserThread::DB, FROM_HERE, | 159 BrowserThread::DB, FROM_HERE, |
156 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, | 160 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, |
157 base::Unretained(precache_database_.get()), | 161 base::Unretained(precache_database_.get()), |
158 base::Time::Now())); | 162 base::Time::Now())); |
159 | 163 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 precache_completion_callback_.Reset(); | 317 precache_completion_callback_.Reset(); |
314 } | 318 } |
315 | 319 |
316 is_precaching_ = false; | 320 is_precaching_ = false; |
317 } | 321 } |
318 | 322 |
319 void PrecacheManager::OnHostsReceived( | 323 void PrecacheManager::OnHostsReceived( |
320 const history::TopHostsList& host_counts) { | 324 const history::TopHostsList& host_counts) { |
321 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 325 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
322 | 326 |
323 std::vector<std::string> hosts; | 327 for (const auto& host_count : host_counts) { |
324 for (const auto& host_count : host_counts) | 328 TopHost* top_host = unfinished_work_->add_top_host(); |
325 unfinished_work_->add_top_host()->set_hostname(host_count.first); | 329 top_host->set_hostname(host_count.first); |
| 330 top_host->set_visits(host_count.second); |
| 331 } |
326 InitializeAndStartFetcher(); | 332 InitializeAndStartFetcher(); |
327 } | 333 } |
328 | 334 |
329 void PrecacheManager::InitializeAndStartFetcher() { | 335 void PrecacheManager::InitializeAndStartFetcher() { |
330 if (!is_precaching_) { | 336 if (!is_precaching_) { |
331 // Don't start precaching if it was canceled while waiting for the list of | 337 // Don't start precaching if it was canceled while waiting for the list of |
332 // hosts. | 338 // hosts. |
333 return; | 339 return; |
334 } | 340 } |
335 // Start precaching. | 341 // Start precaching. |
(...skipping 13 matching lines...) Expand all Loading... |
349 this)); | 355 this)); |
350 precache_fetcher_->Start(); | 356 precache_fetcher_->Start(); |
351 } | 357 } |
352 | 358 |
353 void PrecacheManager::OnHostsReceivedThenDone( | 359 void PrecacheManager::OnHostsReceivedThenDone( |
354 const history::TopHostsList& host_counts) { | 360 const history::TopHostsList& host_counts) { |
355 OnDone(); | 361 OnDone(); |
356 } | 362 } |
357 | 363 |
358 } // namespace precache | 364 } // namespace precache |
OLD | NEW |