OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/thumbnail_database.h" | 5 #include "chrome/browser/history/thumbnail_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "app/sql/statement.h" | 10 #include "app/sql/statement.h" |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 "UPDATE favicons SET image_data=NULL, last_updated=? WHERE id=?")); | 380 "UPDATE favicons SET image_data=NULL, last_updated=? WHERE id=?")); |
381 if (!statement) | 381 if (!statement) |
382 return 0; | 382 return 0; |
383 | 383 |
384 statement.BindInt64(0, time.ToTimeT()); | 384 statement.BindInt64(0, time.ToTimeT()); |
385 statement.BindInt64(1, icon_id); | 385 statement.BindInt64(1, icon_id); |
386 return statement.Run(); | 386 return statement.Run(); |
387 } | 387 } |
388 } | 388 } |
389 | 389 |
390 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FavIconID icon_id, | 390 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, |
391 base::Time time) { | 391 base::Time time) { |
392 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 392 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
393 "UPDATE favicons SET last_updated=? WHERE id=?")); | 393 "UPDATE favicons SET last_updated=? WHERE id=?")); |
394 if (!statement) | 394 if (!statement) |
395 return 0; | 395 return 0; |
396 | 396 |
397 statement.BindInt64(0, time.ToTimeT()); | 397 statement.BindInt64(0, time.ToTimeT()); |
398 statement.BindInt64(1, icon_id); | 398 statement.BindInt64(1, icon_id); |
399 return statement.Run(); | 399 return statement.Run(); |
400 } | 400 } |
401 | 401 |
402 FavIconID ThumbnailDatabase::GetFavIconIDForFavIconURL(const GURL& icon_url, | 402 FaviconID ThumbnailDatabase::GetFaviconIDForFavIconURL(const GURL& icon_url, |
403 int required_icon_type, | 403 int required_icon_type, |
404 IconType* icon_type) { | 404 IconType* icon_type) { |
405 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 405 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
406 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " | 406 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " |
407 "ORDER BY icon_type DESC")); | 407 "ORDER BY icon_type DESC")); |
408 if (!statement) | 408 if (!statement) |
409 return 0; | 409 return 0; |
410 | 410 |
411 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); | 411 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); |
412 statement.BindInt(1, required_icon_type); | 412 statement.BindInt(1, required_icon_type); |
413 if (!statement.Step()) | 413 if (!statement.Step()) |
414 return 0; // not cached | 414 return 0; // not cached |
415 | 415 |
416 if (icon_type) | 416 if (icon_type) |
417 *icon_type = static_cast<IconType>(statement.ColumnInt(1)); | 417 *icon_type = static_cast<IconType>(statement.ColumnInt(1)); |
418 return statement.ColumnInt64(0); | 418 return statement.ColumnInt64(0); |
419 } | 419 } |
420 | 420 |
421 bool ThumbnailDatabase::GetFavicon( | 421 bool ThumbnailDatabase::GetFavicon( |
422 FavIconID icon_id, | 422 FaviconID icon_id, |
423 base::Time* last_updated, | 423 base::Time* last_updated, |
424 std::vector<unsigned char>* png_icon_data, | 424 std::vector<unsigned char>* png_icon_data, |
425 GURL* icon_url) { | 425 GURL* icon_url) { |
426 DCHECK(icon_id); | 426 DCHECK(icon_id); |
427 | 427 |
428 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 428 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
429 "SELECT last_updated, image_data, url FROM favicons WHERE id=?")); | 429 "SELECT last_updated, image_data, url FROM favicons WHERE id=?")); |
430 if (!statement) | 430 if (!statement) |
431 return 0; | 431 return 0; |
432 | 432 |
433 statement.BindInt64(0, icon_id); | 433 statement.BindInt64(0, icon_id); |
434 | 434 |
435 if (!statement.Step()) | 435 if (!statement.Step()) |
436 return false; // No entry for the id. | 436 return false; // No entry for the id. |
437 | 437 |
438 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); | 438 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); |
439 if (statement.ColumnByteLength(1) > 0) | 439 if (statement.ColumnByteLength(1) > 0) |
440 statement.ColumnBlobAsVector(1, png_icon_data); | 440 statement.ColumnBlobAsVector(1, png_icon_data); |
441 if (icon_url) | 441 if (icon_url) |
442 *icon_url = GURL(statement.ColumnString(2)); | 442 *icon_url = GURL(statement.ColumnString(2)); |
443 | 443 |
444 return true; | 444 return true; |
445 } | 445 } |
446 | 446 |
447 FavIconID ThumbnailDatabase::AddFavIcon(const GURL& icon_url, | 447 FaviconID ThumbnailDatabase::AddFavIcon(const GURL& icon_url, |
448 IconType icon_type) { | 448 IconType icon_type) { |
449 | 449 |
450 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 450 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
451 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); | 451 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); |
452 if (!statement) | 452 if (!statement) |
453 return 0; | 453 return 0; |
454 | 454 |
455 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); | 455 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); |
456 statement.BindInt(1, icon_type); | 456 statement.BindInt(1, icon_type); |
457 if (!statement.Run()) | 457 if (!statement.Run()) |
458 return 0; | 458 return 0; |
459 return db_.GetLastInsertRowId(); | 459 return db_.GetLastInsertRowId(); |
460 } | 460 } |
461 | 461 |
462 bool ThumbnailDatabase::DeleteFavIcon(FavIconID id) { | 462 bool ThumbnailDatabase::DeleteFavIcon(FaviconID id) { |
463 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 463 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
464 "DELETE FROM favicons WHERE id = ?")); | 464 "DELETE FROM favicons WHERE id = ?")); |
465 if (!statement) | 465 if (!statement) |
466 return false; | 466 return false; |
467 | 467 |
468 statement.BindInt64(0, id); | 468 statement.BindInt64(0, id); |
469 return statement.Run(); | 469 return statement.Run(); |
470 } | 470 } |
471 | 471 |
472 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, | 472 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 return result; | 510 return result; |
511 | 511 |
512 IconMapping icon_mapping; | 512 IconMapping icon_mapping; |
513 FillIconMapping(statement, page_url, &icon_mapping); | 513 FillIconMapping(statement, page_url, &icon_mapping); |
514 mapping_data->push_back(icon_mapping); | 514 mapping_data->push_back(icon_mapping); |
515 } | 515 } |
516 return result; | 516 return result; |
517 } | 517 } |
518 | 518 |
519 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, | 519 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, |
520 FavIconID icon_id) { | 520 FaviconID icon_id) { |
521 return AddIconMapping(page_url, icon_id, false); | 521 return AddIconMapping(page_url, icon_id, false); |
522 } | 522 } |
523 | 523 |
524 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id, | 524 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id, |
525 FavIconID icon_id) { | 525 FaviconID icon_id) { |
526 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 526 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
527 "UPDATE icon_mapping SET icon_id=? WHERE id=?")); | 527 "UPDATE icon_mapping SET icon_id=? WHERE id=?")); |
528 if (!statement) | 528 if (!statement) |
529 return 0; | 529 return 0; |
530 | 530 |
531 statement.BindInt64(0, icon_id); | 531 statement.BindInt64(0, icon_id); |
532 statement.BindInt64(1, mapping_id); | 532 statement.BindInt64(1, mapping_id); |
533 return statement.Run(); | 533 return statement.Run(); |
534 } | 534 } |
535 | 535 |
536 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) { | 536 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) { |
537 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 537 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
538 "DELETE FROM icon_mapping WHERE page_url = ?")); | 538 "DELETE FROM icon_mapping WHERE page_url = ?")); |
539 if (!statement) | 539 if (!statement) |
540 return false; | 540 return false; |
541 | 541 |
542 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); | 542 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); |
543 return statement.Run(); | 543 return statement.Run(); |
544 } | 544 } |
545 | 545 |
546 bool ThumbnailDatabase::HasMappingFor(FavIconID id) { | 546 bool ThumbnailDatabase::HasMappingFor(FaviconID id) { |
547 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 547 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
548 "SELECT id FROM icon_mapping " | 548 "SELECT id FROM icon_mapping " |
549 "WHERE icon_id=?")); | 549 "WHERE icon_id=?")); |
550 if (!statement) | 550 if (!statement) |
551 return false; | 551 return false; |
552 | 552 |
553 statement.BindInt64(0, id); | 553 statement.BindInt64(0, id); |
554 return statement.Step(); | 554 return statement.Step(); |
555 } | 555 } |
556 | 556 |
557 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) { | 557 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) { |
558 URLDatabase::IconMappingEnumerator e; | 558 URLDatabase::IconMappingEnumerator e; |
559 if (!url_db->InitIconMappingEnumeratorForEverything(&e)) | 559 if (!url_db->InitIconMappingEnumeratorForEverything(&e)) |
560 return false; | 560 return false; |
561 | 561 |
562 IconMapping info; | 562 IconMapping info; |
563 while (e.GetNextIconMapping(&info)) { | 563 while (e.GetNextIconMapping(&info)) { |
564 // TODO: Using bulk insert to improve the performance. | 564 // TODO: Using bulk insert to improve the performance. |
565 if (!AddIconMapping(info.page_url, info.icon_id)) | 565 if (!AddIconMapping(info.page_url, info.icon_id)) |
566 return false; | 566 return false; |
567 } | 567 } |
568 return true; | 568 return true; |
569 } | 569 } |
570 | 570 |
571 IconMappingID ThumbnailDatabase::AddToTemporaryIconMappingTable( | 571 IconMappingID ThumbnailDatabase::AddToTemporaryIconMappingTable( |
572 const GURL& page_url, const FavIconID icon_id) { | 572 const GURL& page_url, const FaviconID icon_id) { |
573 return AddIconMapping(page_url, icon_id, true); | 573 return AddIconMapping(page_url, icon_id, true); |
574 } | 574 } |
575 | 575 |
576 bool ThumbnailDatabase::CommitTemporaryIconMappingTable() { | 576 bool ThumbnailDatabase::CommitTemporaryIconMappingTable() { |
577 // Delete the old icon_mapping table. | 577 // Delete the old icon_mapping table. |
578 if (!db_.Execute("DROP TABLE icon_mapping")) | 578 if (!db_.Execute("DROP TABLE icon_mapping")) |
579 return false; | 579 return false; |
580 | 580 |
581 // Rename the temporary one. | 581 // Rename the temporary one. |
582 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping")) | 582 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping")) |
583 return false; | 583 return false; |
584 | 584 |
585 // The renamed table needs the index (the temporary table doesn't have one). | 585 // The renamed table needs the index (the temporary table doesn't have one). |
586 InitIconMappingIndex(); | 586 InitIconMappingIndex(); |
587 | 587 |
588 return true; | 588 return true; |
589 } | 589 } |
590 | 590 |
591 FavIconID ThumbnailDatabase::CopyToTemporaryFavIconTable(FavIconID source) { | 591 FaviconID ThumbnailDatabase::CopyToTemporaryFavIconTable(FaviconID source) { |
592 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 592 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
593 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" | 593 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" |
594 "SELECT url, last_updated, image_data, icon_type " | 594 "SELECT url, last_updated, image_data, icon_type " |
595 "FROM favicons WHERE id = ?")); | 595 "FROM favicons WHERE id = ?")); |
596 if (!statement) | 596 if (!statement) |
597 return 0; | 597 return 0; |
598 statement.BindInt64(0, source); | 598 statement.BindInt64(0, source); |
599 if (!statement.Run()) | 599 if (!statement.Run()) |
600 return 0; | 600 return 0; |
601 | 601 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 | 714 |
715 void ThumbnailDatabase::InitIconMappingIndex() { | 715 void ThumbnailDatabase::InitIconMappingIndex() { |
716 // Add an index on the url column. We ignore errors. Since this is always | 716 // Add an index on the url column. We ignore errors. Since this is always |
717 // called during startup, the index will normally already exist. | 717 // called during startup, the index will normally already exist. |
718 db_.Execute("CREATE INDEX icon_mapping_page_url_idx" | 718 db_.Execute("CREATE INDEX icon_mapping_page_url_idx" |
719 " ON icon_mapping(page_url)"); | 719 " ON icon_mapping(page_url)"); |
720 db_.Execute("CREATE INDEX icon_mapping_icon_id_idx ON icon_mapping(icon_id)"); | 720 db_.Execute("CREATE INDEX icon_mapping_icon_id_idx ON icon_mapping(icon_id)"); |
721 } | 721 } |
722 | 722 |
723 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, | 723 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, |
724 FavIconID icon_id, | 724 FaviconID icon_id, |
725 bool is_temporary) { | 725 bool is_temporary) { |
726 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; | 726 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; |
727 const char* statement_name = | 727 const char* statement_name = |
728 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; | 728 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; |
729 | 729 |
730 std::string sql; | 730 std::string sql; |
731 sql.append("INSERT INTO "); | 731 sql.append("INSERT INTO "); |
732 sql.append(name); | 732 sql.append(name); |
733 sql.append("(page_url, icon_id) VALUES (?, ?)"); | 733 sql.append("(page_url, icon_id) VALUES (?, ?)"); |
734 | 734 |
(...skipping 17 matching lines...) Expand all Loading... |
752 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) { | 752 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) { |
753 NOTREACHED(); | 753 NOTREACHED(); |
754 return false; | 754 return false; |
755 } | 755 } |
756 meta_table_.SetVersionNumber(4); | 756 meta_table_.SetVersionNumber(4); |
757 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber)); | 757 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber)); |
758 return true; | 758 return true; |
759 } | 759 } |
760 | 760 |
761 } // namespace history | 761 } // namespace history |
OLD | NEW |