Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_ | |
| 6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_ | |
| 7 | |
| 8 #include "chrome/browser/extensions/extension_action_icon_factory.h" | |
| 9 #include "chrome/browser/extensions/extension_context_menu_model.h" | |
| 10 #include "chrome/browser/ui/views/extensions/extension_popup.h" | |
| 11 #include "ui/gfx/image/image.h" | |
| 12 #include "ui/views/context_menu_controller.h" | |
| 13 #include "ui/views/widget/widget_observer.h" | |
| 14 | |
| 15 class Browser; | |
| 16 class ExtensionAction; | |
| 17 | |
| 18 namespace content { | |
| 19 class WebContents; | |
| 20 } | |
| 21 | |
| 22 namespace extensions { | |
| 23 class Command; | |
| 24 class Extension; | |
| 25 } | |
| 26 | |
| 27 namespace ui { | |
| 28 class Accelerator; | |
| 29 } | |
| 30 | |
| 31 namespace views { | |
| 32 class MenuRunner; | |
| 33 class View; | |
| 34 class Widget; | |
| 35 } | |
| 36 | |
| 37 // An abstract "View" for an ExtensionAction (either a BrowserAction or a | |
| 38 // PageAction). This contains the logic for showing the action's popup and | |
| 39 // the context menu. This class doesn't subclass View directly, as the | |
| 40 // implementations for page actions/browser actions are different types of | |
| 41 // views. | |
| 42 // All common logic for executing extension actions should go in this class; | |
|
sky
2014/08/05 22:29:46
No one is going to subclass this, right?
Devlin
2014/08/05 23:51:09
Correct. :)
| |
| 43 // ViewDelegate classes should only have knowledge relating to the views::View | |
| 44 // wrapper. | |
| 45 class ExtensionActionView : public ExtensionActionIconFactory::Observer, | |
|
sky
2014/08/05 22:29:46
I don't like naming this class a View if its not a
Devlin
2014/08/05 23:51:10
I know :/ This was a desperate attempt at a name
| |
| 46 public ExtensionContextMenuModel::PopupDelegate, | |
| 47 public views::ContextMenuController, | |
| 48 public views::WidgetObserver { | |
| 49 public: | |
| 50 class ViewDelegate { | |
|
sky
2014/08/05 22:29:46
I prefer this to live in its own file and be named
Devlin
2014/08/05 23:51:10
Ah, didn't know that - I think I've only seen dele
| |
| 51 public: | |
| 52 virtual ~ViewDelegate() {} | |
|
sky
2014/08/05 22:29:46
make protected so that it's clear the delegate is
Devlin
2014/08/05 23:51:10
Done.
| |
| 53 | |
| 54 // Returns |this| as a view. We need this because our subclasses implement | |
| 55 // different kinds of views, and inheriting View here is a really bad idea. | |
| 56 virtual views::View* GetAsView() = 0; | |
| 57 | |
| 58 // Returns true if this is a nested view. | |
|
sky
2014/08/05 22:29:47
What does 'nested' mean here?
Devlin
2014/08/05 23:51:10
Rephrased for clarity and correctness.
| |
| 59 virtual bool IsNestedView() = 0; | |
| 60 | |
| 61 // Returns the FocusManager to use when registering accelerators. | |
| 62 virtual views::FocusManager* GetFocusManagerForAccelerator() = 0; | |
| 63 | |
| 64 // Returns the parent for the associated context menu. | |
| 65 virtual views::Widget* GetParentForContextMenu() = 0; | |
| 66 | |
| 67 // Returns the reference view for the extension action's popup. | |
| 68 virtual views::View* GetReferenceViewForPopup() = 0; | |
| 69 | |
| 70 // Returns the current web contents. | |
| 71 virtual content::WebContents* GetCurrentWebContents() = 0; | |
| 72 | |
| 73 // Hides whatever popup is active (even if it's not this one). | |
| 74 virtual void HideActivePopup() = 0; | |
| 75 | |
| 76 // Called when the icon is updated; this is forwarded from the icon factory. | |
| 77 virtual void OnIconUpdated() = 0; | |
| 78 | |
| 79 // Called when a popup is shown. See ExecuteAction() for the definition of | |
| 80 // |grant_tab_permissions|. | |
| 81 virtual void OnPopupShown(bool grant_tab_permissions) {} | |
| 82 | |
| 83 // Does any additional cleanup after the popup is closed. | |
| 84 virtual void CleanupPopup() {} | |
| 85 | |
| 86 // Called immediately before the context menu is shown. | |
| 87 virtual void OnWillShowContextMenus() {} | |
| 88 | |
| 89 // Called once the context menu has closed. | |
|
sky
2014/08/05 22:29:46
Make it clear this may not be called (if the conte
Devlin
2014/08/05 23:51:10
Done.
| |
| 90 virtual void OnContextMenuDone() {} | |
| 91 }; | |
| 92 | |
| 93 ExtensionActionView(const extensions::Extension* extension, | |
| 94 Browser* browser, | |
| 95 ExtensionAction* extension_action, | |
| 96 ViewDelegate* delegate); | |
| 97 virtual ~ExtensionActionView(); | |
| 98 | |
| 99 // ExtensionContextMenuModel::PopupDelegate: | |
| 100 virtual void InspectPopup() OVERRIDE; | |
| 101 | |
| 102 // Executes the default extension action (typically showing the popup), and | |
| 103 // attributes the action to a user (thus, only use this for actions that | |
| 104 // *were* done by the user). | |
| 105 void ExecuteActionByUser(); | |
| 106 | |
| 107 // Executes the extension action with |show_action|. If | |
| 108 // |grant_tab_permissions| is true, this will grant the extension active tab | |
| 109 // permissions. Only do this if this was done through a user action (and not | |
| 110 // e.g. an API). Returns true if a popup is shown. | |
| 111 bool ExecuteAction(ExtensionPopup::ShowAction show_action, | |
| 112 bool grant_tab_permissions); | |
| 113 | |
| 114 // Hides the popup, if one is open. | |
| 115 void HidePopup(); | |
| 116 | |
| 117 // Returns the icon from the |icon_factory_|. | |
| 118 gfx::Image GetIcon(int tab_id); | |
| 119 | |
| 120 // Returns the current tab id. | |
| 121 int GetCurrentTabId() const; | |
| 122 | |
| 123 // Registers an accelerator for the extension action's command, if one | |
| 124 // exists. | |
| 125 void RegisterCommand(); | |
| 126 | |
| 127 // Unregisters the accelerator for the extension action's command, if one | |
| 128 // exists. If |only_if_removed| is true, then this will only unregister if the | |
| 129 // command has been removed. | |
| 130 void UnregisterCommand(bool only_if_removed); | |
| 131 | |
| 132 // Handles the View::AcceleratorPressed method. Doesn't OVERRIDE because we | |
| 133 // don't actually derive from View here. | |
| 134 bool AcceleratorPressed(const ui::Accelerator& accelerator); | |
| 135 | |
| 136 const extensions::Extension* extension() const { return extension_; } | |
| 137 Browser* browser() { return browser_; } | |
| 138 ExtensionAction* extension_action() { return extension_action_; } | |
| 139 const ExtensionAction* extension_action() const { return extension_action_; } | |
| 140 ExtensionPopup* popup() { return popup_; } | |
| 141 bool is_menu_running() const { return menu_runner_.get() != NULL; } | |
| 142 | |
| 143 private: | |
| 144 // ExtensionActionIconFactory::Observer: | |
| 145 virtual void OnIconUpdated() OVERRIDE; | |
| 146 | |
| 147 // views::WidgetObserver: | |
| 148 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; | |
| 149 | |
| 150 // views::ContextMenuController: | |
| 151 virtual void ShowContextMenuForView(views::View* source, | |
| 152 const gfx::Point& point, | |
| 153 ui::MenuSourceType source_type) OVERRIDE; | |
| 154 | |
| 155 // Shows the popup for the extension action, given the associated |popup_url|. | |
| 156 // Returns true if a popup is successfully shown. | |
| 157 bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action, | |
| 158 const GURL& popup_url); | |
| 159 | |
| 160 // Populates |command| with the command associated with |extension|, if one | |
| 161 // exists. Returns true if |command| was populated. | |
| 162 bool GetExtensionCommand(extensions::Command* command); | |
| 163 | |
| 164 // Cleans up after the popup. If |close_widget| is true, this will call | |
| 165 // Widget::Close() on the popup's widget; otherwise it assumes the popup is | |
| 166 // already closing. | |
| 167 void CleanupPopup(bool close_widget); | |
| 168 | |
| 169 // The extension associated with the action we're displaying. | |
| 170 const extensions::Extension* extension_; | |
| 171 | |
| 172 // The corresponding browser. | |
| 173 Browser* browser_; | |
| 174 | |
| 175 // The browser action this view represents. The ExtensionAction is not owned | |
| 176 // by this class. | |
| 177 ExtensionAction* extension_action_; | |
| 178 | |
| 179 // Our delegate. | |
| 180 ViewDelegate* delegate_; | |
| 181 | |
| 182 // The object that will be used to get the browser action icon for us. | |
| 183 // It may load the icon asynchronously (in which case the initial icon | |
| 184 // returned by the factory will be transparent), so we have to observe it for | |
| 185 // updates to the icon. | |
| 186 ExtensionActionIconFactory icon_factory_; | |
| 187 | |
| 188 // Responsible for running the menu. | |
| 189 scoped_ptr<views::MenuRunner> menu_runner_; | |
| 190 | |
| 191 // The browser action's popup, if it is visible; NULL otherwise. | |
| 192 ExtensionPopup* popup_; | |
| 193 | |
| 194 // The extension key binding accelerator this extension action is listening | |
| 195 // for (to show the popup). | |
| 196 scoped_ptr<ui::Accelerator> action_keybinding_; | |
| 197 | |
| 198 DISALLOW_COPY_AND_ASSIGN(ExtensionActionView); | |
| 199 }; | |
| 200 | |
| 201 #endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_ | |
| OLD | NEW |