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 "chrome/browser/history/history_backend.h" | 5 #include "chrome/browser/history/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 26 matching lines...) Expand all Loading... |
37 #include "chrome/browser/history/visit_filter.h" | 37 #include "chrome/browser/history/visit_filter.h" |
38 #include "chrome/common/chrome_constants.h" | 38 #include "chrome/common/chrome_constants.h" |
39 #include "chrome/common/importer/imported_favicon_usage.h" | 39 #include "chrome/common/importer/imported_favicon_usage.h" |
40 #include "chrome/common/url_constants.h" | 40 #include "chrome/common/url_constants.h" |
41 #include "components/favicon_base/select_favicon_frames.h" | 41 #include "components/favicon_base/select_favicon_frames.h" |
42 #include "components/history/core/browser/history_client.h" | 42 #include "components/history/core/browser/history_client.h" |
43 #include "components/history/core/browser/keyword_search_term.h" | 43 #include "components/history/core/browser/keyword_search_term.h" |
44 #include "components/history/core/browser/page_usage_data.h" | 44 #include "components/history/core/browser/page_usage_data.h" |
45 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 45 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
46 #include "sql/error_delegate_util.h" | 46 #include "sql/error_delegate_util.h" |
| 47 #include "third_party/skia/include/core/SkBitmap.h" |
| 48 #include "ui/gfx/codec/png_codec.h" |
47 #include "url/gurl.h" | 49 #include "url/gurl.h" |
48 | 50 |
49 #if defined(OS_ANDROID) | 51 #if defined(OS_ANDROID) |
50 #include "chrome/browser/history/android/android_provider_backend.h" | 52 #include "chrome/browser/history/android/android_provider_backend.h" |
51 #endif | 53 #endif |
52 | 54 |
53 using base::Time; | 55 using base::Time; |
54 using base::TimeDelta; | 56 using base::TimeDelta; |
55 using base::TimeTicks; | 57 using base::TimeTicks; |
56 | 58 |
(...skipping 1710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1767 favicon_ids.push_back(favicon_id); | 1769 favicon_ids.push_back(favicon_id); |
1768 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids); | 1770 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids); |
1769 mapping_changed = true; | 1771 mapping_changed = true; |
1770 } | 1772 } |
1771 | 1773 |
1772 if (mapping_changed || !bitmap_identical) | 1774 if (mapping_changed || !bitmap_identical) |
1773 SendFaviconChangedNotificationForPageAndRedirects(page_url); | 1775 SendFaviconChangedNotificationForPageAndRedirects(page_url); |
1774 ScheduleCommit(); | 1776 ScheduleCommit(); |
1775 } | 1777 } |
1776 | 1778 |
1777 void HistoryBackend::SetFavicons( | 1779 void HistoryBackend::SetFavicons(const GURL& page_url, |
1778 const GURL& page_url, | 1780 favicon_base::IconType icon_type, |
1779 favicon_base::IconType icon_type, | 1781 const GURL& icon_url, |
1780 const std::vector<favicon_base::FaviconRawBitmapData>& | 1782 const std::vector<SkBitmap>& bitmaps) { |
1781 favicon_bitmap_data) { | |
1782 if (!thumbnail_db_ || !db_) | 1783 if (!thumbnail_db_ || !db_) |
1783 return; | 1784 return; |
1784 | 1785 |
1785 DCHECK(ValidateSetFaviconsParams(favicon_bitmap_data)); | 1786 DCHECK_GE(kMaxFaviconBitmapsPerIconURL, bitmaps.size()); |
1786 | |
1787 // Build map of FaviconRawBitmapData for each icon url. | |
1788 typedef std::map<GURL, std::vector<favicon_base::FaviconRawBitmapData> > | |
1789 BitmapDataByIconURL; | |
1790 BitmapDataByIconURL grouped_by_icon_url; | |
1791 for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) { | |
1792 const GURL& icon_url = favicon_bitmap_data[i].icon_url; | |
1793 grouped_by_icon_url[icon_url].push_back(favicon_bitmap_data[i]); | |
1794 } | |
1795 | 1787 |
1796 // Track whether the method modifies or creates any favicon bitmaps, favicons | 1788 // Track whether the method modifies or creates any favicon bitmaps, favicons |
1797 // or icon mappings. | 1789 // or icon mappings. |
1798 bool data_modified = false; | 1790 bool data_modified = false; |
1799 | 1791 |
1800 std::vector<favicon_base::FaviconID> icon_ids; | 1792 favicon_base::FaviconID icon_id = |
1801 for (BitmapDataByIconURL::const_iterator it = grouped_by_icon_url.begin(); | 1793 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL); |
1802 it != grouped_by_icon_url.end(); ++it) { | |
1803 const GURL& icon_url = it->first; | |
1804 favicon_base::FaviconID icon_id = | |
1805 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL); | |
1806 | 1794 |
1807 if (!icon_id) { | 1795 if (!icon_id) { |
1808 // TODO(pkotwicz): Remove the favicon sizes attribute from | 1796 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type); |
1809 // ThumbnailDatabase::AddFavicon(). | 1797 data_modified = true; |
1810 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type); | |
1811 data_modified = true; | |
1812 } | |
1813 icon_ids.push_back(icon_id); | |
1814 | |
1815 if (!data_modified) | |
1816 SetFaviconBitmaps(icon_id, it->second, &data_modified); | |
1817 else | |
1818 SetFaviconBitmaps(icon_id, it->second, NULL); | |
1819 } | 1798 } |
1820 | 1799 |
| 1800 data_modified |= SetFaviconBitmaps(icon_id, bitmaps); |
| 1801 |
| 1802 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id); |
1821 data_modified |= | 1803 data_modified |= |
1822 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); | 1804 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); |
1823 | 1805 |
1824 if (data_modified) { | 1806 if (data_modified) { |
1825 // Send notification to the UI as an icon mapping, favicon, or favicon | 1807 // Send notification to the UI as an icon mapping, favicon, or favicon |
1826 // bitmap was changed by this function. | 1808 // bitmap was changed by this function. |
1827 SendFaviconChangedNotificationForPageAndRedirects(page_url); | 1809 SendFaviconChangedNotificationForPageAndRedirects(page_url); |
1828 } | 1810 } |
1829 ScheduleCommit(); | 1811 ScheduleCommit(); |
1830 } | 1812 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1977 if (mappings_updated) { | 1959 if (mappings_updated) { |
1978 SendFaviconChangedNotificationForPageAndRedirects(*page_url); | 1960 SendFaviconChangedNotificationForPageAndRedirects(*page_url); |
1979 ScheduleCommit(); | 1961 ScheduleCommit(); |
1980 } | 1962 } |
1981 } | 1963 } |
1982 | 1964 |
1983 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes, | 1965 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes, |
1984 bitmap_results); | 1966 bitmap_results); |
1985 } | 1967 } |
1986 | 1968 |
1987 void HistoryBackend::SetFaviconBitmaps( | 1969 bool HistoryBackend::SetFaviconBitmaps(favicon_base::FaviconID icon_id, |
1988 favicon_base::FaviconID icon_id, | 1970 const std::vector<SkBitmap>& bitmaps) { |
1989 const std::vector<favicon_base::FaviconRawBitmapData>& favicon_bitmap_data, | |
1990 bool* favicon_bitmaps_changed) { | |
1991 if (favicon_bitmaps_changed) | |
1992 *favicon_bitmaps_changed = false; | |
1993 | |
1994 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; | 1971 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; |
1995 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); | 1972 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); |
1996 | 1973 |
1997 std::vector<favicon_base::FaviconRawBitmapData> to_add = favicon_bitmap_data; | 1974 typedef std::pair<scoped_refptr<base::RefCountedBytes>, gfx::Size> |
| 1975 PNGEncodedBitmap; |
| 1976 std::vector<PNGEncodedBitmap> to_add; |
| 1977 for (size_t i = 0; i < bitmaps.size(); ++i) { |
| 1978 scoped_refptr<base::RefCountedBytes> bitmap_data( |
| 1979 new base::RefCountedBytes); |
| 1980 if (!gfx::PNGCodec::EncodeBGRASkBitmap( |
| 1981 bitmaps[i], false, &bitmap_data->data())) { |
| 1982 continue; |
| 1983 } |
| 1984 to_add.push_back(std::make_pair( |
| 1985 bitmap_data, gfx::Size(bitmaps[i].width(), bitmaps[i].height()))); |
| 1986 } |
1998 | 1987 |
| 1988 bool favicon_bitmaps_changed = false; |
1999 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { | 1989 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { |
2000 const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size; | 1990 const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size; |
2001 std::vector<favicon_base::FaviconRawBitmapData>::iterator match_it = | 1991 std::vector<PNGEncodedBitmap>::iterator match_it = to_add.end(); |
2002 to_add.end(); | 1992 for (std::vector<PNGEncodedBitmap>::iterator it = to_add.begin(); |
2003 for (std::vector<favicon_base::FaviconRawBitmapData>::iterator it = | |
2004 to_add.begin(); | |
2005 it != to_add.end(); | 1993 it != to_add.end(); |
2006 ++it) { | 1994 ++it) { |
2007 if (it->pixel_size == pixel_size) { | 1995 if (it->second == pixel_size) { |
2008 match_it = it; | 1996 match_it = it; |
2009 break; | 1997 break; |
2010 } | 1998 } |
2011 } | 1999 } |
2012 | 2000 |
2013 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; | 2001 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; |
2014 if (match_it == to_add.end()) { | 2002 if (match_it == to_add.end()) { |
2015 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); | 2003 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); |
2016 | 2004 |
2017 if (favicon_bitmaps_changed) | 2005 favicon_bitmaps_changed = true; |
2018 *favicon_bitmaps_changed = true; | |
2019 } else { | 2006 } else { |
2020 if (favicon_bitmaps_changed && | 2007 if (!favicon_bitmaps_changed && |
2021 !*favicon_bitmaps_changed && | 2008 IsFaviconBitmapDataEqual(bitmap_id, match_it->first)) { |
2022 IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) { | |
2023 thumbnail_db_->SetFaviconBitmapLastUpdateTime( | 2009 thumbnail_db_->SetFaviconBitmapLastUpdateTime( |
2024 bitmap_id, base::Time::Now()); | 2010 bitmap_id, base::Time::Now()); |
2025 } else { | 2011 } else { |
2026 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data, | 2012 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->first, |
2027 base::Time::Now()); | 2013 base::Time::Now()); |
2028 | 2014 favicon_bitmaps_changed = true; |
2029 if (favicon_bitmaps_changed) | |
2030 *favicon_bitmaps_changed = true; | |
2031 } | 2015 } |
2032 to_add.erase(match_it); | 2016 to_add.erase(match_it); |
2033 } | 2017 } |
2034 } | 2018 } |
2035 | 2019 |
2036 for (size_t i = 0; i < to_add.size(); ++i) { | 2020 for (size_t i = 0; i < to_add.size(); ++i) { |
2037 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data, | 2021 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].first, |
2038 base::Time::Now(), to_add[i].pixel_size); | 2022 base::Time::Now(), to_add[i].second); |
2039 | 2023 |
2040 if (favicon_bitmaps_changed) | 2024 favicon_bitmaps_changed = true; |
2041 *favicon_bitmaps_changed = true; | |
2042 } | 2025 } |
2043 } | 2026 return favicon_bitmaps_changed; |
2044 | |
2045 bool HistoryBackend::ValidateSetFaviconsParams(const std::vector< | |
2046 favicon_base::FaviconRawBitmapData>& favicon_bitmap_data) const { | |
2047 typedef std::map<GURL, size_t> BitmapsPerIconURL; | |
2048 BitmapsPerIconURL num_bitmaps_per_icon_url; | |
2049 for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) { | |
2050 if (!favicon_bitmap_data[i].bitmap_data.get()) | |
2051 return false; | |
2052 | |
2053 const GURL& icon_url = favicon_bitmap_data[i].icon_url; | |
2054 if (!num_bitmaps_per_icon_url.count(icon_url)) | |
2055 num_bitmaps_per_icon_url[icon_url] = 1u; | |
2056 else | |
2057 ++num_bitmaps_per_icon_url[icon_url]; | |
2058 } | |
2059 | |
2060 if (num_bitmaps_per_icon_url.size() > kMaxFaviconsPerPage) | |
2061 return false; | |
2062 | |
2063 for (BitmapsPerIconURL::const_iterator it = num_bitmaps_per_icon_url.begin(); | |
2064 it != num_bitmaps_per_icon_url.end(); ++it) { | |
2065 if (it->second > kMaxFaviconBitmapsPerIconURL) | |
2066 return false; | |
2067 } | |
2068 return true; | |
2069 } | 2027 } |
2070 | 2028 |
2071 bool HistoryBackend::IsFaviconBitmapDataEqual( | 2029 bool HistoryBackend::IsFaviconBitmapDataEqual( |
2072 FaviconBitmapID bitmap_id, | 2030 FaviconBitmapID bitmap_id, |
2073 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { | 2031 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { |
2074 if (!new_bitmap_data.get()) | 2032 if (!new_bitmap_data.get()) |
2075 return false; | 2033 return false; |
2076 | 2034 |
2077 scoped_refptr<base::RefCountedMemory> original_bitmap_data; | 2035 scoped_refptr<base::RefCountedMemory> original_bitmap_data; |
2078 thumbnail_db_->GetFaviconBitmap(bitmap_id, | 2036 thumbnail_db_->GetFaviconBitmap(bitmap_id, |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2753 int rank = kPageVisitStatsMaxTopSites; | 2711 int rank = kPageVisitStatsMaxTopSites; |
2754 std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url); | 2712 std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url); |
2755 if (it != most_visited_urls_map_.end()) | 2713 if (it != most_visited_urls_map_.end()) |
2756 rank = (*it).second; | 2714 rank = (*it).second; |
2757 UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank", | 2715 UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank", |
2758 rank, kPageVisitStatsMaxTopSites + 1); | 2716 rank, kPageVisitStatsMaxTopSites + 1); |
2759 } | 2717 } |
2760 #endif | 2718 #endif |
2761 | 2719 |
2762 } // namespace history | 2720 } // namespace history |
OLD | NEW |