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

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

Issue 2586813004: Report downloaded resources at most once (Closed)
Patch Set: Removed superfluous test Created 4 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/precache/core/precache_url_table.h" 5 #include "components/precache/core/precache_url_table.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "sql/connection.h" 10 #include "sql/connection.h"
11 #include "sql/statement.h" 11 #include "sql/statement.h"
12 12
13 using sql::Statement; 13 using sql::Statement;
14 14
15 namespace { 15 namespace {
16 16
17 // Returns the spec of the given URL. 17 // Returns the spec of the given URL.
18 std::string GetKey(const GURL& url) { 18 std::string GetKey(const GURL& url) {
19 return url.spec(); 19 return url.spec();
20 } 20 }
21 21
22 } // namespace 22 } // namespace
23 23
24 namespace precache { 24 namespace precache {
25 25
26 bool PrecacheURLInfo::operator==(const PrecacheURLInfo& other) const { 26 bool PrecacheURLInfo::operator==(const PrecacheURLInfo& other) const {
27 return was_precached == other.was_precached && 27 return was_precached == other.was_precached &&
28 is_precached == other.is_precached && was_used == other.was_used; 28 is_precached == other.is_precached && was_used == other.was_used &&
29 is_download_reported == other.is_download_reported;
29 } 30 }
30 31
31 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {} 32 PrecacheURLTable::PrecacheURLTable() : db_(NULL) {}
32 33
33 PrecacheURLTable::~PrecacheURLTable() {} 34 PrecacheURLTable::~PrecacheURLTable() {}
34 35
35 bool PrecacheURLTable::Init(sql::Connection* db) { 36 bool PrecacheURLTable::Init(sql::Connection* db) {
36 DCHECK(!db_); // Init must only be called once. 37 DCHECK(!db_); // Init must only be called once.
37 DCHECK(db); // The database connection must be non-NULL. 38 DCHECK(db); // The database connection must be non-NULL.
38 db_ = db; 39 db_ = db;
39 return CreateTableIfNonExistent(); 40 return CreateTableIfNonExistent();
40 } 41 }
41 42
42 void PrecacheURLTable::AddURL(const GURL& url, 43 void PrecacheURLTable::AddURL(const GURL& url,
43 int64_t referrer_host_id, 44 int64_t referrer_host_id,
44 bool is_precached, 45 bool is_precached,
45 const base::Time& precache_time) { 46 const base::Time& precache_time,
46 Statement statement( 47 bool is_download_reported) {
47 db_->GetCachedStatement(SQL_FROM_HERE, 48 Statement statement(db_->GetCachedStatement(
48 "INSERT OR REPLACE INTO precache_urls (url, " 49 SQL_FROM_HERE,
49 "referrer_host_id, was_used, is_precached, time) " 50 "INSERT OR REPLACE INTO precache_urls "
50 "VALUES(?,?,0,?,?)")); 51 "(url, referrer_host_id, was_used, is_precached, time, "
51 52 " is_download_reported)"
53 "VALUES(?, ?, 0, ?, ?, ?)"));
52 statement.BindString(0, GetKey(url)); 54 statement.BindString(0, GetKey(url));
53 statement.BindInt64(1, referrer_host_id); 55 statement.BindInt64(1, referrer_host_id);
54 statement.BindInt64(2, is_precached ? 1 : 0); 56 statement.BindInt64(2, is_precached ? 1 : 0);
55 statement.BindInt64(3, precache_time.ToInternalValue()); 57 statement.BindInt64(3, precache_time.ToInternalValue());
58 statement.BindInt64(4, is_download_reported);
56 statement.Run(); 59 statement.Run();
57 } 60 }
58 61
59 PrecacheURLInfo PrecacheURLTable::GetURLInfo(const GURL& url) { 62 PrecacheURLInfo PrecacheURLTable::GetURLInfo(const GURL& url) {
60 Statement statement(db_->GetCachedStatement( 63 Statement statement(db_->GetCachedStatement(
61 SQL_FROM_HERE, 64 SQL_FROM_HERE,
62 "SELECT is_precached, was_used FROM precache_urls WHERE url=?")); 65 "SELECT is_precached, was_used, is_download_reported "
66 "FROM precache_urls WHERE url=?"));
63 statement.BindString(0, GetKey(url)); 67 statement.BindString(0, GetKey(url));
64 68
65 if (statement.Step()) { 69 if (statement.Step()) {
66 return {/*present=*/true, /*is_precached=*/statement.ColumnBool(0), 70 return {/*present=*/true, /*is_precached=*/statement.ColumnBool(0),
67 /*was_used==*/statement.ColumnBool(1)}; 71 /*was_used==*/statement.ColumnBool(1),
72 /*is_download_reported=*/statement.ColumnBool(2)};
68 } else { 73 } else {
69 return {/*present=*/false, /*is_precached=*/false, /*was_used=*/false}; 74 return {/*present=*/false, /*is_precached=*/false, /*was_used=*/false,
75 /*is_download_reported=*/false};
70 } 76 }
71 } 77 }
72 78
73 void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) { 79 void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) {
74 Statement statement( 80 Statement statement(
75 db_->GetCachedStatement(SQL_FROM_HERE, 81 db_->GetCachedStatement(SQL_FROM_HERE,
76 "UPDATE precache_urls SET was_used=1, " 82 "UPDATE precache_urls SET was_used=1, "
77 "is_precached=0 " 83 "is_precached=0 "
78 "WHERE url=? and was_used=0 and is_precached=1")); 84 "WHERE url=? and was_used=0 and is_precached=1"));
79 85
80 statement.BindString(0, GetKey(url)); 86 statement.BindString(0, GetKey(url));
81 statement.Run(); 87 statement.Run();
82 } 88 }
83 89
84 void PrecacheURLTable::SetURLAsNotPrecached(const GURL& url) { 90 void PrecacheURLTable::SetURLAsNotPrecached(const GURL& url) {
85 Statement statement( 91 Statement statement(
86 db_->GetCachedStatement(SQL_FROM_HERE, 92 db_->GetCachedStatement(SQL_FROM_HERE,
87 "UPDATE precache_urls SET is_precached=0 " 93 "UPDATE precache_urls SET is_precached=0 "
88 "WHERE url=? and is_precached=1")); 94 "WHERE url=? and is_precached=1"));
89 statement.BindString(0, GetKey(url)); 95 statement.BindString(0, GetKey(url));
90 statement.Run(); 96 statement.Run();
91 } 97 }
92 98
93 void PrecacheURLTable::GetURLListForReferrerHost( 99 void PrecacheURLTable::GetURLListForReferrerHost(
94 int64_t referrer_host_id, 100 int64_t referrer_host_id,
95 std::vector<GURL>* used_urls, 101 std::vector<GURL>* used_urls,
96 std::vector<GURL>* unused_urls) { 102 std::vector<GURL>* downloaded_urls) {
97 Statement statement(db_->GetCachedStatement( 103 Statement statement(
98 SQL_FROM_HERE, 104 db_->GetCachedStatement(SQL_FROM_HERE,
99 "SELECT url, was_used from precache_urls where referrer_host_id=?")); 105 "SELECT url, was_used, is_download_reported "
106 "from precache_urls where referrer_host_id=?"));
100 statement.BindInt64(0, referrer_host_id); 107 statement.BindInt64(0, referrer_host_id);
101 while (statement.Step()) { 108 while (statement.Step()) {
102 GURL url(statement.ColumnString(0)); 109 GURL url(statement.ColumnString(0));
103 if (statement.ColumnInt(1)) 110 if (statement.ColumnInt(1))
104 used_urls->push_back(url); 111 used_urls->push_back(url);
105 else 112 if (!statement.ColumnInt(2))
106 unused_urls->push_back(url); 113 downloaded_urls->push_back(url);
107 } 114 }
108 } 115 }
109 116
117 void PrecacheURLTable::SetDownloadReported(int64_t referrer_host_id) {
118 Statement statement(
119 db_->GetCachedStatement(SQL_FROM_HERE,
120 "UPDATE precache_urls SET is_download_reported=1 "
121 "WHERE referrer_host_id=?"));
122 statement.BindInt64(0, referrer_host_id);
123 statement.Run();
124 }
125
110 void PrecacheURLTable::ClearAllForReferrerHost(int64_t referrer_host_id) { 126 void PrecacheURLTable::ClearAllForReferrerHost(int64_t referrer_host_id) {
111 // Delete the URLs that are not precached. 127 // Delete the URLs that are not precached.
112 Statement delete_statement( 128 Statement delete_statement(
113 db_->GetCachedStatement(SQL_FROM_HERE, 129 db_->GetCachedStatement(SQL_FROM_HERE,
114 "DELETE FROM precache_urls WHERE " 130 "DELETE FROM precache_urls WHERE "
115 "referrer_host_id=? AND is_precached=0")); 131 "referrer_host_id=? AND is_precached=0"));
116 delete_statement.BindInt64(0, referrer_host_id); 132 delete_statement.BindInt64(0, referrer_host_id);
117 delete_statement.Run(); 133 delete_statement.Run();
118 134
119 // Clear was_used for precached URLs. 135 // Clear was_used for precached URLs.
(...skipping 26 matching lines...) Expand all
146 SQL_FROM_HERE, 162 SQL_FROM_HERE,
147 "SELECT url, time FROM precache_urls where is_precached=1")); 163 "SELECT url, time FROM precache_urls where is_precached=1"));
148 164
149 while (statement.Step()) { 165 while (statement.Step()) {
150 GURL url = GURL(statement.ColumnString(0)); 166 GURL url = GURL(statement.ColumnString(0));
151 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1)); 167 (*map)[url] = base::Time::FromInternalValue(statement.ColumnInt64(1));
152 } 168 }
153 } 169 }
154 170
155 bool PrecacheURLTable::CreateTableIfNonExistent() { 171 bool PrecacheURLTable::CreateTableIfNonExistent() {
172 // TODO(jamartin): The PRIMARY KEY should be (url, referrer_host_id).
156 if (!db_->DoesTableExist("precache_urls")) { 173 if (!db_->DoesTableExist("precache_urls")) {
157 return db_->Execute( 174 return db_->Execute(
158 "CREATE TABLE precache_urls " 175 "CREATE TABLE precache_urls "
159 "(url TEXT PRIMARY KEY, referrer_host_id INTEGER, was_used INTEGER, " 176 "(url TEXT PRIMARY KEY, referrer_host_id INTEGER, was_used INTEGER, "
160 "is_precached INTEGER, " 177 "is_precached INTEGER, "
twifkak 2016/12/20 22:48:09 Um. Ugh. There is no default 1 for is_precached he
jamartin 2016/12/20 23:14:07 Acknowledged. This may cause a bug in the future
161 "time INTEGER)"); 178 "time INTEGER, is_download_reported INTEGER)");
162 } else { 179 } else {
163 // Migrate the table by creating the missing columns. 180 // Migrate the table by creating the missing columns.
164 if (!db_->DoesColumnExist("precache_urls", "was_used") && 181 if (!db_->DoesColumnExist("precache_urls", "was_used") &&
165 !db_->Execute("ALTER TABLE precache_urls ADD COLUMN was_used INTEGER")) 182 !db_->Execute("ALTER TABLE precache_urls "
183 "ADD COLUMN was_used INTEGER")) {
166 return false; 184 return false;
185 }
167 if (!db_->DoesColumnExist("precache_urls", "is_precached") && 186 if (!db_->DoesColumnExist("precache_urls", "is_precached") &&
168 !db_->Execute( 187 !db_->Execute("ALTER TABLE precache_urls ADD COLUMN is_precached "
169 "ALTER TABLE precache_urls ADD COLUMN is_precached " 188 "INTEGER default 1")) {
170 "INTEGER default 1"))
171 return false; 189 return false;
190 }
172 if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") && 191 if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") &&
173 !db_->Execute( 192 !db_->Execute(
174 "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER")) 193 "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER")) {
175 return false; 194 return false;
195 }
196 if (!db_->DoesColumnExist("precache_urls", "is_download_reported") &&
197 !db_->Execute("ALTER TABLE precache_urls "
198 "ADD COLUMN is_download_reported INTEGER")) {
199 return false;
200 }
176 } 201 }
177 return true; 202 return true;
178 } 203 }
179 204
180 } // namespace precache 205 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698