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/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h"
11 #include "base/time/time.h"
12 #include "sql/connection.h"
13 #include "sql/transaction.h"
18 // The number of days old that an entry in the precache URL table can be before
19 // it is considered "old" and is removed from the table.
20 const int kPrecacheHistoryExpiryPeriodDays
= 60;
26 PrecacheDatabase::PrecacheDatabase() : is_flush_posted_(false) {
27 // A PrecacheDatabase can be constructed on any thread.
28 thread_checker_
.DetachFromThread();
31 PrecacheDatabase::~PrecacheDatabase() {
32 // Since the PrecacheDatabase is refcounted, it will only be deleted if there
33 // are no references remaining to it, meaning that it is not in use. Thus, it
34 // is safe to delete it, regardless of what thread we are on.
35 thread_checker_
.DetachFromThread();
38 bool PrecacheDatabase::Init(const base::FilePath
& db_path
) {
39 DCHECK(thread_checker_
.CalledOnValidThread());
40 DCHECK(!db_
); // Init must only be called once.
42 db_
.reset(new sql::Connection());
43 db_
->set_histogram_tag("Precache");
45 if (!db_
->Open(db_path
)) {
46 // Don't initialize the URL table if unable to access
51 if (!precache_url_table_
.Init(db_
.get())) {
52 // Raze and close the database connection to indicate that it's not usable,
53 // and so that the database will be created anew next time, in case it's
61 void PrecacheDatabase::DeleteExpiredPrecacheHistory(
62 const base::Time
& current_time
) {
63 if (!IsDatabaseAccessible()) {
64 // Do nothing if unable to access the database.
68 // Delete old precache history that has expired.
69 base::Time delete_end
= current_time
- base::TimeDelta::FromDays(
70 kPrecacheHistoryExpiryPeriodDays
);
71 buffered_writes_
.push_back(
72 base::Bind(&PrecacheURLTable::DeleteAllPrecachedBefore
,
73 base::Unretained(&precache_url_table_
), delete_end
));
78 void PrecacheDatabase::RecordURLPrecached(const GURL
& url
,
79 const base::Time
& fetch_time
,
80 int64 size
, bool was_cached
) {
81 if (!IsDatabaseAccessible()) {
82 // Don't track anything if unable to access the database.
86 if (buffered_urls_
.find(url
.spec()) != buffered_urls_
.end()) {
87 // If the URL for this fetch is in the write buffer, then flush the write
92 if (was_cached
&& !precache_url_table_
.HasURL(url
)) {
93 // Since the precache came from the cache, and there's no entry in the URL
94 // table for the URL, this means that the resource was already in the cache
95 // because of user browsing. Thus, this precache had no effect, so ignore
101 // The precache only counts as overhead if it was downloaded over the
103 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated",
104 static_cast<base::HistogramBase::Sample
>(size
));
107 // Use the URL table to keep track of URLs that are in the cache thanks to
108 // precaching. If a row for the URL already exists, than update the timestamp
110 buffered_writes_
.push_back(
111 base::Bind(&PrecacheURLTable::AddURL
,
112 base::Unretained(&precache_url_table_
), url
, fetch_time
));
113 buffered_urls_
.insert(url
.spec());
117 void PrecacheDatabase::RecordURLFetched(const GURL
& url
,
118 const base::Time
& fetch_time
,
119 int64 size
, bool was_cached
,
120 bool is_connection_cellular
) {
121 if (!IsDatabaseAccessible()) {
122 // Don't track anything if unable to access the database.
126 if (buffered_urls_
.find(url
.spec()) != buffered_urls_
.end()) {
127 // If the URL for this fetch is in the write buffer, then flush the write
132 if (was_cached
&& !precache_url_table_
.HasURL(url
)) {
133 // Ignore cache hits that precache can't take credit for.
137 base::HistogramBase::Sample size_sample
=
138 static_cast<base::HistogramBase::Sample
>(size
);
140 // The fetch was served over the network during user browsing, so count it
141 // as downloaded non-precache bytes.
142 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample
);
143 if (is_connection_cellular
) {
144 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular",
148 // The fetch was served from the cache, and since there's an entry for this
149 // URL in the URL table, this means that the resource was served from the
150 // cache only because precaching put it there. Thus, precaching was helpful,
151 // so count the fetch as saved bytes.
152 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample
);
153 if (is_connection_cellular
) {
154 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample
);
158 // Since the resource has been fetched during user browsing, remove any record
159 // of that URL having been precached from the URL table, if any exists.
160 // The current fetch would have put this resource in the cache regardless of
161 // whether or not it was previously precached, so delete any record of that
162 // URL having been precached from the URL table.
163 buffered_writes_
.push_back(
164 base::Bind(&PrecacheURLTable::DeleteURL
,
165 base::Unretained(&precache_url_table_
), url
));
166 buffered_urls_
.insert(url
.spec());
170 bool PrecacheDatabase::IsDatabaseAccessible() const {
171 DCHECK(thread_checker_
.CalledOnValidThread());
174 return db_
->is_open();
177 void PrecacheDatabase::Flush() {
178 DCHECK(thread_checker_
.CalledOnValidThread());
179 if (buffered_writes_
.empty()) {
180 // Do nothing if there's nothing to flush.
181 DCHECK(buffered_urls_
.empty());
185 if (IsDatabaseAccessible()) {
186 sql::Transaction
transaction(db_
.get());
187 if (transaction
.Begin()) {
188 for (std::vector
<base::Closure
>::const_iterator it
=
189 buffered_writes_
.begin();
190 it
!= buffered_writes_
.end(); ++it
) {
194 transaction
.Commit();
198 // Clear the buffer, even if the database is inaccessible or unable to begin a
200 buffered_writes_
.clear();
201 buffered_urls_
.clear();
204 void PrecacheDatabase::PostedFlush() {
205 DCHECK(thread_checker_
.CalledOnValidThread());
206 DCHECK(is_flush_posted_
);
207 is_flush_posted_
= false;
211 void PrecacheDatabase::MaybePostFlush() {
212 DCHECK(thread_checker_
.CalledOnValidThread());
214 if (buffered_writes_
.empty() || is_flush_posted_
) {
215 // There's no point in posting a flush if there's nothing to be flushed or
216 // if a flush has already been posted.
220 DCHECK(base::MessageLoop::current());
221 // Post a delayed task to flush the buffer in 1 second, so that multiple
222 // database writes can be buffered up and flushed together in the same
224 base::MessageLoop::current()->PostDelayedTask(
225 FROM_HERE
, base::Bind(&PrecacheDatabase::PostedFlush
,
226 scoped_refptr
<PrecacheDatabase
>(this)),
227 base::TimeDelta::FromSeconds(1));
228 is_flush_posted_
= true;
231 } // namespace precache