| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 14 #include "content/browser/download/download_item.h" | 15 #include "content/browser/download/download_item.h" |
| 15 #include "content/browser/download/download_persistent_store_info.h" | 16 #include "content/browser/download/download_persistent_store_info.h" |
| 16 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 17 | 18 |
| 18 // Download schema: | 19 // Download schema: |
| 19 // | 20 // |
| 20 // id SQLite-generated primary key. | 21 // id SQLite-generated primary key. |
| 21 // full_path Location of the download on disk. | 22 // full_path Location of the download on disk. |
| 22 // url URL of the downloaded file. | 23 // url URL of the downloaded file. |
| 23 // start_time When the download was started. | 24 // start_time When the download was started. |
| 24 // received_bytes Total size downloaded. | 25 // received_bytes Total size downloaded. |
| 25 // total_bytes Total size of the download. | 26 // total_bytes Total size of the download. |
| 26 // state Identifies if this download is completed or not. Not used | 27 // state Identifies if this download is completed or not. Not used |
| 27 // directly by the history system. See DownloadItem's | 28 // directly by the history system. See DownloadItem's |
| 28 // DownloadState for where this is used. | 29 // DownloadState for where this is used. |
| 30 // end_time When the download completed. |
| 31 // opened 1 if the download has ever been opened else 0 |
| 29 | 32 |
| 30 namespace history { | 33 namespace history { |
| 31 | 34 |
| 32 namespace { | 35 namespace { |
| 33 | 36 |
| 34 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
| 35 | 38 |
| 36 // Binds/reads the given file path to the given column of the given statement. | 39 // Binds/reads the given file path to the given column of the given statement. |
| 37 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { | 40 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { |
| 38 statement.BindString(col, path.value()); | 41 statement.BindString(col, path.value()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 61 | 64 |
| 62 DownloadDatabase::DownloadDatabase() | 65 DownloadDatabase::DownloadDatabase() |
| 63 : owning_thread_set_(false), | 66 : owning_thread_set_(false), |
| 64 owning_thread_(0), | 67 owning_thread_(0), |
| 65 next_id_(0) { | 68 next_id_(0) { |
| 66 } | 69 } |
| 67 | 70 |
| 68 DownloadDatabase::~DownloadDatabase() { | 71 DownloadDatabase::~DownloadDatabase() { |
| 69 } | 72 } |
| 70 | 73 |
| 74 bool DownloadDatabase::EnsureColumnExists( |
| 75 const std::string& name, const std::string& type) { |
| 76 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; |
| 77 return GetDB().DoesColumnExist("downloads", name.c_str()) || |
| 78 GetDB().Execute(add_col.c_str()); |
| 79 } |
| 80 |
| 71 bool DownloadDatabase::InitDownloadTable() { | 81 bool DownloadDatabase::InitDownloadTable() { |
| 72 if (!GetDB().DoesTableExist("downloads")) { | 82 meta_table_.Init(&GetDB(), 0, 0); |
| 73 if (!GetDB().Execute( | 83 meta_table_.GetValue(kNextDownloadId, &next_id_); |
| 84 if (GetDB().DoesTableExist("downloads")) { |
| 85 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| 86 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
| 87 } else { |
| 88 return GetDB().Execute( |
| 74 "CREATE TABLE downloads (" | 89 "CREATE TABLE downloads (" |
| 75 "id INTEGER PRIMARY KEY," | 90 "id INTEGER PRIMARY KEY," |
| 76 "full_path LONGVARCHAR NOT NULL," | 91 "full_path LONGVARCHAR NOT NULL," |
| 77 "url LONGVARCHAR NOT NULL," | 92 "url LONGVARCHAR NOT NULL," |
| 78 "start_time INTEGER NOT NULL," | 93 "start_time INTEGER NOT NULL," |
| 79 "received_bytes INTEGER NOT NULL," | 94 "received_bytes INTEGER NOT NULL," |
| 80 "total_bytes INTEGER NOT NULL," | 95 "total_bytes INTEGER NOT NULL," |
| 81 "state INTEGER NOT NULL)")) | 96 "state INTEGER NOT NULL," |
| 82 return false; | 97 "end_time INTEGER NOT NULL," |
| 98 "opened INTEGER NOT NULL)"); |
| 83 } | 99 } |
| 84 meta_table_.Init(&GetDB(), 0, 0); | |
| 85 meta_table_.GetValue(kNextDownloadId, &next_id_); | |
| 86 return true; | |
| 87 } | 100 } |
| 88 | 101 |
| 89 bool DownloadDatabase::DropDownloadTable() { | 102 bool DownloadDatabase::DropDownloadTable() { |
| 90 return GetDB().Execute("DROP TABLE downloads"); | 103 return GetDB().Execute("DROP TABLE downloads"); |
| 91 } | 104 } |
| 92 | 105 |
| 93 void DownloadDatabase::QueryDownloads( | 106 void DownloadDatabase::QueryDownloads( |
| 94 std::vector<DownloadPersistentStoreInfo>* results) { | 107 std::vector<DownloadPersistentStoreInfo>* results) { |
| 95 results->clear(); | 108 results->clear(); |
| 96 | 109 |
| 97 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 110 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 98 "SELECT id, full_path, url, start_time, received_bytes, " | 111 "SELECT id, full_path, url, start_time, received_bytes, " |
| 99 "total_bytes, state " | 112 "total_bytes, state, end_time, opened " |
| 100 "FROM downloads " | 113 "FROM downloads " |
| 101 "ORDER BY start_time")); | 114 "ORDER BY start_time")); |
| 102 if (!statement) | 115 if (!statement) |
| 103 return; | 116 return; |
| 104 | 117 |
| 105 while (statement.Step()) { | 118 while (statement.Step()) { |
| 106 DownloadPersistentStoreInfo info; | 119 DownloadPersistentStoreInfo info; |
| 107 info.db_handle = statement.ColumnInt64(0); | 120 info.db_handle = statement.ColumnInt64(0); |
| 108 | 121 |
| 109 info.path = ColumnFilePath(statement, 1); | 122 info.path = ColumnFilePath(statement, 1); |
| 110 info.url = GURL(statement.ColumnString(2)); | 123 info.url = GURL(statement.ColumnString(2)); |
| 111 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); | 124 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); |
| 112 info.received_bytes = statement.ColumnInt64(4); | 125 info.received_bytes = statement.ColumnInt64(4); |
| 113 info.total_bytes = statement.ColumnInt64(5); | 126 info.total_bytes = statement.ColumnInt64(5); |
| 114 info.state = statement.ColumnInt(6); | 127 info.state = statement.ColumnInt(6); |
| 128 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7)); |
| 129 info.opened = statement.ColumnInt(8) != 0; |
| 115 results->push_back(info); | 130 results->push_back(info); |
| 116 } | 131 } |
| 117 } | 132 } |
| 118 | 133 |
| 119 bool DownloadDatabase::UpdateDownload(int64 received_bytes, | 134 bool DownloadDatabase::UpdateDownload(DownloadItemData data) { |
| 120 int32 state, | 135 DCHECK(data.db_handle > 0); |
| 121 DownloadID db_handle) { | |
| 122 DCHECK(db_handle > 0); | |
| 123 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 136 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 124 "UPDATE downloads " | 137 "UPDATE downloads " |
| 125 "SET received_bytes=?, state=? WHERE id=?")); | 138 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?")); |
| 126 if (!statement) | 139 if (!statement) |
| 127 return false; | 140 return false; |
| 128 | 141 |
| 129 statement.BindInt64(0, received_bytes); | 142 statement.BindInt64(0, data.received_bytes); |
| 130 statement.BindInt(1, state); | 143 statement.BindInt(1, data.state); |
| 131 statement.BindInt64(2, db_handle); | 144 statement.BindInt64(2, data.end_time.ToTimeT()); |
| 145 statement.BindInt(3, (data.opened ? 1 : 0)); |
| 146 statement.BindInt64(4, data.db_handle); |
| 132 return statement.Run(); | 147 return statement.Run(); |
| 133 } | 148 } |
| 134 | 149 |
| 135 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, | 150 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, |
| 136 DownloadID db_handle) { | 151 DownloadID db_handle) { |
| 137 DCHECK(db_handle > 0); | 152 DCHECK(db_handle > 0); |
| 138 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 153 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 139 "UPDATE downloads SET full_path=? WHERE id=?")); | 154 "UPDATE downloads SET full_path=? WHERE id=?")); |
| 140 if (!statement) | 155 if (!statement) |
| 141 return false; | 156 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 159 const DownloadPersistentStoreInfo& info) { | 174 const DownloadPersistentStoreInfo& info) { |
| 160 if (owning_thread_set_) { | 175 if (owning_thread_set_) { |
| 161 CHECK_EQ(owning_thread_, base::PlatformThread::CurrentId()); | 176 CHECK_EQ(owning_thread_, base::PlatformThread::CurrentId()); |
| 162 } else { | 177 } else { |
| 163 owning_thread_ = base::PlatformThread::CurrentId(); | 178 owning_thread_ = base::PlatformThread::CurrentId(); |
| 164 owning_thread_set_ = true; | 179 owning_thread_set_ = true; |
| 165 } | 180 } |
| 166 | 181 |
| 167 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 182 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 168 "INSERT INTO downloads " | 183 "INSERT INTO downloads " |
| 169 "(full_path, url, start_time, received_bytes, total_bytes, state) " | 184 "(full_path, url, start_time, received_bytes, total_bytes, state, " |
| 170 "VALUES (?, ?, ?, ?, ?, ?)")); | 185 "end_time, opened) " |
| 186 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); |
| 171 if (!statement) | 187 if (!statement) |
| 172 return 0; | 188 return 0; |
| 173 | 189 |
| 174 BindFilePath(statement, info.path, 0); | 190 BindFilePath(statement, info.path, 0); |
| 175 statement.BindString(1, info.url.spec()); | 191 statement.BindString(1, info.url.spec()); |
| 176 statement.BindInt64(2, info.start_time.ToTimeT()); | 192 statement.BindInt64(2, info.start_time.ToTimeT()); |
| 177 statement.BindInt64(3, info.received_bytes); | 193 statement.BindInt64(3, info.received_bytes); |
| 178 statement.BindInt64(4, info.total_bytes); | 194 statement.BindInt64(4, info.total_bytes); |
| 179 statement.BindInt(5, info.state); | 195 statement.BindInt(5, info.state); |
| 196 statement.BindInt64(6, info.end_time.ToTimeT()); |
| 197 statement.BindInt(7, info.opened ? 1 : 0); |
| 180 | 198 |
| 181 if (statement.Run()) { | 199 if (statement.Run()) { |
| 182 int64 id = GetDB().GetLastInsertRowId(); | 200 int64 id = GetDB().GetLastInsertRowId(); |
| 183 | 201 |
| 184 CHECK_EQ(0u, returned_ids_.count(id)); | 202 CHECK_EQ(0u, returned_ids_.count(id)); |
| 185 returned_ids_.insert(id); | 203 returned_ids_.insert(id); |
| 186 | 204 |
| 187 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | 205 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} |
| 188 meta_table_.SetValue(kNextDownloadId, ++next_id_); | 206 meta_table_.SetValue(kNextDownloadId, ++next_id_); |
| 189 | 207 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 statement.BindInt64( | 260 statement.BindInt64( |
| 243 1, | 261 1, |
| 244 end_time ? end_time : std::numeric_limits<int64>::max()); | 262 end_time ? end_time : std::numeric_limits<int64>::max()); |
| 245 statement.BindInt(2, DownloadItem::COMPLETE); | 263 statement.BindInt(2, DownloadItem::COMPLETE); |
| 246 statement.BindInt(3, DownloadItem::CANCELLED); | 264 statement.BindInt(3, DownloadItem::CANCELLED); |
| 247 statement.BindInt(4, DownloadItem::INTERRUPTED); | 265 statement.BindInt(4, DownloadItem::INTERRUPTED); |
| 248 statement.Run(); | 266 statement.Run(); |
| 249 } | 267 } |
| 250 | 268 |
| 251 } // namespace history | 269 } // namespace history |
| OLD | NEW |