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 ~ExtensionActionViewController() override; | |
63 | |
64 // ToolbarActionViewController: | |
65 const std::string& GetId() const override; | |
66 void SetDelegate(ToolbarActionViewDelegate* delegate) override; | |
67 gfx::Image GetIcon(content::WebContents* web_contents) override; | |
68 gfx::ImageSkia GetIconWithBadge() override; | |
69 base::string16 GetAccessibleName( | |
70 content::WebContents* web_contents) const override; | |
71 base::string16 GetTooltip(content::WebContents* web_contents) const override; | |
72 bool IsEnabled(content::WebContents* web_contents) const override; | |
73 bool HasPopup(content::WebContents* web_contents) const override; | |
74 void HidePopup() override; | |
75 gfx::NativeView GetPopupNativeView() override; | |
76 bool IsMenuRunning() const override; | |
77 bool CanDrag() const override; | |
78 bool ExecuteAction(bool by_user) override; | |
79 void PaintExtra(gfx::Canvas* canvas, | |
80 const gfx::Rect& bounds, | |
81 content::WebContents* web_contents) const override; | |
82 void RegisterCommand() override; | |
83 | |
84 // ExtensionContextMenuModel::PopupDelegate: | |
85 void InspectPopup() override; | |
86 | |
87 // Executes the extension action with |show_action|. If | |
88 // |grant_tab_permissions| is true, this will grant the extension active tab | |
89 // permissions. Only do this if this was done through a user action (and not | |
90 // e.g. an API). Returns true if a popup is shown. | |
91 bool ExecuteAction(ExtensionPopup::ShowAction show_action, | |
92 bool grant_tab_permissions); | |
93 | |
94 void set_icon_observer(ExtensionActionIconFactory::Observer* icon_observer) { | |
95 icon_observer_ = icon_observer; | |
96 } | |
97 | |
98 const extensions::Extension* extension() const { return extension_; } | |
99 Browser* browser() { return browser_; } | |
100 ExtensionAction* extension_action() { return extension_action_; } | |
101 const ExtensionAction* extension_action() const { return extension_action_; } | |
102 ExtensionPopup* popup() { return popup_; } | |
103 | |
104 private: | |
105 // ExtensionActionIconFactory::Observer: | |
106 void OnIconUpdated() override; | |
107 | |
108 // ui::AcceleratorTarget: | |
109 bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | |
110 bool CanHandleAccelerators() const override; | |
111 | |
112 // views::WidgetObserver: | |
113 void OnWidgetDestroying(views::Widget* widget) override; | |
114 | |
115 // views::ContextMenuController: | |
116 void ShowContextMenuForView(views::View* source, | |
117 const gfx::Point& point, | |
118 ui::MenuSourceType source_type) override; | |
119 | |
120 // content::NotificationObserver: | |
121 void Observe(int type, | |
122 const content::NotificationSource& source, | |
123 const content::NotificationDetails& details) override; | |
124 | |
125 // Shows the context menu for extension action. | |
126 void DoShowContextMenu(ui::MenuSourceType source_type); | |
127 | |
128 // Shows the popup for the extension action, given the associated |popup_url|. | |
129 // |grant_tab_permissions| is true if active tab permissions should be given | |
130 // to the extension; this is only true if the popup is opened through a user | |
131 // action. | |
132 // Returns true if a popup is successfully shown. | |
133 bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action, | |
134 const GURL& popup_url, | |
135 bool grant_tab_permissions); | |
136 | |
137 // Populates |command| with the command associated with |extension|, if one | |
138 // exists. Returns true if |command| was populated. | |
139 bool GetExtensionCommand(extensions::Command* command); | |
140 | |
141 // Unregisters the accelerator for the extension action's command, if one | |
142 // exists. If |only_if_removed| is true, then this will only unregister if the | |
143 // command has been removed. | |
144 void UnregisterCommand(bool only_if_removed); | |
145 | |
146 // Closes the currently-active menu, if needed. This is the case when there | |
147 // is an active menu that wouldn't close automatically when a new one is | |
148 // opened. | |
149 // Returns true if a menu was closed, false otherwise. | |
150 bool CloseActiveMenuIfNeeded(); | |
151 | |
152 // Cleans up after the popup. If |close_widget| is true, this will call | |
153 // Widget::Close() on the popup's widget; otherwise it assumes the popup is | |
154 // already closing. | |
155 void CleanupPopup(bool close_widget); | |
156 | |
157 // The extension associated with the action we're displaying. | |
158 const extensions::Extension* extension_; | |
159 | |
160 // The corresponding browser. | |
161 Browser* browser_; | |
162 | |
163 // The browser action this view represents. The ExtensionAction is not owned | |
164 // by this class. | |
165 ExtensionAction* extension_action_; | |
166 | |
167 // Our delegate. | |
168 ToolbarActionViewDelegate* delegate_; | |
169 | |
170 // The object that will be used to get the browser action icon for us. | |
171 // It may load the icon asynchronously (in which case the initial icon | |
172 // returned by the factory will be transparent), so we have to observe it for | |
173 // updates to the icon. | |
174 ExtensionActionIconFactory icon_factory_; | |
175 | |
176 // The observer that we need to notify when the icon of the button has been | |
177 // updated. | |
178 ExtensionActionIconFactory::Observer* icon_observer_; | |
179 | |
180 // Responsible for running the menu. | |
181 scoped_ptr<views::MenuRunner> menu_runner_; | |
182 | |
183 // The browser action's popup, if it is visible; NULL otherwise. | |
184 ExtensionPopup* popup_; | |
185 | |
186 // The extension key binding accelerator this extension action is listening | |
187 // for (to show the popup). | |
188 scoped_ptr<ui::Accelerator> action_keybinding_; | |
189 | |
190 content::NotificationRegistrar registrar_; | |
191 | |
192 // If non-NULL, this is the next ExtensionActionViewController context menu | |
193 // which wants to run once the current owner (this one) is done. | |
194 base::Closure followup_context_menu_task_; | |
195 | |
196 base::WeakPtrFactory<ExtensionActionViewController> weak_factory_; | |
197 | |
198 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController); | |
199 }; | |
200 | |
201 #endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H
_ | |
OLD | NEW |