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 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" | 5 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
11 #include "chrome/browser/extensions/extension_action.h" | 11 #include "chrome/browser/extensions/extension_action.h" |
12 #include "chrome/browser/extensions/extension_view.h" | 12 #include "chrome/browser/extensions/extension_view.h" |
13 #include "chrome/browser/extensions/extension_view_host.h" | 13 #include "chrome/browser/extensions/extension_view_host.h" |
14 #include "chrome/browser/extensions/extension_view_host_factory.h" | 14 #include "chrome/browser/extensions/extension_view_host_factory.h" |
15 #include "chrome/browser/extensions/sidebar_container.h" | |
16 #include "chrome/browser/extensions/sidebar_manager.h" | |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/sessions/session_tab_helper.h" | 18 #include "chrome/browser/sessions/session_tab_helper.h" |
17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/extensions/accelerator_priority.h" | 20 #include "chrome/browser/ui/extensions/accelerator_priority.h" |
19 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" | 21 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
20 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 23 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 24 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
22 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | |
23 #include "chrome/common/extensions/api/extension_action/action_info.h" | 25 #include "chrome/common/extensions/api/extension_action/action_info.h" |
26 #include "chrome/common/pref_names.h" | |
24 #include "extensions/browser/extension_host.h" | 27 #include "extensions/browser/extension_host.h" |
25 #include "extensions/browser/extension_registry.h" | 28 #include "extensions/browser/extension_registry.h" |
26 #include "extensions/common/extension.h" | 29 #include "extensions/common/extension.h" |
27 #include "extensions/common/feature_switch.h" | 30 #include "extensions/common/feature_switch.h" |
28 #include "extensions/common/manifest_constants.h" | 31 #include "extensions/common/manifest_constants.h" |
29 #include "ui/gfx/image/image_skia.h" | 32 #include "ui/gfx/image/image_skia.h" |
30 #include "ui/gfx/image/image_skia_operations.h" | 33 #include "ui/gfx/image/image_skia_operations.h" |
31 | 34 |
32 using extensions::ActionInfo; | 35 using extensions::ActionInfo; |
33 using extensions::CommandService; | 36 using extensions::CommandService; |
(...skipping 13 matching lines...) Expand all Loading... | |
47 icon_factory_(browser->profile(), extension, extension_action, this), | 50 icon_factory_(browser->profile(), extension, extension_action, this), |
48 icon_observer_(nullptr), | 51 icon_observer_(nullptr), |
49 extension_registry_( | 52 extension_registry_( |
50 extensions::ExtensionRegistry::Get(browser_->profile())), | 53 extensions::ExtensionRegistry::Get(browser_->profile())), |
51 popup_host_observer_(this), | 54 popup_host_observer_(this), |
52 weak_factory_(this) { | 55 weak_factory_(this) { |
53 DCHECK(extension_action); | 56 DCHECK(extension_action); |
54 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || | 57 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || |
55 extension_action->action_type() == ActionInfo::TYPE_BROWSER); | 58 extension_action->action_type() == ActionInfo::TYPE_BROWSER); |
56 DCHECK(extension); | 59 DCHECK(extension); |
60 | |
61 extensions::SidebarManager* sidebar_manager = | |
62 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
63 sidebar_manager->AddObserver(this); | |
57 } | 64 } |
58 | 65 |
59 ExtensionActionViewController::~ExtensionActionViewController() { | 66 ExtensionActionViewController::~ExtensionActionViewController() { |
60 DCHECK(!is_showing_popup()); | 67 DCHECK(!is_showing_popup()); |
68 | |
69 extensions::SidebarManager* sidebar_manager = | |
70 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
71 | |
72 sidebar_manager->RemoveObserver(this); | |
73 if (GetPreferredPopupViewController() == this) { | |
74 int count = browser_->tab_strip_model()->count(); | |
75 for (int i = 0; i < count; ++i) { | |
76 content::WebContents* contents = | |
77 browser_->tab_strip_model()->GetWebContentsAt(i); | |
78 if (contents) { | |
79 extensions::SidebarContainer* sidebar = | |
80 sidebar_manager->GetSidebarContainerFor(contents); | |
81 if (sidebar && sidebar->extension_id() == GetId()) | |
82 sidebar_manager->HideSidebar(contents); | |
83 } | |
84 } | |
85 } | |
61 } | 86 } |
62 | 87 |
63 const std::string& ExtensionActionViewController::GetId() const { | 88 const std::string& ExtensionActionViewController::GetId() const { |
64 return extension_->id(); | 89 return extension_->id(); |
65 } | 90 } |
66 | 91 |
67 void ExtensionActionViewController::SetDelegate( | 92 void ExtensionActionViewController::SetDelegate( |
68 ToolbarActionViewDelegate* delegate) { | 93 ToolbarActionViewDelegate* delegate) { |
69 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); | 94 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); |
70 if (delegate) { | 95 if (delegate) { |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 return this; | 321 return this; |
297 } | 322 } |
298 | 323 |
299 bool ExtensionActionViewController::TriggerPopupWithUrl( | 324 bool ExtensionActionViewController::TriggerPopupWithUrl( |
300 PopupShowAction show_action, | 325 PopupShowAction show_action, |
301 const GURL& popup_url, | 326 const GURL& popup_url, |
302 bool grant_tab_permissions) { | 327 bool grant_tab_permissions) { |
303 if (!ExtensionIsValid()) | 328 if (!ExtensionIsValid()) |
304 return false; | 329 return false; |
305 | 330 |
306 bool already_showing = is_showing_popup(); | 331 bool already_showing = is_showing_popup(); |
Devlin
2015/07/07 21:39:30
I think we should just re-brand most of these popu
ltilve
2015/07/09 22:15:51
We are having some doubts about mixing both functi
Devlin
2015/07/10 20:18:42
We actually do close the popup, too - it's just ha
ltilve
2015/07/17 16:26:22
We have done some refactoring to increase readabil
| |
332 bool use_sidebar = extension_action_->open_in_sidebar(); | |
307 | 333 |
308 // Always hide the current popup, even if it's not owned by this extension. | 334 // Always hide the current popup, even if it's not owned by this extension. |
309 // Only one popup should be visible at a time. | 335 // Only one popup should be visible at a time. |
310 HideActivePopup(); | 336 HideActivePopup(); |
311 | 337 |
312 // If we were showing a popup already, then we treat the action to open the | 338 // If we were showing a popup already, then we treat the action to open the |
313 // same one as a desire to close it (like clicking a menu button that was | 339 // same one as a desire to close it (like clicking a menu button that was |
314 // already open). | 340 // already open). |
315 if (already_showing) | 341 if (already_showing) |
316 return false; | 342 return false; |
317 | 343 |
344 extensions::SidebarManager* sidebar_manager = | |
345 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
346 content::WebContents* web_contents = view_delegate_->GetCurrentWebContents(); | |
347 | |
348 if (use_sidebar) { | |
349 extensions::SidebarContainer* sidebar = | |
350 sidebar_manager->GetSidebarContainerFor(web_contents); | |
351 if (sidebar && sidebar->extension_id() == GetId()) { | |
352 sidebar_manager->HideSidebar(web_contents); | |
353 return false; | |
354 } | |
355 | |
356 sidebar_manager->AddObserver(this); | |
357 | |
358 sidebar_manager->CreateSidebar(web_contents, popup_url, browser_); | |
359 return true; | |
360 } | |
361 | |
362 // Hide sidebar if visible | |
363 if (sidebar_manager->GetSidebarContainerFor(web_contents) != nullptr) { | |
364 sidebar_manager->HideSidebar(web_contents); | |
365 } | |
366 | |
318 scoped_ptr<extensions::ExtensionViewHost> host( | 367 scoped_ptr<extensions::ExtensionViewHost> host( |
319 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, | 368 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, |
320 browser_)); | 369 browser_)); |
321 if (!host) | 370 if (!host) |
322 return false; | 371 return false; |
323 | 372 |
324 popup_host_ = host.get(); | 373 popup_host_ = host.get(); |
325 popup_host_observer_.Add(popup_host_); | 374 popup_host_observer_.Add(popup_host_); |
326 if (toolbar_actions_bar_) | 375 if (toolbar_actions_bar_) |
327 toolbar_actions_bar_->SetPopupOwner(this); | 376 toolbar_actions_bar_->SetPopupOwner(this); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 popup_host_observer_.Remove(popup_host_); | 410 popup_host_observer_.Remove(popup_host_); |
362 popup_host_ = nullptr; | 411 popup_host_ = nullptr; |
363 if (toolbar_actions_bar_) { | 412 if (toolbar_actions_bar_) { |
364 toolbar_actions_bar_->SetPopupOwner(nullptr); | 413 toolbar_actions_bar_->SetPopupOwner(nullptr); |
365 if (toolbar_actions_bar_->popped_out_action() == this && | 414 if (toolbar_actions_bar_->popped_out_action() == this && |
366 !view_delegate_->IsMenuRunning()) | 415 !view_delegate_->IsMenuRunning()) |
367 toolbar_actions_bar_->UndoPopOut(); | 416 toolbar_actions_bar_->UndoPopOut(); |
368 } | 417 } |
369 view_delegate_->OnPopupClosed(); | 418 view_delegate_->OnPopupClosed(); |
370 } | 419 } |
420 | |
421 void ExtensionActionViewController::OnPopupShown(bool grant_tab_permissions) { | |
422 view_delegate_->OnPopupShown(grant_tab_permissions); | |
423 } | |
424 | |
425 void ExtensionActionViewController::OnSidebarHidden( | |
426 content::WebContents* tab, | |
427 const std::string& content_id) { | |
428 UpdateButtonState(); | |
429 } | |
430 | |
431 void ExtensionActionViewController::UpdateButtonState() { | |
432 if (GetPreferredPopupViewController() != this) | |
433 return; | |
434 | |
435 // Should the button be depressed? | |
436 content::WebContents* current = view_delegate_->GetCurrentWebContents(); | |
437 if (current) { | |
438 extensions::SidebarManager* sidebar_manager = | |
439 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
440 extensions::SidebarContainer* sidebar = | |
441 sidebar_manager->GetSidebarContainerFor(current); | |
442 if (sidebar) { | |
443 if (sidebar->extension_id() == GetId()) { | |
444 toolbar_actions_bar_->SetPopupOwner(this); | |
445 if (toolbar_actions_bar_ && | |
446 !toolbar_actions_bar_->IsActionVisible(this) && | |
447 extensions::FeatureSwitch::extension_action_redesign() | |
448 ->IsEnabled()) { | |
449 platform_delegate_->CloseOverflowMenu(); | |
450 toolbar_actions_bar_->PopOutAction( | |
451 this, base::Bind(&ExtensionActionViewController::OnPopupShown, | |
Devlin
2015/07/07 21:39:30
I don't think we should have actions with a sideba
ltilve
2015/07/09 22:15:51
Could you please elaborate a bit more what you mea
Devlin
2015/07/10 20:18:42
Nah. Having any kind of slid out action for a per
ltilve
2015/07/17 16:26:22
Done. We are not sliding out the browser action no
| |
452 weak_factory_.GetWeakPtr(), true)); | |
453 } else { | |
454 // Without the popup corner arrow indicator, marking the browserAction | |
455 // icon | |
456 // is necessary for extension attribution | |
457 view_delegate_->OnPopupShown(true); | |
458 } | |
459 return; | |
460 } | |
461 } | |
462 } | |
463 | |
464 // Reset button state | |
465 if (toolbar_actions_bar_) { | |
466 toolbar_actions_bar_->SetPopupOwner(nullptr); | |
467 if (toolbar_actions_bar_->popped_out_action() == this && | |
468 !view_delegate_->IsMenuRunning()) { | |
469 toolbar_actions_bar_->UndoPopOut(); | |
470 } | |
471 } | |
472 view_delegate_->OnPopupClosed(); | |
473 } | |
OLD | NEW |