Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: components/precache/core/precache_database.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Changed components_tests.gyp to depend on precache_core target instead of precache target Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/precache/core/precache_database.h"
6
7 #include "base/files/file_path.h"
8 #include "base/metrics/histogram.h"
9 #include "base/time/time.h"
10 #include "components/precache/core/precache_url_table.h"
11 #include "sql/connection.h"
12 #include "sql/transaction.h"
13 #include "url/gurl.h"
14
15 namespace {
16
17 // The number of days old that an entry in the precache URL table can be before
18 // it is considered "old" and is removed from the table.
19 const int kPrecacheHistoryExpiryPeriodDays = 60;
20
21 } // namespace
22
23 namespace precache {
24
25 PrecacheDatabase::PrecacheDatabase()
26 : precache_url_table_(new PrecacheURLTable()) {
27 // A PrecacheDatabase can be constructed on any thread.
28 thread_checker_.DetachFromThread();
29 }
30
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();
36 }
37
38 bool PrecacheDatabase::Init(const base::FilePath& db_path) {
39 DCHECK(thread_checker_.CalledOnValidThread());
40 DCHECK(!db_); // Init must only be called once.
41
42 db_.reset(new sql::Connection());
43 db_->set_histogram_tag("Precache");
44
45 if (!db_->Open(db_path)) {
46 // Don't initialize the URL table if unable to access
47 // the database.
48 return false;
49 }
50
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
54 // corrupted.
55 db_->RazeAndClose();
56 return false;
57 }
58 return true;
59 }
60
61 void PrecacheDatabase::DeleteExpiredPrecacheHistory(
62 const base::Time& current_time) {
63 if (!IsDatabaseAccessible()) {
64 // Do nothing if unable to access the database.
65 return;
66 }
67
68 // Delete old precache history that has expired.
69 precache_url_table_->DeleteAllPrecachedBefore(
70 current_time -
71 base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays));
72 }
73
74 void PrecacheDatabase::RecordURLPrecached(const GURL& url,
75 const base::Time& fetch_time,
76 int64 size, bool was_cached) {
77 if (!IsDatabaseAccessible()) {
78 // Don't track anything if unable to access the database.
79 return;
80 }
81
82 sql::Transaction transaction(db_.get());
83 if (!transaction.Begin()) {
84 // Do nothing if unable to begin a transaction.
85 return;
86 }
87
88 if (was_cached && !precache_url_table_->HasURL(url)) {
89 // Since the precache came from the cache, and there's no entry in the URL
90 // table for the URL, this means that the resource was already in the cache
91 // because of user browsing. Thus, this precache had no effect, so ignore
92 // it.
93 return;
94 }
95
96 if (!was_cached) {
97 // The precache only counts as overhead if it was downloaded over the
98 // network.
99 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", size);
100 }
101
102 // Use the URL table to keep track of URLs that are in the cache thanks to
103 // precaching. If a row for the URL already exists, than update the timestamp
104 // to |fetch_time|.
105 precache_url_table_->AddURL(url, fetch_time);
106
107 transaction.Commit();
108 }
109
110 void PrecacheDatabase::RecordURLFetched(const GURL& url,
111 const base::Time& fetch_time,
112 int64 size, bool was_cached,
113 bool is_connection_cellular) {
114 if (!IsDatabaseAccessible()) {
115 // Don't track anything if unable to access the database.
116 return;
117 }
118
119 sql::Transaction transaction(db_.get());
120 if (!transaction.Begin()) {
121 // Do nothing if unable to begin a transaction.
122 return;
123 }
124
125 if (was_cached && !precache_url_table_->HasURL(url)) {
126 // Ignore cache hits that precache can't take credit for.
127 return;
128 }
129
130 if (!was_cached) {
131 // The fetch was served over the network during user browsing, so count it
132 // as downloaded non-precache bytes.
133 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size);
134 if (is_connection_cellular) {
135 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", size);
136 }
137 } else {
138 // The fetch was served from the cache, and since there's an entry for this
139 // URL in the URL table, this means that the resource was served from the
140 // cache only because precaching put it there. Thus, precaching was helpful,
141 // so count the fetch as saved bytes.
142 UMA_HISTOGRAM_COUNTS("Precache.Saved", size);
143 if (is_connection_cellular) {
144 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size);
145 }
146 }
147
148 // Since the resource has been fetched during user browsing, remove any record
149 // of that URL having been precached from the URL table, if any exists.
150 // The current fetch would have put this resource in the cache regardless of
151 // whether or not it was previously precached, so delete any record of that
152 // URL having been precached from the URL table.
153 precache_url_table_->DeleteURL(url);
154
155 transaction.Commit();
156 }
157
158 bool PrecacheDatabase::IsDatabaseAccessible() const {
159 DCHECK(thread_checker_.CalledOnValidThread());
160 DCHECK(db_);
161
162 return db_->is_open();
163 }
164
165 } // namespace precache
OLDNEW
« no previous file with comments | « components/precache/core/precache_database.h ('k') | components/precache/core/precache_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698