Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_action_view.cc
blobcff8c2f5177995dd0ae87937f7fff3071eac3312
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
7 #include <string>
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sessions/session_tab_helper.h"
12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h"
14 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
15 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
16 #include "chrome/browser/ui/view_ids.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/notification_source.h"
19 #include "grit/theme_resources.h"
20 #include "ui/accessibility/ax_view_state.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/compositor/paint_recorder.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/image/image_skia_source.h"
27 #include "ui/resources/grit/ui_resources.h"
28 #include "ui/views/controls/button/label_button_border.h"
29 #include "ui/views/controls/menu/menu_controller.h"
30 #include "ui/views/controls/menu/menu_runner.h"
31 #include "ui/views/resources/grit/views_resources.h"
33 using views::LabelButtonBorder;
35 namespace {
37 // Toolbar action buttons have no insets because the badges are drawn right at
38 // the edge of the view's area. Other badding (such as centering the icon) is
39 // handled directly by the Image.
40 const int kBorderInset = 0;
42 // The ToolbarActionView which is currently showing its context menu, if any.
43 // Since only one context menu can be shown (even across browser windows), it's
44 // safe to have this be a global singleton.
45 ToolbarActionView* context_menu_owner = nullptr;
47 } // namespace
49 ////////////////////////////////////////////////////////////////////////////////
50 // ToolbarActionView
52 ToolbarActionView::ToolbarActionView(
53 ToolbarActionViewController* view_controller,
54 Profile* profile,
55 ToolbarActionView::Delegate* delegate)
56 : MenuButton(this, base::string16(), nullptr, false),
57 view_controller_(view_controller),
58 profile_(profile),
59 delegate_(delegate),
60 called_register_command_(false),
61 wants_to_run_(false),
62 weak_factory_(this) {
63 set_id(VIEW_ID_BROWSER_ACTION);
64 view_controller_->SetDelegate(this);
65 SetHorizontalAlignment(gfx::ALIGN_CENTER);
66 set_drag_controller(delegate_);
68 set_context_menu_controller(this);
70 // We also listen for browser theme changes on linux because a switch from or
71 // to GTK requires that we regrab our browser action images.
72 registrar_.Add(
73 this,
74 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
75 content::Source<ThemeService>(
76 ThemeServiceFactory::GetForProfile(profile_)));
78 // If the button is within a menu, we need to make it focusable in order to
79 // have it accessible via keyboard navigation, but it shouldn't request focus
80 // (because that would close the menu).
81 if (delegate_->ShownInsideMenu()) {
82 set_request_focus_on_press(false);
83 SetFocusable(true);
86 UpdateState();
89 ToolbarActionView::~ToolbarActionView() {
90 if (context_menu_owner == this)
91 context_menu_owner = nullptr;
92 view_controller_->SetDelegate(nullptr);
95 gfx::Size ToolbarActionView::GetPreferredSize() const {
96 return gfx::Size(ToolbarActionsBar::IconWidth(false),
97 ToolbarActionsBar::IconHeight());
100 void ToolbarActionView::OnDragDone() {
101 views::MenuButton::OnDragDone();
102 delegate_->OnToolbarActionViewDragDone();
105 void ToolbarActionView::ViewHierarchyChanged(
106 const ViewHierarchyChangedDetails& details) {
107 if (details.is_add && !called_register_command_ && GetFocusManager()) {
108 view_controller_->RegisterCommand();
109 called_register_command_ = true;
112 MenuButton::ViewHierarchyChanged(details);
115 void ToolbarActionView::GetAccessibleState(ui::AXViewState* state) {
116 views::MenuButton::GetAccessibleState(state);
117 state->role = ui::AX_ROLE_BUTTON;
120 void ToolbarActionView::ButtonPressed(views::Button* sender,
121 const ui::Event& event) {
122 gfx::Point menu_point;
123 ui::MenuSourceType type = ui::MENU_SOURCE_NONE;
124 if (event.IsMouseEvent()) {
125 menu_point = static_cast<const ui::MouseEvent&>(event).location();
126 type = ui::MENU_SOURCE_MOUSE;
127 } else if (event.IsKeyEvent()) {
128 menu_point = GetKeyboardContextMenuLocation();
129 type = ui::MENU_SOURCE_KEYBOARD;
130 } else if (event.IsGestureEvent()) {
131 menu_point = static_cast<const ui::GestureEvent&>(event).location();
132 type = ui::MENU_SOURCE_TOUCH;
135 HandleActivation(menu_point, type);
138 void ToolbarActionView::UpdateState() {
139 content::WebContents* web_contents = GetCurrentWebContents();
140 if (SessionTabHelper::IdForTab(web_contents) < 0)
141 return;
143 if (!view_controller_->IsEnabled(web_contents) &&
144 !view_controller_->DisabledClickOpensMenu()) {
145 SetState(views::CustomButton::STATE_DISABLED);
146 } else if (state() == views::CustomButton::STATE_DISABLED) {
147 SetState(views::CustomButton::STATE_NORMAL);
150 wants_to_run_ = view_controller_->WantsToRun(web_contents);
152 gfx::ImageSkia icon(
153 view_controller_->GetIcon(web_contents,
154 GetPreferredSize()).AsImageSkia());
156 if (!icon.isNull()) {
157 ThemeService* theme = ThemeServiceFactory::GetForProfile(profile_);
159 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION);
160 SetImage(views::Button::STATE_NORMAL,
161 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon));
164 SetTooltipText(view_controller_->GetTooltip(web_contents));
165 SetAccessibleName(view_controller_->GetAccessibleName(web_contents));
167 Layout(); // We need to layout since we may have added an icon as a result.
168 SchedulePaint();
171 void ToolbarActionView::Observe(int type,
172 const content::NotificationSource& source,
173 const content::NotificationDetails& details) {
174 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
175 UpdateState();
178 bool ToolbarActionView::Activate() {
179 if (!view_controller_->HasPopup(GetCurrentWebContents()))
180 return true;
182 // Unfortunately, we don't get any of the event points for this call. Since
183 // these are only used for showing a context menu when an action is disabled,
184 // it's not that big a deal. Fake it.
185 // TODO(devlin): This could obviously be improved.
186 HandleActivation(GetKeyboardContextMenuLocation(), ui::MENU_SOURCE_KEYBOARD);
188 // TODO(erikkay): Run a nested modal loop while the mouse is down to
189 // enable menu-like drag-select behavior.
191 // The return value of this method is returned via OnMousePressed.
192 // We need to return false here since we're handing off focus to another
193 // widget/view, and true will grab it right back and try to send events
194 // to us.
195 return false;
198 void ToolbarActionView::OnMouseEntered(const ui::MouseEvent& event) {
199 delegate_->OnMouseEnteredToolbarActionView();
200 views::MenuButton::OnMouseEntered(event);
203 bool ToolbarActionView::OnMousePressed(const ui::MouseEvent& event) {
204 if (!event.IsRightMouseButton()) {
205 return view_controller_->HasPopup(GetCurrentWebContents()) ?
206 MenuButton::OnMousePressed(event) : LabelButton::OnMousePressed(event);
208 return false;
211 void ToolbarActionView::OnMouseReleased(const ui::MouseEvent& event) {
212 if (view_controller_->HasPopup(GetCurrentWebContents()) || IsMenuRunning()) {
213 // TODO(erikkay) this never actually gets called (probably because of the
214 // loss of focus).
215 MenuButton::OnMouseReleased(event);
216 } else {
217 LabelButton::OnMouseReleased(event);
221 void ToolbarActionView::OnMouseExited(const ui::MouseEvent& event) {
222 if (view_controller_->HasPopup(GetCurrentWebContents()) || IsMenuRunning())
223 MenuButton::OnMouseExited(event);
224 else
225 LabelButton::OnMouseExited(event);
228 bool ToolbarActionView::OnKeyReleased(const ui::KeyEvent& event) {
229 return view_controller_->HasPopup(GetCurrentWebContents()) ?
230 MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event);
233 void ToolbarActionView::OnGestureEvent(ui::GestureEvent* event) {
234 if (view_controller_->HasPopup(GetCurrentWebContents()))
235 MenuButton::OnGestureEvent(event);
236 else
237 LabelButton::OnGestureEvent(event);
240 scoped_ptr<LabelButtonBorder> ToolbarActionView::CreateDefaultBorder() const {
241 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder();
242 border->set_insets(gfx::Insets(kBorderInset, kBorderInset,
243 kBorderInset, kBorderInset));
244 return border.Pass();
247 bool ToolbarActionView::ShouldEnterPushedState(const ui::Event& event) {
248 return view_controller_->HasPopup(GetCurrentWebContents()) ?
249 MenuButton::ShouldEnterPushedState(event) :
250 LabelButton::ShouldEnterPushedState(event);
253 gfx::ImageSkia ToolbarActionView::GetIconForTest() {
254 return GetImage(views::Button::STATE_NORMAL);
257 views::View* ToolbarActionView::GetAsView() {
258 return this;
261 views::FocusManager* ToolbarActionView::GetFocusManagerForAccelerator() {
262 return GetFocusManager();
265 views::View* ToolbarActionView::GetReferenceViewForPopup() {
266 // Browser actions in the overflow menu can still show popups, so we may need
267 // a reference view other than this button's parent. If so, use the overflow
268 // view.
269 return visible() ? this : delegate_->GetOverflowReferenceView();
272 bool ToolbarActionView::IsMenuRunning() const {
273 return menu_runner_.get() != nullptr;
276 content::WebContents* ToolbarActionView::GetCurrentWebContents() const {
277 return delegate_->GetCurrentWebContents();
280 void ToolbarActionView::OnPopupShown(bool by_user) {
281 // If this was through direct user action, we press the menu button.
282 if (by_user) {
283 // We set the state of the menu button we're using as a reference view,
284 // which is either this or the overflow reference view.
285 // This cast is safe because GetReferenceViewForPopup returns either |this|
286 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton.
287 views::MenuButton* reference_view =
288 static_cast<views::MenuButton*>(GetReferenceViewForPopup());
289 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view));
293 void ToolbarActionView::OnPopupClosed() {
294 pressed_lock_.reset(); // Unpress the menu button if it was pressed.
297 void ToolbarActionView::ShowContextMenuForView(
298 views::View* source,
299 const gfx::Point& point,
300 ui::MenuSourceType source_type) {
301 // If there's another active menu that won't be dismissed by opening this one,
302 // then we can't show this one right away, since we can only show one nested
303 // menu at a time.
304 // If the other menu is an extension action's context menu, then we'll run
305 // this one after that one closes. If it's a different type of menu, then we
306 // close it and give up, for want of a better solution. (Luckily, this is
307 // rare).
308 // TODO(devlin): Update this when views code no longer runs menus in a nested
309 // loop.
310 if (context_menu_owner) {
311 context_menu_owner->followup_context_menu_task_ =
312 base::Bind(&ToolbarActionView::DoShowContextMenu,
313 weak_factory_.GetWeakPtr(),
314 source_type);
316 if (CloseActiveMenuIfNeeded())
317 return;
319 // Otherwise, no other menu is showing, and we can proceed normally.
320 DoShowContextMenu(source_type);
323 void ToolbarActionView::DoShowContextMenu(
324 ui::MenuSourceType source_type) {
325 ui::MenuModel* context_menu_model = view_controller_->GetContextMenu();
326 // It's possible the action doesn't have a context menu.
327 if (!context_menu_model)
328 return;
330 DCHECK(visible()); // We should never show a context menu for a hidden item.
331 DCHECK(!context_menu_owner);
332 context_menu_owner = this;
334 gfx::Point screen_loc;
335 ConvertPointToScreen(this, &screen_loc);
337 int run_types =
338 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU;
339 if (delegate_->ShownInsideMenu())
340 run_types |= views::MenuRunner::IS_NESTED;
342 // RunMenuAt expects a nested menu to be parented by the same widget as the
343 // already visible menu, in this case the Chrome menu.
344 views::Widget* parent = delegate_->ShownInsideMenu() ?
345 delegate_->GetOverflowReferenceView()->GetWidget() :
346 GetWidget();
348 menu_runner_.reset(new views::MenuRunner(context_menu_model, run_types));
350 if (menu_runner_->RunMenuAt(parent,
351 this,
352 gfx::Rect(screen_loc, size()),
353 views::MENU_ANCHOR_TOPLEFT,
354 source_type) == views::MenuRunner::MENU_DELETED) {
355 return;
358 context_menu_owner = nullptr;
359 menu_runner_.reset();
360 view_controller_->OnContextMenuClosed();
362 // If another extension action wants to show its context menu, allow it to.
363 if (!followup_context_menu_task_.is_null()) {
364 base::Closure task = followup_context_menu_task_;
365 followup_context_menu_task_ = base::Closure();
366 task.Run();
370 bool ToolbarActionView::CloseActiveMenuIfNeeded() {
371 // If this view is shown inside another menu, there's a possibility that there
372 // is another context menu showing that we have to close before we can
373 // activate a different menu.
374 if (delegate_->ShownInsideMenu()) {
375 views::MenuController* menu_controller =
376 views::MenuController::GetActiveInstance();
377 // If this is shown inside a menu, then there should always be an active
378 // menu controller.
379 DCHECK(menu_controller);
380 if (menu_controller->in_nested_run()) {
381 // There is another menu showing. Close the outermost menu (since we are
382 // shown in the same menu, we don't want to close the whole thing).
383 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST);
384 return true;
388 return false;
391 void ToolbarActionView::HandleActivation(const gfx::Point& menu_point,
392 ui::MenuSourceType source_type) {
393 if (!view_controller_->IsEnabled(GetCurrentWebContents())) {
394 // We should only get a button pressed event with a non-enabled action if
395 // the left-click behavior should open the menu.
396 DCHECK(view_controller_->DisabledClickOpensMenu());
397 context_menu_controller()->ShowContextMenuForView(
398 this, menu_point, source_type);
399 } else {
400 view_controller_->ExecuteAction(true);