| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
| 55 BrowserThread::DB, FROM_HERE, | 55 BrowserThread::DB, FROM_HERE, |
| 56 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init), | 56 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init), |
| 57 precache_database_, db_path)); | 57 precache_database_, db_path)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 PrecacheManager::~PrecacheManager() {} | 60 PrecacheManager::~PrecacheManager() {} |
| 61 | 61 |
| 62 bool PrecacheManager::ShouldRun() const { | 62 bool PrecacheManager::ShouldRun() const { |
| 63 // Verify IsPrecachingAllowed() before calling IsPrecachingEnabled(). This is | 63 // Verify PrecachingAllowed() before calling IsPrecachingEnabled(). This is |
| 64 // because field trials are only assigned when requested. This allows us to | 64 // because field trials are only assigned when requested. This allows us to |
| 65 // create Control and Experiment groups that are limited to users for whom | 65 // create Control and Experiment groups that are limited to users for whom |
| 66 // IsPrecachingAllowed() is true, thus accentuating the impact of precaching. | 66 // PrecachingAllowed() is true, thus accentuating the impact of precaching. |
| 67 return IsPrecachingAllowed() && IsPrecachingEnabled(); | 67 return PrecachingAllowed() == AllowedType::ALLOWED && IsPrecachingEnabled(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool PrecacheManager::WouldRun() const { | 70 bool PrecacheManager::WouldRun() const { |
| 71 return IsPrecachingAllowed(); | 71 return PrecachingAllowed() == AllowedType::ALLOWED; |
| 72 } | 72 } |
| 73 | 73 |
| 74 // static | 74 // static |
| 75 bool PrecacheManager::IsPrecachingEnabled() { | 75 bool PrecacheManager::IsPrecachingEnabled() { |
| 76 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == | 76 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == |
| 77 kPrecacheFieldTrialEnabledGroup || | 77 kPrecacheFieldTrialEnabledGroup || |
| 78 base::CommandLine::ForCurrentProcess()->HasSwitch( | 78 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 79 switches::kEnablePrecache); | 79 switches::kEnablePrecache); |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool PrecacheManager::IsPrecachingAllowed() const { | 82 PrecacheManager::AllowedType PrecacheManager::PrecachingAllowed() const { |
| 83 if (!(sync_service_ && sync_service_->backend_initialized())) |
| 84 return AllowedType::PENDING; |
| 85 |
| 83 // SyncService delegates to SyncPrefs, which must be called on the UI thread. | 86 // SyncService delegates to SyncPrefs, which must be called on the UI thread. |
| 84 return sync_service_ && | 87 if (sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && |
| 85 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && | 88 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
| 86 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS); | 89 return AllowedType::ALLOWED; |
| 90 |
| 91 return AllowedType::DISALLOWED; |
| 87 } | 92 } |
| 88 | 93 |
| 89 void PrecacheManager::StartPrecaching( | 94 void PrecacheManager::StartPrecaching( |
| 90 const PrecacheCompletionCallback& precache_completion_callback, | 95 const PrecacheCompletionCallback& precache_completion_callback, |
| 91 const history::HistoryService& history_service) { | 96 const history::HistoryService& history_service) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 97 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 93 | 98 |
| 94 if (is_precaching_) { | 99 if (is_precaching_) { |
| 95 DLOG(WARNING) << "Cannot start precaching because precaching is already " | 100 DLOG(WARNING) << "Cannot start precaching because precaching is already " |
| 96 "in progress."; | 101 "in progress."; |
| 97 return; | 102 return; |
| 98 } | 103 } |
| 99 is_precaching_ = true; | |
| 100 | |
| 101 precache_completion_callback_ = precache_completion_callback; | 104 precache_completion_callback_ = precache_completion_callback; |
| 102 | 105 |
| 103 if (ShouldRun()) { | 106 if (ShouldRun()) { |
| 107 is_precaching_ = true; |
| 108 |
| 104 BrowserThread::PostTask( | 109 BrowserThread::PostTask( |
| 105 BrowserThread::DB, FROM_HERE, | 110 BrowserThread::DB, FROM_HERE, |
| 106 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, | 111 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, |
| 107 precache_database_, base::Time::Now())); | 112 precache_database_, base::Time::Now())); |
| 108 | 113 |
| 109 // Request NumTopHosts() top hosts. Note that PrecacheFetcher is further | 114 // Request NumTopHosts() top hosts. Note that PrecacheFetcher is further |
| 110 // bound by the value of PrecacheConfigurationSettings.top_sites_count, as | 115 // bound by the value of PrecacheConfigurationSettings.top_sites_count, as |
| 111 // retrieved from the server. | 116 // retrieved from the server. |
| 112 history_service.TopHosts( | 117 history_service.TopHosts( |
| 113 NumTopHosts(), | 118 NumTopHosts(), |
| 114 base::Bind(&PrecacheManager::OnHostsReceived, AsWeakPtr())); | 119 base::Bind(&PrecacheManager::OnHostsReceived, AsWeakPtr())); |
| 115 } else { | 120 } else { |
| 121 if (PrecachingAllowed() != AllowedType::PENDING) { |
| 122 // We are not waiting on the sync backend to be initialized. The user |
| 123 // either is not in the field trial, or does not have sync enabled. |
| 124 // Pretend that precaching started, so that the PrecacheServiceLauncher |
| 125 // doesn't try to start it again. |
| 126 is_precaching_ = true; |
| 127 } |
| 128 |
| 116 OnDone(); | 129 OnDone(); |
| 117 } | 130 } |
| 118 } | 131 } |
| 119 | 132 |
| 120 void PrecacheManager::CancelPrecaching() { | 133 void PrecacheManager::CancelPrecaching() { |
| 121 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 122 | 135 |
| 123 if (!is_precaching_) { | 136 if (!is_precaching_) { |
| 124 // Do nothing if precaching is not in progress. | 137 // Do nothing if precaching is not in progress. |
| 125 return; | 138 return; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 base::Bind(&PrecacheDatabase::ClearHistory, precache_database_)); | 193 base::Bind(&PrecacheDatabase::ClearHistory, precache_database_)); |
| 181 } | 194 } |
| 182 | 195 |
| 183 void PrecacheManager::Shutdown() { | 196 void PrecacheManager::Shutdown() { |
| 184 CancelPrecaching(); | 197 CancelPrecaching(); |
| 185 } | 198 } |
| 186 | 199 |
| 187 void PrecacheManager::OnDone() { | 200 void PrecacheManager::OnDone() { |
| 188 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 201 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 189 | 202 |
| 190 // If OnDone has been called, then we should just be finishing precaching. | |
| 191 DCHECK(is_precaching_); | |
| 192 is_precaching_ = false; | |
| 193 | |
| 194 precache_fetcher_.reset(); | 203 precache_fetcher_.reset(); |
| 195 | 204 |
| 196 precache_completion_callback_.Run(); | 205 precache_completion_callback_.Run(!is_precaching_); |
| 197 // Uninitialize the callback so that any scoped_refptrs in it are released. | 206 // Uninitialize the callback so that any scoped_refptrs in it are released. |
| 198 precache_completion_callback_.Reset(); | 207 precache_completion_callback_.Reset(); |
| 208 |
| 209 is_precaching_ = false; |
| 199 } | 210 } |
| 200 | 211 |
| 201 void PrecacheManager::OnHostsReceived( | 212 void PrecacheManager::OnHostsReceived( |
| 202 const history::TopHostsList& host_counts) { | 213 const history::TopHostsList& host_counts) { |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 214 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 204 | 215 |
| 205 if (!is_precaching_) { | 216 if (!is_precaching_) { |
| 206 // Don't start precaching if it was canceled while waiting for the list of | 217 // Don't start precaching if it was canceled while waiting for the list of |
| 207 // hosts. | 218 // hosts. |
| 208 return; | 219 return; |
| 209 } | 220 } |
| 210 | 221 |
| 211 std::vector<std::string> hosts; | 222 std::vector<std::string> hosts; |
| 212 for (const auto& host_count : host_counts) | 223 for (const auto& host_count : host_counts) |
| 213 hosts.push_back(host_count.first); | 224 hosts.push_back(host_count.first); |
| 214 | 225 |
| 215 // Start precaching. | 226 // Start precaching. |
| 216 precache_fetcher_.reset( | 227 precache_fetcher_.reset( |
| 217 new PrecacheFetcher(hosts, browser_context_->GetRequestContext(), | 228 new PrecacheFetcher(hosts, browser_context_->GetRequestContext(), |
| 218 variations::GetVariationParamValue( | 229 variations::GetVariationParamValue( |
| 219 kPrecacheFieldTrialName, kManifestURLPrefixParam), | 230 kPrecacheFieldTrialName, kManifestURLPrefixParam), |
| 220 this)); | 231 this)); |
| 221 precache_fetcher_->Start(); | 232 precache_fetcher_->Start(); |
| 222 } | 233 } |
| 223 | 234 |
| 224 } // namespace precache | 235 } // namespace precache |
| OLD | NEW |