| 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 <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 "(\"%08X-%s-%s-%s-%s\", id, hex(randomblob(2)), hex(randomblob(2))," | 214 "(\"%08X-%s-%s-%s-%s\", id, hex(randomblob(2)), hex(randomblob(2))," |
| 215 " hex(randomblob(2)), hex(randomblob(6)))"; | 215 " hex(randomblob(2)), hex(randomblob(6)))"; |
| 216 return GetDB().Execute(kMigrateGuidsQuery); | 216 return GetDB().Execute(kMigrateGuidsQuery); |
| 217 } | 217 } |
| 218 | 218 |
| 219 bool DownloadDatabase::MigrateDownloadTabUrl() { | 219 bool DownloadDatabase::MigrateDownloadTabUrl() { |
| 220 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && | 220 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && |
| 221 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); | 221 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); |
| 222 } | 222 } |
| 223 | 223 |
| 224 bool DownloadDatabase::MigrateDownloadSiteInstanceUrl() { |
| 225 return EnsureColumnExists("site_url", "VARCHAR NOT NULL DEFAULT ''"); |
| 226 } |
| 227 |
| 224 bool DownloadDatabase::InitDownloadTable() { | 228 bool DownloadDatabase::InitDownloadTable() { |
| 225 const char kSchema[] = | 229 const char kSchema[] = |
| 226 "CREATE TABLE downloads (" | 230 "CREATE TABLE downloads (" |
| 227 "id INTEGER PRIMARY KEY," // Primary key. | 231 "id INTEGER PRIMARY KEY," // Primary key. |
| 228 "guid VARCHAR NOT NULL," // GUID. | 232 "guid VARCHAR NOT NULL," // GUID. |
| 229 "current_path LONGVARCHAR NOT NULL," // Current disk location | 233 "current_path LONGVARCHAR NOT NULL," // Current disk location |
| 230 "target_path LONGVARCHAR NOT NULL," // Final disk location | 234 "target_path LONGVARCHAR NOT NULL," // Final disk location |
| 231 "start_time INTEGER NOT NULL," // When the download was started. | 235 "start_time INTEGER NOT NULL," // When the download was started. |
| 232 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 236 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
| 233 "total_bytes INTEGER NOT NULL," // Total size of the download. | 237 "total_bytes INTEGER NOT NULL," // Total size of the download. |
| 234 "state INTEGER NOT NULL," // 1=complete, 4=interrupted | 238 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
| 235 "danger_type INTEGER NOT NULL," // Danger type, validated. | 239 "danger_type INTEGER NOT NULL," // Danger type, validated. |
| 236 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason | 240 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason |
| 237 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. | 241 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. |
| 238 "end_time INTEGER NOT NULL," // When the download completed. | 242 "end_time INTEGER NOT NULL," // When the download completed. |
| 239 "opened INTEGER NOT NULL," // 1 if it has ever been opened | 243 "opened INTEGER NOT NULL," // 1 if it has ever been opened |
| 240 // else 0 | 244 // else 0 |
| 241 "referrer VARCHAR NOT NULL," // HTTP Referrer | 245 "referrer VARCHAR NOT NULL," // HTTP Referrer |
| 246 "site_url VARCHAR NOT NULL," // Site URL for initiating site |
| 247 // instance. |
| 242 "tab_url VARCHAR NOT NULL," // Tab URL for initiator. | 248 "tab_url VARCHAR NOT NULL," // Tab URL for initiator. |
| 243 "tab_referrer_url VARCHAR NOT NULL," // Tag referrer URL for initiator. | 249 "tab_referrer_url VARCHAR NOT NULL," // Tag referrer URL for initiator. |
| 244 "http_method VARCHAR NOT NULL," // HTTP method. | 250 "http_method VARCHAR NOT NULL," // HTTP method. |
| 245 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the | 251 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the |
| 246 // download | 252 // download |
| 247 "by_ext_name VARCHAR NOT NULL," // name of extension | 253 "by_ext_name VARCHAR NOT NULL," // name of extension |
| 248 "etag VARCHAR NOT NULL," // ETag | 254 "etag VARCHAR NOT NULL," // ETag |
| 249 "last_modified VARCHAR NOT NULL," // Last-Modified header | 255 "last_modified VARCHAR NOT NULL," // Last-Modified header |
| 250 "mime_type VARCHAR(255) NOT NULL," // MIME type. | 256 "mime_type VARCHAR(255) NOT NULL," // MIME type. |
| 251 "original_mime_type VARCHAR(255) NOT NULL)"; // Original MIME type. | 257 "original_mime_type VARCHAR(255) NOT NULL)"; // Original MIME type. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 results->clear(); | 309 results->clear(); |
| 304 std::set<uint32_t> ids; | 310 std::set<uint32_t> ids; |
| 305 | 311 |
| 306 std::map<uint32_t, DownloadRow*> info_map; | 312 std::map<uint32_t, DownloadRow*> info_map; |
| 307 | 313 |
| 308 sql::Statement statement_main(GetDB().GetCachedStatement( | 314 sql::Statement statement_main(GetDB().GetCachedStatement( |
| 309 SQL_FROM_HERE, | 315 SQL_FROM_HERE, |
| 310 "SELECT id, guid, current_path, target_path, mime_type, " | 316 "SELECT id, guid, current_path, target_path, mime_type, " |
| 311 "original_mime_type, start_time, received_bytes, total_bytes, state, " | 317 "original_mime_type, start_time, received_bytes, total_bytes, state, " |
| 312 "danger_type, interrupt_reason, hash, end_time, opened, referrer, " | 318 "danger_type, interrupt_reason, hash, end_time, opened, referrer, " |
| 313 "tab_url, tab_referrer_url, http_method, by_ext_id, by_ext_name, etag, " | 319 "site_url, tab_url, tab_referrer_url, http_method, by_ext_id, " |
| 314 "last_modified FROM downloads ORDER BY start_time")); | 320 "by_ext_name, etag, last_modified FROM downloads ORDER BY start_time")); |
| 315 | 321 |
| 316 while (statement_main.Step()) { | 322 while (statement_main.Step()) { |
| 317 scoped_ptr<DownloadRow> info(new DownloadRow()); | 323 scoped_ptr<DownloadRow> info(new DownloadRow()); |
| 318 int column = 0; | 324 int column = 0; |
| 319 | 325 |
| 320 // SQLITE does not have unsigned integers, so explicitly handle negative | 326 // SQLITE does not have unsigned integers, so explicitly handle negative |
| 321 // |id|s instead of casting them to very large uint32s, which would break | 327 // |id|s instead of casting them to very large uint32s, which would break |
| 322 // the max(id) logic in GetNextDownloadId(). | 328 // the max(id) logic in GetNextDownloadId(). |
| 323 int64_t signed_id = statement_main.ColumnInt64(column++); | 329 int64_t signed_id = statement_main.ColumnInt64(column++); |
| 324 info->id = IntToDownloadId(signed_id); | 330 info->id = IntToDownloadId(signed_id); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 337 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 343 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
| 338 info->danger_type = | 344 info->danger_type = |
| 339 IntToDownloadDangerType(statement_main.ColumnInt(column++)); | 345 IntToDownloadDangerType(statement_main.ColumnInt(column++)); |
| 340 info->interrupt_reason = | 346 info->interrupt_reason = |
| 341 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); | 347 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); |
| 342 statement_main.ColumnBlobAsString(column++, &info->hash); | 348 statement_main.ColumnBlobAsString(column++, &info->hash); |
| 343 info->end_time = | 349 info->end_time = |
| 344 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); | 350 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); |
| 345 info->opened = statement_main.ColumnInt(column++) != 0; | 351 info->opened = statement_main.ColumnInt(column++) != 0; |
| 346 info->referrer_url = GURL(statement_main.ColumnString(column++)); | 352 info->referrer_url = GURL(statement_main.ColumnString(column++)); |
| 353 info->site_url = GURL(statement_main.ColumnString(column++)); |
| 347 info->tab_url = GURL(statement_main.ColumnString(column++)); | 354 info->tab_url = GURL(statement_main.ColumnString(column++)); |
| 348 info->tab_referrer_url = GURL(statement_main.ColumnString(column++)); | 355 info->tab_referrer_url = GURL(statement_main.ColumnString(column++)); |
| 349 info->http_method = statement_main.ColumnString(column++); | 356 info->http_method = statement_main.ColumnString(column++); |
| 350 info->by_ext_id = statement_main.ColumnString(column++); | 357 info->by_ext_id = statement_main.ColumnString(column++); |
| 351 info->by_ext_name = statement_main.ColumnString(column++); | 358 info->by_ext_name = statement_main.ColumnString(column++); |
| 352 info->etag = statement_main.ColumnString(column++); | 359 info->etag = statement_main.ColumnString(column++); |
| 353 info->last_modified = statement_main.ColumnString(column++); | 360 info->last_modified = statement_main.ColumnString(column++); |
| 354 | 361 |
| 355 // If the record is corrupted, note that and drop it. | 362 // If the record is corrupted, note that and drop it. |
| 356 // http://crbug.com/251269 | 363 // http://crbug.com/251269 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 | 518 |
| 512 if (info.danger_type == DownloadDangerType::INVALID) | 519 if (info.danger_type == DownloadDangerType::INVALID) |
| 513 return false; | 520 return false; |
| 514 | 521 |
| 515 { | 522 { |
| 516 sql::Statement statement_insert(GetDB().GetCachedStatement( | 523 sql::Statement statement_insert(GetDB().GetCachedStatement( |
| 517 SQL_FROM_HERE, | 524 SQL_FROM_HERE, |
| 518 "INSERT INTO downloads " | 525 "INSERT INTO downloads " |
| 519 "(id, guid, current_path, target_path, mime_type, original_mime_type, " | 526 "(id, guid, current_path, target_path, mime_type, original_mime_type, " |
| 520 " start_time, received_bytes, total_bytes, state, danger_type, " | 527 " start_time, received_bytes, total_bytes, state, danger_type, " |
| 521 " interrupt_reason, hash, end_time, opened, referrer, tab_url, " | 528 " interrupt_reason, hash, end_time, opened, referrer, " |
| 522 " tab_referrer_url, http_method, by_ext_id, by_ext_name, etag, " | 529 " site_url, tab_url, tab_referrer_url, http_method, " |
| 523 " last_modified) " | 530 " by_ext_id, by_ext_name, etag, last_modified) " |
| 524 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 531 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 525 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 532 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 526 " ?, ?, ?)")); | 533 " ?, ?, ?, ?)")); |
| 527 | 534 |
| 528 int column = 0; | 535 int column = 0; |
| 529 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); | 536 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); |
| 530 statement_insert.BindString(column++, info.guid); | 537 statement_insert.BindString(column++, info.guid); |
| 531 BindFilePath(statement_insert, info.current_path, column++); | 538 BindFilePath(statement_insert, info.current_path, column++); |
| 532 BindFilePath(statement_insert, info.target_path, column++); | 539 BindFilePath(statement_insert, info.target_path, column++); |
| 533 statement_insert.BindString(column++, info.mime_type); | 540 statement_insert.BindString(column++, info.mime_type); |
| 534 statement_insert.BindString(column++, info.original_mime_type); | 541 statement_insert.BindString(column++, info.original_mime_type); |
| 535 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | 542 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); |
| 536 statement_insert.BindInt64(column++, info.received_bytes); | 543 statement_insert.BindInt64(column++, info.received_bytes); |
| 537 statement_insert.BindInt64(column++, info.total_bytes); | 544 statement_insert.BindInt64(column++, info.total_bytes); |
| 538 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); | 545 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); |
| 539 statement_insert.BindInt(column++, | 546 statement_insert.BindInt(column++, |
| 540 DownloadDangerTypeToInt(info.danger_type)); | 547 DownloadDangerTypeToInt(info.danger_type)); |
| 541 statement_insert.BindInt( | 548 statement_insert.BindInt( |
| 542 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); | 549 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); |
| 543 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); | 550 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); |
| 544 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | 551 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); |
| 545 statement_insert.BindInt(column++, info.opened ? 1 : 0); | 552 statement_insert.BindInt(column++, info.opened ? 1 : 0); |
| 546 statement_insert.BindString(column++, info.referrer_url.spec()); | 553 statement_insert.BindString(column++, info.referrer_url.spec()); |
| 554 statement_insert.BindString(column++, info.site_url.spec()); |
| 547 statement_insert.BindString(column++, info.tab_url.spec()); | 555 statement_insert.BindString(column++, info.tab_url.spec()); |
| 548 statement_insert.BindString(column++, info.tab_referrer_url.spec()); | 556 statement_insert.BindString(column++, info.tab_referrer_url.spec()); |
| 549 statement_insert.BindString(column++, info.http_method); | 557 statement_insert.BindString(column++, info.http_method); |
| 550 statement_insert.BindString(column++, info.by_ext_id); | 558 statement_insert.BindString(column++, info.by_ext_id); |
| 551 statement_insert.BindString(column++, info.by_ext_name); | 559 statement_insert.BindString(column++, info.by_ext_name); |
| 552 statement_insert.BindString(column++, info.etag); | 560 statement_insert.BindString(column++, info.etag); |
| 553 statement_insert.BindString(column++, info.last_modified); | 561 statement_insert.BindString(column++, info.last_modified); |
| 554 if (!statement_insert.Run()) { | 562 if (!statement_insert.Run()) { |
| 555 // GetErrorCode() returns a bitmask where the lower byte is a more general | 563 // GetErrorCode() returns a bitmask where the lower byte is a more general |
| 556 // code and the upper byte is a more specific code. In order to save | 564 // code and the upper byte is a more specific code. In order to save |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 size_t DownloadDatabase::CountDownloads() { | 634 size_t DownloadDatabase::CountDownloads() { |
| 627 EnsureInProgressEntriesCleanedUp(); | 635 EnsureInProgressEntriesCleanedUp(); |
| 628 | 636 |
| 629 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 637 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 630 "SELECT count(*) from downloads")); | 638 "SELECT count(*) from downloads")); |
| 631 statement.Step(); | 639 statement.Step(); |
| 632 return statement.ColumnInt(0); | 640 return statement.ColumnInt(0); |
| 633 } | 641 } |
| 634 | 642 |
| 635 } // namespace history | 643 } // namespace history |
| OLD | NEW |