page_action_image_view.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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::AccessibleViewState* 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) OVERRIDE;
86
87  // Shows the popup, with the given URL.
88  void ShowPopupWithURL(const GURL& popup_url,
89                        ExtensionPopup::ShowAction show_action);
90
91  // Hides the active popup, if there is one.
92  void HidePopup();
93
94  // The location bar view that owns us.
95  LocationBarView* owner_;
96
97  // The PageAction that this view represents. The PageAction is not owned by
98  // us, it resides in the extension of this particular profile.
99  ExtensionAction* page_action_;
100
101  // The corresponding browser.
102  Browser* browser_;
103
104  // The object that will be used to get the page action icon for us.
105  // It may load the icon asynchronously (in which case the initial icon
106  // returned by the factory will be transparent), so we have to observe it for
107  // updates to the icon.
108  scoped_ptr<ExtensionActionIconFactory> icon_factory_;
109
110  // The tab id we are currently showing the icon for.
111  int current_tab_id_;
112
113  // The URL we are currently showing the icon for.
114  GURL current_url_;
115
116  // The string to show for a tooltip;
117  std::string tooltip_;
118
119  // This is used for post-install visual feedback. The page_action icon is
120  // briefly shown even if it hasn't been enabled by its extension.
121  bool preview_enabled_;
122
123  // The current popup and the button it came from.  NULL if no popup.
124  ExtensionPopup* popup_;
125
126  // The extension command accelerator this page action is listening for (to
127  // show the popup).
128  scoped_ptr<ui::Accelerator> page_action_keybinding_;
129
130  scoped_ptr<views::MenuRunner> menu_runner_;
131
132  DISALLOW_IMPLICIT_CONSTRUCTORS(PageActionImageView);
133};
134
135#endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PAGE_ACTION_IMAGE_VIEW_H_
136