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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
// Copyright (c) 2012 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/views/bookmarks/bookmark_menu_delegate.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_drag_drop_views.h"
#include "chrome/browser/ui/views/event_utils.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/user_metrics.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/window_open_disposition.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/widget/widget.h"
using base::UserMetricsAction;
using bookmarks::BookmarkNodeData;
using content::PageNavigator;
using views::MenuItemView;
// Max width of a menu. There does not appear to be an OS value for this, yet
// both IE and FF restrict the max width of a menu.
static const int kMaxMenuWidth = 400;
BookmarkMenuDelegate::BookmarkMenuDelegate(Browser* browser,
PageNavigator* navigator,
views::Widget* parent,
int first_menu_id,
int max_menu_id)
: browser_(browser),
profile_(browser->profile()),
page_navigator_(navigator),
parent_(parent),
menu_(NULL),
parent_menu_item_(NULL),
next_menu_id_(first_menu_id),
min_menu_id_(first_menu_id),
max_menu_id_(max_menu_id),
real_delegate_(NULL),
is_mutating_model_(false),
location_(BOOKMARK_LAUNCH_LOCATION_NONE) {}
BookmarkMenuDelegate::~BookmarkMenuDelegate() {
GetBookmarkModel()->RemoveObserver(this);
}
void BookmarkMenuDelegate::Init(views::MenuDelegate* real_delegate,
MenuItemView* parent,
const BookmarkNode* node,
int start_child_index,
ShowOptions show_options,
BookmarkLaunchLocation location) {
GetBookmarkModel()->AddObserver(this);
real_delegate_ = real_delegate;
location_ = location;
if (parent) {
parent_menu_item_ = parent;
// Add a separator if there are existing items in the menu, and if the
// current node has children. If |node| is the bookmark bar then the
// managed node is shown as its first child, if it's not empty.
BookmarkModel* model = GetBookmarkModel();
ChromeBookmarkClient* client = GetChromeBookmarkClient();
bool show_managed = show_options == SHOW_PERMANENT_FOLDERS &&
node == model->bookmark_bar_node() &&
!client->managed_node()->empty();
bool has_children =
(start_child_index < node->child_count()) || show_managed;
int initial_count = parent->GetSubmenu() ?
parent->GetSubmenu()->GetMenuItemCount() : 0;
if (has_children && initial_count > 0)
parent->AppendSeparator();
if (show_managed)
BuildMenuForManagedNode(parent, &next_menu_id_);
BuildMenu(node, start_child_index, parent, &next_menu_id_);
if (show_options == SHOW_PERMANENT_FOLDERS)
BuildMenusForPermanentNodes(parent, &next_menu_id_);
} else {
menu_ = CreateMenu(node, start_child_index, show_options);
}
}
void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) {
page_navigator_ = navigator;
if (context_menu_.get())
context_menu_->SetPageNavigator(navigator);
}
BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() {
return BookmarkModelFactory::GetForProfile(profile_);
}
ChromeBookmarkClient* BookmarkMenuDelegate::GetChromeBookmarkClient() {
return ChromeBookmarkClientFactory::GetForProfile(profile_);
}
void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node,
int start_index) {
DCHECK(!parent_menu_item_);
if (!node_to_menu_map_[node])
CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS);
menu_ = node_to_menu_map_[node];
}
base::string16 BookmarkMenuDelegate::GetTooltipText(
int id,
const gfx::Point& screen_loc) const {
MenuIDToNodeMap::const_iterator i = menu_id_to_node_map_.find(id);
// When removing bookmarks it may be possible to end up here without a node.
if (i == menu_id_to_node_map_.end()) {
DCHECK(is_mutating_model_);
return base::string16();
}
const BookmarkNode* node = i->second;
if (node->is_url()) {
return BookmarkBarView::CreateToolTipForURLAndTitle(
parent_, screen_loc, node->url(), node->GetTitle(), profile_);
}
return base::string16();
}
bool BookmarkMenuDelegate::IsTriggerableEvent(views::MenuItemView* menu,
const ui::Event& e) {
return e.type() == ui::ET_GESTURE_TAP ||
e.type() == ui::ET_GESTURE_TAP_DOWN ||
event_utils::IsPossibleDispositionEvent(e);
}
void BookmarkMenuDelegate::ExecuteCommand(int id, int mouse_event_flags) {
DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end());
const BookmarkNode* node = menu_id_to_node_map_[id];
std::vector<const BookmarkNode*> selection;
selection.push_back(node);
chrome::OpenAll(parent_->GetNativeWindow(), page_navigator_, selection,
ui::DispositionFromEventFlags(mouse_event_flags),
profile_);
RecordBookmarkLaunch(node, location_);
}
bool BookmarkMenuDelegate::ShouldExecuteCommandWithoutClosingMenu(
int id,
const ui::Event& event) {
return (event.flags() & ui::EF_LEFT_MOUSE_BUTTON) &&
ui::DispositionFromEventFlags(event.flags()) == NEW_BACKGROUND_TAB;
}
bool BookmarkMenuDelegate::GetDropFormats(
MenuItemView* menu,
int* formats,
std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
*formats = ui::OSExchangeData::URL;
custom_formats->insert(BookmarkNodeData::GetBookmarkCustomFormat());
return true;
}
bool BookmarkMenuDelegate::AreDropTypesRequired(MenuItemView* menu) {
return true;
}
bool BookmarkMenuDelegate::CanDrop(MenuItemView* menu,
const ui::OSExchangeData& data) {
// Only accept drops of 1 node, which is the case for all data dragged from
// bookmark bar and menus.
if (!drop_data_.Read(data) || drop_data_.elements.size() != 1 ||
!profile_->GetPrefs()->GetBoolean(prefs::kEditBookmarksEnabled))
return false;
if (drop_data_.has_single_url())
return true;
const BookmarkNode* drag_node =
drop_data_.GetFirstNode(GetBookmarkModel(), profile_->GetPath());
if (!drag_node) {
// Dragging a folder from another profile, always accept.
return true;
}
// Drag originated from same profile and is not a URL. Only accept it if
// the dragged node is not a parent of the node menu represents.
if (menu_id_to_node_map_.find(menu->GetCommand()) ==
menu_id_to_node_map_.end()) {
// If we don't know the menu assume its because we're embedded. We'll
// figure out the real operation when GetDropOperation is invoked.
return true;
}
const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()];
DCHECK(drop_node);
while (drop_node && drop_node != drag_node)
drop_node = drop_node->parent();
return (drop_node == NULL);
}
int BookmarkMenuDelegate::GetDropOperation(
MenuItemView* item,
const ui::DropTargetEvent& event,
views::MenuDelegate::DropPosition* position) {
// Should only get here if we have drop data.
DCHECK(drop_data_.is_valid());
const BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()];
const BookmarkNode* drop_parent = node->parent();
int index_to_drop_at = drop_parent->GetIndexOf(node);
BookmarkModel* model = GetBookmarkModel();
switch (*position) {
case views::MenuDelegate::DROP_AFTER:
if (node == model->other_node() || node == model->mobile_node()) {
// Dropping after these nodes makes no sense.
*position = views::MenuDelegate::DROP_NONE;
}
index_to_drop_at++;
break;
case views::MenuDelegate::DROP_BEFORE:
if (node == model->mobile_node()) {
// Dropping before this node makes no sense.
*position = views::MenuDelegate::DROP_NONE;
}
break;
case views::MenuDelegate::DROP_ON:
drop_parent = node;
index_to_drop_at = node->child_count();
break;
default:
break;
}
DCHECK(drop_parent);
return chrome::GetBookmarkDropOperation(
profile_, event, drop_data_, drop_parent, index_to_drop_at);
}
int BookmarkMenuDelegate::OnPerformDrop(
MenuItemView* menu,
views::MenuDelegate::DropPosition position,
const ui::DropTargetEvent& event) {
const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()];
DCHECK(drop_node);
BookmarkModel* model = GetBookmarkModel();
DCHECK(model);
const BookmarkNode* drop_parent = drop_node->parent();
DCHECK(drop_parent);
int index_to_drop_at = drop_parent->GetIndexOf(drop_node);
switch (position) {
case views::MenuDelegate::DROP_AFTER:
index_to_drop_at++;
break;
case views::MenuDelegate::DROP_ON:
DCHECK(drop_node->is_folder());
drop_parent = drop_node;
index_to_drop_at = drop_node->child_count();
break;
case views::MenuDelegate::DROP_BEFORE:
if (drop_node == model->other_node() ||
drop_node == model->mobile_node()) {
// This can happen with SHOW_PERMANENT_FOLDERS.
drop_parent = model->bookmark_bar_node();
index_to_drop_at = drop_parent->child_count();
}
break;
default:
break;
}
bool copy = event.source_operations() == ui::DragDropTypes::DRAG_COPY;
return chrome::DropBookmarks(profile_, drop_data_,
drop_parent, index_to_drop_at, copy);
}
bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source,
int id,
const gfx::Point& p,
ui::MenuSourceType source_type) {
DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end());
std::vector<const BookmarkNode*> nodes;
nodes.push_back(menu_id_to_node_map_[id]);
bool close_on_delete = !parent_menu_item_ &&
(nodes[0]->parent() == GetBookmarkModel()->other_node() &&
nodes[0]->parent()->child_count() == 1);
context_menu_.reset(
new BookmarkContextMenu(
parent_,
browser_,
profile_,
page_navigator_,
nodes[0]->parent(),
nodes,
close_on_delete));
context_menu_->set_observer(this);
context_menu_->RunMenuAt(p, source_type);
context_menu_.reset(NULL);
return true;
}
bool BookmarkMenuDelegate::CanDrag(MenuItemView* menu) {
const BookmarkNode* node = menu_id_to_node_map_[menu->GetCommand()];
// Don't let users drag the other folder.
return node->parent() != GetBookmarkModel()->root_node();
}
void BookmarkMenuDelegate::WriteDragData(MenuItemView* sender,
ui::OSExchangeData* data) {
DCHECK(sender && data);
content::RecordAction(UserMetricsAction("BookmarkBar_DragFromFolder"));
BookmarkNodeData drag_data(menu_id_to_node_map_[sender->GetCommand()]);
drag_data.Write(profile_->GetPath(), data);
}
int BookmarkMenuDelegate::GetDragOperations(MenuItemView* sender) {
return chrome::GetBookmarkDragOperation(
profile_, menu_id_to_node_map_[sender->GetCommand()]);
}
int BookmarkMenuDelegate::GetMaxWidthForMenu(MenuItemView* menu) {
return kMaxMenuWidth;
}
void BookmarkMenuDelegate::BookmarkModelChanged() {
}
void BookmarkMenuDelegate::BookmarkNodeFaviconChanged(
BookmarkModel* model,
const BookmarkNode* node) {
NodeToMenuMap::iterator menu_pair = node_to_menu_map_.find(node);
if (menu_pair == node_to_menu_map_.end())
return; // We're not showing a menu item for the node.
menu_pair->second->SetIcon(model->GetFavicon(node).AsImageSkia());
}
void BookmarkMenuDelegate::WillRemoveBookmarks(
const std::vector<const BookmarkNode*>& bookmarks) {
DCHECK(!is_mutating_model_);
is_mutating_model_ = true; // Set to false in DidRemoveBookmarks().
// Remove the observer so that when the remove happens we don't prematurely
// cancel the menu. The observer is added back in DidRemoveBookmarks().
GetBookmarkModel()->RemoveObserver(this);
// Remove the menu items.
std::set<MenuItemView*> changed_parent_menus;
for (std::vector<const BookmarkNode*>::const_iterator i(bookmarks.begin());
i != bookmarks.end(); ++i) {
NodeToMenuMap::iterator node_to_menu = node_to_menu_map_.find(*i);
if (node_to_menu != node_to_menu_map_.end()) {
MenuItemView* menu = node_to_menu->second;
MenuItemView* parent = menu->GetParentMenuItem();
// |parent| is NULL when removing a root. This happens when right clicking
// to delete an empty folder.
if (parent) {
changed_parent_menus.insert(parent);
parent->RemoveMenuItemAt(menu->parent()->GetIndexOf(menu));
}
node_to_menu_map_.erase(node_to_menu);
menu_id_to_node_map_.erase(menu->GetCommand());
}
}
// All the bookmarks in |bookmarks| should have the same parent. It's possible
// to support different parents, but this would need to prune any nodes whose
// parent has been removed. As all nodes currently have the same parent, there
// is the DCHECK.
DCHECK(changed_parent_menus.size() <= 1);
// Remove any descendants of the removed nodes in |node_to_menu_map_|.
for (NodeToMenuMap::iterator i(node_to_menu_map_.begin());
i != node_to_menu_map_.end(); ) {
bool ancestor_removed = false;
for (std::vector<const BookmarkNode*>::const_iterator j(bookmarks.begin());
j != bookmarks.end(); ++j) {
if (i->first->HasAncestor(*j)) {
ancestor_removed = true;
break;
}
}
if (ancestor_removed) {
menu_id_to_node_map_.erase(i->second->GetCommand());
node_to_menu_map_.erase(i++);
} else {
++i;
}
}
for (std::set<MenuItemView*>::const_iterator i(changed_parent_menus.begin());
i != changed_parent_menus.end(); ++i)
(*i)->ChildrenChanged();
}
void BookmarkMenuDelegate::DidRemoveBookmarks() {
// Balances remove in WillRemoveBookmarksImpl.
GetBookmarkModel()->AddObserver(this);
DCHECK(is_mutating_model_);
is_mutating_model_ = false;
}
MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent,
int start_child_index,
ShowOptions show_options) {
MenuItemView* menu = new MenuItemView(real_delegate_);
menu->SetCommand(next_menu_id_++);
menu_id_to_node_map_[menu->GetCommand()] = parent;
menu->set_has_icons(true);
bool show_permanent = show_options == SHOW_PERMANENT_FOLDERS;
if (show_permanent && parent == GetBookmarkModel()->bookmark_bar_node())
BuildMenuForManagedNode(menu, &next_menu_id_);
BuildMenu(parent, start_child_index, menu, &next_menu_id_);
if (show_permanent)
BuildMenusForPermanentNodes(menu, &next_menu_id_);
return menu;
}
void BookmarkMenuDelegate::BuildMenusForPermanentNodes(
views::MenuItemView* menu,
int* next_menu_id) {
BookmarkModel* model = GetBookmarkModel();
bool added_separator = false;
BuildMenuForPermanentNode(model->other_node(), IDR_BOOKMARK_BAR_FOLDER, menu,
next_menu_id, &added_separator);
BuildMenuForPermanentNode(model->mobile_node(), IDR_BOOKMARK_BAR_FOLDER, menu,
next_menu_id, &added_separator);
}
void BookmarkMenuDelegate::BuildMenuForPermanentNode(
const BookmarkNode* node,
int icon_resource_id,
MenuItemView* menu,
int* next_menu_id,
bool* added_separator) {
if (!node->IsVisible() || node->GetTotalNodeCount() == 1)
return; // No children, don't create a menu.
int id = *next_menu_id;
// Don't create the submenu if its menu ID will be outside the range allowed.
if (IsOutsideMenuIdRange(id))
return;
(*next_menu_id)++;
if (!*added_separator) {
*added_separator = true;
menu->AppendSeparator();
}
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(icon_resource_id);
MenuItemView* submenu = menu->AppendSubMenuWithIcon(
id, node->GetTitle(), *folder_icon);
BuildMenu(node, 0, submenu, next_menu_id);
menu_id_to_node_map_[id] = node;
}
void BookmarkMenuDelegate::BuildMenuForManagedNode(
MenuItemView* menu,
int* next_menu_id) {
// Don't add a separator for this menu.
bool added_separator = true;
const BookmarkNode* node = GetChromeBookmarkClient()->managed_node();
BuildMenuForPermanentNode(node, IDR_BOOKMARK_BAR_FOLDER_MANAGED, menu,
next_menu_id, &added_separator);
}
void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent,
int start_child_index,
MenuItemView* menu,
int* next_menu_id) {
node_to_menu_map_[parent] = menu;
DCHECK(parent->empty() || start_child_index < parent->child_count());
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
for (int i = start_child_index; i < parent->child_count(); ++i) {
const BookmarkNode* node = parent->GetChild(i);
const int id = *next_menu_id;
// Don't create the item if its menu ID will be outside the range allowed.
if (IsOutsideMenuIdRange(id))
break;
(*next_menu_id)++;
menu_id_to_node_map_[id] = node;
if (node->is_url()) {
const gfx::Image& image = GetBookmarkModel()->GetFavicon(node);
const gfx::ImageSkia* icon = image.IsEmpty() ?
rb->GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia();
node_to_menu_map_[node] =
menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon);
} else if (node->is_folder()) {
gfx::ImageSkia* folder_icon =
rb->GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER);
MenuItemView* submenu = menu->AppendSubMenuWithIcon(
id, node->GetTitle(), *folder_icon);
BuildMenu(node, 0, submenu, next_menu_id);
} else {
NOTREACHED();
}
}
}
bool BookmarkMenuDelegate::IsOutsideMenuIdRange(int menu_id) const {
return menu_id < min_menu_id_ || menu_id > max_menu_id_;
}
|