| 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/thumbnail_database.h" | 5 #include "components/history/core/browser/thumbnail_database.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // favicons. There is a separate row for every size in a | 57 // favicons. There is a separate row for every size in a |
| 58 // multi resolution bitmap. The bitmap data is associated | 58 // multi resolution bitmap. The bitmap data is associated |
| 59 // to the favicon via the |icon_id| field which matches | 59 // to the favicon via the |icon_id| field which matches |
| 60 // the |id| field in the appropriate row in the |favicons| | 60 // the |id| field in the appropriate row in the |favicons| |
| 61 // table. | 61 // table. |
| 62 // | 62 // |
| 63 // id Unique ID. | 63 // id Unique ID. |
| 64 // icon_id The ID of the favicon that the bitmap is associated to. | 64 // icon_id The ID of the favicon that the bitmap is associated to. |
| 65 // last_updated The time at which this favicon was inserted into the | 65 // last_updated The time at which this favicon was inserted into the |
| 66 // table. This is used to determine if it needs to be | 66 // table. This is used to determine if it needs to be |
| 67 // redownloaded from the web. | 67 // redownloaded from the web. Value 0 denotes that the bitmap |
| 68 // has been explicitly expired. |
| 68 // image_data PNG encoded data of the favicon. | 69 // image_data PNG encoded data of the favicon. |
| 69 // width Pixel width of |image_data|. | 70 // width Pixel width of |image_data|. |
| 70 // height Pixel height of |image_data|. | 71 // height Pixel height of |image_data|. |
| 71 // last_requested The time at which this bitmap was last requested. This is | 72 // last_requested The time at which this bitmap was last requested. This |
| 72 // used to determine the priority with which the bitmap | 73 // entry is non-zero iff the bitmap is of type ON_DEMAND. |
| 73 // should be retained on cleanup. | 74 // This info is used for clearing old ON_DEMAND bitmaps. |
| 75 // (On-demand bitmaps cannot get cleared along with expired |
| 76 // visits in history DB because there is no corresponding |
| 77 // visit.) |
| 74 | 78 |
| 75 namespace { | 79 namespace { |
| 76 | 80 |
| 77 // For this database, schema migrations are deprecated after two | 81 // For this database, schema migrations are deprecated after two |
| 78 // years. This means that the oldest non-deprecated version should be | 82 // years. This means that the oldest non-deprecated version should be |
| 79 // two years old or greater (thus the migrations to get there are | 83 // two years old or greater (thus the migrations to get there are |
| 80 // older). Databases containing deprecated versions will be cleared | 84 // older). Databases containing deprecated versions will be cleared |
| 81 // at startup. Since this database is a cache, losing old data is not | 85 // at startup. Since this database is a cache, losing old data is not |
| 82 // fatal (in fact, very old data may be expired immediately at startup | 86 // fatal (in fact, very old data may be expired immediately at startup |
| 83 // anyhow). | 87 // anyhow). |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 | 500 |
| 497 if (last_requested) | 501 if (last_requested) |
| 498 *last_requested = base::Time::FromInternalValue(statement.ColumnInt64(4)); | 502 *last_requested = base::Time::FromInternalValue(statement.ColumnInt64(4)); |
| 499 | 503 |
| 500 return true; | 504 return true; |
| 501 } | 505 } |
| 502 | 506 |
| 503 FaviconBitmapID ThumbnailDatabase::AddFaviconBitmap( | 507 FaviconBitmapID ThumbnailDatabase::AddFaviconBitmap( |
| 504 favicon_base::FaviconID icon_id, | 508 favicon_base::FaviconID icon_id, |
| 505 const scoped_refptr<base::RefCountedMemory>& icon_data, | 509 const scoped_refptr<base::RefCountedMemory>& icon_data, |
| 510 FaviconBitmapType type, |
| 506 base::Time time, | 511 base::Time time, |
| 507 const gfx::Size& pixel_size) { | 512 const gfx::Size& pixel_size) { |
| 508 DCHECK(icon_id); | 513 DCHECK(icon_id); |
| 509 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 514 |
| 510 "INSERT INTO favicon_bitmaps (icon_id, image_data, last_updated, width, " | 515 sql::Statement statement(db_.GetCachedStatement( |
| 511 "height) VALUES (?, ?, ?, ?, ?)")); | 516 SQL_FROM_HERE, |
| 517 "INSERT INTO favicon_bitmaps (icon_id, image_data, last_updated, " |
| 518 "last_requested, width, height) VALUES (?, ?, ?, ?, ?, ?)")); |
| 519 |
| 512 statement.BindInt64(0, icon_id); | 520 statement.BindInt64(0, icon_id); |
| 513 if (icon_data.get() && icon_data->size()) { | 521 if (icon_data.get() && icon_data->size()) { |
| 514 statement.BindBlob(1, icon_data->front(), | 522 statement.BindBlob(1, icon_data->front(), |
| 515 static_cast<int>(icon_data->size())); | 523 static_cast<int>(icon_data->size())); |
| 516 } else { | 524 } else { |
| 517 statement.BindNull(1); | 525 statement.BindNull(1); |
| 518 } | 526 } |
| 519 statement.BindInt64(2, time.ToInternalValue()); | 527 |
| 520 statement.BindInt(3, pixel_size.width()); | 528 // On-visit bitmaps: |
| 521 statement.BindInt(4, pixel_size.height()); | 529 // - keep track of last_updated: last write time is used for expiration; |
| 530 // - always have last_requested==0: no need to keep track of last read time. |
| 531 statement.BindInt64(2, type == ON_VISIT ? time.ToInternalValue() : 0); |
| 532 // On-demand bitmaps: |
| 533 // - always have last_updated==0: last write time is not stored as they are |
| 534 // always expired and thus ready to be replaced by ON_VISIT icons; |
| 535 // - keep track of last_requested: last read time is used for cache eviction. |
| 536 statement.BindInt64(3, type == ON_DEMAND ? time.ToInternalValue() : 0); |
| 537 |
| 538 statement.BindInt(4, pixel_size.width()); |
| 539 statement.BindInt(5, pixel_size.height()); |
| 522 | 540 |
| 523 if (!statement.Run()) | 541 if (!statement.Run()) |
| 524 return 0; | 542 return 0; |
| 525 return db_.GetLastInsertRowId(); | 543 return db_.GetLastInsertRowId(); |
| 526 } | 544 } |
| 527 | 545 |
| 528 bool ThumbnailDatabase::SetFaviconBitmap( | 546 bool ThumbnailDatabase::SetFaviconBitmap( |
| 529 FaviconBitmapID bitmap_id, | 547 FaviconBitmapID bitmap_id, |
| 530 scoped_refptr<base::RefCountedMemory> bitmap_data, | 548 scoped_refptr<base::RefCountedMemory> bitmap_data, |
| 531 base::Time time) { | 549 base::Time time) { |
| 532 DCHECK(bitmap_id); | 550 DCHECK(bitmap_id); |
| 533 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 551 // By updating last_updated timestamp, we assume the icon is of type ON_VISIT. |
| 534 "UPDATE favicon_bitmaps SET image_data=?, last_updated=? WHERE id=?")); | 552 // If it is ON_DEMAND, reset last_requested to 0 and thus silently change the |
| 553 // type to ON_VISIT. |
| 554 sql::Statement statement( |
| 555 db_.GetCachedStatement(SQL_FROM_HERE, |
| 556 "UPDATE favicon_bitmaps SET image_data=?, " |
| 557 "last_updated=?, last_requested=? WHERE id=?")); |
| 535 if (bitmap_data.get() && bitmap_data->size()) { | 558 if (bitmap_data.get() && bitmap_data->size()) { |
| 536 statement.BindBlob(0, bitmap_data->front(), | 559 statement.BindBlob(0, bitmap_data->front(), |
| 537 static_cast<int>(bitmap_data->size())); | 560 static_cast<int>(bitmap_data->size())); |
| 538 } else { | 561 } else { |
| 539 statement.BindNull(0); | 562 statement.BindNull(0); |
| 540 } | 563 } |
| 541 statement.BindInt64(1, time.ToInternalValue()); | 564 statement.BindInt64(1, time.ToInternalValue()); |
| 542 statement.BindInt64(2, bitmap_id); | 565 statement.BindInt64(2, 0); |
| 566 statement.BindInt64(3, bitmap_id); |
| 543 | 567 |
| 544 return statement.Run(); | 568 return statement.Run(); |
| 545 } | 569 } |
| 546 | 570 |
| 547 bool ThumbnailDatabase::SetFaviconBitmapLastUpdateTime( | 571 bool ThumbnailDatabase::SetFaviconBitmapLastUpdateTime( |
| 548 FaviconBitmapID bitmap_id, | 572 FaviconBitmapID bitmap_id, |
| 549 base::Time time) { | 573 base::Time time) { |
| 550 DCHECK(bitmap_id); | 574 DCHECK(bitmap_id); |
| 551 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 575 // By updating last_updated timestamp, we assume the icon is of type ON_VISIT. |
| 552 "UPDATE favicon_bitmaps SET last_updated=? WHERE id=?")); | 576 // If it is ON_DEMAND, reset last_requested to 0 and thus silently change the |
| 577 // type to ON_VISIT. |
| 578 sql::Statement statement( |
| 579 db_.GetCachedStatement(SQL_FROM_HERE, |
| 580 "UPDATE favicon_bitmaps SET last_updated=?, " |
| 581 "last_requested=? WHERE id=?")); |
| 553 statement.BindInt64(0, time.ToInternalValue()); | 582 statement.BindInt64(0, time.ToInternalValue()); |
| 554 statement.BindInt64(1, bitmap_id); | 583 statement.BindInt64(1, 0); |
| 584 statement.BindInt64(2, bitmap_id); |
| 555 return statement.Run(); | 585 return statement.Run(); |
| 556 } | 586 } |
| 557 | 587 |
| 558 bool ThumbnailDatabase::SetFaviconBitmapLastRequestedTime( | 588 bool ThumbnailDatabase::TouchOnDemandFavicon(const GURL& icon_url, |
| 559 FaviconBitmapID bitmap_id, | 589 base::Time time) { |
| 560 base::Time time) { | 590 // Look up the icon ids for the url. |
| 561 DCHECK(bitmap_id); | 591 sql::Statement id_statement(db_.GetCachedStatement( |
| 562 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 592 SQL_FROM_HERE, "SELECT id FROM favicons WHERE url=?")); |
| 563 "UPDATE favicon_bitmaps SET last_requested=? WHERE id=?")); | 593 id_statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); |
| 564 statement.BindInt64(0, time.ToInternalValue()); | 594 |
| 565 statement.BindInt64(1, bitmap_id); | 595 base::Time max_time = |
| 566 return statement.Run(); | 596 time - base::TimeDelta::FromDays(kFaviconUpdateLastRequestedAfterDays); |
| 597 |
| 598 while (id_statement.Step()) { |
| 599 favicon_base::FaviconID icon_id = id_statement.ColumnInt64(0); |
| 600 |
| 601 // Update the time only for ON_DEMAND bitmaps (i.e. with last_requested > |
| 602 // 0). For performance reasons, update the time only if the currently stored |
| 603 // time is old enough (UPDATEs where the WHERE condition does not match any |
| 604 // entries are way faster than UPDATEs that really change some data). |
| 605 sql::Statement statement(db_.GetCachedStatement( |
| 606 SQL_FROM_HERE, |
| 607 "UPDATE favicon_bitmaps SET last_requested=? WHERE icon_id=? AND " |
| 608 "last_requested>0 AND last_requested<=?")); |
| 609 statement.BindInt64(0, time.ToInternalValue()); |
| 610 statement.BindInt64(1, icon_id); |
| 611 statement.BindInt64(2, max_time.ToInternalValue()); |
| 612 if (!statement.Run()) |
| 613 return false; |
| 614 } |
| 615 return true; |
| 567 } | 616 } |
| 568 | 617 |
| 569 bool ThumbnailDatabase::DeleteFaviconBitmap(FaviconBitmapID bitmap_id) { | 618 bool ThumbnailDatabase::DeleteFaviconBitmap(FaviconBitmapID bitmap_id) { |
| 570 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, | 619 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, |
| 571 "DELETE FROM favicon_bitmaps WHERE id=?")); | 620 "DELETE FROM favicon_bitmaps WHERE id=?")); |
| 572 statement.BindInt64(0, bitmap_id); | 621 statement.BindInt64(0, bitmap_id); |
| 573 return statement.Run(); | 622 return statement.Run(); |
| 574 } | 623 } |
| 575 | 624 |
| 576 bool ThumbnailDatabase::SetFaviconOutOfDate(favicon_base::FaviconID icon_id) { | 625 bool ThumbnailDatabase::SetFaviconOutOfDate(favicon_base::FaviconID icon_id) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 | 676 |
| 628 if (!statement.Run()) | 677 if (!statement.Run()) |
| 629 return 0; | 678 return 0; |
| 630 return db_.GetLastInsertRowId(); | 679 return db_.GetLastInsertRowId(); |
| 631 } | 680 } |
| 632 | 681 |
| 633 favicon_base::FaviconID ThumbnailDatabase::AddFavicon( | 682 favicon_base::FaviconID ThumbnailDatabase::AddFavicon( |
| 634 const GURL& icon_url, | 683 const GURL& icon_url, |
| 635 favicon_base::IconType icon_type, | 684 favicon_base::IconType icon_type, |
| 636 const scoped_refptr<base::RefCountedMemory>& icon_data, | 685 const scoped_refptr<base::RefCountedMemory>& icon_data, |
| 686 FaviconBitmapType type, |
| 637 base::Time time, | 687 base::Time time, |
| 638 const gfx::Size& pixel_size) { | 688 const gfx::Size& pixel_size) { |
| 639 favicon_base::FaviconID icon_id = AddFavicon(icon_url, icon_type); | 689 favicon_base::FaviconID icon_id = AddFavicon(icon_url, icon_type); |
| 640 if (!icon_id || !AddFaviconBitmap(icon_id, icon_data, time, pixel_size)) | 690 if (!icon_id || !AddFaviconBitmap(icon_id, icon_data, type, time, pixel_size)) |
| 641 return 0; | 691 return 0; |
| 642 | 692 |
| 643 return icon_id; | 693 return icon_id; |
| 644 } | 694 } |
| 645 | 695 |
| 646 bool ThumbnailDatabase::DeleteFavicon(favicon_base::FaviconID id) { | 696 bool ThumbnailDatabase::DeleteFavicon(favicon_base::FaviconID id) { |
| 647 sql::Statement statement; | 697 sql::Statement statement; |
| 648 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, | 698 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, |
| 649 "DELETE FROM favicons WHERE id = ?")); | 699 "DELETE FROM favicons WHERE id = ?")); |
| 650 statement.BindInt64(0, id); | 700 statement.BindInt64(0, id); |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1058 meta_table_.SetVersionNumber(8); | 1108 meta_table_.SetVersionNumber(8); |
| 1059 meta_table_.SetCompatibleVersionNumber(std::min(8, kCompatibleVersionNumber)); | 1109 meta_table_.SetCompatibleVersionNumber(std::min(8, kCompatibleVersionNumber)); |
| 1060 return true; | 1110 return true; |
| 1061 } | 1111 } |
| 1062 | 1112 |
| 1063 bool ThumbnailDatabase::IsFaviconDBStructureIncorrect() { | 1113 bool ThumbnailDatabase::IsFaviconDBStructureIncorrect() { |
| 1064 return !db_.IsSQLValid("SELECT id, url, icon_type FROM favicons"); | 1114 return !db_.IsSQLValid("SELECT id, url, icon_type FROM favicons"); |
| 1065 } | 1115 } |
| 1066 | 1116 |
| 1067 } // namespace history | 1117 } // namespace history |
| OLD | NEW |