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

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: merge conflict 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_controller.h"
9 #include "chrome/browser/extensions/extension_context_menu_model.h" 9 #include "chrome/browser/ui/views/extensions/extension_action_view_delegate.h"
10 #include "chrome/browser/ui/views/extensions/extension_popup.h"
11 #include "content/public/browser/notification_observer.h" 10 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h" 11 #include "content/public/browser/notification_registrar.h"
13 #include "ui/views/context_menu_controller.h"
14 #include "ui/views/controls/button/menu_button.h" 12 #include "ui/views/controls/button/menu_button.h"
15 #include "ui/views/controls/button/menu_button_listener.h" 13 #include "ui/views/controls/button/menu_button_listener.h"
16 #include "ui/views/drag_controller.h" 14 #include "ui/views/drag_controller.h"
17 #include "ui/views/view.h" 15 #include "ui/views/view.h"
18 #include "ui/views/widget/widget_observer.h"
19 16
20 class Browser; 17 class Browser;
21 class BrowserActionButton; 18 class BrowserActionButton;
22 class ExtensionAction; 19 class ExtensionAction;
23 20
24 namespace extensions { 21 namespace extensions {
25 class Extension; 22 class Extension;
26 class ExtensionToolbarModel;
27 } 23 }
28 24
29 namespace gfx { 25 namespace gfx {
30 class Image; 26 class Image;
31 } 27 }
32 28
33 namespace views {
34 class MenuItemView;
35 class MenuRunner;
36 }
37
38 //////////////////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////////////////
39 // BrowserActionView 30 // BrowserActionView
40 // A single entry in the browser action container. This contains the actual 31 // A single entry in the browser action container. This contains the actual
41 // BrowserActionButton, as well as the logic to paint the badge. 32 // BrowserActionButton, as well as the logic to paint the badge.
42 class BrowserActionView : public views::View { 33 class BrowserActionView : public views::View {
43 public: 34 public:
44 // Need DragController here because BrowserActionView could be 35 // Need DragController here because BrowserActionView could be
45 // dragged/dropped. 36 // dragged/dropped.
46 class Delegate : public views::DragController { 37 class Delegate : public views::DragController {
47 public: 38 public:
48 // Returns the current tab's ID, or -1 if there is no current tab. 39 // Returns the current web contents.
49 virtual int GetCurrentTabId() const = 0; 40 virtual content::WebContents* GetCurrentWebContents() = 0;
50 41
51 // Called when a browser action becomes visible/hidden. 42 // Called when a browser action becomes visible/hidden.
52 virtual void OnBrowserActionVisibilityChanged() = 0; 43 virtual void OnBrowserActionVisibilityChanged() = 0;
53 44
54 // Whether the container for this button is shown inside a menu. 45 // Whether the container for this button is shown inside a menu.
55 virtual bool ShownInsideMenu() const = 0; 46 virtual bool ShownInsideMenu() const = 0;
56 47
57 // Notifies that a drag completed. Note this will only happen if the view 48 // 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 49 // 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 50 // was a move in the browser actions, since we re-create the views each
60 // time we re-order the browser actions). 51 // time we re-order the browser actions).
61 virtual void OnBrowserActionViewDragDone() = 0; 52 virtual void OnBrowserActionViewDragDone() = 0;
62 53
63 // Returns the view of the browser actions overflow menu to use as a 54 // 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. 55 // reference point for a popup when this view isn't visible.
65 virtual views::View* GetOverflowReferenceView() = 0; 56 virtual views::View* GetOverflowReferenceView() = 0;
66 57
67 // Sets the delegate's active popup owner to be |popup_owner|. 58 // Sets the delegate's active popup owner to be |popup_owner|.
68 virtual void SetPopupOwner(BrowserActionButton* popup_owner) = 0; 59 virtual void SetPopupOwner(BrowserActionButton* popup_owner) = 0;
69 60
70 // Hides the active popup of the delegate, if one exists. 61 // Hides the active popup of the delegate, if one exists.
71 virtual void HideActivePopup() = 0; 62 virtual void HideActivePopup() = 0;
72 63
73 // Returns the Model backing the browser actions.
74 virtual extensions::ExtensionToolbarModel* GetModel() = 0;
75
76 protected: 64 protected:
77 virtual ~Delegate() {} 65 virtual ~Delegate() {}
78 }; 66 };
79 67
80 BrowserActionView(const extensions::Extension* extension, 68 BrowserActionView(const extensions::Extension* extension,
81 Browser* browser, 69 Browser* browser,
82 Delegate* delegate); 70 Delegate* delegate);
83 virtual ~BrowserActionView(); 71 virtual ~BrowserActionView();
84 72
85 BrowserActionButton* button() { return button_; } 73 BrowserActionButton* button() { return button_.get(); }
86 74
87 // Gets browser action button icon with the badge. 75 // Gets browser action button icon with the badge.
88 gfx::ImageSkia GetIconWithBadge(); 76 gfx::ImageSkia GetIconWithBadge();
89 77
90 // Overridden from views::View: 78 // Overridden from views::View:
91 virtual void Layout() OVERRIDE; 79 virtual void Layout() OVERRIDE;
92 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 80 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
93 virtual gfx::Size GetPreferredSize() const OVERRIDE; 81 virtual gfx::Size GetPreferredSize() const OVERRIDE;
94 82
95 protected: 83 protected:
96 // Overridden from views::View to paint the badge on top of children. 84 // Overridden from views::View to paint the badge on top of children.
97 virtual void PaintChildren(gfx::Canvas* canvas, 85 virtual void PaintChildren(gfx::Canvas* canvas,
98 const views::CullSet& cull_set) OVERRIDE; 86 const views::CullSet& cull_set) OVERRIDE;
99 87
100 private: 88 private:
101 // The Browser object this view is associated with.
102 Browser* browser_;
103
104 // Usually a container for this view. 89 // Usually a container for this view.
105 Delegate* delegate_; 90 Delegate* delegate_;
106 91
107 // The button this view contains. 92 // The button this view contains.
108 BrowserActionButton* button_; 93 scoped_ptr<BrowserActionButton> button_;
109
110 // Extension this view associated with.
111 const extensions::Extension* extension_;
112 94
113 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); 95 DISALLOW_COPY_AND_ASSIGN(BrowserActionView);
114 }; 96 };
115 97
116 //////////////////////////////////////////////////////////////////////////////// 98 ////////////////////////////////////////////////////////////////////////////////
117 // BrowserActionButton 99 // BrowserActionButton
118 100
119 // The BrowserActionButton is a specialization of the MenuButton class. 101 // The BrowserActionButton is a specialization of the MenuButton class.
120 // It acts on a ExtensionAction, in this case a BrowserAction and handles 102 // This wraps an ExtensionActionView, and has knowledge of how to render itself
121 // loading the image for the button asynchronously on the file thread. 103 // and when to trigger the extension action.
122 class BrowserActionButton : public views::MenuButton, 104 class BrowserActionButton : public views::MenuButton,
105 public ExtensionActionViewDelegate,
123 public views::ButtonListener, 106 public views::ButtonListener,
124 public views::ContextMenuController, 107 public content::NotificationObserver {
125 public content::NotificationObserver,
126 public ExtensionActionIconFactory::Observer,
127 public views::WidgetObserver,
128 public ExtensionContextMenuModel::PopupDelegate {
129 public: 108 public:
130 // The IconObserver will receive a notification when the button's icon has 109 // The IconObserver will receive a notification when the button's icon has
131 // been updated. 110 // been updated.
132 class IconObserver { 111 class IconObserver {
133 public: 112 public:
134 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0; 113 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0;
135 114
136 protected: 115 protected:
137 virtual ~IconObserver() {} 116 virtual ~IconObserver() {}
138 }; 117 };
139 118
140 BrowserActionButton(const extensions::Extension* extension, 119 BrowserActionButton(const extensions::Extension* extension,
141 Browser* browser_, 120 Browser* browser,
142 BrowserActionView::Delegate* delegate); 121 BrowserActionView::Delegate* delegate);
122 virtual ~BrowserActionButton();
143 123
144 // Call this instead of delete. 124 const extensions::Extension* extension() const {
145 void Destroy(); 125 return view_controller_->extension();
146 126 }
147 ExtensionAction* browser_action() const { return browser_action_; } 127 ExtensionAction* extension_action() {
148 const extensions::Extension* extension() { return extension_; } 128 return view_controller_->extension_action();
149 129 }
130 ExtensionActionViewController* view_controller() {
131 return view_controller_.get();
132 }
150 void set_icon_observer(IconObserver* icon_observer) { 133 void set_icon_observer(IconObserver* icon_observer) {
151 icon_observer_ = icon_observer; 134 icon_observer_ = icon_observer;
152 } 135 }
153 136
154 // Called to update the display to match the browser action's state. 137 // Called to update the display to match the browser action's state.
155 void UpdateState(); 138 void UpdateState();
156 139
157 // Does this button's action have a popup? 140 // Does this button's action have a popup?
158 virtual bool IsPopup(); 141 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 142
173 // Overridden from views::View: 143 // Overridden from views::View:
174 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.
209 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
210
211 // ExtensionContextMenuModel::PopupDelegate:
212 virtual void InspectPopup() OVERRIDE;
213
214 // Notifications when to set button state to pushed/not pushed (for when the 169 // Notifications when to set button state to pushed/not pushed (for when the
215 // popup/context menu is hidden or shown by the container). 170 // popup/context menu is hidden or shown by the container).
216 void SetButtonPushed(); 171 void SetButtonPushed();
217 void SetButtonNotPushed(); 172 void SetButtonNotPushed();
218 173
219 // Whether the browser action is enabled on this tab. Note that we cannot use 174 // 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 175 // the built-in views enabled/SetEnabled because disabled views do not
221 // receive drag events. 176 // receive drag events.
222 bool IsEnabled(int tab_id) const; 177 bool IsEnabled(int tab_id) const;
223 178
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. 179 // Gets the icon of this button and its badge.
229 gfx::ImageSkia GetIconWithBadge(); 180 gfx::ImageSkia GetIconWithBadge();
230 181
231 // Returns button icon so it can be accessed during tests. 182 // Returns button icon so it can be accessed during tests.
232 gfx::ImageSkia GetIconForTest(); 183 gfx::ImageSkia GetIconForTest();
233 184
234 protected: 185 protected:
235 // Overridden from views::View: 186 // Overridden from views::View:
236 virtual void ViewHierarchyChanged( 187 virtual void ViewHierarchyChanged(
237 const ViewHierarchyChangedDetails& details) OVERRIDE; 188 const ViewHierarchyChangedDetails& details) OVERRIDE;
238 virtual void OnDragDone() OVERRIDE; 189 virtual void OnDragDone() OVERRIDE;
239 190
240 private: 191 private:
241 virtual ~BrowserActionButton(); 192 // ExtensionActionViewDelegate:
193 virtual views::View* GetAsView() OVERRIDE;
194 virtual bool IsShownInMenu() OVERRIDE;
195 virtual views::FocusManager* GetFocusManagerForAccelerator() OVERRIDE;
196 virtual views::Widget* GetParentForContextMenu() OVERRIDE;
197 virtual views::View* GetReferenceViewForPopup() OVERRIDE;
198 virtual content::WebContents* GetCurrentWebContents() OVERRIDE;
199 virtual void HideActivePopup() OVERRIDE;
200 virtual void OnIconUpdated() OVERRIDE;
201 virtual void OnPopupShown(bool grant_tab_permissions) OVERRIDE;
202 virtual void CleanupPopup() OVERRIDE;
203 virtual void OnWillShowContextMenus() OVERRIDE;
204 virtual void OnContextMenuDone() OVERRIDE;
242 205
243 // views::WidgetObserver: 206 // The controller for this ExtensionAction view.
244 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; 207 scoped_ptr<ExtensionActionViewController> view_controller_;
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 208
274 // Delegate that usually represents a container for BrowserActionView. 209 // Delegate that usually represents a container for BrowserActionView.
275 BrowserActionView::Delegate* delegate_; 210 BrowserActionView::Delegate* delegate_;
276 211
277 // Used to make sure MaybeRegisterExtensionCommand() is called only once 212 // Used to make sure we only register the command once.
278 // from ViewHierarchyChanged().
279 bool called_registered_extension_command_; 213 bool called_registered_extension_command_;
280 214
281 content::NotificationRegistrar registrar_; 215 content::NotificationRegistrar registrar_;
282 216
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 217 // The observer that we need to notify when the icon of the button has been
294 // updated. 218 // updated.
295 IconObserver* icon_observer_; 219 IconObserver* icon_observer_;
296 220
297 friend class base::DeleteHelper<BrowserActionButton>;
298
299 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); 221 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton);
300 }; 222 };
301 223
302 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ 224 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698