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/prefs/pref_service.h" | |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 10 #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/api/extension_action/extension_action_api.h" |
11 #include "chrome/browser/extensions/extension_action.h" | 12 #include "chrome/browser/extensions/extension_action.h" |
12 #include "chrome/browser/extensions/extension_view.h" | 13 #include "chrome/browser/extensions/extension_view.h" |
13 #include "chrome/browser/extensions/extension_view_host.h" | 14 #include "chrome/browser/extensions/extension_view_host.h" |
14 #include "chrome/browser/extensions/extension_view_host_factory.h" | 15 #include "chrome/browser/extensions/extension_view_host_factory.h" |
16 #include "chrome/browser/extensions/sidebar_container.h" | |
17 #include "chrome/browser/extensions/sidebar_manager.h" | |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/sessions/session_tab_helper.h" | 19 #include "chrome/browser/sessions/session_tab_helper.h" |
17 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/extensions/accelerator_priority.h" | 21 #include "chrome/browser/ui/extensions/accelerator_priority.h" |
19 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" | 22 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
24 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
20 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 25 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 26 #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" | 27 #include "chrome/common/extensions/api/extension_action/action_info.h" |
28 #include "chrome/common/pref_names.h" | |
24 #include "extensions/browser/extension_host.h" | 29 #include "extensions/browser/extension_host.h" |
25 #include "extensions/browser/extension_registry.h" | 30 #include "extensions/browser/extension_registry.h" |
26 #include "extensions/common/extension.h" | 31 #include "extensions/common/extension.h" |
27 #include "extensions/common/feature_switch.h" | 32 #include "extensions/common/feature_switch.h" |
28 #include "extensions/common/manifest_constants.h" | 33 #include "extensions/common/manifest_constants.h" |
29 #include "ui/gfx/image/image_skia.h" | 34 #include "ui/gfx/image/image_skia.h" |
30 #include "ui/gfx/image/image_skia_operations.h" | 35 #include "ui/gfx/image/image_skia_operations.h" |
31 | 36 |
32 using extensions::ActionInfo; | 37 using extensions::ActionInfo; |
33 using extensions::CommandService; | 38 using extensions::CommandService; |
(...skipping 17 matching lines...) Expand all Loading... | |
51 popup_host_observer_(this), | 56 popup_host_observer_(this), |
52 weak_factory_(this) { | 57 weak_factory_(this) { |
53 DCHECK(extension_action); | 58 DCHECK(extension_action); |
54 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || | 59 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || |
55 extension_action->action_type() == ActionInfo::TYPE_BROWSER); | 60 extension_action->action_type() == ActionInfo::TYPE_BROWSER); |
56 DCHECK(extension); | 61 DCHECK(extension); |
57 } | 62 } |
58 | 63 |
59 ExtensionActionViewController::~ExtensionActionViewController() { | 64 ExtensionActionViewController::~ExtensionActionViewController() { |
60 DCHECK(!is_showing_popup()); | 65 DCHECK(!is_showing_popup()); |
66 | |
67 extensions::SidebarManager* sidebar_manager = | |
68 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
69 | |
70 sidebar_manager->RemoveObserver(this); | |
Devlin
2015/06/19 19:56:10
ScopedObserver
ltilve
2015/06/28 22:44:20
Done.
| |
71 for (std::set<content::WebContents*>::iterator it = | |
72 active_in_webcontents_.begin(); | |
73 it != active_in_webcontents_.end(); ++it) | |
74 sidebar_manager->HideSidebar(*it); | |
61 } | 75 } |
62 | 76 |
63 const std::string& ExtensionActionViewController::GetId() const { | 77 const std::string& ExtensionActionViewController::GetId() const { |
64 return extension_->id(); | 78 return extension_->id(); |
65 } | 79 } |
66 | 80 |
67 void ExtensionActionViewController::SetDelegate( | 81 void ExtensionActionViewController::SetDelegate( |
68 ToolbarActionViewDelegate* delegate) { | 82 ToolbarActionViewDelegate* delegate) { |
69 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); | 83 DCHECK((delegate == nullptr) ^ (view_delegate_ == nullptr)); |
70 if (delegate) { | 84 if (delegate) { |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 } | 311 } |
298 | 312 |
299 bool ExtensionActionViewController::TriggerPopupWithUrl( | 313 bool ExtensionActionViewController::TriggerPopupWithUrl( |
300 PopupShowAction show_action, | 314 PopupShowAction show_action, |
301 const GURL& popup_url, | 315 const GURL& popup_url, |
302 bool grant_tab_permissions) { | 316 bool grant_tab_permissions) { |
303 if (!ExtensionIsValid()) | 317 if (!ExtensionIsValid()) |
304 return false; | 318 return false; |
305 | 319 |
306 bool already_showing = is_showing_popup(); | 320 bool already_showing = is_showing_popup(); |
321 bool use_sidebar = extension_action_->open_in_sidebar(); | |
307 | 322 |
308 // Always hide the current popup, even if it's not owned by this extension. | 323 // Always hide the current popup, even if it's not owned by this extension. |
309 // Only one popup should be visible at a time. | 324 // Only one popup should be visible at a time. |
310 HideActivePopup(); | 325 if (!use_sidebar) |
Devlin
2015/06/19 19:56:10
This raises the question of if a popup and a sideb
ltilve
2015/06/28 22:44:20
Done, popups and sidebars should not be showing at
Devlin
2015/07/07 21:39:29
Why don't you think popups and sidebars should be
ltilve
2015/07/09 22:15:50
Done. I had misunderstood the ownership issue. We
| |
326 HideActivePopup(); | |
311 | 327 |
312 // If we were showing a popup already, then we treat the action to open the | 328 // 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 | 329 // same one as a desire to close it (like clicking a menu button that was |
314 // already open). | 330 // already open). |
315 if (already_showing) | 331 if (already_showing) |
316 return false; | 332 return false; |
317 | 333 |
334 if (use_sidebar) { | |
335 extensions::SidebarManager* sidebar_manager = | |
336 extensions::SidebarManager::GetFromContext(browser_->profile()); | |
337 | |
338 content::WebContents* web_contents = | |
339 view_delegate_->GetCurrentWebContents(); | |
340 | |
341 extensions::SidebarContainer* sidebar = | |
342 sidebar_manager->GetSidebarContainerFor(web_contents); | |
343 if (sidebar && sidebar->extension_id() == GetId()) { | |
344 sidebar_manager->HideSidebar(web_contents); | |
345 return false; | |
346 } | |
347 | |
348 active_in_webcontents_.insert(web_contents); | |
349 sidebar_manager->AddObserver(this); | |
350 | |
351 sidebar_manager->CreateSidebar(web_contents, popup_url, browser_); | |
352 return true; | |
Devlin
2015/06/19 19:56:10
I think this should still handle sliding out the a
ltilve
2015/06/28 22:44:20
Done. After the previous change, the attribution p
| |
353 } | |
354 | |
318 scoped_ptr<extensions::ExtensionViewHost> host( | 355 scoped_ptr<extensions::ExtensionViewHost> host( |
319 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, | 356 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, |
320 browser_)); | 357 browser_)); |
321 if (!host) | 358 if (!host) |
322 return false; | 359 return false; |
323 | 360 |
324 popup_host_ = host.get(); | 361 popup_host_ = host.get(); |
325 popup_host_observer_.Add(popup_host_); | 362 popup_host_observer_.Add(popup_host_); |
326 if (toolbar_actions_bar_) | 363 if (toolbar_actions_bar_) |
327 toolbar_actions_bar_->SetPopupOwner(this); | 364 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_); | 398 popup_host_observer_.Remove(popup_host_); |
362 popup_host_ = nullptr; | 399 popup_host_ = nullptr; |
363 if (toolbar_actions_bar_) { | 400 if (toolbar_actions_bar_) { |
364 toolbar_actions_bar_->SetPopupOwner(nullptr); | 401 toolbar_actions_bar_->SetPopupOwner(nullptr); |
365 if (toolbar_actions_bar_->popped_out_action() == this && | 402 if (toolbar_actions_bar_->popped_out_action() == this && |
366 !view_delegate_->IsMenuRunning()) | 403 !view_delegate_->IsMenuRunning()) |
367 toolbar_actions_bar_->UndoPopOut(); | 404 toolbar_actions_bar_->UndoPopOut(); |
368 } | 405 } |
369 view_delegate_->OnPopupClosed(); | 406 view_delegate_->OnPopupClosed(); |
370 } | 407 } |
408 | |
409 void ExtensionActionViewController::OnPopupShown(bool grant_tab_permissions) { | |
410 view_delegate_->OnPopupShown(grant_tab_permissions); | |
411 } | |
412 | |
413 void ExtensionActionViewController::OnSidebarHidden( | |
414 content::WebContents* tab, | |
415 const std::string& content_id) { | |
416 if (view_delegate_->GetCurrentWebContents() == tab && content_id == GetId()) { | |
417 view_delegate_->OnPopupClosed(); | |
418 active_in_webcontents_.erase( | |
419 active_in_webcontents_.find(view_delegate_->GetCurrentWebContents())); | |
420 if (active_in_webcontents_.size() == 0) { | |
421 extensions::SidebarManager::GetFromContext(browser_->profile()) | |
422 ->RemoveObserver(this); | |
423 | |
424 if (toolbar_actions_bar_) { | |
425 toolbar_actions_bar_->SetPopupOwner(nullptr); | |
426 if (toolbar_actions_bar_->popped_out_action() == this && | |
427 !view_delegate_->IsMenuRunning()) | |
428 toolbar_actions_bar_->UndoPopOut(); | |
429 } | |
430 } | |
431 } | |
432 } | |
433 | |
434 void ExtensionActionViewController::OnSidebarSwitched( | |
435 content::WebContents* old_tab, | |
436 const std::string& old_content_id, | |
437 content::WebContents* new_tab, | |
438 const std::string& new_content_id) { | |
439 if (browser_->tab_strip_model()->GetIndexOfWebContents( | |
440 new_tab ? new_tab : old_tab) == TabStripModel::kNoTab) | |
441 return; | |
442 | |
443 if (old_content_id == GetId() && old_content_id != new_content_id) { | |
Devlin
2015/06/19 19:56:10
Under which circumstances would a sidebar be switc
ltilve
2015/06/28 22:44:20
Done. That code was refactored into a clearer Upda
| |
444 if (toolbar_actions_bar_) { | |
445 toolbar_actions_bar_->SetPopupOwner(nullptr); | |
446 if (toolbar_actions_bar_->popped_out_action() == this && | |
447 !view_delegate_->IsMenuRunning()) | |
448 toolbar_actions_bar_->UndoPopOut(); | |
449 } | |
450 view_delegate_->OnPopupClosed(); | |
451 } | |
452 | |
453 if (view_delegate_->GetCurrentWebContents() == new_tab && | |
454 new_content_id == GetId() && old_content_id != new_content_id) { | |
455 if (toolbar_actions_bar_) | |
456 toolbar_actions_bar_->SetPopupOwner(this); | |
457 if (toolbar_actions_bar_ && !toolbar_actions_bar_->IsActionVisible(this) && | |
458 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) { | |
459 platform_delegate_->CloseOverflowMenu(); | |
460 toolbar_actions_bar_->PopOutAction( | |
461 this, base::Bind(&ExtensionActionViewController::OnPopupShown, | |
462 weak_factory_.GetWeakPtr(), true)); | |
463 } else { | |
464 // Without the popup corner arrow indicator, marking the browserAction | |
465 // icon | |
466 // is necessary for extension attribution | |
467 view_delegate_->OnPopupShown(true); | |
468 } | |
469 } | |
470 | |
471 if (old_tab == new_tab && old_content_id == GetId()) | |
472 OnSidebarHidden(old_tab, old_content_id); | |
473 } | |
OLD | NEW |