| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 } | 88 } |
| 87 | 89 |
| 88 buffered_writes_.push_back(base::Bind( | 90 buffered_writes_.push_back(base::Bind( |
| 89 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); | 91 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); |
| 90 Flush(); | 92 Flush(); |
| 91 } | 93 } |
| 92 | 94 |
| 93 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, | 95 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, |
| 94 const base::TimeDelta& latency, | 96 const base::TimeDelta& latency, |
| 95 const base::Time& fetch_time, | 97 const base::Time& fetch_time, |
| 96 int64_t size, | 98 const net::HttpResponseInfo& info, |
| 97 bool was_cached) { | 99 int64_t size) { |
| 98 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); | 100 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); |
| 99 | 101 |
| 100 if (!IsDatabaseAccessible()) { | 102 if (!IsDatabaseAccessible()) { |
| 101 // Don't track anything if unable to access the database. | 103 // Don't track anything if unable to access the database. |
| 102 return; | 104 return; |
| 103 } | 105 } |
| 104 | 106 |
| 105 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 107 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { |
| 106 // If the URL for this fetch is in the write buffer, then flush the write | 108 // If the URL for this fetch is in the write buffer, then flush the write |
| 107 // buffer. | 109 // buffer. |
| 108 Flush(); | 110 Flush(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 if (was_cached && !precache_url_table_.HasURL(url)) { | 113 DCHECK(info.headers) << "The headers are required to get the freshness."; |
| 114 if (info.headers) { |
| 115 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 116 "Precache.Freshness.Prefetch", |
| 117 info.headers->GetFreshnessLifetimes(info.response_time) |
| 118 .freshness.InSeconds(), |
| 119 base::TimeDelta::FromMinutes(5).InSeconds() /* min */, |
| 120 base::TimeDelta::FromDays(356).InSeconds() /* max */, |
| 121 100 /* bucket_count */); |
| 122 } |
| 123 |
| 124 if (info.was_cached && !precache_url_table_.HasURL(url)) { |
| 112 // Since the precache came from the cache, and there's no entry in the URL | 125 // Since the precache came from the cache, and there's no entry in the URL |
| 113 // table for the URL, this means that the resource was already in the cache | 126 // table for the URL, this means that the resource was already in the cache |
| 114 // because of user browsing. Thus, this precache had no effect, so ignore | 127 // because of user browsing. Therefore, this precache won't be considered as |
| 115 // it. | 128 // precache-motivated since it had no significant effect (besides a possible |
| 129 // revalidation and a change in the cache LRU priority). |
| 116 return; | 130 return; |
| 117 } | 131 } |
| 118 | 132 |
| 119 if (!was_cached) { | 133 if (!info.was_cached) { |
| 120 // The precache only counts as overhead if it was downloaded over the | 134 // The precache only counts as overhead if it was downloaded over the |
| 121 // network. | 135 // network. |
| 122 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", | 136 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", |
| 123 static_cast<base::HistogramBase::Sample>(size)); | 137 static_cast<base::HistogramBase::Sample>(size)); |
| 124 } | 138 } |
| 125 | 139 |
| 126 // Use the URL table to keep track of URLs that are in the cache thanks to | 140 // Use the URL table to keep track of URLs that are in the cache thanks to |
| 127 // precaching. If a row for the URL already exists, than update the timestamp | 141 // precaching. If a row for the URL already exists, than update the timestamp |
| 128 // to |fetch_time|. | 142 // to |fetch_time|. |
| 129 buffered_writes_.push_back( | 143 buffered_writes_.push_back( |
| 130 base::Bind(&PrecacheURLTable::AddURL, | 144 base::Bind(&PrecacheURLTable::AddURL, |
| 131 base::Unretained(&precache_url_table_), url, fetch_time)); | 145 base::Unretained(&precache_url_table_), url, fetch_time)); |
| 132 buffered_urls_.insert(url.spec()); | 146 buffered_urls_.insert(url.spec()); |
| 133 MaybePostFlush(); | 147 MaybePostFlush(); |
| 134 } | 148 } |
| 135 | 149 |
| 136 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, | 150 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, |
| 137 const base::TimeDelta& latency, | 151 const base::TimeDelta& latency, |
| 138 const base::Time& fetch_time, | 152 const base::Time& fetch_time, |
| 153 const net::HttpResponseInfo& info, |
| 139 int64_t size, | 154 int64_t size, |
| 140 bool was_cached, | |
| 141 int host_rank, | 155 int host_rank, |
| 142 bool is_connection_cellular) { | 156 bool is_connection_cellular) { |
| 143 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); | 157 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); |
| 144 | 158 |
| 145 if (host_rank != history::kMaxTopHosts) { | 159 if (host_rank != history::kMaxTopHosts) { |
| 146 // The resource was loaded on a page that could have been affected by | 160 // The resource was loaded on a page that could have been affected by |
| 147 // precaching. | 161 // precaching. |
| 148 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); | 162 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); |
| 149 } else { | 163 } else { |
| 150 // The resource was loaded on a page that could NOT have been affected by | 164 // The resource was loaded on a page that could NOT have been affected by |
| 151 // precaching. | 165 // precaching. |
| 152 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 166 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
| 153 } | 167 } |
| 154 | 168 |
| 155 if (!IsDatabaseAccessible()) { | 169 if (!IsDatabaseAccessible()) { |
| 156 // Don't track anything if unable to access the database. | 170 // Don't track anything if unable to access the database. |
| 157 return; | 171 return; |
| 158 } | 172 } |
| 159 | 173 |
| 160 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 174 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { |
| 161 // If the URL for this fetch is in the write buffer, then flush the write | 175 // If the URL for this fetch is in the write buffer, then flush the write |
| 162 // buffer. | 176 // buffer. |
| 163 Flush(); | 177 Flush(); |
| 164 } | 178 } |
| 165 | 179 |
| 166 if (was_cached && !precache_url_table_.HasURL(url)) { | 180 if (info.was_cached && !precache_url_table_.HasURL(url)) { |
| 167 // Ignore cache hits that precache can't take credit for. | 181 // Ignore cache hits that precache can't take credit for. |
| 168 return; | 182 return; |
| 169 } | 183 } |
| 170 | 184 |
| 171 base::HistogramBase::Sample size_sample = | 185 base::HistogramBase::Sample size_sample = |
| 172 static_cast<base::HistogramBase::Sample>(size); | 186 static_cast<base::HistogramBase::Sample>(size); |
| 173 if (!was_cached) { | 187 if (!info.was_cached) { |
| 174 // The fetch was served over the network during user browsing, so count it | 188 // The fetch was served over the network during user browsing, so count it |
| 175 // as downloaded non-precache bytes. | 189 // as downloaded non-precache bytes. |
| 176 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); | 190 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); |
| 177 if (is_connection_cellular) { | 191 if (is_connection_cellular) { |
| 178 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", | 192 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", |
| 179 size_sample); | 193 size_sample); |
| 180 } | 194 } |
| 181 } else { | 195 } else { // info.was_cached. |
| 182 // The fetch was served from the cache, and since there's an entry for this | 196 // The fetch was served from the cache, and since there's an entry for this |
| 183 // URL in the URL table, this means that the resource was served from the | 197 // URL in the URL table, this means that the resource was served from the |
| 184 // cache only because precaching put it there. Thus, precaching was helpful, | 198 // cache only because precaching put it there. Thus, precaching was helpful, |
| 185 // so count the fetch as saved bytes. | 199 // so count the fetch as saved bytes. |
| 186 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); | 200 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample); |
| 187 if (is_connection_cellular) { | 201 if (is_connection_cellular) { |
| 188 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); | 202 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample); |
| 189 } | 203 } |
| 204 |
| 205 DCHECK(info.headers) << "The headers are required to get the freshness."; |
| 206 if (info.headers) { |
| 207 // TODO(jamartin): Maybe report stale_while_validate as well. |
| 208 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 209 "Precache.Saved.Freshness", |
| 210 info.headers->GetFreshnessLifetimes(info.response_time) |
| 211 .freshness.InSeconds(), |
| 212 base::TimeDelta::FromMinutes(5).InSeconds() /* min */, |
| 213 base::TimeDelta::FromDays(356).InSeconds() /* max */, |
| 214 100 /* bucket_count */); |
| 215 } |
| 190 } | 216 } |
| 191 | 217 |
| 192 // Since the resource has been fetched during user browsing, remove any record | 218 // Since the resource has been fetched during user browsing, remove any record |
| 193 // of that URL having been precached from the URL table, if any exists. | 219 // of that URL having been precached from the URL table, if any exists. |
| 194 // The current fetch would have put this resource in the cache regardless of | 220 // The current fetch would have put this resource in the cache regardless of |
| 195 // whether or not it was previously precached, so delete any record of that | 221 // whether or not it was previously precached, so delete any record of that |
| 196 // URL having been precached from the URL table. | 222 // URL having been precached from the URL table. |
| 197 buffered_writes_.push_back( | 223 buffered_writes_.push_back( |
| 198 base::Bind(&PrecacheURLTable::DeleteURL, | 224 base::Bind(&PrecacheURLTable::DeleteURL, |
| 199 base::Unretained(&precache_url_table_), url)); | 225 base::Unretained(&precache_url_table_), url)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 303 |
| 278 void PrecacheDatabase::DeleteUnfinishedWork() { | 304 void PrecacheDatabase::DeleteUnfinishedWork() { |
| 279 precache_session_table_.DeleteUnfinishedWork(); | 305 precache_session_table_.DeleteUnfinishedWork(); |
| 280 } | 306 } |
| 281 | 307 |
| 282 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 308 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 283 return weak_factory_.GetWeakPtr(); | 309 return weak_factory_.GetWeakPtr(); |
| 284 } | 310 } |
| 285 | 311 |
| 286 } // namespace precache | 312 } // namespace precache |
| OLD | NEW |