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

Side by Side Diff: components/precache/core/precache_database.cc

Issue 2586813004: Report downloaded resources at most once (Closed)
Patch Set: Removed superfluous test Created 3 years, 12 months 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/core/precache_database.h" 5 #include "components/precache/core/precache_database.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 126
127 PrecacheReferrerHostEntry PrecacheDatabase::GetReferrerHost( 127 PrecacheReferrerHostEntry PrecacheDatabase::GetReferrerHost(
128 const std::string& referrer_host) { 128 const std::string& referrer_host) {
129 DCHECK(thread_checker_.CalledOnValidThread()); 129 DCHECK(thread_checker_.CalledOnValidThread());
130 return precache_referrer_host_table_.GetReferrerHost(referrer_host); 130 return precache_referrer_host_table_.GetReferrerHost(referrer_host);
131 } 131 }
132 132
133 void PrecacheDatabase::GetURLListForReferrerHost( 133 void PrecacheDatabase::GetURLListForReferrerHost(
134 int64_t referrer_host_id, 134 int64_t referrer_host_id,
135 std::vector<GURL>* used_urls, 135 std::vector<GURL>* used_urls,
136 std::vector<GURL>* unused_urls) { 136 std::vector<GURL>* downloaded_urls) {
137 DCHECK(thread_checker_.CalledOnValidThread()); 137 DCHECK(thread_checker_.CalledOnValidThread());
138 DCHECK_NE(PrecacheReferrerHostEntry::kInvalidId, referrer_host_id); 138 DCHECK_NE(PrecacheReferrerHostEntry::kInvalidId, referrer_host_id);
139 139
140 // Flush any pending writes to the URL and referrer host tables. 140 // Flush any pending writes to the URL and referrer host tables.
141 Flush(); 141 Flush();
142 142
143 precache_url_table_.GetURLListForReferrerHost(referrer_host_id, used_urls, 143 precache_url_table_.GetURLListForReferrerHost(referrer_host_id, used_urls,
144 unused_urls); 144 downloaded_urls);
145 precache_url_table_.SetDownloadReported(referrer_host_id);
145 } 146 }
146 147
147 void PrecacheDatabase::RecordURLPrefetchMetrics( 148 void PrecacheDatabase::RecordURLPrefetchMetrics(
148 const net::HttpResponseInfo& info, 149 const net::HttpResponseInfo& info,
149 const base::TimeDelta& latency) { 150 const base::TimeDelta& latency) {
150 DCHECK(thread_checker_.CalledOnValidThread()); 151 DCHECK(thread_checker_.CalledOnValidThread());
151 152
152 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); 153 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency);
153 154
154 DCHECK(info.headers) << "The headers are required to get the freshness."; 155 DCHECK(info.headers) << "The headers are required to get the freshness.";
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 190 }
190 191
191 // Use the URL table to keep track of URLs. URLs that are fetched via network 192 // Use the URL table to keep track of URLs. URLs that are fetched via network
192 // or already in the cache due to prior precaching are recorded as 193 // or already in the cache due to prior precaching are recorded as
193 // precache-motivated. URLs that came from the cache and not recorded as 194 // precache-motivated. URLs that came from the cache and not recorded as
194 // precached previously, were already in the cache because of user browsing. 195 // precached previously, were already in the cache because of user browsing.
195 // Therefore, this precache will not be considered as precache-motivated, 196 // Therefore, this precache will not be considered as precache-motivated,
196 // since it had no significant effect (besides a possible revalidation and a 197 // since it had no significant effect (besides a possible revalidation and a
197 // change in the cache LRU priority). If a row for the URL already exists, 198 // change in the cache LRU priority). If a row for the URL already exists,
198 // then the timestamp is updated. 199 // then the timestamp is updated.
200 const PrecacheURLInfo info = precache_url_table_.GetURLInfo(url);
199 buffered_writes_.push_back(base::Bind( 201 buffered_writes_.push_back(base::Bind(
200 &PrecacheDatabase::RecordURLPrefetchInternal, GetWeakPtr(), url, 202 &PrecacheDatabase::RecordURLPrefetchInternal, GetWeakPtr(), url,
201 referrer_host, 203 referrer_host, !was_cached || info.is_precached, fetch_time,
202 !was_cached || precache_url_table_.GetURLInfo(url).is_precached, 204 // Reset to 0 only if is_precached && !was_cached.
203 fetch_time)); 205 (!info.is_precached || was_cached) && info.is_download_reported));
twifkak 2016/12/20 22:48:09 Perhaps extract this logic to be more readable: b
jamartin 2016/12/20 23:14:07 Done.
204 buffered_urls_.insert(url.spec()); 206 buffered_urls_.insert(url.spec());
205 MaybePostFlush(); 207 MaybePostFlush();
206 } 208 }
207 209
208 void PrecacheDatabase::RecordURLPrefetchInternal( 210 void PrecacheDatabase::RecordURLPrefetchInternal(
209 const GURL& url, 211 const GURL& url,
210 const std::string& referrer_host, 212 const std::string& referrer_host,
211 bool is_precached, 213 bool is_precached,
212 const base::Time& fetch_time) { 214 const base::Time& fetch_time,
215 bool is_download_reported) {
213 int64_t referrer_host_id = 216 int64_t referrer_host_id =
214 precache_referrer_host_table_.GetReferrerHost(referrer_host).id; 217 precache_referrer_host_table_.GetReferrerHost(referrer_host).id;
215 if (referrer_host_id == PrecacheReferrerHostEntry::kInvalidId) { 218 if (referrer_host_id == PrecacheReferrerHostEntry::kInvalidId) {
216 referrer_host_id = precache_referrer_host_table_.UpdateReferrerHost( 219 referrer_host_id = precache_referrer_host_table_.UpdateReferrerHost(
217 referrer_host, 0, fetch_time); 220 referrer_host, 0, fetch_time);
218 } 221 }
219 DCHECK_NE(referrer_host_id, PrecacheReferrerHostEntry::kInvalidId); 222 DCHECK_NE(referrer_host_id, PrecacheReferrerHostEntry::kInvalidId);
220 precache_url_table_.AddURL(url, referrer_host_id, is_precached, fetch_time); 223 precache_url_table_.AddURL(url, referrer_host_id, is_precached, fetch_time,
224 is_download_reported);
221 } 225 }
222 226
223 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, 227 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url,
224 const base::TimeDelta& latency, 228 const base::TimeDelta& latency,
225 const base::Time& fetch_time, 229 const base::Time& fetch_time,
226 const net::HttpResponseInfo& info, 230 const net::HttpResponseInfo& info,
227 int64_t size, 231 int64_t size,
228 int host_rank, 232 int host_rank,
229 bool is_connection_cellular) { 233 bool is_connection_cellular) {
230 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); 234 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 GetWeakPtr(), hostname, manifest_id, fetch_time)); 334 GetWeakPtr(), hostname, manifest_id, fetch_time));
331 MaybePostFlush(); 335 MaybePostFlush();
332 } 336 }
333 337
334 void PrecacheDatabase::UpdatePrecacheReferrerHostInternal( 338 void PrecacheDatabase::UpdatePrecacheReferrerHostInternal(
335 const std::string& hostname, 339 const std::string& hostname,
336 int64_t manifest_id, 340 int64_t manifest_id,
337 const base::Time& fetch_time) { 341 const base::Time& fetch_time) {
338 int64_t referrer_host_id = precache_referrer_host_table_.UpdateReferrerHost( 342 int64_t referrer_host_id = precache_referrer_host_table_.UpdateReferrerHost(
339 hostname, manifest_id, fetch_time); 343 hostname, manifest_id, fetch_time);
344
340 if (referrer_host_id != PrecacheReferrerHostEntry::kInvalidId) { 345 if (referrer_host_id != PrecacheReferrerHostEntry::kInvalidId) {
341 precache_url_table_.ClearAllForReferrerHost(referrer_host_id); 346 precache_url_table_.ClearAllForReferrerHost(referrer_host_id);
342 } 347 }
343 } 348 }
344 349
345 void PrecacheDatabase::RecordTimeSinceLastPrecache( 350 void PrecacheDatabase::RecordTimeSinceLastPrecache(
346 const base::Time& fetch_time) { 351 const base::Time& fetch_time) {
347 const base::Time& last_precache_timestamp = GetLastPrecacheTimestamp(); 352 const base::Time& last_precache_timestamp = GetLastPrecacheTimestamp();
348 // It could still be null if the DB was not accessible. 353 // It could still be null if the DB was not accessible.
349 if (!last_precache_timestamp.is_null()) { 354 if (!last_precache_timestamp.is_null()) {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 450
446 PrecacheQuota PrecacheDatabase::GetQuota() { 451 PrecacheQuota PrecacheDatabase::GetQuota() {
447 return precache_session_table_.GetQuota(); 452 return precache_session_table_.GetQuota();
448 } 453 }
449 454
450 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { 455 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() {
451 return weak_factory_.GetWeakPtr(); 456 return weak_factory_.GetWeakPtr();
452 } 457 }
453 458
454 } // namespace precache 459 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698