| 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..73f456f77a23bb28471ddea26c4e43c057238185 100644
|
| --- a/chrome/browser/history/download_database.cc
|
| +++ b/chrome/browser/history/download_database.cc
|
| @@ -14,16 +14,18 @@
|
| #include "base/time.h"
|
| #include "base/utf_string_conversions.h"
|
| #include "build/build_config.h"
|
| +#include "chrome/browser/history/download_row.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 {
|
|
|
| +// static
|
| +const int64 DownloadDatabase::kUninitializedHandle = -1;
|
| +
|
| namespace {
|
|
|
| static const char kSchema[] =
|
| @@ -42,8 +44,8 @@ static const char kSchema[] =
|
| // DownloadItem::DownloadState to change without breaking the database schema.
|
| // They guarantee that the values of the |state| field in the database are one
|
| // of the values returned by StateToInt, and that the values of the |state|
|
| -// field of the DownloadPersistentStoreInfos returned by QueryDownloads() are
|
| -// one of the values returned by IntToState().
|
| +// field of the DownloadRows returned by QueryDownloads() are one of the values
|
| +// returned by IntToState().
|
| static const int kStateInvalid = -1;
|
| static const int kStateInProgress = 0;
|
| static const int kStateComplete = 1;
|
| @@ -112,15 +114,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 +130,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 +140,15 @@ bool DownloadDatabase::InitDownloadTable() {
|
| }
|
|
|
| bool DownloadDatabase::DropDownloadTable() {
|
| - CheckThread();
|
| return GetDB().Execute("DROP TABLE downloads");
|
| }
|
|
|
| void DownloadDatabase::QueryDownloads(
|
| - std::vector<DownloadPersistentStoreInfo>* results) {
|
| - CheckThread();
|
| + std::vector<DownloadRow>* results) {
|
| 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, "
|
| @@ -167,7 +157,7 @@ void DownloadDatabase::QueryDownloads(
|
| "ORDER BY start_time"));
|
|
|
| while (statement.Step()) {
|
| - DownloadPersistentStoreInfo info;
|
| + DownloadRow info;
|
| info.db_handle = statement.ColumnInt64(0);
|
| info.path = ColumnFilePath(statement, 1);
|
| info.url = GURL(statement.ColumnString(2));
|
| @@ -193,8 +183,7 @@ void DownloadDatabase::QueryDownloads(
|
| }
|
| }
|
|
|
| -bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
|
| - CheckThread();
|
| +bool DownloadDatabase::UpdateDownload(const DownloadRow& data) {
|
| DCHECK(data.db_handle > 0);
|
| int state = StateToInt(data.state);
|
| if (state == kStateInvalid) {
|
| @@ -203,30 +192,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);
|
| @@ -236,20 +215,18 @@ bool DownloadDatabase::CleanUpInProgressEntries() {
|
| }
|
|
|
| int64 DownloadDatabase::CreateDownload(
|
| - const DownloadPersistentStoreInfo& info) {
|
| - CheckThread();
|
| -
|
| + const DownloadRow& info) {
|
| if (next_db_handle_ == 0) {
|
| // This is unlikely. All current known tests and users already call
|
| // QueryDownloads() before CreateDownload().
|
| - std::vector<DownloadPersistentStoreInfo> results;
|
| + std::vector<DownloadRow> results;
|
| QueryDownloads(&results);
|
| CHECK_NE(0, next_db_handle_);
|
| }
|
|
|
| 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 +252,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
|
|
|