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 #include "chrome/browser/ui/views/extensions/extension_action_view_controller.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | |
10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | |
11 #include "chrome/browser/extensions/extension_action.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/sessions/session_tab_helper.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/extensions/accelerator_priority.h" | |
16 #include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate.h" | |
17 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | |
18 #include "chrome/common/extensions/api/extension_action/action_info.h" | |
19 #include "content/public/browser/notification_details.h" | |
20 #include "content/public/browser/notification_source.h" | |
21 #include "extensions/browser/notification_types.h" | |
22 #include "extensions/common/extension.h" | |
23 #include "extensions/common/manifest_constants.h" | |
24 #include "ui/gfx/image/image_skia.h" | |
25 #include "ui/gfx/image/image_skia_operations.h" | |
26 #include "ui/views/controls/menu/menu_controller.h" | |
27 #include "ui/views/controls/menu/menu_runner.h" | |
28 #include "ui/views/view.h" | |
29 #include "ui/views/widget/widget.h" | |
30 | |
31 using extensions::ActionInfo; | |
32 using extensions::CommandService; | |
33 | |
34 namespace { | |
35 | |
36 // The ExtensionActionViewController which is currently showing its context | |
37 // menu, if any. | |
38 // Since only one context menu can be shown (even across browser windows), it's | |
39 // safe to have this be a global singleton. | |
40 ExtensionActionViewController* context_menu_owner = NULL; | |
41 | |
42 } // namespace | |
43 | |
44 ExtensionActionViewController::ExtensionActionViewController( | |
45 const extensions::Extension* extension, | |
46 Browser* browser, | |
47 ExtensionAction* extension_action) | |
48 : extension_(extension), | |
49 browser_(browser), | |
50 extension_action_(extension_action), | |
51 delegate_(nullptr), | |
52 icon_factory_(browser->profile(), extension, extension_action, this), | |
53 icon_observer_(nullptr), | |
54 popup_(nullptr), | |
55 weak_factory_(this) { | |
56 DCHECK(extension_action); | |
57 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || | |
58 extension_action->action_type() == ActionInfo::TYPE_BROWSER); | |
59 DCHECK(extension); | |
60 | |
61 content::NotificationSource notification_source = | |
62 content::Source<Profile>(browser->profile()->GetOriginalProfile()); | |
63 registrar_.Add(this, | |
64 extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED, | |
65 notification_source); | |
66 registrar_.Add(this, | |
67 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | |
68 notification_source); | |
69 } | |
70 | |
71 ExtensionActionViewController::~ExtensionActionViewController() { | |
72 if (context_menu_owner == this) | |
73 context_menu_owner = NULL; | |
74 HidePopup(); | |
75 UnregisterCommand(false); | |
76 } | |
77 | |
78 const std::string& ExtensionActionViewController::GetId() const { | |
79 return extension_->id(); | |
80 } | |
81 | |
82 void ExtensionActionViewController::SetDelegate( | |
83 ToolbarActionViewDelegate* delegate) { | |
84 delegate_ = delegate; | |
85 delegate_->GetAsView()->set_context_menu_controller(this); | |
86 } | |
87 | |
88 gfx::Image ExtensionActionViewController::GetIcon( | |
89 content::WebContents* web_contents) { | |
90 return icon_factory_.GetIcon(SessionTabHelper::IdForTab(web_contents)); | |
91 } | |
92 | |
93 gfx::ImageSkia ExtensionActionViewController::GetIconWithBadge() { | |
94 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); | |
95 gfx::Size spacing(0, ToolbarView::kVertSpacing); | |
96 gfx::ImageSkia icon = *GetIcon(web_contents).ToImageSkia(); | |
97 if (!IsEnabled(web_contents)) | |
98 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | |
99 return extension_action_->GetIconWithBadge( | |
100 icon, SessionTabHelper::IdForTab(web_contents), spacing); | |
101 } | |
102 | |
103 base::string16 ExtensionActionViewController::GetAccessibleName( | |
104 content::WebContents* web_contents) const { | |
105 std::string title = | |
106 extension_action()->GetTitle(SessionTabHelper::IdForTab(web_contents)); | |
107 return base::UTF8ToUTF16(title.empty() ? extension()->name() : title); | |
108 } | |
109 | |
110 base::string16 ExtensionActionViewController::GetTooltip( | |
111 content::WebContents* web_contents) const { | |
112 return GetAccessibleName(web_contents); | |
113 } | |
114 | |
115 bool ExtensionActionViewController::IsEnabled( | |
116 content::WebContents* web_contents) const { | |
117 return extension_action_->GetIsVisible( | |
118 SessionTabHelper::IdForTab(web_contents)); | |
119 } | |
120 | |
121 bool ExtensionActionViewController::HasPopup( | |
122 content::WebContents* web_contents) const { | |
123 int tab_id = SessionTabHelper::IdForTab(web_contents); | |
124 return (tab_id < 0) ? false : extension_action_->HasPopup(tab_id); | |
125 } | |
126 | |
127 void ExtensionActionViewController::HidePopup() { | |
128 if (popup_) | |
129 CleanupPopup(true); | |
130 } | |
131 | |
132 gfx::NativeView ExtensionActionViewController::GetPopupNativeView() { | |
133 return popup_ ? popup_->GetWidget()->GetNativeView() : nullptr; | |
134 } | |
135 | |
136 bool ExtensionActionViewController::IsMenuRunning() const { | |
137 return menu_runner_.get() != NULL; | |
138 } | |
139 | |
140 bool ExtensionActionViewController::CanDrag() const { | |
141 return true; | |
142 } | |
143 | |
144 bool ExtensionActionViewController::ExecuteAction(bool by_user) { | |
145 return ExecuteAction(ExtensionPopup::SHOW, by_user); | |
146 } | |
147 | |
148 void ExtensionActionViewController::PaintExtra( | |
149 gfx::Canvas* canvas, | |
150 const gfx::Rect& bounds, | |
151 content::WebContents* web_contents) const { | |
152 int tab_id = SessionTabHelper::IdForTab(web_contents); | |
153 if (tab_id >= 0) | |
154 extension_action_->PaintBadge(canvas, bounds, tab_id); | |
155 } | |
156 | |
157 void ExtensionActionViewController::RegisterCommand() { | |
158 // If we've already registered, do nothing. | |
159 if (action_keybinding_.get()) | |
160 return; | |
161 | |
162 extensions::Command extension_command; | |
163 views::FocusManager* focus_manager = | |
164 delegate_->GetFocusManagerForAccelerator(); | |
165 if (focus_manager && GetExtensionCommand(&extension_command)) { | |
166 action_keybinding_.reset( | |
167 new ui::Accelerator(extension_command.accelerator())); | |
168 focus_manager->RegisterAccelerator( | |
169 *action_keybinding_, | |
170 GetAcceleratorPriority(extension_command.accelerator(), extension_), | |
171 this); | |
172 } | |
173 } | |
174 | |
175 void ExtensionActionViewController::InspectPopup() { | |
176 ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT, true); | |
177 } | |
178 | |
179 bool ExtensionActionViewController::ExecuteAction( | |
180 ExtensionPopup::ShowAction show_action, bool grant_tab_permissions) { | |
181 if (extensions::ExtensionActionAPI::Get(browser_->profile())-> | |
182 ExecuteExtensionAction(extension_, browser_, grant_tab_permissions) == | |
183 ExtensionAction::ACTION_SHOW_POPUP) { | |
184 GURL popup_url = extension_action_->GetPopupUrl( | |
185 SessionTabHelper::IdForTab(delegate_->GetCurrentWebContents())); | |
186 return static_cast<ExtensionActionViewController*>( | |
187 delegate_->GetPreferredPopupViewController())->ShowPopupWithUrl( | |
188 show_action, popup_url, grant_tab_permissions); | |
189 } | |
190 return false; | |
191 } | |
192 | |
193 void ExtensionActionViewController::OnIconUpdated() { | |
194 delegate_->OnIconUpdated(); | |
195 if (icon_observer_) | |
196 icon_observer_->OnIconUpdated(); | |
197 } | |
198 | |
199 bool ExtensionActionViewController::AcceleratorPressed( | |
200 const ui::Accelerator& accelerator) { | |
201 // We shouldn't be handling any accelerators if the view is hidden, unless | |
202 // this is a browser action. | |
203 DCHECK(extension_action_->action_type() == ActionInfo::TYPE_BROWSER || | |
204 delegate_->GetAsView()->visible()); | |
205 | |
206 // Normal priority shortcuts must be handled via standard browser commands to | |
207 // be processed at the proper time. | |
208 if (GetAcceleratorPriority(accelerator, extension()) == | |
209 ui::AcceleratorManager::kNormalPriority) | |
210 return false; | |
211 | |
212 ExecuteAction(true); | |
213 return true; | |
214 } | |
215 | |
216 bool ExtensionActionViewController::CanHandleAccelerators() const { | |
217 // Page actions can only handle accelerators when they are visible. | |
218 // Browser actions can handle accelerators even when not visible, since they | |
219 // might be hidden in an overflow menu. | |
220 return extension_action_->action_type() == ActionInfo::TYPE_PAGE ? | |
221 delegate_->GetAsView()->visible() : true; | |
222 } | |
223 | |
224 void ExtensionActionViewController::OnWidgetDestroying(views::Widget* widget) { | |
225 DCHECK(popup_); | |
226 DCHECK_EQ(popup_->GetWidget(), widget); | |
227 CleanupPopup(false); | |
228 } | |
229 | |
230 void ExtensionActionViewController::ShowContextMenuForView( | |
231 views::View* source, | |
232 const gfx::Point& point, | |
233 ui::MenuSourceType source_type) { | |
234 // If there's another active menu that won't be dismissed by opening this one, | |
235 // then we can't show this one right away, since we can only show one nested | |
236 // menu at a time. | |
237 // If the other menu is an extension action's context menu, then we'll run | |
238 // this one after that one closes. If it's a different type of menu, then we | |
239 // close it and give up, for want of a better solution. (Luckily, this is | |
240 // rare). | |
241 // TODO(devlin): Update this when views code no longer runs menus in a nested | |
242 // loop. | |
243 if (context_menu_owner) { | |
244 context_menu_owner->followup_context_menu_task_ = | |
245 base::Bind(&ExtensionActionViewController::DoShowContextMenu, | |
246 weak_factory_.GetWeakPtr(), | |
247 source_type); | |
248 } | |
249 if (CloseActiveMenuIfNeeded()) | |
250 return; | |
251 | |
252 // Otherwise, no other menu is showing, and we can proceed normally. | |
253 DoShowContextMenu(source_type); | |
254 } | |
255 | |
256 void ExtensionActionViewController::Observe( | |
257 int type, | |
258 const content::NotificationSource& source, | |
259 const content::NotificationDetails& details) { | |
260 DCHECK(type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED || | |
261 type == extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED); | |
262 std::pair<const std::string, const std::string>* payload = | |
263 content::Details<std::pair<const std::string, const std::string> >( | |
264 details).ptr(); | |
265 if (extension_->id() == payload->first && | |
266 payload->second == | |
267 extensions::manifest_values::kBrowserActionCommandEvent) { | |
268 if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED) | |
269 RegisterCommand(); | |
270 else | |
271 UnregisterCommand(true); | |
272 } | |
273 } | |
274 | |
275 void ExtensionActionViewController::DoShowContextMenu( | |
276 ui::MenuSourceType source_type) { | |
277 if (!extension_->ShowConfigureContextMenus()) | |
278 return; | |
279 | |
280 DCHECK(!context_menu_owner); | |
281 context_menu_owner = this; | |
282 | |
283 // We shouldn't have both a popup and a context menu showing. | |
284 delegate_->HideActivePopup(); | |
285 | |
286 // Reconstructs the menu every time because the menu's contents are dynamic. | |
287 scoped_refptr<ExtensionContextMenuModel> context_menu_model( | |
288 new ExtensionContextMenuModel(extension_, browser_, this)); | |
289 | |
290 gfx::Point screen_loc; | |
291 views::View::ConvertPointToScreen(delegate_->GetAsView(), &screen_loc); | |
292 | |
293 int run_types = views::MenuRunner::HAS_MNEMONICS | | |
294 views::MenuRunner::CONTEXT_MENU; | |
295 if (delegate_->IsShownInMenu()) | |
296 run_types |= views::MenuRunner::IS_NESTED; | |
297 | |
298 views::Widget* parent = delegate_->GetParentForContextMenu(); | |
299 | |
300 menu_runner_.reset( | |
301 new views::MenuRunner(context_menu_model.get(), run_types)); | |
302 | |
303 if (menu_runner_->RunMenuAt( | |
304 parent, | |
305 delegate_->GetContextMenuButton(), | |
306 gfx::Rect(screen_loc, delegate_->GetAsView()->size()), | |
307 views::MENU_ANCHOR_TOPLEFT, | |
308 source_type) == views::MenuRunner::MENU_DELETED) { | |
309 return; | |
310 } | |
311 | |
312 context_menu_owner = NULL; | |
313 menu_runner_.reset(); | |
314 | |
315 // If another extension action wants to show its context menu, allow it to. | |
316 if (!followup_context_menu_task_.is_null()) { | |
317 base::Closure task = followup_context_menu_task_; | |
318 followup_context_menu_task_ = base::Closure(); | |
319 task.Run(); | |
320 } | |
321 } | |
322 | |
323 bool ExtensionActionViewController::ShowPopupWithUrl( | |
324 ExtensionPopup::ShowAction show_action, | |
325 const GURL& popup_url, | |
326 bool grant_tab_permissions) { | |
327 // If we're already showing the popup for this browser action, just hide it | |
328 // and return. | |
329 bool already_showing = popup_ != NULL; | |
330 | |
331 // Always hide the current popup, even if it's not the same. | |
332 // Only one popup should be visible at a time. | |
333 delegate_->HideActivePopup(); | |
334 | |
335 // Similarly, don't allow a context menu and a popup to be showing | |
336 // simultaneously. | |
337 CloseActiveMenuIfNeeded(); | |
338 | |
339 if (already_showing) | |
340 return false; | |
341 | |
342 views::BubbleBorder::Arrow arrow = base::i18n::IsRTL() ? | |
343 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | |
344 | |
345 views::View* reference_view = delegate_->GetReferenceViewForPopup(); | |
346 | |
347 popup_ = ExtensionPopup::ShowPopup( | |
348 popup_url, browser_, reference_view, arrow, show_action); | |
349 popup_->GetWidget()->AddObserver(this); | |
350 | |
351 delegate_->OnPopupShown(grant_tab_permissions); | |
352 | |
353 return true; | |
354 } | |
355 | |
356 bool ExtensionActionViewController::GetExtensionCommand( | |
357 extensions::Command* command) { | |
358 DCHECK(command); | |
359 CommandService* command_service = CommandService::Get(browser_->profile()); | |
360 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { | |
361 return command_service->GetPageActionCommand( | |
362 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); | |
363 } | |
364 return command_service->GetBrowserActionCommand( | |
365 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); | |
366 } | |
367 | |
368 void ExtensionActionViewController::UnregisterCommand(bool only_if_removed) { | |
369 views::FocusManager* focus_manager = | |
370 delegate_->GetFocusManagerForAccelerator(); | |
371 if (!focus_manager || !action_keybinding_.get()) | |
372 return; | |
373 | |
374 // If |only_if_removed| is true, it means that we only need to unregister | |
375 // ourselves as an accelerator if the command was removed. Otherwise, we need | |
376 // to unregister ourselves no matter what (likely because we are shutting | |
377 // down). | |
378 extensions::Command extension_command; | |
379 if (!only_if_removed || !GetExtensionCommand(&extension_command)) { | |
380 focus_manager->UnregisterAccelerator(*action_keybinding_, this); | |
381 action_keybinding_.reset(); | |
382 } | |
383 } | |
384 | |
385 bool ExtensionActionViewController::CloseActiveMenuIfNeeded() { | |
386 // If this view is shown inside another menu, there's a possibility that there | |
387 // is another context menu showing that we have to close before we can | |
388 // activate a different menu. | |
389 if (delegate_->IsShownInMenu()) { | |
390 views::MenuController* menu_controller = | |
391 views::MenuController::GetActiveInstance(); | |
392 // If this is shown inside a menu, then there should always be an active | |
393 // menu controller. | |
394 DCHECK(menu_controller); | |
395 if (menu_controller->in_nested_run()) { | |
396 // There is another menu showing. Close the outermost menu (since we are | |
397 // shown in the same menu, we don't want to close the whole thing). | |
398 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | |
399 return true; | |
400 } | |
401 } | |
402 | |
403 return false; | |
404 } | |
405 | |
406 void ExtensionActionViewController::CleanupPopup(bool close_widget) { | |
407 DCHECK(popup_); | |
408 delegate_->CleanupPopup(); | |
409 popup_->GetWidget()->RemoveObserver(this); | |
410 if (close_widget) | |
411 popup_->GetWidget()->Close(); | |
412 popup_ = NULL; | |
413 } | |
OLD | NEW |