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

Unified 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: Moved MostVisitedURLsProvider into chrome/browser/precache Created 7 years, 2 months 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 side-by-side diff with in-line comments
Download patch
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..61b61cc818bd398177f5c8eac9acd2567fd9e19f
--- /dev/null
+++ b/components/precache/core/precache_database.cc
@@ -0,0 +1,182 @@
+// 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 <map>
+
+#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;
+
+// The default max range of UMA_HISTOGRAM_COUNTS is 1 million, which is too
+// small for tracking precache byte counts, since counts can easily be greater
+// than 1 MB.
+#define UMA_HISTOGRAM_BYTES(name, sample) \
+ UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000000000, 50);
mmenke 2013/10/29 18:23:34 Samples are integers, aren't they? If that's the
mmenke 2013/10/29 18:23:34 I think we really should change the name. One cou
sclittle 2013/10/29 23:40:45 Done.
sclittle 2013/10/29 23:40:45 Changed to report values in KB using UMA_HISTOGRAM
+
+void RecordSingleDayPrecacheUMA(
+ const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) {
+ UMA_HISTOGRAM_BYTES("Precache.DailyPrecachedBytes", stats.precached_bytes);
+ UMA_HISTOGRAM_BYTES("Precache.DailyDownloadedBytes", stats.downloaded_bytes);
+ UMA_HISTOGRAM_BYTES("Precache.DailyDownloadedBytes.Cellular",
+ stats.downloaded_bytes_cellular);
+ UMA_HISTOGRAM_BYTES("Precache.DailySavedBytes", stats.saved_bytes);
+ UMA_HISTOGRAM_BYTES("Precache.DailySavedBytes.Cellular",
+ stats.saved_bytes_cellular);
+
+ if (stats.saved_bytes + stats.downloaded_bytes > 0) {
+ UMA_HISTOGRAM_PERCENTAGE("Precache.DailySavingsPercentage",
+ stats.saved_bytes * 100.0 /
+ (stats.saved_bytes + stats.downloaded_bytes));
+ }
+
+ if (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular > 0) {
+ UMA_HISTOGRAM_PERCENTAGE(
+ "Precache.DailySavingsPercentage.Cellular",
+ stats.saved_bytes_cellular * 100.0 /
+ (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular));
+ }
+}
+
+#undef UMA_HISTOGRAM_BYTES
mmenke 2013/10/29 18:23:34 Don't think this is needed, and #undef seems prett
sclittle 2013/10/29 23:40:45 Removed.
+
+} // namespace
+
+namespace precache {
+
+PrecacheDatabase::PrecacheDatabase()
+ : precache_url_table_(new PrecacheURLTable()),
+ precache_statistics_table_(new PrecacheStatisticsTable()) {
+ // A PrecacheDatabase can be constructed on any thread.
+ DetachFromThread();
mmenke 2013/10/29 18:23:34 Does this work? Looks like the parent's destructo
sclittle 2013/10/29 23:40:45 If the thread ID is not set, CalledOnValidThread()
+}
+
+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(sql::Connection* db) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!db_); // Init must only be called once.
+ DCHECK(db); // |db| must not be NULL.
+
+ db_.reset(db);
+ if (CantAccessDatabase()) {
+ // 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());
+}
+
+void PrecacheDatabase::ReportAndDeleteOldStats(base::Time end_date) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(db_);
+
+ if (CantAccessDatabase()) {
+ // Do nothing if unable to access the database.
+ return;
+ }
+
+ // Delete old precache history that has expired.
+ precache_url_table_->DeleteAllPrecachedBetween(
+ base::Time::FromInternalValue(0),
mmenke 2013/10/29 18:23:34 Can't this just be base::Time()?
sclittle 2013/10/29 23:40:45 Done.
+ end_date - base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays));
+
+ std::map<base::Time, PrecacheStatisticsTable::PrecacheStatistics> stats_map;
mmenke 2013/10/29 18:23:34 This is really crying out for a public typedef in
sclittle 2013/10/29 23:40:45 Done.
+ precache_statistics_table_->GetAllStatsBetween(
+ base::Time::FromInternalValue(0), end_date, &stats_map);
mmenke 2013/10/29 18:23:34 base::Time(0)?
sclittle 2013/10/29 23:40:45 Changed to base::Time().
+
+ // 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 (std::map<base::Time,
+ PrecacheStatisticsTable::PrecacheStatistics>::const_iterator
+ it = stats_map.begin();
+ it != stats_map.end(); ++it) {
+ RecordSingleDayPrecacheUMA(it->second);
+ }
+
+ precache_statistics_table_->DeleteAllStatsBetween(
+ base::Time::FromInternalValue(0), end_date);
+}
+
+void PrecacheDatabase::RecordURLFetched(GURL url, base::Time fetch_time,
+ int64 size, bool was_cached,
+ bool is_precaching, bool is_cellular) {
mmenke 2013/10/29 18:23:34 Should we separate out those resources we had to r
sclittle 2013/10/29 23:40:45 That's true, but for individual fetches we're just
+ DCHECK(CalledOnValidThread());
+ DCHECK(db_);
+
+ if (CantAccessDatabase()) {
mmenke 2013/10/29 18:23:34 Should we just DCHECK on this instead, or is it an
sclittle 2013/10/29 23:40:45 If the user ran out of disk space or something and
+ // Don't track anything if unable to access the database.
+ return;
+ }
+
+ if (is_precaching) {
+ if (was_cached && !precache_url_table_->HasURL(url)) {
+ // If a precache fetch came from the cache, and the user browsed the
+ // resource recently, then the precache fetch did nothing, so ignore it.
mmenke 2013/10/29 18:23:34 I'm not following...The table should have the URL
sclittle 2013/10/29 23:40:45 The URL table only keeps track of URLs that are in
mmenke 2013/10/30 18:29:08 You seem to add everything we download to the tabl
sclittle 2013/10/30 20:29:48 The purpose of the URL table is to allow us to ide
+ return;
+ }
+
+ if (!was_cached) {
+ PrecacheStatisticsTable::PrecacheStatistics stats;
+ stats.precached_bytes = size;
+ precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats);
mmenke 2013/10/29 18:23:34 Why are we only setting one of the fields? I'm co
sclittle 2013/10/29 23:40:45 All fields in the PrecacheStatistics default to 0.
+ }
+
+ precache_url_table_->AddURL(url, fetch_time);
+ return;
+ }
+
+ if (!was_cached) {
+ PrecacheStatisticsTable::PrecacheStatistics stats;
+ stats.downloaded_bytes = size;
+ if (is_cellular) {
+ stats.downloaded_bytes_cellular = size;
+ }
+ precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats);
+
+ // Since the fetch was over the network, delete any record of it having been
+ // prefetched from the URL table. If it had been precached, the resource
+ // must have expired.
+ precache_url_table_->DeleteURL(url);
+ return;
+ }
+
+ if (was_cached && precache_url_table_->HasURL(url)) {
+ PrecacheStatisticsTable::PrecacheStatistics stats;
+ stats.saved_bytes = size;
+ if (is_cellular) {
+ stats.saved_bytes_cellular = size;
+ }
+ precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats);
mmenke 2013/10/29 18:23:34 When not precaching, we record stats if it wasn't
sclittle 2013/10/29 23:40:45 Added comment.
+
+ precache_url_table_->DeleteURL(url);
+ }
+}
+
+bool PrecacheDatabase::CantAccessDatabase() {
mmenke 2013/10/29 18:23:34 nit: I think it's easy to miss the "T" when readi
mmenke 2013/10/29 18:23:34 Can this be const?
sclittle 2013/10/29 23:40:45 Done.
sclittle 2013/10/29 23:40:45 Done.
+ DCHECK(CalledOnValidThread());
+ DCHECK(db_);
+
+ return !db_->is_open();
+}
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698