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

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

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

Powered by Google App Engine
This is Rietveld 408576698