browser_action_overflow_menu_controller.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_controller.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/browser/extensions/extension_action.h"
10#include "chrome/browser/extensions/extension_action_manager.h"
11#include "chrome/browser/extensions/extension_context_menu_model.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/browser_list.h"
14#include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
15#include "chrome/browser/ui/views/toolbar/browser_action_view.h"
16#include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
17#include "extensions/common/extension.h"
18#include "ui/gfx/canvas.h"
19#include "ui/views/controls/menu/menu_item_view.h"
20#include "ui/views/controls/menu/menu_runner.h"
21#include "ui/views/controls/menu/submenu_view.h"
22#include "ui/views/widget/widget.h"
23
24// In the browser actions container's chevron menu, a menu item view's icon
25// comes from BrowserActionView::GetIconWithBadge() (which comes from the
26// browser action button's icon) when the menu item view is created. But, the
27// browser action button's icon may not be loaded in time because it is read
28// from file system in another thread.
29// The IconUpdater will update the menu item view's icon when the browser
30// action button's icon has been updated.
31class IconUpdater : public BrowserActionButton::IconObserver {
32 public:
33  IconUpdater(views::MenuItemView* menu_item_view,
34              BrowserActionButton* button)
35      : menu_item_view_(menu_item_view),
36        button_(button) {
37    DCHECK(menu_item_view);
38    DCHECK(button);
39    button->set_icon_observer(this);
40  }
41  virtual ~IconUpdater() {
42    button_->set_icon_observer(NULL);
43  }
44
45  // Overridden from BrowserActionButton::IconObserver:
46  virtual void OnIconUpdated(const gfx::ImageSkia& icon) OVERRIDE {
47    menu_item_view_->SetIcon(icon);
48  }
49
50 private:
51  // The menu item view whose icon might be updated.
52  views::MenuItemView* menu_item_view_;
53
54  // The button to be observed. When its icon changes, update the corresponding
55  // menu item view's icon.
56  BrowserActionButton* button_;
57
58  DISALLOW_COPY_AND_ASSIGN(IconUpdater);
59};
60
61BrowserActionOverflowMenuController::BrowserActionOverflowMenuController(
62    BrowserActionsContainer* owner,
63    Browser* browser,
64    views::MenuButton* menu_button,
65    const std::vector<BrowserActionView*>& views,
66    int start_index)
67    : owner_(owner),
68      browser_(browser),
69      observer_(NULL),
70      menu_button_(menu_button),
71      menu_(NULL),
72      views_(&views),
73      start_index_(start_index),
74      for_drop_(false) {
75  menu_ = new views::MenuItemView(this);
76  menu_runner_.reset(new views::MenuRunner(menu_));
77  menu_->set_has_icons(true);
78
79  size_t command_id = 1;  // Menu id 0 is reserved, start with 1.
80  for (size_t i = start_index; i < views_->size(); ++i) {
81    BrowserActionView* view = (*views_)[i];
82    views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon(
83        command_id,
84        base::UTF8ToUTF16(view->button()->extension()->name()),
85        view->GetIconWithBadge());
86
87    // Set the tooltip for this item.
88    base::string16 tooltip = base::UTF8ToUTF16(
89        extensions::ExtensionActionManager::Get(owner_->profile())->
90        GetBrowserAction(*view->button()->extension())->
91        GetTitle(owner_->GetCurrentTabId()));
92    menu_->SetTooltip(tooltip, command_id);
93
94    icon_updaters_.push_back(new IconUpdater(menu_item, view->button()));
95
96    ++command_id;
97  }
98}
99
100BrowserActionOverflowMenuController::~BrowserActionOverflowMenuController() {
101  if (observer_)
102    observer_->NotifyMenuDeleted(this);
103}
104
105bool BrowserActionOverflowMenuController::RunMenu(views::Widget* window,
106                                                  bool for_drop) {
107  for_drop_ = for_drop;
108
109  gfx::Rect bounds = menu_button_->bounds();
110  gfx::Point screen_loc;
111  views::View::ConvertPointToScreen(menu_button_, &screen_loc);
112  bounds.set_x(screen_loc.x());
113  bounds.set_y(screen_loc.y());
114
115  views::MenuItemView::AnchorPosition anchor = views::MenuItemView::TOPRIGHT;
116  // As we maintain our own lifetime we can safely ignore the result.
117  ignore_result(menu_runner_->RunMenuAt(window, menu_button_, bounds, anchor,
118      ui::MENU_SOURCE_NONE, for_drop_ ? views::MenuRunner::FOR_DROP : 0));
119  if (!for_drop_) {
120    // Give the context menu (if any) a chance to execute the user-selected
121    // command.
122    base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
123  }
124  return true;
125}
126
127void BrowserActionOverflowMenuController::CancelMenu() {
128  menu_->Cancel();
129}
130
131bool BrowserActionOverflowMenuController::IsCommandEnabled(int id) const {
132  BrowserActionView* view = (*views_)[start_index_ + id - 1];
133  return view->button()->IsEnabled(owner_->GetCurrentTabId());
134}
135
136void BrowserActionOverflowMenuController::ExecuteCommand(int id) {
137  BrowserActionView* view = (*views_)[start_index_ + id - 1];
138  owner_->OnBrowserActionExecuted(view->button());
139}
140
141bool BrowserActionOverflowMenuController::ShowContextMenu(
142    views::MenuItemView* source,
143    int id,
144    const gfx::Point& p,
145    ui::MenuSourceType source_type) {
146  const extensions::Extension* extension =
147      (*views_)[start_index_ + id - 1]->button()->extension();
148  if (!extension->ShowConfigureContextMenus())
149    return false;
150
151  scoped_refptr<ExtensionContextMenuModel> context_menu_contents =
152      new ExtensionContextMenuModel(extension, browser_, owner_);
153  views::MenuRunner context_menu_runner(context_menu_contents.get());
154
155  // We can ignore the result as we delete ourself.
156  // This blocks until the user choses something or dismisses the menu.
157  ignore_result(context_menu_runner.RunMenuAt(menu_button_->GetWidget(),
158      NULL, gfx::Rect(p, gfx::Size()), views::MenuItemView::TOPLEFT,
159      source_type,
160      views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::IS_NESTED |
161      views::MenuRunner::CONTEXT_MENU));
162
163  // The user is done with the context menu, so we can close the underlying
164  // menu.
165  menu_->Cancel();
166
167  return true;
168}
169
170void BrowserActionOverflowMenuController::DropMenuClosed(
171    views::MenuItemView* menu) {
172  delete this;
173}
174
175bool BrowserActionOverflowMenuController::GetDropFormats(
176    views::MenuItemView* menu,
177    int* formats,
178    std::set<OSExchangeData::CustomFormat>* custom_formats) {
179  custom_formats->insert(BrowserActionDragData::GetBrowserActionCustomFormat());
180  return true;
181}
182
183bool BrowserActionOverflowMenuController::AreDropTypesRequired(
184    views::MenuItemView* menu) {
185  return true;
186}
187
188bool BrowserActionOverflowMenuController::CanDrop(
189    views::MenuItemView* menu, const OSExchangeData& data) {
190  BrowserActionDragData drop_data;
191  if (!drop_data.Read(data))
192    return false;
193  return drop_data.IsFromProfile(owner_->profile());
194}
195
196int BrowserActionOverflowMenuController::GetDropOperation(
197    views::MenuItemView* item,
198    const ui::DropTargetEvent& event,
199    DropPosition* position) {
200  // Don't allow dropping from the BrowserActionContainer into slot 0 of the
201  // overflow menu since once the move has taken place the item you are dragging
202  // falls right out of the menu again once the user releases the button
203  // (because we don't shrink the BrowserActionContainer when you do this).
204  if ((item->GetCommand() == 0) && (*position == DROP_BEFORE)) {
205    BrowserActionDragData drop_data;
206    if (!drop_data.Read(event.data()))
207      return ui::DragDropTypes::DRAG_NONE;
208
209    if (drop_data.index() < owner_->VisibleBrowserActions())
210      return ui::DragDropTypes::DRAG_NONE;
211  }
212
213  return ui::DragDropTypes::DRAG_MOVE;
214}
215
216int BrowserActionOverflowMenuController::OnPerformDrop(
217    views::MenuItemView* menu,
218    DropPosition position,
219    const ui::DropTargetEvent& event) {
220  BrowserActionDragData drop_data;
221  if (!drop_data.Read(event.data()))
222    return ui::DragDropTypes::DRAG_NONE;
223
224  size_t drop_index;
225  ViewForId(menu->GetCommand(), &drop_index);
226
227  // When not dragging within the overflow menu (dragging an icon into the menu)
228  // subtract one to get the right index.
229  if (position == DROP_BEFORE &&
230      drop_data.index() < owner_->VisibleBrowserActions())
231    --drop_index;
232
233  owner_->MoveBrowserAction(drop_data.id(), drop_index);
234
235  if (for_drop_)
236    delete this;
237  return ui::DragDropTypes::DRAG_MOVE;
238}
239
240bool BrowserActionOverflowMenuController::CanDrag(views::MenuItemView* menu) {
241  return true;
242}
243
244void BrowserActionOverflowMenuController::WriteDragData(
245    views::MenuItemView* sender, OSExchangeData* data) {
246  size_t drag_index;
247  BrowserActionView* view = ViewForId(sender->GetCommand(), &drag_index);
248  std::string id = view->button()->extension()->id();
249
250  BrowserActionDragData drag_data(id, drag_index);
251  drag_data.Write(owner_->profile(), data);
252}
253
254int BrowserActionOverflowMenuController::GetDragOperations(
255    views::MenuItemView* sender) {
256  return ui::DragDropTypes::DRAG_MOVE;
257}
258
259BrowserActionView* BrowserActionOverflowMenuController::ViewForId(
260    int id, size_t* index) {
261  // The index of the view being dragged (GetCommand gives a 1-based index into
262  // the overflow menu).
263  size_t view_index = owner_->VisibleBrowserActions() + id - 1;
264  if (index)
265    *index = view_index;
266  return owner_->GetBrowserActionViewAt(view_index);
267}
268