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

Unified 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: @r167394 Created 8 years, 1 month 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: chrome/browser/history/download_database.cc
diff --git a/chrome/browser/history/download_database.cc b/chrome/browser/history/download_database.cc
index c9881156db31829652ffccc9ffd972e32777b381..18faa9c27fad8cb56542445fed038a07ff8cc42f 100644
--- a/chrome/browser/history/download_database.cc
+++ b/chrome/browser/history/download_database.cc
@@ -14,16 +14,17 @@
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "build/build_config.h"
+#include "chrome/browser/history/download_persistent_store_info.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item.h"
-#include "content/public/browser/download_persistent_store_info.h"
#include "sql/statement.h"
using content::DownloadItem;
-using content::DownloadPersistentStoreInfo;
namespace history {
+const int64 DownloadDatabase::kUninitializedHandle = -1;
sky 2012/11/13 20:45:52 nit: for static we generally put "// static" on th
benjhayden 2012/11/13 21:24:14 Done.
+
namespace {
static const char kSchema[] =
@@ -112,15 +113,6 @@ DownloadDatabase::DownloadDatabase()
DownloadDatabase::~DownloadDatabase() {
}
-void DownloadDatabase::CheckThread() {
- if (owning_thread_set_) {
- DCHECK(owning_thread_ == base::PlatformThread::CurrentId());
- } else {
- owning_thread_ = base::PlatformThread::CurrentId();
- owning_thread_set_ = true;
- }
-}
-
bool DownloadDatabase::EnsureColumnExists(
const std::string& name, const std::string& type) {
std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type;
@@ -137,7 +129,6 @@ bool DownloadDatabase::MigrateDownloadsState() {
}
bool DownloadDatabase::InitDownloadTable() {
- CheckThread();
GetMetaTable().GetValue(kNextDownloadId, &next_id_);
if (GetDB().DoesTableExist("downloads")) {
return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") &&
@@ -148,17 +139,15 @@ bool DownloadDatabase::InitDownloadTable() {
}
bool DownloadDatabase::DropDownloadTable() {
- CheckThread();
return GetDB().Execute("DROP TABLE downloads");
}
void DownloadDatabase::QueryDownloads(
std::vector<DownloadPersistentStoreInfo>* results) {
- CheckThread();
results->clear();
if (next_db_handle_ < 1)
next_db_handle_ = 1;
- std::set<DownloadID> db_handles;
+ std::set<int64> db_handles;
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"SELECT id, full_path, url, start_time, received_bytes, "
@@ -194,7 +183,6 @@ void DownloadDatabase::QueryDownloads(
}
bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
- CheckThread();
DCHECK(data.db_handle > 0);
int state = StateToInt(data.state);
if (state == kStateInvalid) {
@@ -203,30 +191,20 @@ bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
}
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE downloads "
- "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?"));
- statement.BindInt64(0, data.received_bytes);
- statement.BindInt(1, state);
- statement.BindInt64(2, data.end_time.ToTimeT());
- statement.BindInt(3, (data.opened ? 1 : 0));
- statement.BindInt64(4, data.db_handle);
-
- return statement.Run();
-}
-
-bool DownloadDatabase::UpdateDownloadPath(const FilePath& path,
- DownloadID db_handle) {
- CheckThread();
- DCHECK(db_handle > 0);
- sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
- "UPDATE downloads SET full_path=? WHERE id=?"));
- BindFilePath(statement, path, 0);
- statement.BindInt64(1, db_handle);
+ "SET full_path=?, received_bytes=?, state=?, end_time=?, total_bytes=?, "
+ "opened=? WHERE id=?"));
+ BindFilePath(statement, data.path, 0);
+ statement.BindInt64(1, data.received_bytes);
+ statement.BindInt(2, state);
+ statement.BindInt64(3, data.end_time.ToTimeT());
+ statement.BindInt(4, data.total_bytes);
+ statement.BindInt(5, (data.opened ? 1 : 0));
+ statement.BindInt64(6, data.db_handle);
return statement.Run();
}
bool DownloadDatabase::CleanUpInProgressEntries() {
- CheckThread();
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE downloads SET state=? WHERE state=?"));
statement.BindInt(0, kStateCancelled);
@@ -237,8 +215,6 @@ bool DownloadDatabase::CleanUpInProgressEntries() {
int64 DownloadDatabase::CreateDownload(
const DownloadPersistentStoreInfo& info) {
- CheckThread();
-
if (next_db_handle_ == 0) {
// This is unlikely. All current known tests and users already call
// QueryDownloads() before CreateDownload().
@@ -249,7 +225,7 @@ int64 DownloadDatabase::CreateDownload(
int state = StateToInt(info.state);
if (state == kStateInvalid)
- return false;
+ return kUninitializedHandle;
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"INSERT INTO downloads "
@@ -275,75 +251,21 @@ int64 DownloadDatabase::CreateDownload(
return db_handle;
}
- return 0;
+ return kUninitializedHandle;
}
-void DownloadDatabase::RemoveDownload(DownloadID db_handle) {
- CheckThread();
-
+void DownloadDatabase::RemoveDownload(int64 handle) {
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"DELETE FROM downloads WHERE id=?"));
- statement.BindInt64(0, db_handle);
-
+ statement.BindInt64(0, handle);
statement.Run();
}
-bool DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin,
- base::Time delete_end) {
- CheckThread();
- time_t start_time = delete_begin.ToTimeT();
- time_t end_time = delete_end.ToTimeT();
-
- int num_downloads_deleted = -1;
- {
- sql::Statement count(GetDB().GetCachedStatement(SQL_FROM_HERE,
- "SELECT count(*) FROM downloads WHERE start_time >= ? "
- "AND start_time < ? AND (State = ? OR State = ? OR State = ?)"));
- count.BindInt64(0, start_time);
- count.BindInt64(
- 1,
- end_time ? end_time : std::numeric_limits<int64>::max());
- count.BindInt(2, kStateComplete);
- count.BindInt(3, kStateCancelled);
- count.BindInt(4, kStateInterrupted);
- if (count.Step())
- num_downloads_deleted = count.ColumnInt(0);
- }
-
-
- bool success = false;
- base::TimeTicks started_removing = base::TimeTicks::Now();
- {
- // This does not use an index. We currently aren't likely to have enough
- // downloads where an index by time will give us a lot of benefit.
- sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
- "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? "
- "AND (State = ? OR State = ? OR State = ?)"));
- statement.BindInt64(0, start_time);
- statement.BindInt64(
- 1,
- end_time ? end_time : std::numeric_limits<int64>::max());
- statement.BindInt(2, kStateComplete);
- statement.BindInt(3, kStateCancelled);
- statement.BindInt(4, kStateInterrupted);
-
- success = statement.Run();
- }
-
- base::TimeTicks finished_removing = base::TimeTicks::Now();
-
- if (num_downloads_deleted >= 0) {
- UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount",
- num_downloads_deleted);
- base::TimeDelta micros = (1000 * (finished_removing - started_removing));
- UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros);
- if (num_downloads_deleted > 0) {
- UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord",
- (1000 * micros) / num_downloads_deleted);
- }
- }
-
- return success;
+int DownloadDatabase::CountDownloads() {
+ sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
+ "SELECT count(*) from downloads"));
+ statement.Step();
+ return statement.ColumnInt(0);
}
} // namespace history

Powered by Google App Engine
This is Rietveld 408576698