| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 #if defined(OS_MACOSX) | 78 #if defined(OS_MACOSX) |
| 79 // Exclude the thumbnails file from backups. | 79 // Exclude the thumbnails file from backups. |
| 80 base::mac::SetFileBackupExclusion(db_name); | 80 base::mac::SetFileBackupExclusion(db_name); |
| 81 #endif | 81 #endif |
| 82 | 82 |
| 83 // Create the tables. | 83 // Create the tables. |
| 84 if (!meta_table_.Init(&db_, kCurrentVersionNumber, | 84 if (!meta_table_.Init(&db_, kCurrentVersionNumber, |
| 85 kCompatibleVersionNumber) || | 85 kCompatibleVersionNumber) || |
| 86 !InitThumbnailTable() || | 86 !InitThumbnailTable() || |
| 87 !InitFaviconsTable(&db_, false) || | 87 !InitFaviconsTable(&db_, false) || |
| 88 !InitIconMappingTable(&db_, false)) { | 88 !InitFaviconsIndex() || |
| 89 !InitIconMappingTable(&db_, false) || |
| 90 !InitIconMappingIndex()) { |
| 89 db_.Close(); | 91 db_.Close(); |
| 90 return sql::INIT_FAILURE; | 92 return sql::INIT_FAILURE; |
| 91 } | 93 } |
| 92 InitFaviconsIndex(); | |
| 93 InitIconMappingIndex(); | |
| 94 | 94 |
| 95 // Version check. We should not encounter a database too old for us to handle | 95 // Version check. We should not encounter a database too old for us to handle |
| 96 // in the wild, so we try to continue in that case. | 96 // in the wild, so we try to continue in that case. |
| 97 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 97 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| 98 LOG(WARNING) << "Thumbnail database is too new."; | 98 LOG(WARNING) << "Thumbnail database is too new."; |
| 99 return sql::INIT_TOO_NEW; | 99 return sql::INIT_TOO_NEW; |
| 100 } | 100 } |
| 101 | 101 |
| 102 int cur_version = meta_table_.GetVersionNumber(); | 102 int cur_version = meta_table_.GetVersionNumber(); |
| 103 if (cur_version == 2) { | 103 if (cur_version == 2) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // Set the default icon_type as FAVICON to be consistent with | 212 // Set the default icon_type as FAVICON to be consistent with |
| 213 // table upgrade in UpgradeToVersion4(). | 213 // table upgrade in UpgradeToVersion4(). |
| 214 "icon_type INTEGER DEFAULT 1," | 214 "icon_type INTEGER DEFAULT 1," |
| 215 "sizes LONGVARCHAR)"); | 215 "sizes LONGVARCHAR)"); |
| 216 if (!db->Execute(sql.c_str())) | 216 if (!db->Execute(sql.c_str())) |
| 217 return false; | 217 return false; |
| 218 } | 218 } |
| 219 return true; | 219 return true; |
| 220 } | 220 } |
| 221 | 221 |
| 222 void ThumbnailDatabase::InitFaviconsIndex() { | 222 bool ThumbnailDatabase::InitFaviconsIndex() { |
| 223 // Add an index on the url column. We ignore errors. Since this is always | 223 // Add an index on the url column. |
| 224 // called during startup, the index will normally already exist. | 224 return |
| 225 db_.Execute("CREATE INDEX favicons_url ON favicons(url)"); | 225 db_.Execute("CREATE INDEX IF NOT EXISTS favicons_url ON favicons(url)"); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void ThumbnailDatabase::BeginTransaction() { | 228 void ThumbnailDatabase::BeginTransaction() { |
| 229 db_.BeginTransaction(); | 229 db_.BeginTransaction(); |
| 230 } | 230 } |
| 231 | 231 |
| 232 void ThumbnailDatabase::CommitTransaction() { | 232 void ThumbnailDatabase::CommitTransaction() { |
| 233 db_.CommitTransaction(); | 233 db_.CommitTransaction(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void ThumbnailDatabase::Vacuum() { | 236 void ThumbnailDatabase::Vacuum() { |
| 237 DCHECK(db_.transaction_nesting() == 0) << | 237 DCHECK(db_.transaction_nesting() == 0) << |
| 238 "Can not have a transaction when vacuuming."; | 238 "Can not have a transaction when vacuuming."; |
| 239 db_.Execute("VACUUM"); | 239 ignore_result(db_.Execute("VACUUM")); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void ThumbnailDatabase::SetPageThumbnail( | 242 void ThumbnailDatabase::SetPageThumbnail( |
| 243 const GURL& url, | 243 const GURL& url, |
| 244 URLID id, | 244 URLID id, |
| 245 const gfx::Image* thumbnail, | 245 const gfx::Image* thumbnail, |
| 246 const ThumbnailScore& score, | 246 const ThumbnailScore& score, |
| 247 base::Time time) { | 247 base::Time time) { |
| 248 if (use_top_sites_) { | 248 if (use_top_sites_) { |
| 249 LOG(WARNING) << "Use TopSites instead."; | 249 LOG(WARNING) << "Use TopSites instead."; |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 bool ThumbnailDatabase::CommitTemporaryIconMappingTable() { | 598 bool ThumbnailDatabase::CommitTemporaryIconMappingTable() { |
| 599 // Delete the old icon_mapping table. | 599 // Delete the old icon_mapping table. |
| 600 if (!db_.Execute("DROP TABLE icon_mapping")) | 600 if (!db_.Execute("DROP TABLE icon_mapping")) |
| 601 return false; | 601 return false; |
| 602 | 602 |
| 603 // Rename the temporary one. | 603 // Rename the temporary one. |
| 604 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping")) | 604 if (!db_.Execute("ALTER TABLE temp_icon_mapping RENAME TO icon_mapping")) |
| 605 return false; | 605 return false; |
| 606 | 606 |
| 607 // The renamed table needs the index (the temporary table doesn't have one). | 607 // The renamed table needs the index (the temporary table doesn't have one). |
| 608 InitIconMappingIndex(); | 608 return InitIconMappingIndex(); |
| 609 | |
| 610 return true; | |
| 611 } | 609 } |
| 612 | 610 |
| 613 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) { | 611 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) { |
| 614 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 612 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 615 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" | 613 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" |
| 616 "SELECT url, last_updated, image_data, icon_type " | 614 "SELECT url, last_updated, image_data, icon_type " |
| 617 "FROM favicons WHERE id = ?")); | 615 "FROM favicons WHERE id = ?")); |
| 618 if (!statement) | 616 if (!statement) |
| 619 return 0; | 617 return 0; |
| 620 statement.BindInt64(0, source); | 618 statement.BindInt64(0, source); |
| 621 if (!statement.Run()) | 619 if (!statement.Run()) |
| 622 return 0; | 620 return 0; |
| 623 | 621 |
| 624 // We return the ID of the newly inserted favicon. | 622 // We return the ID of the newly inserted favicon. |
| 625 return db_.GetLastInsertRowId(); | 623 return db_.GetLastInsertRowId(); |
| 626 } | 624 } |
| 627 | 625 |
| 628 bool ThumbnailDatabase::CommitTemporaryFaviconTable() { | 626 bool ThumbnailDatabase::CommitTemporaryFaviconTable() { |
| 629 // Delete the old favicons table. | 627 // Delete the old favicons table. |
| 630 if (!db_.Execute("DROP TABLE favicons")) | 628 if (!db_.Execute("DROP TABLE favicons")) |
| 631 return false; | 629 return false; |
| 632 | 630 |
| 633 // Rename the temporary one. | 631 // Rename the temporary one. |
| 634 if (!db_.Execute("ALTER TABLE temp_favicons RENAME TO favicons")) | 632 if (!db_.Execute("ALTER TABLE temp_favicons RENAME TO favicons")) |
| 635 return false; | 633 return false; |
| 636 | 634 |
| 637 // The renamed table needs the index (the temporary table doesn't have one). | 635 // The renamed table needs the index (the temporary table doesn't have one). |
| 638 InitFaviconsIndex(); | 636 return InitFaviconsIndex(); |
| 639 return true; | |
| 640 } | 637 } |
| 641 | 638 |
| 642 bool ThumbnailDatabase::NeedsMigrationToTopSites() { | 639 bool ThumbnailDatabase::NeedsMigrationToTopSites() { |
| 643 return !use_top_sites_; | 640 return !use_top_sites_; |
| 644 } | 641 } |
| 645 | 642 |
| 646 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file, | 643 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file, |
| 647 const FilePath& new_db_file) { | 644 const FilePath& new_db_file) { |
| 648 // Init favicons table - same schema as the thumbnails. | 645 // Init favicons table - same schema as the thumbnails. |
| 649 sql::Connection favicons; | 646 sql::Connection favicons; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 706 // Reset the DB to point to new file. | 703 // Reset the DB to point to new file. |
| 707 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK) | 704 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK) |
| 708 return false; | 705 return false; |
| 709 | 706 |
| 710 file_util::Delete(old_db_file, false); | 707 file_util::Delete(old_db_file, false); |
| 711 | 708 |
| 712 meta_table_.Reset(); | 709 meta_table_.Reset(); |
| 713 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) | 710 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) |
| 714 return false; | 711 return false; |
| 715 | 712 |
| 716 InitFaviconsIndex(); | 713 if (!InitFaviconsIndex()) |
| 714 return false; |
| 717 | 715 |
| 718 // Reopen the transaction. | 716 // Reopen the transaction. |
| 719 BeginTransaction(); | 717 BeginTransaction(); |
| 720 use_top_sites_ = true; | 718 use_top_sites_ = true; |
| 721 return true; | 719 return true; |
| 722 } | 720 } |
| 723 | 721 |
| 724 bool ThumbnailDatabase::InitIconMappingTable(sql::Connection* db, | 722 bool ThumbnailDatabase::InitIconMappingTable(sql::Connection* db, |
| 725 bool is_temporary) { | 723 bool is_temporary) { |
| 726 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; | 724 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; |
| 727 if (!db->DoesTableExist(name)) { | 725 if (!db->DoesTableExist(name)) { |
| 728 std::string sql; | 726 std::string sql; |
| 729 sql.append("CREATE TABLE "); | 727 sql.append("CREATE TABLE "); |
| 730 sql.append(name); | 728 sql.append(name); |
| 731 sql.append("(" | 729 sql.append("(" |
| 732 "id INTEGER PRIMARY KEY," | 730 "id INTEGER PRIMARY KEY," |
| 733 "page_url LONGVARCHAR NOT NULL," | 731 "page_url LONGVARCHAR NOT NULL," |
| 734 "icon_id INTEGER)"); | 732 "icon_id INTEGER)"); |
| 735 if (!db->Execute(sql.c_str())) | 733 if (!db->Execute(sql.c_str())) |
| 736 return false; | 734 return false; |
| 737 } | 735 } |
| 738 return true; | 736 return true; |
| 739 } | 737 } |
| 740 | 738 |
| 741 void ThumbnailDatabase::InitIconMappingIndex() { | 739 bool ThumbnailDatabase::InitIconMappingIndex() { |
| 742 // Add an index on the url column. We ignore errors. Since this is always | 740 // Add an index on the url column. |
| 743 // called during startup, the index will normally already exist. | 741 return |
| 744 db_.Execute("CREATE INDEX icon_mapping_page_url_idx" | 742 db_.Execute("CREATE INDEX IF NOT EXISTS icon_mapping_page_url_idx" |
| 745 " ON icon_mapping(page_url)"); | 743 " ON icon_mapping(page_url)") && |
| 746 db_.Execute("CREATE INDEX icon_mapping_icon_id_idx ON icon_mapping(icon_id)"); | 744 db_.Execute("CREATE INDEX IF NOT EXISTS icon_mapping_icon_id_idx" |
| 745 " ON icon_mapping(icon_id)"); |
| 747 } | 746 } |
| 748 | 747 |
| 749 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, | 748 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, |
| 750 FaviconID icon_id, | 749 FaviconID icon_id, |
| 751 bool is_temporary) { | 750 bool is_temporary) { |
| 752 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; | 751 const char* name = is_temporary ? "temp_icon_mapping" : "icon_mapping"; |
| 753 const char* statement_name = | 752 const char* statement_name = |
| 754 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; | 753 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; |
| 755 | 754 |
| 756 std::string sql; | 755 std::string sql; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 792 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) { | 791 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) { |
| 793 NOTREACHED(); | 792 NOTREACHED(); |
| 794 return false; | 793 return false; |
| 795 } | 794 } |
| 796 meta_table_.SetVersionNumber(5); | 795 meta_table_.SetVersionNumber(5); |
| 797 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber)); | 796 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber)); |
| 798 return true; | 797 return true; |
| 799 } | 798 } |
| 800 | 799 |
| 801 } // namespace history | 800 } // namespace history |
| OLD | NEW |