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.
5 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_action.h"
9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sessions/session_tab_helper.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "ui/accessibility/ax_view_state.h"
16 #include "ui/events/event.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/image/image.h"
21 const char PageActionImageView::kViewClassName
[] = "PageActionImageView";
23 PageActionImageView::PageActionImageView(LocationBarView
* owner
,
24 ExtensionAction
* page_action
,
26 : view_controller_(new ExtensionActionViewController(
27 extensions::ExtensionRegistry::Get(browser
->profile())->
28 enabled_extensions().GetByID(page_action
->extension_id()),
32 preview_enabled_(false) {
33 // There should be an associated focus manager so that we can safely register
34 // accelerators for commands.
35 DCHECK(GetFocusManagerForAccelerator());
36 SetAccessibilityFocusable(true);
37 view_controller_
->SetDelegate(this);
38 view_controller_
->RegisterCommand();
41 PageActionImageView::~PageActionImageView() {
44 const char* PageActionImageView::GetClassName() const {
45 return kViewClassName
;
48 void PageActionImageView::GetAccessibleState(ui::AXViewState
* state
) {
49 state
->role
= ui::AX_ROLE_BUTTON
;
50 state
->name
= base::UTF8ToUTF16(tooltip_
);
53 bool PageActionImageView::OnMousePressed(const ui::MouseEvent
& event
) {
54 // We want to show the bubble on mouse release; that is the standard behavior
55 // for buttons. (Also, triggering on mouse press causes bugs like
56 // http://crbug.com/33155.)
60 void PageActionImageView::OnMouseReleased(const ui::MouseEvent
& event
) {
61 if (!HitTestPoint(event
.location()))
64 if (event
.IsRightMouseButton()) {
65 // Don't show a menu here, its handled in View::ProcessMouseReleased. We
66 // show the context menu by way of being the ContextMenuController.
70 view_controller_
->ExecuteAction(true);
73 bool PageActionImageView::OnKeyPressed(const ui::KeyEvent
& event
) {
74 if (event
.key_code() == ui::VKEY_SPACE
||
75 event
.key_code() == ui::VKEY_RETURN
) {
76 view_controller_
->ExecuteAction(true);
82 void PageActionImageView::OnGestureEvent(ui::GestureEvent
* event
) {
83 if (event
->type() == ui::ET_GESTURE_TAP
) {
84 view_controller_
->ExecuteAction(true);
89 void PageActionImageView::UpdateVisibility(content::WebContents
* contents
) {
90 int tab_id
= SessionTabHelper::IdForTab(contents
);
93 (!preview_enabled_
&& !extension_action()->GetIsVisible(tab_id
))) {
99 tooltip_
= extension_action()->GetTitle(tab_id
);
100 SetTooltipText(base::UTF8ToUTF16(tooltip_
));
103 gfx::Image icon
= view_controller_
->GetIcon(contents
);
105 SetImage(*icon
.ToImageSkia());
110 void PageActionImageView::PaintChildren(const PaintContext
& context
) {
111 View::PaintChildren(context
);
112 int tab_id
= SessionTabHelper::IdForTab(GetCurrentWebContents());
114 gfx::Canvas
* canvas
= context
.canvas();
115 view_controller_
->extension_action()->PaintBadge(
116 canvas
, GetLocalBounds(), tab_id
);
120 void PageActionImageView::UpdateState() {
121 UpdateVisibility(GetCurrentWebContents());
124 views::View
* PageActionImageView::GetAsView() {
128 bool PageActionImageView::IsShownInMenu() {
132 views::FocusManager
* PageActionImageView::GetFocusManagerForAccelerator() {
133 return owner_
->GetFocusManager();
136 views::Widget
* PageActionImageView::GetParentForContextMenu() {
140 ToolbarActionViewController
*
141 PageActionImageView::GetPreferredPopupViewController() {
142 return view_controller_
.get();
145 views::View
* PageActionImageView::GetReferenceViewForPopup() {
149 views::MenuButton
* PageActionImageView::GetContextMenuButton() {
150 return NULL
; // No menu button for page action views.
153 content::WebContents
* PageActionImageView::GetCurrentWebContents() const {
154 return owner_
->GetWebContents();