Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/gtk/global_history_menu.h" | 5 #include "chrome/browser/ui/gtk/global_history_menu.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "chrome/browser/favicon_service.h" | 12 #include "chrome/browser/favicon_service.h" |
| 13 #include "chrome/browser/history/top_sites.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/sessions/tab_restore_service.h" | 15 #include "chrome/browser/sessions/tab_restore_service.h" |
| 15 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 16 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h" | 18 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h" |
| 18 #include "chrome/browser/ui/gtk/global_menu_bar.h" | 19 #include "chrome/browser/ui/gtk/global_menu_bar.h" |
| 19 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 20 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 20 #include "chrome/browser/ui/gtk/gtk_util.h" | 21 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 21 #include "chrome/browser/ui/gtk/owned_widget_gtk.h" | 22 #include "chrome/browser/ui/gtk/owned_widget_gtk.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 // the vector will be empty. | 89 // the vector will be empty. |
| 89 std::vector<HistoryItem*> tabs; | 90 std::vector<HistoryItem*> tabs; |
| 90 | 91 |
| 91 private: | 92 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(HistoryItem); | 93 DISALLOW_COPY_AND_ASSIGN(HistoryItem); |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser) | 96 GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser) |
| 96 : browser_(browser), | 97 : browser_(browser), |
| 97 profile_(browser_->profile()), | 98 profile_(browser_->profile()), |
| 99 top_sites_(NULL), | |
| 98 default_favicon_(NULL), | 100 default_favicon_(NULL), |
| 99 tab_restore_service_(NULL) { | 101 tab_restore_service_(NULL) { |
| 100 } | 102 } |
| 101 | 103 |
| 102 GlobalHistoryMenu::~GlobalHistoryMenu() { | 104 GlobalHistoryMenu::~GlobalHistoryMenu() { |
| 103 if (tab_restore_service_) | 105 if (tab_restore_service_) |
| 104 tab_restore_service_->RemoveObserver(this); | 106 tab_restore_service_->RemoveObserver(this); |
| 105 | 107 |
| 106 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), | 108 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), |
| 107 menu_item_history_map_.end()); | 109 menu_item_history_map_.end()); |
| 108 menu_item_history_map_.clear(); | 110 menu_item_history_map_.clear(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void GlobalHistoryMenu::Init(GtkWidget* history_menu) { | 113 void GlobalHistoryMenu::Init(GtkWidget* history_menu) { |
| 112 history_menu_ = history_menu; | 114 history_menu_ = history_menu; |
| 113 | 115 |
| 114 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 116 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
| 115 | 117 |
| 116 if (profile_) { | 118 if (profile_) { |
| 119 top_sites_ = profile_->GetTopSites(); | |
| 120 if (top_sites_) { | |
| 121 GetTopSitesData(); | |
| 122 | |
| 123 // Register for notification when TopSites changes so that we can update | |
| 124 // ourself. | |
| 125 registrar_.Add(this, NotificationType::TOP_SITES_CHANGED, | |
| 126 Source<history::TopSites>(top_sites_)); | |
| 127 } | |
| 128 | |
| 117 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); | 129 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); |
| 118 if (tab_restore_service_) { | 130 if (tab_restore_service_) { |
| 119 tab_restore_service_->LoadTabsFromLastSession(); | 131 tab_restore_service_->LoadTabsFromLastSession(); |
| 120 tab_restore_service_->AddObserver(this); | 132 tab_restore_service_->AddObserver(this); |
| 121 | 133 |
| 122 // If LoadTabsFromLastSession doesn't load tabs, it won't call | 134 // If LoadTabsFromLastSession doesn't load tabs, it won't call |
| 123 // TabRestoreServiceChanged(). This ensures that all new windows after | 135 // TabRestoreServiceChanged(). This ensures that all new windows after |
| 124 // the first one will have their menus populated correctly. | 136 // the first one will have their menus populated correctly. |
| 125 TabRestoreServiceChanged(tab_restore_service_); | 137 TabRestoreServiceChanged(tab_restore_service_); |
| 126 } | 138 } |
| 127 | 139 |
| 128 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 140 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
| 129 Source<Profile>(profile_)); | 141 Source<Profile>(profile_)); |
| 130 } | 142 } |
| 131 } | 143 } |
| 132 | 144 |
| 145 void GlobalHistoryMenu::GetTopSitesData() { | |
| 146 if (top_sites_) { | |
|
Evan Stade
2011/04/28 22:38:47
should this be a dcheck?
| |
| 147 top_sites_->GetMostVisitedURLs( | |
| 148 &top_sites_consumer_, | |
| 149 NewCallback(this, &GlobalHistoryMenu::OnTopSitesReceived)); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void GlobalHistoryMenu::OnTopSitesReceived( | |
| 154 const history::MostVisitedURLList& visited_list) { | |
| 155 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_MOST_VISITED); | |
| 156 | |
| 157 int index = GetIndexOfMenuItemWithTag( | |
| 158 history_menu_, | |
| 159 GlobalMenuBar::TAG_MOST_VISITED_HEADER) + 1; | |
| 160 | |
| 161 for (size_t i = 0; i < visited_list.size(); ++i) { | |
|
Evan Stade
2011/04/28 22:38:47
I think this list may be as long as 20; do we real
| |
| 162 const history::MostVisitedURL& visited = visited_list[i]; | |
| 163 if (visited.url.spec().empty()) | |
| 164 break; // This is the signal that there are no more real visited sites. | |
| 165 | |
| 166 HistoryItem* item = new HistoryItem(); | |
| 167 item->title = visited.title; | |
| 168 item->url = visited.url; | |
| 169 | |
| 170 // The TopSites system doesn't give us icons; it gives us chrome:// urls to | |
| 171 // icons so fetch the icons normally. | |
| 172 GetFaviconForHistoryItem(item); | |
| 173 | |
| 174 AddHistoryItemToMenu(item, | |
| 175 history_menu_, | |
| 176 GlobalMenuBar::TAG_MOST_VISITED, | |
| 177 index++); | |
| 178 } | |
| 179 } | |
| 180 | |
| 133 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( | 181 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( |
| 134 GtkWidget* menu_item) { | 182 GtkWidget* menu_item) { |
| 135 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); | 183 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); |
| 136 return it != menu_item_history_map_.end() ? it->second : NULL; | 184 return it != menu_item_history_map_.end() ? it->second : NULL; |
| 137 } | 185 } |
| 138 | 186 |
| 139 bool GlobalHistoryMenu::HasValidHistoryItemForTab( | 187 bool GlobalHistoryMenu::HasValidHistoryItemForTab( |
| 140 const TabRestoreService::Tab& entry) { | 188 const TabRestoreService::Tab& entry) { |
| 141 if (entry.navigations.empty()) | 189 if (entry.navigations.empty()) |
| 142 return false; | 190 return false; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 if (submenu) | 359 if (submenu) |
| 312 closure->menu_bar->ClearMenuSection(submenu, closure->tag); | 360 closure->menu_bar->ClearMenuSection(submenu, closure->tag); |
| 313 | 361 |
| 314 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item); | 362 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item); |
| 315 } | 363 } |
| 316 } | 364 } |
| 317 | 365 |
| 318 void GlobalHistoryMenu::Observe(NotificationType type, | 366 void GlobalHistoryMenu::Observe(NotificationType type, |
| 319 const NotificationSource& source, | 367 const NotificationSource& source, |
| 320 const NotificationDetails& details) { | 368 const NotificationDetails& details) { |
| 321 DCHECK(type.value == NotificationType::BROWSER_THEME_CHANGED); | 369 if (type.value == NotificationType::BROWSER_THEME_CHANGED) { |
| 322 | 370 // Keeping track of which menu items have the default icon is going an |
| 323 // Keeping track of which menu items have the default icon is going an | 371 // error-prone pain, so instead just store the new default favicon and |
| 324 // error-prone pain, so instead just store the new default favicon and | 372 // we'll update on the next menu change event. |
| 325 // we'll update on the next menu change event. | 373 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
| 326 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 374 } else if (type.value == NotificationType::TOP_SITES_CHANGED) { |
| 375 GetTopSitesData(); | |
| 376 } else { | |
| 377 NOTREACHED(); | |
| 378 } | |
| 327 } | 379 } |
| 328 | 380 |
| 329 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { | 381 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { |
| 330 const TabRestoreService::Entries& entries = service->entries(); | 382 const TabRestoreService::Entries& entries = service->entries(); |
| 331 | 383 |
| 332 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); | 384 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); |
| 333 | 385 |
| 334 // We'll get the index the "Recently Closed" header. (This can vary depending | 386 // We'll get the index the "Recently Closed" header. (This can vary depending |
| 335 // on the number of "Most Visited" items. | 387 // on the number of "Most Visited" items. |
| 336 int index = GetIndexOfMenuItemWithTag( | 388 int index = GetIndexOfMenuItemWithTag( |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 508 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 457 if (item->session_id && service) { | 509 if (item->session_id && service) { |
| 458 service->RestoreEntryById(browser_->tab_restore_service_delegate(), | 510 service->RestoreEntryById(browser_->tab_restore_service_delegate(), |
| 459 item->session_id, false); | 511 item->session_id, false); |
| 460 } else { | 512 } else { |
| 461 DCHECK(item->url.is_valid()); | 513 DCHECK(item->url.is_valid()); |
| 462 browser_->OpenURL(item->url, GURL(), disposition, | 514 browser_->OpenURL(item->url, GURL(), disposition, |
| 463 PageTransition::AUTO_BOOKMARK); | 515 PageTransition::AUTO_BOOKMARK); |
| 464 } | 516 } |
| 465 } | 517 } |
| OLD | NEW |