OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_BROWSER_ACTION_VIEW_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 11 #include "chrome/browser/extensions/extension_context_menu_model.h" |
11 #include "chrome/browser/extensions/image_loading_tracker.h" | |
12 #include "content/public/browser/notification_observer.h" | 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
13 #include "ui/views/context_menu_controller.h" | 14 #include "ui/views/context_menu_controller.h" |
14 #include "ui/views/controls/button/menu_button.h" | 15 #include "ui/views/controls/button/menu_button.h" |
15 #include "ui/views/controls/button/menu_button_listener.h" | 16 #include "ui/views/controls/button/menu_button_listener.h" |
16 #include "ui/views/drag_controller.h" | 17 #include "ui/views/drag_controller.h" |
17 #include "ui/views/view.h" | 18 #include "ui/views/view.h" |
18 | 19 |
19 class Browser; | 20 class Browser; |
20 class BrowserActionButton; | 21 class BrowserActionButton; |
21 class ExtensionAction; | 22 class ExtensionAction; |
22 | 23 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 101 |
101 //////////////////////////////////////////////////////////////////////////////// | 102 //////////////////////////////////////////////////////////////////////////////// |
102 // BrowserActionButton | 103 // BrowserActionButton |
103 | 104 |
104 // The BrowserActionButton is a specialization of the MenuButton class. | 105 // The BrowserActionButton is a specialization of the MenuButton class. |
105 // It acts on a ExtensionAction, in this case a BrowserAction and handles | 106 // It acts on a ExtensionAction, in this case a BrowserAction and handles |
106 // loading the image for the button asynchronously on the file thread. | 107 // loading the image for the button asynchronously on the file thread. |
107 class BrowserActionButton : public views::MenuButton, | 108 class BrowserActionButton : public views::MenuButton, |
108 public views::ButtonListener, | 109 public views::ButtonListener, |
109 public views::ContextMenuController, | 110 public views::ContextMenuController, |
110 public ImageLoadingTracker::Observer, | 111 public content::NotificationObserver, |
111 public content::NotificationObserver { | 112 public ExtensionActionIconFactory::Observer { |
112 public: | 113 public: |
113 BrowserActionButton(const extensions::Extension* extension, | 114 BrowserActionButton(const extensions::Extension* extension, |
114 Browser* browser_, | 115 Browser* browser_, |
115 BrowserActionView::Delegate* delegate); | 116 BrowserActionView::Delegate* delegate); |
116 | 117 |
117 // Call this instead of delete. | 118 // Call this instead of delete. |
118 void Destroy(); | 119 void Destroy(); |
119 | 120 |
120 ExtensionAction* browser_action() const { return browser_action_; } | 121 ExtensionAction* browser_action() const { return browser_action_; } |
121 const extensions::Extension* extension() { return extension_; } | 122 const extensions::Extension* extension() { return extension_; } |
(...skipping 10 matching lines...) Expand all Loading... |
132 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | 133 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
133 | 134 |
134 // Overridden from views::ButtonListener: | 135 // Overridden from views::ButtonListener: |
135 virtual void ButtonPressed(views::Button* sender, | 136 virtual void ButtonPressed(views::Button* sender, |
136 const ui::Event& event) OVERRIDE; | 137 const ui::Event& event) OVERRIDE; |
137 | 138 |
138 // Overridden from views::ContextMenuController. | 139 // Overridden from views::ContextMenuController. |
139 virtual void ShowContextMenuForView(View* source, | 140 virtual void ShowContextMenuForView(View* source, |
140 const gfx::Point& point) OVERRIDE; | 141 const gfx::Point& point) OVERRIDE; |
141 | 142 |
142 // Overridden from ImageLoadingTracker. | |
143 virtual void OnImageLoaded(const gfx::Image& image, | |
144 const std::string& extension_id, | |
145 int index) OVERRIDE; | |
146 | |
147 // Overridden from content::NotificationObserver: | 143 // Overridden from content::NotificationObserver: |
148 virtual void Observe(int type, | 144 virtual void Observe(int type, |
149 const content::NotificationSource& source, | 145 const content::NotificationSource& source, |
150 const content::NotificationDetails& details) OVERRIDE; | 146 const content::NotificationDetails& details) OVERRIDE; |
151 | 147 |
| 148 // Overriden from ExtensionActionIconFactory::Observer. |
| 149 virtual void OnIconUpdated() OVERRIDE; |
| 150 |
152 // MenuButton behavior overrides. These methods all default to TextButton | 151 // MenuButton behavior overrides. These methods all default to TextButton |
153 // behavior unless this button is a popup. In that case, it uses MenuButton | 152 // behavior unless this button is a popup. In that case, it uses MenuButton |
154 // behavior. MenuButton has the notion of a child popup being shown where the | 153 // behavior. MenuButton has the notion of a child popup being shown where the |
155 // button will stay in the pushed state until the "menu" (a popup in this | 154 // button will stay in the pushed state until the "menu" (a popup in this |
156 // case) is dismissed. | 155 // case) is dismissed. |
157 virtual bool Activate() OVERRIDE; | 156 virtual bool Activate() OVERRIDE; |
158 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; | 157 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; |
159 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; | 158 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; |
160 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; | 159 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; |
161 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; | 160 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; |
162 | 161 |
163 // Overridden from ui::AcceleratorTarget. | 162 // Overridden from ui::AcceleratorTarget. |
164 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; | 163 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; |
165 | 164 |
166 // Notifications when to set button state to pushed/not pushed (for when the | 165 // Notifications when to set button state to pushed/not pushed (for when the |
167 // popup/context menu is hidden or shown by the container). | 166 // popup/context menu is hidden or shown by the container). |
168 void SetButtonPushed(); | 167 void SetButtonPushed(); |
169 void SetButtonNotPushed(); | 168 void SetButtonNotPushed(); |
170 | 169 |
171 // Whether the browser action is enabled on this tab. Note that we cannot use | 170 // Whether the browser action is enabled on this tab. Note that we cannot use |
172 // the built-in views enabled/SetEnabled because disabled views do not | 171 // the built-in views enabled/SetEnabled because disabled views do not |
173 // receive drag events. | 172 // receive drag events. |
174 bool IsEnabled(int tab_id) const; | 173 bool IsEnabled(int tab_id) const; |
175 | 174 |
| 175 // Returns icon factory for the button. |
| 176 ExtensionActionIconFactory& icon_factory() { return icon_factory_; } |
| 177 |
176 // Returns button icon so it can be accessed during tests. | 178 // Returns button icon so it can be accessed during tests. |
177 gfx::ImageSkia GetIconForTest(); | 179 gfx::ImageSkia GetIconForTest(); |
178 | 180 |
179 protected: | 181 protected: |
180 // Overridden from views::View: | 182 // Overridden from views::View: |
181 virtual void ViewHierarchyChanged(bool is_add, | 183 virtual void ViewHierarchyChanged(bool is_add, |
182 View* parent, | 184 View* parent, |
183 View* child) OVERRIDE; | 185 View* child) OVERRIDE; |
184 | 186 |
185 private: | 187 private: |
186 virtual ~BrowserActionButton(); | 188 virtual ~BrowserActionButton(); |
187 | 189 |
188 // Register an extension command if the extension has an active one. | 190 // Register an extension command if the extension has an active one. |
189 void MaybeRegisterExtensionCommand(); | 191 void MaybeRegisterExtensionCommand(); |
190 | 192 |
191 // Unregisters an extension command, if the extension has registered one and | 193 // Unregisters an extension command, if the extension has registered one and |
192 // it is active. | 194 // it is active. |
193 void MaybeUnregisterExtensionCommand(bool only_if_active); | 195 void MaybeUnregisterExtensionCommand(bool only_if_active); |
194 | 196 |
195 // The Browser object this button is associated with. | 197 // The Browser object this button is associated with. |
196 Browser* browser_; | 198 Browser* browser_; |
197 | 199 |
198 // The browser action this view represents. The ExtensionAction is not owned | 200 // The browser action this view represents. The ExtensionAction is not owned |
199 // by this class. | 201 // by this class. |
200 ExtensionAction* browser_action_; | 202 ExtensionAction* browser_action_; |
201 | 203 |
202 // The extension associated with the browser action we're displaying. | 204 // The extension associated with the browser action we're displaying. |
203 const extensions::Extension* extension_; | 205 const extensions::Extension* extension_; |
204 | 206 |
205 // The object that is waiting for the image loading to complete | 207 // The object that will be used to get the browser action icon for us. |
206 // asynchronously. | 208 // It may load the icon asynchronously (in which case the initial icon |
207 ImageLoadingTracker tracker_; | 209 // returned by the factory will be transparent), so we have to observe it for |
208 | 210 // updates to the icon. |
209 // The default icon for our browser action. This might be non-empty if the | 211 ExtensionActionIconFactory icon_factory_; |
210 // browser action had a value for default_icon in the manifest. | |
211 SkBitmap default_icon_; | |
212 | 212 |
213 // Delegate that usually represents a container for BrowserActionView. | 213 // Delegate that usually represents a container for BrowserActionView. |
214 BrowserActionView::Delegate* delegate_; | 214 BrowserActionView::Delegate* delegate_; |
215 | 215 |
216 // The context menu. This member is non-NULL only when the menu is shown. | 216 // The context menu. This member is non-NULL only when the menu is shown. |
217 views::MenuItemView* context_menu_; | 217 views::MenuItemView* context_menu_; |
218 | 218 |
219 // Used to make sure MaybeRegisterExtensionCommand() is called only once | 219 // Used to make sure MaybeRegisterExtensionCommand() is called only once |
220 // from ViewHierarchyChanged(). | 220 // from ViewHierarchyChanged(). |
221 bool called_registered_extension_command_; | 221 bool called_registered_extension_command_; |
222 | 222 |
223 content::NotificationRegistrar registrar_; | 223 content::NotificationRegistrar registrar_; |
224 | 224 |
225 // The extension key binding accelerator this browser action is listening for | 225 // The extension key binding accelerator this browser action is listening for |
226 // (to show the popup). | 226 // (to show the popup). |
227 scoped_ptr<ui::Accelerator> keybinding_; | 227 scoped_ptr<ui::Accelerator> keybinding_; |
228 | 228 |
229 // Responsible for running the menu. | 229 // Responsible for running the menu. |
230 scoped_ptr<views::MenuRunner> menu_runner_; | 230 scoped_ptr<views::MenuRunner> menu_runner_; |
231 | 231 |
232 friend class base::DeleteHelper<BrowserActionButton>; | 232 friend class base::DeleteHelper<BrowserActionButton>; |
233 | 233 |
234 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); | 234 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); |
235 }; | 235 }; |
236 | 236 |
237 #endif // CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ | 237 #endif // CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
OLD | NEW |