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