1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/gtk/global_history_menu.h"
#include <gtk/gtk.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/stl_util.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.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"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
#include "chrome/browser/ui/gtk/global_menu_bar.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/notification_source.h"
#include "grit/generated_resources.h"
#include "ui/base/gtk/owned_widget_gtk.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/text/text_elider.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/gtk_util.h"
namespace {
// The maximum number of most visited items to display.
const unsigned int kMostVisitedCount = 8;
// The number of recently closed items to get.
const unsigned int kRecentlyClosedCount = 8;
// Menus more than this many chars long will get trimmed.
const int kMaximumMenuWidthInChars = 50;
} // namespace
struct GlobalHistoryMenu::ClearMenuClosure {
GtkWidget* container;
GlobalHistoryMenu* menu_bar;
int tag;
};
struct GlobalHistoryMenu::GetIndexClosure {
bool found;
int current_index;
int tag;
};
class GlobalHistoryMenu::HistoryItem {
public:
HistoryItem()
: menu_item(NULL),
session_id(0) {}
// The title for the menu item.
string16 title;
// The URL that will be navigated to if the user selects this item.
GURL url;
// A pointer to the menu_item. This is a weak reference in the GTK+ version
// because the GtkMenu must sink the reference.
GtkWidget* menu_item;
// This ID is unique for a browser session and can be passed to the
// TabRestoreService to re-open the closed window or tab that this
// references. A non-0 session ID indicates that this is an entry can be
// restored that way. Otherwise, the URL will be used to open the item and
// this ID will be 0.
SessionID::id_type session_id;
// If the HistoryItem is a window, this will be the vector of tabs. Note
// that this is a list of weak references. The |menu_item_map_| is the owner
// of all items. If it is not a window, then the entry is a single page and
// the vector will be empty.
std::vector<HistoryItem*> tabs;
private:
DISALLOW_COPY_AND_ASSIGN(HistoryItem);
};
GlobalHistoryMenu::GlobalHistoryMenu(Browser* browser)
: browser_(browser),
profile_(browser_->profile()),
top_sites_(NULL),
tab_restore_service_(NULL) {
}
GlobalHistoryMenu::~GlobalHistoryMenu() {
if (tab_restore_service_)
tab_restore_service_->RemoveObserver(this);
STLDeleteContainerPairSecondPointers(menu_item_history_map_.begin(),
menu_item_history_map_.end());
menu_item_history_map_.clear();
}
void GlobalHistoryMenu::Init(GtkWidget* history_menu,
GtkWidget* history_menu_item) {
history_menu_.Own(history_menu);
// We have to connect to |history_menu_item|'s "activate" signal instead of
// |history_menu|'s "show" signal because we are not supposed to modify the
// menu during "show"
g_signal_connect(history_menu_item, "activate",
G_CALLBACK(OnMenuActivateThunk), this);
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, chrome::NOTIFICATION_TOP_SITES_CHANGED,
content::Source<history::TopSites>(top_sites_));
}
}
}
void GlobalHistoryMenu::GetTopSitesData() {
DCHECK(top_sites_);
top_sites_->GetMostVisitedURLs(
&top_sites_consumer_,
base::Bind(&GlobalHistoryMenu::OnTopSitesReceived,
base::Unretained(this)));
}
void GlobalHistoryMenu::OnTopSitesReceived(
const history::MostVisitedURLList& visited_list) {
ClearMenuSection(history_menu_.get(), GlobalMenuBar::TAG_MOST_VISITED);
int index = GetIndexOfMenuItemWithTag(
history_menu_.get(),
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;
AddHistoryItemToMenu(item,
history_menu_.get(),
GlobalMenuBar::TAG_MOST_VISITED,
index++);
}
}
GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForMenuItem(
GtkWidget* menu_item) {
MenuItemToHistoryMap::iterator it = menu_item_history_map_.find(menu_item);
return it != menu_item_history_map_.end() ? it->second : NULL;
}
bool GlobalHistoryMenu::HasValidHistoryItemForTab(
const TabRestoreService::Tab& entry) {
if (entry.navigations.empty())
return false;
const TabNavigation& current_navigation =
entry.navigations.at(entry.current_navigation_index);
if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL))
return false;
return true;
}
GlobalHistoryMenu::HistoryItem* GlobalHistoryMenu::HistoryItemForTab(
const TabRestoreService::Tab& entry) {
if (!HasValidHistoryItemForTab(entry))
return NULL;
const TabNavigation& current_navigation =
entry.navigations.at(entry.current_navigation_index);
HistoryItem* item = new HistoryItem();
item->title = current_navigation.title();
item->url = current_navigation.virtual_url();
item->session_id = entry.id;
return item;
}
GtkWidget* GlobalHistoryMenu::AddHistoryItemToMenu(HistoryItem* item,
GtkWidget* menu,
int tag,
int index) {
string16 title = item->title;
std::string url_string = item->url.possibly_invalid_spec();
if (title.empty())
title = UTF8ToUTF16(url_string);
ui::ElideString(title, kMaximumMenuWidthInChars, &title);
GtkWidget* menu_item = gtk_menu_item_new_with_label(
UTF16ToUTF8(title).c_str());
item->menu_item = menu_item;
gtk_widget_show(menu_item);
g_object_set_data(G_OBJECT(menu_item), "type-tag", GINT_TO_POINTER(tag));
g_signal_connect(menu_item, "activate",
G_CALLBACK(OnRecentlyClosedItemActivatedThunk), this);
std::string tooltip = gtk_util::BuildTooltipTitleFor(item->title, item->url);
gtk_widget_set_tooltip_markup(menu_item, tooltip.c_str());
menu_item_history_map_.insert(std::make_pair(menu_item, item));
gtk_menu_shell_insert(GTK_MENU_SHELL(menu), menu_item, index);
return menu_item;
}
int GlobalHistoryMenu::GetIndexOfMenuItemWithTag(GtkWidget* menu, int tag_id) {
GetIndexClosure closure;
closure.found = false;
closure.current_index = 0;
closure.tag = tag_id;
gtk_container_foreach(
GTK_CONTAINER(menu),
reinterpret_cast<void (*)(GtkWidget*, void*)>(GetIndexCallback),
&closure);
return closure.current_index;
}
void GlobalHistoryMenu::ClearMenuSection(GtkWidget* menu, int tag) {
ClearMenuClosure closure;
closure.container = menu;
closure.menu_bar = this;
closure.tag = tag;
gtk_container_foreach(
GTK_CONTAINER(menu),
reinterpret_cast<void (*)(GtkWidget*, void*)>(ClearMenuCallback),
&closure);
}
// static
void GlobalHistoryMenu::GetIndexCallback(GtkWidget* menu_item,
GetIndexClosure* closure) {
int tag = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item), "type-tag"));
if (tag == closure->tag)
closure->found = true;
if (!closure->found)
closure->current_index++;
}
// static
void GlobalHistoryMenu::ClearMenuCallback(GtkWidget* menu_item,
ClearMenuClosure* closure) {
DCHECK_NE(closure->tag, 0);
int tag = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item), "type-tag"));
if (closure->tag == tag) {
HistoryItem* item = closure->menu_bar->HistoryItemForMenuItem(menu_item);
if (item) {
closure->menu_bar->menu_item_history_map_.erase(menu_item);
delete item;
}
GtkWidget* submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item));
if (submenu)
closure->menu_bar->ClearMenuSection(submenu, closure->tag);
gtk_container_remove(GTK_CONTAINER(closure->container), menu_item);
}
}
void GlobalHistoryMenu::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_TOP_SITES_CHANGED) {
GetTopSitesData();
} else {
NOTREACHED();
}
}
void GlobalHistoryMenu::TabRestoreServiceChanged(TabRestoreService* service) {
const TabRestoreService::Entries& entries = service->entries();
ClearMenuSection(history_menu_.get(), GlobalMenuBar::TAG_RECENTLY_CLOSED);
// We'll get the index the "Recently Closed" header. (This can vary depending
// on the number of "Most Visited" items.
int index = GetIndexOfMenuItemWithTag(
history_menu_.get(),
GlobalMenuBar::TAG_RECENTLY_CLOSED_HEADER) + 1;
unsigned int added_count = 0;
for (TabRestoreService::Entries::const_iterator it = entries.begin();
it != entries.end() && added_count < kRecentlyClosedCount; ++it) {
TabRestoreService::Entry* entry = *it;
if (entry->type == TabRestoreService::WINDOW) {
TabRestoreService::Window* entry_win =
static_cast<TabRestoreService::Window*>(entry);
std::vector<TabRestoreService::Tab>& tabs = entry_win->tabs;
if (tabs.empty())
continue;
// Check that this window has valid content. Sometimes it is possible for
// there to not be any subitems for a given window; if that is the case,
// do not add the entry to the main menu.
int valid_tab_count = 0;
std::vector<TabRestoreService::Tab>::const_iterator it;
for (it = tabs.begin(); it != tabs.end(); ++it) {
if (HasValidHistoryItemForTab(*it))
valid_tab_count++;
}
if (valid_tab_count == 0)
continue;
// Create the item for the parent/window. Do not set the title yet
// because the actual number of items that are in the menu will not be
// known until things like the NTP are filtered out, which is done when
// the tab items are actually created.
HistoryItem* item = new HistoryItem();
item->session_id = entry_win->id;
GtkWidget* submenu = gtk_menu_new();
GtkWidget* restore_item = gtk_menu_item_new_with_label(
l10n_util::GetStringUTF8(
IDS_HISTORY_CLOSED_RESTORE_WINDOW_LINUX).c_str());
g_object_set_data(G_OBJECT(restore_item), "type-tag",
GINT_TO_POINTER(GlobalMenuBar::TAG_RECENTLY_CLOSED));
g_signal_connect(restore_item, "activate",
G_CALLBACK(OnRecentlyClosedItemActivatedThunk), this);
gtk_widget_show(restore_item);
// The mac version of this code allows the user to click on the parent
// menu item to have the same effect as clicking the restore window
// submenu item. GTK+ helpfully activates a menu item when it shows a
// submenu so toss that feature out.
menu_item_history_map_.insert(std::make_pair(restore_item, item));
gtk_menu_shell_append(GTK_MENU_SHELL(submenu), restore_item);
GtkWidget* separator = gtk_separator_menu_item_new();
gtk_widget_show(separator);
gtk_menu_shell_append(GTK_MENU_SHELL(submenu), separator);
// Loop over the window's tabs and add them to the submenu.
int subindex = 2;
for (it = tabs.begin(); it != tabs.end(); ++it) {
TabRestoreService::Tab tab = *it;
HistoryItem* tab_item = HistoryItemForTab(tab);
if (tab_item) {
item->tabs.push_back(tab_item);
AddHistoryItemToMenu(tab_item,
submenu,
GlobalMenuBar::TAG_RECENTLY_CLOSED,
subindex++);
}
}
// Now that the number of tabs that has been added is known, set the
// title of the parent menu item.
std::string title =
(item->tabs.size() == 1) ?
l10n_util::GetStringUTF8(
IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE) :
l10n_util::GetStringFUTF8(
IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE,
base::IntToString16(item->tabs.size()));
// Create the menu item parent. Unlike mac, it's can't be activated.
GtkWidget* parent_item = gtk_menu_item_new_with_label(
title.c_str());
gtk_widget_show(parent_item);
g_object_set_data(G_OBJECT(parent_item), "type-tag",
GINT_TO_POINTER(GlobalMenuBar::TAG_RECENTLY_CLOSED));
gtk_menu_item_set_submenu(GTK_MENU_ITEM(parent_item), submenu);
gtk_menu_shell_insert(GTK_MENU_SHELL(history_menu_.get()), parent_item,
index++);
++added_count;
} else if (entry->type == TabRestoreService::TAB) {
TabRestoreService::Tab* tab =
static_cast<TabRestoreService::Tab*>(entry);
HistoryItem* item = HistoryItemForTab(*tab);
if (item) {
AddHistoryItemToMenu(item,
history_menu_.get(),
GlobalMenuBar::TAG_RECENTLY_CLOSED,
index++);
++added_count;
}
}
}
}
void GlobalHistoryMenu::TabRestoreServiceDestroyed(
TabRestoreService* service) {
tab_restore_service_ = NULL;
}
void GlobalHistoryMenu::OnRecentlyClosedItemActivated(GtkWidget* sender) {
WindowOpenDisposition disposition =
gtk_util::DispositionForCurrentButtonPressEvent();
HistoryItem* item = HistoryItemForMenuItem(sender);
// If this item can be restored using TabRestoreService, do so. Otherwise,
// just load the URL.
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile());
if (item->session_id && service) {
service->RestoreEntryById(browser_->tab_restore_service_delegate(),
item->session_id, false);
} else {
DCHECK(item->url.is_valid());
browser_->OpenURL(OpenURLParams(item->url, GURL(), disposition,
content::PAGE_TRANSITION_AUTO_BOOKMARK, false));
}
}
void GlobalHistoryMenu::OnMenuActivate(GtkWidget* sender) {
if (!tab_restore_service_) {
tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_);
if (tab_restore_service_) {
tab_restore_service_->LoadTabsFromLastSession();
tab_restore_service_->AddObserver(this);
// If LoadTabsFromLastSession doesn't load tabs, it won't call
// TabRestoreServiceChanged(). This ensures that all new windows after
// the first one will have their menus populated correctly.
TabRestoreServiceChanged(tab_restore_service_);
}
}
}
|