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

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

Issue 661493004: Add infrastructure for Chrome Actions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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/ui/views/extensions/extension_action_view_controller.h" 8 #include "chrome/browser/ui/views/extensions/extension_action_view_controller.h"
9 #include "chrome/browser/ui/views/extensions/extension_action_view_delegate.h" 9 #include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate.h"
10 #include "content/public/browser/notification_observer.h" 10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h" 11 #include "content/public/browser/notification_registrar.h"
12 #include "ui/views/controls/button/menu_button.h" 12 #include "ui/views/controls/button/menu_button.h"
13 #include "ui/views/controls/button/menu_button_listener.h" 13 #include "ui/views/controls/button/menu_button_listener.h"
14 #include "ui/views/drag_controller.h" 14 #include "ui/views/drag_controller.h"
15 #include "ui/views/view.h" 15 #include "ui/views/view.h"
16 16
17 class Browser; 17 class Browser;
18 class ExtensionAction; 18 class ExtensionAction;
19 19
20 namespace extensions { 20 namespace extensions {
21 class Extension; 21 class Extension;
22 } 22 }
23 23
24 namespace gfx { 24 namespace gfx {
25 class Image; 25 class Image;
26 } 26 }
27 27
28 //////////////////////////////////////////////////////////////////////////////// 28 ////////////////////////////////////////////////////////////////////////////////
29 // BrowserActionView 29 // BrowserActionView
30 // A wrapper around an ExtensionActionViewController to display an extension 30 // A wrapper around an ExtensionActionViewController to display an extension
31 // action in the BrowserActionsContainer. 31 // action in the BrowserActionsContainer.
32 // Despite its name, this class can handle either Browser Actions or Page 32 // Despite its name, this class can handle any type of toolbar action, including
33 // Actions. 33 // extension actions (browser and page actions) and chrome actions.
34 // TODO(devlin): Rename this and BrowserActionsContainer when more of the 34 // TODO(devlin): Rename this and BrowserActionsContainer when more of the
35 // toolbar redesign is done. 35 // toolbar redesign is done.
36 class BrowserActionView : public views::MenuButton, 36 class BrowserActionView : public views::MenuButton,
37 public ExtensionActionViewDelegate, 37 public ToolbarActionViewDelegate,
38 public views::ButtonListener, 38 public views::ButtonListener,
39 public content::NotificationObserver { 39 public content::NotificationObserver {
40 public: 40 public:
41 // Need DragController here because BrowserActionView could be 41 // Need DragController here because BrowserActionView could be
42 // dragged/dropped. 42 // dragged/dropped.
43 class Delegate : public views::DragController { 43 class Delegate : public views::DragController {
44 public: 44 public:
45 // Returns the current web contents. 45 // Returns the current web contents.
46 virtual content::WebContents* GetCurrentWebContents() = 0; 46 virtual content::WebContents* GetCurrentWebContents() = 0;
47 47
(...skipping 11 matching lines...) Expand all
59 virtual views::MenuButton* GetOverflowReferenceView() = 0; 59 virtual views::MenuButton* GetOverflowReferenceView() = 0;
60 60
61 // Sets the delegate's active popup owner to be |popup_owner|. 61 // Sets the delegate's active popup owner to be |popup_owner|.
62 virtual void SetPopupOwner(BrowserActionView* popup_owner) = 0; 62 virtual void SetPopupOwner(BrowserActionView* popup_owner) = 0;
63 63
64 // Hides the active popup of the delegate, if one exists. 64 // Hides the active popup of the delegate, if one exists.
65 virtual void HideActivePopup() = 0; 65 virtual void HideActivePopup() = 0;
66 66
67 // Returns the primary BrowserActionView associated with the given 67 // Returns the primary BrowserActionView associated with the given
68 // |extension|. 68 // |extension|.
69 virtual BrowserActionView* GetMainViewForExtension( 69 virtual BrowserActionView* GetMainViewForAction(
70 const extensions::Extension* extension) = 0; 70 BrowserActionView* view) = 0;
71 71
72 protected: 72 protected:
73 virtual ~Delegate() {} 73 virtual ~Delegate() {}
74 }; 74 };
75 75
76 // The IconObserver will receive a notification when the button's icon has 76 // The type of action this view is showing.
77 // been updated. 77 enum Type {
78 class IconObserver { 78 TYPE_EXTENSION_ACTION,
79 public: 79 TYPE_CHROME_ACTION,
80 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0;
81
82 protected:
83 virtual ~IconObserver() {}
84 }; 80 };
85 81
86 BrowserActionView(const extensions::Extension* extension, 82 BrowserActionView(scoped_ptr<ToolbarActionViewController> view_controller,
87 ExtensionAction* extension_action, 83 Type type,
88 Browser* browser, 84 Browser* browser,
89 BrowserActionView::Delegate* delegate); 85 Delegate* delegate);
90 virtual ~BrowserActionView(); 86 virtual ~BrowserActionView();
91 87
92 const extensions::Extension* extension() const {
93 return view_controller_->extension();
94 }
95 ExtensionAction* extension_action() {
96 return view_controller_->extension_action();
97 }
98 ExtensionActionViewController* view_controller() {
99 return view_controller_.get();
100 }
101 void set_icon_observer(IconObserver* icon_observer) {
102 icon_observer_ = icon_observer;
103 }
104
105 // Called to update the display to match the browser action's state. 88 // Called to update the display to match the browser action's state.
106 void UpdateState(); 89 void UpdateState();
107 90
108 // Does this button's action have a popup?
109 bool IsPopup();
110
111 // Overridden from views::View: 91 // Overridden from views::View:
112 virtual void GetAccessibleState(ui::AXViewState* state) override; 92 virtual void GetAccessibleState(ui::AXViewState* state) override;
113 93
114 // Overridden from views::ButtonListener: 94 // Overridden from views::ButtonListener:
115 virtual void ButtonPressed(views::Button* sender, 95 virtual void ButtonPressed(views::Button* sender,
116 const ui::Event& event) override; 96 const ui::Event& event) override;
117 97
118 // Overridden from content::NotificationObserver: 98 // Overridden from content::NotificationObserver:
119 virtual void Observe(int type, 99 virtual void Observe(int type,
120 const content::NotificationSource& source, 100 const content::NotificationSource& source,
121 const content::NotificationDetails& details) override; 101 const content::NotificationDetails& details) override;
122 102
123 // MenuButton behavior overrides. These methods all default to LabelButton 103 // MenuButton behavior overrides. These methods all default to LabelButton
124 // behavior unless this button is a popup. In that case, it uses MenuButton 104 // behavior unless this button is a popup. In that case, it uses MenuButton
125 // behavior. MenuButton has the notion of a child popup being shown where the 105 // behavior. MenuButton has the notion of a child popup being shown where the
126 // button will stay in the pushed state until the "menu" (a popup in this 106 // button will stay in the pushed state until the "menu" (a popup in this
127 // case) is dismissed. 107 // case) is dismissed.
128 virtual bool Activate() override; 108 virtual bool Activate() override;
129 virtual bool OnMousePressed(const ui::MouseEvent& event) override; 109 virtual bool OnMousePressed(const ui::MouseEvent& event) override;
130 virtual void OnMouseReleased(const ui::MouseEvent& event) override; 110 virtual void OnMouseReleased(const ui::MouseEvent& event) override;
131 virtual void OnMouseExited(const ui::MouseEvent& event) override; 111 virtual void OnMouseExited(const ui::MouseEvent& event) override;
132 virtual bool OnKeyReleased(const ui::KeyEvent& event) override; 112 virtual bool OnKeyReleased(const ui::KeyEvent& event) override;
133 virtual void OnGestureEvent(ui::GestureEvent* event) override; 113 virtual void OnGestureEvent(ui::GestureEvent* event) override;
134 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const 114 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const
135 override; 115 override;
136 116
137 // Whether the browser action is enabled on this tab. Note that we cannot use 117 // ExtensionActionViewDelegate: (public because called by others).
138 // the built-in views enabled/SetEnabled because disabled views do not 118 virtual int GetCurrentTabId() const override;
139 // receive drag events.
140 bool IsEnabled(int tab_id) const;
141 119
142 // Gets the icon of this button and its badge. 120 // A convenience method to get the |view_controller_| as an
143 gfx::ImageSkia GetIconWithBadge(); 121 // ExtensionActionViewController. This will DCHECK if this isn't a view for
122 // an extension action.
123 ExtensionActionViewController* GetExtensionActionViewController();
124
125 ToolbarActionViewController* view_controller() {
126 return view_controller_.get();
127 }
128 Browser* browser() { return browser_; }
129 Type type() const { return type_; }
144 130
145 // Returns button icon so it can be accessed during tests. 131 // Returns button icon so it can be accessed during tests.
146 gfx::ImageSkia GetIconForTest(); 132 gfx::ImageSkia GetIconForTest();
147 133
148 private: 134 private:
149 // Overridden from views::View: 135 // Overridden from views::View:
150 virtual void ViewHierarchyChanged( 136 virtual void ViewHierarchyChanged(
151 const ViewHierarchyChangedDetails& details) override; 137 const ViewHierarchyChangedDetails& details) override;
152 virtual void OnDragDone() override; 138 virtual void OnDragDone() override;
153 virtual gfx::Size GetPreferredSize() const override; 139 virtual gfx::Size GetPreferredSize() const override;
154 virtual void PaintChildren(gfx::Canvas* canvas, 140 virtual void PaintChildren(gfx::Canvas* canvas,
155 const views::CullSet& cull_set) override; 141 const views::CullSet& cull_set) override;
156 142
157 // ExtensionActionViewDelegate: 143 // ExtensionActionViewDelegate:
sky 2014/10/15 23:52:30 ToolbarActionViewDelegate
Devlin 2014/10/16 16:46:38 Done.
158 virtual views::View* GetAsView() override; 144 virtual views::View* GetAsView() override;
159 virtual bool IsShownInMenu() override; 145 virtual bool IsShownInMenu() override;
160 virtual views::FocusManager* GetFocusManagerForAccelerator() override; 146 virtual views::FocusManager* GetFocusManagerForAccelerator() override;
161 virtual views::Widget* GetParentForContextMenu() override; 147 virtual views::Widget* GetParentForContextMenu() override;
162 virtual ExtensionActionViewController* GetPreferredPopupViewController() 148 virtual ToolbarActionViewController* GetPreferredPopupViewController()
163 override; 149 override;
164 virtual views::View* GetReferenceViewForPopup() override; 150 virtual views::View* GetReferenceViewForPopup() override;
165 virtual views::MenuButton* GetContextMenuButton() override; 151 virtual views::MenuButton* GetContextMenuButton() override;
166 virtual content::WebContents* GetCurrentWebContents() override;
167 virtual void HideActivePopup() override; 152 virtual void HideActivePopup() override;
168 virtual void OnIconUpdated() override; 153 virtual void OnIconUpdated() override;
169 virtual void OnPopupShown(bool grant_tab_permissions) override; 154 virtual void OnPopupShown(bool grant_tab_permissions) override;
170 virtual void CleanupPopup() override; 155 virtual void CleanupPopup() override;
171 156
172 // A lock to keep the MenuButton pressed when a menu or popup is visible. 157 // A lock to keep the MenuButton pressed when a menu or popup is visible.
173 // This needs to be destroyed after |view_controller_|, because 158 // This needs to be destroyed after |view_controller_|, because
174 // |view_controller_|'s destructor can call CleanupPopup(), which uses this 159 // |view_controller_|'s destructor can call CleanupPopup(), which uses this
175 // object. 160 // object.
176 scoped_ptr<views::MenuButton::PressedLock> pressed_lock_; 161 scoped_ptr<views::MenuButton::PressedLock> pressed_lock_;
177 162
178 // The controller for this ExtensionAction view. 163 // The controller for this toolbar action view.
179 scoped_ptr<ExtensionActionViewController> view_controller_; 164 scoped_ptr<ToolbarActionViewController> view_controller_;
165
166 // The type of action this view represents.
167 Type type_;
168
169 // The associated browser.
170 Browser* browser_;
180 171
181 // Delegate that usually represents a container for BrowserActionView. 172 // Delegate that usually represents a container for BrowserActionView.
182 BrowserActionView::Delegate* delegate_; 173 Delegate* delegate_;
183 174
184 // Used to make sure we only register the command once. 175 // Used to make sure we only register the command once.
185 bool called_registered_extension_command_; 176 bool called_register_command_;
186 177
187 content::NotificationRegistrar registrar_; 178 content::NotificationRegistrar registrar_;
188 179
189 // The observer that we need to notify when the icon of the button has been
190 // updated.
191 IconObserver* icon_observer_;
192
193 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); 180 DISALLOW_COPY_AND_ASSIGN(BrowserActionView);
194 }; 181 };
195 182
196 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ 183 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698