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_CONTROLLER_H_ | |
6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_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/toolbar/toolbar_action_view_controller.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" | |
14 #include "ui/base/accelerators/accelerator.h" | |
15 #include "ui/gfx/image/image.h" | |
16 #include "ui/views/context_menu_controller.h" | |
17 #include "ui/views/widget/widget_observer.h" | |
18 | |
19 class Browser; | |
20 class ExtensionAction; | |
21 class ToolbarActionViewDelegate; | |
22 | |
23 namespace content { | |
24 class WebContents; | |
25 } | |
26 | |
27 namespace extensions { | |
28 class Command; | |
29 class Extension; | |
30 } | |
31 | |
32 namespace ui { | |
33 class Accelerator; | |
34 } | |
35 | |
36 namespace views { | |
37 class MenuRunner; | |
38 class View; | |
39 class Widget; | |
40 } | |
41 | |
42 // An abstract "View" for an ExtensionAction (either a BrowserAction or a | |
43 // PageAction). This contains the logic for showing the action's popup and | |
44 // the context menu. This class doesn't subclass View directly, as the | |
45 // implementations for page actions/browser actions are different types of | |
46 // views. | |
47 // All common logic for executing extension actions should go in this class; | |
48 // ToolbarActionViewDelegate classes should only have knowledge relating to | |
49 // the views::View wrapper. | |
50 class ExtensionActionViewController | |
51 : public ToolbarActionViewController, | |
52 public content::NotificationObserver, | |
53 public ExtensionActionIconFactory::Observer, | |
54 public ExtensionContextMenuModel::PopupDelegate, | |
55 public ui::AcceleratorTarget, | |
56 public views::ContextMenuController, | |
57 public views::WidgetObserver { | |
58 public: | |
59 ExtensionActionViewController(const extensions::Extension* extension, | |
60 Browser* browser, | |
61 ExtensionAction* extension_action); | |
62 virtual ~ExtensionActionViewController(); | |
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 | |
85 // ExtensionContextMenuModel::PopupDelegate: | |
86 virtual void InspectPopup() override; | |
87 | |
88 // Executes the extension action with |show_action|. If | |
89 // |grant_tab_permissions| is true, this will grant the extension active tab | |
90 // permissions. Only do this if this was done through a user action (and not | |
91 // e.g. an API). Returns true if a popup is shown. | |
92 bool ExecuteAction(ExtensionPopup::ShowAction show_action, | |
93 bool grant_tab_permissions); | |
94 | |
95 void set_icon_observer(ExtensionActionIconFactory::Observer* icon_observer) { | |
96 icon_observer_ = icon_observer; | |
97 } | |
98 | |
99 const extensions::Extension* extension() const { return extension_; } | |
100 Browser* browser() { return browser_; } | |
101 ExtensionAction* extension_action() { return extension_action_; } | |
102 const ExtensionAction* extension_action() const { return extension_action_; } | |
103 ExtensionPopup* popup() { return popup_; } | |
104 | |
105 private: | |
106 // ExtensionActionIconFactory::Observer: | |
107 virtual void OnIconUpdated() override; | |
108 | |
109 // ui::AcceleratorTarget: | |
110 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | |
111 virtual bool CanHandleAccelerators() const override; | |
112 | |
113 // views::WidgetObserver: | |
114 virtual void OnWidgetDestroying(views::Widget* widget) override; | |
115 | |
116 // views::ContextMenuController: | |
117 virtual void ShowContextMenuForView(views::View* source, | |
118 const gfx::Point& point, | |
119 ui::MenuSourceType source_type) override; | |
120 | |
121 // content::NotificationObserver: | |
122 virtual void Observe(int type, | |
123 const content::NotificationSource& source, | |
124 const content::NotificationDetails& details) override; | |
125 | |
126 // Shows the context menu for extension action. | |
127 void DoShowContextMenu(ui::MenuSourceType source_type); | |
128 | |
129 // Shows the popup for the extension action, given the associated |popup_url|. | |
130 // |grant_tab_permissions| is true if active tab permissions should be given | |
131 // to the extension; this is only true if the popup is opened through a user | |
132 // action. | |
133 // Returns true if a popup is successfully shown. | |
134 bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action, | |
135 const GURL& popup_url, | |
136 bool grant_tab_permissions); | |
137 | |
138 // Populates |command| with the command associated with |extension|, if one | |
139 // exists. Returns true if |command| was populated. | |
140 bool GetExtensionCommand(extensions::Command* command); | |
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 | |
147 // Closes the currently-active menu, if needed. This is the case when there | |
148 // is an active menu that wouldn't close automatically when a new one is | |
149 // opened. | |
150 // Returns true if a menu was closed, false otherwise. | |
151 bool CloseActiveMenuIfNeeded(); | |
152 | |
153 // Cleans up after the popup. If |close_widget| is true, this will call | |
154 // Widget::Close() on the popup's widget; otherwise it assumes the popup is | |
155 // already closing. | |
156 void CleanupPopup(bool close_widget); | |
157 | |
158 // The extension associated with the action we're displaying. | |
159 const extensions::Extension* extension_; | |
160 | |
161 // The corresponding browser. | |
162 Browser* browser_; | |
163 | |
164 // The browser action this view represents. The ExtensionAction is not owned | |
165 // by this class. | |
166 ExtensionAction* extension_action_; | |
167 | |
168 // Our delegate. | |
169 ToolbarActionViewDelegate* delegate_; | |
170 | |
171 // The object that will be used to get the browser action icon for us. | |
172 // It may load the icon asynchronously (in which case the initial icon | |
173 // returned by the factory will be transparent), so we have to observe it for | |
174 // updates to the icon. | |
175 ExtensionActionIconFactory icon_factory_; | |
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 | |
181 // Responsible for running the menu. | |
182 scoped_ptr<views::MenuRunner> menu_runner_; | |
183 | |
184 // The browser action's popup, if it is visible; NULL otherwise. | |
185 ExtensionPopup* popup_; | |
186 | |
187 // The extension key binding accelerator this extension action is listening | |
188 // for (to show the popup). | |
189 scoped_ptr<ui::Accelerator> action_keybinding_; | |
190 | |
191 content::NotificationRegistrar registrar_; | |
192 | |
193 // If non-NULL, this is the next ExtensionActionViewController context menu | |
194 // which wants to run once the current owner (this one) is done. | |
195 base::Closure followup_context_menu_task_; | |
196 | |
197 base::WeakPtrFactory<ExtensionActionViewController> weak_factory_; | |
198 | |
199 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController); | |
200 }; | |
201 | |
202 #endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H
_ | |
OLD | NEW |