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 #ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/extensions/extension_action.h"
13 #include "chrome/browser/extensions/extension_action_icon_factory.h"
14 #include "chrome/browser/extensions/extension_context_menu_model.h"
15 #include "chrome/browser/ui/views/extensions/extension_popup.h"
16 #include "ui/views/context_menu_controller.h"
17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/widget/widget_observer.h"
21 class LocationBarView
;
30 // PageActionImageView is used by the LocationBarView to display the icon for a
31 // given PageAction and notify the extension when the icon is clicked.
32 class PageActionImageView
: public views::ImageView
,
33 public ExtensionContextMenuModel::PopupDelegate
,
34 public views::WidgetObserver
,
35 public views::ContextMenuController
,
36 public ExtensionActionIconFactory::Observer
,
37 public ExtensionAction::IconAnimation::Observer
{
39 PageActionImageView(LocationBarView
* owner
,
40 ExtensionAction
* page_action
,
42 virtual ~PageActionImageView();
44 ExtensionAction
* page_action() { return page_action_
; }
46 int current_tab_id() { return current_tab_id_
; }
48 void set_preview_enabled(bool preview_enabled
) {
49 preview_enabled_
= preview_enabled
;
52 // Overridden from views::View:
53 virtual void GetAccessibleState(ui::AccessibleViewState
* state
) OVERRIDE
;
54 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
55 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
56 virtual bool OnKeyPressed(const ui::KeyEvent
& event
) OVERRIDE
;
58 // Overridden from ExtensionContextMenuModel::Delegate
59 virtual void InspectPopup(ExtensionAction
* action
) OVERRIDE
;
61 // Overridden from views::WidgetObserver:
62 virtual void OnWidgetDestroying(views::Widget
* widget
) OVERRIDE
;
64 // Overridden from views::ContextMenuController.
65 virtual void ShowContextMenuForView(View
* source
,
66 const gfx::Point
& point
,
67 ui::MenuSourceType source_type
) OVERRIDE
;
69 // Overriden from ExtensionActionIconFactory::Observer.
70 virtual void OnIconUpdated() OVERRIDE
;
72 // Overridden from ui::AcceleratorTarget:
73 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
74 virtual bool CanHandleAccelerators() const OVERRIDE
;
76 // Called to notify the PageAction that it should determine whether to be
77 // visible or hidden. |contents| is the WebContents that is active, |url| is
78 // the current page URL.
79 void UpdateVisibility(content::WebContents
* contents
, const GURL
& url
);
81 // Either notify listeners or show a popup depending on the page action.
82 void ExecuteAction(ExtensionPopup::ShowAction show_action
);
85 // Overridden from ExtensionAction::IconAnimation::Observer:
86 virtual void OnIconChanged() OVERRIDE
;
88 // Overridden from View.
89 virtual void PaintChildren(gfx::Canvas
* canvas
) OVERRIDE
;
91 // Shows the popup, with the given URL.
92 void ShowPopupWithURL(const GURL
& popup_url
,
93 ExtensionPopup::ShowAction show_action
);
95 // Hides the active popup, if there is one.
98 // The location bar view that owns us.
99 LocationBarView
* owner_
;
101 // The PageAction that this view represents. The PageAction is not owned by
102 // us, it resides in the extension of this particular profile.
103 ExtensionAction
* page_action_
;
105 // The corresponding browser.
108 // The object that will be used to get the page action icon for us.
109 // It may load the icon asynchronously (in which case the initial icon
110 // returned by the factory will be transparent), so we have to observe it for
111 // updates to the icon.
112 scoped_ptr
<ExtensionActionIconFactory
> icon_factory_
;
114 // The tab id we are currently showing the icon for.
117 // The URL we are currently showing the icon for.
120 // The string to show for a tooltip;
121 std::string tooltip_
;
123 // This is used for post-install visual feedback. The page_action icon is
124 // briefly shown even if it hasn't been enabled by its extension.
125 bool preview_enabled_
;
127 // The current popup and the button it came from. NULL if no popup.
128 ExtensionPopup
* popup_
;
130 // The extension command accelerator this page action is listening for (to
132 scoped_ptr
<ui::Accelerator
> page_action_keybinding_
;
134 // The extension command accelerator this script badge is listening for (to
136 scoped_ptr
<ui::Accelerator
> script_badge_keybinding_
;
138 scoped_ptr
<views::MenuRunner
> menu_runner_
;
140 // Fade-in animation for the icon with observer scoped to this.
141 ExtensionAction::IconAnimation::ScopedObserver
142 scoped_icon_animation_observer_
;
144 DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView
);
147 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_