Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Side by Side Diff: components/precache/content/precache_manager.cc

Issue 2507753003: Do not precache when the cache size is small (Closed)
Patch Set: Comment on the IO thread Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/histogram_macros.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "components/history/core/browser/history_service.h" 18 #include "components/history/core/browser/history_service.h"
18 #include "components/precache/core/precache_database.h" 19 #include "components/precache/core/precache_database.h"
19 #include "components/precache/core/precache_switches.h" 20 #include "components/precache/core/precache_switches.h"
20 #include "components/precache/core/proto/unfinished_work.pb.h" 21 #include "components/precache/core/proto/unfinished_work.pb.h"
21 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
22 #include "components/sync/driver/sync_service.h" 23 #include "components/sync/driver/sync_service.h"
23 #include "components/variations/metrics_util.h" 24 #include "components/variations/metrics_util.h"
24 #include "components/variations/variations_associated_data.h" 25 #include "components/variations/variations_associated_data.h"
25 #include "content/public/browser/browser_context.h" 26 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/storage_partition.h" 28 #include "content/public/browser/storage_partition.h"
28 #include "net/base/network_change_notifier.h" 29 #include "net/base/network_change_notifier.h"
30 #include "net/http/http_cache.h"
31 #include "net/url_request/url_request_context.h"
32 #include "net/url_request/url_request_context_getter.h"
29 33
30 using content::BrowserThread; 34 using content::BrowserThread;
31 35
32 namespace { 36 namespace {
33 37
34 const char kPrecacheFieldTrialName[] = "Precache"; 38 const char kPrecacheFieldTrialName[] = "Precache";
35 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; 39 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled";
36 const char kPrecacheFieldTrialControlGroup[] = "Control"; 40 const char kPrecacheFieldTrialControlGroup[] = "Control";
37 const char kConfigURLParam[] = "config_url"; 41 const char kConfigURLParam[] = "config_url";
38 const char kManifestURLPrefixParam[] = "manifest_url_prefix"; 42 const char kManifestURLPrefixParam[] = "manifest_url_prefix";
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 108
105 // SyncService delegates to SyncPrefs, which must be called on the UI thread. 109 // SyncService delegates to SyncPrefs, which must be called on the UI thread.
106 if (history_service_ && 110 if (history_service_ &&
107 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) && 111 sync_service_->GetActiveDataTypes().Has(syncer::SESSIONS) &&
108 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS)) 112 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS))
109 return AllowedType::ALLOWED; 113 return AllowedType::ALLOWED;
110 114
111 return AllowedType::DISALLOWED; 115 return AllowedType::DISALLOWED;
112 } 116 }
113 117
118 void PrecacheManager::OnCacheBackendReceived(int net_error_code) {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 if (net_error_code == net::OK) {
121 DCHECK(cache_backend_);
122 int result = cache_backend_->CalculateSizeOfAllEntries(
123 base::Bind(&PrecacheManager::OnCacheSizeReceived, AsWeakPtr()));
124 if (result == net::ERR_IO_PENDING) {
125 // Wait for the callback.
126 } else if (result >= 0) {
127 // The result is the expected bytes already.
128 OnCacheSizeReceived(result);
129 } else {
130 // Error occurred. Couldn't get the size. Assume there is no cache.
131 OnCacheSizeReceived(0);
132 }
133 } else { // net_error_code != net::OK
134 // Assume there is no cache.
135 OnCacheSizeReceived(0);
136 }
137 cache_backend_ = nullptr;
138 }
139
140 void PrecacheManager::OnCacheSizeReceived(int cache_size_bytes) {
141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
142
143 BrowserThread::PostTask(
144 BrowserThread::UI, FROM_HERE,
145 base::Bind(&PrecacheManager::OnCacheSizeReceivedInUIThread, AsWeakPtr(),
146 cache_size_bytes));
147 }
148
149 void PrecacheManager::OnCacheSizeReceivedInUIThread(int cache_size_bytes) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI);
151
152 UMA_HISTOGRAM_MEMORY_KB("Precache.CacheSize.AllEntries",
153 cache_size_bytes / 1024);
154 if (cache_size_bytes < min_cache_size_bytes_) {
155 OnDone(); // Do not continue.
156 } else {
157 BrowserThread::PostTaskAndReplyWithResult(
158 BrowserThread::DB, FROM_HERE,
159 base::Bind(&PrecacheDatabase::GetUnfinishedWork,
160 base::Unretained(precache_database_.get())),
161 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr()));
162 }
163 }
164
165 void PrecacheManager::PrecacheIfCacheIsBigEnough(
166 net::URLRequestContextGetter* url_request_context_getter) {
167 DCHECK_CURRENTLY_ON(BrowserThread::IO);
168 CHECK(url_request_context_getter);
169
170 // Continue with OnGetUnfinishedWorkDone only if the size of the cache is
171 // at least min_cache_size_bytes_.
172 // Class disk_cache::Backend does not expose its maximum size. However, caches
173 // are usually full, so we can use the size of all the entries stored in the
174 // cache (via CalculateSizeOfAllEntries) as a proxy of its maximum size.
175 net::URLRequestContext* context =
176 url_request_context_getter->GetURLRequestContext();
177 if (!context) {
178 OnCacheSizeReceived(0);
179 return;
180 }
181 net::HttpTransactionFactory* factory = context->http_transaction_factory();
182 if (!factory) {
183 OnCacheSizeReceived(0);
184 return;
185 }
186 net::HttpCache* cache = factory->GetCache();
187 if (cache) {
188 const int net_error_code = cache->GetBackend(
189 &cache_backend_,
190 base::Bind(&PrecacheManager::OnCacheBackendReceived, AsWeakPtr()));
191 if (net_error_code != net::ERR_IO_PENDING) {
192 // No need to wait for the callback. The callback hasn't been called with
193 // the appropriate code, so we call it directly.
194 OnCacheBackendReceived(net_error_code);
195 }
196 } else { // !cache.
197 // There is no known cache. Assume that there is no cache.
198 OnCacheSizeReceived(0);
199 }
200 }
201
114 void PrecacheManager::StartPrecaching( 202 void PrecacheManager::StartPrecaching(
115 const PrecacheCompletionCallback& precache_completion_callback) { 203 const PrecacheCompletionCallback& precache_completion_callback) {
116 DCHECK_CURRENTLY_ON(BrowserThread::UI); 204 DCHECK_CURRENTLY_ON(BrowserThread::UI);
117 205
118 if (is_precaching_) { 206 if (is_precaching_) {
119 DLOG(WARNING) << "Cannot start precaching because precaching is already " 207 DLOG(WARNING) << "Cannot start precaching because precaching is already "
120 "in progress."; 208 "in progress.";
121 return; 209 return;
122 } 210 }
123 precache_completion_callback_ = precache_completion_callback; 211 precache_completion_callback_ = precache_completion_callback;
124 212
125 is_precaching_ = true; 213 is_precaching_ = true;
126 BrowserThread::PostTask( 214 BrowserThread::PostTask(
127 BrowserThread::DB, FROM_HERE, 215 BrowserThread::DB, FROM_HERE,
128 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp, 216 base::Bind(&PrecacheDatabase::SetLastPrecacheTimestamp,
129 base::Unretained(precache_database_.get()), 217 base::Unretained(precache_database_.get()),
130 base::Time::Now())); 218 base::Time::Now()));
131 BrowserThread::PostTaskAndReplyWithResult( 219
132 BrowserThread::DB, 220 net::URLRequestContextGetter* url_request_context_getter =
133 FROM_HERE, 221 content::BrowserContext::GetDefaultStoragePartition(browser_context_)
134 base::Bind(&PrecacheDatabase::GetUnfinishedWork, 222 ->GetURLRequestContext();
135 base::Unretained(precache_database_.get())), 223 if (url_request_context_getter) {
136 base::Bind(&PrecacheManager::OnGetUnfinishedWorkDone, AsWeakPtr())); 224 BrowserThread::PostTask(
225 BrowserThread::IO, FROM_HERE,
226 base::Bind(&PrecacheManager::PrecacheIfCacheIsBigEnough, AsWeakPtr(),
227 base::Unretained(url_request_context_getter)));
twifkak 2016/11/19 02:15:06 Actually, on further thought, use scoped_refptr<>
jamartin 2016/11/19 02:54:22 Done.
228 } else { // !url_request_context_getter_.
229 OnCacheSizeReceivedInUIThread(0);
230 }
137 } 231 }
138 232
139 void PrecacheManager::OnGetUnfinishedWorkDone( 233 void PrecacheManager::OnGetUnfinishedWorkDone(
140 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) { 234 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) {
141 // Reset progress on a prefetch that has taken too long to complete. 235 // Reset progress on a prefetch that has taken too long to complete.
142 if (unfinished_work->has_start_time() && 236 if (unfinished_work->has_start_time() &&
143 base::Time::Now() - 237 base::Time::Now() -
144 base::Time::FromInternalValue(unfinished_work->start_time()) > 238 base::Time::FromInternalValue(unfinished_work->start_time()) >
145 base::TimeDelta::FromHours(6)) { 239 base::TimeDelta::FromHours(6)) {
146 PrecacheFetcher::RecordCompletionStatistics( 240 PrecacheFetcher::RecordCompletionStatistics(
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 this)); 449 this));
356 precache_fetcher_->Start(); 450 precache_fetcher_->Start();
357 } 451 }
358 452
359 void PrecacheManager::OnHostsReceivedThenDone( 453 void PrecacheManager::OnHostsReceivedThenDone(
360 const history::TopHostsList& host_counts) { 454 const history::TopHostsList& host_counts) {
361 OnDone(); 455 OnDone();
362 } 456 }
363 457
364 } // namespace precache 458 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/content/precache_manager.h ('k') | components/precache/content/precache_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698