Chromium Code Reviews| Index: components/precache/core/precache_database.cc |
| diff --git a/components/precache/core/precache_database.cc b/components/precache/core/precache_database.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cc35a4d59fc2d2d737191ac02d81630902467ff0 |
| --- /dev/null |
| +++ b/components/precache/core/precache_database.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/precache/core/precache_database.h" |
| + |
| +#include "base/metrics/histogram.h" |
| +#include "base/time/time.h" |
| +#include "components/precache/core/precache_statistics_table.h" |
| +#include "components/precache/core/precache_url_table.h" |
| +#include "sql/connection.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +// The number of days old that an entry in the precache URL table can be before |
| +// it is considered "old" and is removed from the table. |
| +const int64 kPrecacheHistoryExpiryPeriodDays = 60; |
| + |
| +void RecordSingleDayPrecacheUMA( |
| + const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) { |
| + UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedPrecacheMotivatedKB", |
| + stats.downloaded_precache_motivated_bytes / 1024); |
| + UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB", |
| + stats.downloaded_non_precache_bytes / 1024); |
| + UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB.Cellular", |
| + stats.downloaded_non_precache_bytes_cellular / 1024); |
| + UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB", stats.saved_bytes / 1024); |
| + UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB.Cellular", |
| + stats.saved_bytes_cellular / 1024); |
| + |
| + if (stats.saved_bytes + stats.downloaded_non_precache_bytes > 0) { |
| + UMA_HISTOGRAM_PERCENTAGE( |
| + "Precache.DailySavingsPercentage", |
| + stats.saved_bytes * 100.0 / |
| + (stats.saved_bytes + stats.downloaded_non_precache_bytes)); |
| + } |
| + |
| + if (stats.saved_bytes_cellular + |
| + stats.downloaded_non_precache_bytes_cellular > 0) { |
| + UMA_HISTOGRAM_PERCENTAGE( |
| + "Precache.DailySavingsPercentage.Cellular", |
| + stats.saved_bytes_cellular * 100.0 / |
| + (stats.saved_bytes_cellular + |
| + stats.downloaded_non_precache_bytes_cellular)); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +namespace precache { |
| + |
| +PrecacheDatabase::PrecacheDatabase() |
| + : precache_url_table_(new PrecacheURLTable()), |
| + precache_statistics_table_(new PrecacheStatisticsTable()) { |
| + // A PrecacheDatabase can be constructed on any thread. |
| + DetachFromThread(); |
| +} |
| + |
| +PrecacheDatabase::~PrecacheDatabase() { |
| + // Since the PrecacheDatabase is refcounted, it will only be deleted if there |
| + // are no references remaining to it, meaning that it is not in use. Thus, it |
| + // is safe to delete it, regardless of what thread we are on. |
| + DetachFromThread(); |
| +} |
| + |
| +void PrecacheDatabase::Init(scoped_ptr<sql::Connection> db) { |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
I'm not seeing where this database is coming from
sclittle
2013/11/19 23:27:10
There will be a PrecacheManager BrowserContextKeye
Scott Hess - ex-Googler
2013/11/27 01:32:33
But why does the caller create the database, as op
sclittle
2013/12/02 21:12:52
Good point. Changed this to take in a path instead
|
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!db_); // Init must only be called once. |
| + DCHECK(db); // |db| must not be NULL. |
| + |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
sql::Connection has a set_histogram_tag() which in
sclittle
2013/11/19 23:27:10
Done.
|
| + db_ = db.Pass(); |
| + if (!IsDatabaseAccessible()) { |
| + // Don't initialize the URL table or statistics table if unable to access |
| + // the database. |
| + return; |
| + } |
| + |
| + precache_url_table_->Init(db_.get()); |
| + precache_statistics_table_->Init(db_.get()); |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These should probably be in a transaction.
sclittle
2013/11/19 23:27:10
Done.
|
| +} |
| + |
| +void PrecacheDatabase::ReportAndDeleteOldStats(const base::Time& current_time) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(db_); |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
|
| + |
| + if (!IsDatabaseAccessible()) { |
| + // Do nothing if unable to access the database. |
| + return; |
| + } |
| + |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
You should probably start a transaction here to en
sclittle
2013/11/19 23:27:10
Done.
|
| + // Delete old precache history that has expired. |
| + precache_url_table_->DeleteAllPrecachedBefore( |
| + current_time - |
| + base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); |
| + |
| + PrecacheStatisticsTable::PrecacheStatisticsMap stats_map; |
| + precache_statistics_table_->GetOldStats(current_time, &stats_map); |
| + |
| + // Report UMA for every row of old statistics in the statistics table. There |
| + // won't be any rows in the statistics table for days when nothing was fetched |
| + // or precached. |
| + for (PrecacheStatisticsTable::PrecacheStatisticsMap::const_iterator it = |
| + stats_map.begin(); |
| + it != stats_map.end(); ++it) { |
| + RecordSingleDayPrecacheUMA(it->second); |
| + } |
| + |
| + precache_statistics_table_->DeleteOldStats(current_time); |
| +} |
| + |
| +void PrecacheDatabase::RecordURLPrecached(const GURL& url, |
| + const base::Time& fetch_time, |
| + int64 size, bool was_cached) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(db_); |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
|
| + |
| + if (!IsDatabaseAccessible()) { |
| + // Don't track anything if unable to access the database. |
| + return; |
| + } |
| + |
| + if (was_cached && !precache_url_table_->HasURL(url)) { |
| + // Since the precache came from the cache, and there's no entry in the URL |
| + // table for the URL, this means that the resource was already in the cache |
| + // because of user browsing. Thus, this precache had no effect, so ignore |
| + // it. |
| + return; |
| + } |
| + |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Probably should start a transaction here.
sclittle
2013/11/19 23:27:10
Done.
|
| + if (!was_cached) { |
| + // The precache only counts as overhead if it was downloaded over the |
| + // network. |
| + PrecacheStatisticsTable::PrecacheStatistics stats; |
| + stats.downloaded_precache_motivated_bytes = size; |
| + precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); |
| + } |
| + |
| + // Use the URL table to keep track of URLs that are in the cache thanks to |
| + // precaching. If a row for the URL already exists, than update the timestamp |
| + // to |fetch_time|. |
| + precache_url_table_->AddURL(url, fetch_time); |
| +} |
| + |
| +void PrecacheDatabase::RecordURLFetched(const GURL& url, |
| + const base::Time& fetch_time, |
| + int64 size, bool was_cached, |
| + bool is_connection_cellular) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(db_); |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
|
| + |
| + if (!IsDatabaseAccessible()) { |
| + // Don't track anything if unable to access the database. |
| + return; |
| + } |
| + |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Probably should open a transaction here.
sclittle
2013/11/19 23:27:10
Done.
|
| + if (was_cached && !precache_url_table_->HasURL(url)) { |
| + // Ignore cache hits that precache can't take credit for. |
| + return; |
| + } |
| + |
| + PrecacheStatisticsTable::PrecacheStatistics stats; |
| + |
| + if (!was_cached) { |
| + // The fetch was served over the network during user browsing, so count it |
| + // as downloaded non-precache bytes. |
| + stats.downloaded_non_precache_bytes = size; |
| + if (is_connection_cellular) { |
| + stats.downloaded_non_precache_bytes_cellular = size; |
| + } |
| + } else { |
| + // The fetch was served from the cache, and since there's an entry for this |
| + // URL in the URL table, this means that the resource was served from the |
| + // cache only because precaching put it there. Thus, precaching was helpful, |
| + // so count the fetch as saved bytes. |
| + stats.saved_bytes = size; |
| + if (is_connection_cellular) { |
| + stats.saved_bytes_cellular = size; |
| + } |
| + } |
| + precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); |
| + |
| + // Since the resource has been fetched during user browsing, remove any record |
| + // of that URL having been precached from the URL table, if any exists. |
| + // The current fetch would have put this resource in the cache regardless of |
| + // whether or not it was previously precached, so delete any record of that |
| + // URL having been precached from the URL table. |
| + precache_url_table_->DeleteURL(url); |
| +} |
| + |
| +bool PrecacheDatabase::IsDatabaseAccessible() const { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(db_); |
| + |
| + return db_->is_open(); |
| +} |
| + |
| +} // namespace precache |