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 "components/history/core/browser/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/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "chrome/browser/history/download_row.h" | 20 #include "components/history/core/browser/download_constants.h" |
| 21 #include "components/history/core/browser/download_row.h" |
21 #include "components/history/core/browser/history_types.h" | 22 #include "components/history/core/browser/history_types.h" |
22 #include "content/public/browser/download_interrupt_reasons.h" | |
23 #include "content/public/browser/download_item.h" | |
24 #include "sql/statement.h" | 23 #include "sql/statement.h" |
25 | 24 |
26 using content::DownloadItem; | |
27 | |
28 namespace history { | 25 namespace history { |
29 | 26 |
30 namespace { | 27 namespace { |
31 | 28 |
32 // Reason for dropping a particular record. | 29 // Reason for dropping a particular record. |
33 enum DroppedReason { | 30 enum DroppedReason { |
34 DROPPED_REASON_BAD_STATE = 0, | 31 DROPPED_REASON_BAD_STATE = 0, |
35 DROPPED_REASON_BAD_DANGER_TYPE = 1, | 32 DROPPED_REASON_BAD_DANGER_TYPE = 1, |
36 DROPPED_REASON_BAD_ID = 2, | 33 DROPPED_REASON_BAD_ID = 2, |
37 DROPPED_REASON_DUPLICATE_ID = 3, | 34 DROPPED_REASON_DUPLICATE_ID = 3, |
(...skipping 19 matching lines...) Expand all Loading... |
57 statement.BindString16(col, path.value()); | 54 statement.BindString16(col, path.value()); |
58 } | 55 } |
59 base::FilePath ColumnFilePath(sql::Statement& statement, int col) { | 56 base::FilePath ColumnFilePath(sql::Statement& statement, int col) { |
60 return base::FilePath(statement.ColumnString16(col)); | 57 return base::FilePath(statement.ColumnString16(col)); |
61 } | 58 } |
62 | 59 |
63 #endif | 60 #endif |
64 | 61 |
65 } // namespace | 62 } // namespace |
66 | 63 |
67 // These constants and the transformation functions below are used to allow | 64 DownloadDatabase::DownloadDatabase( |
68 // DownloadItem::DownloadState and DownloadDangerType to change without | 65 DownloadInterruptReason download_interrupt_no_reason, |
69 // breaking the database schema. | 66 DownloadInterruptReason download_interrupt_crash) |
70 // They guarantee that the values of the |state| field in the database are one | |
71 // of the values returned by StateToInt, and that the values of the |state| | |
72 // field of the DownloadRows returned by QueryDownloads() are one of the values | |
73 // returned by IntToState(). | |
74 const int DownloadDatabase::kStateInvalid = -1; | |
75 const int DownloadDatabase::kStateInProgress = 0; | |
76 const int DownloadDatabase::kStateComplete = 1; | |
77 const int DownloadDatabase::kStateCancelled = 2; | |
78 const int DownloadDatabase::kStateBug140687 = 3; | |
79 const int DownloadDatabase::kStateInterrupted = 4; | |
80 | |
81 const int DownloadDatabase::kDangerTypeInvalid = -1; | |
82 const int DownloadDatabase::kDangerTypeNotDangerous = 0; | |
83 const int DownloadDatabase::kDangerTypeDangerousFile = 1; | |
84 const int DownloadDatabase::kDangerTypeDangerousUrl = 2; | |
85 const int DownloadDatabase::kDangerTypeDangerousContent = 3; | |
86 const int DownloadDatabase::kDangerTypeMaybeDangerousContent = 4; | |
87 const int DownloadDatabase::kDangerTypeUncommonContent = 5; | |
88 const int DownloadDatabase::kDangerTypeUserValidated = 6; | |
89 const int DownloadDatabase::kDangerTypeDangerousHost = 7; | |
90 const int DownloadDatabase::kDangerTypePotentiallyUnwanted = 8; | |
91 | |
92 int DownloadDatabase::StateToInt(DownloadItem::DownloadState state) { | |
93 switch (state) { | |
94 case DownloadItem::IN_PROGRESS: return DownloadDatabase::kStateInProgress; | |
95 case DownloadItem::COMPLETE: return DownloadDatabase::kStateComplete; | |
96 case DownloadItem::CANCELLED: return DownloadDatabase::kStateCancelled; | |
97 case DownloadItem::INTERRUPTED: return DownloadDatabase::kStateInterrupted; | |
98 case DownloadItem::MAX_DOWNLOAD_STATE: | |
99 NOTREACHED(); | |
100 return DownloadDatabase::kStateInvalid; | |
101 } | |
102 NOTREACHED(); | |
103 return DownloadDatabase::kStateInvalid; | |
104 } | |
105 | |
106 DownloadItem::DownloadState DownloadDatabase::IntToState(int state) { | |
107 switch (state) { | |
108 case DownloadDatabase::kStateInProgress: return DownloadItem::IN_PROGRESS; | |
109 case DownloadDatabase::kStateComplete: return DownloadItem::COMPLETE; | |
110 case DownloadDatabase::kStateCancelled: return DownloadItem::CANCELLED; | |
111 // We should not need kStateBug140687 here because MigrateDownloadsState() | |
112 // is called in HistoryDatabase::Init(). | |
113 case DownloadDatabase::kStateInterrupted: return DownloadItem::INTERRUPTED; | |
114 default: return DownloadItem::MAX_DOWNLOAD_STATE; | |
115 } | |
116 } | |
117 | |
118 int DownloadDatabase::DangerTypeToInt(content::DownloadDangerType danger_type) { | |
119 switch (danger_type) { | |
120 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | |
121 return DownloadDatabase::kDangerTypeNotDangerous; | |
122 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: | |
123 return DownloadDatabase::kDangerTypeDangerousFile; | |
124 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: | |
125 return DownloadDatabase::kDangerTypeDangerousUrl; | |
126 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: | |
127 return DownloadDatabase::kDangerTypeDangerousContent; | |
128 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | |
129 return DownloadDatabase::kDangerTypeMaybeDangerousContent; | |
130 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: | |
131 return DownloadDatabase::kDangerTypeUncommonContent; | |
132 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | |
133 return DownloadDatabase::kDangerTypeUserValidated; | |
134 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: | |
135 return DownloadDatabase::kDangerTypeDangerousHost; | |
136 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: | |
137 return DownloadDatabase::kDangerTypePotentiallyUnwanted; | |
138 case content::DOWNLOAD_DANGER_TYPE_MAX: | |
139 NOTREACHED(); | |
140 return DownloadDatabase::kDangerTypeInvalid; | |
141 } | |
142 NOTREACHED(); | |
143 return DownloadDatabase::kDangerTypeInvalid; | |
144 } | |
145 | |
146 content::DownloadDangerType DownloadDatabase::IntToDangerType(int danger_type) { | |
147 switch (danger_type) { | |
148 case DownloadDatabase::kDangerTypeNotDangerous: | |
149 return content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; | |
150 case DownloadDatabase::kDangerTypeDangerousFile: | |
151 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | |
152 case DownloadDatabase::kDangerTypeDangerousUrl: | |
153 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; | |
154 case DownloadDatabase::kDangerTypeDangerousContent: | |
155 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; | |
156 case DownloadDatabase::kDangerTypeMaybeDangerousContent: | |
157 return content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT; | |
158 case DownloadDatabase::kDangerTypeUncommonContent: | |
159 return content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT; | |
160 case DownloadDatabase::kDangerTypeUserValidated: | |
161 return content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED; | |
162 case DownloadDatabase::kDangerTypeDangerousHost: | |
163 return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST; | |
164 case DownloadDatabase::kDangerTypePotentiallyUnwanted: | |
165 return content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED; | |
166 default: | |
167 return content::DOWNLOAD_DANGER_TYPE_MAX; | |
168 } | |
169 } | |
170 | |
171 DownloadDatabase::DownloadDatabase() | |
172 : owning_thread_set_(false), | 67 : owning_thread_set_(false), |
173 owning_thread_(0), | 68 owning_thread_(0), |
174 in_progress_entry_cleanup_completed_(false) { | 69 in_progress_entry_cleanup_completed_(false), |
| 70 download_interrupt_no_reason_(download_interrupt_no_reason), |
| 71 download_interrupt_crash_(download_interrupt_crash) { |
175 } | 72 } |
176 | 73 |
177 DownloadDatabase::~DownloadDatabase() { | 74 DownloadDatabase::~DownloadDatabase() { |
178 } | 75 } |
179 | 76 |
180 bool DownloadDatabase::EnsureColumnExists( | 77 bool DownloadDatabase::EnsureColumnExists( |
181 const std::string& name, const std::string& type) { | 78 const std::string& name, const std::string& type) { |
182 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; | 79 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; |
183 return GetDB().DoesColumnExist("downloads", name.c_str()) || | 80 return GetDB().DoesColumnExist("downloads", name.c_str()) || |
184 GetDB().Execute(add_col.c_str()); | 81 GetDB().Execute(add_col.c_str()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 " total_bytes, state, danger_type, interrupt_reason, end_time, opened ) " | 140 " total_bytes, state, danger_type, interrupt_reason, end_time, opened ) " |
244 "SELECT id, full_path, full_path, " | 141 "SELECT id, full_path, full_path, " |
245 " CASE start_time WHEN 0 THEN 0 ELSE " | 142 " CASE start_time WHEN 0 THEN 0 ELSE " |
246 " (start_time + 11644473600) * 1000000 END, " | 143 " (start_time + 11644473600) * 1000000 END, " |
247 " received_bytes, total_bytes, " | 144 " received_bytes, total_bytes, " |
248 " state, ?, ?, " | 145 " state, ?, ?, " |
249 " CASE end_time WHEN 0 THEN 0 ELSE " | 146 " CASE end_time WHEN 0 THEN 0 ELSE " |
250 " (end_time + 11644473600) * 1000000 END, " | 147 " (end_time + 11644473600) * 1000000 END, " |
251 " opened " | 148 " opened " |
252 "FROM downloads_tmp")); | 149 "FROM downloads_tmp")); |
253 statement_populate.BindInt(0, content::DOWNLOAD_INTERRUPT_REASON_NONE); | 150 statement_populate.BindInt(0, download_interrupt_no_reason_); |
254 statement_populate.BindInt(1, kDangerTypeNotDangerous); | 151 statement_populate.BindInt(1, kDangerTypeNotDangerous); |
255 if (!statement_populate.Run()) | 152 if (!statement_populate.Run()) |
256 return false; | 153 return false; |
257 | 154 |
258 // Create new chain table and populate it. | 155 // Create new chain table and populate it. |
259 if (!GetDB().Execute(kReasonPathDangerUrlChainSchema)) | 156 if (!GetDB().Execute(kReasonPathDangerUrlChainSchema)) |
260 return false; | 157 return false; |
261 | 158 |
262 if (!GetDB().Execute("INSERT INTO downloads_url_chains " | 159 if (!GetDB().Execute("INSERT INTO downloads_url_chains " |
263 " ( id, chain_index, url) " | 160 " ( id, chain_index, url) " |
(...skipping 25 matching lines...) Expand all Loading... |
289 const char kSchema[] = | 186 const char kSchema[] = |
290 "CREATE TABLE downloads (" | 187 "CREATE TABLE downloads (" |
291 "id INTEGER PRIMARY KEY," // Primary key. | 188 "id INTEGER PRIMARY KEY," // Primary key. |
292 "current_path LONGVARCHAR NOT NULL," // Current disk location | 189 "current_path LONGVARCHAR NOT NULL," // Current disk location |
293 "target_path LONGVARCHAR NOT NULL," // Final disk location | 190 "target_path LONGVARCHAR NOT NULL," // Final disk location |
294 "start_time INTEGER NOT NULL," // When the download was started. | 191 "start_time INTEGER NOT NULL," // When the download was started. |
295 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 192 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
296 "total_bytes INTEGER NOT NULL," // Total size of the download. | 193 "total_bytes INTEGER NOT NULL," // Total size of the download. |
297 "state INTEGER NOT NULL," // 1=complete, 4=interrupted | 194 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
298 "danger_type INTEGER NOT NULL," // Danger type, validated. | 195 "danger_type INTEGER NOT NULL," // Danger type, validated. |
299 "interrupt_reason INTEGER NOT NULL," // content::DownloadInterruptReason | 196 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason |
300 "end_time INTEGER NOT NULL," // When the download completed. | 197 "end_time INTEGER NOT NULL," // When the download completed. |
301 "opened INTEGER NOT NULL," // 1 if it has ever been opened | 198 "opened INTEGER NOT NULL," // 1 if it has ever been opened |
302 // else 0 | 199 // else 0 |
303 "referrer VARCHAR NOT NULL," // HTTP Referrer | 200 "referrer VARCHAR NOT NULL," // HTTP Referrer |
304 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the | 201 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the |
305 // download | 202 // download |
306 "by_ext_name VARCHAR NOT NULL," // name of extension | 203 "by_ext_name VARCHAR NOT NULL," // name of extension |
307 "etag VARCHAR NOT NULL," // ETag | 204 "etag VARCHAR NOT NULL," // ETag |
308 "last_modified VARCHAR NOT NULL," // Last-Modified header | 205 "last_modified VARCHAR NOT NULL," // Last-Modified header |
309 "mime_type VARCHAR(255) NOT NULL," // MIME type. | 206 "mime_type VARCHAR(255) NOT NULL," // MIME type. |
(...skipping 17 matching lines...) Expand all Loading... |
327 return (!GetDB().DoesTableExist("downloads_url_chain") && | 224 return (!GetDB().DoesTableExist("downloads_url_chain") && |
328 GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); | 225 GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); |
329 } | 226 } |
330 } | 227 } |
331 | 228 |
332 uint32 DownloadDatabase::GetNextDownloadId() { | 229 uint32 DownloadDatabase::GetNextDownloadId() { |
333 sql::Statement select_max_id(GetDB().GetUniqueStatement( | 230 sql::Statement select_max_id(GetDB().GetUniqueStatement( |
334 "SELECT max(id) FROM downloads")); | 231 "SELECT max(id) FROM downloads")); |
335 bool result = select_max_id.Step(); | 232 bool result = select_max_id.Step(); |
336 DCHECK(result); | 233 DCHECK(result); |
337 // If there are zero records in the downloads table, then max(id) will return | 234 // If there are zero records in the downloads table, then max(id) will |
338 // 0 = kInvalidId, so GetNextDownloadId() will set *id = kInvalidId + 1. | 235 // return 0 = kInvalidDownloadId, so GetNextDownloadId() will set |
339 // If there is at least one record but all of the |id|s are <= kInvalidId, | 236 // *id = kInvalidDownloadId + 1. |
340 // then max(id) will return <= kInvalidId, so GetNextDownloadId should return | 237 // |
341 // kInvalidId + 1. Note that any records with |id <= kInvalidId| will be | 238 // If there is at least one record but all of the |id|s are |
342 // dropped in QueryDownloads() | 239 // <= kInvalidDownloadId, then max(id) will return <= kInvalidDownloadId, |
| 240 // so GetNextDownloadId() should return kInvalidDownloadId + 1. |
| 241 // |
| 242 // Note that any records with |id <= kInvalidDownloadId| will be dropped in |
| 243 // QueryDownloads(). |
| 244 // |
343 // SQLITE doesn't have unsigned integers. | 245 // SQLITE doesn't have unsigned integers. |
344 return 1 + static_cast<uint32>(std::max( | 246 return 1 + static_cast<uint32>(std::max( |
345 static_cast<int64>(content::DownloadItem::kInvalidId), | 247 static_cast<int64>(kInvalidDownloadId), |
346 select_max_id.ColumnInt64(0))); | 248 select_max_id.ColumnInt64(0))); |
347 } | 249 } |
348 | 250 |
349 bool DownloadDatabase::DropDownloadTable() { | 251 bool DownloadDatabase::DropDownloadTable() { |
350 return GetDB().Execute("DROP TABLE downloads"); | 252 return GetDB().Execute("DROP TABLE downloads"); |
351 } | 253 } |
352 | 254 |
353 void DownloadDatabase::QueryDownloads( | 255 void DownloadDatabase::QueryDownloads( |
354 std::vector<DownloadRow>* results) { | 256 std::vector<DownloadRow>* results) { |
355 EnsureInProgressEntriesCleanedUp(); | 257 EnsureInProgressEntriesCleanedUp(); |
(...skipping 22 matching lines...) Expand all Loading... |
378 info->id = static_cast<uint32>(signed_id); | 280 info->id = static_cast<uint32>(signed_id); |
379 info->current_path = ColumnFilePath(statement_main, column++); | 281 info->current_path = ColumnFilePath(statement_main, column++); |
380 info->target_path = ColumnFilePath(statement_main, column++); | 282 info->target_path = ColumnFilePath(statement_main, column++); |
381 info->mime_type = statement_main.ColumnString(column++); | 283 info->mime_type = statement_main.ColumnString(column++); |
382 info->original_mime_type = statement_main.ColumnString(column++); | 284 info->original_mime_type = statement_main.ColumnString(column++); |
383 info->start_time = base::Time::FromInternalValue( | 285 info->start_time = base::Time::FromInternalValue( |
384 statement_main.ColumnInt64(column++)); | 286 statement_main.ColumnInt64(column++)); |
385 info->received_bytes = statement_main.ColumnInt64(column++); | 287 info->received_bytes = statement_main.ColumnInt64(column++); |
386 info->total_bytes = statement_main.ColumnInt64(column++); | 288 info->total_bytes = statement_main.ColumnInt64(column++); |
387 int state = statement_main.ColumnInt(column++); | 289 int state = statement_main.ColumnInt(column++); |
388 info->state = IntToState(state); | 290 info->state = IntToDownloadState(state); |
389 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) | 291 if (info->state == kStateInvalid) |
390 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 292 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
391 info->danger_type = IntToDangerType(statement_main.ColumnInt(column++)); | 293 info->danger_type = |
392 info->interrupt_reason = static_cast<content::DownloadInterruptReason>( | 294 IntToDownloadDangerType(statement_main.ColumnInt(column++)); |
| 295 info->interrupt_reason = static_cast<DownloadInterruptReason>( |
393 statement_main.ColumnInt(column++)); | 296 statement_main.ColumnInt(column++)); |
394 info->end_time = base::Time::FromInternalValue( | 297 info->end_time = base::Time::FromInternalValue( |
395 statement_main.ColumnInt64(column++)); | 298 statement_main.ColumnInt64(column++)); |
396 info->opened = statement_main.ColumnInt(column++) != 0; | 299 info->opened = statement_main.ColumnInt(column++) != 0; |
397 info->referrer_url = GURL(statement_main.ColumnString(column++)); | 300 info->referrer_url = GURL(statement_main.ColumnString(column++)); |
398 info->by_ext_id = statement_main.ColumnString(column++); | 301 info->by_ext_id = statement_main.ColumnString(column++); |
399 info->by_ext_name = statement_main.ColumnString(column++); | 302 info->by_ext_name = statement_main.ColumnString(column++); |
400 info->etag = statement_main.ColumnString(column++); | 303 info->etag = statement_main.ColumnString(column++); |
401 info->last_modified = statement_main.ColumnString(column++); | 304 info->last_modified = statement_main.ColumnString(column++); |
402 | 305 |
403 // If the record is corrupted, note that and drop it. | 306 // If the record is corrupted, note that and drop it. |
404 // http://crbug.com/251269 | 307 // http://crbug.com/251269 |
405 DroppedReason dropped_reason = DROPPED_REASON_MAX; | 308 DroppedReason dropped_reason = DROPPED_REASON_MAX; |
406 if (signed_id <= static_cast<int64>(content::DownloadItem::kInvalidId)) { | 309 if (signed_id <= static_cast<int64>(kInvalidDownloadId)) { |
407 // SQLITE doesn't have unsigned integers. | 310 // SQLITE doesn't have unsigned integers. |
408 dropped_reason = DROPPED_REASON_BAD_ID; | 311 dropped_reason = DROPPED_REASON_BAD_ID; |
409 } else if (!ids.insert(info->id).second) { | 312 } else if (!ids.insert(info->id).second) { |
410 dropped_reason = DROPPED_REASON_DUPLICATE_ID; | 313 dropped_reason = DROPPED_REASON_DUPLICATE_ID; |
411 NOTREACHED() << info->id; | 314 NOTREACHED() << info->id; |
412 } else if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) { | 315 } else if (info->state == kStateInvalid) { |
413 dropped_reason = DROPPED_REASON_BAD_STATE; | 316 dropped_reason = DROPPED_REASON_BAD_STATE; |
414 } else if (info->danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { | 317 } else if (info->danger_type == kDangerTypeInvalid) { |
415 dropped_reason = DROPPED_REASON_BAD_DANGER_TYPE; | 318 dropped_reason = DROPPED_REASON_BAD_DANGER_TYPE; |
416 } | 319 } |
417 if (dropped_reason != DROPPED_REASON_MAX) { | 320 if (dropped_reason != DROPPED_REASON_MAX) { |
418 UMA_HISTOGRAM_ENUMERATION("Download.DatabaseRecordDropped", | 321 UMA_HISTOGRAM_ENUMERATION("Download.DatabaseRecordDropped", |
419 dropped_reason, | 322 dropped_reason, |
420 DROPPED_REASON_MAX + 1); | 323 DROPPED_REASON_MAX + 1); |
421 } else { | 324 } else { |
422 DCHECK(!ContainsKey(info_map, info->id)); | 325 DCHECK(!ContainsKey(info_map, info->id)); |
423 uint32 id = info->id; | 326 uint32 id = info->id; |
424 info_map[id] = info.release(); | 327 info_map[id] = info.release(); |
425 } | 328 } |
426 } | 329 } |
427 | 330 |
428 sql::Statement statement_chain(GetDB().GetCachedStatement( | 331 sql::Statement statement_chain(GetDB().GetCachedStatement( |
429 SQL_FROM_HERE, | 332 SQL_FROM_HERE, |
430 "SELECT id, chain_index, url FROM downloads_url_chains " | 333 "SELECT id, chain_index, url FROM downloads_url_chains " |
431 "ORDER BY id, chain_index")); | 334 "ORDER BY id, chain_index")); |
432 | 335 |
433 while (statement_chain.Step()) { | 336 while (statement_chain.Step()) { |
434 int column = 0; | 337 int column = 0; |
435 // See the comment above about SQLITE lacking unsigned integers. | 338 // See the comment above about SQLITE lacking unsigned integers. |
436 int64 signed_id = statement_chain.ColumnInt64(column++); | 339 int64 signed_id = statement_chain.ColumnInt64(column++); |
437 int chain_index = statement_chain.ColumnInt(column++); | 340 int chain_index = statement_chain.ColumnInt(column++); |
438 | 341 |
439 if (signed_id <= static_cast<int64>(content::DownloadItem::kInvalidId)) | 342 if (signed_id <= static_cast<int64>(kInvalidDownloadId)) |
440 continue; | 343 continue; |
441 uint32 id = static_cast<uint32>(signed_id); | 344 uint32 id = static_cast<uint32>(signed_id); |
442 | 345 |
443 // Note that these DCHECKs may trip as a result of corrupted databases. | 346 // Note that these DCHECKs may trip as a result of corrupted databases. |
444 // We have them because in debug builds the chances are higher there's | 347 // We have them because in debug builds the chances are higher there's |
445 // an actual bug than that the database is corrupt, but we handle the | 348 // an actual bug than that the database is corrupt, but we handle the |
446 // DB corruption case in production code. | 349 // DB corruption case in production code. |
447 | 350 |
448 // Confirm the id has already been seen--if it hasn't, discard the | 351 // Confirm the id has already been seen--if it hasn't, discard the |
449 // record. | 352 // record. |
(...skipping 29 matching lines...) Expand all Loading... |
479 results->push_back(*row); | 382 results->push_back(*row); |
480 } | 383 } |
481 delete row; | 384 delete row; |
482 it->second = NULL; | 385 it->second = NULL; |
483 } | 386 } |
484 } | 387 } |
485 | 388 |
486 bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { | 389 bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { |
487 EnsureInProgressEntriesCleanedUp(); | 390 EnsureInProgressEntriesCleanedUp(); |
488 | 391 |
489 DCHECK_NE(content::DownloadItem::kInvalidId, data.id); | 392 DCHECK_NE(kInvalidDownloadId, data.id); |
490 int state = StateToInt(data.state); | 393 if (data.state == kStateInvalid) { |
491 if (state == kStateInvalid) { | |
492 NOTREACHED(); | 394 NOTREACHED(); |
493 return false; | 395 return false; |
494 } | 396 } |
495 int danger_type = DangerTypeToInt(data.danger_type); | 397 if (data.danger_type == kDangerTypeInvalid) { |
496 if (danger_type == kDangerTypeInvalid) { | |
497 NOTREACHED(); | 398 NOTREACHED(); |
498 return false; | 399 return false; |
499 } | 400 } |
500 | 401 |
501 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 402 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
502 "UPDATE downloads " | 403 "UPDATE downloads " |
503 "SET current_path=?, target_path=?, " | 404 "SET current_path=?, target_path=?, " |
504 "mime_type=?, original_mime_type=?, " | 405 "mime_type=?, original_mime_type=?, " |
505 "received_bytes=?, state=?, " | 406 "received_bytes=?, state=?, " |
506 "danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, " | 407 "danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, " |
507 "opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? " | 408 "opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? " |
508 "WHERE id=?")); | 409 "WHERE id=?")); |
509 int column = 0; | 410 int column = 0; |
510 BindFilePath(statement, data.current_path, column++); | 411 BindFilePath(statement, data.current_path, column++); |
511 BindFilePath(statement, data.target_path, column++); | 412 BindFilePath(statement, data.target_path, column++); |
512 statement.BindString(column++, data.mime_type); | 413 statement.BindString(column++, data.mime_type); |
513 statement.BindString(column++, data.original_mime_type); | 414 statement.BindString(column++, data.original_mime_type); |
514 statement.BindInt64(column++, data.received_bytes); | 415 statement.BindInt64(column++, data.received_bytes); |
515 statement.BindInt(column++, state); | 416 statement.BindInt(column++, static_cast<int>(data.state)); |
516 statement.BindInt(column++, danger_type); | 417 statement.BindInt(column++, static_cast<int>(data.danger_type)); |
517 statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); | 418 statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); |
518 statement.BindInt64(column++, data.end_time.ToInternalValue()); | 419 statement.BindInt64(column++, data.end_time.ToInternalValue()); |
519 statement.BindInt64(column++, data.total_bytes); | 420 statement.BindInt64(column++, data.total_bytes); |
520 statement.BindInt(column++, (data.opened ? 1 : 0)); | 421 statement.BindInt(column++, (data.opened ? 1 : 0)); |
521 statement.BindString(column++, data.by_ext_id); | 422 statement.BindString(column++, data.by_ext_id); |
522 statement.BindString(column++, data.by_ext_name); | 423 statement.BindString(column++, data.by_ext_name); |
523 statement.BindString(column++, data.etag); | 424 statement.BindString(column++, data.etag); |
524 statement.BindString(column++, data.last_modified); | 425 statement.BindString(column++, data.last_modified); |
525 statement.BindInt(column++, data.id); | 426 statement.BindInt(column++, data.id); |
526 | 427 |
527 return statement.Run(); | 428 return statement.Run(); |
528 } | 429 } |
529 | 430 |
530 void DownloadDatabase::EnsureInProgressEntriesCleanedUp() { | 431 void DownloadDatabase::EnsureInProgressEntriesCleanedUp() { |
531 if (in_progress_entry_cleanup_completed_) | 432 if (in_progress_entry_cleanup_completed_) |
532 return; | 433 return; |
533 | 434 |
534 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 435 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
535 "UPDATE downloads SET state=?, interrupt_reason=? WHERE state=?")); | 436 "UPDATE downloads SET state=?, interrupt_reason=? WHERE state=?")); |
536 statement.BindInt(0, kStateInterrupted); | 437 statement.BindInt(0, kStateInterrupted); |
537 statement.BindInt(1, content::DOWNLOAD_INTERRUPT_REASON_CRASH); | 438 statement.BindInt(1, download_interrupt_crash_); |
538 statement.BindInt(2, kStateInProgress); | 439 statement.BindInt(2, kStateInProgress); |
539 | 440 |
540 statement.Run(); | 441 statement.Run(); |
541 in_progress_entry_cleanup_completed_ = true; | 442 in_progress_entry_cleanup_completed_ = true; |
542 } | 443 } |
543 | 444 |
544 bool DownloadDatabase::CreateDownload(const DownloadRow& info) { | 445 bool DownloadDatabase::CreateDownload(const DownloadRow& info) { |
545 DCHECK_NE(content::DownloadItem::kInvalidId, info.id); | 446 DCHECK_NE(kInvalidDownloadId, info.id); |
546 EnsureInProgressEntriesCleanedUp(); | 447 EnsureInProgressEntriesCleanedUp(); |
547 | 448 |
548 if (info.url_chain.empty()) | 449 if (info.url_chain.empty()) |
549 return false; | 450 return false; |
550 | 451 |
551 int state = StateToInt(info.state); | 452 if (info.state == kStateInvalid) |
552 if (state == kStateInvalid) | |
553 return false; | 453 return false; |
554 | 454 |
555 int danger_type = DangerTypeToInt(info.danger_type); | 455 if (info.danger_type == kDangerTypeInvalid) |
556 if (danger_type == kDangerTypeInvalid) | |
557 return false; | 456 return false; |
558 | 457 |
559 { | 458 { |
560 sql::Statement statement_insert(GetDB().GetCachedStatement( | 459 sql::Statement statement_insert(GetDB().GetCachedStatement( |
561 SQL_FROM_HERE, | 460 SQL_FROM_HERE, |
562 "INSERT INTO downloads " | 461 "INSERT INTO downloads " |
563 "(id, current_path, target_path, " | 462 "(id, current_path, target_path, " |
564 " mime_type, original_mime_type, " | 463 " mime_type, original_mime_type, " |
565 " start_time, " | 464 " start_time, " |
566 " received_bytes, total_bytes, state, danger_type, interrupt_reason, " | 465 " received_bytes, total_bytes, state, danger_type, interrupt_reason, " |
567 " end_time, opened, referrer, by_ext_id, by_ext_name, etag, " | 466 " end_time, opened, referrer, by_ext_id, by_ext_name, etag, " |
568 " last_modified) " | 467 " last_modified) " |
569 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 468 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
570 | 469 |
571 int column = 0; | 470 int column = 0; |
572 statement_insert.BindInt(column++, info.id); | 471 statement_insert.BindInt(column++, info.id); |
573 BindFilePath(statement_insert, info.current_path, column++); | 472 BindFilePath(statement_insert, info.current_path, column++); |
574 BindFilePath(statement_insert, info.target_path, column++); | 473 BindFilePath(statement_insert, info.target_path, column++); |
575 statement_insert.BindString(column++, info.mime_type); | 474 statement_insert.BindString(column++, info.mime_type); |
576 statement_insert.BindString(column++, info.original_mime_type); | 475 statement_insert.BindString(column++, info.original_mime_type); |
577 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | 476 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); |
578 statement_insert.BindInt64(column++, info.received_bytes); | 477 statement_insert.BindInt64(column++, info.received_bytes); |
579 statement_insert.BindInt64(column++, info.total_bytes); | 478 statement_insert.BindInt64(column++, info.total_bytes); |
580 statement_insert.BindInt(column++, state); | 479 statement_insert.BindInt(column++, static_cast<int>(info.state)); |
581 statement_insert.BindInt(column++, danger_type); | 480 statement_insert.BindInt(column++, static_cast<int>(info.danger_type)); |
582 statement_insert.BindInt(column++, info.interrupt_reason); | 481 statement_insert.BindInt(column++, info.interrupt_reason); |
583 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | 482 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); |
584 statement_insert.BindInt(column++, info.opened ? 1 : 0); | 483 statement_insert.BindInt(column++, info.opened ? 1 : 0); |
585 statement_insert.BindString(column++, info.referrer_url.spec()); | 484 statement_insert.BindString(column++, info.referrer_url.spec()); |
586 statement_insert.BindString(column++, info.by_ext_id); | 485 statement_insert.BindString(column++, info.by_ext_id); |
587 statement_insert.BindString(column++, info.by_ext_name); | 486 statement_insert.BindString(column++, info.by_ext_name); |
588 statement_insert.BindString(column++, info.etag); | 487 statement_insert.BindString(column++, info.etag); |
589 statement_insert.BindString(column++, info.last_modified); | 488 statement_insert.BindString(column++, info.last_modified); |
590 if (!statement_insert.Run()) { | 489 if (!statement_insert.Run()) { |
591 // GetErrorCode() returns a bitmask where the lower byte is a more general | 490 // GetErrorCode() returns a bitmask where the lower byte is a more general |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 size_t DownloadDatabase::CountDownloads() { | 561 size_t DownloadDatabase::CountDownloads() { |
663 EnsureInProgressEntriesCleanedUp(); | 562 EnsureInProgressEntriesCleanedUp(); |
664 | 563 |
665 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 564 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
666 "SELECT count(*) from downloads")); | 565 "SELECT count(*) from downloads")); |
667 statement.Step(); | 566 statement.Step(); |
668 return statement.ColumnInt(0); | 567 return statement.ColumnInt(0); |
669 } | 568 } |
670 | 569 |
671 } // namespace history | 570 } // namespace history |
OLD | NEW |