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/string_number_conversions.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 GlobalHistoryMenu::~GlobalHistoryMenu() { | 107 GlobalHistoryMenu::~GlobalHistoryMenu() { |
108 if (tab_restore_service_) | 108 if (tab_restore_service_) |
109 tab_restore_service_->RemoveObserver(this); | 109 tab_restore_service_->RemoveObserver(this); |
110 | 110 |
111 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), | 111 STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(), |
112 menu_item_history_map_.end()); | 112 menu_item_history_map_.end()); |
113 menu_item_history_map_.clear(); | 113 menu_item_history_map_.clear(); |
114 } | 114 } |
115 | 115 |
116 void GlobalHistoryMenu::Init(GtkWidget* history_menu) { | 116 void GlobalHistoryMenu::Init(GtkWidget* history_menu, |
117 history_menu_ = history_menu; | 117 GtkWidget* history_menu_item) { |
| 118 history_menu_.Own(history_menu); |
| 119 |
| 120 // We have to connect to |history_menu_item|'s "activate" signal instead of |
| 121 // |history_menu|'s "show" signal because we are not supposed to modify the |
| 122 // menu during "show" |
| 123 g_signal_connect(history_menu_item, "activate", |
| 124 G_CALLBACK(OnMenuActivateThunk), this); |
118 | 125 |
119 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 126 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
120 | 127 |
121 if (profile_) { | 128 if (profile_) { |
122 top_sites_ = profile_->GetTopSites(); | 129 top_sites_ = profile_->GetTopSites(); |
123 if (top_sites_) { | 130 if (top_sites_) { |
124 GetTopSitesData(); | 131 GetTopSitesData(); |
125 | 132 |
126 // Register for notification when TopSites changes so that we can update | 133 // Register for notification when TopSites changes so that we can update |
127 // ourself. | 134 // ourself. |
128 registrar_.Add(this, NotificationType::TOP_SITES_CHANGED, | 135 registrar_.Add(this, NotificationType::TOP_SITES_CHANGED, |
129 Source<history::TopSites>(top_sites_)); | 136 Source<history::TopSites>(top_sites_)); |
130 } | 137 } |
131 | 138 |
132 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); | |
133 if (tab_restore_service_) { | |
134 tab_restore_service_->LoadTabsFromLastSession(); | |
135 tab_restore_service_->AddObserver(this); | |
136 | |
137 // If LoadTabsFromLastSession doesn't load tabs, it won't call | |
138 // TabRestoreServiceChanged(). This ensures that all new windows after | |
139 // the first one will have their menus populated correctly. | |
140 TabRestoreServiceChanged(tab_restore_service_); | |
141 } | |
142 | |
143 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 139 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
144 Source<Profile>(profile_)); | 140 Source<Profile>(profile_)); |
145 } | 141 } |
146 } | 142 } |
147 | 143 |
148 void GlobalHistoryMenu::GetTopSitesData() { | 144 void GlobalHistoryMenu::GetTopSitesData() { |
149 DCHECK(top_sites_); | 145 DCHECK(top_sites_); |
150 | 146 |
151 top_sites_->GetMostVisitedURLs( | 147 top_sites_->GetMostVisitedURLs( |
152 &top_sites_consumer_, | 148 &top_sites_consumer_, |
153 NewCallback(this, &GlobalHistoryMenu::OnTopSitesReceived)); | 149 NewCallback(this, &GlobalHistoryMenu::OnTopSitesReceived)); |
154 } | 150 } |
155 | 151 |
156 void GlobalHistoryMenu::OnTopSitesReceived( | 152 void GlobalHistoryMenu::OnTopSitesReceived( |
157 const history::MostVisitedURLList& visited_list) { | 153 const history::MostVisitedURLList& visited_list) { |
158 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_MOST_VISITED); | 154 ClearMenuSection(history_menu_.get(), GlobalMenuBar::TAG_MOST_VISITED); |
159 | 155 |
160 int index = GetIndexOfMenuItemWithTag( | 156 int index = GetIndexOfMenuItemWithTag( |
161 history_menu_, | 157 history_menu_.get(), |
162 GlobalMenuBar::TAG_MOST_VISITED_HEADER) + 1; | 158 GlobalMenuBar::TAG_MOST_VISITED_HEADER) + 1; |
163 | 159 |
164 for (size_t i = 0; i < visited_list.size() && i < kMostVisitedCount; ++i) { | 160 for (size_t i = 0; i < visited_list.size() && i < kMostVisitedCount; ++i) { |
165 const history::MostVisitedURL& visited = visited_list[i]; | 161 const history::MostVisitedURL& visited = visited_list[i]; |
166 if (visited.url.spec().empty()) | 162 if (visited.url.spec().empty()) |
167 break; // This is the signal that there are no more real visited sites. | 163 break; // This is the signal that there are no more real visited sites. |
168 | 164 |
169 HistoryItem* item = new HistoryItem(); | 165 HistoryItem* item = new HistoryItem(); |
170 item->title = visited.title; | 166 item->title = visited.title; |
171 item->url = visited.url; | 167 item->url = visited.url; |
172 | 168 |
173 // The TopSites system doesn't give us icons; it gives us chrome:// urls to | 169 // The TopSites system doesn't give us icons; it gives us chrome:// urls |
174 // icons so fetch the icons normally. | 170 // to icons so fetch the icons normally. |
175 GetFaviconForHistoryItem(item); | 171 GetFaviconForHistoryItem(item); |
176 | 172 |
177 AddHistoryItemToMenu(item, | 173 AddHistoryItemToMenu(item, |
178 history_menu_, | 174 history_menu_.get(), |
179 GlobalMenuBar::TAG_MOST_VISITED, | 175 GlobalMenuBar::TAG_MOST_VISITED, |
180 index++); | 176 index++); |
181 } | 177 } |
182 } | 178 } |
183 | 179 |
184 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( | 180 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( |
185 GtkWidget* menu_item) { | 181 GtkWidget* menu_item) { |
186 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); | 182 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); |
187 return it != menu_item_history_map_.end() ? it->second : NULL; | 183 return it != menu_item_history_map_.end() ? it->second : NULL; |
188 } | 184 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 364 |
369 void GlobalHistoryMenu::Observe(NotificationType type, | 365 void GlobalHistoryMenu::Observe(NotificationType type, |
370 const NotificationSource& source, | 366 const NotificationSource& source, |
371 const NotificationDetails& details) { | 367 const NotificationDetails& details) { |
372 if (type.value == NotificationType::BROWSER_THEME_CHANGED) { | 368 if (type.value == NotificationType::BROWSER_THEME_CHANGED) { |
373 // Keeping track of which menu items have the default icon is going an | 369 // Keeping track of which menu items have the default icon is going an |
374 // error-prone pain, so instead just store the new default favicon and | 370 // error-prone pain, so instead just store the new default favicon and |
375 // we'll update on the next menu change event. | 371 // we'll update on the next menu change event. |
376 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); | 372 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); |
377 } else if (type.value == NotificationType::TOP_SITES_CHANGED) { | 373 } else if (type.value == NotificationType::TOP_SITES_CHANGED) { |
378 GetTopSitesData(); | 374 if (Source<history::TopSites>(source).ptr() == top_sites_) |
| 375 GetTopSitesData(); |
379 } else { | 376 } else { |
380 NOTREACHED(); | 377 NOTREACHED(); |
381 } | 378 } |
382 } | 379 } |
383 | 380 |
384 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { | 381 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { |
385 const TabRestoreService::Entries& entries = service->entries(); | 382 const TabRestoreService::Entries& entries = service->entries(); |
386 | 383 |
387 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); | 384 ClearMenuSection(history_menu_.get(), GlobalMenuBar::TAG_RECENTLY_CLOSED); |
388 | 385 |
389 // 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 |
390 // on the number of "Most Visited" items. | 387 // on the number of "Most Visited" items. |
391 int index = GetIndexOfMenuItemWithTag( | 388 int index = GetIndexOfMenuItemWithTag( |
392 history_menu_, | 389 history_menu_.get(), |
393 GlobalMenuBar::TAG_RECENTLY_CLOSED_HEADER) + 1; | 390 GlobalMenuBar::TAG_RECENTLY_CLOSED_HEADER) + 1; |
394 | 391 |
395 unsigned int added_count = 0; | 392 unsigned int added_count = 0; |
396 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 393 for (TabRestoreService::Entries::const_iterator it = entries.begin(); |
397 it != entries.end() && added_count < kRecentlyClosedCount; ++it) { | 394 it != entries.end() && added_count < kRecentlyClosedCount; ++it) { |
398 TabRestoreService::Entry* entry = *it; | 395 TabRestoreService::Entry* entry = *it; |
399 | 396 |
400 if (entry->type == TabRestoreService::WINDOW) { | 397 if (entry->type == TabRestoreService::WINDOW) { |
401 TabRestoreService::Window* entry_win = | 398 TabRestoreService::Window* entry_win = |
402 static_cast<TabRestoreService::Window*>(entry); | 399 static_cast<TabRestoreService::Window*>(entry); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 base::IntToString16(item->tabs.size())); | 467 base::IntToString16(item->tabs.size())); |
471 | 468 |
472 // Create the menu item parent. Unlike mac, it's can't be activated. | 469 // Create the menu item parent. Unlike mac, it's can't be activated. |
473 GtkWidget* parent_item = gtk_image_menu_item_new_with_label( | 470 GtkWidget* parent_item = gtk_image_menu_item_new_with_label( |
474 title.c_str()); | 471 title.c_str()); |
475 gtk_widget_show(parent_item); | 472 gtk_widget_show(parent_item); |
476 g_object_set_data(G_OBJECT(parent_item), "type-tag", | 473 g_object_set_data(G_OBJECT(parent_item), "type-tag", |
477 GINT_TO_POINTER(GlobalMenuBar::TAG_RECENTLY_CLOSED)); | 474 GINT_TO_POINTER(GlobalMenuBar::TAG_RECENTLY_CLOSED)); |
478 gtk_menu_item_set_submenu(GTK_MENU_ITEM(parent_item), submenu); | 475 gtk_menu_item_set_submenu(GTK_MENU_ITEM(parent_item), submenu); |
479 | 476 |
480 gtk_menu_shell_insert(GTK_MENU_SHELL(history_menu_), parent_item, | 477 gtk_menu_shell_insert(GTK_MENU_SHELL(history_menu_.get()), parent_item, |
481 index++); | 478 index++); |
482 ++added_count; | 479 ++added_count; |
483 } else if (entry->type == TabRestoreService::TAB) { | 480 } else if (entry->type == TabRestoreService::TAB) { |
484 TabRestoreService::Tab* tab = | 481 TabRestoreService::Tab* tab = |
485 static_cast<TabRestoreService::Tab*>(entry); | 482 static_cast<TabRestoreService::Tab*>(entry); |
486 HistoryItem* item = HistoryItemForTab(*tab); | 483 HistoryItem* item = HistoryItemForTab(*tab); |
487 if (item) { | 484 if (item) { |
488 AddHistoryItemToMenu(item, | 485 AddHistoryItemToMenu(item, |
489 history_menu_, | 486 history_menu_.get(), |
490 GlobalMenuBar::TAG_RECENTLY_CLOSED, | 487 GlobalMenuBar::TAG_RECENTLY_CLOSED, |
491 index++); | 488 index++); |
492 ++added_count; | 489 ++added_count; |
493 } | 490 } |
494 } | 491 } |
495 } | 492 } |
496 } | 493 } |
497 | 494 |
498 void GlobalHistoryMenu::TabRestoreServiceDestroyed( | 495 void GlobalHistoryMenu::TabRestoreServiceDestroyed( |
499 TabRestoreService* service) { | 496 TabRestoreService* service) { |
(...skipping 11 matching lines...) Expand all Loading... |
511 TabRestoreServiceFactory::GetForProfile(browser_->profile()); | 508 TabRestoreServiceFactory::GetForProfile(browser_->profile()); |
512 if (item->session_id && service) { | 509 if (item->session_id && service) { |
513 service->RestoreEntryById(browser_->tab_restore_service_delegate(), | 510 service->RestoreEntryById(browser_->tab_restore_service_delegate(), |
514 item->session_id, false); | 511 item->session_id, false); |
515 } else { | 512 } else { |
516 DCHECK(item->url.is_valid()); | 513 DCHECK(item->url.is_valid()); |
517 browser_->OpenURL(item->url, GURL(), disposition, | 514 browser_->OpenURL(item->url, GURL(), disposition, |
518 PageTransition::AUTO_BOOKMARK); | 515 PageTransition::AUTO_BOOKMARK); |
519 } | 516 } |
520 } | 517 } |
| 518 |
| 519 void GlobalHistoryMenu::OnMenuActivate(GtkWidget* sender) { |
| 520 if (!tab_restore_service_) { |
| 521 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); |
| 522 if (tab_restore_service_) { |
| 523 tab_restore_service_->LoadTabsFromLastSession(); |
| 524 tab_restore_service_->AddObserver(this); |
| 525 |
| 526 // If LoadTabsFromLastSession doesn't load tabs, it won't call |
| 527 // TabRestoreServiceChanged(). This ensures that all new windows after |
| 528 // the first one will have their menus populated correctly. |
| 529 TabRestoreServiceChanged(tab_restore_service_); |
| 530 } |
| 531 } |
| 532 } |
OLD | NEW |