Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / precache / core / precache_database.cc
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/bind.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"
14 #include "url/gurl.h"
15
16 namespace {
17
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;
21
22 }  // namespace
23
24 namespace precache {
25
26 PrecacheDatabase::PrecacheDatabase() : is_flush_posted_(false) {
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   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));
74
75   Flush();
76 }
77
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.
83     return;
84   }
85
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
88     // buffer.
89     Flush();
90   }
91
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
96     // it.
97     return;
98   }
99
100   if (!was_cached) {
101     // The precache only counts as overhead if it was downloaded over the
102     // network.
103     UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated",
104                          static_cast<base::HistogramBase::Sample>(size));
105   }
106
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
109   // to |fetch_time|.
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());
114   MaybePostFlush();
115 }
116
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.
123     return;
124   }
125
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
128     // buffer.
129     Flush();
130   }
131
132   if (was_cached && !precache_url_table_.HasURL(url)) {
133     // Ignore cache hits that precache can't take credit for.
134     return;
135   }
136
137   base::HistogramBase::Sample size_sample =
138       static_cast<base::HistogramBase::Sample>(size);
139   if (!was_cached) {
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",
145                            size_sample);
146     }
147   } else {
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);
155     }
156   }
157
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());
167   MaybePostFlush();
168 }
169
170 bool PrecacheDatabase::IsDatabaseAccessible() const {
171   DCHECK(thread_checker_.CalledOnValidThread());
172   DCHECK(db_);
173
174   return db_->is_open();
175 }
176
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());
182     return;
183   }
184
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) {
191         it->Run();
192       }
193
194       transaction.Commit();
195     }
196   }
197
198   // Clear the buffer, even if the database is inaccessible or unable to begin a
199   // transaction.
200   buffered_writes_.clear();
201   buffered_urls_.clear();
202 }
203
204 void PrecacheDatabase::PostedFlush() {
205   DCHECK(thread_checker_.CalledOnValidThread());
206   DCHECK(is_flush_posted_);
207   is_flush_posted_ = false;
208   Flush();
209 }
210
211 void PrecacheDatabase::MaybePostFlush() {
212   DCHECK(thread_checker_.CalledOnValidThread());
213
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.
217     return;
218   }
219
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
223   // transaction.
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;
229 }
230
231 }  // namespace precache