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
{
38 PageActionImageView(LocationBarView
* owner
,
39 ExtensionAction
* page_action
,
41 virtual ~PageActionImageView();
43 ExtensionAction
* page_action() { return page_action_
; }
45 int current_tab_id() { return current_tab_id_
; }
47 void set_preview_enabled(bool preview_enabled
) {
48 preview_enabled_
= preview_enabled
;
51 // Overridden from views::View:
52 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
53 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
54 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
55 virtual bool OnKeyPressed(const ui::KeyEvent
& event
) OVERRIDE
;
57 // Overridden from ExtensionContextMenuModel::Delegate
58 virtual void InspectPopup(ExtensionAction
* action
) OVERRIDE
;
60 // Overridden from views::WidgetObserver:
61 virtual void OnWidgetDestroying(views::Widget
* widget
) OVERRIDE
;
63 // Overridden from views::ContextMenuController.
64 virtual void ShowContextMenuForView(View
* source
,
65 const gfx::Point
& point
,
66 ui::MenuSourceType source_type
) OVERRIDE
;
68 // Overriden from ExtensionActionIconFactory::Observer.
69 virtual void OnIconUpdated() OVERRIDE
;
71 // Overridden from ui::AcceleratorTarget:
72 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
73 virtual bool CanHandleAccelerators() const OVERRIDE
;
75 // Called to notify the PageAction that it should determine whether to be
76 // visible or hidden. |contents| is the WebContents that is active, |url| is
77 // the current page URL.
78 void UpdateVisibility(content::WebContents
* contents
, const GURL
& url
);
80 // Either notify listeners or show a popup depending on the page action.
81 void ExecuteAction(ExtensionPopup::ShowAction show_action
);
84 // Overridden from View.
85 virtual void PaintChildren(gfx::Canvas
* canvas
,
86 const views::CullSet
& cull_set
) OVERRIDE
;
88 // Shows the popup, with the given URL.
89 void ShowPopupWithURL(const GURL
& popup_url
,
90 ExtensionPopup::ShowAction show_action
);
92 // Hides the active popup, if there is one.
95 // The location bar view that owns us.
96 LocationBarView
* owner_
;
98 // The PageAction that this view represents. The PageAction is not owned by
99 // us, it resides in the extension of this particular profile.
100 ExtensionAction
* page_action_
;
102 // The corresponding browser.
105 // The object that will be used to get the page action icon for us.
106 // It may load the icon asynchronously (in which case the initial icon
107 // returned by the factory will be transparent), so we have to observe it for
108 // updates to the icon.
109 scoped_ptr
<ExtensionActionIconFactory
> icon_factory_
;
111 // The tab id we are currently showing the icon for.
114 // The URL we are currently showing the icon for.
117 // The string to show for a tooltip;
118 std::string tooltip_
;
120 // This is used for post-install visual feedback. The page_action icon is
121 // briefly shown even if it hasn't been enabled by its extension.
122 bool preview_enabled_
;
124 // The current popup and the button it came from. NULL if no popup.
125 ExtensionPopup
* popup_
;
127 // The extension command accelerator this page action is listening for (to
129 scoped_ptr
<ui::Accelerator
> page_action_keybinding_
;
131 scoped_ptr
<views::MenuRunner
> menu_runner_
;
133 DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView
);
136 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_