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

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

Issue 2856873002: [Thumbnails DB] Allow setting last_requested time when accessing favicons. (Closed)
Patch Set: Peter's comments Created 3 years, 6 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
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/history_backend.h" 5 #include "components/history/core/browser/history_backend.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <list> 9 #include <list>
10 #include <map> 10 #include <map>
(...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after
1629 // Set the preexisting favicon bitmaps as expired as the preexisting favicon 1629 // Set the preexisting favicon bitmaps as expired as the preexisting favicon
1630 // bitmaps are not consistent with the merged in data. 1630 // bitmaps are not consistent with the merged in data.
1631 thumbnail_db_->SetFaviconOutOfDate(favicon_id); 1631 thumbnail_db_->SetFaviconOutOfDate(favicon_id);
1632 1632
1633 // Delete an arbitrary favicon bitmap to avoid going over the limit of 1633 // Delete an arbitrary favicon bitmap to avoid going over the limit of
1634 // |kMaxFaviconBitmapsPerIconURL|. 1634 // |kMaxFaviconBitmapsPerIconURL|.
1635 if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) { 1635 if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) {
1636 thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id); 1636 thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id);
1637 favicon_sizes.erase(favicon_sizes.begin()); 1637 favicon_sizes.erase(favicon_sizes.begin());
1638 } 1638 }
1639 thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data, base::Time::Now(), 1639 thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data,
1640 pixel_size); 1640 FaviconBitmapType::ON_VISIT,
1641 base::Time::Now(), pixel_size);
1641 favicon_sizes.push_back(pixel_size); 1642 favicon_sizes.push_back(pixel_size);
1642 } 1643 }
1643 1644
1644 // A site may have changed the favicons that it uses for |page_url|. 1645 // A site may have changed the favicons that it uses for |page_url|.
1645 // Example Scenario: 1646 // Example Scenario:
1646 // page_url = news.google.com 1647 // page_url = news.google.com
1647 // Initial State: www.google.com/favicon.ico 16x16, 32x32 1648 // Initial State: www.google.com/favicon.ico 16x16, 32x32
1648 // MergeFavicon(news.google.com, news.google.com/news_specific.ico, ..., 1649 // MergeFavicon(news.google.com, news.google.com/news_specific.ico, ...,
1649 // ..., 16x16) 1650 // ..., 16x16)
1650 // 1651 //
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1691 // and having multiple equally sized favicon bitmaps for |page_url| is 1692 // and having multiple equally sized favicon bitmaps for |page_url| is
1692 // ambiguous in terms of GetFaviconsForURL(). 1693 // ambiguous in terms of GetFaviconsForURL().
1693 std::vector<gfx::Size>::iterator it = 1694 std::vector<gfx::Size>::iterator it =
1694 std::find(favicon_sizes.begin(), favicon_sizes.end(), 1695 std::find(favicon_sizes.begin(), favicon_sizes.end(),
1695 bitmaps_to_copy[j].pixel_size); 1696 bitmaps_to_copy[j].pixel_size);
1696 if (it != favicon_sizes.end()) 1697 if (it != favicon_sizes.end())
1697 continue; 1698 continue;
1698 1699
1699 // Add the favicon bitmap as expired as it is not consistent with the 1700 // Add the favicon bitmap as expired as it is not consistent with the
1700 // merged in data. 1701 // merged in data.
1701 thumbnail_db_->AddFaviconBitmap( 1702 thumbnail_db_->AddFaviconBitmap(favicon_id,
1702 favicon_id, bitmaps_to_copy[j].bitmap_data, base::Time(), 1703 bitmaps_to_copy[j].bitmap_data,
1703 bitmaps_to_copy[j].pixel_size); 1704 FaviconBitmapType::ON_VISIT, base::Time(),
1705 bitmaps_to_copy[j].pixel_size);
1704 favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size); 1706 favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size);
1705 favicon_bitmaps_copied = true; 1707 favicon_bitmaps_copied = true;
1706 1708
1707 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL) 1709 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
1708 break; 1710 break;
1709 } 1711 }
1710 } 1712 }
1711 1713
1712 // Update the favicon mappings such that only |icon_url| is mapped to 1714 // Update the favicon mappings such that only |icon_url| is mapped to
1713 // |page_url|. 1715 // |page_url|.
(...skipping 12 matching lines...) Expand all
1726 } 1728 }
1727 1729
1728 ScheduleCommit(); 1730 ScheduleCommit();
1729 } 1731 }
1730 1732
1731 void HistoryBackend::SetFavicons(const GURL& page_url, 1733 void HistoryBackend::SetFavicons(const GURL& page_url,
1732 favicon_base::IconType icon_type, 1734 favicon_base::IconType icon_type,
1733 const GURL& icon_url, 1735 const GURL& icon_url,
1734 const std::vector<SkBitmap>& bitmaps) { 1736 const std::vector<SkBitmap>& bitmaps) {
1735 SetFaviconsImpl(page_url, icon_type, icon_url, bitmaps, 1737 SetFaviconsImpl(page_url, icon_type, icon_url, bitmaps,
1736 /*bitmaps_are_expired=*/false); 1738 FaviconBitmapType::ON_VISIT);
1737 } 1739 }
1738 1740
1739 bool HistoryBackend::SetLastResortFavicons( 1741 bool HistoryBackend::SetOnDemandFavicons(const GURL& page_url,
1740 const GURL& page_url, 1742 favicon_base::IconType icon_type,
1741 favicon_base::IconType icon_type, 1743 const GURL& icon_url,
1742 const GURL& icon_url, 1744 const std::vector<SkBitmap>& bitmaps) {
1743 const std::vector<SkBitmap>& bitmaps) {
1744 if (!thumbnail_db_ || !db_) 1745 if (!thumbnail_db_ || !db_)
1745 return false; 1746 return false;
1746 1747
1747 // Verify there's no known data for the page URL. 1748 // Verify there's no known data for the page URL.
1748 if (thumbnail_db_->GetIconMappingsForPageURL(page_url, 1749 if (thumbnail_db_->GetIconMappingsForPageURL(page_url,
1749 /*mapping_data=*/nullptr)) { 1750 /*mapping_data=*/nullptr)) {
1750 return false; 1751 return false;
1751 } 1752 }
1752 1753
1753 return SetFaviconsImpl(page_url, icon_type, icon_url, bitmaps, 1754 return SetFaviconsImpl(page_url, icon_type, icon_url, bitmaps,
1754 /*bitmaps_are_expired=*/true); 1755 FaviconBitmapType::ON_DEMAND);
1755 } 1756 }
1756 1757
1757 void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) { 1758 void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) {
1758 std::vector<IconMapping> icon_mappings; 1759 std::vector<IconMapping> icon_mappings;
1759 1760
1760 if (!thumbnail_db_ || 1761 if (!thumbnail_db_ ||
1761 !thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings)) 1762 !thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings))
1762 return; 1763 return;
1763 1764
1764 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 1765 for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
1765 m != icon_mappings.end(); ++m) { 1766 m != icon_mappings.end(); ++m) {
1766 thumbnail_db_->SetFaviconOutOfDate(m->icon_id); 1767 thumbnail_db_->SetFaviconOutOfDate(m->icon_id);
1767 } 1768 }
1768 ScheduleCommit(); 1769 ScheduleCommit();
1769 } 1770 }
1770 1771
1772 void HistoryBackend::TouchOnDemandFavicon(const GURL& icon_url) {
1773 if (!thumbnail_db_)
1774 return;
1775
1776 thumbnail_db_->TouchOnDemandFavicon(icon_url, Time::Now());
1777 ScheduleCommit();
1778 }
1779
1771 void HistoryBackend::SetImportedFavicons( 1780 void HistoryBackend::SetImportedFavicons(
1772 const favicon_base::FaviconUsageDataList& favicon_usage) { 1781 const favicon_base::FaviconUsageDataList& favicon_usage) {
1773 if (!db_ || !thumbnail_db_) 1782 if (!db_ || !thumbnail_db_)
1774 return; 1783 return;
1775 1784
1776 Time now = Time::Now(); 1785 Time now = Time::Now();
1777 1786
1778 // Track all URLs that had their favicons set or updated. 1787 // Track all URLs that had their favicons set or updated.
1779 std::set<GURL> favicons_changed; 1788 std::set<GURL> favicons_changed;
1780 1789
1781 for (size_t i = 0; i < favicon_usage.size(); i++) { 1790 for (size_t i = 0; i < favicon_usage.size(); i++) {
1782 favicon_base::FaviconID favicon_id = 1791 favicon_base::FaviconID favicon_id =
1783 thumbnail_db_->GetFaviconIDForFaviconURL(favicon_usage[i].favicon_url, 1792 thumbnail_db_->GetFaviconIDForFaviconURL(favicon_usage[i].favicon_url,
1784 favicon_base::FAVICON); 1793 favicon_base::FAVICON);
1785 if (!favicon_id) { 1794 if (!favicon_id) {
1786 // This favicon doesn't exist yet, so we create it using the given data. 1795 // This favicon doesn't exist yet, so we create it using the given data.
1787 // TODO(pkotwicz): Pass in real pixel size. 1796 // TODO(pkotwicz): Pass in real pixel size.
1788 favicon_id = thumbnail_db_->AddFavicon( 1797 favicon_id = thumbnail_db_->AddFavicon(
1789 favicon_usage[i].favicon_url, favicon_base::FAVICON, 1798 favicon_usage[i].favicon_url, favicon_base::FAVICON,
1790 new base::RefCountedBytes(favicon_usage[i].png_data), now, 1799 new base::RefCountedBytes(favicon_usage[i].png_data),
1791 gfx::Size()); 1800 FaviconBitmapType::ON_VISIT, now, gfx::Size());
1792 } 1801 }
1793 1802
1794 // Save the mapping from all the URLs to the favicon. 1803 // Save the mapping from all the URLs to the favicon.
1795 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); 1804 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin();
1796 url != favicon_usage[i].urls.end(); ++url) { 1805 url != favicon_usage[i].urls.end(); ++url) {
1797 URLRow url_row; 1806 URLRow url_row;
1798 if (!db_->GetRowForURL(*url, &url_row)) { 1807 if (!db_->GetRowForURL(*url, &url_row)) {
1799 // If the URL is present as a bookmark, add the url in history to 1808 // If the URL is present as a bookmark, add the url in history to
1800 // save the favicon mapping. This will match with what history db does 1809 // save the favicon mapping. This will match with what history db does
1801 // for regular bookmarked URLs with favicons - when history db is 1810 // for regular bookmarked URLs with favicons - when history db is
(...skipping 24 matching lines...) Expand all
1826 if (!favicons_changed.empty()) { 1835 if (!favicons_changed.empty()) {
1827 // Send the notification about the changed favicon URLs. 1836 // Send the notification about the changed favicon URLs.
1828 NotifyFaviconsChanged(favicons_changed, GURL()); 1837 NotifyFaviconsChanged(favicons_changed, GURL());
1829 } 1838 }
1830 } 1839 }
1831 1840
1832 bool HistoryBackend::SetFaviconsImpl(const GURL& page_url, 1841 bool HistoryBackend::SetFaviconsImpl(const GURL& page_url,
1833 favicon_base::IconType icon_type, 1842 favicon_base::IconType icon_type,
1834 const GURL& icon_url, 1843 const GURL& icon_url,
1835 const std::vector<SkBitmap>& bitmaps, 1844 const std::vector<SkBitmap>& bitmaps,
1836 bool bitmaps_are_expired) { 1845 FaviconBitmapType type) {
1837 if (!thumbnail_db_ || !db_) 1846 if (!thumbnail_db_ || !db_)
1838 return false; 1847 return false;
1839 1848
1840 DCHECK_GE(kMaxFaviconBitmapsPerIconURL, bitmaps.size()); 1849 DCHECK_GE(kMaxFaviconBitmapsPerIconURL, bitmaps.size());
1841 1850
1842 favicon_base::FaviconID icon_id = 1851 favicon_base::FaviconID icon_id =
1843 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type); 1852 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type);
1844 1853
1845 bool favicon_created = false; 1854 bool favicon_created = false;
1846 if (!icon_id) { 1855 if (!icon_id) {
1847 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type); 1856 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
1848 favicon_created = true; 1857 favicon_created = true;
1849 } 1858 }
1850 1859
1851 bool favicon_data_modified = false; 1860 bool favicon_data_modified = false;
1852 if (favicon_created || !bitmaps_are_expired) 1861 if (favicon_created || type == FaviconBitmapType::ON_VISIT) {
1853 favicon_data_modified = SetFaviconBitmaps(icon_id, bitmaps); 1862 favicon_data_modified = SetFaviconBitmaps(icon_id, bitmaps, type);
1854 1863 }
1855 if (favicon_created && bitmaps_are_expired)
1856 thumbnail_db_->SetFaviconOutOfDate(icon_id);
1857 1864
1858 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id); 1865 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id);
1859 bool mapping_changed = 1866 bool mapping_changed =
1860 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); 1867 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids);
1861 1868
1862 if (mapping_changed) { 1869 if (mapping_changed) {
1863 // Notify the UI that this function changed an icon mapping. 1870 // Notify the UI that this function changed an icon mapping.
1864 SendFaviconChangedNotificationForPageAndRedirects(page_url); 1871 SendFaviconChangedNotificationForPageAndRedirects(page_url);
1865 } 1872 }
1866 1873
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1900 SendFaviconChangedNotificationForPageAndRedirects(*page_url); 1907 SendFaviconChangedNotificationForPageAndRedirects(*page_url);
1901 ScheduleCommit(); 1908 ScheduleCommit();
1902 } 1909 }
1903 } 1910 }
1904 1911
1905 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes, 1912 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes,
1906 bitmap_results); 1913 bitmap_results);
1907 } 1914 }
1908 1915
1909 bool HistoryBackend::SetFaviconBitmaps(favicon_base::FaviconID icon_id, 1916 bool HistoryBackend::SetFaviconBitmaps(favicon_base::FaviconID icon_id,
1910 const std::vector<SkBitmap>& bitmaps) { 1917 const std::vector<SkBitmap>& bitmaps,
1918 FaviconBitmapType type) {
1911 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; 1919 std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
1912 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); 1920 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes);
1913 1921
1914 typedef std::pair<scoped_refptr<base::RefCountedBytes>, gfx::Size> 1922 typedef std::pair<scoped_refptr<base::RefCountedBytes>, gfx::Size>
1915 PNGEncodedBitmap; 1923 PNGEncodedBitmap;
1916 std::vector<PNGEncodedBitmap> to_add; 1924 std::vector<PNGEncodedBitmap> to_add;
1917 for (size_t i = 0; i < bitmaps.size(); ++i) { 1925 for (size_t i = 0; i < bitmaps.size(); ++i) {
1918 scoped_refptr<base::RefCountedBytes> bitmap_data(new base::RefCountedBytes); 1926 scoped_refptr<base::RefCountedBytes> bitmap_data(new base::RefCountedBytes);
1919 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmaps[i], false, 1927 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmaps[i], false,
1920 &bitmap_data->data())) { 1928 &bitmap_data->data())) {
(...skipping 16 matching lines...) Expand all
1937 } 1945 }
1938 1946
1939 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; 1947 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id;
1940 if (match_it == to_add.end()) { 1948 if (match_it == to_add.end()) {
1941 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); 1949 thumbnail_db_->DeleteFaviconBitmap(bitmap_id);
1942 1950
1943 favicon_bitmaps_changed = true; 1951 favicon_bitmaps_changed = true;
1944 } else { 1952 } else {
1945 if (!favicon_bitmaps_changed && 1953 if (!favicon_bitmaps_changed &&
1946 IsFaviconBitmapDataEqual(bitmap_id, match_it->first)) { 1954 IsFaviconBitmapDataEqual(bitmap_id, match_it->first)) {
1947 thumbnail_db_->SetFaviconBitmapLastUpdateTime(bitmap_id, 1955 thumbnail_db_->SetFaviconBitmapLastUpdateTime(
1948 base::Time::Now()); 1956 bitmap_id, base::Time::Now() /* new last updated time */);
1949 } else { 1957 } else {
1950 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->first, 1958 thumbnail_db_->SetFaviconBitmap(
1951 base::Time::Now()); 1959 bitmap_id, match_it->first,
1960 base::Time::Now() /* new last updated time */);
1952 favicon_bitmaps_changed = true; 1961 favicon_bitmaps_changed = true;
1953 } 1962 }
1954 to_add.erase(match_it); 1963 to_add.erase(match_it);
1955 } 1964 }
1956 } 1965 }
1957 1966
1958 for (size_t i = 0; i < to_add.size(); ++i) { 1967 for (size_t i = 0; i < to_add.size(); ++i) {
1959 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].first, base::Time::Now(), 1968 thumbnail_db_->AddFaviconBitmap(
1960 to_add[i].second); 1969 icon_id, to_add[i].first, type,
1970 base::Time::Now() /* new last updated / last requested time */,
1971 to_add[i].second);
1961 1972
1962 favicon_bitmaps_changed = true; 1973 favicon_bitmaps_changed = true;
1963 } 1974 }
1964 return favicon_bitmaps_changed; 1975 return favicon_bitmaps_changed;
1965 } 1976 }
1966 1977
1967 bool HistoryBackend::IsFaviconBitmapDataEqual( 1978 bool HistoryBackend::IsFaviconBitmapDataEqual(
1968 FaviconBitmapID bitmap_id, 1979 FaviconBitmapID bitmap_id,
1969 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { 1980 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) {
1970 if (!new_bitmap_data) 1981 if (!new_bitmap_data)
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after
2639 // transaction is currently open. 2650 // transaction is currently open.
2640 db_->CommitTransaction(); 2651 db_->CommitTransaction();
2641 db_->Vacuum(); 2652 db_->Vacuum();
2642 db_->BeginTransaction(); 2653 db_->BeginTransaction();
2643 db_->GetStartDate(&first_recorded_time_); 2654 db_->GetStartDate(&first_recorded_time_);
2644 2655
2645 return true; 2656 return true;
2646 } 2657 }
2647 2658
2648 } // namespace history 2659 } // namespace history
OLDNEW
« no previous file with comments | « components/history/core/browser/history_backend.h ('k') | components/history/core/browser/history_backend_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698