| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/extension_action_icon_factory.h" | 8 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
| 9 #include "chrome/browser/extensions/extension_context_menu_model.h" | 9 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 10 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| 10 #include "chrome/browser/ui/views/extensions/extension_popup.h" | 11 #include "chrome/browser/ui/views/extensions/extension_popup.h" |
| 12 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" |
| 11 #include "ui/base/accelerators/accelerator.h" | 14 #include "ui/base/accelerators/accelerator.h" |
| 12 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
| 13 #include "ui/views/context_menu_controller.h" | 16 #include "ui/views/context_menu_controller.h" |
| 14 #include "ui/views/widget/widget_observer.h" | 17 #include "ui/views/widget/widget_observer.h" |
| 15 | 18 |
| 16 class Browser; | 19 class Browser; |
| 17 class ExtensionAction; | 20 class ExtensionAction; |
| 18 class ExtensionActionViewDelegate; | 21 class ToolbarActionViewDelegate; |
| 19 | 22 |
| 20 namespace content { | 23 namespace content { |
| 21 class WebContents; | 24 class WebContents; |
| 22 } | 25 } |
| 23 | 26 |
| 24 namespace extensions { | 27 namespace extensions { |
| 25 class Command; | 28 class Command; |
| 26 class Extension; | 29 class Extension; |
| 27 } | 30 } |
| 28 | 31 |
| 29 namespace ui { | 32 namespace ui { |
| 30 class Accelerator; | 33 class Accelerator; |
| 31 } | 34 } |
| 32 | 35 |
| 33 namespace views { | 36 namespace views { |
| 34 class MenuRunner; | 37 class MenuRunner; |
| 35 class View; | 38 class View; |
| 36 class Widget; | 39 class Widget; |
| 37 } | 40 } |
| 38 | 41 |
| 39 // An abstract "View" for an ExtensionAction (either a BrowserAction or a | 42 // An abstract "View" for an ExtensionAction (either a BrowserAction or a |
| 40 // PageAction). This contains the logic for showing the action's popup and | 43 // PageAction). This contains the logic for showing the action's popup and |
| 41 // the context menu. This class doesn't subclass View directly, as the | 44 // the context menu. This class doesn't subclass View directly, as the |
| 42 // implementations for page actions/browser actions are different types of | 45 // implementations for page actions/browser actions are different types of |
| 43 // views. | 46 // views. |
| 44 // All common logic for executing extension actions should go in this class; | 47 // All common logic for executing extension actions should go in this class; |
| 45 // ExtensionActionViewDelegate classes should only have knowledge relating to | 48 // ToolbarActionViewDelegate classes should only have knowledge relating to |
| 46 // the views::View wrapper. | 49 // the views::View wrapper. |
| 47 class ExtensionActionViewController | 50 class ExtensionActionViewController |
| 48 : public ExtensionActionIconFactory::Observer, | 51 : public ToolbarActionViewController, |
| 52 public content::NotificationObserver, |
| 53 public ExtensionActionIconFactory::Observer, |
| 49 public ExtensionContextMenuModel::PopupDelegate, | 54 public ExtensionContextMenuModel::PopupDelegate, |
| 50 public ui::AcceleratorTarget, | 55 public ui::AcceleratorTarget, |
| 51 public views::ContextMenuController, | 56 public views::ContextMenuController, |
| 52 public views::WidgetObserver { | 57 public views::WidgetObserver { |
| 53 public: | 58 public: |
| 54 ExtensionActionViewController(const extensions::Extension* extension, | 59 ExtensionActionViewController(const extensions::Extension* extension, |
| 55 Browser* browser, | 60 Browser* browser, |
| 56 ExtensionAction* extension_action, | 61 ExtensionAction* extension_action); |
| 57 ExtensionActionViewDelegate* delegate); | |
| 58 virtual ~ExtensionActionViewController(); | 62 virtual ~ExtensionActionViewController(); |
| 59 | 63 |
| 64 // ToolbarActionViewController: |
| 65 virtual const std::string& GetId() const override; |
| 66 virtual void SetDelegate(ToolbarActionViewDelegate* delegate) override; |
| 67 virtual gfx::Image GetIcon(content::WebContents* web_contents) override; |
| 68 virtual gfx::ImageSkia GetIconWithBadge() override; |
| 69 virtual base::string16 GetAccessibleName(content::WebContents* web_contents) |
| 70 const override; |
| 71 virtual base::string16 GetTooltip(content::WebContents* web_contents) |
| 72 const override; |
| 73 virtual bool IsEnabled(content::WebContents* web_contents) const override; |
| 74 virtual bool HasPopup(content::WebContents* web_contents) const override; |
| 75 virtual void HidePopup() override; |
| 76 virtual gfx::NativeView GetPopupNativeView() override; |
| 77 virtual bool IsMenuRunning() const override; |
| 78 virtual bool CanDrag() const override; |
| 79 virtual bool ExecuteAction(bool by_user) override; |
| 80 virtual void PaintExtra(gfx::Canvas* canvas, |
| 81 const gfx::Rect& bounds, |
| 82 content::WebContents* web_contents) const override; |
| 83 virtual void RegisterCommand() override; |
| 84 |
| 60 // ExtensionContextMenuModel::PopupDelegate: | 85 // ExtensionContextMenuModel::PopupDelegate: |
| 61 virtual void InspectPopup() override; | 86 virtual void InspectPopup() override; |
| 62 | 87 |
| 63 // Executes the default extension action (typically showing the popup), and | |
| 64 // attributes the action to a user (thus, only use this for actions that | |
| 65 // *were* done by the user). | |
| 66 void ExecuteActionByUser(); | |
| 67 | |
| 68 // Executes the extension action with |show_action|. If | 88 // Executes the extension action with |show_action|. If |
| 69 // |grant_tab_permissions| is true, this will grant the extension active tab | 89 // |grant_tab_permissions| is true, this will grant the extension active tab |
| 70 // permissions. Only do this if this was done through a user action (and not | 90 // permissions. Only do this if this was done through a user action (and not |
| 71 // e.g. an API). Returns true if a popup is shown. | 91 // e.g. an API). Returns true if a popup is shown. |
| 72 bool ExecuteAction(ExtensionPopup::ShowAction show_action, | 92 bool ExecuteAction(ExtensionPopup::ShowAction show_action, |
| 73 bool grant_tab_permissions); | 93 bool grant_tab_permissions); |
| 74 | 94 |
| 75 // Hides the popup, if one is open. | 95 void set_icon_observer(ExtensionActionIconFactory::Observer* icon_observer) { |
| 76 void HidePopup(); | 96 icon_observer_ = icon_observer; |
| 77 | 97 } |
| 78 // Returns the icon from the |icon_factory_|. | |
| 79 gfx::Image GetIcon(int tab_id); | |
| 80 | |
| 81 // Returns the current tab id. | |
| 82 int GetCurrentTabId() const; | |
| 83 | |
| 84 // Registers an accelerator for the extension action's command, if one | |
| 85 // exists. | |
| 86 void RegisterCommand(); | |
| 87 | |
| 88 // Unregisters the accelerator for the extension action's command, if one | |
| 89 // exists. If |only_if_removed| is true, then this will only unregister if the | |
| 90 // command has been removed. | |
| 91 void UnregisterCommand(bool only_if_removed); | |
| 92 | 98 |
| 93 const extensions::Extension* extension() const { return extension_; } | 99 const extensions::Extension* extension() const { return extension_; } |
| 94 Browser* browser() { return browser_; } | 100 Browser* browser() { return browser_; } |
| 95 ExtensionAction* extension_action() { return extension_action_; } | 101 ExtensionAction* extension_action() { return extension_action_; } |
| 96 const ExtensionAction* extension_action() const { return extension_action_; } | 102 const ExtensionAction* extension_action() const { return extension_action_; } |
| 97 ExtensionPopup* popup() { return popup_; } | 103 ExtensionPopup* popup() { return popup_; } |
| 98 bool is_menu_running() const { return menu_runner_.get() != NULL; } | |
| 99 | 104 |
| 100 private: | 105 private: |
| 101 // ExtensionActionIconFactory::Observer: | 106 // ExtensionActionIconFactory::Observer: |
| 102 virtual void OnIconUpdated() override; | 107 virtual void OnIconUpdated() override; |
| 103 | 108 |
| 104 // ui::AcceleratorTarget: | 109 // ui::AcceleratorTarget: |
| 105 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | 110 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) override; |
| 106 virtual bool CanHandleAccelerators() const override; | 111 virtual bool CanHandleAccelerators() const override; |
| 107 | 112 |
| 108 // views::WidgetObserver: | 113 // views::WidgetObserver: |
| 109 virtual void OnWidgetDestroying(views::Widget* widget) override; | 114 virtual void OnWidgetDestroying(views::Widget* widget) override; |
| 110 | 115 |
| 111 // views::ContextMenuController: | 116 // views::ContextMenuController: |
| 112 virtual void ShowContextMenuForView(views::View* source, | 117 virtual void ShowContextMenuForView(views::View* source, |
| 113 const gfx::Point& point, | 118 const gfx::Point& point, |
| 114 ui::MenuSourceType source_type) override; | 119 ui::MenuSourceType source_type) override; |
| 115 | 120 |
| 121 // content::NotificationObserver: |
| 122 virtual void Observe(int type, |
| 123 const content::NotificationSource& source, |
| 124 const content::NotificationDetails& details) override; |
| 125 |
| 116 // Shows the context menu for extension action. | 126 // Shows the context menu for extension action. |
| 117 void DoShowContextMenu(ui::MenuSourceType source_type); | 127 void DoShowContextMenu(ui::MenuSourceType source_type); |
| 118 | 128 |
| 119 // Shows the popup for the extension action, given the associated |popup_url|. | 129 // Shows the popup for the extension action, given the associated |popup_url|. |
| 120 // |grant_tab_permissions| is true if active tab permissions should be given | 130 // |grant_tab_permissions| is true if active tab permissions should be given |
| 121 // to the extension; this is only true if the popup is opened through a user | 131 // to the extension; this is only true if the popup is opened through a user |
| 122 // action. | 132 // action. |
| 123 // Returns true if a popup is successfully shown. | 133 // Returns true if a popup is successfully shown. |
| 124 bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action, | 134 bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action, |
| 125 const GURL& popup_url, | 135 const GURL& popup_url, |
| 126 bool grant_tab_permissions); | 136 bool grant_tab_permissions); |
| 127 | 137 |
| 128 // Populates |command| with the command associated with |extension|, if one | 138 // Populates |command| with the command associated with |extension|, if one |
| 129 // exists. Returns true if |command| was populated. | 139 // exists. Returns true if |command| was populated. |
| 130 bool GetExtensionCommand(extensions::Command* command); | 140 bool GetExtensionCommand(extensions::Command* command); |
| 131 | 141 |
| 142 // Unregisters the accelerator for the extension action's command, if one |
| 143 // exists. If |only_if_removed| is true, then this will only unregister if the |
| 144 // command has been removed. |
| 145 void UnregisterCommand(bool only_if_removed); |
| 146 |
| 132 // Closes the currently-active menu, if needed. This is the case when there | 147 // Closes the currently-active menu, if needed. This is the case when there |
| 133 // is an active menu that wouldn't close automatically when a new one is | 148 // is an active menu that wouldn't close automatically when a new one is |
| 134 // opened. | 149 // opened. |
| 135 // Returns true if a menu was closed, false otherwise. | 150 // Returns true if a menu was closed, false otherwise. |
| 136 bool CloseActiveMenuIfNeeded(); | 151 bool CloseActiveMenuIfNeeded(); |
| 137 | 152 |
| 138 // Cleans up after the popup. If |close_widget| is true, this will call | 153 // Cleans up after the popup. If |close_widget| is true, this will call |
| 139 // Widget::Close() on the popup's widget; otherwise it assumes the popup is | 154 // Widget::Close() on the popup's widget; otherwise it assumes the popup is |
| 140 // already closing. | 155 // already closing. |
| 141 void CleanupPopup(bool close_widget); | 156 void CleanupPopup(bool close_widget); |
| 142 | 157 |
| 143 // The extension associated with the action we're displaying. | 158 // The extension associated with the action we're displaying. |
| 144 const extensions::Extension* extension_; | 159 const extensions::Extension* extension_; |
| 145 | 160 |
| 146 // The corresponding browser. | 161 // The corresponding browser. |
| 147 Browser* browser_; | 162 Browser* browser_; |
| 148 | 163 |
| 149 // The browser action this view represents. The ExtensionAction is not owned | 164 // The browser action this view represents. The ExtensionAction is not owned |
| 150 // by this class. | 165 // by this class. |
| 151 ExtensionAction* extension_action_; | 166 ExtensionAction* extension_action_; |
| 152 | 167 |
| 153 // Our delegate. | 168 // Our delegate. |
| 154 ExtensionActionViewDelegate* delegate_; | 169 ToolbarActionViewDelegate* delegate_; |
| 155 | 170 |
| 156 // The object that will be used to get the browser action icon for us. | 171 // The object that will be used to get the browser action icon for us. |
| 157 // It may load the icon asynchronously (in which case the initial icon | 172 // It may load the icon asynchronously (in which case the initial icon |
| 158 // returned by the factory will be transparent), so we have to observe it for | 173 // returned by the factory will be transparent), so we have to observe it for |
| 159 // updates to the icon. | 174 // updates to the icon. |
| 160 ExtensionActionIconFactory icon_factory_; | 175 ExtensionActionIconFactory icon_factory_; |
| 161 | 176 |
| 177 // The observer that we need to notify when the icon of the button has been |
| 178 // updated. |
| 179 ExtensionActionIconFactory::Observer* icon_observer_; |
| 180 |
| 162 // Responsible for running the menu. | 181 // Responsible for running the menu. |
| 163 scoped_ptr<views::MenuRunner> menu_runner_; | 182 scoped_ptr<views::MenuRunner> menu_runner_; |
| 164 | 183 |
| 165 // The browser action's popup, if it is visible; NULL otherwise. | 184 // The browser action's popup, if it is visible; NULL otherwise. |
| 166 ExtensionPopup* popup_; | 185 ExtensionPopup* popup_; |
| 167 | 186 |
| 168 // The extension key binding accelerator this extension action is listening | 187 // The extension key binding accelerator this extension action is listening |
| 169 // for (to show the popup). | 188 // for (to show the popup). |
| 170 scoped_ptr<ui::Accelerator> action_keybinding_; | 189 scoped_ptr<ui::Accelerator> action_keybinding_; |
| 171 | 190 |
| 191 content::NotificationRegistrar registrar_; |
| 192 |
| 172 // If non-NULL, this is the next ExtensionActionViewController context menu | 193 // If non-NULL, this is the next ExtensionActionViewController context menu |
| 173 // which wants to run once the current owner (this one) is done. | 194 // which wants to run once the current owner (this one) is done. |
| 174 base::Closure followup_context_menu_task_; | 195 base::Closure followup_context_menu_task_; |
| 175 | 196 |
| 176 base::WeakPtrFactory<ExtensionActionViewController> weak_factory_; | 197 base::WeakPtrFactory<ExtensionActionViewController> weak_factory_; |
| 177 | 198 |
| 178 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController); | 199 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController); |
| 179 }; | 200 }; |
| 180 | 201 |
| 181 #endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H
_ | 202 #endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H
_ |
| OLD | NEW |