| Index: chrome/browser/ui/toolbar/media_router_action.cc
|
| diff --git a/chrome/browser/ui/toolbar/media_router_action.cc b/chrome/browser/ui/toolbar/media_router_action.cc
|
| index 97c60b2a4d4204916f94fb4734162b036ff85d49..aa074d6a7e9ccd1fff426173f14a62b2b42e1e4d 100644
|
| --- a/chrome/browser/ui/toolbar/media_router_action.cc
|
| +++ b/chrome/browser/ui/toolbar/media_router_action.cc
|
| @@ -41,17 +41,19 @@ MediaRouterAction::MediaRouterAction(Browser* browser,
|
| media_router::MediaRoutesObserver(GetMediaRouter(browser)),
|
| current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE),
|
| has_local_display_route_(false),
|
| + has_dialog_(false),
|
| delegate_(nullptr),
|
| browser_(browser),
|
| toolbar_actions_bar_(toolbar_actions_bar),
|
| platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)),
|
| contextual_menu_(browser),
|
| tab_strip_model_observer_(this),
|
| + toolbar_actions_bar_observer_(this),
|
| weak_ptr_factory_(this) {
|
| DCHECK(browser_);
|
| DCHECK(toolbar_actions_bar_);
|
| tab_strip_model_observer_.Add(browser_->tab_strip_model());
|
| -
|
| + toolbar_actions_bar_observer_.Add(toolbar_actions_bar_);
|
| RegisterObserver();
|
| }
|
|
|
| @@ -66,13 +68,11 @@ std::string MediaRouterAction::GetId() const {
|
| void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) {
|
| delegate_ = delegate;
|
|
|
| - // Updates the current popup state if |delegate_| is non-null and has
|
| - // WebContents ready.
|
| // In cases such as opening a new browser window, SetDelegate() will be
|
| - // called before the WebContents is set. In those cases, we update the popup
|
| - // state when ActiveTabChanged() is called.
|
| + // called before the WebContents is set. In those cases, we register with the
|
| + // dialog controller when ActiveTabChanged() is called.
|
| if (delegate_ && delegate_->GetCurrentWebContents())
|
| - UpdatePopupState();
|
| + RegisterWithDialogController();
|
| }
|
|
|
| gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents,
|
| @@ -113,7 +113,6 @@ bool MediaRouterAction::HasPopup(
|
|
|
| void MediaRouterAction::HidePopup() {
|
| GetMediaRouterDialogController()->HideMediaRouterDialog();
|
| - OnPopupHidden();
|
| }
|
|
|
| gfx::NativeView MediaRouterAction::GetPopupNativeView() {
|
| @@ -143,8 +142,7 @@ bool MediaRouterAction::ExecuteAction(bool by_user) {
|
| }
|
|
|
| void MediaRouterAction::UpdateState() {
|
| - if (delegate_)
|
| - delegate_->UpdateState();
|
| + delegate_->UpdateState();
|
| }
|
|
|
| bool MediaRouterAction::DisabledClickOpensMenu() const {
|
| @@ -153,7 +151,6 @@ bool MediaRouterAction::DisabledClickOpensMenu() const {
|
|
|
| void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) {
|
| issue_.reset(issue ? new media_router::Issue(*issue) : nullptr);
|
| -
|
| MaybeUpdateIcon();
|
| }
|
|
|
| @@ -172,42 +169,50 @@ void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents,
|
| content::WebContents* new_contents,
|
| int index,
|
| int reason) {
|
| - UpdatePopupState();
|
| + RegisterWithDialogController();
|
| + UpdateDialogState();
|
| +}
|
| +
|
| +void MediaRouterAction::OnToolbarActionsBarAnimationEnded() {
|
| + UpdateDialogState();
|
| }
|
|
|
| -void MediaRouterAction::OnPopupHidden() {
|
| - if (delegate_)
|
| +void MediaRouterAction::OnDialogHidden() {
|
| + if (has_dialog_) {
|
| + has_dialog_ = false;
|
| delegate_->OnPopupClosed();
|
| + }
|
| }
|
|
|
| -void MediaRouterAction::OnPopupShown() {
|
| - // We depress the action regardless of whether ExecuteAction() was user
|
| - // initiated.
|
| - if (delegate_)
|
| +void MediaRouterAction::OnDialogShown() {
|
| + if (!has_dialog_) {
|
| + has_dialog_ = true;
|
| + // We depress the action regardless of whether ExecuteAction() was user
|
| + // initiated.
|
| delegate_->OnPopupShown(true);
|
| + }
|
| }
|
|
|
| -void MediaRouterAction::UpdatePopupState() {
|
| +void MediaRouterAction::RegisterWithDialogController() {
|
| MediaRouterDialogControllerImpl* controller =
|
| GetMediaRouterDialogController();
|
|
|
| if (!controller)
|
| return;
|
|
|
| - // When each browser window is created, its toolbar creates a
|
| - // MediaRouterAction that is only destroyed when the browser window is torn
|
| - // down. |controller| will keep track of that instance. If |this| was created
|
| - // in overflow mode, it will be destroyed when the overflow menu is closed.
|
| - // If SetMediaRouterAction() was previously called, this is a no-op.
|
| + // |controller| keeps track of |this| if |this| was created with the browser
|
| + // window or ephemerally by activating the Cast functionality. If |this| was
|
| + // created in overflow mode, it will be destroyed when the overflow menu is
|
| + // closed.
|
| if (!toolbar_actions_bar_->in_overflow_mode())
|
| controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr());
|
| +}
|
|
|
| - // Update the button in case the pressed state is out of sync with dialog
|
| - // visibility.
|
| - if (controller->IsShowingMediaRouterDialog())
|
| - OnPopupShown();
|
| +void MediaRouterAction::UpdateDialogState() {
|
| + if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog())
|
| + OnDialogShown();
|
| else
|
| - OnPopupHidden();
|
| + OnDialogHidden();
|
| }
|
|
|
| MediaRouterDialogControllerImpl*
|
| @@ -231,9 +236,10 @@ void MediaRouterAction::MaybeUpdateIcon() {
|
| current_icon_ = new_icon;
|
|
|
| // Tell the associated view to update its icon to reflect the change made
|
| - // above.
|
| + // above. If MaybeUpdateIcon() was called as a result of instantiating
|
| + // |this|, then |delegate_| may not be set yet.
|
| if (delegate_)
|
| - delegate_->UpdateState();
|
| + UpdateState();
|
| }
|
| }
|
|
|
|
|