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" | |
| 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."; |
| 112 // Since the precache came from the cache, and there's no entry in the URL | 114 if (info.headers) { |
| 113 // table for the URL, this means that the resource was already in the cache | 115 UMA_HISTOGRAM_CUSTOM_COUNTS( |
|
twifkak
2016/07/18 04:30:35
Why not UMA_HISTOGRAM_CUSTOM_TIMES?
jamartin
2016/07/18 16:29:43
I could if you really wanted. However, UMA_HISTOGR
twifkak
2016/07/18 23:50:44
Nah, that's a good enough reason for me.
| |
| 114 // because of user browsing. Thus, this precache had no effect, so ignore | 116 "Precache.Freshness.Prefetch", |
| 115 // it. | 117 info.headers->GetFreshnessLifetimes(info.response_time) |
| 118 .freshness.InSeconds(), | |
| 119 base::TimeDelta::FromMinutes(1).InSeconds() /* min */, | |
| 120 base::TimeDelta::FromHours(36).InSeconds() /* max */, | |
|
twifkak
2016/07/18 04:30:35
Obviously a lot of things are going to go in the o
jamartin
2016/07/18 16:29:43
Well, my thought was to mimic the other counter (.
twifkak
2016/07/18 23:50:44
Yeah, that sounds pretty reasonable.
| |
| 121 100 /* bucket_count */); | |
| 122 } | |
| 123 | |
| 124 if (info.was_cached && !info.network_accessed && | |
|
twifkak
2016/07/18 04:30:35
I thought the plan was to add a Precache.Saved.Upp
jamartin
2016/07/18 16:29:43
You are right. I don't understand this code as wel
twifkak
2016/07/18 23:50:44
Oy, that sounds fun.
| |
| 125 !precache_url_table_.HasURL(url)) { | |
| 126 // Since the precache came from the cache, there's no entry in the URL table | |
| 127 // for the URL and there was no validation, this means that the resource was | |
| 128 // already in the cache because of user browsing. Thus, this precache had no | |
| 129 // effect, so ignore it. | |
| 130 // We take credit for resources that were already in the cache due to user | |
| 131 // browsing but needed validation at the time of the prefetch. | |
| 116 return; | 132 return; |
| 117 } | 133 } |
| 118 | 134 |
| 119 if (!was_cached) { | 135 if (!info.was_cached) { |
| 120 // The precache only counts as overhead if it was downloaded over the | 136 // The precache only counts as overhead if it was downloaded over the |
| 121 // network. | 137 // network. |
| 122 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", | 138 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", |
| 123 static_cast<base::HistogramBase::Sample>(size)); | 139 static_cast<base::HistogramBase::Sample>(size)); |
| 124 } | 140 } |
| 125 | 141 |
| 126 // Use the URL table to keep track of URLs that are in the cache thanks to | 142 // 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 | 143 // precaching. If a row for the URL already exists, than update the timestamp |
| 128 // to |fetch_time|. | 144 // to |fetch_time|. |
| 129 buffered_writes_.push_back( | 145 buffered_writes_.push_back( |
| 130 base::Bind(&PrecacheURLTable::AddURL, | 146 base::Bind(&PrecacheURLTable::AddURL, |
| 131 base::Unretained(&precache_url_table_), url, fetch_time)); | 147 base::Unretained(&precache_url_table_), url, fetch_time)); |
| 132 buffered_urls_.insert(url.spec()); | 148 buffered_urls_.insert(url.spec()); |
| 133 MaybePostFlush(); | 149 MaybePostFlush(); |
| 134 } | 150 } |
| 135 | 151 |
| 136 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, | 152 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url, |
| 137 const base::TimeDelta& latency, | 153 const base::TimeDelta& latency, |
| 138 const base::Time& fetch_time, | 154 const base::Time& fetch_time, |
| 155 const net::HttpResponseInfo& info, | |
| 139 int64_t size, | 156 int64_t size, |
| 140 bool was_cached, | |
| 141 int host_rank, | 157 int host_rank, |
| 142 bool is_connection_cellular) { | 158 bool is_connection_cellular) { |
| 143 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); | 159 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency); |
| 144 | 160 |
| 145 if (host_rank != history::kMaxTopHosts) { | 161 if (host_rank != history::kMaxTopHosts) { |
| 146 // The resource was loaded on a page that could have been affected by | 162 // The resource was loaded on a page that could have been affected by |
| 147 // precaching. | 163 // precaching. |
| 148 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); | 164 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency); |
| 149 } else { | 165 } else { |
| 150 // The resource was loaded on a page that could NOT have been affected by | 166 // The resource was loaded on a page that could NOT have been affected by |
| 151 // precaching. | 167 // precaching. |
| 152 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 168 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
| 153 } | 169 } |
| 154 | 170 |
| 155 if (!IsDatabaseAccessible()) { | 171 if (!IsDatabaseAccessible()) { |
| 156 // Don't track anything if unable to access the database. | 172 // Don't track anything if unable to access the database. |
| 157 return; | 173 return; |
| 158 } | 174 } |
| 159 | 175 |
| 160 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 176 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 | 177 // If the URL for this fetch is in the write buffer, then flush the write |
| 162 // buffer. | 178 // buffer. |
| 163 Flush(); | 179 Flush(); |
| 164 } | 180 } |
| 165 | 181 |
| 166 if (was_cached && !precache_url_table_.HasURL(url)) { | 182 if (info.was_cached && !precache_url_table_.HasURL(url)) { |
| 167 // Ignore cache hits that precache can't take credit for. | 183 // Ignore cache hits that precache can't take credit for. |
| 168 return; | 184 return; |
| 169 } | 185 } |
| 170 | 186 |
| 171 base::HistogramBase::Sample size_sample = | 187 base::HistogramBase::Sample size_sample = |
| 172 static_cast<base::HistogramBase::Sample>(size); | 188 static_cast<base::HistogramBase::Sample>(size); |
| 173 if (!was_cached) { | 189 if (!info.was_cached) { |
| 174 // The fetch was served over the network during user browsing, so count it | 190 // The fetch was served over the network during user browsing, so count it |
| 175 // as downloaded non-precache bytes. | 191 // as downloaded non-precache bytes. |
| 176 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); | 192 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample); |
| 177 if (is_connection_cellular) { | 193 if (is_connection_cellular) { |
| 178 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", | 194 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", |
| 179 size_sample); | 195 size_sample); |
| 180 } | 196 } |
| 181 } else { | 197 } else { |
| 182 // The fetch was served from the cache, and since there's an entry for this | 198 // 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 | 199 // URL in the URL table, this means that the resource was served from the |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 | 293 |
| 278 void PrecacheDatabase::DeleteUnfinishedWork() { | 294 void PrecacheDatabase::DeleteUnfinishedWork() { |
| 279 precache_session_table_.DeleteUnfinishedWork(); | 295 precache_session_table_.DeleteUnfinishedWork(); |
| 280 } | 296 } |
| 281 | 297 |
| 282 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 298 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 283 return weak_factory_.GetWeakPtr(); | 299 return weak_factory_.GetWeakPtr(); |
| 284 } | 300 } |
| 285 | 301 |
| 286 } // namespace precache | 302 } // namespace precache |
| OLD | NEW |