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

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

Issue 1133463005: Update all bookmarks which use an icon URL when a favicon's bitmap is updated (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@startup_do_not_unexpire
Patch Set: Created 5 years, 7 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 1570 matching lines...) Expand 10 before | Expand all | Expand 10 after
1581 const GURL& icon_url, 1581 const GURL& icon_url,
1582 favicon_base::IconType icon_type, 1582 favicon_base::IconType icon_type,
1583 scoped_refptr<base::RefCountedMemory> bitmap_data, 1583 scoped_refptr<base::RefCountedMemory> bitmap_data,
1584 const gfx::Size& pixel_size) { 1584 const gfx::Size& pixel_size) {
1585 if (!thumbnail_db_ || !db_) 1585 if (!thumbnail_db_ || !db_)
1586 return; 1586 return;
1587 1587
1588 favicon_base::FaviconID favicon_id = 1588 favicon_base::FaviconID favicon_id =
1589 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, nullptr); 1589 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, nullptr);
1590 1590
1591 bool favicon_created = false;
1591 if (!favicon_id) { 1592 if (!favicon_id) {
1592 // There is no favicon at |icon_url|, create it. 1593 // There is no favicon at |icon_url|, create it.
1593 favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type); 1594 favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
1595 favicon_created = true;
1594 } 1596 }
1595 1597
1596 std::vector<FaviconBitmapIDSize> bitmap_id_sizes; 1598 std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
1597 thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes); 1599 thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes);
1598 1600
1599 // If there is already a favicon bitmap of |pixel_size| at |icon_url|, 1601 // If there is already a favicon bitmap of |pixel_size| at |icon_url|,
1600 // replace it. 1602 // replace it.
1601 bool bitmap_identical = false; 1603 bool bitmap_identical = false;
1602 bool replaced_bitmap = false; 1604 bool replaced_bitmap = false;
1603 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) { 1605 for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1667 // Difficulty 1: All but news.google.com/news_specific.ico are unmapped from 1669 // Difficulty 1: All but news.google.com/news_specific.ico are unmapped from
1668 // news.google.com 1670 // news.google.com
1669 // Difficulty 2: The favicon bitmaps for www.google.com/favicon.ico are not 1671 // Difficulty 2: The favicon bitmaps for www.google.com/favicon.ico are not
1670 // modified. 1672 // modified.
1671 1673
1672 std::vector<IconMapping> icon_mappings; 1674 std::vector<IconMapping> icon_mappings;
1673 thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings); 1675 thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings);
1674 1676
1675 // Copy the favicon bitmaps mapped to |page_url| to the favicon at |icon_url| 1677 // Copy the favicon bitmaps mapped to |page_url| to the favicon at |icon_url|
1676 // till the limit of |kMaxFaviconBitmapsPerIconURL| is reached. 1678 // till the limit of |kMaxFaviconBitmapsPerIconURL| is reached.
1679 bool favicon_bitmaps_copied = false;
1677 for (size_t i = 0; i < icon_mappings.size(); ++i) { 1680 for (size_t i = 0; i < icon_mappings.size(); ++i) {
1678 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL) 1681 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
1679 break; 1682 break;
1680 1683
1681 if (icon_mappings[i].icon_url == icon_url) 1684 if (icon_mappings[i].icon_url == icon_url)
1682 continue; 1685 continue;
1683 1686
1684 std::vector<FaviconBitmap> bitmaps_to_copy; 1687 std::vector<FaviconBitmap> bitmaps_to_copy;
1685 thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id, 1688 thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id,
1686 &bitmaps_to_copy); 1689 &bitmaps_to_copy);
1687 for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) { 1690 for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) {
1688 // Do not add a favicon bitmap at a pixel size for which there is already 1691 // Do not add a favicon bitmap at a pixel size for which there is already
1689 // a favicon bitmap mapped to |icon_url|. The one there is more correct 1692 // a favicon bitmap mapped to |icon_url|. The one there is more correct
1690 // and having multiple equally sized favicon bitmaps for |page_url| is 1693 // and having multiple equally sized favicon bitmaps for |page_url| is
1691 // ambiguous in terms of GetFaviconsForURL(). 1694 // ambiguous in terms of GetFaviconsForURL().
1692 std::vector<gfx::Size>::iterator it = 1695 std::vector<gfx::Size>::iterator it =
1693 std::find(favicon_sizes.begin(), favicon_sizes.end(), 1696 std::find(favicon_sizes.begin(), favicon_sizes.end(),
1694 bitmaps_to_copy[j].pixel_size); 1697 bitmaps_to_copy[j].pixel_size);
1695 if (it != favicon_sizes.end()) 1698 if (it != favicon_sizes.end())
1696 continue; 1699 continue;
1697 1700
1698 // Add the favicon bitmap as expired as it is not consistent with the 1701 // Add the favicon bitmap as expired as it is not consistent with the
1699 // merged in data. 1702 // merged in data.
1700 thumbnail_db_->AddFaviconBitmap( 1703 thumbnail_db_->AddFaviconBitmap(
1701 favicon_id, bitmaps_to_copy[j].bitmap_data, base::Time(), 1704 favicon_id, bitmaps_to_copy[j].bitmap_data, base::Time(),
1702 bitmaps_to_copy[j].pixel_size); 1705 bitmaps_to_copy[j].pixel_size);
1703 favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size); 1706 favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size);
1707 favicon_bitmaps_copied = true;
1704 1708
1705 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL) 1709 if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
1706 break; 1710 break;
1707 } 1711 }
1708 } 1712 }
1709 1713
1710 // 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
1711 // |page_url|. 1715 // |page_url|.
1712 bool mapping_changed = false; 1716 bool mapping_changed = false;
1713 if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) { 1717 if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) {
1714 std::vector<favicon_base::FaviconID> favicon_ids; 1718 std::vector<favicon_base::FaviconID> favicon_ids;
1715 favicon_ids.push_back(favicon_id); 1719 favicon_ids.push_back(favicon_id);
1716 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids); 1720 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids);
1717 mapping_changed = true; 1721 mapping_changed = true;
1718 } 1722 }
1719 1723
1720 if (mapping_changed || !bitmap_identical) 1724 if (mapping_changed)
1721 SendFaviconChangedNotificationForPageAndRedirects(page_url); 1725 SendFaviconChangedNotificationForPageAndRedirects(page_url);
1726
1727 if (!favicon_created && (!bitmap_identical || favicon_bitmaps_copied)) {
1728 // If there was a favicon at |icon_url| prior to MergeFavicon() being
1729 // called, there may be page URLs which also use the favicon at |icon_url|.
1730 // Notify the UI that the favicon has changed for |icon_url|.
1731 SendFaviconChangedNotificationForIconURL(icon_url);
1732 }
1733
1722 ScheduleCommit(); 1734 ScheduleCommit();
1723 } 1735 }
1724 1736
1725 void HistoryBackend::SetFavicons(const GURL& page_url, 1737 void HistoryBackend::SetFavicons(const GURL& page_url,
1726 favicon_base::IconType icon_type, 1738 favicon_base::IconType icon_type,
1727 const GURL& icon_url, 1739 const GURL& icon_url,
1728 const std::vector<SkBitmap>& bitmaps) { 1740 const std::vector<SkBitmap>& bitmaps) {
1729 if (!thumbnail_db_ || !db_) 1741 if (!thumbnail_db_ || !db_)
1730 return; 1742 return;
1731 1743
1732 DCHECK_GE(kMaxFaviconBitmapsPerIconURL, bitmaps.size()); 1744 DCHECK_GE(kMaxFaviconBitmapsPerIconURL, bitmaps.size());
1733 1745
1734 // Track whether the method modifies or creates any favicon bitmaps, favicons
1735 // or icon mappings.
1736 bool data_modified = false;
1737
1738 favicon_base::FaviconID icon_id = 1746 favicon_base::FaviconID icon_id =
1739 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, nullptr); 1747 thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, nullptr);
1740 1748
1749 bool favicon_created = false;
1741 if (!icon_id) { 1750 if (!icon_id) {
1742 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type); 1751 icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
1743 data_modified = true; 1752 favicon_created = true;
1744 } 1753 }
1745 1754
1746 data_modified |= SetFaviconBitmaps(icon_id, bitmaps); 1755 bool favicon_data_modified = SetFaviconBitmaps(icon_id, bitmaps);
1747 1756
1748 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id); 1757 std::vector<favicon_base::FaviconID> icon_ids(1u, icon_id);
1749 data_modified |= 1758 bool mapping_changed =
1750 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids); 1759 SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids);
1751 1760
1752 if (data_modified) { 1761 if (mapping_changed) {
1753 // Send notification to the UI as an icon mapping, favicon, or favicon 1762 // Notify the UI that this function changed an icon mapping.
1754 // bitmap was changed by this function.
1755 SendFaviconChangedNotificationForPageAndRedirects(page_url); 1763 SendFaviconChangedNotificationForPageAndRedirects(page_url);
1756 } 1764 }
1765
1766 if (favicon_data_modified && !favicon_created) {
1767 // If there was a favicon at |icon_url| prior to SetFavicons() being called,
1768 // there may be page URLs which also use the favicon at |icon_url|. Notify
1769 // the UI that the favicon has changed for |icon_url|.
1770 SendFaviconChangedNotificationForIconURL(icon_url);
1771 }
1757 ScheduleCommit(); 1772 ScheduleCommit();
1758 } 1773 }
1759 1774
1760 void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) { 1775 void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) {
1761 std::vector<IconMapping> icon_mappings; 1776 std::vector<IconMapping> icon_mappings;
1762 1777
1763 if (!thumbnail_db_ || 1778 if (!thumbnail_db_ ||
1764 !thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings)) 1779 !thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings))
1765 return; 1780 return;
1766 1781
1767 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); 1782 for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
1768 m != icon_mappings.end(); ++m) { 1783 m != icon_mappings.end(); ++m) {
1769 thumbnail_db_->SetFaviconOutOfDate(m->icon_id); 1784 thumbnail_db_->SetFaviconOutOfDate(m->icon_id);
1770 } 1785 }
1771 ScheduleCommit(); 1786 ScheduleCommit();
1772 } 1787 }
1773 1788
1774 void HistoryBackend::SetImportedFavicons( 1789 void HistoryBackend::SetImportedFavicons(
1775 const favicon_base::FaviconUsageDataList& favicon_usage) { 1790 const favicon_base::FaviconUsageDataList& favicon_usage) {
1776 if (!db_ || !thumbnail_db_) 1791 if (!db_ || !thumbnail_db_)
1777 return; 1792 return;
1778 1793
1779 Time now = Time::Now(); 1794 Time now = Time::Now();
1780 1795
1781 // Track all URLs that had their favicons set or updated. 1796 // Track all URLs that had their favicons set or updated.
1782 std::set<GURL> favicons_changed; 1797 std::vector<GURL> favicons_changed;
1783 1798
1784 for (size_t i = 0; i < favicon_usage.size(); i++) { 1799 for (size_t i = 0; i < favicon_usage.size(); i++) {
1785 favicon_base::FaviconID favicon_id = 1800 favicon_base::FaviconID favicon_id =
1786 thumbnail_db_->GetFaviconIDForFaviconURL( 1801 thumbnail_db_->GetFaviconIDForFaviconURL(
1787 favicon_usage[i].favicon_url, favicon_base::FAVICON, nullptr); 1802 favicon_usage[i].favicon_url, favicon_base::FAVICON, nullptr);
1788 if (!favicon_id) { 1803 if (!favicon_id) {
1789 // This favicon doesn't exist yet, so we create it using the given data. 1804 // This favicon doesn't exist yet, so we create it using the given data.
1790 // TODO(pkotwicz): Pass in real pixel size. 1805 // TODO(pkotwicz): Pass in real pixel size.
1791 favicon_id = thumbnail_db_->AddFavicon( 1806 favicon_id = thumbnail_db_->AddFavicon(
1792 favicon_usage[i].favicon_url, favicon_base::FAVICON, 1807 favicon_usage[i].favicon_url, favicon_base::FAVICON,
(...skipping 13 matching lines...) Expand all
1806 // cleaned, we keep an entry in the db with 0 visits as long as that 1821 // cleaned, we keep an entry in the db with 0 visits as long as that
1807 // url is bookmarked. 1822 // url is bookmarked.
1808 if (history_client && history_client->IsBookmarked(*url)) { 1823 if (history_client && history_client->IsBookmarked(*url)) {
1809 URLRow url_info(*url); 1824 URLRow url_info(*url);
1810 url_info.set_visit_count(0); 1825 url_info.set_visit_count(0);
1811 url_info.set_typed_count(0); 1826 url_info.set_typed_count(0);
1812 url_info.set_last_visit(base::Time()); 1827 url_info.set_last_visit(base::Time());
1813 url_info.set_hidden(false); 1828 url_info.set_hidden(false);
1814 db_->AddURL(url_info); 1829 db_->AddURL(url_info);
1815 thumbnail_db_->AddIconMapping(*url, favicon_id); 1830 thumbnail_db_->AddIconMapping(*url, favicon_id);
1816 favicons_changed.insert(*url); 1831 favicons_changed.push_back(*url);
1817 } 1832 }
1818 } else { 1833 } else {
1819 if (!thumbnail_db_->GetIconMappingsForPageURL( 1834 if (!thumbnail_db_->GetIconMappingsForPageURL(
1820 *url, favicon_base::FAVICON, nullptr)) { 1835 *url, favicon_base::FAVICON, nullptr)) {
1821 // URL is present in history, update the favicon *only* if it is not 1836 // URL is present in history, update the favicon *only* if it is not
1822 // set already. 1837 // set already.
1823 thumbnail_db_->AddIconMapping(*url, favicon_id); 1838 thumbnail_db_->AddIconMapping(*url, favicon_id);
1824 favicons_changed.insert(*url); 1839 favicons_changed.push_back(*url);
1825 } 1840 }
1826 } 1841 }
1827 } 1842 }
1828 } 1843 }
1829 1844
1830 if (!favicons_changed.empty()) { 1845 if (!favicons_changed.empty()) {
1831 // Send the notification about the changed favicon URLs. 1846 // Send the notification about the changed favicon URLs.
1832 NotifyFaviconChanged(favicons_changed); 1847 NotifyFaviconsChanged(favicons_changed, std::vector<GURL>());
1833 } 1848 }
1834 } 1849 }
1835 1850
1836 void HistoryBackend::UpdateFaviconMappingsAndFetchImpl( 1851 void HistoryBackend::UpdateFaviconMappingsAndFetchImpl(
1837 const GURL* page_url, 1852 const GURL* page_url,
1838 const std::vector<GURL>& icon_urls, 1853 const std::vector<GURL>& icon_urls,
1839 int icon_types, 1854 int icon_types,
1840 const std::vector<int>& desired_sizes, 1855 const std::vector<int>& desired_sizes,
1841 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results) { 1856 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results) {
1842 // If |page_url| is specified, |icon_types| must be either a single icon 1857 // If |page_url| is specified, |icon_types| must be either a single icon
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
2151 // No known redirects, construct mock redirect chain containing |page_url|. 2166 // No known redirects, construct mock redirect chain containing |page_url|.
2152 redirect_list->push_back(page_url); 2167 redirect_list->push_back(page_url);
2153 } 2168 }
2154 } 2169 }
2155 2170
2156 void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects( 2171 void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects(
2157 const GURL& page_url) { 2172 const GURL& page_url) {
2158 RedirectList redirect_list; 2173 RedirectList redirect_list;
2159 GetCachedRecentRedirects(page_url, &redirect_list); 2174 GetCachedRecentRedirects(page_url, &redirect_list);
2160 if (!redirect_list.empty()) { 2175 if (!redirect_list.empty()) {
2161 std::set<GURL> favicons_changed(redirect_list.begin(), redirect_list.end()); 2176 std::vector<GURL> favicons_changed(redirect_list.begin(),
2162 NotifyFaviconChanged(favicons_changed); 2177 redirect_list.end());
2178 NotifyFaviconsChanged(favicons_changed, std::vector<GURL>());
2163 } 2179 }
2164 } 2180 }
2165 2181
2182 void HistoryBackend::SendFaviconChangedNotificationForIconURL(
2183 const GURL& icon_url) {
2184 NotifyFaviconsChanged(std::vector<GURL>(),
2185 std::vector<GURL>(1u, icon_url));
2186 }
2187
2166 void HistoryBackend::Commit() { 2188 void HistoryBackend::Commit() {
2167 if (!db_) 2189 if (!db_)
2168 return; 2190 return;
2169 2191
2170 #if defined(OS_IOS) 2192 #if defined(OS_IOS)
2171 // Attempts to get the application running long enough to commit the database 2193 // Attempts to get the application running long enough to commit the database
2172 // transaction if it is currently being backgrounded. 2194 // transaction if it is currently being backgrounded.
2173 base::ios::ScopedCriticalAction scoped_critical_action; 2195 base::ios::ScopedCriticalAction scoped_critical_action;
2174 #endif 2196 #endif
2175 2197
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
2438 scoped_ptr<HistoryDBTask> task, 2460 scoped_ptr<HistoryDBTask> task,
2439 scoped_refptr<base::SingleThreadTaskRunner> origin_loop, 2461 scoped_refptr<base::SingleThreadTaskRunner> origin_loop,
2440 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled) { 2462 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled) {
2441 bool scheduled = !queued_history_db_tasks_.empty(); 2463 bool scheduled = !queued_history_db_tasks_.empty();
2442 queued_history_db_tasks_.push_back( 2464 queued_history_db_tasks_.push_back(
2443 new QueuedHistoryDBTask(task.Pass(), origin_loop, is_canceled)); 2465 new QueuedHistoryDBTask(task.Pass(), origin_loop, is_canceled));
2444 if (!scheduled) 2466 if (!scheduled)
2445 ProcessDBTaskImpl(); 2467 ProcessDBTaskImpl();
2446 } 2468 }
2447 2469
2448 void HistoryBackend::NotifyFaviconChanged(const std::set<GURL>& urls) { 2470 void HistoryBackend::NotifyFaviconsChanged(const std::vector<GURL>& page_urls,
2471 const std::vector<GURL>& icon_urls) {
2449 if (delegate_) 2472 if (delegate_)
2450 delegate_->NotifyFaviconChanged(urls); 2473 delegate_->NotifyFaviconsChanged(page_urls, icon_urls);
2451 } 2474 }
2452 2475
2453 void HistoryBackend::NotifyURLVisited(ui::PageTransition transition, 2476 void HistoryBackend::NotifyURLVisited(ui::PageTransition transition,
2454 const URLRow& row, 2477 const URLRow& row,
2455 const RedirectList& redirects, 2478 const RedirectList& redirects,
2456 base::Time visit_time) { 2479 base::Time visit_time) {
2457 URLRow url_info(row); 2480 URLRow url_info(row);
2458 if (typed_url_syncable_service_.get()) 2481 if (typed_url_syncable_service_.get())
2459 typed_url_syncable_service_->OnUrlVisited(transition, &url_info); 2482 typed_url_syncable_service_->OnUrlVisited(transition, &url_info);
2460 2483
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
2639 return true; 2662 return true;
2640 } 2663 }
2641 2664
2642 HistoryClient* HistoryBackend::GetHistoryClient() { 2665 HistoryClient* HistoryBackend::GetHistoryClient() {
2643 if (history_client_) 2666 if (history_client_)
2644 history_client_->BlockUntilBookmarksLoaded(); 2667 history_client_->BlockUntilBookmarksLoaded();
2645 return history_client_; 2668 return history_client_;
2646 } 2669 }
2647 2670
2648 } // namespace history 2671 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698