| 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/toolbar/toolbar_actions_bar.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/profiler/scoped_tracker.h" | 11 #include "base/profiler/scoped_tracker.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "chrome/browser/extensions/extension_message_bubble_controller.h" | 14 #include "chrome/browser/extensions/extension_message_bubble_controller.h" |
| 15 #include "chrome/browser/extensions/extension_util.h" | 15 #include "chrome/browser/extensions/extension_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/sessions/session_tab_helper.h" | 17 #include "chrome/browser/sessions/session_tab_helper.h" |
| 18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" | 20 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" |
| 21 #include "chrome/browser/ui/extensions/extension_message_bubble_bridge.h" |
| 21 #include "chrome/browser/ui/extensions/extension_message_bubble_factory.h" | 22 #include "chrome/browser/ui/extensions/extension_message_bubble_factory.h" |
| 22 #include "chrome/browser/ui/layout_constants.h" | 23 #include "chrome/browser/ui/layout_constants.h" |
| 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 25 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 25 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 26 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| 26 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" | 27 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" |
| 27 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_observer.h" | 28 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_observer.h" |
| 28 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 29 #include "components/crx_file/id_util.h" | 30 #include "components/crx_file/id_util.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" | 31 #include "components/pref_registry/pref_registry_syncable.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 main_bar_(main_bar), | 119 main_bar_(main_bar), |
| 119 platform_settings_(), | 120 platform_settings_(), |
| 120 popup_owner_(nullptr), | 121 popup_owner_(nullptr), |
| 121 model_observer_(this), | 122 model_observer_(this), |
| 122 suppress_layout_(false), | 123 suppress_layout_(false), |
| 123 suppress_animation_(true), | 124 suppress_animation_(true), |
| 124 checked_extension_bubble_(false), | 125 checked_extension_bubble_(false), |
| 125 is_drag_in_progress_(false), | 126 is_drag_in_progress_(false), |
| 126 popped_out_action_(nullptr), | 127 popped_out_action_(nullptr), |
| 127 is_popped_out_sticky_(false), | 128 is_popped_out_sticky_(false), |
| 129 is_showing_bubble_(false), |
| 128 weak_ptr_factory_(this) { | 130 weak_ptr_factory_(this) { |
| 129 if (model_) // |model_| can be null in unittests. | 131 if (model_) // |model_| can be null in unittests. |
| 130 model_observer_.Add(model_); | 132 model_observer_.Add(model_); |
| 131 } | 133 } |
| 132 | 134 |
| 133 ToolbarActionsBar::~ToolbarActionsBar() { | 135 ToolbarActionsBar::~ToolbarActionsBar() { |
| 134 // We don't just call DeleteActions() here because it makes assumptions about | 136 // We don't just call DeleteActions() here because it makes assumptions about |
| 135 // the order of deletion between the views and the ToolbarActionsBar. | 137 // the order of deletion between the views and the ToolbarActionsBar. |
| 136 DCHECK(toolbar_actions_.empty()) << | 138 DCHECK(toolbar_actions_.empty()) << |
| 137 "Must call DeleteActions() before destruction."; | 139 "Must call DeleteActions() before destruction."; |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 else if (drag_type == DRAG_TO_MAIN && | 484 else if (drag_type == DRAG_TO_MAIN && |
| 483 dragged_index >= static_cast<int>(model_->visible_icon_count())) | 485 dragged_index >= static_cast<int>(model_->visible_icon_count())) |
| 484 delta = 1; | 486 delta = 1; |
| 485 model_->MoveActionIcon(toolbar_actions_[dragged_index]->GetId(), | 487 model_->MoveActionIcon(toolbar_actions_[dragged_index]->GetId(), |
| 486 dropped_index); | 488 dropped_index); |
| 487 if (delta) | 489 if (delta) |
| 488 model_->SetVisibleIconCount(model_->visible_icon_count() + delta); | 490 model_->SetVisibleIconCount(model_->visible_icon_count() + delta); |
| 489 } | 491 } |
| 490 | 492 |
| 491 void ToolbarActionsBar::OnAnimationEnded() { | 493 void ToolbarActionsBar::OnAnimationEnded() { |
| 494 // Notify the observers now, since showing a bubble or popup could potentially |
| 495 // cause another animation to start. |
| 496 FOR_EACH_OBSERVER(ToolbarActionsBarObserver, observers_, |
| 497 OnToolbarActionsBarAnimationEnded()); |
| 498 |
| 492 // Check if we were waiting for animation to complete to either show a | 499 // Check if we were waiting for animation to complete to either show a |
| 493 // message bubble, or to show a popup. | 500 // message bubble, or to show a popup. |
| 494 if (pending_extension_bubble_controller_) { | 501 if (pending_bubble_controller_) { |
| 495 MaybeShowExtensionBubble(std::move(pending_extension_bubble_controller_)); | 502 ShowToolbarActionBubble(std::move(pending_bubble_controller_)); |
| 496 } else if (pending_toolbar_bubble_controller_) { | |
| 497 ShowToolbarActionBubble(std::move(pending_toolbar_bubble_controller_)); | |
| 498 } else if (!popped_out_closure_.is_null()) { | 503 } else if (!popped_out_closure_.is_null()) { |
| 499 popped_out_closure_.Run(); | 504 popped_out_closure_.Run(); |
| 500 popped_out_closure_.Reset(); | 505 popped_out_closure_.Reset(); |
| 501 } | 506 } |
| 502 FOR_EACH_OBSERVER(ToolbarActionsBarObserver, observers_, | 507 } |
| 503 OnToolbarActionsBarAnimationEnded()); | 508 |
| 509 void ToolbarActionsBar::OnBubbleClosed() { |
| 510 is_showing_bubble_ = false; |
| 504 } | 511 } |
| 505 | 512 |
| 506 bool ToolbarActionsBar::IsActionVisibleOnMainBar( | 513 bool ToolbarActionsBar::IsActionVisibleOnMainBar( |
| 507 const ToolbarActionViewController* action) const { | 514 const ToolbarActionViewController* action) const { |
| 508 if (in_overflow_mode()) | 515 if (in_overflow_mode()) |
| 509 return main_bar_->IsActionVisibleOnMainBar(action); | 516 return main_bar_->IsActionVisibleOnMainBar(action); |
| 510 | 517 |
| 511 size_t index = std::find(toolbar_actions_.begin(), | 518 size_t index = std::find(toolbar_actions_.begin(), |
| 512 toolbar_actions_.end(), | 519 toolbar_actions_.end(), |
| 513 action) - toolbar_actions_.begin(); | 520 action) - toolbar_actions_.begin(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 void ToolbarActionsBar::AddObserver(ToolbarActionsBarObserver* observer) { | 581 void ToolbarActionsBar::AddObserver(ToolbarActionsBarObserver* observer) { |
| 575 observers_.AddObserver(observer); | 582 observers_.AddObserver(observer); |
| 576 } | 583 } |
| 577 | 584 |
| 578 void ToolbarActionsBar::RemoveObserver(ToolbarActionsBarObserver* observer) { | 585 void ToolbarActionsBar::RemoveObserver(ToolbarActionsBarObserver* observer) { |
| 579 observers_.RemoveObserver(observer); | 586 observers_.RemoveObserver(observer); |
| 580 } | 587 } |
| 581 | 588 |
| 582 void ToolbarActionsBar::ShowToolbarActionBubble( | 589 void ToolbarActionsBar::ShowToolbarActionBubble( |
| 583 std::unique_ptr<ToolbarActionsBarBubbleDelegate> bubble) { | 590 std::unique_ptr<ToolbarActionsBarBubbleDelegate> bubble) { |
| 584 DCHECK(bubble->GetAnchorActionId().empty() || | 591 if (delegate_->IsAnimating()) { |
| 585 GetActionForId(bubble->GetAnchorActionId())); | 592 // If the toolbar is animating, we can't effectively anchor the bubble, |
| 586 if (delegate_->IsAnimating()) | 593 // so wait until animation stops. |
| 587 pending_toolbar_bubble_controller_ = std::move(bubble); | 594 pending_bubble_controller_ = std::move(bubble); |
| 588 else | 595 } else if (bubble->ShouldShow()) { |
| 596 // We check ShouldShow() above since we show the bubble asynchronously, and |
| 597 // it might no longer have been valid. |
| 598 is_showing_bubble_ = true; |
| 589 delegate_->ShowToolbarActionBubble(std::move(bubble)); | 599 delegate_->ShowToolbarActionBubble(std::move(bubble)); |
| 600 } |
| 590 } | 601 } |
| 591 | 602 |
| 592 void ToolbarActionsBar::MaybeShowExtensionBubble( | 603 void ToolbarActionsBar::MaybeShowExtensionBubble( |
| 593 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller) { | 604 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller) { |
| 605 if (!controller->ShouldShow()) |
| 606 return; |
| 607 |
| 594 controller->HighlightExtensionsIfNecessary(); // Safe to call multiple times. | 608 controller->HighlightExtensionsIfNecessary(); // Safe to call multiple times. |
| 595 if (delegate_->IsAnimating()) { | 609 ShowToolbarActionBubble(scoped_ptr<ToolbarActionsBarBubbleDelegate>( |
| 596 // If the toolbar is animating, we can't effectively anchor the bubble, | 610 new ExtensionMessageBubbleBridge(std::move(controller)))); |
| 597 // so wait until animation stops. | |
| 598 pending_extension_bubble_controller_ = std::move(controller); | |
| 599 } else if (controller->ShouldShow()) { | |
| 600 // We check ShouldShow() above because the affected extensions may have been | |
| 601 // removed since the controller was initialized. | |
| 602 const std::vector<std::string>& affected_extensions = | |
| 603 controller->GetExtensionIdList(); | |
| 604 ToolbarActionViewController* anchor_action = nullptr; | |
| 605 for (const std::string& id : affected_extensions) { | |
| 606 anchor_action = GetActionForId(id); | |
| 607 if (anchor_action) | |
| 608 break; | |
| 609 } | |
| 610 delegate_->ShowExtensionMessageBubble(std::move(controller), anchor_action); | |
| 611 } | |
| 612 } | 611 } |
| 613 | 612 |
| 614 void ToolbarActionsBar::OnToolbarActionAdded( | 613 void ToolbarActionsBar::OnToolbarActionAdded( |
| 615 const ToolbarActionsModel::ToolbarItem& item, | 614 const ToolbarActionsModel::ToolbarItem& item, |
| 616 int index) { | 615 int index) { |
| 617 DCHECK(GetActionForId(item.id) == nullptr) | 616 DCHECK(GetActionForId(item.id) == nullptr) |
| 618 << "Asked to add a toolbar action view for an action that already " | 617 << "Asked to add a toolbar action view for an action that already " |
| 619 "exists"; | 618 "exists"; |
| 620 | 619 |
| 621 toolbar_actions_.insert(toolbar_actions_.begin() + index, | 620 toolbar_actions_.insert(toolbar_actions_.begin() + index, |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 for (ToolbarActionViewController* action : toolbar_actions_) { | 796 for (ToolbarActionViewController* action : toolbar_actions_) { |
| 798 if (action->GetId() == action_id) | 797 if (action->GetId() == action_id) |
| 799 return action; | 798 return action; |
| 800 } | 799 } |
| 801 return nullptr; | 800 return nullptr; |
| 802 } | 801 } |
| 803 | 802 |
| 804 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() { | 803 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() { |
| 805 return browser_->tab_strip_model()->GetActiveWebContents(); | 804 return browser_->tab_strip_model()->GetActiveWebContents(); |
| 806 } | 805 } |
| OLD | NEW |