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 "sql/connection.h" | 16 #include "sql/connection.h" |
| 17 #include "sql/transaction.h" | 17 #include "sql/transaction.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // The number of days old that an entry in the precache URL table can be before | 22 // 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. | 23 // it is considered "old" and is removed from the table. |
| 24 const int kPrecacheHistoryExpiryPeriodDays = 60; | 24 const int kPrecacheHistoryExpiryPeriodDays = 60; |
| 25 | 25 |
| 26 const int kSecondsInMinute = 60; | |
| 27 const int kSecondsInDay = kSecondsInMinute * 60 * 24; | |
| 28 | |
| 26 } // namespace | 29 } // namespace |
| 27 | 30 |
| 28 namespace precache { | 31 namespace precache { |
| 29 | 32 |
| 30 PrecacheDatabase::PrecacheDatabase() | 33 PrecacheDatabase::PrecacheDatabase() |
| 31 : is_flush_posted_(false), weak_factory_(this) { | 34 : is_flush_posted_(false), weak_factory_(this) { |
| 32 // A PrecacheDatabase can be constructed on any thread. | 35 // A PrecacheDatabase can be constructed on any thread. |
| 33 thread_checker_.DetachFromThread(); | 36 thread_checker_.DetachFromThread(); |
| 34 } | 37 } |
| 35 | 38 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 if (!IsDatabaseAccessible()) { | 86 if (!IsDatabaseAccessible()) { |
| 84 // Do nothing if unable to access the database. | 87 // Do nothing if unable to access the database. |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 87 | 90 |
| 88 buffered_writes_.push_back(base::Bind( | 91 buffered_writes_.push_back(base::Bind( |
| 89 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); | 92 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); |
| 90 Flush(); | 93 Flush(); |
| 91 } | 94 } |
| 92 | 95 |
| 96 void PrecacheDatabase::SetLastPrecacheTimestamp(const base::Time& time) { | |
| 97 last_precache_timestamp_ = time; | |
| 98 | |
| 99 if (!IsDatabaseAccessible()) { | |
| 100 // Do nothing if unable to access the database. | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 buffered_writes_.push_back( | |
| 105 base::Bind(&PrecacheSessionTable::SetLastPrecacheTimestamp, | |
| 106 base::Unretained(&precache_session_table_), time)); | |
| 107 MaybePostFlush(); | |
| 108 } | |
| 109 | |
| 110 base::Time PrecacheDatabase::GetLastPrecacheTimestamp() { | |
| 111 if (!last_precache_timestamp_.is_null() || !IsDatabaseAccessible()) { | |
|
twifkak
2016/07/08 23:47:53
Nit: Can get rid of the redundant return statement
jamartin
2016/07/12 14:18:44
Good point.
| |
| 112 // Use the cached value, possibly not initialized (and hence is_null()). | |
| 113 return last_precache_timestamp_; | |
| 114 } | |
| 115 | |
| 116 last_precache_timestamp_ = precache_session_table_.GetLastPrecacheTimestamp(); | |
| 117 return last_precache_timestamp_; | |
| 118 } | |
| 119 | |
| 93 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, | 120 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, |
| 94 const base::TimeDelta& latency, | 121 const base::TimeDelta& latency, |
| 95 const base::Time& fetch_time, | 122 const base::Time& fetch_time, |
| 96 int64_t size, | 123 int64_t size, |
| 97 bool was_cached) { | 124 bool was_cached) { |
| 98 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); | 125 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); |
| 99 | 126 |
| 100 if (!IsDatabaseAccessible()) { | 127 if (!IsDatabaseAccessible()) { |
| 101 // Don't track anything if unable to access the database. | 128 // Don't track anything if unable to access the database. |
| 102 return; | 129 return; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 // The resource was loaded on a page that could NOT have been affected by | 177 // The resource was loaded on a page that could NOT have been affected by |
| 151 // precaching. | 178 // precaching. |
| 152 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 179 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
| 153 } | 180 } |
| 154 | 181 |
| 155 if (!IsDatabaseAccessible()) { | 182 if (!IsDatabaseAccessible()) { |
| 156 // Don't track anything if unable to access the database. | 183 // Don't track anything if unable to access the database. |
| 157 return; | 184 return; |
| 158 } | 185 } |
| 159 | 186 |
| 187 RecordTimeSinceLastPrecache(fetch_time); | |
| 188 | |
| 160 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 189 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 | 190 // If the URL for this fetch is in the write buffer, then flush the write |
| 162 // buffer. | 191 // buffer. |
| 163 Flush(); | 192 Flush(); |
| 164 } | 193 } |
| 165 | 194 |
| 166 if (was_cached && !precache_url_table_.HasURL(url)) { | 195 if (was_cached && !precache_url_table_.HasURL(url)) { |
| 167 // Ignore cache hits that precache can't take credit for. | 196 // Ignore cache hits that precache can't take credit for. |
| 168 return; | 197 return; |
| 169 } | 198 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 194 // The current fetch would have put this resource in the cache regardless of | 223 // 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 | 224 // whether or not it was previously precached, so delete any record of that |
| 196 // URL having been precached from the URL table. | 225 // URL having been precached from the URL table. |
| 197 buffered_writes_.push_back( | 226 buffered_writes_.push_back( |
| 198 base::Bind(&PrecacheURLTable::DeleteURL, | 227 base::Bind(&PrecacheURLTable::DeleteURL, |
| 199 base::Unretained(&precache_url_table_), url)); | 228 base::Unretained(&precache_url_table_), url)); |
| 200 buffered_urls_.insert(url.spec()); | 229 buffered_urls_.insert(url.spec()); |
| 201 MaybePostFlush(); | 230 MaybePostFlush(); |
| 202 } | 231 } |
| 203 | 232 |
| 233 void PrecacheDatabase::RecordTimeSinceLastPrecache( | |
| 234 const base::Time& fetch_time) { | |
| 235 if (last_precache_timestamp_.is_null()) { | |
|
twifkak
2016/07/08 23:47:53
Why don't you just call [this->]GetLastPrecacheTim
jamartin
2016/07/12 14:18:44
Good point.
This grew organically. This GetLastPr
| |
| 236 last_precache_timestamp_ = | |
| 237 precache_session_table_.GetLastPrecacheTimestamp(); | |
| 238 } | |
| 239 // It could still be null if the DB was not accessible. | |
| 240 if (!last_precache_timestamp_.is_null()) { | |
| 241 // This is the timespan (in seconds) between the last call to | |
| 242 // PrecacheManager::StartPrecaching and the fetch time of a non-precache | |
| 243 // URL. Please note that the session started by that call to | |
| 244 // PrecacheManager::StartPrecaching may not have precached this particular | |
| 245 // URL or even any URL for that matter. | |
| 246 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 247 "Precache.TimeSinceLastPrecache", | |
| 248 (fetch_time - last_precache_timestamp_).InSeconds(), | |
| 249 kSecondsInMinute, kSecondsInDay, 100); | |
|
rkaplow
2016/07/08 14:37:59
not really obvious to me you have a min of 60 - wh
rkaplow
2016/07/08 14:37:59
picking the max as a day seems fine though (respon
jamartin
2016/07/08 14:52:49
Thanks. Ideally I'd like more than a day, probably
jamartin
2016/07/08 14:52:49
0 is not a valid min as per Histogram::FactoryGet.
twifkak
2016/07/08 23:47:53
IIRC, the advice I was given a while back was to m
jamartin
2016/07/12 14:18:44
Done (max = 36h).
BTW, what do you think of the m
twifkak
2016/07/16 00:47:49
Oh yeah, good question.
Short: I think it's fine.
| |
| 250 } | |
| 251 } | |
| 252 | |
| 204 bool PrecacheDatabase::IsDatabaseAccessible() const { | 253 bool PrecacheDatabase::IsDatabaseAccessible() const { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); | 254 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 DCHECK(db_); | 255 DCHECK(db_); |
| 207 | 256 |
| 208 return db_->is_open(); | 257 return db_->is_open(); |
| 209 } | 258 } |
| 210 | 259 |
| 211 void PrecacheDatabase::Flush() { | 260 void PrecacheDatabase::Flush() { |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); | 261 DCHECK(thread_checker_.CalledOnValidThread()); |
| 213 if (buffered_writes_.empty()) { | 262 if (buffered_writes_.empty()) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 | 326 |
| 278 void PrecacheDatabase::DeleteUnfinishedWork() { | 327 void PrecacheDatabase::DeleteUnfinishedWork() { |
| 279 precache_session_table_.DeleteUnfinishedWork(); | 328 precache_session_table_.DeleteUnfinishedWork(); |
| 280 } | 329 } |
| 281 | 330 |
| 282 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 331 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 283 return weak_factory_.GetWeakPtr(); | 332 return weak_factory_.GetWeakPtr(); |
| 284 } | 333 } |
| 285 | 334 |
| 286 } // namespace precache | 335 } // namespace precache |
| OLD | NEW |