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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "components/history/core/browser/history_service.h" | 17 #include "components/history/core/browser/history_service.h" |
| 18 #include "components/precache/core/precache_database.h" | 18 #include "components/precache/core/precache_database.h" |
| 19 #include "components/precache/core/precache_switches.h" | 19 #include "components/precache/core/precache_switches.h" |
| 20 #include "components/precache/core/proto/unfinished_work.pb.h" | 20 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 21 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 22 #include "components/sync/driver/sync_service.h" | 22 #include "components/sync/driver/sync_service.h" |
| 23 #include "components/variations/metrics_util.h" | 23 #include "components/variations/metrics_util.h" |
| 24 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| 25 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
| 26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/storage_partition.h" | 27 #include "content/public/browser/storage_partition.h" |
| 28 #include "net/base/network_change_notifier.h" | 28 #include "net/base/network_change_notifier.h" |
| 29 #include "net/http/http_cache.h" | |
| 30 #include "net/url_request/url_request_context.h" | |
| 31 #include "net/url_request/url_request_context_getter.h" | |
| 29 | 32 |
| 30 using content::BrowserThread; | 33 using content::BrowserThread; |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 const char kPrecacheFieldTrialName[] = "Precache"; | 37 const char kPrecacheFieldTrialName[] = "Precache"; |
| 35 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; | 38 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; |
| 36 const char kPrecacheFieldTrialControlGroup[] = "Control"; | 39 const char kPrecacheFieldTrialControlGroup[] = "Control"; |
| 37 const char kConfigURLParam[] = "config_url"; | 40 const char kConfigURLParam[] = "config_url"; |
| 38 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; | 41 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 | 107 |
| 105 // SyncService delegates to SyncPrefs, which must be called on the UI thread. | 108 // SyncService delegates to SyncPrefs, which must be called on the UI thread. |
| 106 if (history_service_ && | 109 if (history_service_ && |
| 107 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && | 110 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && |
| 108 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | 111 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
| 109 return AllowedType::ALLOWED; | 112 return AllowedType::ALLOWED; |
| 110 | 113 |
| 111 return AllowedType::DISALLOWED; | 114 return AllowedType::DISALLOWED; |
| 112 } | 115 } |
| 113 | 116 |
| 117 void PrecacheManager::OnCacheBackendReceived(int net_error_code) { | |
| 118 if (net_error_code == net::OK) { | |
|
twifkak
2016/11/18 00:18:00
Add a DCHECK_CURRENTLY_ON here? Also, are you sure
jamartin
2016/11/18 03:55:31
Discussed offline. It seems that most of this need
| |
| 119 DCHECK(cache_backend_); | |
| 120 int result = cache_backend_->CalculateSizeOfAllEntries(base::Bind( | |
| 121 &PrecacheManager::OnCacheSizeReceived, base::Unretained(this))); | |
| 122 if (result == net::ERR_IO_PENDING) { | |
| 123 // Wait for the callback. | |
| 124 } else if (result >= 0) { | |
| 125 // The result is the expected bytes already. | |
| 126 OnCacheSizeReceived(result); | |
| 127 } else { | |
| 128 // Error occurred. Couldn't get the size. Assume there is no cache. | |
| 129 OnCacheSizeReceived(0); | |
| 130 } | |
| 131 } else { // net_error_code != net::OK | |
| 132 // Assume there is no cache. | |
| 133 OnCacheSizeReceived(0); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) { | |
| 138 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 139 if (cache_size_bytes < min_cache_size_bytes_) { | |
| 140 OnDone(); | |
|
twifkak
2016/11/17 21:52:04
Add an UMA for the cache_size_bytes when this happ
jamartin
2016/11/18 03:55:32
Done.
| |
| 141 } else { | |
| 142 BrowserThread::PostTaskAndReplyWithResult( | |
| 143 BrowserThread::DB, FROM_HERE, | |
| 144 base::Bind(&PrecacheDatabase::GetUnfinishedWork, | |
| 145 base::Unretained(precache_database_.get())), | |
| 146 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); | |
| 147 } | |
| 148 } | |
| 149 | |
| 114 void PrecacheManager::StartPrecaching( | 150 void PrecacheManager::StartPrecaching( |
| 115 const PrecacheCompletionCallback& precache_completion_callback) { | 151 const PrecacheCompletionCallback& precache_completion_callback) { |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 152 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 117 | 153 |
| 118 if (is_precaching_) { | 154 if (is_precaching_) { |
| 119 DLOG(WARNING) << "Cannot start precaching because precaching is already " | 155 DLOG(WARNING) << "Cannot start precaching because precaching is already " |
| 120 "in progress."; | 156 "in progress."; |
| 121 return; | 157 return; |
| 122 } | 158 } |
| 123 precache_completion_callback_ = precache_completion_callback; | 159 precache_completion_callback_ = precache_completion_callback; |
| 124 | 160 |
| 125 is_precaching_ = true; | 161 is_precaching_ = true; |
| 126 BrowserThread::PostTask( | 162 BrowserThread::PostTask( |
| 127 BrowserThread::DB, FROM_HERE, | 163 BrowserThread::DB, FROM_HERE, |
| 128 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, | 164 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, |
| 129 base::Unretained(precache_database_.get()), | 165 base::Unretained(precache_database_.get()), |
| 130 base::Time::Now())); | 166 base::Time::Now())); |
| 131 BrowserThread::PostTaskAndReplyWithResult( | 167 |
| 132 BrowserThread::DB, | 168 // Continue with OnGetUnfinishedWorkDone only if the size of the cache is |
|
twifkak
2016/11/17 21:52:04
Perhaps this check should be inside OnGetUnfinishe
twifkak
2016/11/18 01:26:41
Actually, my next CL will want to pass the cache b
jamartin
2016/11/18 03:55:32
Acknowledged.
| |
| 133 FROM_HERE, | 169 // at least min_cache_size_bytes_. |
| 134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, | 170 // Class disk_cache::Backend does not expose its maximum size. However, caches |
| 135 base::Unretained(precache_database_.get())), | 171 // are usually full, so we can use the size of all the entries stored in the |
| 136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); | 172 // cache (via CalculateSizeOfAllEntries) as a proxy of its maximum size. |
| 173 net::HttpCache* cache = | |
| 174 content::BrowserContext::GetDefaultStoragePartition(browser_context_) | |
| 175 ->GetURLRequestContext() | |
|
twifkak
2016/11/17 21:52:04
May return null? https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
| |
| 176 ->GetURLRequestContext() | |
|
twifkak
2016/11/17 21:52:04
May return null: https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
| |
| 177 ->http_transaction_factory() | |
|
twifkak
2016/11/17 21:52:04
May return null? https://cs.chromium.org/chromium/
jamartin
2016/11/18 03:55:32
Done.
| |
| 178 ->GetCache(); | |
| 179 if (cache) { | |
| 180 const int net_error_code = cache->GetBackend( | |
| 181 &cache_backend_, base::Bind(&PrecacheManager::OnCacheBackendReceived, | |
| 182 base::Unretained(this))); | |
|
twifkak
2016/11/17 23:15:19
I think you should use AsWeakPtr() instead?
jamartin
2016/11/18 03:55:31
Done.
| |
| 183 if (net_error_code != net::ERR_IO_PENDING) { | |
| 184 // No need to wait for the callback. The callback hasn't been called with | |
| 185 // the appropriate code, so we call it directly. | |
| 186 OnCacheBackendReceived(net_error_code); | |
| 187 } | |
| 188 } else { // !cache | |
| 189 // There is no known cache. Assume that there is no cache. | |
| 190 // TODO(jamartin): Verify that it is not only that it lacks initialization. | |
| 191 OnCacheSizeReceived(0); | |
| 192 } | |
| 137 } | 193 } |
| 138 | 194 |
| 139 void PrecacheManager::OnGetUnfinishedWorkDone( | 195 void PrecacheManager::OnGetUnfinishedWorkDone( |
| 140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { | 196 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { |
| 141 // Reset progress on a prefetch that has taken too long to complete. | 197 // Reset progress on a prefetch that has taken too long to complete. |
| 142 if (unfinished_work->has_start_time() && | 198 if (unfinished_work->has_start_time() && |
| 143 base::Time::Now() - | 199 base::Time::Now() - |
| 144 base::Time::FromInternalValue(unfinished_work->start_time()) > | 200 base::Time::FromInternalValue(unfinished_work->start_time()) > |
| 145 base::TimeDelta::FromHours(6)) { | 201 base::TimeDelta::FromHours(6)) { |
| 146 PrecacheFetcher::RecordCompletionStatistics( | 202 PrecacheFetcher::RecordCompletionStatistics( |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 this)); | 411 this)); |
| 356 precache_fetcher_->Start(); | 412 precache_fetcher_->Start(); |
| 357 } | 413 } |
| 358 | 414 |
| 359 void PrecacheManager::OnHostsReceivedThenDone( | 415 void PrecacheManager::OnHostsReceivedThenDone( |
| 360 const history::TopHostsList& host_counts) { | 416 const history::TopHostsList& host_counts) { |
| 361 OnDone(); | 417 OnDone(); |
| 362 } | 418 } |
| 363 | 419 |
| 364 } // namespace precache | 420 } // namespace precache |
| OLD | NEW |