Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: components/history/core/browser/thumbnail_database.cc

Issue 1004373002: Add last_requested field to the favicon_bitmaps table of the favicons database. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/history/core/browser/thumbnail_database.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/thumbnail_database.h" 5 #include "components/history/core/browser/thumbnail_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 // table. 59 // table.
60 // 60 //
61 // id Unique ID. 61 // id Unique ID.
62 // icon_id The ID of the favicon that the bitmap is associated to. 62 // icon_id The ID of the favicon that the bitmap is associated to.
63 // last_updated The time at which this favicon was inserted into the 63 // last_updated The time at which this favicon was inserted into the
64 // table. This is used to determine if it needs to be 64 // table. This is used to determine if it needs to be
65 // redownloaded from the web. 65 // redownloaded from the web.
66 // image_data PNG encoded data of the favicon. 66 // image_data PNG encoded data of the favicon.
67 // width Pixel width of |image_data|. 67 // width Pixel width of |image_data|.
68 // height Pixel height of |image_data|. 68 // height Pixel height of |image_data|.
69 //
70 // favicon_bitmap_usage
huangs 2015/03/14 02:15:26 I think |bitmap_usage| would be cleaner. Although
Roger McFarlane (Chromium) 2015/03/17 20:55:53 Done.
71 // This table contains a record of the last time a given
72 // favicon bitmap was requested via the various icon
huangs 2015/03/14 02:15:26 s/favicon/icon/ (|favicon| is overloaded, but it's
Roger McFarlane (Chromium) 2015/03/17 20:55:53 Done.
73 // services.
74 //
75 // bitmap_id The ID of the favicon_bitmaps entry for this usage record.
76 // last_requested The time at which this bitmap was last requested. This is
77 // used to determine the priority with which the bitmap
78 // should be retained on cleanup.
69 79
70 namespace { 80 namespace {
71 81
72 // For this database, schema migrations are deprecated after two 82 // For this database, schema migrations are deprecated after two
73 // years. This means that the oldest non-deprecated version should be 83 // years. This means that the oldest non-deprecated version should be
74 // two years old or greater (thus the migrations to get there are 84 // two years old or greater (thus the migrations to get there are
75 // older). Databases containing deprecated versions will be cleared 85 // older). Databases containing deprecated versions will be cleared
76 // at startup. Since this database is a cache, losing old data is not 86 // at startup. Since this database is a cache, losing old data is not
77 // fatal (in fact, very old data may be expired immediately at startup 87 // fatal (in fact, very old data may be expired immediately at startup
78 // anyhow). 88 // anyhow).
79 89
90 // Version 8: ????????/r?????? by rogerm@chromium on 2015-??-??
80 // Version 7: 911a634d/r209424 by qsr@chromium.org on 2013-07-01 91 // Version 7: 911a634d/r209424 by qsr@chromium.org on 2013-07-01
81 // Version 6: 610f923b/r152367 by pkotwicz@chromium.org on 2012-08-20 92 // Version 6: 610f923b/r152367 by pkotwicz@chromium.org on 2012-08-20
82 // Version 5: e2ee8ae9/r105004 by groby@chromium.org on 2011-10-12 93 // Version 5: e2ee8ae9/r105004 by groby@chromium.org on 2011-10-12
83 // Version 4: 5f104d76/r77288 by sky@chromium.org on 2011-03-08 (deprecated) 94 // Version 4: 5f104d76/r77288 by sky@chromium.org on 2011-03-08 (deprecated)
84 // Version 3: 09911bf3/r15 by initial.commit on 2008-07-26 (deprecated) 95 // Version 3: 09911bf3/r15 by initial.commit on 2008-07-26 (deprecated)
85 96
86 // Version number of the database. 97 // Version number of the database.
87 // NOTE(shess): When changing the version, add a new golden file for 98 // NOTE(shess): When changing the version, add a new golden file for
88 // the new version and a test to verify that Init() works with it. 99 // the new version and a test to verify that Init() works with it.
89 const int kCurrentVersionNumber = 7; 100 const int kCurrentVersionNumber = 8;
90 const int kCompatibleVersionNumber = 7; 101 const int kCompatibleVersionNumber = 7;
91 const int kDeprecatedVersionNumber = 4; // and earlier. 102 const int kDeprecatedVersionNumber = 4; // and earlier.
92 103
93 void FillIconMapping(const sql::Statement& statement, 104 void FillIconMapping(const sql::Statement& statement,
94 const GURL& page_url, 105 const GURL& page_url,
95 history::IconMapping* icon_mapping) { 106 history::IconMapping* icon_mapping) {
96 icon_mapping->mapping_id = statement.ColumnInt64(0); 107 icon_mapping->mapping_id = statement.ColumnInt64(0);
97 icon_mapping->icon_id = statement.ColumnInt64(1); 108 icon_mapping->icon_id = statement.ColumnInt64(1);
98 icon_mapping->icon_type = 109 icon_mapping->icon_type =
99 static_cast<favicon_base::IconType>(statement.ColumnInt(2)); 110 static_cast<favicon_base::IconType>(statement.ColumnInt(2));
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 "id INTEGER PRIMARY KEY," 319 "id INTEGER PRIMARY KEY,"
309 "icon_id INTEGER NOT NULL," 320 "icon_id INTEGER NOT NULL,"
310 "last_updated INTEGER DEFAULT 0," 321 "last_updated INTEGER DEFAULT 0,"
311 "image_data BLOB," 322 "image_data BLOB,"
312 "width INTEGER DEFAULT 0," 323 "width INTEGER DEFAULT 0,"
313 "height INTEGER DEFAULT 0" 324 "height INTEGER DEFAULT 0"
314 ")"; 325 ")";
315 if (!db->Execute(kFaviconBitmapsSql)) 326 if (!db->Execute(kFaviconBitmapsSql))
316 return false; 327 return false;
317 328
329 const char kFaviconBitmapUsageSql[] =
330 "CREATE TABLE IF NOT EXISTS favicon_bitmap_usage"
331 "("
332 "bitmap_id INTEGER PRIMARY KEY,"
333 "last_requested INTEGER)";
334 if (!db->Execute(kFaviconBitmapUsageSql))
335 return false;
336
318 return true; 337 return true;
319 } 338 }
320 339
321 // NOTE(shess): Schema modifications must consider initial creation in 340 // NOTE(shess): Schema modifications must consider initial creation in
322 // |InitImpl()|, recovery in |RecoverDatabaseOrRaze()|, and history pruning in 341 // |InitImpl()|, recovery in |RecoverDatabaseOrRaze()|, and history pruning in
323 // |RetainDataForPageUrls()|. 342 // |RetainDataForPageUrls()|.
324 bool InitIndices(sql::Connection* db) { 343 bool InitIndices(sql::Connection* db) {
325 const char kIconMappingUrlIndexSql[] = 344 const char kIconMappingUrlIndexSql[] =
326 "CREATE INDEX IF NOT EXISTS icon_mapping_page_url_idx" 345 "CREATE INDEX IF NOT EXISTS icon_mapping_page_url_idx"
327 " ON icon_mapping(page_url)"; 346 " ON icon_mapping(page_url)";
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 // Recover the database to the extent possible, razing it if recovery 408 // Recover the database to the extent possible, razing it if recovery
390 // is not possible. 409 // is not possible.
391 // TODO(shess): This is mostly just a safe proof of concept. In the 410 // TODO(shess): This is mostly just a safe proof of concept. In the
392 // real world, this database is probably not worthwhile recovering, as 411 // real world, this database is probably not worthwhile recovering, as
393 // opposed to just razing it and starting over whenever corruption is 412 // opposed to just razing it and starting over whenever corruption is
394 // detected. So this database is a good test subject. 413 // detected. So this database is a good test subject.
395 void RecoverDatabaseOrRaze(sql::Connection* db, const base::FilePath& db_path) { 414 void RecoverDatabaseOrRaze(sql::Connection* db, const base::FilePath& db_path) {
396 // NOTE(shess): This code is currently specific to the version 415 // NOTE(shess): This code is currently specific to the version
397 // number. I am working on simplifying things to loosen the 416 // number. I am working on simplifying things to loosen the
398 // dependency, meanwhile contact me if you need to bump the version. 417 // dependency, meanwhile contact me if you need to bump the version.
399 DCHECK_EQ(7, kCurrentVersionNumber); 418 DCHECK_EQ(8, kCurrentVersionNumber);
400 419
401 // TODO(shess): Reset back after? 420 // TODO(shess): Reset back after?
402 db->reset_error_callback(); 421 db->reset_error_callback();
403 422
404 // For histogram purposes. 423 // For histogram purposes.
405 size_t favicons_rows_recovered = 0; 424 size_t favicons_rows_recovered = 0;
406 size_t favicon_bitmaps_rows_recovered = 0; 425 size_t favicon_bitmaps_rows_recovered = 0;
407 size_t icon_mapping_rows_recovered = 0; 426 size_t icon_mapping_rows_recovered = 0;
408 int64 original_size = 0; 427 int64 original_size = 0;
409 base::GetFileSize(db_path, &original_size); 428 base::GetFileSize(db_path, &original_size);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 // the code simple. http://crbug.com/327485 for numbers. 462 // the code simple. http://crbug.com/327485 for numbers.
444 DCHECK_LE(kDeprecatedVersionNumber, 6); 463 DCHECK_LE(kDeprecatedVersionNumber, 6);
445 if (version <= 6) { 464 if (version <= 6) {
446 sql::Recovery::Unrecoverable(recovery.Pass()); 465 sql::Recovery::Unrecoverable(recovery.Pass());
447 RecordRecoveryEvent(RECOVERY_EVENT_DEPRECATED); 466 RecordRecoveryEvent(RECOVERY_EVENT_DEPRECATED);
448 return; 467 return;
449 } 468 }
450 469
451 // Earlier versions have been handled or deprecated, later versions should be 470 // Earlier versions have been handled or deprecated, later versions should be
452 // impossible. 471 // impossible.
453 if (version != 7) { 472 if (version != 8) {
454 sql::Recovery::Unrecoverable(recovery.Pass()); 473 sql::Recovery::Unrecoverable(recovery.Pass());
455 RecordRecoveryEvent(RECOVERY_EVENT_FAILED_META_WRONG_VERSION); 474 RecordRecoveryEvent(RECOVERY_EVENT_FAILED_META_WRONG_VERSION);
456 return; 475 return;
457 } 476 }
458 477
459 // Recover to current schema version. 478 // Recover to current schema version.
460 sql::MetaTable recover_meta_table; 479 sql::MetaTable recover_meta_table;
461 if (!recover_meta_table.Init(recovery->db(), kCurrentVersionNumber, 480 if (!recover_meta_table.Init(recovery->db(), kCurrentVersionNumber,
462 kCompatibleVersionNumber)) { 481 kCompatibleVersionNumber)) {
463 sql::Recovery::Rollback(recovery.Pass()); 482 sql::Recovery::Rollback(recovery.Pass());
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 *png_icon_data = data; 739 *png_icon_data = data;
721 } 740 }
722 741
723 if (pixel_size) { 742 if (pixel_size) {
724 *pixel_size = gfx::Size(statement.ColumnInt(2), 743 *pixel_size = gfx::Size(statement.ColumnInt(2),
725 statement.ColumnInt(3)); 744 statement.ColumnInt(3));
726 } 745 }
727 return true; 746 return true;
728 } 747 }
729 748
749 bool ThumbnailDatabase::GetFaviconBitmapLastRequestedTime(
750 FaviconBitmapID bitmap_id,
751 base::Time* last_requested) {
752 DCHECK(bitmap_id);
753 DCHECK(last_requested);
754 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
755 "SELECT last_requested FROM favicon_bitmap_usage WHERE bitmap_id=?"));
756 statement.BindInt64(0, bitmap_id);
757
758 if (!statement.Step())
759 return false;
760
761 *last_requested = base::Time::FromInternalValue(statement.ColumnInt64(0));
762 return true;
763 }
764
730 FaviconBitmapID ThumbnailDatabase::AddFaviconBitmap( 765 FaviconBitmapID ThumbnailDatabase::AddFaviconBitmap(
731 favicon_base::FaviconID icon_id, 766 favicon_base::FaviconID icon_id,
732 const scoped_refptr<base::RefCountedMemory>& icon_data, 767 const scoped_refptr<base::RefCountedMemory>& icon_data,
733 base::Time time, 768 base::Time time,
734 const gfx::Size& pixel_size) { 769 const gfx::Size& pixel_size) {
735 DCHECK(icon_id); 770 DCHECK(icon_id);
736 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 771 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
737 "INSERT INTO favicon_bitmaps (icon_id, image_data, last_updated, width, " 772 "INSERT INTO favicon_bitmaps (icon_id, image_data, last_updated, width, "
738 "height) VALUES (?, ?, ?, ?, ?)")); 773 "height) VALUES (?, ?, ?, ?, ?)"));
739 statement.BindInt64(0, icon_id); 774 statement.BindInt64(0, icon_id);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 FaviconBitmapID bitmap_id, 810 FaviconBitmapID bitmap_id,
776 base::Time time) { 811 base::Time time) {
777 DCHECK(bitmap_id); 812 DCHECK(bitmap_id);
778 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 813 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
779 "UPDATE favicon_bitmaps SET last_updated=? WHERE id=?")); 814 "UPDATE favicon_bitmaps SET last_updated=? WHERE id=?"));
780 statement.BindInt64(0, time.ToInternalValue()); 815 statement.BindInt64(0, time.ToInternalValue());
781 statement.BindInt64(1, bitmap_id); 816 statement.BindInt64(1, bitmap_id);
782 return statement.Run(); 817 return statement.Run();
783 } 818 }
784 819
820 bool ThumbnailDatabase::SetFaviconBitmapLastRequestedTime(
821 FaviconBitmapID bitmap_id,
822 base::Time time) {
823 DCHECK(bitmap_id);
824 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
825 "INSERT OR REPLACE INTO favicon_bitmap_usage (bitmap_id, last_requested)"
826 " VALUES (?, ?)"));
827 statement.BindInt64(0, bitmap_id);
828 statement.BindInt64(1, time.ToInternalValue());
829 return statement.Run();
830 }
831
785 bool ThumbnailDatabase::DeleteFaviconBitmap(FaviconBitmapID bitmap_id) { 832 bool ThumbnailDatabase::DeleteFaviconBitmap(FaviconBitmapID bitmap_id) {
786 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 833 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
787 "DELETE FROM favicon_bitmaps WHERE id=?")); 834 "DELETE FROM favicon_bitmaps WHERE id=?"));
788 statement.BindInt64(0, bitmap_id); 835 statement.BindInt64(0, bitmap_id);
836 if (!statement.Run())
837 return false;
838
839 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
840 "DELETE FROM favicon_bitmap_usage WHERE bitmap_id=?"));
841 statement.BindInt64(0, bitmap_id);
789 return statement.Run(); 842 return statement.Run();
790 } 843 }
791 844
792 bool ThumbnailDatabase::SetFaviconOutOfDate(favicon_base::FaviconID icon_id) { 845 bool ThumbnailDatabase::SetFaviconOutOfDate(favicon_base::FaviconID icon_id) {
793 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 846 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
794 "UPDATE favicon_bitmaps SET last_updated=? WHERE icon_id=?")); 847 "UPDATE favicon_bitmaps SET last_updated=? WHERE icon_id=?"));
795 statement.BindInt64(0, 0); 848 statement.BindInt64(0, 0);
796 statement.BindInt64(1, icon_id); 849 statement.BindInt64(1, icon_id);
797 850
798 return statement.Run(); 851 return statement.Run();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 favicon_base::FaviconID icon_id = AddFavicon(icon_url, icon_type); 912 favicon_base::FaviconID icon_id = AddFavicon(icon_url, icon_type);
860 if (!icon_id || !AddFaviconBitmap(icon_id, icon_data, time, pixel_size)) 913 if (!icon_id || !AddFaviconBitmap(icon_id, icon_data, time, pixel_size))
861 return 0; 914 return 0;
862 915
863 return icon_id; 916 return icon_id;
864 } 917 }
865 918
866 bool ThumbnailDatabase::DeleteFavicon(favicon_base::FaviconID id) { 919 bool ThumbnailDatabase::DeleteFavicon(favicon_base::FaviconID id) {
867 sql::Statement statement; 920 sql::Statement statement;
868 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, 921 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
869 "DELETE FROM favicons WHERE id = ?")); 922 "DELETE FROM favicons WHERE id=?"));
870 statement.BindInt64(0, id); 923 statement.BindInt64(0, id);
871 if (!statement.Run()) 924 if (!statement.Run())
872 return false; 925 return false;
873 926
874 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, 927 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
875 "DELETE FROM favicon_bitmaps WHERE icon_id = ?")); 928 "DELETE FROM favicon_bitmap_usage WHERE bitmap_id IN"
929 " (SELECT id from favicon_bitmaps where icon_id=?)"));
930 statement.BindInt64(0, id);
931 if (!statement.Run())
932 return false;
933
934 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
935 "DELETE FROM favicon_bitmaps WHERE icon_id=?"));
876 statement.BindInt64(0, id); 936 statement.BindInt64(0, id);
877 return statement.Run(); 937 return statement.Run();
878 } 938 }
879 939
880 bool ThumbnailDatabase::GetIconMappingsForPageURL( 940 bool ThumbnailDatabase::GetIconMappingsForPageURL(
881 const GURL& page_url, 941 const GURL& page_url,
882 int required_icon_types, 942 int required_icon_types,
883 std::vector<IconMapping>* filtered_mapping_data) { 943 std::vector<IconMapping>* filtered_mapping_data) {
884 std::vector<IconMapping> mapping_data; 944 std::vector<IconMapping> mapping_data;
885 if (!GetIconMappingsForPageURL(page_url, &mapping_data)) 945 if (!GetIconMappingsForPageURL(page_url, &mapping_data))
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
1228 if (!UpgradeToVersion6()) 1288 if (!UpgradeToVersion6())
1229 return CantUpgradeToVersion(cur_version); 1289 return CantUpgradeToVersion(cur_version);
1230 } 1290 }
1231 1291
1232 if (cur_version == 6) { 1292 if (cur_version == 6) {
1233 ++cur_version; 1293 ++cur_version;
1234 if (!UpgradeToVersion7()) 1294 if (!UpgradeToVersion7())
1235 return CantUpgradeToVersion(cur_version); 1295 return CantUpgradeToVersion(cur_version);
1236 } 1296 }
1237 1297
1298 if (cur_version == 7) {
1299 ++cur_version;
1300 if (!UpgradeToVersion8())
1301 return CantUpgradeToVersion(cur_version);
1302 }
1303
1238 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) << 1304 LOG_IF(WARNING, cur_version < kCurrentVersionNumber) <<
1239 "Thumbnail database version " << cur_version << " is too old to handle."; 1305 "Thumbnail database version " << cur_version << " is too old to handle.";
1240 1306
1241 // Initialization is complete. 1307 // Initialization is complete.
1242 if (!transaction.Commit()) 1308 if (!transaction.Commit())
1243 return sql::INIT_FAILURE; 1309 return sql::INIT_FAILURE;
1244 1310
1245 // Raze the database if the structure of the favicons database is not what 1311 // Raze the database if the structure of the favicons database is not what
1246 // it should be. This error cannot be detected via the SQL error code because 1312 // it should be. This error cannot be detected via the SQL error code because
1247 // the error code for running SQL statements against a database with missing 1313 // the error code for running SQL statements against a database with missing
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1308 db_.Execute("CREATE INDEX IF NOT EXISTS favicons_url ON favicons(url)"); 1374 db_.Execute("CREATE INDEX IF NOT EXISTS favicons_url ON favicons(url)");
1309 1375
1310 if (!success) 1376 if (!success)
1311 return false; 1377 return false;
1312 1378
1313 meta_table_.SetVersionNumber(7); 1379 meta_table_.SetVersionNumber(7);
1314 meta_table_.SetCompatibleVersionNumber(std::min(7, kCompatibleVersionNumber)); 1380 meta_table_.SetCompatibleVersionNumber(std::min(7, kCompatibleVersionNumber));
1315 return true; 1381 return true;
1316 } 1382 }
1317 1383
1384 bool ThumbnailDatabase::UpgradeToVersion8() {
1385 meta_table_.SetVersionNumber(8);
1386 meta_table_.SetCompatibleVersionNumber(std::min(8, kCompatibleVersionNumber));
1387 return true;
huangs 2015/03/14 02:15:26 Would you need to add the table by executing |kFav
Roger McFarlane (Chromium) 2015/03/17 20:55:53 No, that's already done by this point. The tables
1388 }
1389
1318 bool ThumbnailDatabase::IsFaviconDBStructureIncorrect() { 1390 bool ThumbnailDatabase::IsFaviconDBStructureIncorrect() {
1319 return !db_.IsSQLValid("SELECT id, url, icon_type FROM favicons"); 1391 return !db_.IsSQLValid("SELECT id, url, icon_type FROM favicons");
1320 } 1392 }
1321 1393
1322 } // namespace history 1394 } // namespace history
OLDNEW
« no previous file with comments | « components/history/core/browser/thumbnail_database.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698