diff options
-rw-r--r-- | chrome/browser/history/top_sites.cc | 23 | ||||
-rw-r--r-- | chrome/browser/history/top_sites.h | 2 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/global_history_menu.cc | 69 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/global_history_menu.h | 17 | ||||
-rw-r--r-- | chrome/browser/ui/gtk/global_menu_bar.cc | 2 |
5 files changed, 94 insertions, 19 deletions
diff --git a/chrome/browser/history/top_sites.cc b/chrome/browser/history/top_sites.cc index f985259..483b021 100644 --- a/chrome/browser/history/top_sites.cc +++ b/chrome/browser/history/top_sites.cc @@ -370,6 +370,7 @@ void TopSites::AddBlacklistedURL(const GURL& url) { } ResetThreadSafeCache(); + NotifyTopSitesChanged(); } void TopSites::RemoveBlacklistedURL(const GURL& url) { @@ -381,6 +382,7 @@ void TopSites::RemoveBlacklistedURL(const GURL& url) { blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL); } ResetThreadSafeCache(); + NotifyTopSitesChanged(); } bool TopSites::IsBlacklisted(const GURL& url) { @@ -397,6 +399,7 @@ void TopSites::ClearBlacklistedURLs() { blacklist->Clear(); } ResetThreadSafeCache(); + NotifyTopSitesChanged(); } void TopSites::AddPinnedURL(const GURL& url, size_t pinned_index) { @@ -419,6 +422,7 @@ void TopSites::AddPinnedURL(const GURL& url, size_t pinned_index) { } ResetThreadSafeCache(); + NotifyTopSitesChanged(); } bool TopSites::IsURLPinned(const GURL& url) { @@ -437,6 +441,7 @@ void TopSites::RemovePinnedURL(const GURL& url) { } ResetThreadSafeCache(); + NotifyTopSitesChanged(); } bool TopSites::GetPinnedURLAtIndex(size_t index, GURL* url) { @@ -832,11 +837,9 @@ void TopSites::SetTopSites(const MostVisitedURLList& new_top_sites) { MostVisitedURLList top_sites(new_top_sites); AddPrepopulatedPages(&top_sites); - bool changed = false; TopSitesDelta delta; DiffMostVisited(cache_->top_sites(), top_sites, &delta); if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { - changed = true; backend_->UpdateTopSites(delta); } @@ -860,7 +863,6 @@ void TopSites::SetTopSites(const MostVisitedURLList& new_top_sites) { SetPageThumbnailEncoded(mv.url, it->second.thumbnail, it->second.thumbnail_score); - changed = true; temp_images_.erase(it); break; } @@ -873,13 +875,7 @@ void TopSites::SetTopSites(const MostVisitedURLList& new_top_sites) { ResetThreadSafeCache(); ResetThreadSafeImageCache(); - - if (changed) { - NotificationService::current()->Notify( - NotificationType::TOP_SITES_CHANGED, - Source<TopSites>(this), - NotificationService::NoDetails()); - } + NotifyTopSitesChanged(); // Restart the timer that queries history for top sites. This is done to // ensure we stay in sync with history. @@ -932,6 +928,13 @@ void TopSites::ResetThreadSafeImageCache() { thread_safe_cache_->RemoveUnreferencedThumbnails(); } +void TopSites::NotifyTopSitesChanged() { + NotificationService::current()->Notify( + NotificationType::TOP_SITES_CHANGED, + Source<TopSites>(this), + NotificationService::NoDetails()); +} + void TopSites::RestartQueryForTopSitesTimer(base::TimeDelta delta) { if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < (base::TimeTicks::Now() + delta))) { diff --git a/chrome/browser/history/top_sites.h b/chrome/browser/history/top_sites.h index 055e0e6..11433a2 100644 --- a/chrome/browser/history/top_sites.h +++ b/chrome/browser/history/top_sites.h @@ -292,6 +292,8 @@ class TopSites void ResetThreadSafeImageCache(); + void NotifyTopSitesChanged(); + // Stops and starts timer with a delay of |delta|. void RestartQueryForTopSitesTimer(base::TimeDelta delta); diff --git a/chrome/browser/ui/gtk/global_history_menu.cc b/chrome/browser/ui/gtk/global_history_menu.cc index eb1f87c..119e8c7 100644 --- a/chrome/browser/ui/gtk/global_history_menu.cc +++ b/chrome/browser/ui/gtk/global_history_menu.cc @@ -7,9 +7,10 @@ #include <gtk/gtk.h> #include "base/stl_util-inl.h" -#include "base/utf_string_conversions.h" #include "base/string_number_conversions.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/favicon/favicon_service.h" +#include "chrome/browser/history/top_sites.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/sessions/tab_restore_service.h" #include "chrome/browser/sessions/tab_restore_service_factory.h" @@ -29,6 +30,9 @@ namespace { +// The maximum number of most visited items to display. +const unsigned int kMostVisitedCount = 12; + // The number of recently closed items to get. const unsigned int kRecentlyClosedCount = 10; @@ -95,6 +99,7 @@ class GlobalHistoryMenu::HistoryItem { GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser) : browser_(browser), profile_(browser_->profile()), + top_sites_(NULL), default_favicon_(NULL), tab_restore_service_(NULL) { } @@ -114,6 +119,16 @@ void GlobalHistoryMenu::Init(GtkWidget* history_menu) { default_favicon_ = GtkThemeService::GetDefaultFavicon(true); if (profile_) { + top_sites_ = profile_->GetTopSites(); + if (top_sites_) { + GetTopSitesData(); + + // Register for notification when TopSites changes so that we can update + // ourself. + registrar_.Add(this, NotificationType::TOP_SITES_CHANGED, + Source<history::TopSites>(top_sites_)); + } + tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); if (tab_restore_service_) { tab_restore_service_->LoadTabsFromLastSession(); @@ -130,6 +145,42 @@ void GlobalHistoryMenu::Init(GtkWidget* history_menu) { } } +void GlobalHistoryMenu::GetTopSitesData() { + DCHECK(top_sites_); + + top_sites_->GetMostVisitedURLs( + &top_sites_consumer_, + NewCallback(this, &GlobalHistoryMenu::OnTopSitesReceived)); +} + +void GlobalHistoryMenu::OnTopSitesReceived( + const history::MostVisitedURLList& visited_list) { + ClearMenuSection(history_menu_, GlobalMenuBar::TAG_MOST_VISITED); + + int index = GetIndexOfMenuItemWithTag( + history_menu_, + GlobalMenuBar::TAG_MOST_VISITED_HEADER) + 1; + + for (size_t i = 0; i < visited_list.size() && i < kMostVisitedCount; ++i) { + const history::MostVisitedURL& visited = visited_list[i]; + if (visited.url.spec().empty()) + break; // This is the signal that there are no more real visited sites. + + HistoryItem* item = new HistoryItem(); + item->title = visited.title; + item->url = visited.url; + + // The TopSites system doesn't give us icons; it gives us chrome:// urls to + // icons so fetch the icons normally. + GetFaviconForHistoryItem(item); + + AddHistoryItemToMenu(item, + history_menu_, + GlobalMenuBar::TAG_MOST_VISITED, + index++); + } +} + GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( GtkWidget* menu_item) { MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); @@ -318,12 +369,16 @@ void GlobalHistoryMenu::ClearMenuCallback(GtkWidget* menu_item, void GlobalHistoryMenu::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { - DCHECK(type.value == NotificationType::BROWSER_THEME_CHANGED); - - // Keeping track of which menu items have the default icon is going an - // error-prone pain, so instead just store the new default favicon and - // we'll update on the next menu change event. - default_favicon_ = GtkThemeService::GetDefaultFavicon(true); + if (type.value == NotificationType::BROWSER_THEME_CHANGED) { + // Keeping track of which menu items have the default icon is going an + // error-prone pain, so instead just store the new default favicon and + // we'll update on the next menu change event. + default_favicon_ = GtkThemeService::GetDefaultFavicon(true); + } else if (type.value == NotificationType::TOP_SITES_CHANGED) { + GetTopSitesData(); + } else { + NOTREACHED(); + } } void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { diff --git a/chrome/browser/ui/gtk/global_history_menu.h b/chrome/browser/ui/gtk/global_history_menu.h index d523ee5..ca2d1e1 100644 --- a/chrome/browser/ui/gtk/global_history_menu.h +++ b/chrome/browser/ui/gtk/global_history_menu.h @@ -7,15 +7,21 @@ #include <map> -#include "content/browser/cancelable_request.h" #include "chrome/browser/favicon/favicon_service.h" +#include "chrome/browser/history/history_types.h" #include "chrome/browser/sessions/tab_restore_service.h" #include "chrome/browser/sessions/tab_restore_service_observer.h" +#include "content/browser/cancelable_request.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" #include "ui/base/gtk/gtk_signal.h" class Browser; + +namespace history { +class TopSites; +} + typedef struct _GdkPixbuf GdkPixbuf; // Controls the History menu. @@ -36,6 +42,12 @@ class GlobalHistoryMenu : public NotificationObserver, typedef std::map<GtkWidget*, HistoryItem*> MenuItemToHistoryMap; + // Sends a message off to History for data. + void GetTopSitesData(); + + // Callback to receive data requested from GetTopSitesData(). + void OnTopSitesReceived(const history::MostVisitedURLList& visited_list); + // Returns the currently existing HistoryItem associated with // |menu_item|. Can return NULL. HistoryItem* HistoryItemForMenuItem(GtkWidget* menu_item); @@ -91,6 +103,9 @@ class GlobalHistoryMenu : public NotificationObserver, Browser* browser_; Profile* profile_; + history::TopSites* top_sites_; + CancelableRequestConsumer top_sites_consumer_; + NotificationRegistrar registrar_; GdkPixbuf* default_favicon_; diff --git a/chrome/browser/ui/gtk/global_menu_bar.cc b/chrome/browser/ui/gtk/global_menu_bar.cc index 368faaa..de70b8a 100644 --- a/chrome/browser/ui/gtk/global_menu_bar.cc +++ b/chrome/browser/ui/gtk/global_menu_bar.cc @@ -11,8 +11,8 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/gtk/accelerators_gtk.h" -#include "chrome/browser/ui/gtk/gtk_util.h" #include "chrome/browser/ui/gtk/gtk_theme_service.h" +#include "chrome/browser/ui/gtk/gtk_util.h" #include "chrome/common/pref_names.h" #include "content/common/notification_service.h" #include "grit/generated_resources.h" |