| 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/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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // Use the URL table to keep track of URLs. URLs that are fetched via network | 191 // 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 | 192 // 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 | 193 // precache-motivated. URLs that came from the cache and not recorded as |
| 194 // precached previously, were already in the cache because of user browsing. | 194 // precached previously, were already in the cache because of user browsing. |
| 195 // Therefore, this precache will not be considered as precache-motivated, | 195 // Therefore, this precache will not be considered as precache-motivated, |
| 196 // since it had no significant effect (besides a possible revalidation and a | 196 // 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, | 197 // change in the cache LRU priority). If a row for the URL already exists, |
| 198 // then the timestamp is updated. | 198 // then the timestamp is updated. |
| 199 buffered_writes_.push_back(base::Bind( | 199 buffered_writes_.push_back(base::Bind( |
| 200 &PrecacheDatabase::RecordURLPrefetchInternal, GetWeakPtr(), url, | 200 &PrecacheDatabase::RecordURLPrefetchInternal, GetWeakPtr(), url, |
| 201 referrer_host, !was_cached || precache_url_table_.IsURLPrecached(url), | 201 referrer_host, |
| 202 !was_cached || precache_url_table_.GetURLInfo(url).is_precached, |
| 202 fetch_time)); | 203 fetch_time)); |
| 203 buffered_urls_.insert(url.spec()); | 204 buffered_urls_.insert(url.spec()); |
| 204 MaybePostFlush(); | 205 MaybePostFlush(); |
| 205 } | 206 } |
| 206 | 207 |
| 207 void PrecacheDatabase::RecordURLPrefetchInternal( | 208 void PrecacheDatabase::RecordURLPrefetchInternal( |
| 208 const GURL& url, | 209 const GURL& url, |
| 209 const std::string& referrer_host, | 210 const std::string& referrer_host, |
| 210 bool is_precached, | 211 bool is_precached, |
| 211 const base::Time& fetch_time) { | 212 const base::Time& fetch_time) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } | 248 } |
| 248 | 249 |
| 249 RecordTimeSinceLastPrecache(fetch_time); | 250 RecordTimeSinceLastPrecache(fetch_time); |
| 250 | 251 |
| 251 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 252 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { |
| 252 // If the URL for this fetch is in the write buffer, then flush the write | 253 // If the URL for this fetch is in the write buffer, then flush the write |
| 253 // buffer. | 254 // buffer. |
| 254 Flush(); | 255 Flush(); |
| 255 } | 256 } |
| 256 | 257 |
| 257 bool is_precached = precache_url_table_.IsURLPrecachedAndUnused(url); | 258 const PrecacheURLInfo url_info = precache_url_table_.GetURLInfo(url); |
| 258 if (info.was_cached && !is_precached) { | 259 |
| 259 // Ignore cache hits that precache can't take credit for. | 260 if (url_info.was_precached) { |
| 260 return; | 261 UMA_HISTOGRAM_ENUMERATION( |
| 262 "Precache.CacheStatus.NonPrefetch.FromPrecache", |
| 263 info.cache_entry_status, |
| 264 net::HttpResponseInfo::CacheEntryStatus::ENTRY_MAX); |
| 261 } | 265 } |
| 262 | 266 |
| 263 base::HistogramBase::Sample size_sample = | 267 base::HistogramBase::Sample size_sample = |
| 264 static_cast<base::HistogramBase::Sample>(size); | 268 static_cast<base::HistogramBase::Sample>(size); |
| 265 if (!info.was_cached) { | 269 if (!info.was_cached) { |
| 266 // The fetch was served over the network during user browsing, so count it | 270 // The fetch was served over the network during user browsing, so count it |
| 267 // as downloaded non-precache bytes. | 271 // as downloaded non-precache bytes. |
| 268 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); | 272 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); |
| 269 if (is_connection_cellular) { | 273 if (is_connection_cellular) { |
| 270 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", | 274 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", |
| 271 size_sample); | 275 size_sample); |
| 272 } | 276 } |
| 273 // Since the resource has been fetched during user browsing, mark the URL as | 277 // Since the resource has been fetched during user browsing, mark the URL as |
| 274 // used in the precache URL table, if any exists. The current fetch would | 278 // used in the precache URL table, if any exists. The current fetch would |
| 275 // have put this resource in the cache regardless of whether or not it was | 279 // have put this resource in the cache regardless of whether or not it was |
| 276 // previously precached, so mark the URL as used. | 280 // previously precached, so mark the URL as used. |
| 277 buffered_writes_.push_back( | 281 buffered_writes_.push_back( |
| 278 base::Bind(&PrecacheURLTable::SetURLAsNotPrecached, | 282 base::Bind(&PrecacheURLTable::SetURLAsNotPrecached, |
| 279 base::Unretained(&precache_url_table_), url)); | 283 base::Unretained(&precache_url_table_), url)); |
| 280 buffered_urls_.insert(url.spec()); | 284 buffered_urls_.insert(url.spec()); |
| 281 MaybePostFlush(); | 285 MaybePostFlush(); |
| 282 } else { // info.was_cached. | 286 } else if (/* info.was_cached && */ url_info.is_precached && |
| 287 !url_info.was_used) { |
| 283 // The fetch was served from the cache, and since there's an entry for this | 288 // The fetch was served from the cache, and since there's an entry for this |
| 284 // URL in the URL table, this means that the resource was served from the | 289 // URL in the URL table, this means that the resource was served from the |
| 285 // cache only because precaching put it there. Thus, precaching was helpful, | 290 // cache only because precaching put it there. Thus, precaching was helpful, |
| 286 // so count the fetch as saved bytes. | 291 // so count the fetch as saved bytes. |
| 287 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); | 292 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); |
| 288 if (is_connection_cellular) { | 293 if (is_connection_cellular) { |
| 289 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); | 294 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); |
| 290 } | 295 } |
| 291 | 296 |
| 292 DCHECK(info.headers) << "The headers are required to get the freshness."; | 297 DCHECK(info.headers) << "The headers are required to get the freshness."; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 445 |
| 441 PrecacheQuota PrecacheDatabase::GetQuota() { | 446 PrecacheQuota PrecacheDatabase::GetQuota() { |
| 442 return precache_session_table_.GetQuota(); | 447 return precache_session_table_.GetQuota(); |
| 443 } | 448 } |
| 444 | 449 |
| 445 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 450 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 446 return weak_factory_.GetWeakPtr(); | 451 return weak_factory_.GetWeakPtr(); |
| 447 } | 452 } |
| 448 | 453 |
| 449 } // namespace precache | 454 } // namespace precache |
| OLD | NEW |