Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/memory/scoped_ptr.h" | |
| 13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/stringprintf.h" | |
| 14 #include "base/time.h" | 17 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 16 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 17 #include "chrome/browser/history/download_row.h" | 20 #include "chrome/browser/history/download_row.h" |
| 21 #include "chrome/browser/history/history_types.h" | |
| 22 #include "content/public/browser/download_interrupt_reasons.h" | |
| 18 #include "content/public/browser/download_item.h" | 23 #include "content/public/browser/download_item.h" |
| 19 #include "sql/statement.h" | 24 #include "sql/statement.h" |
| 20 | 25 |
| 21 using content::DownloadItem; | 26 using content::DownloadItem; |
| 22 | 27 |
| 23 namespace history { | 28 namespace history { |
| 24 | 29 |
| 25 // static | 30 // static |
| 26 const int64 DownloadDatabase::kUninitializedHandle = -1; | 31 const int64 DownloadDatabase::kUninitializedHandle = -1; |
| 27 | 32 |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 30 static const char kSchema[] = | 35 static const char kSchema[] = |
| 31 "CREATE TABLE downloads (" | 36 "CREATE TABLE downloads (" |
| 32 "id INTEGER PRIMARY KEY," // SQLite-generated primary key. | 37 "id INTEGER PRIMARY KEY," // Primary key. |
| 33 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk. | 38 "current_path LONGVARCHAR NOT NULL," // Current disk location of the download |
| 34 "url LONGVARCHAR NOT NULL," // URL of the downloaded file. | 39 "target_path LONGVARCHAR NOT NULL," // Final disk location of the download |
| 35 "start_time INTEGER NOT NULL," // When the download was started. | 40 "start_time INTEGER NOT NULL," // When the download was started. |
| 36 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 41 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
| 37 "total_bytes INTEGER NOT NULL," // Total size of the download. | 42 "total_bytes INTEGER NOT NULL," // Total size of the download. |
| 38 "state INTEGER NOT NULL," // 1=complete, 2=cancelled, 4=interrupted | 43 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
| 44 "danger_type INTEGER NOT NULL, " // Not dangerous, danger type, validated. | |
| 45 "interrupt_reason INTEGER NOT NULL," // Reason the download was interrupted. | |
| 39 "end_time INTEGER NOT NULL," // When the download completed. | 46 "end_time INTEGER NOT NULL," // When the download completed. |
| 40 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 | 47 "opened INTEGER NOT NULL)"; // 1 if it has ever been opened else 0 |
| 41 | 48 |
| 49 static const char kUrlChainSchema[] = | |
| 50 "CREATE TABLE downloads_url_chains (" | |
| 51 "id INTEGER NOT NULL," // downloads.id. | |
| 52 "chain_index INTEGER NOT NULL," // Index of url in chain | |
| 53 // 0 is initial target, | |
| 54 // MAX is target after redirects. | |
| 55 "url LONGVARCHAR NOT NULL, " // URL. | |
| 56 "PRIMARY KEY (id, chain_index) )"; | |
| 57 | |
| 42 // These constants and next two functions are used to allow | 58 // These constants and next two functions are used to allow |
| 43 // DownloadItem::DownloadState to change without breaking the database schema. | 59 // DownloadItem::DownloadState and DownloadDangerType to change without |
| 60 // breaking the database schema. | |
| 44 // They guarantee that the values of the |state| field in the database are one | 61 // They guarantee that the values of the |state| field in the database are one |
| 45 // of the values returned by StateToInt, and that the values of the |state| | 62 // of the values returned by StateToInt, and that the values of the |state| |
| 46 // field of the DownloadRows returned by QueryDownloads() are one of the values | 63 // field of the DownloadRows returned by QueryDownloads() are one of the values |
| 47 // returned by IntToState(). | 64 // returned by IntToState(). |
| 48 static const int kStateInvalid = -1; | 65 static const int kStateInvalid = -1; |
| 49 static const int kStateInProgress = 0; | 66 static const int kStateInProgress = 0; |
| 50 static const int kStateComplete = 1; | 67 static const int kStateComplete = 1; |
| 51 static const int kStateCancelled = 2; | 68 static const int kStateCancelled = 2; |
| 52 static const int kStateBug140687 = 3; | 69 static const int kStateBug140687 = 3; |
| 53 static const int kStateInterrupted = 4; | 70 static const int kStateInterrupted = 4; |
| 54 | 71 |
| 72 static const int kDangerTypeInvalid = -1; | |
| 73 static const int kDangerTypeNotDangerous = 0; | |
| 74 static const int kDangerTypeDangerousFile = 1; | |
| 75 static const int kDangerTypeDangerousUrl = 2; | |
| 76 static const int kDangerTypeDangerousContent = 3; | |
| 77 static const int kDangerTypeMaybeDangerousContent = 4; | |
| 78 static const int kDangerTypeUncommonContent = 5; | |
| 79 static const int kDangerTypeUserValidated = 6; | |
| 80 | |
| 55 int StateToInt(DownloadItem::DownloadState state) { | 81 int StateToInt(DownloadItem::DownloadState state) { |
| 56 switch (state) { | 82 switch (state) { |
| 57 case DownloadItem::IN_PROGRESS: return kStateInProgress; | 83 case DownloadItem::IN_PROGRESS: return kStateInProgress; |
| 58 case DownloadItem::COMPLETE: return kStateComplete; | 84 case DownloadItem::COMPLETE: return kStateComplete; |
| 59 case DownloadItem::CANCELLED: return kStateCancelled; | 85 case DownloadItem::CANCELLED: return kStateCancelled; |
| 60 case DownloadItem::INTERRUPTED: return kStateInterrupted; | 86 case DownloadItem::INTERRUPTED: return kStateInterrupted; |
| 61 case DownloadItem::MAX_DOWNLOAD_STATE: return kStateInvalid; | 87 case DownloadItem::MAX_DOWNLOAD_STATE: return kStateInvalid; |
|
benjhayden
2013/01/23 21:02:06
Do we want NOTREACHED() here and after the next li
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
Done.
benjhayden
2013/01/25 20:03:47
Looks like you added them to IntToState but not St
Randy Smith (Not in Mondays)
2013/01/25 20:37:21
Ooops; right. Should have been the other way arou
| |
| 62 default: return kStateInvalid; | |
| 63 } | 88 } |
| 89 return kStateInvalid; | |
| 64 } | 90 } |
| 65 | 91 |
| 66 DownloadItem::DownloadState IntToState(int state) { | 92 DownloadItem::DownloadState IntToState(int state) { |
| 67 switch (state) { | 93 switch (state) { |
| 68 case kStateInProgress: return DownloadItem::IN_PROGRESS; | 94 case kStateInProgress: return DownloadItem::IN_PROGRESS; |
| 69 case kStateComplete: return DownloadItem::COMPLETE; | 95 case kStateComplete: return DownloadItem::COMPLETE; |
| 70 case kStateCancelled: return DownloadItem::CANCELLED; | 96 case kStateCancelled: return DownloadItem::CANCELLED; |
| 71 // We should not need kStateBug140687 here because MigrateDownloadState() | 97 // We should not need kStateBug140687 here because MigrateDownloadsState() |
| 72 // is called in HistoryDatabase::Init(). | 98 // is called in HistoryDatabase::Init(). |
| 73 case kStateInterrupted: return DownloadItem::INTERRUPTED; | 99 case kStateInterrupted: return DownloadItem::INTERRUPTED; |
| 74 default: return DownloadItem::MAX_DOWNLOAD_STATE; | 100 default: return DownloadItem::MAX_DOWNLOAD_STATE; |
| 75 } | 101 } |
| 76 } | 102 } |
| 77 | 103 |
| 104 int DangerTypeToInt(content::DownloadDangerType danger_type) { | |
| 105 switch (danger_type) { | |
| 106 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | |
| 107 return kDangerTypeNotDangerous; | |
| 108 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: | |
| 109 return kDangerTypeDangerousFile; | |
| 110 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
| 111 return kDangerTypeDangerousUrl; | |
| 112 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
| 113 return kDangerTypeDangerousContent; | |
| 114 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | |
| 115 return kDangerTypeMaybeDangerousContent; | |
| 116 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: | |
| 117 return kDangerTypeUncommonContent; | |
| 118 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | |
| 119 return kDangerTypeUserValidated; | |
| 120 case content::DOWNLOAD_DANGER_TYPE_MAX: | |
| 121 NOTREACHED(); | |
| 122 return kDangerTypeInvalid; | |
| 123 } | |
| 124 NOTREACHED(); | |
| 125 return kDangerTypeInvalid; | |
| 126 } | |
| 127 | |
| 128 content::DownloadDangerType IntToDangerType(int danger_type) { | |
| 129 switch (danger_type) { | |
| 130 case kDangerTypeNotDangerous: | |
| 131 return content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; | |
| 132 case kDangerTypeDangerousFile: | |
| 133 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | |
| 134 case kDangerTypeDangerousUrl: | |
| 135 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | |
| 136 case kDangerTypeDangerousContent: | |
| 137 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; | |
| 138 case kDangerTypeMaybeDangerousContent: | |
| 139 return content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT; | |
| 140 case kDangerTypeUncommonContent: | |
| 141 return content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT; | |
| 142 case kDangerTypeUserValidated: | |
| 143 return content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED; | |
| 144 default: | |
| 145 return content::DOWNLOAD_DANGER_TYPE_MAX; | |
| 146 } | |
| 147 } | |
| 148 | |
| 78 #if defined(OS_POSIX) | 149 #if defined(OS_POSIX) |
| 79 | 150 |
| 80 // Binds/reads the given file path to the given column of the given statement. | 151 // Binds/reads the given file path to the given column of the given statement. |
| 81 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { | 152 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { |
| 82 statement.BindString(col, path.value()); | 153 statement.BindString(col, path.value()); |
| 83 } | 154 } |
| 84 FilePath ColumnFilePath(sql::Statement& statement, int col) { | 155 FilePath ColumnFilePath(sql::Statement& statement, int col) { |
| 85 return FilePath(statement.ColumnString(col)); | 156 return FilePath(statement.ColumnString(col)); |
| 86 } | 157 } |
| 87 | 158 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 } | 192 } |
| 122 | 193 |
| 123 bool DownloadDatabase::MigrateDownloadsState() { | 194 bool DownloadDatabase::MigrateDownloadsState() { |
| 124 sql::Statement statement(GetDB().GetUniqueStatement( | 195 sql::Statement statement(GetDB().GetUniqueStatement( |
| 125 "UPDATE downloads SET state=? WHERE state=?")); | 196 "UPDATE downloads SET state=? WHERE state=?")); |
| 126 statement.BindInt(0, kStateInterrupted); | 197 statement.BindInt(0, kStateInterrupted); |
| 127 statement.BindInt(1, kStateBug140687); | 198 statement.BindInt(1, kStateBug140687); |
| 128 return statement.Run(); | 199 return statement.Run(); |
| 129 } | 200 } |
| 130 | 201 |
| 202 bool DownloadDatabase::MigrateDownloadsReasonPathsAndDangerType() { | |
| 203 // We need to rename the table and copy back from it because SQLite | |
| 204 // provides no way to rename or delete a column. | |
| 205 if (!GetDB().Execute("ALTER TABLE downloads RENAME TO downloads_tmp")) | |
|
benjhayden
2013/01/23 21:02:06
Is this method called in a transaction?
If not, wo
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
It is wrapped in a transaction. See HistoryDataba
| |
| 206 return false; | |
| 207 | |
| 208 // Recreate main table. | |
| 209 if (!GetDB().Execute(kSchema)) | |
| 210 return false; | |
| 211 | |
| 212 // Populate it. As we do so, we transform the time values from time_t | |
| 213 // (seconds since 1/1/1970 UTC), to our internal measure (microseconds | |
| 214 // since the Windows Epoch). Note that this is dependent on the | |
| 215 // internal representation of base::Time and needs to change if that changes. | |
| 216 sql::Statement statement_populate(GetDB().GetUniqueStatement( | |
| 217 "INSERT INTO downloads " | |
| 218 "( id, current_path, target_path, start_time, received_bytes, total_bytes, " | |
| 219 " state, danger_type, interrupt_reason, end_time, opened ) " | |
| 220 "SELECT id, full_path, full_path, " | |
| 221 " CASE start_time WHEN 0 THEN 0 ELSE " | |
| 222 " (start_time + 11644473600) * 1000000 END, " | |
| 223 " received_bytes, total_bytes, " | |
| 224 " state, ?, ?, " | |
| 225 " CASE end_time WHEN 0 THEN 0 ELSE " | |
| 226 " (end_time + 11644473600) * 1000000 END, " | |
| 227 " opened " | |
| 228 "FROM downloads_tmp")); | |
| 229 statement_populate.BindInt(0, content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
|
benjhayden
2013/01/23 21:02:06
Is there any code anywhere that assumes that, if t
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
I would argue from first principles that there can
| |
| 230 statement_populate.BindInt(1, kDangerTypeNotDangerous); | |
| 231 if (!statement_populate.Run()) | |
| 232 return false; | |
| 233 | |
| 234 // Create new chain table and populate it. | |
| 235 if (!GetDB().Execute(kUrlChainSchema)) | |
| 236 return false; | |
| 237 | |
| 238 if (!GetDB().Execute("INSERT INTO downloads_url_chains " | |
| 239 " ( id, chain_index, url) " | |
| 240 " SELECT id, 0, url from downloads_tmp")) | |
| 241 return false; | |
| 242 | |
| 243 // Get rid of temporary table. | |
| 244 if (!GetDB().Execute("DROP TABLE downloads_tmp")) | |
|
benjhayden
2013/01/23 21:02:06
Is this something that we want to do regardless of
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
I'm slightly confused. If any previous step in th
benjhayden
2013/01/25 20:03:47
Sorry. I saw that this might not be run and was wo
Randy Smith (Not in Mondays)
2013/01/25 20:37:21
Fair enough. I think that's taken care of by the
| |
| 245 return false; | |
| 246 | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 131 bool DownloadDatabase::InitDownloadTable() { | 250 bool DownloadDatabase::InitDownloadTable() { |
| 132 GetMetaTable().GetValue(kNextDownloadId, &next_id_); | 251 GetMetaTable().GetValue(kNextDownloadId, &next_id_); |
| 133 if (GetDB().DoesTableExist("downloads")) { | 252 if (GetDB().DoesTableExist("downloads")) { |
| 134 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && | 253 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| 135 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); | 254 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
| 136 } else { | 255 } else { |
| 137 return GetDB().Execute(kSchema); | 256 // If the "downloads" table doesn't exist, the downloads_url_chain |
| 257 // table better not. | |
| 258 return (!GetDB().DoesTableExist("downloads_url_chain") && | |
| 259 GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); | |
| 138 } | 260 } |
| 139 } | 261 } |
| 140 | 262 |
| 141 bool DownloadDatabase::DropDownloadTable() { | 263 bool DownloadDatabase::DropDownloadTable() { |
| 142 return GetDB().Execute("DROP TABLE downloads"); | 264 return GetDB().Execute("DROP TABLE downloads"); |
| 143 } | 265 } |
| 144 | 266 |
| 145 void DownloadDatabase::QueryDownloads( | 267 void DownloadDatabase::QueryDownloads( |
| 146 std::vector<DownloadRow>* results) { | 268 std::vector<DownloadRow>* results) { |
| 147 DCHECK(results); | |
| 148 results->clear(); | 269 results->clear(); |
| 149 if (next_db_handle_ < 1) | 270 if (next_db_handle_ < 1) |
| 150 next_db_handle_ = 1; | 271 next_db_handle_ = 1; |
| 151 std::set<int64> db_handles; | 272 std::set<int64> db_handles; |
| 152 | 273 |
| 153 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 274 std::map<DownloadID, DownloadRow*> info_map; |
| 154 "SELECT id, full_path, url, start_time, received_bytes, " | 275 |
| 155 "total_bytes, state, end_time, opened " | 276 sql::Statement statement_main(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 277 "SELECT id, current_path, target_path, start_time, received_bytes, " | |
| 278 "total_bytes, state, danger_type, interrupt_reason, end_time, opened " | |
| 156 "FROM downloads " | 279 "FROM downloads " |
| 157 "ORDER BY start_time")); | 280 "ORDER BY start_time")); |
| 158 | 281 |
| 159 while (statement.Step()) { | 282 while (statement_main.Step()) { |
| 160 DownloadRow info; | 283 scoped_ptr<DownloadRow> info(new DownloadRow()); |
| 161 info.db_handle = statement.ColumnInt64(0); | 284 int column = 0; |
| 162 info.path = ColumnFilePath(statement, 1); | 285 |
| 163 info.url = GURL(statement.ColumnString(2)); | 286 int db_handle = statement_main.ColumnInt64(column++); |
| 164 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); | 287 info->db_handle = db_handle; |
| 165 info.received_bytes = statement.ColumnInt64(4); | 288 info->current_path = ColumnFilePath(statement_main, column++); |
| 166 info.total_bytes = statement.ColumnInt64(5); | 289 info->target_path = ColumnFilePath(statement_main, column++); |
| 167 int state = statement.ColumnInt(6); | 290 info->start_time = base::Time::FromInternalValue( |
| 168 info.state = IntToState(state); | 291 statement_main.ColumnInt64(column++)); |
| 169 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7)); | 292 info->received_bytes = statement_main.ColumnInt64(column++); |
| 170 info.opened = statement.ColumnInt(8) != 0; | 293 info->total_bytes = statement_main.ColumnInt64(column++); |
| 171 if (info.db_handle >= next_db_handle_) | 294 int state = statement_main.ColumnInt(column++); |
| 172 next_db_handle_ = info.db_handle + 1; | 295 info->state = IntToState(state); |
| 173 if (!db_handles.insert(info.db_handle).second) { | 296 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) |
| 174 // info.db_handle was already in db_handles. The database is corrupt. | 297 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
| 175 base::debug::Alias(&info.db_handle); | 298 info->danger_type = IntToDangerType(statement_main.ColumnInt(column++)); |
| 299 info->interrupt_reason = static_cast<content::DownloadInterruptReason>( | |
| 300 statement_main.ColumnInt(column++)); | |
| 301 info->end_time = base::Time::FromInternalValue( | |
| 302 statement_main.ColumnInt64(column++)); | |
| 303 info->opened = statement_main.ColumnInt(column++) != 0; | |
| 304 if (info->db_handle >= next_db_handle_) | |
| 305 next_db_handle_ = info->db_handle + 1; | |
| 306 if (!db_handles.insert(info->db_handle).second) { | |
| 307 // info->db_handle was already in db_handles. The database is corrupt. | |
| 308 base::debug::Alias(&info->db_handle); | |
| 176 DCHECK(false); | 309 DCHECK(false); |
| 177 } | 310 } |
| 178 if (info.state == DownloadItem::MAX_DOWNLOAD_STATE) { | 311 |
| 179 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 312 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE || |
| 313 info->danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { | |
| 314 DroppedReason reason = | |
| 315 (info->state == DownloadItem::MAX_DOWNLOAD_STATE ? | |
|
benjhayden
2013/01/23 21:02:06
Sometimes I wonder if you're just making fun of ho
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
I try not to be subtle when I make fun of people--
benjhayden
2013/01/25 20:03:47
What about this?
if (state...) {
} else if (dange
Randy Smith (Not in Mondays)
2013/01/25 20:37:21
I'm willing if you want, but I'd prefer the curren
| |
| 316 DROPPED_REASON_BAD_STATE : | |
| 317 info->danger_type == content::DOWNLOAD_DANGER_TYPE_MAX ? | |
| 318 DROPPED_REASON_BAD_DANGER_TYPE : DROPPED_REASON_MAX); | |
| 319 DCHECK_NE(DROPPED_REASON_MAX, reason); | |
| 320 | |
| 321 UMA_HISTOGRAM_ENUMERATION( | |
| 322 "Download.DatabaseRecordDropped", reason, DROPPED_REASON_MAX + 1); | |
|
benjhayden
2013/01/23 21:02:06
histograms.xml CL?
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
It's in progress; I was going to upload it before
| |
| 323 | |
| 180 continue; | 324 continue; |
| 181 } | 325 } |
| 182 results->push_back(info); | 326 |
| 327 DCHECK(!ContainsKey(info_map, info->db_handle)); | |
| 328 info_map[db_handle] = info.release(); | |
| 329 } | |
| 330 | |
| 331 sql::Statement statement_chain(GetDB().GetCachedStatement( | |
| 332 SQL_FROM_HERE, | |
| 333 "SELECT id, chain_index, url FROM downloads_url_chains " | |
| 334 "ORDER BY id, chain_index")); | |
| 335 | |
| 336 while (statement_chain.Step()) { | |
| 337 int column = 0; | |
| 338 int64 db_handle = statement_chain.ColumnInt64(column++); | |
| 339 int chain_index = statement_chain.ColumnInt(column++); | |
| 340 | |
| 341 // Note that these DCHECKs may trip as a result of corrupted databases. | |
| 342 // We have them because in debug builds the chances are higher there's | |
| 343 // an actual bug than that the database is corrupt, but we handle the | |
| 344 // DB corruption case in production code. | |
| 345 | |
| 346 // Confirm the handle has already been seen--if it hasn't, discard the | |
| 347 // record. | |
| 348 DCHECK(ContainsKey(info_map, db_handle)); | |
| 349 if (!ContainsKey(info_map, db_handle)) | |
| 350 continue; | |
| 351 | |
| 352 // Confirm all previous URLs in the chain have already been seen; | |
| 353 // if not, fill in with null or discard record. | |
| 354 int current_chain_size = info_map[db_handle]->url_chain.size(); | |
| 355 std::vector<GURL>* url_chain(&info_map[db_handle]->url_chain); | |
| 356 DCHECK_EQ(chain_index, current_chain_size); | |
| 357 while (current_chain_size < chain_index) { | |
| 358 url_chain->push_back(GURL()); | |
| 359 current_chain_size++; | |
| 360 } | |
| 361 if (current_chain_size > chain_index) | |
| 362 continue; | |
|
benjhayden
2013/01/23 21:02:06
Why discard the url instead of assigning it to its
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
I think we covered this in https://codereview.chro
benjhayden
2013/01/25 20:03:47
Ah, nope, lgtm.
| |
| 363 | |
| 364 // Save the record. | |
| 365 url_chain->push_back(GURL(statement_chain.ColumnString(2))); | |
| 366 } | |
| 367 | |
| 368 for (std::map<DownloadID, DownloadRow*>::iterator | |
| 369 it = info_map.begin(); it != info_map.end(); ++it) { | |
| 370 // Copy the contents of the stored info. | |
| 371 results->push_back(*it->second); | |
| 372 delete it->second; | |
| 373 it->second = NULL; | |
| 183 } | 374 } |
| 184 } | 375 } |
| 185 | 376 |
| 186 bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { | 377 bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { |
| 187 DCHECK(data.db_handle > 0); | 378 DCHECK(data.db_handle > 0); |
| 188 int state = StateToInt(data.state); | 379 int state = StateToInt(data.state); |
| 189 if (state == kStateInvalid) { | 380 if (state == kStateInvalid) { |
| 190 // TODO(benjhayden) [D]CHECK instead. | 381 // TODO(benjhayden) [D]CHECK instead. |
| 191 return false; | 382 return false; |
| 192 } | 383 } |
| 193 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 384 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 194 "UPDATE downloads " | 385 "UPDATE downloads " |
| 195 "SET full_path=?, received_bytes=?, state=?, end_time=?, total_bytes=?, " | 386 "SET current_path=?, target_path=?, received_bytes=?, state=?, " |
| 196 "opened=? WHERE id=?")); | 387 "danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, " |
| 197 BindFilePath(statement, data.path, 0); | 388 "opened=? WHERE id=?")); |
| 198 statement.BindInt64(1, data.received_bytes); | 389 int column = 0; |
| 199 statement.BindInt(2, state); | 390 BindFilePath(statement, data.current_path, column++); |
| 200 statement.BindInt64(3, data.end_time.ToTimeT()); | 391 BindFilePath(statement, data.target_path, column++); |
| 201 statement.BindInt(4, data.total_bytes); | 392 statement.BindInt64(column++, data.received_bytes); |
| 202 statement.BindInt(5, (data.opened ? 1 : 0)); | 393 statement.BindInt(column++, state); |
| 203 statement.BindInt64(6, data.db_handle); | 394 statement.BindInt(column++, DangerTypeToInt(data.danger_type)); |
|
benjhayden
2013/01/23 21:02:06
Do you want to move the DangerTypeToInt up to join
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
Yeah. Any reason for me not to switch them both t
| |
| 395 statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); | |
| 396 statement.BindInt64(column++, data.end_time.ToInternalValue()); | |
| 397 statement.BindInt(column++, data.total_bytes); | |
| 398 statement.BindInt(column++, (data.opened ? 1 : 0)); | |
| 399 statement.BindInt64(column++, data.db_handle); | |
| 204 | 400 |
| 205 return statement.Run(); | 401 return statement.Run(); |
| 206 } | 402 } |
| 207 | 403 |
| 208 bool DownloadDatabase::CleanUpInProgressEntries() { | 404 bool DownloadDatabase::CleanUpInProgressEntries() { |
| 209 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 405 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 210 "UPDATE downloads SET state=? WHERE state=?")); | 406 "UPDATE downloads SET state=? WHERE state=?")); |
| 211 statement.BindInt(0, kStateCancelled); | 407 statement.BindInt(0, kStateCancelled); |
| 212 statement.BindInt(1, kStateInProgress); | 408 statement.BindInt(1, kStateInProgress); |
| 213 | 409 |
| 214 return statement.Run(); | 410 return statement.Run(); |
| 215 } | 411 } |
| 216 | 412 |
| 217 int64 DownloadDatabase::CreateDownload( | 413 int64 DownloadDatabase::CreateDownload( |
| 218 const DownloadRow& info) { | 414 const DownloadRow& info) { |
| 219 if (next_db_handle_ == 0) { | 415 if (next_db_handle_ == 0) { |
| 220 // This is unlikely. All current known tests and users already call | 416 // This is unlikely. All current known tests and users already call |
| 221 // QueryDownloads() before CreateDownload(). | 417 // QueryDownloads() before CreateDownload(). |
| 222 std::vector<DownloadRow> results; | 418 std::vector<DownloadRow> results; |
| 223 QueryDownloads(&results); | 419 QueryDownloads(&results); |
| 224 CHECK_NE(0, next_db_handle_); | 420 CHECK_NE(0, next_db_handle_); |
| 225 } | 421 } |
| 226 | 422 |
| 227 int state = StateToInt(info.state); | 423 int state = StateToInt(info.state); |
| 228 if (state == kStateInvalid) | 424 if (state == kStateInvalid) |
| 229 return kUninitializedHandle; | 425 return kUninitializedHandle; |
| 230 | 426 |
| 231 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 427 int danger_type = DangerTypeToInt(info.danger_type); |
| 232 "INSERT INTO downloads " | 428 if (danger_type == kDangerTypeInvalid) |
| 233 "(id, full_path, url, start_time, received_bytes, total_bytes, state, " | 429 return kUninitializedHandle; |
| 234 "end_time, opened) " | |
| 235 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); | |
| 236 | 430 |
| 237 int db_handle = next_db_handle_++; | 431 int db_handle = next_db_handle_++; |
| 238 | 432 |
| 239 statement.BindInt64(0, db_handle); | 433 { |
| 240 BindFilePath(statement, info.path, 1); | 434 sql::Statement statement_insert(GetDB().GetCachedStatement( |
| 241 statement.BindString(2, info.url.spec()); | 435 SQL_FROM_HERE, |
| 242 statement.BindInt64(3, info.start_time.ToTimeT()); | 436 "INSERT INTO downloads " |
| 243 statement.BindInt64(4, info.received_bytes); | 437 "(id, current_path, target_path, start_time, " |
| 244 statement.BindInt64(5, info.total_bytes); | 438 " received_bytes, total_bytes, state, danger_type, interrupt_reason, " |
| 245 statement.BindInt(6, state); | 439 " end_time, opened) " |
| 246 statement.BindInt64(7, info.end_time.ToTimeT()); | 440 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 247 statement.BindInt(8, info.opened ? 1 : 0); | |
| 248 | 441 |
| 249 if (statement.Run()) { | 442 int column = 0; |
| 250 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | 443 statement_insert.BindInt64(column++, db_handle); |
| 251 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); | 444 BindFilePath(statement_insert, info.current_path, column++); |
| 445 BindFilePath(statement_insert, info.target_path, column++); | |
| 446 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | |
| 447 statement_insert.BindInt64(column++, info.received_bytes); | |
| 448 statement_insert.BindInt64(column++, info.total_bytes); | |
| 449 statement_insert.BindInt(column++, state); | |
| 450 statement_insert.BindInt(column++, danger_type); | |
| 451 statement_insert.BindInt(column++, content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
|
benjhayden
2013/01/23 21:02:06
Why not copy the interrupt_reason from info here?
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
Huh. I have no idea. Copy paste error, best gues
| |
| 452 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | |
| 453 statement_insert.BindInt(column++, info.opened ? 1 : 0); | |
| 454 if (!statement_insert.Run()) { | |
| 455 LOG(WARNING) << "Main insertion for download create failed."; | |
| 456 return kUninitializedHandle; | |
| 457 } | |
| 458 } | |
| 252 | 459 |
| 253 return db_handle; | 460 sql::Statement statement_insert_chain( |
| 461 GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 462 "INSERT INTO downloads_url_chains " | |
| 463 "(id, chain_index, url) " | |
| 464 "VALUES (?, ?, ?)")); | |
| 465 for (size_t i = 0; i < info.url_chain.size(); ++i) { | |
| 466 statement_insert_chain.BindInt64(0, db_handle); | |
| 467 statement_insert_chain.BindInt(1, i); | |
| 468 statement_insert_chain.BindString(2, info.url_chain[i].spec()); | |
| 469 if (!statement_insert_chain.Run()) { | |
| 470 LOG(WARNING) << "Url insertion for download create failed."; | |
| 471 return kUninitializedHandle; | |
| 472 } | |
| 473 statement_insert_chain.Reset(true); | |
| 254 } | 474 } |
| 255 return kUninitializedHandle; | 475 |
| 476 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} | |
| 477 GetMetaTable().SetValue(kNextDownloadId, ++next_id_); | |
| 478 | |
| 479 return db_handle; | |
| 256 } | 480 } |
| 257 | 481 |
| 258 void DownloadDatabase::RemoveDownload(int64 handle) { | 482 void DownloadDatabase::RemoveDownload(int64 handle) { |
| 259 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 483 sql::Statement downloads_statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 260 "DELETE FROM downloads WHERE id=?")); | 484 "DELETE FROM downloads WHERE id=?")); |
| 261 statement.BindInt64(0, handle); | 485 downloads_statement.BindInt64(0, handle); |
| 262 statement.Run(); | 486 downloads_statement.Run(); |
| 487 | |
| 488 sql::Statement urlchain_statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 489 "DELETE FROM downloads_url_chains WHERE id=?")); | |
| 490 urlchain_statement.BindInt64(0, handle); | |
| 491 urlchain_statement.Run(); | |
| 263 } | 492 } |
| 264 | 493 |
| 265 int DownloadDatabase::CountDownloads() { | 494 int DownloadDatabase::CountDownloads() { |
| 266 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 495 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 267 "SELECT count(*) from downloads")); | 496 "SELECT count(*) from downloads")); |
| 268 statement.Step(); | 497 statement.Step(); |
| 269 return statement.ColumnInt(0); | 498 return statement.ColumnInt(0); |
| 270 } | 499 } |
| 271 | 500 |
| 272 } // namespace history | 501 } // namespace history |
| OLD | NEW |