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

Side by Side Diff: chrome/browser/ui/gtk/global_history_menu.cc

Issue 6901110: GTK: Query TopSites for the Most Visited pages and populate fill in the global History menu. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ooops. Created 9 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/gtk/global_history_menu.h ('k') | chrome/browser/ui/gtk/global_menu_bar.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 for (size_t i = 0; i < visited_list.size() && i < kMostVisitedCount; ++i) {
165 const history::MostVisitedURL& visited = visited_list[i];
166 if (visited.url.spec().empty())
167 break; // This is the signal that there are no more real visited sites.
168
169 HistoryItem* item = new HistoryItem();
170 item->title = visited.title;
171 item->url = visited.url;
172
173 // The TopSites system doesn't give us icons; it gives us chrome:// urls to
174 // icons so fetch the icons normally.
175 GetFaviconForHistoryItem(item);
176
177 AddHistoryItemToMenu(item,
178 history_menu_,
179 GlobalMenuBar::TAG_MOST_VISITED,
180 index++);
181 }
182 }
183
133 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem( 184 GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem(
134 GtkWidget* menu_item) { 185 GtkWidget* menu_item) {
135 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item); 186 MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item);
136 return it != menu_item_history_map_.end() ? it->second : NULL; 187 return it != menu_item_history_map_.end() ? it->second : NULL;
137 } 188 }
138 189
139 bool GlobalHistoryMenu::HasValidHistoryItemForTab( 190 bool GlobalHistoryMenu::HasValidHistoryItemForTab(
140 const TabRestoreService::Tab& entry) { 191 const TabRestoreService::Tab& entry) {
141 if (entry.navigations.empty()) 192 if (entry.navigations.empty())
142 return false; 193 return false;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 if (submenu) 362 if (submenu)
312 closure->menu_bar->ClearMenuSection(submenu, closure->tag); 363 closure->menu_bar->ClearMenuSection(submenu, closure->tag);
313 364
314 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item); 365 gtk_container_remove(GTK_CONTAINER(closure->container), menu_item);
315 } 366 }
316 } 367 }
317 368
318 void GlobalHistoryMenu::Observe(NotificationType type, 369 void GlobalHistoryMenu::Observe(NotificationType type,
319 const NotificationSource& source, 370 const NotificationSource& source,
320 const NotificationDetails& details) { 371 const NotificationDetails& details) {
321 DCHECK(type.value == NotificationType::BROWSER_THEME_CHANGED); 372 if (type.value == NotificationType::BROWSER_THEME_CHANGED) {
322 373 // 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 374 // 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 375 // we'll update on the next menu change event.
325 // we'll update on the next menu change event. 376 default_favicon_ = GtkThemeService::GetDefaultFavicon(true);
326 default_favicon_ = GtkThemeService::GetDefaultFavicon(true); 377 } else if (type.value == NotificationType::TOP_SITES_CHANGED) {
378 GetTopSitesData();
379 } else {
380 NOTREACHED();
381 }
327 } 382 }
328 383
329 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) { 384 void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) {
330 const TabRestoreService::Entries& entries = service->entries(); 385 const TabRestoreService::Entries& entries = service->entries();
331 386
332 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED); 387 ClearMenuSection(history_menu_, GlobalMenuBar::TAG_RECENTLY_CLOSED);
333 388
334 // We'll get the index the "Recently Closed" header. (This can vary depending 389 // We'll get the index the "Recently Closed" header. (This can vary depending
335 // on the number of "Most Visited" items. 390 // on the number of "Most Visited" items.
336 int index = GetIndexOfMenuItemWithTag( 391 int index = GetIndexOfMenuItemWithTag(
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 TabRestoreServiceFactory::GetForProfile(browser_->profile()); 511 TabRestoreServiceFactory::GetForProfile(browser_->profile());
457 if (item->session_id && service) { 512 if (item->session_id && service) {
458 service->RestoreEntryById(browser_->tab_restore_service_delegate(), 513 service->RestoreEntryById(browser_->tab_restore_service_delegate(),
459 item->session_id, false); 514 item->session_id, false);
460 } else { 515 } else {
461 DCHECK(item->url.is_valid()); 516 DCHECK(item->url.is_valid());
462 browser_->OpenURL(item->url, GURL(), disposition, 517 browser_->OpenURL(item->url, GURL(), disposition,
463 PageTransition::AUTO_BOOKMARK); 518 PageTransition::AUTO_BOOKMARK);
464 } 519 }
465 } 520 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/gtk/global_history_menu.h ('k') | chrome/browser/ui/gtk/global_menu_bar.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698