| 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" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 if (!IsDatabaseAccessible()) { | 83 if (!IsDatabaseAccessible()) { |
| 84 // Do nothing if unable to access the database. | 84 // Do nothing if unable to access the database. |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 | 87 |
| 88 buffered_writes_.push_back(base::Bind( | 88 buffered_writes_.push_back(base::Bind( |
| 89 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); | 89 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_))); |
| 90 Flush(); | 90 Flush(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void PrecacheDatabase::SetLastPrecacheTimestamp(const base::Time& time) { |
| 94 last_precache_timestamp_ = time; |
| 95 |
| 96 if (!IsDatabaseAccessible()) { |
| 97 // Do nothing if unable to access the database. |
| 98 return; |
| 99 } |
| 100 |
| 101 buffered_writes_.push_back( |
| 102 base::Bind(&PrecacheSessionTable::SetLastPrecacheTimestamp, |
| 103 base::Unretained(&precache_session_table_), time)); |
| 104 MaybePostFlush(); |
| 105 } |
| 106 |
| 107 base::Time PrecacheDatabase::GetLastPrecacheTimestamp() { |
| 108 if (!IsDatabaseAccessible()) { |
| 109 // Use the cached value, possibly not initialized (and hence is_null()). |
| 110 return last_precache_timestamp_; |
| 111 } |
| 112 |
| 113 last_precache_timestamp_ = precache_session_table_.GetLastPrecacheTimestamp(); |
| 114 return last_precache_timestamp_; |
| 115 } |
| 116 |
| 93 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, | 117 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, |
| 94 const base::TimeDelta& latency, | 118 const base::TimeDelta& latency, |
| 95 const base::Time& fetch_time, | 119 const base::Time& fetch_time, |
| 96 int64_t size, | 120 int64_t size, |
| 97 bool was_cached) { | 121 bool was_cached) { |
| 98 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); | 122 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); |
| 99 | 123 |
| 100 if (!IsDatabaseAccessible()) { | 124 if (!IsDatabaseAccessible()) { |
| 101 // Don't track anything if unable to access the database. | 125 // Don't track anything if unable to access the database. |
| 102 return; | 126 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 | 174 // The resource was loaded on a page that could NOT have been affected by |
| 151 // precaching. | 175 // precaching. |
| 152 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 176 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
| 153 } | 177 } |
| 154 | 178 |
| 155 if (!IsDatabaseAccessible()) { | 179 if (!IsDatabaseAccessible()) { |
| 156 // Don't track anything if unable to access the database. | 180 // Don't track anything if unable to access the database. |
| 157 return; | 181 return; |
| 158 } | 182 } |
| 159 | 183 |
| 184 RecordTimeSinceLastPrecache(fetch_time); |
| 185 |
| 160 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 186 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 | 187 // If the URL for this fetch is in the write buffer, then flush the write |
| 162 // buffer. | 188 // buffer. |
| 163 Flush(); | 189 Flush(); |
| 164 } | 190 } |
| 165 | 191 |
| 166 if (was_cached && !precache_url_table_.HasURL(url)) { | 192 if (was_cached && !precache_url_table_.HasURL(url)) { |
| 167 // Ignore cache hits that precache can't take credit for. | 193 // Ignore cache hits that precache can't take credit for. |
| 168 return; | 194 return; |
| 169 } | 195 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 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)); |
| 200 buffered_urls_.insert(url.spec()); | 226 buffered_urls_.insert(url.spec()); |
| 201 MaybePostFlush(); | 227 MaybePostFlush(); |
| 202 } | 228 } |
| 203 | 229 |
| 230 void PrecacheDatabase::RecordTimeSinceLastPrecache( |
| 231 const base::Time& fetch_time) { |
| 232 if (last_precache_timestamp_.is_null()) { |
| 233 last_precache_timestamp_ = |
| 234 precache_session_table_.GetLastPrecacheTimestamp(); |
| 235 } |
| 236 // It could still be null if the DB was not accessible. |
| 237 if (!last_precache_timestamp_.is_null()) { |
| 238 // This is the timespan between the last call to |
| 239 // PrecacheManager::StartPrecaching and the fetch time of a non-precache |
| 240 // URL. Please note that the session started by that call to |
| 241 // PrecacheManager::StartPrecaching may not have precached this particular |
| 242 // URL or even any URL for that matter. |
| 243 UMA_HISTOGRAM_TIMES("Precache.TimeSinceLastPrecache", |
| 244 fetch_time - last_precache_timestamp_); |
| 245 } |
| 246 } |
| 247 |
| 204 bool PrecacheDatabase::IsDatabaseAccessible() const { | 248 bool PrecacheDatabase::IsDatabaseAccessible() const { |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); | 249 DCHECK(thread_checker_.CalledOnValidThread()); |
| 206 DCHECK(db_); | 250 DCHECK(db_); |
| 207 | 251 |
| 208 return db_->is_open(); | 252 return db_->is_open(); |
| 209 } | 253 } |
| 210 | 254 |
| 211 void PrecacheDatabase::Flush() { | 255 void PrecacheDatabase::Flush() { |
| 212 DCHECK(thread_checker_.CalledOnValidThread()); | 256 DCHECK(thread_checker_.CalledOnValidThread()); |
| 213 if (buffered_writes_.empty()) { | 257 if (buffered_writes_.empty()) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 321 |
| 278 void PrecacheDatabase::DeleteUnfinishedWork() { | 322 void PrecacheDatabase::DeleteUnfinishedWork() { |
| 279 precache_session_table_.DeleteUnfinishedWork(); | 323 precache_session_table_.DeleteUnfinishedWork(); |
| 280 } | 324 } |
| 281 | 325 |
| 282 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 326 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 283 return weak_factory_.GetWeakPtr(); | 327 return weak_factory_.GetWeakPtr(); |
| 284 } | 328 } |
| 285 | 329 |
| 286 } // namespace precache | 330 } // namespace precache |
| OLD | NEW |