Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: chrome/browser/ui/views/toolbar/browser_action_view.h

Issue 431173002: Create ExtensionActionView class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ 5 #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ 6 #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_
7 7
8 #include "chrome/browser/extensions/extension_action_icon_factory.h" 8 #include "chrome/browser/ui/views/extensions/extension_action_view.h"
9 #include "chrome/browser/extensions/extension_context_menu_model.h"
10 #include "chrome/browser/ui/views/extensions/extension_popup.h"
11 #include "content/public/browser/notification_observer.h" 9 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h" 10 #include "content/public/browser/notification_registrar.h"
13 #include "ui/views/context_menu_controller.h"
14 #include "ui/views/controls/button/menu_button.h" 11 #include "ui/views/controls/button/menu_button.h"
15 #include "ui/views/controls/button/menu_button_listener.h" 12 #include "ui/views/controls/button/menu_button_listener.h"
16 #include "ui/views/drag_controller.h" 13 #include "ui/views/drag_controller.h"
17 #include "ui/views/view.h" 14 #include "ui/views/view.h"
18 #include "ui/views/widget/widget_observer.h"
19 15
20 class Browser; 16 class Browser;
21 class BrowserActionButton; 17 class BrowserActionButton;
22 class ExtensionAction; 18 class ExtensionAction;
23 19
24 namespace extensions { 20 namespace extensions {
25 class Extension; 21 class Extension;
26 class ExtensionToolbarModel;
27 } 22 }
28 23
29 namespace gfx { 24 namespace gfx {
30 class Image; 25 class Image;
31 } 26 }
32 27
33 namespace views {
34 class MenuItemView;
35 class MenuRunner;
36 }
37
38 //////////////////////////////////////////////////////////////////////////////// 28 ////////////////////////////////////////////////////////////////////////////////
39 // BrowserActionView 29 // BrowserActionView
40 // A single entry in the browser action container. This contains the actual 30 // A single entry in the browser action container. This contains the actual
41 // BrowserActionButton, as well as the logic to paint the badge. 31 // BrowserActionButton, as well as the logic to paint the badge.
42 class BrowserActionView : public views::View { 32 class BrowserActionView : public views::View {
43 public: 33 public:
44 // Need DragController here because BrowserActionView could be 34 // Need DragController here because BrowserActionView could be
45 // dragged/dropped. 35 // dragged/dropped.
46 class Delegate : public views::DragController { 36 class Delegate : public views::DragController {
47 public: 37 public:
48 // Returns the current tab's ID, or -1 if there is no current tab. 38 // Returns the current web contents.
49 virtual int GetCurrentTabId() const = 0; 39 virtual content::WebContents* GetCurrentWebContents() = 0;
50 40
51 // Called when a browser action becomes visible/hidden. 41 // Called when a browser action becomes visible/hidden.
52 virtual void OnBrowserActionVisibilityChanged() = 0; 42 virtual void OnBrowserActionVisibilityChanged() = 0;
53 43
54 // Whether the container for this button is shown inside a menu. 44 // Whether the container for this button is shown inside a menu.
55 virtual bool ShownInsideMenu() const = 0; 45 virtual bool ShownInsideMenu() const = 0;
56 46
57 // Notifies that a drag completed. Note this will only happen if the view 47 // Notifies that a drag completed. Note this will only happen if the view
58 // wasn't removed during the drag-and-drop process (i.e., not when there 48 // wasn't removed during the drag-and-drop process (i.e., not when there
59 // was a move in the browser actions, since we re-create the views each 49 // was a move in the browser actions, since we re-create the views each
60 // time we re-order the browser actions). 50 // time we re-order the browser actions).
61 virtual void OnBrowserActionViewDragDone() = 0; 51 virtual void OnBrowserActionViewDragDone() = 0;
62 52
63 // Returns the view of the browser actions overflow menu to use as a 53 // Returns the view of the browser actions overflow menu to use as a
64 // reference point for a popup when this view isn't visible. 54 // reference point for a popup when this view isn't visible.
65 virtual views::View* GetOverflowReferenceView() = 0; 55 virtual views::View* GetOverflowReferenceView() = 0;
66 56
67 // Sets the delegate's active popup owner to be |popup_owner|. 57 // Sets the delegate's active popup owner to be |popup_owner|.
68 virtual void SetPopupOwner(BrowserActionButton* popup_owner) = 0; 58 virtual void SetPopupOwner(BrowserActionButton* popup_owner) = 0;
69 59
70 // Hides the active popup of the delegate, if one exists. 60 // Hides the active popup of the delegate, if one exists.
71 virtual void HideActivePopup() = 0; 61 virtual void HideActivePopup() = 0;
72 62
73 // Returns the Model backing the browser actions.
74 virtual extensions::ExtensionToolbarModel* GetModel() = 0;
75
76 protected: 63 protected:
77 virtual ~Delegate() {} 64 virtual ~Delegate() {}
78 }; 65 };
79 66
80 BrowserActionView(const extensions::Extension* extension, 67 BrowserActionView(const extensions::Extension* extension,
81 Browser* browser, 68 Browser* browser,
82 Delegate* delegate); 69 Delegate* delegate);
83 virtual ~BrowserActionView(); 70 virtual ~BrowserActionView();
84 71
85 BrowserActionButton* button() { return button_; } 72 BrowserActionButton* button() { return button_; }
86 73
87 // Gets browser action button icon with the badge. 74 // Gets browser action button icon with the badge.
88 gfx::ImageSkia GetIconWithBadge(); 75 gfx::ImageSkia GetIconWithBadge();
89 76
90 // Overridden from views::View: 77 // Overridden from views::View:
91 virtual void Layout() OVERRIDE; 78 virtual void Layout() OVERRIDE;
92 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 79 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
93 virtual gfx::Size GetPreferredSize() const OVERRIDE; 80 virtual gfx::Size GetPreferredSize() const OVERRIDE;
94 81
95 protected: 82 protected:
96 // Overridden from views::View to paint the badge on top of children. 83 // Overridden from views::View to paint the badge on top of children.
97 virtual void PaintChildren(gfx::Canvas* canvas, 84 virtual void PaintChildren(gfx::Canvas* canvas,
98 const views::CullSet& cull_set) OVERRIDE; 85 const views::CullSet& cull_set) OVERRIDE;
99 86
100 private: 87 private:
101 // The Browser object this view is associated with.
102 Browser* browser_;
103
104 // Usually a container for this view. 88 // Usually a container for this view.
105 Delegate* delegate_; 89 Delegate* delegate_;
106 90
107 // The button this view contains. 91 // The button this view contains.
108 BrowserActionButton* button_; 92 BrowserActionButton* button_;
sky 2014/08/05 22:29:47 Wrap in scoped_ptr now? If delete needs to be run
Devlin 2014/08/05 23:51:10 Done.
109 93
110 // Extension this view associated with.
111 const extensions::Extension* extension_;
112
113 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); 94 DISALLOW_COPY_AND_ASSIGN(BrowserActionView);
114 }; 95 };
115 96
116 //////////////////////////////////////////////////////////////////////////////// 97 ////////////////////////////////////////////////////////////////////////////////
117 // BrowserActionButton 98 // BrowserActionButton
118 99
119 // The BrowserActionButton is a specialization of the MenuButton class. 100 // The BrowserActionButton is a specialization of the MenuButton class.
120 // It acts on a ExtensionAction, in this case a BrowserAction and handles 101 // This wraps an ExtensionActionView, and has knowledge of how to render itself
121 // loading the image for the button asynchronously on the file thread. 102 // and when to trigger the extension action.
122 class BrowserActionButton : public views::MenuButton, 103 class BrowserActionButton : public views::MenuButton,
104 public ExtensionActionView::ViewDelegate,
123 public views::ButtonListener, 105 public views::ButtonListener,
124 public views::ContextMenuController, 106 public content::NotificationObserver {
125 public content::NotificationObserver,
126 public ExtensionActionIconFactory::Observer,
127 public views::WidgetObserver,
128 public ExtensionContextMenuModel::PopupDelegate {
129 public: 107 public:
130 // The IconObserver will receive a notification when the button's icon has 108 // The IconObserver will receive a notification when the button's icon has
131 // been updated. 109 // been updated.
132 class IconObserver { 110 class IconObserver {
133 public: 111 public:
134 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0; 112 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0;
135 113
136 protected: 114 protected:
137 virtual ~IconObserver() {} 115 virtual ~IconObserver() {}
138 }; 116 };
139 117
140 BrowserActionButton(const extensions::Extension* extension, 118 BrowserActionButton(const extensions::Extension* extension,
141 Browser* browser_, 119 Browser* browser,
142 BrowserActionView::Delegate* delegate); 120 BrowserActionView::Delegate* delegate);
121 virtual ~BrowserActionButton();
143 122
144 // Call this instead of delete. 123 const extensions::Extension* extension() const {
145 void Destroy(); 124 return extension_action_view_->extension();
146 125 }
147 ExtensionAction* browser_action() const { return browser_action_; } 126 ExtensionAction* extension_action() {
148 const extensions::Extension* extension() { return extension_; } 127 return extension_action_view_->extension_action();
149 128 }
129 ExtensionActionView* extension_action_view() {
130 return extension_action_view_.get();
131 }
150 void set_icon_observer(IconObserver* icon_observer) { 132 void set_icon_observer(IconObserver* icon_observer) {
151 icon_observer_ = icon_observer; 133 icon_observer_ = icon_observer;
152 } 134 }
153 135
154 // Called to update the display to match the browser action's state. 136 // Called to update the display to match the browser action's state.
155 void UpdateState(); 137 void UpdateState();
156 138
157 // Does this button's action have a popup? 139 // Does this button's action have a popup?
158 virtual bool IsPopup(); 140 bool IsPopup();
159 virtual GURL GetPopupUrl();
160
161 // Show this extension's popup. If |grant_tab_permissions| is true, this will
162 // grant the extension active tab permissions. Only do this if this was done
163 // through a user action (and not e.g. an API).
164 bool ShowPopup(ExtensionPopup::ShowAction show_action,
165 bool grant_tab_permissions);
166
167 // Hides the popup, if one is open.
168 void HidePopup();
169
170 // Executes the browser action (and also shows the popup, if one exists).
171 void ExecuteBrowserAction();
172 141
173 // Overridden from views::View: 142 // Overridden from views::View:
174 virtual bool CanHandleAccelerators() const OVERRIDE; 143 virtual bool CanHandleAccelerators() const OVERRIDE;
175 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 144 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
176 145
177 // Overridden from views::ButtonListener: 146 // Overridden from views::ButtonListener:
178 virtual void ButtonPressed(views::Button* sender, 147 virtual void ButtonPressed(views::Button* sender,
179 const ui::Event& event) OVERRIDE; 148 const ui::Event& event) OVERRIDE;
180 149
181 // Overridden from views::ContextMenuController.
182 virtual void ShowContextMenuForView(View* source,
183 const gfx::Point& point,
184 ui::MenuSourceType source_type) OVERRIDE;
185
186 // Overridden from content::NotificationObserver: 150 // Overridden from content::NotificationObserver:
187 virtual void Observe(int type, 151 virtual void Observe(int type,
188 const content::NotificationSource& source, 152 const content::NotificationSource& source,
189 const content::NotificationDetails& details) OVERRIDE; 153 const content::NotificationDetails& details) OVERRIDE;
190 154
191 // Overriden from ExtensionActionIconFactory::Observer.
192 virtual void OnIconUpdated() OVERRIDE;
193
194 // MenuButton behavior overrides. These methods all default to LabelButton 155 // MenuButton behavior overrides. These methods all default to LabelButton
195 // behavior unless this button is a popup. In that case, it uses MenuButton 156 // behavior unless this button is a popup. In that case, it uses MenuButton
196 // behavior. MenuButton has the notion of a child popup being shown where the 157 // behavior. MenuButton has the notion of a child popup being shown where the
197 // button will stay in the pushed state until the "menu" (a popup in this 158 // button will stay in the pushed state until the "menu" (a popup in this
198 // case) is dismissed. 159 // case) is dismissed.
199 virtual bool Activate() OVERRIDE; 160 virtual bool Activate() OVERRIDE;
200 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 161 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
201 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 162 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
202 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 163 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
203 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; 164 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
204 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 165 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
205 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const 166 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const
206 OVERRIDE; 167 OVERRIDE;
207 168
208 // Overridden from ui::AcceleratorTarget. 169 // Overridden from ui::AcceleratorTarget.
209 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 170 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
210 171
211 // ExtensionContextMenuModel::PopupDelegate:
212 virtual void InspectPopup() OVERRIDE;
213
214 // Notifications when to set button state to pushed/not pushed (for when the 172 // Notifications when to set button state to pushed/not pushed (for when the
215 // popup/context menu is hidden or shown by the container). 173 // popup/context menu is hidden or shown by the container).
216 void SetButtonPushed(); 174 void SetButtonPushed();
217 void SetButtonNotPushed(); 175 void SetButtonNotPushed();
218 176
219 // Whether the browser action is enabled on this tab. Note that we cannot use 177 // Whether the browser action is enabled on this tab. Note that we cannot use
220 // the built-in views enabled/SetEnabled because disabled views do not 178 // the built-in views enabled/SetEnabled because disabled views do not
221 // receive drag events. 179 // receive drag events.
222 bool IsEnabled(int tab_id) const; 180 bool IsEnabled(int tab_id) const;
223 181
224 // Accessors.
225 ExtensionActionIconFactory& icon_factory() { return icon_factory_; }
226 ExtensionPopup* popup() { return popup_; }
227
228 // Gets the icon of this button and its badge. 182 // Gets the icon of this button and its badge.
229 gfx::ImageSkia GetIconWithBadge(); 183 gfx::ImageSkia GetIconWithBadge();
230 184
231 // Returns button icon so it can be accessed during tests. 185 // Returns button icon so it can be accessed during tests.
232 gfx::ImageSkia GetIconForTest(); 186 gfx::ImageSkia GetIconForTest();
233 187
234 protected: 188 protected:
235 // Overridden from views::View: 189 // Overridden from views::View:
236 virtual void ViewHierarchyChanged( 190 virtual void ViewHierarchyChanged(
237 const ViewHierarchyChangedDetails& details) OVERRIDE; 191 const ViewHierarchyChangedDetails& details) OVERRIDE;
238 virtual void OnDragDone() OVERRIDE; 192 virtual void OnDragDone() OVERRIDE;
239 193
240 private: 194 private:
241 virtual ~BrowserActionButton(); 195 // ExtensionActionView::ViewDelegate:
196 virtual views::View* GetAsView() OVERRIDE;
197 virtual bool IsNestedView() OVERRIDE;
198 virtual views::FocusManager* GetFocusManagerForAccelerator() OVERRIDE;
199 virtual views::Widget* GetParentForContextMenu() OVERRIDE;
200 virtual views::View* GetReferenceViewForPopup() OVERRIDE;
201 virtual content::WebContents* GetCurrentWebContents() OVERRIDE;
202 virtual void HideActivePopup() OVERRIDE;
203 virtual void OnIconUpdated() OVERRIDE;
204 virtual void OnPopupShown(bool grant_tab_permissions) OVERRIDE;
205 virtual void CleanupPopup() OVERRIDE;
206 virtual void OnWillShowContextMenus() OVERRIDE;
207 virtual void OnContextMenuDone() OVERRIDE;
242 208
243 // views::WidgetObserver: 209 // The ExtensionActionView implementation.
244 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; 210 scoped_ptr<ExtensionActionView> extension_action_view_;
245
246 // Register an extension command if the extension has an active one.
247 void MaybeRegisterExtensionCommand();
248
249 // Unregisters an extension command, if the extension has registered one and
250 // it is active.
251 void MaybeUnregisterExtensionCommand(bool only_if_active);
252
253 // Cleans up after the popup. If |close_widget| is true, this will call
254 // Widget::Close() on the popup's widget; otherwise it assumes the popup is
255 // already closing.
256 void CleanupPopup(bool close_widget);
257
258 // The Browser object this button is associated with.
259 Browser* browser_;
260
261 // The browser action this view represents. The ExtensionAction is not owned
262 // by this class.
263 ExtensionAction* browser_action_;
264
265 // The extension associated with the browser action we're displaying.
266 const extensions::Extension* extension_;
267
268 // The object that will be used to get the browser action icon for us.
269 // It may load the icon asynchronously (in which case the initial icon
270 // returned by the factory will be transparent), so we have to observe it for
271 // updates to the icon.
272 ExtensionActionIconFactory icon_factory_;
273 211
274 // Delegate that usually represents a container for BrowserActionView. 212 // Delegate that usually represents a container for BrowserActionView.
275 BrowserActionView::Delegate* delegate_; 213 BrowserActionView::Delegate* delegate_;
276 214
277 // Used to make sure MaybeRegisterExtensionCommand() is called only once 215 // Used to make sure we only register the command once.
278 // from ViewHierarchyChanged().
279 bool called_registered_extension_command_; 216 bool called_registered_extension_command_;
280 217
281 content::NotificationRegistrar registrar_; 218 content::NotificationRegistrar registrar_;
282 219
283 // The extension key binding accelerator this browser action is listening for
284 // (to show the popup).
285 scoped_ptr<ui::Accelerator> keybinding_;
286
287 // Responsible for running the menu.
288 scoped_ptr<views::MenuRunner> menu_runner_;
289
290 // The browser action's popup, if it is visible; NULL otherwise.
291 ExtensionPopup* popup_;
292
293 // The observer that we need to notify when the icon of the button has been 220 // The observer that we need to notify when the icon of the button has been
294 // updated. 221 // updated.
295 IconObserver* icon_observer_; 222 IconObserver* icon_observer_;
296 223
297 friend class base::DeleteHelper<BrowserActionButton>;
298
299 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); 224 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton);
300 }; 225 };
301 226
302 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ 227 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698