Chromium Code Reviews| 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 } | 160 } |
| 161 | 161 |
| 162 void PrecacheManager::CancelPrecaching() { | 162 void PrecacheManager::CancelPrecaching() { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 164 | 164 |
| 165 if (!is_precaching_) { | 165 if (!is_precaching_) { |
| 166 // Do nothing if precaching is not in progress. | 166 // Do nothing if precaching is not in progress. |
| 167 return; | 167 return; |
| 168 } | 168 } |
| 169 is_precaching_ = false; | 169 is_precaching_ = false; |
| 170 // If cancellation occurs after StartPrecaching but before OnHostsReceived, | |
| 171 // is_precaching will be true, but the precache_fetcher_ will not yet be | |
| 172 // constructed. | |
| 173 if (precache_fetcher_) | |
| 174 precache_fetcher_->Shutdown(); | |
| 175 } | |
| 170 | 176 |
| 177 void PrecacheManager::OnShutdownDone() { | |
| 171 // Destroying the |precache_fetcher_| will cancel any fetch in progress. | 178 // Destroying the |precache_fetcher_| will cancel any fetch in progress. |
| 172 precache_fetcher_.reset(); | 179 precache_fetcher_.reset(); |
| 173 | 180 |
| 174 // Uninitialize the callback so that any scoped_refptrs in it are released. | 181 // Uninitialize the callback so that any scoped_refptrs in it are released. |
| 175 precache_completion_callback_.Reset(); | 182 precache_completion_callback_.Reset(); |
| 176 } | 183 } |
| 177 | 184 |
| 178 bool PrecacheManager::IsPrecaching() const { | 185 bool PrecacheManager::IsPrecaching() const { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 186 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 180 return is_precaching_; | 187 return is_precaching_; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 base::Bind(&PrecacheDatabase::ClearHistory, | 250 base::Bind(&PrecacheDatabase::ClearHistory, |
| 244 base::Unretained(precache_database_.get()))); | 251 base::Unretained(precache_database_.get()))); |
| 245 } | 252 } |
| 246 | 253 |
| 247 void PrecacheManager::Shutdown() { | 254 void PrecacheManager::Shutdown() { |
| 248 CancelPrecaching(); | 255 CancelPrecaching(); |
| 249 } | 256 } |
| 250 | 257 |
| 251 void PrecacheManager::OnDone() { | 258 void PrecacheManager::OnDone() { |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 259 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 253 | |
| 254 precache_fetcher_.reset(); | 260 precache_fetcher_.reset(); |
| 255 | 261 |
| 256 // Run completion callback if not null. It's null if the client is in the | 262 // Run completion callback if not null. It's null if the client is in the |
| 257 // Control group and CancelPrecaching is called before TopHosts computation | 263 // Control group and CancelPrecaching is called before TopHosts computation |
| 258 // finishes. | 264 // finishes. |
| 259 if (!precache_completion_callback_.is_null()) { | 265 if (!precache_completion_callback_.is_null()) { |
| 260 precache_completion_callback_.Run(!is_precaching_); | 266 precache_completion_callback_.Run(!is_precaching_); |
| 261 // Uninitialize the callback so that any scoped_refptrs in it are released. | 267 // Uninitialize the callback so that any scoped_refptrs in it are released. |
| 262 precache_completion_callback_.Reset(); | 268 precache_completion_callback_.Reset(); |
| 263 } | 269 } |
| 264 | 270 |
| 265 is_precaching_ = false; | 271 is_precaching_ = false; |
| 266 } | 272 } |
| 267 | 273 |
| 268 void PrecacheManager::OnHostsReceived( | 274 void PrecacheManager::OnHostsReceived( |
| 269 const history::TopHostsList& host_counts) { | 275 const history::TopHostsList& host_counts) { |
| 270 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 276 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 271 | 277 |
| 272 if (!is_precaching_) { | 278 if (!is_precaching_) { |
| 273 // Don't start precaching if it was canceled while waiting for the list of | 279 // Don't start precaching if it was canceled while waiting for the list of |
| 274 // hosts. | 280 // hosts. |
| 275 return; | 281 return; |
| 276 } | 282 } |
| 277 | 283 |
| 278 std::vector<std::string> hosts; | 284 std::vector<std::string> hosts; |
| 279 for (const auto& host_count : host_counts) | 285 for (const auto& host_count : host_counts) |
| 280 hosts.push_back(host_count.first); | 286 hosts.push_back(host_count.first); |
| 281 | |
| 282 // Start precaching. | 287 // Start precaching. |
| 283 precache_fetcher_.reset(new PrecacheFetcher( | 288 precache_fetcher_.reset(new PrecacheFetcher( |
| 284 hosts, | 289 hosts, |
| 285 content::BrowserContext::GetDefaultStoragePartition(browser_context_)-> | 290 content::BrowserContext::GetDefaultStoragePartition(browser_context_)-> |
| 286 GetURLRequestContext(), | 291 GetURLRequestContext(), |
| 287 GURL(variations::GetVariationParamValue( | 292 GURL(variations::GetVariationParamValue( |
| 288 kPrecacheFieldTrialName, kConfigURLParam)), | 293 kPrecacheFieldTrialName, kConfigURLParam)), |
| 289 variations::GetVariationParamValue( | 294 variations::GetVariationParamValue( |
| 290 kPrecacheFieldTrialName, kManifestURLPrefixParam), | 295 kPrecacheFieldTrialName, kManifestURLPrefixParam), |
| 291 this)); | 296 this, |
| 297 precache_database_->GetWeakPtr(), | |
| 298 BrowserThread::GetMessageLoopProxyForThread( BrowserThread::DB))); | |
| 299 precache_fetcher_->Init(base::TimeDelta::FromHours(6)); | |
| 300 } | |
| 301 | |
| 302 void PrecacheManager::OnInitDone() { | |
|
sclittle
2016/05/10 00:01:26
Why is this method necessary? Couldn't PrecacheFet
bengr
2016/05/19 01:25:44
Done.
| |
| 292 precache_fetcher_->Start(); | 303 precache_fetcher_->Start(); |
| 293 } | 304 } |
| 294 | 305 |
| 295 void PrecacheManager::OnHostsReceivedThenDone( | 306 void PrecacheManager::OnHostsReceivedThenDone( |
| 296 const history::TopHostsList& host_counts) { | 307 const history::TopHostsList& host_counts) { |
| 297 OnDone(); | 308 OnDone(); |
| 298 } | 309 } |
| 299 | 310 |
| 300 } // namespace precache | 311 } // namespace precache |
| OLD | NEW |