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