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/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" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/history/core/browser/history_constants.h" | 14 #include "components/history/core/browser/history_constants.h" |
| 15 #include "components/precache/core/proto/unfinished_work.pb.h" | 15 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 16 #include "net/http/http_response_headers.h" | |
| 16 #include "sql/connection.h" | 17 #include "sql/connection.h" |
| 17 #include "sql/transaction.h" | 18 #include "sql/transaction.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // The number of days old that an entry in the precache URL table can be before | 23 // The number of days old that an entry in the precache URL table can be before |
| 23 // it is considered "old" and is removed from the table. | 24 // it is considered "old" and is removed from the table. |
| 24 const int kPrecacheHistoryExpiryPeriodDays = 60; | 25 const int kPrecacheHistoryExpiryPeriodDays = 60; |
| 25 | 26 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 if (last_precache_timestamp_.is_null() && IsDatabaseAccessible()) { | 112 if (last_precache_timestamp_.is_null() && IsDatabaseAccessible()) { |
| 112 last_precache_timestamp_ = | 113 last_precache_timestamp_ = |
| 113 precache_session_table_.GetLastPrecacheTimestamp(); | 114 precache_session_table_.GetLastPrecacheTimestamp(); |
| 114 } | 115 } |
| 115 return last_precache_timestamp_; | 116 return last_precache_timestamp_; |
| 116 } | 117 } |
| 117 | 118 |
| 118 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, | 119 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, |
| 119 const base::TimeDelta& latency, | 120 const base::TimeDelta& latency, |
| 120 const base::Time& fetch_time, | 121 const base::Time& fetch_time, |
| 121 int64_t size, | 122 const net::HttpResponseInfo& info, |
| 122 bool was_cached) { | 123 int64_t size) { |
| 123 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); | 124 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); |
| 124 | 125 |
| 125 if (!IsDatabaseAccessible()) { | 126 if (!IsDatabaseAccessible()) { |
| 126 // Don't track anything if unable to access the database. | 127 // Don't track anything if unable to access the database. |
| 127 return; | 128 return; |
| 128 } | 129 } |
| 129 | 130 |
| 130 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 131 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { |
| 131 // If the URL for this fetch is in the write buffer, then flush the write | 132 // If the URL for this fetch is in the write buffer, then flush the write |
| 132 // buffer. | 133 // buffer. |
| 133 Flush(); | 134 Flush(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 if (was_cached && !precache_url_table_.HasURL(url)) { | 137 DCHECK(info.headers) << "The headers are required to get the freshness."; |
| 138 if (info.headers) { | |
|
bengr
2016/07/26 17:29:10
#include "net/http/http_response_info.h"
jamartin
2016/07/26 19:56:22
Done.
| |
| 139 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 140 "Precache.Freshness.Prefetch", | |
| 141 info.headers->GetFreshnessLifetimes(info.response_time) | |
| 142 .freshness.InSeconds(), | |
| 143 base::TimeDelta::FromMinutes(5).InSeconds() /* min */, | |
| 144 base::TimeDelta::FromDays(356).InSeconds() /* max */, | |
| 145 100 /* bucket_count */); | |
| 146 } | |
| 147 | |
| 148 if (info.was_cached && !precache_url_table_.HasURL(url)) { | |
| 137 // Since the precache came from the cache, and there's no entry in the URL | 149 // Since the precache came from the cache, and there's no entry in the URL |
| 138 // table for the URL, this means that the resource was already in the cache | 150 // table for the URL, this means that the resource was already in the cache |
| 139 // because of user browsing. Thus, this precache had no effect, so ignore | 151 // because of user browsing. Therefore, this precache won't be considered as |
| 140 // it. | 152 // precache-motivated since it had no significant effect (besides a possible |
| 153 // revalidation and a change in the cache LRU priority). | |
| 141 return; | 154 return; |
| 142 } | 155 } |
| 143 | 156 |
| 144 if (!was_cached) { | 157 if (!info.was_cached) { |
| 145 // The precache only counts as overhead if it was downloaded over the | 158 // The precache only counts as overhead if it was downloaded over the |
| 146 // network. | 159 // network. |
| 147 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", | 160 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", |
| 148 static_cast<base::HistogramBase::Sample>(size)); | 161 static_cast<base::HistogramBase::Sample>(size)); |
| 149 } | 162 } |
| 150 | 163 |
| 151 // Use the URL table to keep track of URLs that are in the cache thanks to | 164 // Use the URL table to keep track of URLs that are in the cache thanks to |
| 152 // precaching. If a row for the URL already exists, than update the timestamp | 165 // precaching. If a row for the URL already exists, than update the timestamp |
| 153 // to |fetch_time|. | 166 // to |fetch_time|. |
| 154 buffered_writes_.push_back( | 167 buffered_writes_.push_back( |
| 155 base::Bind(&PrecacheURLTable::AddURL, | 168 base::Bind(&PrecacheURLTable::AddURL, |
| 156 base::Unretained(&precache_url_table_), url, fetch_time)); | 169 base::Unretained(&precache_url_table_), url, fetch_time)); |
| 157 buffered_urls_.insert(url.spec()); | 170 buffered_urls_.insert(url.spec()); |
| 158 MaybePostFlush(); | 171 MaybePostFlush(); |
| 159 } | 172 } |
| 160 | 173 |
| 161 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, | 174 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, |
| 162 const base::TimeDelta& latency, | 175 const base::TimeDelta& latency, |
| 163 const base::Time& fetch_time, | 176 const base::Time& fetch_time, |
| 177 const net::HttpResponseInfo& info, | |
| 164 int64_t size, | 178 int64_t size, |
| 165 bool was_cached, | |
| 166 int host_rank, | 179 int host_rank, |
| 167 bool is_connection_cellular) { | 180 bool is_connection_cellular) { |
| 168 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); | 181 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); |
| 169 | 182 |
| 170 if (host_rank != history::kMaxTopHosts) { | 183 if (host_rank != history::kMaxTopHosts) { |
| 171 // The resource was loaded on a page that could have been affected by | 184 // The resource was loaded on a page that could have been affected by |
| 172 // precaching. | 185 // precaching. |
| 173 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); | 186 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); |
| 174 } else { | 187 } else { |
| 175 // The resource was loaded on a page that could NOT have been affected by | 188 // The resource was loaded on a page that could NOT have been affected by |
| 176 // precaching. | 189 // precaching. |
| 177 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 190 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
| 178 } | 191 } |
| 179 | 192 |
| 180 if (!IsDatabaseAccessible()) { | 193 if (!IsDatabaseAccessible()) { |
| 181 // Don't track anything if unable to access the database. | 194 // Don't track anything if unable to access the database. |
| 182 return; | 195 return; |
| 183 } | 196 } |
| 184 | 197 |
| 185 RecordTimeSinceLastPrecache(fetch_time); | 198 RecordTimeSinceLastPrecache(fetch_time); |
| 186 | 199 |
| 187 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 200 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { |
| 188 // If the URL for this fetch is in the write buffer, then flush the write | 201 // If the URL for this fetch is in the write buffer, then flush the write |
| 189 // buffer. | 202 // buffer. |
| 190 Flush(); | 203 Flush(); |
| 191 } | 204 } |
| 192 | 205 |
| 193 if (was_cached && !precache_url_table_.HasURL(url)) { | 206 if (info.was_cached && !precache_url_table_.HasURL(url)) { |
| 194 // Ignore cache hits that precache can't take credit for. | 207 // Ignore cache hits that precache can't take credit for. |
| 195 return; | 208 return; |
| 196 } | 209 } |
| 197 | 210 |
| 198 base::HistogramBase::Sample size_sample = | 211 base::HistogramBase::Sample size_sample = |
| 199 static_cast<base::HistogramBase::Sample>(size); | 212 static_cast<base::HistogramBase::Sample>(size); |
| 200 if (!was_cached) { | 213 if (!info.was_cached) { |
| 201 // The fetch was served over the network during user browsing, so count it | 214 // The fetch was served over the network during user browsing, so count it |
| 202 // as downloaded non-precache bytes. | 215 // as downloaded non-precache bytes. |
| 203 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); | 216 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); |
| 204 if (is_connection_cellular) { | 217 if (is_connection_cellular) { |
| 205 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", | 218 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", |
| 206 size_sample); | 219 size_sample); |
| 207 } | 220 } |
| 208 } else { | 221 } else { // info.was_cached. |
| 209 // The fetch was served from the cache, and since there's an entry for this | 222 // The fetch was served from the cache, and since there's an entry for this |
| 210 // URL in the URL table, this means that the resource was served from the | 223 // URL in the URL table, this means that the resource was served from the |
| 211 // cache only because precaching put it there. Thus, precaching was helpful, | 224 // cache only because precaching put it there. Thus, precaching was helpful, |
| 212 // so count the fetch as saved bytes. | 225 // so count the fetch as saved bytes. |
| 213 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); | 226 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); |
| 214 if (is_connection_cellular) { | 227 if (is_connection_cellular) { |
| 215 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); | 228 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); |
| 216 } | 229 } |
| 230 | |
| 231 DCHECK(info.headers) << "The headers are required to get the freshness."; | |
| 232 if (info.headers) { | |
| 233 // TODO(jamartin): Maybe report stale_while_validate as well. | |
| 234 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 235 "Precache.Saved.Freshness", | |
| 236 info.headers->GetFreshnessLifetimes(info.response_time) | |
| 237 .freshness.InSeconds(), | |
| 238 base::TimeDelta::FromMinutes(5).InSeconds() /* min */, | |
| 239 base::TimeDelta::FromDays(356).InSeconds() /* max */, | |
| 240 100 /* bucket_count */); | |
| 241 } | |
| 217 } | 242 } |
| 218 | 243 |
| 219 // Since the resource has been fetched during user browsing, remove any record | 244 // Since the resource has been fetched during user browsing, remove any record |
| 220 // of that URL having been precached from the URL table, if any exists. | 245 // of that URL having been precached from the URL table, if any exists. |
| 221 // The current fetch would have put this resource in the cache regardless of | 246 // The current fetch would have put this resource in the cache regardless of |
| 222 // whether or not it was previously precached, so delete any record of that | 247 // whether or not it was previously precached, so delete any record of that |
| 223 // URL having been precached from the URL table. | 248 // URL having been precached from the URL table. |
| 224 buffered_writes_.push_back( | 249 buffered_writes_.push_back( |
| 225 base::Bind(&PrecacheURLTable::DeleteURL, | 250 base::Bind(&PrecacheURLTable::DeleteURL, |
| 226 base::Unretained(&precache_url_table_), url)); | 251 base::Unretained(&precache_url_table_), url)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 | 348 |
| 324 void PrecacheDatabase::DeleteUnfinishedWork() { | 349 void PrecacheDatabase::DeleteUnfinishedWork() { |
| 325 precache_session_table_.DeleteUnfinishedWork(); | 350 precache_session_table_.DeleteUnfinishedWork(); |
| 326 } | 351 } |
| 327 | 352 |
| 328 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 353 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 329 return weak_factory_.GetWeakPtr(); | 354 return weak_factory_.GetWeakPtr(); |
| 330 } | 355 } |
| 331 | 356 |
| 332 } // namespace precache | 357 } // namespace precache |
| OLD | NEW |