page_action_image_view.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
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_
7
8#include <map>
9#include <string>
10
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"
19
20class Browser;
21class LocationBarView;
22
23namespace content {
24class WebContents;
25}
26namespace views {
27class MenuRunner;
28}
29
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.
32class PageActionImageView : public views::ImageView,
33                            public ExtensionContextMenuModel::PopupDelegate,
34                            public views::WidgetObserver,
35                            public views::ContextMenuController,
36                            public ExtensionActionIconFactory::Observer {
37 public:
38  PageActionImageView(LocationBarView* owner,
39                      ExtensionAction* page_action,
40                      Browser* browser);
41  virtual ~PageActionImageView();
42
43  ExtensionAction* page_action() { return page_action_; }
44
45  int current_tab_id() { return current_tab_id_; }
46
47  void set_preview_enabled(bool preview_enabled) {
48    preview_enabled_ = preview_enabled;
49  }
50
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;
56
57  // Overridden from ExtensionContextMenuModel::Delegate
58  virtual void InspectPopup(ExtensionAction* action) OVERRIDE;
59
60  // Overridden from views::WidgetObserver:
61  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
62
63  // Overridden from views::ContextMenuController.
64  virtual void ShowContextMenuForView(View* source,
65                                      const gfx::Point& point,
66                                      ui::MenuSourceType source_type) OVERRIDE;
67
68  // Overriden from ExtensionActionIconFactory::Observer.
69  virtual void OnIconUpdated() OVERRIDE;
70
71  // Overridden from ui::AcceleratorTarget:
72  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
73  virtual bool CanHandleAccelerators() const OVERRIDE;
74
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);
79
80  // Either notify listeners or show a popup depending on the page action.
81  void ExecuteAction(ExtensionPopup::ShowAction show_action);
82
83 private:
84  // Overridden from View.
85  virtual void PaintChildren(gfx::Canvas* canvas,
86                             const views::CullSet& cull_set) OVERRIDE;
87
88  // Shows the popup, with the given URL.
89  void ShowPopupWithURL(const GURL& popup_url,
90                        ExtensionPopup::ShowAction show_action);
91
92  // Hides the active popup, if there is one.
93  void HidePopup();
94
95  // The location bar view that owns us.
96  LocationBarView* owner_;
97
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_;
101
102  // The corresponding browser.
103  Browser* browser_;
104
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_;
110
111  // The tab id we are currently showing the icon for.
112  int current_tab_id_;
113
114  // The URL we are currently showing the icon for.
115  GURL current_url_;
116
117  // The string to show for a tooltip;
118  std::string tooltip_;
119
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_;
123
124  // The current popup and the button it came from.  NULL if no popup.
125  ExtensionPopup* popup_;
126
127  // The extension command accelerator this page action is listening for (to
128  // show the popup).
129  scoped_ptr<ui::Accelerator> page_action_keybinding_;
130
131  scoped_ptr<views::MenuRunner> menu_runner_;
132
133  DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView);
134};
135
136#endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
137