Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / precache / core / precache_database.cc
blob2b99111962d78ee85d840fa0a95564fc0bac7ea9
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
3 // found in the LICENSE file.
5 #include "components/precache/core/precache_database.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/location.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 #include "components/history/core/browser/history_constants.h"
15 #include "sql/connection.h"
16 #include "sql/transaction.h"
17 #include "url/gurl.h"
19 namespace {
21 // The number of days old that an entry in the precache URL table can be before
22 // it is considered "old" and is removed from the table.
23 const int kPrecacheHistoryExpiryPeriodDays = 60;
25 } // namespace
27 namespace precache {
29 PrecacheDatabase::PrecacheDatabase() : is_flush_posted_(false) {
30 // A PrecacheDatabase can be constructed on any thread.
31 thread_checker_.DetachFromThread();
34 PrecacheDatabase::~PrecacheDatabase() {
35 // Since the PrecacheDatabase is refcounted, it will only be deleted if there
36 // are no references remaining to it, meaning that it is not in use. Thus, it
37 // is safe to delete it, regardless of what thread we are on.
38 thread_checker_.DetachFromThread();
41 bool PrecacheDatabase::Init(const base::FilePath& db_path) {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 DCHECK(!db_); // Init must only be called once.
45 db_.reset(new sql::Connection());
46 db_->set_histogram_tag("Precache");
48 if (!db_->Open(db_path)) {
49 // Don't initialize the URL table if unable to access
50 // the database.
51 return false;
54 if (!precache_url_table_.Init(db_.get())) {
55 // Raze and close the database connection to indicate that it's not usable,
56 // and so that the database will be created anew next time, in case it's
57 // corrupted.
58 db_->RazeAndClose();
59 return false;
61 return true;
64 void PrecacheDatabase::DeleteExpiredPrecacheHistory(
65 const base::Time& current_time) {
66 if (!IsDatabaseAccessible()) {
67 // Do nothing if unable to access the database.
68 return;
71 // Delete old precache history that has expired.
72 base::Time delete_end = current_time - base::TimeDelta::FromDays(
73 kPrecacheHistoryExpiryPeriodDays);
74 buffered_writes_.push_back(
75 base::Bind(&PrecacheURLTable::DeleteAllPrecachedBefore,
76 base::Unretained(&precache_url_table_), delete_end));
77 Flush();
80 void PrecacheDatabase::ClearHistory() {
81 if (!IsDatabaseAccessible()) {
82 // Do nothing if unable to access the database.
83 return;
86 buffered_writes_.push_back(base::Bind(
87 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_)));
88 Flush();
91 void PrecacheDatabase::RecordURLPrefetch(const GURL& url,
92 const base::TimeDelta& latency,
93 const base::Time& fetch_time,
94 int64 size,
95 bool was_cached) {
96 UMA_HISTOGRAM_TIMES("Precache.Latency.Prefetch", latency);
98 if (!IsDatabaseAccessible()) {
99 // Don't track anything if unable to access the database.
100 return;
103 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) {
104 // If the URL for this fetch is in the write buffer, then flush the write
105 // buffer.
106 Flush();
109 if (was_cached && !precache_url_table_.HasURL(url)) {
110 // Since the precache came from the cache, and there's no entry in the URL
111 // table for the URL, this means that the resource was already in the cache
112 // because of user browsing. Thus, this precache had no effect, so ignore
113 // it.
114 return;
117 if (!was_cached) {
118 // The precache only counts as overhead if it was downloaded over the
119 // network.
120 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated",
121 static_cast<base::HistogramBase::Sample>(size));
124 // Use the URL table to keep track of URLs that are in the cache thanks to
125 // precaching. If a row for the URL already exists, than update the timestamp
126 // to |fetch_time|.
127 buffered_writes_.push_back(
128 base::Bind(&PrecacheURLTable::AddURL,
129 base::Unretained(&precache_url_table_), url, fetch_time));
130 buffered_urls_.insert(url.spec());
131 MaybePostFlush();
134 void PrecacheDatabase::RecordURLNonPrefetch(const GURL& url,
135 const base::TimeDelta& latency,
136 const base::Time& fetch_time,
137 int64 size,
138 bool was_cached,
139 int host_rank,
140 bool is_connection_cellular) {
141 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch", latency);
143 if (host_rank != history::kMaxTopHosts) {
144 // The resource was loaded on a page that could have been affected by
145 // precaching.
146 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.TopHosts", latency);
147 } else {
148 // The resource was loaded on a page that could NOT have been affected by
149 // precaching.
150 UMA_HISTOGRAM_TIMES("Precache.Latency.NonPrefetch.NonTopHosts", latency);
153 if (!IsDatabaseAccessible()) {
154 // Don't track anything if unable to access the database.
155 return;
158 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) {
159 // If the URL for this fetch is in the write buffer, then flush the write
160 // buffer.
161 Flush();
164 if (was_cached && !precache_url_table_.HasURL(url)) {
165 // Ignore cache hits that precache can't take credit for.
166 return;
169 base::HistogramBase::Sample size_sample =
170 static_cast<base::HistogramBase::Sample>(size);
171 if (!was_cached) {
172 // The fetch was served over the network during user browsing, so count it
173 // as downloaded non-precache bytes.
174 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample);
175 if (is_connection_cellular) {
176 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular",
177 size_sample);
179 } else {
180 // The fetch was served from the cache, and since there's an entry for this
181 // URL in the URL table, this means that the resource was served from the
182 // cache only because precaching put it there. Thus, precaching was helpful,
183 // so count the fetch as saved bytes.
184 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample);
185 if (is_connection_cellular) {
186 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample);
190 // Since the resource has been fetched during user browsing, remove any record
191 // of that URL having been precached from the URL table, if any exists.
192 // The current fetch would have put this resource in the cache regardless of
193 // whether or not it was previously precached, so delete any record of that
194 // URL having been precached from the URL table.
195 buffered_writes_.push_back(
196 base::Bind(&PrecacheURLTable::DeleteURL,
197 base::Unretained(&precache_url_table_), url));
198 buffered_urls_.insert(url.spec());
199 MaybePostFlush();
202 bool PrecacheDatabase::IsDatabaseAccessible() const {
203 DCHECK(thread_checker_.CalledOnValidThread());
204 DCHECK(db_);
206 return db_->is_open();
209 void PrecacheDatabase::Flush() {
210 DCHECK(thread_checker_.CalledOnValidThread());
211 if (buffered_writes_.empty()) {
212 // Do nothing if there's nothing to flush.
213 DCHECK(buffered_urls_.empty());
214 return;
217 if (IsDatabaseAccessible()) {
218 sql::Transaction transaction(db_.get());
219 if (transaction.Begin()) {
220 for (std::vector<base::Closure>::const_iterator it =
221 buffered_writes_.begin();
222 it != buffered_writes_.end(); ++it) {
223 it->Run();
226 transaction.Commit();
230 // Clear the buffer, even if the database is inaccessible or unable to begin a
231 // transaction.
232 buffered_writes_.clear();
233 buffered_urls_.clear();
236 void PrecacheDatabase::PostedFlush() {
237 DCHECK(thread_checker_.CalledOnValidThread());
238 DCHECK(is_flush_posted_);
239 is_flush_posted_ = false;
240 Flush();
243 void PrecacheDatabase::MaybePostFlush() {
244 DCHECK(thread_checker_.CalledOnValidThread());
246 if (buffered_writes_.empty() || is_flush_posted_) {
247 // There's no point in posting a flush if there's nothing to be flushed or
248 // if a flush has already been posted.
249 return;
252 // Post a delayed task to flush the buffer in 1 second, so that multiple
253 // database writes can be buffered up and flushed together in the same
254 // transaction.
255 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
256 FROM_HERE, base::Bind(&PrecacheDatabase::PostedFlush,
257 scoped_refptr<PrecacheDatabase>(this)),
258 base::TimeDelta::FromSeconds(1));
259 is_flush_posted_ = true;
262 } // namespace precache