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/string_number_conversions.h" | |
| 10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 11 #include "base/string_number_conversions.h" | |
| 12 #include "chrome/browser/favicon/favicon_service.h" | 12 #include "chrome/browser/favicon/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" |
| 23 #include "content/common/notification_service.h" | 24 #include "content/common/notification_service.h" |
| 24 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/text/text_elider.h" | 27 #include "ui/base/text/text_elider.h" |
| 27 #include "ui/gfx/codec/png_codec.h" | 28 #include "ui/gfx/codec/png_codec.h" |
| 28 #include "ui/gfx/gtk_util.h" | 29 #include "ui/gfx/gtk_util.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 33 // The maximum number of most visited items to display. | |
| 34 const unsigned int kMostVisitedCount = 12; | |
| 35 | |
| 32 // The number of recently closed items to get. | 36 // The number of recently closed items to get. |
| 33 const unsigned int kRecentlyClosedCount = 10; | 37 const unsigned int kRecentlyClosedCount = 10; |
| 34 | 38 |
| 35 // Menus more than this many chars long will get trimmed. | 39 // Menus more than this many chars long will get trimmed. |
| 36 const int kMaximumMenuWidthInChars = 50; | 40 const int kMaximumMenuWidthInChars = 50; |
| 37 | 41 |
| 38 } // namespace | 42 } // namespace |
| 39 | 43 |
| 40 struct GlobalHistoryMenu::ClearMenuClosure { | 44 struct GlobalHistoryMenu::ClearMenuClosure { |
| 41 GtkWidget* container; | 45 GtkWidget* container; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 // the vector will be empty. | 92 // the vector will be empty. |
| 89 std::vector<HistoryItem*> tabs; | 93 std::vector<HistoryItem*> tabs; |
| 90 | 94 |
| 91 private: | 95 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(HistoryItem); | 96 DISALLOW_COPY_AND_ASSIGN(HistoryItem); |
| 93 }; | 97 }; |
| 94 | 98 |
| 95 GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser) | 99 GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser) |
| 96 : browser_(browser), | 100 : browser_(browser), |
| 97 profile_(browser_->profile()), | 101 profile_(browser_->profile()), |
| 102 top_sites_(NULL), | |
| 98 default_favicon_(NULL), | 103 default_favicon_(NULL), |
| 99 tab_restore_service_(NULL) { | 104 tab_restore_service_(NULL) { |
| 100 } | 105 } |
| 101 | 106 |
| 102 GlobalHistoryMenu::~GlobalHistoryMenu() { | 107 GlobalHistoryMenu::~GlobalHistoryMenu() { |
| 103 if (tab_restore_service_) | 108 if (tab_restore_service_) |
| 104 tab_restore_service_->RemoveObserver(this); | 109 tab_restore_service_->RemoveObserver(this); |
| 105 | 110 |
| 106 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), | 111 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), |
| 107 menu_item_history_map_.end()); | 112 menu_item_history_map_.end()); |
| 108 menu_item_history_map_.clear(); | 113 menu_item_history_map_.clear(); |
| 109 } | 114 } |
| 110 | 115 |
| 111 void GlobalHistoryMenu::Init(GtkWidget* history_menu) { | 116 void GlobalHistoryMenu::Init(GtkWidget* history_menu) { |
| 112 history_menu_ = history_menu; | 117 history_menu_ = history_menu; |
| 113 | 118 |
| 114 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 119 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
| 115 | 120 |
| 116 if (profile_) { | 121 if (profile_) { |
| 122 top_sites_ = profile_->GetTopSites(); | |
| 123 if (top_sites_) { | |
| 124 GetTopSitesData(); | |
| 125 | |
| 126 // Register for notification when TopSites changes so that we can update | |
| 127 // ourself. | |
| 128 registrar_.Add(this, NotificationType::TOP_SITES_CHANGED, | |
| 129 Source<history::TopSites>(top_sites_)); | |
| 130 } | |
| 131 | |
| 117 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); | 132 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); |
| 118 if (tab_restore_service_) { | 133 if (tab_restore_service_) { |
| 119 tab_restore_service_->LoadTabsFromLastSession(); | 134 tab_restore_service_->LoadTabsFromLastSession(); |
| 120 tab_restore_service_->AddObserver(this); | 135 tab_restore_service_->AddObserver(this); |
| 121 | 136 |
| 122 // If LoadTabsFromLastSession doesn't load tabs, it won't call | 137 // If LoadTabsFromLastSession doesn't load tabs, it won't call |
| 123 // TabRestoreServiceChanged(). This ensures that all new windows after | 138 // TabRestoreServiceChanged(). This ensures that all new windows after |
| 124 // the first one will have their menus populated correctly. | 139 // the first one will have their menus populated correctly. |
| 125 TabRestoreServiceChanged(tab_restore_service_); | 140 TabRestoreServiceChanged(tab_restore_service_); |
| 126 } | 141 } |
| 127 | 142 |
| 128 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 143 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
| 129 Source<Profile>(profile_)); | 144 Source<Profile>(profile_)); |
| 130 } | 145 } |
| 131 } | 146 } |
| 132 | 147 |
| 148 void GlobalHistoryMenu::GetTopSitesData() { | |
| 149 DCHECK(top_sites_); | |
| 150 | |
| 151 top_sites_->GetMostVisitedURLs( | |
| 152 &top_sites_consumer_, | |
| 153 NewCallback(this, &GlobalHistoryMenu::OnTopSitesReceived)); | |
| 154 } | |
| 155 | |
| 156 void GlobalHistoryMenu::OnTopSitesReceived( | |
| 157 const history::MostVisitedURLList& visited_list) { | |
| 158 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_MOST_VISITED); | |
| 159 | |
| 160 int index = GetIndexOfMenuItemWithTag( | |
| 161 history_menu_, | |
| 162 GlobalMenuBar::TAG_MOST_VISITED_HEADER) + 1; | |
| 163 | |
| 164 unsigned int added_count = 0; | |
| 165 for (size_t i = 0; | |
| 166 i < visited_list.size() && added_count < kMostVisitedCount; ++i) { | |
| 167 const history::MostVisitedURL& visited = visited_list[i]; | |
| 168 if (visited.url.spec().empty()) | |
| 169 break; // This is the signal that there are no more real visited sites. | |
| 170 | |
| 171 HistoryItem* item = new HistoryItem(); | |
| 172 item->title = visited.title; | |
| 173 item->url = visited.url; | |
| 174 | |
| 175 // The TopSites system doesn't give us icons; it gives us chrome:// urls to | |
| 176 // icons so fetch the icons normally. | |
| 177 GetFaviconForHistoryItem(item); | |
| 178 | |
| 179 AddHistoryItemToMenu(item, | |
| 180 history_menu_, | |
| 181 GlobalMenuBar::TAG_MOST_VISITED, | |
| 182 index++); | |
| 183 added_count++; | |
|
Evan Stade
2011/04/28 23:19:35
isn't added_count the same as i?
Elliot Glaysher
2011/04/28 23:25:13
The thought process was: ooh, I do this later on i
| |
| 184 } | |
| 185 } | |
| 186 | |
| 133 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( | 187 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( |
| 134 GtkWidget* menu_item) { | 188 GtkWidget* menu_item) { |
| 135 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); | 189 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); |
| 136 return it != menu_item_history_map_.end() ? it->second : NULL; | 190 return it != menu_item_history_map_.end() ? it->second : NULL; |
| 137 } | 191 } |
| 138 | 192 |
| 139 bool GlobalHistoryMenu::HasValidHistoryItemForTab( | 193 bool GlobalHistoryMenu::HasValidHistoryItemForTab( |
| 140 const TabRestoreService::Tab& entry) { | 194 const TabRestoreService::Tab& entry) { |
| 141 if (entry.navigations.empty()) | 195 if (entry.navigations.empty()) |
| 142 return false; | 196 return false; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 if (submenu) | 365 if (submenu) |
| 312 closure->menu_bar->ClearMenuSection(submenu, closure->tag); | 366 closure->menu_bar->ClearMenuSection(submenu, closure->tag); |
| 313 | 367 |
| 314 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item); | 368 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item); |
| 315 } | 369 } |
| 316 } | 370 } |
| 317 | 371 |
| 318 void GlobalHistoryMenu::Observe(NotificationType type, | 372 void GlobalHistoryMenu::Observe(NotificationType type, |
| 319 const NotificationSource& source, | 373 const NotificationSource& source, |
| 320 const NotificationDetails& details) { | 374 const NotificationDetails& details) { |
| 321 DCHECK(type.value == NotificationType::BROWSER_THEME_CHANGED); | 375 if (type.value == NotificationType::BROWSER_THEME_CHANGED) { |
| 322 | 376 // 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 | 377 // 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 | 378 // we'll update on the next menu change event. |
| 325 // we'll update on the next menu change event. | 379 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
| 326 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 380 } else if (type.value == NotificationType::TOP_SITES_CHANGED) { |
| 381 GetTopSitesData(); | |
| 382 } else { | |
| 383 NOTREACHED(); | |
| 384 } | |
| 327 } | 385 } |
| 328 | 386 |
| 329 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { | 387 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { |
| 330 const TabRestoreService::Entries& entries = service->entries(); | 388 const TabRestoreService::Entries& entries = service->entries(); |
| 331 | 389 |
| 332 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); | 390 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); |
| 333 | 391 |
| 334 // We'll get the index the "Recently Closed" header. (This can vary depending | 392 // We'll get the index the "Recently Closed" header. (This can vary depending |
| 335 // on the number of "Most Visited" items. | 393 // on the number of "Most Visited" items. |
| 336 int index = GetIndexOfMenuItemWithTag( | 394 int index = GetIndexOfMenuItemWithTag( |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 514 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
| 457 if (item->session_id && service) { | 515 if (item->session_id && service) { |
| 458 service->RestoreEntryById(browser_->tab_restore_service_delegate(), | 516 service->RestoreEntryById(browser_->tab_restore_service_delegate(), |
| 459 item->session_id, false); | 517 item->session_id, false); |
| 460 } else { | 518 } else { |
| 461 DCHECK(item->url.is_valid()); | 519 DCHECK(item->url.is_valid()); |
| 462 browser_->OpenURL(item->url, GURL(), disposition, | 520 browser_->OpenURL(item->url, GURL(), disposition, |
| 463 PageTransition::AUTO_BOOKMARK); | 521 PageTransition::AUTO_BOOKMARK); |
| 464 } | 522 } |
| 465 } | 523 } |
| OLD | NEW |