Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 sql::Transaction transaction(db_.get()); | |
| 52 bool initialization_was_successful = transaction.Begin() && | |
| 53 precache_url_table_->Init(db_.get()) && | |
| 54 transaction.Commit(); | |
|
Scott Hess - ex-Googler
2013/12/03 20:55:38
This doesn't make quite as much sense when you onl
sclittle
2013/12/04 19:07:10
Done.
| |
| 55 | |
| 56 if (!initialization_was_successful) { | |
| 57 // Raze and close the database connection to indicate that it's not usable, | |
| 58 // and so that the database will be created anew next time, in case it's | |
| 59 // corrupted. | |
| 60 db_->RazeAndClose(); | |
| 61 return false; | |
| 62 } | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 void PrecacheDatabase::DeleteExpiredPrecacheHistory( | |
| 67 const base::Time& current_time) { | |
| 68 if (!IsDatabaseAccessible()) { | |
| 69 // Do nothing if unable to access the database. | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 // Delete old precache history that has expired. | |
| 74 precache_url_table_->DeleteAllPrecachedBefore( | |
| 75 current_time - | |
| 76 base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 77 } | |
| 78 | |
| 79 void PrecacheDatabase::RecordURLPrecached(const GURL& url, | |
| 80 const base::Time& fetch_time, | |
| 81 int64 size, bool was_cached) { | |
| 82 if (!IsDatabaseAccessible()) { | |
| 83 // Don't track anything if unable to access the database. | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 sql::Transaction transaction(db_.get()); | |
| 88 if (!transaction.Begin()) { | |
| 89 // Do nothing if unable to begin a transaction. | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 94 // Since the precache came from the cache, and there's no entry in the URL | |
| 95 // table for the URL, this means that the resource was already in the cache | |
| 96 // because of user browsing. Thus, this precache had no effect, so ignore | |
| 97 // it. | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 if (!was_cached) { | |
| 102 // The precache only counts as overhead if it was downloaded over the | |
| 103 // network. | |
| 104 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", 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 precache_url_table_->AddURL(url, fetch_time); | |
| 111 | |
| 112 transaction.Commit(); | |
| 113 } | |
| 114 | |
| 115 void PrecacheDatabase::RecordURLFetched(const GURL& url, | |
| 116 const base::Time& fetch_time, | |
| 117 int64 size, bool was_cached, | |
| 118 bool is_connection_cellular) { | |
| 119 if (!IsDatabaseAccessible()) { | |
| 120 // Don't track anything if unable to access the database. | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 sql::Transaction transaction(db_.get()); | |
| 125 if (!transaction.Begin()) { | |
| 126 // Do nothing if unable to begin a transaction. | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 131 // Ignore cache hits that precache can't take credit for. | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 if (!was_cached) { | |
| 136 // The fetch was served over the network during user browsing, so count it | |
| 137 // as downloaded non-precache bytes. | |
| 138 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size); | |
| 139 if (is_connection_cellular) { | |
| 140 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", | |
| 141 size); | |
| 142 } | |
| 143 } else { | |
| 144 // The fetch was served from the cache, and since there's an entry for this | |
| 145 // URL in the URL table, this means that the resource was served from the | |
| 146 // cache only because precaching put it there. Thus, precaching was helpful, | |
| 147 // so count the fetch as saved bytes. | |
| 148 UMA_HISTOGRAM_COUNTS("Precache.Saved", size); | |
| 149 if (is_connection_cellular) { | |
| 150 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 // Since the resource has been fetched during user browsing, remove any record | |
| 155 // of that URL having been precached from the URL table, if any exists. | |
| 156 // The current fetch would have put this resource in the cache regardless of | |
| 157 // whether or not it was previously precached, so delete any record of that | |
| 158 // URL having been precached from the URL table. | |
| 159 precache_url_table_->DeleteURL(url); | |
| 160 | |
| 161 transaction.Commit(); | |
| 162 } | |
| 163 | |
| 164 bool PrecacheDatabase::IsDatabaseAccessible() const { | |
| 165 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 166 DCHECK(db_); | |
| 167 | |
| 168 return db_->is_open(); | |
| 169 } | |
| 170 | |
| 171 } // namespace precache | |
| OLD | NEW |