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 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 36 DROPPED_REASON_BAD_ID = 2, | 36 DROPPED_REASON_BAD_ID = 2, |
| 37 DROPPED_REASON_DUPLICATE_ID = 3, | 37 DROPPED_REASON_DUPLICATE_ID = 3, |
| 38 DROPPED_REASON_MAX | 38 DROPPED_REASON_MAX |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 static const char kSchema[] = | 41 static const char kSchema[] = |
| 42 "CREATE TABLE downloads (" | 42 "CREATE TABLE downloads (" |
| 43 "id INTEGER PRIMARY KEY," // Primary key. | 43 "id INTEGER PRIMARY KEY," // Primary key. |
| 44 "current_path LONGVARCHAR NOT NULL," // Current disk location | 44 "current_path LONGVARCHAR NOT NULL," // Current disk location |
| 45 "target_path LONGVARCHAR NOT NULL," // Final disk location | 45 "target_path LONGVARCHAR NOT NULL," // Final disk location |
| 46 "mime_type VARCHAR(255) NOT NULL," // Mime type. | |
|
asanka
2013/12/16 21:51:48
Why VARCHAR(255) ? I don't think length constraint
| |
| 47 "original_mime_type VARCHAR(255) NOT NULL," // Original mime type. | |
| 46 "start_time INTEGER NOT NULL," // When the download was started. | 48 "start_time INTEGER NOT NULL," // When the download was started. |
| 47 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 49 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
| 48 "total_bytes INTEGER NOT NULL," // Total size of the download. | 50 "total_bytes INTEGER NOT NULL," // Total size of the download. |
| 49 "state INTEGER NOT NULL," // 1=complete, 4=interrupted | 51 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
| 50 "danger_type INTEGER NOT NULL, " // Danger type, validated. | 52 "danger_type INTEGER NOT NULL, " // Danger type, validated. |
| 51 "interrupt_reason INTEGER NOT NULL," // content::DownloadInterruptReason | 53 "interrupt_reason INTEGER NOT NULL," // content::DownloadInterruptReason |
| 52 "end_time INTEGER NOT NULL," // When the download completed. | 54 "end_time INTEGER NOT NULL," // When the download completed. |
| 53 "opened INTEGER NOT NULL," // 1 if it has ever been opened else 0 | 55 "opened INTEGER NOT NULL," // 1 if it has ever been opened else 0 |
| 54 "referrer VARCHAR NOT NULL," // HTTP Referrer | 56 "referrer VARCHAR NOT NULL," // HTTP Referrer |
| 55 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the | 57 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 } | 282 } |
| 281 | 283 |
| 282 bool DownloadDatabase::MigrateDownloadValidators() { | 284 bool DownloadDatabase::MigrateDownloadValidators() { |
| 283 return EnsureColumnExists("etag", "VARCHAR NOT NULL DEFAULT \"\"") && | 285 return EnsureColumnExists("etag", "VARCHAR NOT NULL DEFAULT \"\"") && |
| 284 EnsureColumnExists("last_modified", "VARCHAR NOT NULL DEFAULT \"\""); | 286 EnsureColumnExists("last_modified", "VARCHAR NOT NULL DEFAULT \"\""); |
| 285 } | 287 } |
| 286 | 288 |
| 287 bool DownloadDatabase::InitDownloadTable() { | 289 bool DownloadDatabase::InitDownloadTable() { |
| 288 if (GetDB().DoesTableExist("downloads")) { | 290 if (GetDB().DoesTableExist("downloads")) { |
| 289 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && | 291 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| 290 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); | 292 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0") && |
| 293 EnsureColumnExists("mime_type", "VARCHAR(255) NOT NULL" | |
| 294 " DEFAULT ''") && | |
|
asanka
2013/12/16 21:51:48
Nit: \"\" for consistency. Also as Randy asked, th
| |
| 295 EnsureColumnExists("original_mime_type", "VARCHAR(255) NOT NULL" | |
| 296 " DEFAULT ''"); | |
| 297 | |
| 291 } else { | 298 } else { |
| 292 // If the "downloads" table doesn't exist, the downloads_url_chain | 299 // If the "downloads" table doesn't exist, the downloads_url_chain |
| 293 // table better not. | 300 // table better not. |
| 294 return (!GetDB().DoesTableExist("downloads_url_chain") && | 301 return (!GetDB().DoesTableExist("downloads_url_chain") && |
| 295 GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); | 302 GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); |
| 296 } | 303 } |
| 297 } | 304 } |
| 298 | 305 |
| 299 void DownloadDatabase::GetNextDownloadId(uint32* id) { | 306 void DownloadDatabase::GetNextDownloadId(uint32* id) { |
| 300 sql::Statement select_max_id(GetDB().GetUniqueStatement( | 307 sql::Statement select_max_id(GetDB().GetUniqueStatement( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 323 void DownloadDatabase::QueryDownloads( | 330 void DownloadDatabase::QueryDownloads( |
| 324 std::vector<DownloadRow>* results) { | 331 std::vector<DownloadRow>* results) { |
| 325 EnsureInProgressEntriesCleanedUp(); | 332 EnsureInProgressEntriesCleanedUp(); |
| 326 | 333 |
| 327 results->clear(); | 334 results->clear(); |
| 328 std::set<uint32> ids; | 335 std::set<uint32> ids; |
| 329 | 336 |
| 330 std::map<uint32, DownloadRow*> info_map; | 337 std::map<uint32, DownloadRow*> info_map; |
| 331 | 338 |
| 332 sql::Statement statement_main(GetDB().GetCachedStatement(SQL_FROM_HERE, | 339 sql::Statement statement_main(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 333 "SELECT id, current_path, target_path, start_time, received_bytes, " | 340 "SELECT id, current_path, target_path, " |
| 341 "mime_type, original_mime_type, " | |
| 342 "start_time, received_bytes, " | |
| 334 "total_bytes, state, danger_type, interrupt_reason, end_time, opened, " | 343 "total_bytes, state, danger_type, interrupt_reason, end_time, opened, " |
| 335 "referrer, by_ext_id, by_ext_name, etag, last_modified " | 344 "referrer, by_ext_id, by_ext_name, etag, last_modified " |
| 336 "FROM downloads ORDER BY start_time")); | 345 "FROM downloads ORDER BY start_time")); |
| 337 | 346 |
| 338 while (statement_main.Step()) { | 347 while (statement_main.Step()) { |
| 339 scoped_ptr<DownloadRow> info(new DownloadRow()); | 348 scoped_ptr<DownloadRow> info(new DownloadRow()); |
| 340 int column = 0; | 349 int column = 0; |
| 341 | 350 |
| 342 // SQLITE does not have unsigned integers, so explicitly handle negative | 351 // SQLITE does not have unsigned integers, so explicitly handle negative |
| 343 // |id|s instead of casting them to very large uint32s, which would break | 352 // |id|s instead of casting them to very large uint32s, which would break |
| 344 // the max(id) logic in GetNextDownloadId(). | 353 // the max(id) logic in GetNextDownloadId(). |
| 345 int64 signed_id = statement_main.ColumnInt64(column++); | 354 int64 signed_id = statement_main.ColumnInt64(column++); |
| 346 info->id = static_cast<uint32>(signed_id); | 355 info->id = static_cast<uint32>(signed_id); |
| 347 info->current_path = ColumnFilePath(statement_main, column++); | 356 info->current_path = ColumnFilePath(statement_main, column++); |
| 348 info->target_path = ColumnFilePath(statement_main, column++); | 357 info->target_path = ColumnFilePath(statement_main, column++); |
| 358 info->mime_type = statement_main.ColumnString(column++); | |
| 359 info->original_mime_type = statement_main.ColumnString(column++); | |
| 349 info->start_time = base::Time::FromInternalValue( | 360 info->start_time = base::Time::FromInternalValue( |
| 350 statement_main.ColumnInt64(column++)); | 361 statement_main.ColumnInt64(column++)); |
| 351 info->received_bytes = statement_main.ColumnInt64(column++); | 362 info->received_bytes = statement_main.ColumnInt64(column++); |
| 352 info->total_bytes = statement_main.ColumnInt64(column++); | 363 info->total_bytes = statement_main.ColumnInt64(column++); |
| 353 int state = statement_main.ColumnInt(column++); | 364 int state = statement_main.ColumnInt(column++); |
| 354 info->state = IntToState(state); | 365 info->state = IntToState(state); |
| 355 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) | 366 if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) |
| 356 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 367 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
| 357 info->danger_type = IntToDangerType(statement_main.ColumnInt(column++)); | 368 info->danger_type = IntToDangerType(statement_main.ColumnInt(column++)); |
| 358 info->interrupt_reason = static_cast<content::DownloadInterruptReason>( | 369 info->interrupt_reason = static_cast<content::DownloadInterruptReason>( |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 return false; | 470 return false; |
| 460 } | 471 } |
| 461 int danger_type = DangerTypeToInt(data.danger_type); | 472 int danger_type = DangerTypeToInt(data.danger_type); |
| 462 if (danger_type == kDangerTypeInvalid) { | 473 if (danger_type == kDangerTypeInvalid) { |
| 463 NOTREACHED(); | 474 NOTREACHED(); |
| 464 return false; | 475 return false; |
| 465 } | 476 } |
| 466 | 477 |
| 467 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 478 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 468 "UPDATE downloads " | 479 "UPDATE downloads " |
| 469 "SET current_path=?, target_path=?, received_bytes=?, state=?, " | 480 "SET current_path=?, target_path=?, " |
| 481 "mime_type=?, original_mime_type=?, " | |
| 482 "received_bytes=?, state=?, " | |
| 470 "danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, " | 483 "danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, " |
| 471 "opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? " | 484 "opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? " |
| 472 "WHERE id=?")); | 485 "WHERE id=?")); |
| 473 int column = 0; | 486 int column = 0; |
| 474 BindFilePath(statement, data.current_path, column++); | 487 BindFilePath(statement, data.current_path, column++); |
| 475 BindFilePath(statement, data.target_path, column++); | 488 BindFilePath(statement, data.target_path, column++); |
| 489 statement.BindString(column++, data.mime_type); | |
| 490 statement.BindString(column++, data.original_mime_type); | |
| 476 statement.BindInt64(column++, data.received_bytes); | 491 statement.BindInt64(column++, data.received_bytes); |
| 477 statement.BindInt(column++, state); | 492 statement.BindInt(column++, state); |
| 478 statement.BindInt(column++, danger_type); | 493 statement.BindInt(column++, danger_type); |
| 479 statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); | 494 statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); |
| 480 statement.BindInt64(column++, data.end_time.ToInternalValue()); | 495 statement.BindInt64(column++, data.end_time.ToInternalValue()); |
| 481 statement.BindInt64(column++, data.total_bytes); | 496 statement.BindInt64(column++, data.total_bytes); |
| 482 statement.BindInt(column++, (data.opened ? 1 : 0)); | 497 statement.BindInt(column++, (data.opened ? 1 : 0)); |
| 483 statement.BindString(column++, data.by_ext_id); | 498 statement.BindString(column++, data.by_ext_id); |
| 484 statement.BindString(column++, data.by_ext_name); | 499 statement.BindString(column++, data.by_ext_name); |
| 485 statement.BindString(column++, data.etag); | 500 statement.BindString(column++, data.etag); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 515 return false; | 530 return false; |
| 516 | 531 |
| 517 int danger_type = DangerTypeToInt(info.danger_type); | 532 int danger_type = DangerTypeToInt(info.danger_type); |
| 518 if (danger_type == kDangerTypeInvalid) | 533 if (danger_type == kDangerTypeInvalid) |
| 519 return false; | 534 return false; |
| 520 | 535 |
| 521 { | 536 { |
| 522 sql::Statement statement_insert(GetDB().GetCachedStatement( | 537 sql::Statement statement_insert(GetDB().GetCachedStatement( |
| 523 SQL_FROM_HERE, | 538 SQL_FROM_HERE, |
| 524 "INSERT INTO downloads " | 539 "INSERT INTO downloads " |
| 525 "(id, current_path, target_path, start_time, " | 540 "(id, current_path, target_path, " |
| 541 "mime_type, original_mime_type, " | |
| 542 "start_time, " | |
| 526 " received_bytes, total_bytes, state, danger_type, interrupt_reason, " | 543 " received_bytes, total_bytes, state, danger_type, interrupt_reason, " |
| 527 " end_time, opened, referrer, by_ext_id, by_ext_name, etag, " | 544 " end_time, opened, referrer, by_ext_id, by_ext_name, etag, " |
| 528 " last_modified) " | 545 " last_modified) " |
| 529 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 546 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 530 | 547 |
| 531 int column = 0; | 548 int column = 0; |
| 532 statement_insert.BindInt(column++, info.id); | 549 statement_insert.BindInt(column++, info.id); |
| 533 BindFilePath(statement_insert, info.current_path, column++); | 550 BindFilePath(statement_insert, info.current_path, column++); |
| 534 BindFilePath(statement_insert, info.target_path, column++); | 551 BindFilePath(statement_insert, info.target_path, column++); |
| 552 statement_insert.BindString(column++, info.mime_type); | |
| 553 statement_insert.BindString(column++, info.original_mime_type); | |
| 535 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | 554 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); |
| 536 statement_insert.BindInt64(column++, info.received_bytes); | 555 statement_insert.BindInt64(column++, info.received_bytes); |
| 537 statement_insert.BindInt64(column++, info.total_bytes); | 556 statement_insert.BindInt64(column++, info.total_bytes); |
| 538 statement_insert.BindInt(column++, state); | 557 statement_insert.BindInt(column++, state); |
| 539 statement_insert.BindInt(column++, danger_type); | 558 statement_insert.BindInt(column++, danger_type); |
| 540 statement_insert.BindInt(column++, info.interrupt_reason); | 559 statement_insert.BindInt(column++, info.interrupt_reason); |
| 541 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | 560 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); |
| 542 statement_insert.BindInt(column++, info.opened ? 1 : 0); | 561 statement_insert.BindInt(column++, info.opened ? 1 : 0); |
| 543 statement_insert.BindString(column++, info.referrer_url.spec()); | 562 statement_insert.BindString(column++, info.referrer_url.spec()); |
| 544 statement_insert.BindString(column++, info.by_ext_id); | 563 statement_insert.BindString(column++, info.by_ext_id); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 620 size_t DownloadDatabase::CountDownloads() { | 639 size_t DownloadDatabase::CountDownloads() { |
| 621 EnsureInProgressEntriesCleanedUp(); | 640 EnsureInProgressEntriesCleanedUp(); |
| 622 | 641 |
| 623 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 642 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 624 "SELECT count(*) from downloads")); | 643 "SELECT count(*) from downloads")); |
| 625 statement.Step(); | 644 statement.Step(); |
| 626 return statement.ColumnInt(0); | 645 return statement.ColumnInt(0); |
| 627 } | 646 } |
| 628 | 647 |
| 629 } // namespace history | 648 } // namespace history |
| OLD | NEW |