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::SetFavicon(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 if (!data_modified) | |
1801 SetFaviconBitmaps(icon_id, bitmaps, &data_modified); | |
1802 else | |
1803 SetFaviconBitmaps(icon_id, bitmaps, NULL); | |
1804 | |
1805 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id); | |
1821 data_modified |= | 1806 data_modified |= |
1822 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); | 1807 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); |
1823 | 1808 |
1824 if (data_modified) { | 1809 if (data_modified) { |
1825 // Send notification to the UI as an icon mapping, favicon, or favicon | 1810 // Send notification to the UI as an icon mapping, favicon, or favicon |
1826 // bitmap was changed by this function. | 1811 // bitmap was changed by this function. |
1827 SendFaviconChangedNotificationForPageAndRedirects(page_url); | 1812 SendFaviconChangedNotificationForPageAndRedirects(page_url); |
1828 } | 1813 } |
1829 ScheduleCommit(); | 1814 ScheduleCommit(); |
1830 } | 1815 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1977 if (mappings_updated) { | 1962 if (mappings_updated) { |
1978 SendFaviconChangedNotificationForPageAndRedirects(*page_url); | 1963 SendFaviconChangedNotificationForPageAndRedirects(*page_url); |
1979 ScheduleCommit(); | 1964 ScheduleCommit(); |
1980 } | 1965 } |
1981 } | 1966 } |
1982 | 1967 |
1983 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes, | 1968 GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_sizes, |
1984 bitmap_results); | 1969 bitmap_results); |
1985 } | 1970 } |
1986 | 1971 |
1987 void HistoryBackend::SetFaviconBitmaps( | 1972 void HistoryBackend::SetFaviconBitmaps(favicon_base::FaviconID icon_id, |
1988 favicon_base::FaviconID icon_id, | 1973 const std::vector<SkBitmap>& bitmaps, |
1989 const std::vector<favicon_base::FaviconRawBitmapData>& favicon_bitmap_data, | 1974 bool* favicon_bitmaps_changed) { |
sky
2014/09/02 17:57:42
This code and call site would be simpler if you re
pkotwicz
2014/09/03 04:15:08
You're right, done!
| |
1990 bool* favicon_bitmaps_changed) { | |
1991 if (favicon_bitmaps_changed) | 1975 if (favicon_bitmaps_changed) |
1992 *favicon_bitmaps_changed = false; | 1976 *favicon_bitmaps_changed = false; |
1993 | 1977 |
1994 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; | 1978 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; |
1995 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); | 1979 thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes); |
1996 | 1980 |
1997 std::vector<favicon_base::FaviconRawBitmapData> to_add = favicon_bitmap_data; | 1981 typedef std::pair<scoped_refptr<base::RefCountedBytes>, gfx::Size> |
1982 PNGEncodedBitmap; | |
1983 std::vector<PNGEncodedBitmap> to_add; | |
1984 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
1985 scoped_refptr<base::RefCountedBytes> bitmap_data( | |
1986 new base::RefCountedBytes); | |
1987 if (!gfx::PNGCodec::EncodeBGRASkBitmap( | |
1988 bitmaps[i], false, &bitmap_data->data())) { | |
1989 continue; | |
1990 } | |
1991 to_add.push_back(std::make_pair( | |
1992 bitmap_data, gfx::Size(bitmaps[i].width(), bitmaps[i].height()))); | |
1993 } | |
1998 | 1994 |
1999 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { | 1995 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { |
2000 const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size; | 1996 const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size; |
2001 std::vector<favicon_base::FaviconRawBitmapData>::iterator match_it = | 1997 std::vector<PNGEncodedBitmap>::iterator match_it = to_add.end(); |
2002 to_add.end(); | 1998 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(); | 1999 it != to_add.end(); |
2006 ++it) { | 2000 ++it) { |
2007 if (it->pixel_size == pixel_size) { | 2001 if (it->second == pixel_size) { |
2008 match_it = it; | 2002 match_it = it; |
2009 break; | 2003 break; |
2010 } | 2004 } |
2011 } | 2005 } |
2012 | 2006 |
2013 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; | 2007 FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id; |
2014 if (match_it == to_add.end()) { | 2008 if (match_it == to_add.end()) { |
2015 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); | 2009 thumbnail_db_->DeleteFaviconBitmap(bitmap_id); |
2016 | 2010 |
2017 if (favicon_bitmaps_changed) | 2011 if (favicon_bitmaps_changed) |
2018 *favicon_bitmaps_changed = true; | 2012 *favicon_bitmaps_changed = true; |
2019 } else { | 2013 } else { |
2020 if (favicon_bitmaps_changed && | 2014 if (favicon_bitmaps_changed && |
2021 !*favicon_bitmaps_changed && | 2015 !*favicon_bitmaps_changed && |
2022 IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) { | 2016 IsFaviconBitmapDataEqual(bitmap_id, match_it->first)) { |
2023 thumbnail_db_->SetFaviconBitmapLastUpdateTime( | 2017 thumbnail_db_->SetFaviconBitmapLastUpdateTime( |
2024 bitmap_id, base::Time::Now()); | 2018 bitmap_id, base::Time::Now()); |
2025 } else { | 2019 } else { |
2026 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data, | 2020 thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->first, |
2027 base::Time::Now()); | 2021 base::Time::Now()); |
2028 | 2022 |
2029 if (favicon_bitmaps_changed) | 2023 if (favicon_bitmaps_changed) |
2030 *favicon_bitmaps_changed = true; | 2024 *favicon_bitmaps_changed = true; |
2031 } | 2025 } |
2032 to_add.erase(match_it); | 2026 to_add.erase(match_it); |
2033 } | 2027 } |
2034 } | 2028 } |
2035 | 2029 |
2036 for (size_t i = 0; i < to_add.size(); ++i) { | 2030 for (size_t i = 0; i < to_add.size(); ++i) { |
2037 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data, | 2031 thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].first, |
2038 base::Time::Now(), to_add[i].pixel_size); | 2032 base::Time::Now(), to_add[i].second); |
2039 | 2033 |
2040 if (favicon_bitmaps_changed) | 2034 if (favicon_bitmaps_changed) |
2041 *favicon_bitmaps_changed = true; | 2035 *favicon_bitmaps_changed = true; |
2042 } | 2036 } |
2043 } | 2037 } |
2044 | 2038 |
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 } | |
2070 | |
2071 bool HistoryBackend::IsFaviconBitmapDataEqual( | 2039 bool HistoryBackend::IsFaviconBitmapDataEqual( |
2072 FaviconBitmapID bitmap_id, | 2040 FaviconBitmapID bitmap_id, |
2073 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { | 2041 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) { |
2074 if (!new_bitmap_data.get()) | 2042 if (!new_bitmap_data.get()) |
2075 return false; | 2043 return false; |
2076 | 2044 |
2077 scoped_refptr<base::RefCountedMemory> original_bitmap_data; | 2045 scoped_refptr<base::RefCountedMemory> original_bitmap_data; |
2078 thumbnail_db_->GetFaviconBitmap(bitmap_id, | 2046 thumbnail_db_->GetFaviconBitmap(bitmap_id, |
2079 NULL, | 2047 NULL, |
2080 &original_bitmap_data, | 2048 &original_bitmap_data, |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2753 int rank = kPageVisitStatsMaxTopSites; | 2721 int rank = kPageVisitStatsMaxTopSites; |
2754 std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url); | 2722 std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url); |
2755 if (it != most_visited_urls_map_.end()) | 2723 if (it != most_visited_urls_map_.end()) |
2756 rank = (*it).second; | 2724 rank = (*it).second; |
2757 UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank", | 2725 UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank", |
2758 rank, kPageVisitStatsMaxTopSites + 1); | 2726 rank, kPageVisitStatsMaxTopSites + 1); |
2759 } | 2727 } |
2760 #endif | 2728 #endif |
2761 | 2729 |
2762 } // namespace history | 2730 } // namespace history |
OLD | NEW |