| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // This GUID generation scheme is only used for migrated download rows and | 209 // This GUID generation scheme is only used for migrated download rows and |
| 210 // assumes that the likelihood of a collision with a GUID generated via | 210 // assumes that the likelihood of a collision with a GUID generated via |
| 211 // base::GenerateGUID() will be vanishingly small. | 211 // base::GenerateGUID() will be vanishingly small. |
| 212 const char kMigrateGuidsQuery[] = | 212 const char kMigrateGuidsQuery[] = |
| 213 "UPDATE downloads SET guid = printf" | 213 "UPDATE downloads SET guid = printf" |
| 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() { |
| 220 return EnsureColumnExists("tab_url", "VARCHAR NOT NULL DEFAULT ''") && |
| 221 EnsureColumnExists("tab_referrer_url", "VARCHAR NOT NULL DEFAULT ''"); |
| 222 } |
| 223 |
| 219 bool DownloadDatabase::InitDownloadTable() { | 224 bool DownloadDatabase::InitDownloadTable() { |
| 220 const char kSchema[] = | 225 const char kSchema[] = |
| 221 "CREATE TABLE downloads (" | 226 "CREATE TABLE downloads (" |
| 222 "id INTEGER PRIMARY KEY," // Primary key. | 227 "id INTEGER PRIMARY KEY," // Primary key. |
| 223 "guid VARCHAR NOT NULL," // GUID. | 228 "guid VARCHAR NOT NULL," // GUID. |
| 224 "current_path LONGVARCHAR NOT NULL," // Current disk location | 229 "current_path LONGVARCHAR NOT NULL," // Current disk location |
| 225 "target_path LONGVARCHAR NOT NULL," // Final disk location | 230 "target_path LONGVARCHAR NOT NULL," // Final disk location |
| 226 "start_time INTEGER NOT NULL," // When the download was started. | 231 "start_time INTEGER NOT NULL," // When the download was started. |
| 227 "received_bytes INTEGER NOT NULL," // Total size downloaded. | 232 "received_bytes INTEGER NOT NULL," // Total size downloaded. |
| 228 "total_bytes INTEGER NOT NULL," // Total size of the download. | 233 "total_bytes INTEGER NOT NULL," // Total size of the download. |
| 229 "state INTEGER NOT NULL," // 1=complete, 4=interrupted | 234 "state INTEGER NOT NULL," // 1=complete, 4=interrupted |
| 230 "danger_type INTEGER NOT NULL," // Danger type, validated. | 235 "danger_type INTEGER NOT NULL," // Danger type, validated. |
| 231 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason | 236 "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason |
| 232 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. | 237 "hash BLOB NOT NULL," // Raw SHA-256 hash of contents. |
| 233 "end_time INTEGER NOT NULL," // When the download completed. | 238 "end_time INTEGER NOT NULL," // When the download completed. |
| 234 "opened INTEGER NOT NULL," // 1 if it has ever been opened | 239 "opened INTEGER NOT NULL," // 1 if it has ever been opened |
| 235 // else 0 | 240 // else 0 |
| 236 "referrer VARCHAR NOT NULL," // HTTP Referrer | 241 "referrer VARCHAR NOT NULL," // HTTP Referrer |
| 242 "tab_url VARCHAR NOT NULL," // Tab URL for initiator. |
| 243 "tab_referrer_url VARCHAR NOT NULL," // Tag referrer URL for initiator. |
| 237 "http_method VARCHAR NOT NULL," // HTTP method. | 244 "http_method VARCHAR NOT NULL," // HTTP method. |
| 238 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the | 245 "by_ext_id VARCHAR NOT NULL," // ID of extension that started the |
| 239 // download | 246 // download |
| 240 "by_ext_name VARCHAR NOT NULL," // name of extension | 247 "by_ext_name VARCHAR NOT NULL," // name of extension |
| 241 "etag VARCHAR NOT NULL," // ETag | 248 "etag VARCHAR NOT NULL," // ETag |
| 242 "last_modified VARCHAR NOT NULL," // Last-Modified header | 249 "last_modified VARCHAR NOT NULL," // Last-Modified header |
| 243 "mime_type VARCHAR(255) NOT NULL," // MIME type. | 250 "mime_type VARCHAR(255) NOT NULL," // MIME type. |
| 244 "original_mime_type VARCHAR(255) NOT NULL)"; // Original MIME type. | 251 "original_mime_type VARCHAR(255) NOT NULL)"; // Original MIME type. |
| 245 | 252 |
| 246 const char kUrlChainSchema[] = | 253 const char kUrlChainSchema[] = |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 results->clear(); | 303 results->clear(); |
| 297 std::set<uint32_t> ids; | 304 std::set<uint32_t> ids; |
| 298 | 305 |
| 299 std::map<uint32_t, DownloadRow*> info_map; | 306 std::map<uint32_t, DownloadRow*> info_map; |
| 300 | 307 |
| 301 sql::Statement statement_main(GetDB().GetCachedStatement( | 308 sql::Statement statement_main(GetDB().GetCachedStatement( |
| 302 SQL_FROM_HERE, | 309 SQL_FROM_HERE, |
| 303 "SELECT id, guid, current_path, target_path, mime_type, " | 310 "SELECT id, guid, current_path, target_path, mime_type, " |
| 304 "original_mime_type, start_time, received_bytes, total_bytes, state, " | 311 "original_mime_type, start_time, received_bytes, total_bytes, state, " |
| 305 "danger_type, interrupt_reason, hash, end_time, opened, referrer, " | 312 "danger_type, interrupt_reason, hash, end_time, opened, referrer, " |
| 306 "http_method, by_ext_id, by_ext_name, etag, last_modified " | 313 "tab_url, tab_referrer_url, http_method, by_ext_id, by_ext_name, etag, " |
| 307 "FROM downloads ORDER BY start_time")); | 314 "last_modified FROM downloads ORDER BY start_time")); |
| 308 | 315 |
| 309 while (statement_main.Step()) { | 316 while (statement_main.Step()) { |
| 310 scoped_ptr<DownloadRow> info(new DownloadRow()); | 317 scoped_ptr<DownloadRow> info(new DownloadRow()); |
| 311 int column = 0; | 318 int column = 0; |
| 312 | 319 |
| 313 // SQLITE does not have unsigned integers, so explicitly handle negative | 320 // SQLITE does not have unsigned integers, so explicitly handle negative |
| 314 // |id|s instead of casting them to very large uint32s, which would break | 321 // |id|s instead of casting them to very large uint32s, which would break |
| 315 // the max(id) logic in GetNextDownloadId(). | 322 // the max(id) logic in GetNextDownloadId(). |
| 316 int64_t signed_id = statement_main.ColumnInt64(column++); | 323 int64_t signed_id = statement_main.ColumnInt64(column++); |
| 317 info->id = IntToDownloadId(signed_id); | 324 info->id = IntToDownloadId(signed_id); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 330 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); | 337 UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); |
| 331 info->danger_type = | 338 info->danger_type = |
| 332 IntToDownloadDangerType(statement_main.ColumnInt(column++)); | 339 IntToDownloadDangerType(statement_main.ColumnInt(column++)); |
| 333 info->interrupt_reason = | 340 info->interrupt_reason = |
| 334 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); | 341 IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); |
| 335 statement_main.ColumnBlobAsString(column++, &info->hash); | 342 statement_main.ColumnBlobAsString(column++, &info->hash); |
| 336 info->end_time = | 343 info->end_time = |
| 337 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); | 344 base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); |
| 338 info->opened = statement_main.ColumnInt(column++) != 0; | 345 info->opened = statement_main.ColumnInt(column++) != 0; |
| 339 info->referrer_url = GURL(statement_main.ColumnString(column++)); | 346 info->referrer_url = GURL(statement_main.ColumnString(column++)); |
| 347 info->tab_url = GURL(statement_main.ColumnString(column++)); |
| 348 info->tab_referrer_url = GURL(statement_main.ColumnString(column++)); |
| 340 info->http_method = statement_main.ColumnString(column++); | 349 info->http_method = statement_main.ColumnString(column++); |
| 341 info->by_ext_id = statement_main.ColumnString(column++); | 350 info->by_ext_id = statement_main.ColumnString(column++); |
| 342 info->by_ext_name = statement_main.ColumnString(column++); | 351 info->by_ext_name = statement_main.ColumnString(column++); |
| 343 info->etag = statement_main.ColumnString(column++); | 352 info->etag = statement_main.ColumnString(column++); |
| 344 info->last_modified = statement_main.ColumnString(column++); | 353 info->last_modified = statement_main.ColumnString(column++); |
| 345 | 354 |
| 346 // If the record is corrupted, note that and drop it. | 355 // If the record is corrupted, note that and drop it. |
| 347 // http://crbug.com/251269 | 356 // http://crbug.com/251269 |
| 348 DroppedReason dropped_reason = DROPPED_REASON_MAX; | 357 DroppedReason dropped_reason = DROPPED_REASON_MAX; |
| 349 if (signed_id <= static_cast<int64_t>(kInvalidDownloadId)) { | 358 if (signed_id <= static_cast<int64_t>(kInvalidDownloadId)) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 | 511 |
| 503 if (info.danger_type == DownloadDangerType::INVALID) | 512 if (info.danger_type == DownloadDangerType::INVALID) |
| 504 return false; | 513 return false; |
| 505 | 514 |
| 506 { | 515 { |
| 507 sql::Statement statement_insert(GetDB().GetCachedStatement( | 516 sql::Statement statement_insert(GetDB().GetCachedStatement( |
| 508 SQL_FROM_HERE, | 517 SQL_FROM_HERE, |
| 509 "INSERT INTO downloads " | 518 "INSERT INTO downloads " |
| 510 "(id, guid, current_path, target_path, mime_type, original_mime_type, " | 519 "(id, guid, current_path, target_path, mime_type, original_mime_type, " |
| 511 " start_time, received_bytes, total_bytes, state, danger_type, " | 520 " start_time, received_bytes, total_bytes, state, danger_type, " |
| 512 " interrupt_reason, hash, end_time, opened, referrer, http_method, " | 521 " interrupt_reason, hash, end_time, opened, referrer, tab_url, " |
| 513 " by_ext_id, by_ext_name, etag, last_modified) " | 522 " tab_referrer_url, http_method, by_ext_id, by_ext_name, etag, " |
| 523 " last_modified) " |
| 514 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 524 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 515 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " | 525 " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " |
| 516 " ?)")); | 526 " ?, ?, ?)")); |
| 517 | 527 |
| 518 int column = 0; | 528 int column = 0; |
| 519 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); | 529 statement_insert.BindInt(column++, DownloadIdToInt(info.id)); |
| 520 statement_insert.BindString(column++, info.guid); | 530 statement_insert.BindString(column++, info.guid); |
| 521 BindFilePath(statement_insert, info.current_path, column++); | 531 BindFilePath(statement_insert, info.current_path, column++); |
| 522 BindFilePath(statement_insert, info.target_path, column++); | 532 BindFilePath(statement_insert, info.target_path, column++); |
| 523 statement_insert.BindString(column++, info.mime_type); | 533 statement_insert.BindString(column++, info.mime_type); |
| 524 statement_insert.BindString(column++, info.original_mime_type); | 534 statement_insert.BindString(column++, info.original_mime_type); |
| 525 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); | 535 statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); |
| 526 statement_insert.BindInt64(column++, info.received_bytes); | 536 statement_insert.BindInt64(column++, info.received_bytes); |
| 527 statement_insert.BindInt64(column++, info.total_bytes); | 537 statement_insert.BindInt64(column++, info.total_bytes); |
| 528 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); | 538 statement_insert.BindInt(column++, DownloadStateToInt(info.state)); |
| 529 statement_insert.BindInt(column++, | 539 statement_insert.BindInt(column++, |
| 530 DownloadDangerTypeToInt(info.danger_type)); | 540 DownloadDangerTypeToInt(info.danger_type)); |
| 531 statement_insert.BindInt( | 541 statement_insert.BindInt( |
| 532 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); | 542 column++, DownloadInterruptReasonToInt(info.interrupt_reason)); |
| 533 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); | 543 statement_insert.BindBlob(column++, info.hash.data(), info.hash.size()); |
| 534 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); | 544 statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); |
| 535 statement_insert.BindInt(column++, info.opened ? 1 : 0); | 545 statement_insert.BindInt(column++, info.opened ? 1 : 0); |
| 536 statement_insert.BindString(column++, info.referrer_url.spec()); | 546 statement_insert.BindString(column++, info.referrer_url.spec()); |
| 547 statement_insert.BindString(column++, info.tab_url.spec()); |
| 548 statement_insert.BindString(column++, info.tab_referrer_url.spec()); |
| 537 statement_insert.BindString(column++, info.http_method); | 549 statement_insert.BindString(column++, info.http_method); |
| 538 statement_insert.BindString(column++, info.by_ext_id); | 550 statement_insert.BindString(column++, info.by_ext_id); |
| 539 statement_insert.BindString(column++, info.by_ext_name); | 551 statement_insert.BindString(column++, info.by_ext_name); |
| 540 statement_insert.BindString(column++, info.etag); | 552 statement_insert.BindString(column++, info.etag); |
| 541 statement_insert.BindString(column++, info.last_modified); | 553 statement_insert.BindString(column++, info.last_modified); |
| 542 if (!statement_insert.Run()) { | 554 if (!statement_insert.Run()) { |
| 543 // GetErrorCode() returns a bitmask where the lower byte is a more general | 555 // GetErrorCode() returns a bitmask where the lower byte is a more general |
| 544 // code and the upper byte is a more specific code. In order to save | 556 // code and the upper byte is a more specific code. In order to save |
| 545 // memory, take the general code, of which there are fewer than 50. See | 557 // memory, take the general code, of which there are fewer than 50. See |
| 546 // also sql/connection.cc | 558 // also sql/connection.cc |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 size_t DownloadDatabase::CountDownloads() { | 626 size_t DownloadDatabase::CountDownloads() { |
| 615 EnsureInProgressEntriesCleanedUp(); | 627 EnsureInProgressEntriesCleanedUp(); |
| 616 | 628 |
| 617 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 629 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 618 "SELECT count(*) from downloads")); | 630 "SELECT count(*) from downloads")); |
| 619 statement.Step(); | 631 statement.Step(); |
| 620 return statement.ColumnInt(0); | 632 return statement.ColumnInt(0); |
| 621 } | 633 } |
| 622 | 634 |
| 623 } // namespace history | 635 } // namespace history |
| OLD | NEW |