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