| OLD | NEW |
| 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/toolbar/toolbar_action_view_delegate.h" |
| 9 #include "chrome/browser/ui/views/extensions/extension_action_view_delegate.h" | |
| 10 #include "content/public/browser/notification_observer.h" | 9 #include "content/public/browser/notification_observer.h" |
| 11 #include "content/public/browser/notification_registrar.h" | 10 #include "content/public/browser/notification_registrar.h" |
| 12 #include "ui/views/controls/button/menu_button.h" | 11 #include "ui/views/controls/button/menu_button.h" |
| 13 #include "ui/views/controls/button/menu_button_listener.h" | 12 #include "ui/views/controls/button/menu_button_listener.h" |
| 14 #include "ui/views/drag_controller.h" | 13 #include "ui/views/drag_controller.h" |
| 15 #include "ui/views/view.h" | 14 #include "ui/views/view.h" |
| 16 | 15 |
| 17 class Browser; | 16 class Browser; |
| 18 class ExtensionAction; | 17 class ExtensionAction; |
| 18 class ExtensionActionViewController; |
| 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 a ToolbarActionViewController to display a toolbar action |
| 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 component 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 Loading... |
| 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 BrowserActionView(scoped_ptr<ToolbarActionViewController> view_controller, |
| 77 // been updated. | |
| 78 class IconObserver { | |
| 79 public: | |
| 80 virtual void OnIconUpdated(const gfx::ImageSkia& icon) = 0; | |
| 81 | |
| 82 protected: | |
| 83 virtual ~IconObserver() {} | |
| 84 }; | |
| 85 | |
| 86 BrowserActionView(const extensions::Extension* extension, | |
| 87 ExtensionAction* extension_action, | |
| 88 Browser* browser, | 77 Browser* browser, |
| 89 BrowserActionView::Delegate* delegate); | 78 Delegate* delegate); |
| 90 virtual ~BrowserActionView(); | 79 virtual ~BrowserActionView(); |
| 91 | 80 |
| 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. | 81 // Called to update the display to match the browser action's state. |
| 106 void UpdateState(); | 82 void UpdateState(); |
| 107 | 83 |
| 108 // Does this button's action have a popup? | |
| 109 bool IsPopup(); | |
| 110 | |
| 111 // Overridden from views::View: | 84 // Overridden from views::View: |
| 112 virtual void GetAccessibleState(ui::AXViewState* state) override; | 85 virtual void GetAccessibleState(ui::AXViewState* state) override; |
| 113 | 86 |
| 114 // Overridden from views::ButtonListener: | 87 // Overridden from views::ButtonListener: |
| 115 virtual void ButtonPressed(views::Button* sender, | 88 virtual void ButtonPressed(views::Button* sender, |
| 116 const ui::Event& event) override; | 89 const ui::Event& event) override; |
| 117 | 90 |
| 118 // Overridden from content::NotificationObserver: | 91 // Overridden from content::NotificationObserver: |
| 119 virtual void Observe(int type, | 92 virtual void Observe(int type, |
| 120 const content::NotificationSource& source, | 93 const content::NotificationSource& source, |
| 121 const content::NotificationDetails& details) override; | 94 const content::NotificationDetails& details) override; |
| 122 | 95 |
| 123 // MenuButton behavior overrides. These methods all default to LabelButton | 96 // MenuButton behavior overrides. These methods all default to LabelButton |
| 124 // behavior unless this button is a popup. In that case, it uses MenuButton | 97 // 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 | 98 // 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 | 99 // button will stay in the pushed state until the "menu" (a popup in this |
| 127 // case) is dismissed. | 100 // case) is dismissed. |
| 128 virtual bool Activate() override; | 101 virtual bool Activate() override; |
| 129 virtual bool OnMousePressed(const ui::MouseEvent& event) override; | 102 virtual bool OnMousePressed(const ui::MouseEvent& event) override; |
| 130 virtual void OnMouseReleased(const ui::MouseEvent& event) override; | 103 virtual void OnMouseReleased(const ui::MouseEvent& event) override; |
| 131 virtual void OnMouseExited(const ui::MouseEvent& event) override; | 104 virtual void OnMouseExited(const ui::MouseEvent& event) override; |
| 132 virtual bool OnKeyReleased(const ui::KeyEvent& event) override; | 105 virtual bool OnKeyReleased(const ui::KeyEvent& event) override; |
| 133 virtual void OnGestureEvent(ui::GestureEvent* event) override; | 106 virtual void OnGestureEvent(ui::GestureEvent* event) override; |
| 134 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const | 107 virtual scoped_ptr<views::LabelButtonBorder> CreateDefaultBorder() const |
| 135 override; | 108 override; |
| 136 | 109 |
| 137 // Whether the browser action is enabled on this tab. Note that we cannot use | 110 // ToolbarActionViewDelegate: (public because called by others). |
| 138 // the built-in views enabled/SetEnabled because disabled views do not | 111 virtual content::WebContents* GetCurrentWebContents() const override; |
| 139 // receive drag events. | |
| 140 bool IsEnabled(int tab_id) const; | |
| 141 | 112 |
| 142 // Gets the icon of this button and its badge. | 113 // A convenience method to get the |view_controller_| as an |
| 143 gfx::ImageSkia GetIconWithBadge(); | 114 // ExtensionActionViewController. This will DCHECK if this isn't a view for |
| 115 // an extension action. |
| 116 ExtensionActionViewController* GetExtensionActionViewController(); |
| 117 |
| 118 ToolbarActionViewController* view_controller() { |
| 119 return view_controller_.get(); |
| 120 } |
| 121 Browser* browser() { return browser_; } |
| 144 | 122 |
| 145 // Returns button icon so it can be accessed during tests. | 123 // Returns button icon so it can be accessed during tests. |
| 146 gfx::ImageSkia GetIconForTest(); | 124 gfx::ImageSkia GetIconForTest(); |
| 147 | 125 |
| 148 private: | 126 private: |
| 149 // Overridden from views::View: | 127 // Overridden from views::View: |
| 150 virtual void ViewHierarchyChanged( | 128 virtual void ViewHierarchyChanged( |
| 151 const ViewHierarchyChangedDetails& details) override; | 129 const ViewHierarchyChangedDetails& details) override; |
| 152 virtual void OnDragDone() override; | 130 virtual void OnDragDone() override; |
| 153 virtual gfx::Size GetPreferredSize() const override; | 131 virtual gfx::Size GetPreferredSize() const override; |
| 154 virtual void PaintChildren(gfx::Canvas* canvas, | 132 virtual void PaintChildren(gfx::Canvas* canvas, |
| 155 const views::CullSet& cull_set) override; | 133 const views::CullSet& cull_set) override; |
| 156 | 134 |
| 157 // ExtensionActionViewDelegate: | 135 // ToolbarActionViewDelegate: |
| 158 virtual views::View* GetAsView() override; | 136 virtual views::View* GetAsView() override; |
| 159 virtual bool IsShownInMenu() override; | 137 virtual bool IsShownInMenu() override; |
| 160 virtual views::FocusManager* GetFocusManagerForAccelerator() override; | 138 virtual views::FocusManager* GetFocusManagerForAccelerator() override; |
| 161 virtual views::Widget* GetParentForContextMenu() override; | 139 virtual views::Widget* GetParentForContextMenu() override; |
| 162 virtual ExtensionActionViewController* GetPreferredPopupViewController() | 140 virtual ToolbarActionViewController* GetPreferredPopupViewController() |
| 163 override; | 141 override; |
| 164 virtual views::View* GetReferenceViewForPopup() override; | 142 virtual views::View* GetReferenceViewForPopup() override; |
| 165 virtual views::MenuButton* GetContextMenuButton() override; | 143 virtual views::MenuButton* GetContextMenuButton() override; |
| 166 virtual content::WebContents* GetCurrentWebContents() override; | |
| 167 virtual void HideActivePopup() override; | 144 virtual void HideActivePopup() override; |
| 168 virtual void OnIconUpdated() override; | 145 virtual void OnIconUpdated() override; |
| 169 virtual void OnPopupShown(bool grant_tab_permissions) override; | 146 virtual void OnPopupShown(bool grant_tab_permissions) override; |
| 170 virtual void CleanupPopup() override; | 147 virtual void CleanupPopup() override; |
| 171 | 148 |
| 172 // A lock to keep the MenuButton pressed when a menu or popup is visible. | 149 // A lock to keep the MenuButton pressed when a menu or popup is visible. |
| 173 // This needs to be destroyed after |view_controller_|, because | 150 // This needs to be destroyed after |view_controller_|, because |
| 174 // |view_controller_|'s destructor can call CleanupPopup(), which uses this | 151 // |view_controller_|'s destructor can call CleanupPopup(), which uses this |
| 175 // object. | 152 // object. |
| 176 scoped_ptr<views::MenuButton::PressedLock> pressed_lock_; | 153 scoped_ptr<views::MenuButton::PressedLock> pressed_lock_; |
| 177 | 154 |
| 178 // The controller for this ExtensionAction view. | 155 // The controller for this toolbar action view. |
| 179 scoped_ptr<ExtensionActionViewController> view_controller_; | 156 scoped_ptr<ToolbarActionViewController> view_controller_; |
| 157 |
| 158 // The associated browser. |
| 159 Browser* browser_; |
| 180 | 160 |
| 181 // Delegate that usually represents a container for BrowserActionView. | 161 // Delegate that usually represents a container for BrowserActionView. |
| 182 BrowserActionView::Delegate* delegate_; | 162 Delegate* delegate_; |
| 183 | 163 |
| 184 // Used to make sure we only register the command once. | 164 // Used to make sure we only register the command once. |
| 185 bool called_registered_extension_command_; | 165 bool called_register_command_; |
| 186 | 166 |
| 187 content::NotificationRegistrar registrar_; | 167 content::NotificationRegistrar registrar_; |
| 188 | 168 |
| 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); | 169 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); |
| 194 }; | 170 }; |
| 195 | 171 |
| 196 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ | 172 #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_VIEW_H_ |
| OLD | NEW |