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 kSecondsInHour = kSecondsInMinute * 60; |
| 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()) { |
| 112 last_precache_timestamp_ = |
| 113 precache_session_table_.GetLastPrecacheTimestamp(); |
| 114 } |
| 115 return last_precache_timestamp_; |
| 116 } |
| 117 |
93 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, | 118 void PrecacheDatabase::RecordURLPrefetch(const GURL& url, |
94 const base::TimeDelta& latency, | 119 const base::TimeDelta& latency, |
95 const base::Time& fetch_time, | 120 const base::Time& fetch_time, |
96 int64_t size, | 121 int64_t size, |
97 bool was_cached) { | 122 bool was_cached) { |
98 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); | 123 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency); |
99 | 124 |
100 if (!IsDatabaseAccessible()) { | 125 if (!IsDatabaseAccessible()) { |
101 // Don't track anything if unable to access the database. | 126 // Don't track anything if unable to access the database. |
102 return; | 127 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 | 175 // The resource was loaded on a page that could NOT have been affected by |
151 // precaching. | 176 // precaching. |
152 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); | 177 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency); |
153 } | 178 } |
154 | 179 |
155 if (!IsDatabaseAccessible()) { | 180 if (!IsDatabaseAccessible()) { |
156 // Don't track anything if unable to access the database. | 181 // Don't track anything if unable to access the database. |
157 return; | 182 return; |
158 } | 183 } |
159 | 184 |
| 185 RecordTimeSinceLastPrecache(fetch_time); |
| 186 |
160 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) { | 187 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 | 188 // If the URL for this fetch is in the write buffer, then flush the write |
162 // buffer. | 189 // buffer. |
163 Flush(); | 190 Flush(); |
164 } | 191 } |
165 | 192 |
166 if (was_cached && !precache_url_table_.HasURL(url)) { | 193 if (was_cached && !precache_url_table_.HasURL(url)) { |
167 // Ignore cache hits that precache can't take credit for. | 194 // Ignore cache hits that precache can't take credit for. |
168 return; | 195 return; |
169 } | 196 } |
(...skipping 24 matching lines...) Expand all Loading... |
194 // The current fetch would have put this resource in the cache regardless of | 221 // 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 | 222 // whether or not it was previously precached, so delete any record of that |
196 // URL having been precached from the URL table. | 223 // URL having been precached from the URL table. |
197 buffered_writes_.push_back( | 224 buffered_writes_.push_back( |
198 base::Bind(&PrecacheURLTable::DeleteURL, | 225 base::Bind(&PrecacheURLTable::DeleteURL, |
199 base::Unretained(&precache_url_table_), url)); | 226 base::Unretained(&precache_url_table_), url)); |
200 buffered_urls_.insert(url.spec()); | 227 buffered_urls_.insert(url.spec()); |
201 MaybePostFlush(); | 228 MaybePostFlush(); |
202 } | 229 } |
203 | 230 |
| 231 void PrecacheDatabase::RecordTimeSinceLastPrecache( |
| 232 const base::Time& fetch_time) { |
| 233 const base::Time& last_precache_timestamp = GetLastPrecacheTimestamp(); |
| 234 // It could still be null if the DB was not accessible. |
| 235 if (!last_precache_timestamp.is_null()) { |
| 236 // This is the timespan (in seconds) between the last call to |
| 237 // PrecacheManager::StartPrecaching and the fetch time of a non-precache |
| 238 // URL. Please note that the session started by that call to |
| 239 // PrecacheManager::StartPrecaching may not have precached this particular |
| 240 // URL or even any URL for that matter. |
| 241 // The range was estimated to have the 95 percentile within the last bounded |
| 242 // bucket. |
| 243 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 244 "Precache.TimeSinceLastPrecache", |
| 245 (fetch_time - last_precache_timestamp).InSeconds(), kSecondsInMinute, |
| 246 kSecondsInHour * 36, 100); |
| 247 } |
| 248 } |
| 249 |
204 bool PrecacheDatabase::IsDatabaseAccessible() const { | 250 bool PrecacheDatabase::IsDatabaseAccessible() const { |
205 DCHECK(thread_checker_.CalledOnValidThread()); | 251 DCHECK(thread_checker_.CalledOnValidThread()); |
206 DCHECK(db_); | 252 DCHECK(db_); |
207 | 253 |
208 return db_->is_open(); | 254 return db_->is_open(); |
209 } | 255 } |
210 | 256 |
211 void PrecacheDatabase::Flush() { | 257 void PrecacheDatabase::Flush() { |
212 DCHECK(thread_checker_.CalledOnValidThread()); | 258 DCHECK(thread_checker_.CalledOnValidThread()); |
213 if (buffered_writes_.empty()) { | 259 if (buffered_writes_.empty()) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 323 |
278 void PrecacheDatabase::DeleteUnfinishedWork() { | 324 void PrecacheDatabase::DeleteUnfinishedWork() { |
279 precache_session_table_.DeleteUnfinishedWork(); | 325 precache_session_table_.DeleteUnfinishedWork(); |
280 } | 326 } |
281 | 327 |
282 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { | 328 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
283 return weak_factory_.GetWeakPtr(); | 329 return weak_factory_.GetWeakPtr(); |
284 } | 330 } |
285 | 331 |
286 } // namespace precache | 332 } // namespace precache |
OLD | NEW |