Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / page_action_image_view.cc
blobbaa895b60cab2e6aa8c0a21d0f4affa2375a6ed2
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"
20 // static
21 const char PageActionImageView::kViewClassName[] = "PageActionImageView";
23 PageActionImageView::PageActionImageView(LocationBarView* owner,
24 ExtensionAction* page_action,
25 Browser* browser)
26 : view_controller_(new ExtensionActionViewController(
27 extensions::ExtensionRegistry::Get(browser->profile())->
28 enabled_extensions().GetByID(page_action->extension_id()),
29 browser,
30 page_action)),
31 owner_(owner),
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.)
57 return true;
60 void PageActionImageView::OnMouseReleased(const ui::MouseEvent& event) {
61 if (!HitTestPoint(event.location()))
62 return;
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.
67 return;
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);
77 return true;
79 return false;
82 void PageActionImageView::OnGestureEvent(ui::GestureEvent* event) {
83 if (event->type() == ui::ET_GESTURE_TAP) {
84 view_controller_->ExecuteAction(true);
85 event->SetHandled();
89 void PageActionImageView::UpdateVisibility(content::WebContents* contents) {
90 int tab_id = SessionTabHelper::IdForTab(contents);
91 if (!contents ||
92 tab_id == -1 ||
93 (!preview_enabled_ && !extension_action()->GetIsVisible(tab_id))) {
94 SetVisible(false);
95 return;
98 // Set the tooltip.
99 tooltip_ = extension_action()->GetTitle(tab_id);
100 SetTooltipText(base::UTF8ToUTF16(tooltip_));
102 // Set the image.
103 gfx::Image icon = view_controller_->GetIcon(contents);
104 if (!icon.IsEmpty())
105 SetImage(*icon.ToImageSkia());
107 SetVisible(true);
110 void PageActionImageView::PaintChildren(const PaintContext& context) {
111 View::PaintChildren(context);
112 int tab_id = SessionTabHelper::IdForTab(GetCurrentWebContents());
113 if (tab_id >= 0) {
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() {
125 return this;
128 bool PageActionImageView::IsShownInMenu() {
129 return false;
132 views::FocusManager* PageActionImageView::GetFocusManagerForAccelerator() {
133 return owner_->GetFocusManager();
136 views::Widget* PageActionImageView::GetParentForContextMenu() {
137 return GetWidget();
140 ToolbarActionViewController*
141 PageActionImageView::GetPreferredPopupViewController() {
142 return view_controller_.get();
145 views::View* PageActionImageView::GetReferenceViewForPopup() {
146 return this;
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();