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"
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 "sql/connection.h"
15 #include "sql/transaction.h"
20 // The number of days old that an entry in the precache URL table can be before
21 // it is considered "old" and is removed from the table.
22 const int kPrecacheHistoryExpiryPeriodDays
= 60;
28 PrecacheDatabase::PrecacheDatabase() : is_flush_posted_(false) {
29 // A PrecacheDatabase can be constructed on any thread.
30 thread_checker_
.DetachFromThread();
33 PrecacheDatabase::~PrecacheDatabase() {
34 // Since the PrecacheDatabase is refcounted, it will only be deleted if there
35 // are no references remaining to it, meaning that it is not in use. Thus, it
36 // is safe to delete it, regardless of what thread we are on.
37 thread_checker_
.DetachFromThread();
40 bool PrecacheDatabase::Init(const base::FilePath
& db_path
) {
41 DCHECK(thread_checker_
.CalledOnValidThread());
42 DCHECK(!db_
); // Init must only be called once.
44 db_
.reset(new sql::Connection());
45 db_
->set_histogram_tag("Precache");
47 if (!db_
->Open(db_path
)) {
48 // Don't initialize the URL table if unable to access
53 if (!precache_url_table_
.Init(db_
.get())) {
54 // Raze and close the database connection to indicate that it's not usable,
55 // and so that the database will be created anew next time, in case it's
63 void PrecacheDatabase::DeleteExpiredPrecacheHistory(
64 const base::Time
& current_time
) {
65 if (!IsDatabaseAccessible()) {
66 // Do nothing if unable to access the database.
70 // Delete old precache history that has expired.
71 base::Time delete_end
= current_time
- base::TimeDelta::FromDays(
72 kPrecacheHistoryExpiryPeriodDays
);
73 buffered_writes_
.push_back(
74 base::Bind(&PrecacheURLTable::DeleteAllPrecachedBefore
,
75 base::Unretained(&precache_url_table_
), delete_end
));
80 void PrecacheDatabase::RecordURLPrecached(const GURL
& url
,
81 const base::Time
& fetch_time
,
82 int64 size
, bool was_cached
) {
83 if (!IsDatabaseAccessible()) {
84 // Don't track anything if unable to access the database.
88 if (buffered_urls_
.find(url
.spec()) != buffered_urls_
.end()) {
89 // If the URL for this fetch is in the write buffer, then flush the write
94 if (was_cached
&& !precache_url_table_
.HasURL(url
)) {
95 // Since the precache came from the cache, and there's no entry in the URL
96 // table for the URL, this means that the resource was already in the cache
97 // because of user browsing. Thus, this precache had no effect, so ignore
103 // The precache only counts as overhead if it was downloaded over the
105 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated",
106 static_cast<base::HistogramBase::Sample
>(size
));
109 // Use the URL table to keep track of URLs that are in the cache thanks to
110 // precaching. If a row for the URL already exists, than update the timestamp
112 buffered_writes_
.push_back(
113 base::Bind(&PrecacheURLTable::AddURL
,
114 base::Unretained(&precache_url_table_
), url
, fetch_time
));
115 buffered_urls_
.insert(url
.spec());
119 void PrecacheDatabase::RecordURLFetched(const GURL
& url
,
120 const base::Time
& fetch_time
,
121 int64 size
, bool was_cached
,
122 bool is_connection_cellular
) {
123 if (!IsDatabaseAccessible()) {
124 // Don't track anything if unable to access the database.
128 if (buffered_urls_
.find(url
.spec()) != buffered_urls_
.end()) {
129 // If the URL for this fetch is in the write buffer, then flush the write
134 if (was_cached
&& !precache_url_table_
.HasURL(url
)) {
135 // Ignore cache hits that precache can't take credit for.
139 base::HistogramBase::Sample size_sample
=
140 static_cast<base::HistogramBase::Sample
>(size
);
142 // The fetch was served over the network during user browsing, so count it
143 // as downloaded non-precache bytes.
144 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample
);
145 if (is_connection_cellular
) {
146 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular",
150 // The fetch was served from the cache, and since there's an entry for this
151 // URL in the URL table, this means that the resource was served from the
152 // cache only because precaching put it there. Thus, precaching was helpful,
153 // so count the fetch as saved bytes.
154 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample
);
155 if (is_connection_cellular
) {
156 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample
);
160 // Since the resource has been fetched during user browsing, remove any record
161 // of that URL having been precached from the URL table, if any exists.
162 // The current fetch would have put this resource in the cache regardless of
163 // whether or not it was previously precached, so delete any record of that
164 // URL having been precached from the URL table.
165 buffered_writes_
.push_back(
166 base::Bind(&PrecacheURLTable::DeleteURL
,
167 base::Unretained(&precache_url_table_
), url
));
168 buffered_urls_
.insert(url
.spec());
172 bool PrecacheDatabase::IsDatabaseAccessible() const {
173 DCHECK(thread_checker_
.CalledOnValidThread());
176 return db_
->is_open();
179 void PrecacheDatabase::Flush() {
180 DCHECK(thread_checker_
.CalledOnValidThread());
181 if (buffered_writes_
.empty()) {
182 // Do nothing if there's nothing to flush.
183 DCHECK(buffered_urls_
.empty());
187 if (IsDatabaseAccessible()) {
188 sql::Transaction
transaction(db_
.get());
189 if (transaction
.Begin()) {
190 for (std::vector
<base::Closure
>::const_iterator it
=
191 buffered_writes_
.begin();
192 it
!= buffered_writes_
.end(); ++it
) {
196 transaction
.Commit();
200 // Clear the buffer, even if the database is inaccessible or unable to begin a
202 buffered_writes_
.clear();
203 buffered_urls_
.clear();
206 void PrecacheDatabase::PostedFlush() {
207 DCHECK(thread_checker_
.CalledOnValidThread());
208 DCHECK(is_flush_posted_
);
209 is_flush_posted_
= false;
213 void PrecacheDatabase::MaybePostFlush() {
214 DCHECK(thread_checker_
.CalledOnValidThread());
216 if (buffered_writes_
.empty() || is_flush_posted_
) {
217 // There's no point in posting a flush if there's nothing to be flushed or
218 // if a flush has already been posted.
222 // Post a delayed task to flush the buffer in 1 second, so that multiple
223 // database writes can be buffered up and flushed together in the same
225 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
226 FROM_HERE
, base::Bind(&PrecacheDatabase::PostedFlush
,
227 scoped_refptr
<PrecacheDatabase
>(this)),
228 base::TimeDelta::FromSeconds(1));
229 is_flush_posted_
= true;
232 } // namespace precache