| 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 "components/history/core/browser/download_database.h" | 5 #include "components/history/core/browser/download_database.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 266 |
| 267 bool DownloadDatabase::MigrateDownloadTabUrl() { | 267 bool DownloadDatabase::MigrateDownloadTabUrl() { |
| 268 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && | 268 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && |
| 269 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); | 269 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); |
| 270 } | 270 } |
| 271 | 271 |
| 272 bool DownloadDatabase::MigrateDownloadSiteInstanceUrl() { | 272 bool DownloadDatabase::MigrateDownloadSiteInstanceUrl() { |
| 273 return EnsureColumnExists("site_url", "VARCHAR NOT NULL DEFAULT ''"); | 273 return EnsureColumnExists("site_url", "VARCHAR NOT NULL DEFAULT ''"); |
| 274 } | 274 } |
| 275 | 275 |
| 276 bool DownloadDatabase::MigrateDownloadLastAccessTime() { |
| 277 return EnsureColumnExists("last_access_time", "INTEGER NOT NULL DEFAULT 0"); |
| 278 } |
| 279 |
| 276 bool DownloadDatabase::InitDownloadTable() { | 280 bool DownloadDatabase::InitDownloadTable() { |
| 277 const char kSchema[] = | 281 const char kSchema[] = |
| 278 "CREATE TABLE downloads (" | 282 "CREATE TABLE downloads (" |
| 279 "id INTEGER PRIMARY KEY," // Primary key. | 283 "id INTEGER PRIMARY KEY," // Primary key. |
| 280 "guid VARCHAR NOT NULL," // GUID. | 284 "guid VARCHAR NOT NULL," // GUID. |
| 281 "current_path LONGVARCHAR NOT NULL," // Current disk location | 285 "current_path LONGVARCHAR NOT NULL," // Current disk location |
| 282 "target_path LONGVARCHAR NOT NULL," // Final disk location | 286 "target_path LONGVARCHAR NOT NULL," // Final disk location |
| 283 "start_time INTEGER NOT NULL," // When the download was started. | 287 "start_time INTEGER NOT NULL," // When the download was started. |
| 284 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 288 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
| 285 "total_bytes INTEGER NOT NULL," // Total size of the download. | 289 "total_bytes INTEGER NOT NULL," // Total size of the download. |
| 286 "state INTEGER NOT NULL," // 1=complete, 4=interrupted | 290 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
| 287 "danger_type INTEGER NOT NULL," // Danger type, validated. | 291 "danger_type INTEGER NOT NULL," // Danger type, validated. |
| 288 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason | 292 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason |
| 289 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. | 293 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. |
| 290 "end_time INTEGER NOT NULL," // When the download completed. | 294 "end_time INTEGER NOT NULL," // When the download completed. |
| 291 "opened INTEGER NOT NULL," // 1 if it has ever been opened | 295 "opened INTEGER NOT NULL," // 1 if it has ever been opened |
| 292 // else 0 | 296 // else 0 |
| 297 "last_access_time INTEGER NOT NULL," // The last time it was accessed. |
| 293 "referrer VARCHAR NOT NULL," // HTTP Referrer | 298 "referrer VARCHAR NOT NULL," // HTTP Referrer |
| 294 "site_url VARCHAR NOT NULL," // Site URL for initiating site | 299 "site_url VARCHAR NOT NULL," // Site URL for initiating site |
| 295 // instance. | 300 // instance. |
| 296 "tab_url VARCHAR NOT NULL," // Tab URL for initiator. | 301 "tab_url VARCHAR NOT NULL," // Tab URL for initiator. |
| 297 "tab_referrer_url VARCHAR NOT NULL," // Tag referrer URL for initiator. | 302 "tab_referrer_url VARCHAR NOT NULL," // Tag referrer URL for initiator. |
| 298 "http_method VARCHAR NOT NULL," // HTTP method. | 303 "http_method VARCHAR NOT NULL," // HTTP method. |
| 299 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the | 304 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the |
| 300 // download | 305 // download |
| 301 "by_ext_name VARCHAR NOT NULL," // name of extension | 306 "by_ext_name VARCHAR NOT NULL," // name of extension |
| 302 "etag VARCHAR NOT NULL," // ETag | 307 "etag VARCHAR NOT NULL," // ETag |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 378 |
| 374 results->clear(); | 379 results->clear(); |
| 375 std::set<uint32_t> ids; | 380 std::set<uint32_t> ids; |
| 376 | 381 |
| 377 DownloadRowMap info_map; | 382 DownloadRowMap info_map; |
| 378 | 383 |
| 379 sql::Statement statement_main(GetDB().GetCachedStatement( | 384 sql::Statement statement_main(GetDB().GetCachedStatement( |
| 380 SQL_FROM_HERE, | 385 SQL_FROM_HERE, |
| 381 "SELECT id, guid, current_path, target_path, mime_type, " | 386 "SELECT id, guid, current_path, target_path, mime_type, " |
| 382 "original_mime_type, start_time, received_bytes, total_bytes, state, " | 387 "original_mime_type, start_time, received_bytes, total_bytes, state, " |
| 383 "danger_type, interrupt_reason, hash, end_time, opened, referrer, " | 388 "danger_type, interrupt_reason, hash, end_time, opened, " |
| 384 "site_url, tab_url, tab_referrer_url, http_method, by_ext_id, " | 389 "last_access_time, referrer, site_url, tab_url, tab_referrer_url, " |
| 385 "by_ext_name, etag, last_modified FROM downloads ORDER BY start_time")); | 390 "http_method, by_ext_id, by_ext_name, etag, last_modified " |
| 391 "FROM downloads ORDER BY start_time")); |
| 386 | 392 |
| 387 while (statement_main.Step()) { | 393 while (statement_main.Step()) { |
| 388 std::unique_ptr<DownloadRow> info(new DownloadRow()); | 394 std::unique_ptr<DownloadRow> info(new DownloadRow()); |
| 389 int column = 0; | 395 int column = 0; |
| 390 | 396 |
| 391 // SQLITE does not have unsigned integers, so explicitly handle negative | 397 // SQLITE does not have unsigned integers, so explicitly handle negative |
| 392 // |id|s instead of casting them to very large uint32s, which would break | 398 // |id|s instead of casting them to very large uint32s, which would break |
| 393 // the max(id) logic in GetNextDownloadId(). | 399 // the max(id) logic in GetNextDownloadId(). |
| 394 int64_t signed_id = statement_main.ColumnInt64(column++); | 400 int64_t signed_id = statement_main.ColumnInt64(column++); |
| 395 bool valid = ConvertIntToDownloadId(signed_id, &(info->id)); | 401 bool valid = ConvertIntToDownloadId(signed_id, &(info->id)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 407 if (info->state == DownloadState::INVALID) | 413 if (info->state == DownloadState::INVALID) |
| 408 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 414 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
| 409 info->danger_type = | 415 info->danger_type = |
| 410 IntToDownloadDangerType(statement_main.ColumnInt(column++)); | 416 IntToDownloadDangerType(statement_main.ColumnInt(column++)); |
| 411 info->interrupt_reason = | 417 info->interrupt_reason = |
| 412 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); | 418 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); |
| 413 statement_main.ColumnBlobAsString(column++, &info->hash); | 419 statement_main.ColumnBlobAsString(column++, &info->hash); |
| 414 info->end_time = | 420 info->end_time = |
| 415 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); | 421 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); |
| 416 info->opened = statement_main.ColumnInt(column++) != 0; | 422 info->opened = statement_main.ColumnInt(column++) != 0; |
| 423 info->last_access_time = |
| 424 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); |
| 417 info->referrer_url = GURL(statement_main.ColumnString(column++)); | 425 info->referrer_url = GURL(statement_main.ColumnString(column++)); |
| 418 info->site_url = GURL(statement_main.ColumnString(column++)); | 426 info->site_url = GURL(statement_main.ColumnString(column++)); |
| 419 info->tab_url = GURL(statement_main.ColumnString(column++)); | 427 info->tab_url = GURL(statement_main.ColumnString(column++)); |
| 420 info->tab_referrer_url = GURL(statement_main.ColumnString(column++)); | 428 info->tab_referrer_url = GURL(statement_main.ColumnString(column++)); |
| 421 info->http_method = statement_main.ColumnString(column++); | 429 info->http_method = statement_main.ColumnString(column++); |
| 422 info->by_ext_id = statement_main.ColumnString(column++); | 430 info->by_ext_id = statement_main.ColumnString(column++); |
| 423 info->by_ext_name = statement_main.ColumnString(column++); | 431 info->by_ext_name = statement_main.ColumnString(column++); |
| 424 info->etag = statement_main.ColumnString(column++); | 432 info->etag = statement_main.ColumnString(column++); |
| 425 info->last_modified = statement_main.ColumnString(column++); | 433 info->last_modified = statement_main.ColumnString(column++); |
| 426 | 434 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 return false; | 532 return false; |
| 525 } | 533 } |
| 526 | 534 |
| 527 sql::Statement statement(GetDB().GetCachedStatement( | 535 sql::Statement statement(GetDB().GetCachedStatement( |
| 528 SQL_FROM_HERE, | 536 SQL_FROM_HERE, |
| 529 "UPDATE downloads " | 537 "UPDATE downloads " |
| 530 "SET current_path=?, target_path=?, " | 538 "SET current_path=?, target_path=?, " |
| 531 "mime_type=?, original_mime_type=?, " | 539 "mime_type=?, original_mime_type=?, " |
| 532 "received_bytes=?, state=?, " | 540 "received_bytes=?, state=?, " |
| 533 "danger_type=?, interrupt_reason=?, hash=?, end_time=?, total_bytes=?, " | 541 "danger_type=?, interrupt_reason=?, hash=?, end_time=?, total_bytes=?, " |
| 534 "opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? " | 542 "opened=?, last_access_time=?, by_ext_id=?, by_ext_name=?, etag=?, " |
| 535 "WHERE id=?")); | 543 "last_modified=? WHERE id=?")); |
| 536 int column = 0; | 544 int column = 0; |
| 537 BindFilePath(statement, data.current_path, column++); | 545 BindFilePath(statement, data.current_path, column++); |
| 538 BindFilePath(statement, data.target_path, column++); | 546 BindFilePath(statement, data.target_path, column++); |
| 539 statement.BindString(column++, data.mime_type); | 547 statement.BindString(column++, data.mime_type); |
| 540 statement.BindString(column++, data.original_mime_type); | 548 statement.BindString(column++, data.original_mime_type); |
| 541 statement.BindInt64(column++, data.received_bytes); | 549 statement.BindInt64(column++, data.received_bytes); |
| 542 statement.BindInt(column++, DownloadStateToInt(data.state)); | 550 statement.BindInt(column++, DownloadStateToInt(data.state)); |
| 543 statement.BindInt(column++, DownloadDangerTypeToInt(data.danger_type)); | 551 statement.BindInt(column++, DownloadDangerTypeToInt(data.danger_type)); |
| 544 statement.BindInt(column++, | 552 statement.BindInt(column++, |
| 545 DownloadInterruptReasonToInt(data.interrupt_reason)); | 553 DownloadInterruptReasonToInt(data.interrupt_reason)); |
| 546 statement.BindBlob(column++, data.hash.data(), data.hash.size()); | 554 statement.BindBlob(column++, data.hash.data(), data.hash.size()); |
| 547 statement.BindInt64(column++, data.end_time.ToInternalValue()); | 555 statement.BindInt64(column++, data.end_time.ToInternalValue()); |
| 548 statement.BindInt64(column++, data.total_bytes); | 556 statement.BindInt64(column++, data.total_bytes); |
| 549 statement.BindInt(column++, (data.opened ? 1 : 0)); | 557 statement.BindInt(column++, (data.opened ? 1 : 0)); |
| 558 statement.BindInt64(column++, data.last_access_time.ToInternalValue()); |
| 550 statement.BindString(column++, data.by_ext_id); | 559 statement.BindString(column++, data.by_ext_id); |
| 551 statement.BindString(column++, data.by_ext_name); | 560 statement.BindString(column++, data.by_ext_name); |
| 552 statement.BindString(column++, data.etag); | 561 statement.BindString(column++, data.etag); |
| 553 statement.BindString(column++, data.last_modified); | 562 statement.BindString(column++, data.last_modified); |
| 554 statement.BindInt(column++, DownloadIdToInt(data.id)); | 563 statement.BindInt(column++, DownloadIdToInt(data.id)); |
| 555 | 564 |
| 556 if (!statement.Run()) | 565 if (!statement.Run()) |
| 557 return false; | 566 return false; |
| 558 | 567 |
| 559 if (data.download_slice_info.size() == 0) { | 568 if (data.download_slice_info.size() == 0) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 | 606 |
| 598 if (info.danger_type == DownloadDangerType::INVALID) | 607 if (info.danger_type == DownloadDangerType::INVALID) |
| 599 return false; | 608 return false; |
| 600 | 609 |
| 601 { | 610 { |
| 602 sql::Statement statement_insert(GetDB().GetCachedStatement( | 611 sql::Statement statement_insert(GetDB().GetCachedStatement( |
| 603 SQL_FROM_HERE, | 612 SQL_FROM_HERE, |
| 604 "INSERT INTO downloads " | 613 "INSERT INTO downloads " |
| 605 "(id, guid, current_path, target_path, mime_type, original_mime_type, " | 614 "(id, guid, current_path, target_path, mime_type, original_mime_type, " |
| 606 " start_time, received_bytes, total_bytes, state, danger_type, " | 615 " start_time, received_bytes, total_bytes, state, danger_type, " |
| 607 " interrupt_reason, hash, end_time, opened, referrer, " | 616 " interrupt_reason, hash, end_time, opened, last_access_time, " |
| 608 " site_url, tab_url, tab_referrer_url, http_method, " | 617 " referrer, site_url, tab_url, tab_referrer_url, http_method, " |
| 609 " by_ext_id, by_ext_name, etag, last_modified) " | 618 " by_ext_id, by_ext_name, etag, last_modified) " |
| 610 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 619 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 611 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 620 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 612 " ?, ?, ?, ?)")); | 621 " ?, ?, ?, ?, ?)")); |
| 613 | 622 |
| 614 int column = 0; | 623 int column = 0; |
| 615 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); | 624 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); |
| 616 statement_insert.BindString(column++, info.guid); | 625 statement_insert.BindString(column++, info.guid); |
| 617 BindFilePath(statement_insert, info.current_path, column++); | 626 BindFilePath(statement_insert, info.current_path, column++); |
| 618 BindFilePath(statement_insert, info.target_path, column++); | 627 BindFilePath(statement_insert, info.target_path, column++); |
| 619 statement_insert.BindString(column++, info.mime_type); | 628 statement_insert.BindString(column++, info.mime_type); |
| 620 statement_insert.BindString(column++, info.original_mime_type); | 629 statement_insert.BindString(column++, info.original_mime_type); |
| 621 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | 630 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); |
| 622 statement_insert.BindInt64(column++, info.received_bytes); | 631 statement_insert.BindInt64(column++, info.received_bytes); |
| 623 statement_insert.BindInt64(column++, info.total_bytes); | 632 statement_insert.BindInt64(column++, info.total_bytes); |
| 624 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); | 633 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); |
| 625 statement_insert.BindInt(column++, | 634 statement_insert.BindInt(column++, |
| 626 DownloadDangerTypeToInt(info.danger_type)); | 635 DownloadDangerTypeToInt(info.danger_type)); |
| 627 statement_insert.BindInt( | 636 statement_insert.BindInt( |
| 628 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); | 637 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); |
| 629 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); | 638 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); |
| 630 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | 639 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); |
| 631 statement_insert.BindInt(column++, info.opened ? 1 : 0); | 640 statement_insert.BindInt(column++, info.opened ? 1 : 0); |
| 641 statement_insert.BindInt64(column++, |
| 642 info.last_access_time.ToInternalValue()); |
| 632 statement_insert.BindString(column++, info.referrer_url.spec()); | 643 statement_insert.BindString(column++, info.referrer_url.spec()); |
| 633 statement_insert.BindString(column++, info.site_url.spec()); | 644 statement_insert.BindString(column++, info.site_url.spec()); |
| 634 statement_insert.BindString(column++, info.tab_url.spec()); | 645 statement_insert.BindString(column++, info.tab_url.spec()); |
| 635 statement_insert.BindString(column++, info.tab_referrer_url.spec()); | 646 statement_insert.BindString(column++, info.tab_referrer_url.spec()); |
| 636 statement_insert.BindString(column++, info.http_method); | 647 statement_insert.BindString(column++, info.http_method); |
| 637 statement_insert.BindString(column++, info.by_ext_id); | 648 statement_insert.BindString(column++, info.by_ext_id); |
| 638 statement_insert.BindString(column++, info.by_ext_name); | 649 statement_insert.BindString(column++, info.by_ext_name); |
| 639 statement_insert.BindString(column++, info.etag); | 650 statement_insert.BindString(column++, info.etag); |
| 640 statement_insert.BindString(column++, info.last_modified); | 651 statement_insert.BindString(column++, info.last_modified); |
| 641 if (!statement_insert.Run()) { | 652 if (!statement_insert.Run()) { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 "Download.DatabaseDownloadExistsForDownloadSlice", found); | 791 "Download.DatabaseDownloadExistsForDownloadSlice", found); |
| 781 if (!found) { | 792 if (!found) { |
| 782 RemoveDownloadSlices(info.download_id); | 793 RemoveDownloadSlices(info.download_id); |
| 783 continue; | 794 continue; |
| 784 } | 795 } |
| 785 it->second->download_slice_info.push_back(info); | 796 it->second->download_slice_info.push_back(info); |
| 786 } | 797 } |
| 787 } | 798 } |
| 788 | 799 |
| 789 } // namespace history | 800 } // namespace history |
| OLD | NEW |