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

Side by Side Diff: chrome/browser/history/download_database.cc

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/history/download_database.h" 5 #include "chrome/browser/history/download_database.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/debug/alias.h" 11 #include "base/debug/alias.h"
12 #include "base/file_path.h" 12 #include "base/file_path.h"
13 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "chrome/browser/history/download_persistent_store_info.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/download_item.h" 19 #include "content/public/browser/download_item.h"
19 #include "content/public/browser/download_persistent_store_info.h"
20 #include "sql/statement.h" 20 #include "sql/statement.h"
21 21
22 using content::DownloadItem; 22 using content::DownloadItem;
23 using content::DownloadPersistentStoreInfo;
24 23
25 namespace history { 24 namespace history {
26 25
27 namespace { 26 namespace {
28 27
29 static const char kSchema[] = 28 static const char kSchema[] =
30 "CREATE TABLE downloads (" 29 "CREATE TABLE downloads ("
31 "id INTEGER PRIMARY KEY," // SQLite-generated primary key. 30 "id INTEGER PRIMARY KEY," // SQLite-generated primary key.
32 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk. 31 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk.
33 "url LONGVARCHAR NOT NULL," // URL of the downloaded file. 32 "url LONGVARCHAR NOT NULL," // URL of the downloaded file.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 DownloadDatabase::DownloadDatabase() 104 DownloadDatabase::DownloadDatabase()
106 : owning_thread_set_(false), 105 : owning_thread_set_(false),
107 owning_thread_(0), 106 owning_thread_(0),
108 next_id_(0), 107 next_id_(0),
109 next_db_handle_(0) { 108 next_db_handle_(0) {
110 } 109 }
111 110
112 DownloadDatabase::~DownloadDatabase() { 111 DownloadDatabase::~DownloadDatabase() {
113 } 112 }
114 113
115 void DownloadDatabase::CheckThread() {
116 if (owning_thread_set_) {
117 DCHECK(owning_thread_ == base::PlatformThread::CurrentId());
118 } else {
119 owning_thread_ = base::PlatformThread::CurrentId();
120 owning_thread_set_ = true;
121 }
122 }
123
124 bool DownloadDatabase::EnsureColumnExists( 114 bool DownloadDatabase::EnsureColumnExists(
125 const std::string& name, const std::string& type) { 115 const std::string& name, const std::string& type) {
126 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; 116 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type;
127 return GetDB().DoesColumnExist("downloads", name.c_str()) || 117 return GetDB().DoesColumnExist("downloads", name.c_str()) ||
128 GetDB().Execute(add_col.c_str()); 118 GetDB().Execute(add_col.c_str());
129 } 119 }
130 120
131 bool DownloadDatabase::MigrateDownloadsState() { 121 bool DownloadDatabase::MigrateDownloadsState() {
132 sql::Statement statement(GetDB().GetUniqueStatement( 122 sql::Statement statement(GetDB().GetUniqueStatement(
133 "UPDATE downloads SET state=? WHERE state=?")); 123 "UPDATE downloads SET state=? WHERE state=?"));
134 statement.BindInt(0, kStateInterrupted); 124 statement.BindInt(0, kStateInterrupted);
135 statement.BindInt(1, kStateBug140687); 125 statement.BindInt(1, kStateBug140687);
136 return statement.Run(); 126 return statement.Run();
137 } 127 }
138 128
139 bool DownloadDatabase::InitDownloadTable() { 129 bool DownloadDatabase::InitDownloadTable() {
140 CheckThread();
141 GetMetaTable().GetValue(kNextDownloadId, &next_id_); 130 GetMetaTable().GetValue(kNextDownloadId, &next_id_);
142 if (GetDB().DoesTableExist("downloads")) { 131 if (GetDB().DoesTableExist("downloads")) {
143 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && 132 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") &&
144 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); 133 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0");
145 } else { 134 } else {
146 return GetDB().Execute(kSchema); 135 return GetDB().Execute(kSchema);
147 } 136 }
148 } 137 }
149 138
150 bool DownloadDatabase::DropDownloadTable() { 139 bool DownloadDatabase::DropDownloadTable() {
151 CheckThread();
152 return GetDB().Execute("DROP TABLE downloads"); 140 return GetDB().Execute("DROP TABLE downloads");
153 } 141 }
154 142
155 void DownloadDatabase::QueryDownloads( 143 void DownloadDatabase::QueryDownloads(
156 std::vector<DownloadPersistentStoreInfo>* results) { 144 std::vector<DownloadPersistentStoreInfo>* results) {
157 CheckThread();
158 results->clear(); 145 results->clear();
159 if (next_db_handle_ < 1) 146 if (next_db_handle_ < 1)
160 next_db_handle_ = 1; 147 next_db_handle_ = 1;
161 std::set<DownloadID> db_handles; 148 std::set<DownloadID> db_handles;
162 149
163 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 150 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
164 "SELECT id, full_path, url, start_time, received_bytes, " 151 "SELECT id, full_path, url, start_time, received_bytes, "
165 "total_bytes, state, end_time, opened " 152 "total_bytes, state, end_time, opened "
166 "FROM downloads " 153 "FROM downloads "
167 "ORDER BY start_time")); 154 "ORDER BY start_time"));
(...skipping 19 matching lines...) Expand all
187 } 174 }
188 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) { 175 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) {
189 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); 176 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state);
190 continue; 177 continue;
191 } 178 }
192 results->push_back(info); 179 results->push_back(info);
193 } 180 }
194 } 181 }
195 182
196 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) { 183 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
197 CheckThread();
198 DCHECK(data.db_handle > 0); 184 DCHECK(data.db_handle > 0);
199 int state = StateToInt(data.state); 185 int state = StateToInt(data.state);
200 if (state == kStateInvalid) { 186 if (state == kStateInvalid) {
201 // TODO(benjhayden) [D]CHECK instead. 187 // TODO(benjhayden) [D]CHECK instead.
202 return false; 188 return false;
203 } 189 }
204 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 190 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
205 "UPDATE downloads " 191 "UPDATE downloads "
206 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); 192 "SET full_path=?, received_bytes=?, state=?, end_time=?, total_bytes=?, "
207 statement.BindInt64(0, data.received_bytes); 193 "opened=? WHERE id=?"));
208 statement.BindInt(1, state); 194 BindFilePath(statement, data.path, 0);
209 statement.BindInt64(2, data.end_time.ToTimeT()); 195 statement.BindInt64(1, data.received_bytes);
210 statement.BindInt(3, (data.opened ? 1 : 0)); 196 statement.BindInt(2, state);
211 statement.BindInt64(4, data.db_handle); 197 statement.BindInt64(3, data.end_time.ToTimeT());
212 198 statement.BindInt(4, data.total_bytes);
213 return statement.Run(); 199 statement.BindInt(5, (data.opened ? 1 : 0));
214 } 200 statement.BindInt64(6, data.db_handle);
215
216 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path,
217 DownloadID db_handle) {
218 CheckThread();
219 DCHECK(db_handle > 0);
220 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
221 "UPDATE downloads SET full_path=? WHERE id=?"));
222 BindFilePath(statement, path, 0);
223 statement.BindInt64(1, db_handle);
224 201
225 return statement.Run(); 202 return statement.Run();
226 } 203 }
227 204
228 bool DownloadDatabase::CleanUpInProgressEntries() { 205 bool DownloadDatabase::CleanUpInProgressEntries() {
229 CheckThread();
230 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 206 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
231 "UPDATE downloads SET state=? WHERE state=?")); 207 "UPDATE downloads SET state=? WHERE state=?"));
232 statement.BindInt(0, kStateCancelled); 208 statement.BindInt(0, kStateCancelled);
233 statement.BindInt(1, kStateInProgress); 209 statement.BindInt(1, kStateInProgress);
234 210
235 return statement.Run(); 211 return statement.Run();
236 } 212 }
237 213
238 int64 DownloadDatabase::CreateDownload( 214 int64 DownloadDatabase::CreateDownload(
239 const DownloadPersistentStoreInfo& info) { 215 const DownloadPersistentStoreInfo& info) {
240 CheckThread();
241 216
242 if (next_db_handle_ == 0) { 217 if (next_db_handle_ == 0) {
243 // This is unlikely. All current known tests and users already call 218 // This is unlikely. All current known tests and users already call
244 // QueryDownloads() before CreateDownload(). 219 // QueryDownloads() before CreateDownload().
245 std::vector<DownloadPersistentStoreInfo> results; 220 std::vector<DownloadPersistentStoreInfo> results;
246 QueryDownloads(&results); 221 QueryDownloads(&results);
247 CHECK_NE(0, next_db_handle_); 222 CHECK_NE(0, next_db_handle_);
248 } 223 }
249 224
250 int state = StateToInt(info.state); 225 int state = StateToInt(info.state);
(...skipping 20 matching lines...) Expand all
271 246
272 if (statement.Run()) { 247 if (statement.Run()) {
273 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} 248 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;}
274 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); 249 GetMetaTable().SetValue(kNextDownloadId, ++next_id_);
275 250
276 return db_handle; 251 return db_handle;
277 } 252 }
278 return 0; 253 return 0;
279 } 254 }
280 255
281 void DownloadDatabase::RemoveDownload(DownloadID db_handle) { 256 void DownloadDatabase::RemoveDownloads(const std::set<DownloadID>& handles) {
282 CheckThread(); 257 int downloads_count_before = CountDownloads();
283 258 base::TimeTicks started_removing = base::TimeTicks::Now();
284 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 259 // HistoryBackend uses a long-running Transaction that is committed
285 "DELETE FROM downloads WHERE id=?")); 260 // periodically, so this loop doesn't actually hit the disk too hard.
286 statement.BindInt64(0, db_handle); 261 for (std::set<DownloadID>::const_iterator it = handles.begin();
287 262 it != handles.end(); ++it) {
288 statement.Run(); 263 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
289 } 264 "DELETE FROM downloads WHERE id=?"));
290 265 statement.BindInt64(0, *it);
291 bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, 266 statement.Run();
292 base::Time delete_end) {
293 CheckThread();
294 time_t start_time = delete_begin.ToTimeT();
295 time_t end_time = delete_end.ToTimeT();
296
297 int num_downloads_deleted = -1;
298 {
299 sql::Statement count(GetDB().GetCachedStatement(SQL_FROM_HERE,
300 "SELECT count(*) FROM downloads WHERE start_time >= ? "
301 "AND start_time < ? AND (State = ? OR State = ? OR State = ?)"));
302 count.BindInt64(0, start_time);
303 count.BindInt64(
304 1,
305 end_time ? end_time : std::numeric_limits<int64>::max());
306 count.BindInt(2, kStateComplete);
307 count.BindInt(3, kStateCancelled);
308 count.BindInt(4, kStateInterrupted);
309 if (count.Step())
310 num_downloads_deleted = count.ColumnInt(0);
311 } 267 }
312
313
314 bool success = false;
315 base::TimeTicks started_removing = base::TimeTicks::Now();
316 {
317 // This does not use an index. We currently aren't likely to have enough
318 // downloads where an index by time will give us a lot of benefit.
319 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
320 "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? "
321 "AND (State = ? OR State = ? OR State = ?)"));
322 statement.BindInt64(0, start_time);
323 statement.BindInt64(
324 1,
325 end_time ? end_time : std::numeric_limits<int64>::max());
326 statement.BindInt(2, kStateComplete);
327 statement.BindInt(3, kStateCancelled);
328 statement.BindInt(4, kStateInterrupted);
329
330 success = statement.Run();
331 }
332
333 base::TimeTicks finished_removing = base::TimeTicks::Now(); 268 base::TimeTicks finished_removing = base::TimeTicks::Now();
334 269 int downloads_count_after = CountDownloads();
270 int num_downloads_deleted = downloads_count_before - downloads_count_after;
335 if (num_downloads_deleted >= 0) { 271 if (num_downloads_deleted >= 0) {
336 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount", 272 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount",
337 num_downloads_deleted); 273 num_downloads_deleted);
338 base::TimeDelta micros = (1000 * (finished_removing - started_removing)); 274 base::TimeDelta micros = (1000 * (finished_removing - started_removing));
339 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros); 275 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros);
340 if (num_downloads_deleted > 0) { 276 if (num_downloads_deleted > 0) {
341 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord", 277 UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord",
342 (1000 * micros) / num_downloads_deleted); 278 (1000 * micros) / num_downloads_deleted);
343 } 279 }
344 } 280 }
281 int num_downloads_not_deleted = handles.size() - num_downloads_deleted;
282 if (num_downloads_not_deleted >= 0) {
Randy Smith (Not in Mondays) 2012/09/11 18:36:53 I'm good with these stats--I think I was misreadin
benjhayden 2012/09/13 15:18:16 Done.
283 UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved",
284 num_downloads_not_deleted);
285 }
286 }
345 287
346 return success; 288 int DownloadDatabase::CountDownloads() {
289 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
290 "SELECT count(*) from downloads"));
291 statement.Step();
292 return statement.ColumnInt(0);
347 } 293 }
348 294
349 } // namespace history 295 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698