| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/media_router_action.h" | 5 #include "chrome/browser/ui/toolbar/media_router_action.h" |
| 6 | 6 |
| 7 #include "base/metrics/user_metrics.h" | 7 #include "base/metrics/user_metrics.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/media/router/issue.h" | 9 #include "chrome/browser/media/router/issue.h" |
| 10 #include "chrome/browser/media/router/media_route.h" | 10 #include "chrome/browser/media/router/media_route.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 MediaRouterAction::MediaRouterAction(Browser* browser, | 38 MediaRouterAction::MediaRouterAction(Browser* browser, |
| 39 ToolbarActionsBar* toolbar_actions_bar) | 39 ToolbarActionsBar* toolbar_actions_bar) |
| 40 : media_router::IssuesObserver(GetMediaRouter(browser)), | 40 : media_router::IssuesObserver(GetMediaRouter(browser)), |
| 41 media_router::MediaRoutesObserver(GetMediaRouter(browser)), | 41 media_router::MediaRoutesObserver(GetMediaRouter(browser)), |
| 42 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE), | 42 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE), |
| 43 has_local_display_route_(false), | 43 has_local_display_route_(false), |
| 44 has_dialog_(false), |
| 44 delegate_(nullptr), | 45 delegate_(nullptr), |
| 45 browser_(browser), | 46 browser_(browser), |
| 46 toolbar_actions_bar_(toolbar_actions_bar), | 47 toolbar_actions_bar_(toolbar_actions_bar), |
| 47 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), | 48 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), |
| 48 contextual_menu_(browser), | 49 contextual_menu_(browser), |
| 49 tab_strip_model_observer_(this), | 50 tab_strip_model_observer_(this), |
| 51 toolbar_actions_bar_observer_(this), |
| 50 weak_ptr_factory_(this) { | 52 weak_ptr_factory_(this) { |
| 51 DCHECK(browser_); | 53 DCHECK(browser_); |
| 52 DCHECK(toolbar_actions_bar_); | 54 DCHECK(toolbar_actions_bar_); |
| 53 tab_strip_model_observer_.Add(browser_->tab_strip_model()); | 55 tab_strip_model_observer_.Add(browser_->tab_strip_model()); |
| 54 | 56 toolbar_actions_bar_observer_.Add(toolbar_actions_bar_); |
| 55 RegisterObserver(); | 57 RegisterObserver(); |
| 56 } | 58 } |
| 57 | 59 |
| 58 MediaRouterAction::~MediaRouterAction() { | 60 MediaRouterAction::~MediaRouterAction() { |
| 59 UnregisterObserver(); | 61 UnregisterObserver(); |
| 60 } | 62 } |
| 61 | 63 |
| 62 std::string MediaRouterAction::GetId() const { | 64 std::string MediaRouterAction::GetId() const { |
| 63 return ComponentToolbarActionsFactory::kMediaRouterActionId; | 65 return ComponentToolbarActionsFactory::kMediaRouterActionId; |
| 64 } | 66 } |
| 65 | 67 |
| 66 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { | 68 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { |
| 67 delegate_ = delegate; | 69 delegate_ = delegate; |
| 68 | 70 |
| 69 // Updates the current popup state if |delegate_| is non-null and has | |
| 70 // WebContents ready. | |
| 71 // In cases such as opening a new browser window, SetDelegate() will be | 71 // In cases such as opening a new browser window, SetDelegate() will be |
| 72 // called before the WebContents is set. In those cases, we update the popup | 72 // called before the WebContents is set. In those cases, we register with the |
| 73 // state when ActiveTabChanged() is called. | 73 // dialog controller when ActiveTabChanged() is called. |
| 74 if (delegate_ && delegate_->GetCurrentWebContents()) | 74 if (delegate_ && delegate_->GetCurrentWebContents()) |
| 75 UpdatePopupState(); | 75 RegisterWithDialogController(); |
| 76 } | 76 } |
| 77 | 77 |
| 78 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, | 78 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, |
| 79 const gfx::Size& size) { | 79 const gfx::Size& size) { |
| 80 // Color is defined in the icon. | 80 // Color is defined in the icon. |
| 81 return gfx::Image( | 81 return gfx::Image( |
| 82 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor)); | 82 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 base::string16 MediaRouterAction::GetActionName() const { | 85 base::string16 MediaRouterAction::GetActionName() const { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 106 return false; | 106 return false; |
| 107 } | 107 } |
| 108 | 108 |
| 109 bool MediaRouterAction::HasPopup( | 109 bool MediaRouterAction::HasPopup( |
| 110 content::WebContents* web_contents) const { | 110 content::WebContents* web_contents) const { |
| 111 return true; | 111 return true; |
| 112 } | 112 } |
| 113 | 113 |
| 114 void MediaRouterAction::HidePopup() { | 114 void MediaRouterAction::HidePopup() { |
| 115 GetMediaRouterDialogController()->HideMediaRouterDialog(); | 115 GetMediaRouterDialogController()->HideMediaRouterDialog(); |
| 116 OnPopupHidden(); | |
| 117 } | 116 } |
| 118 | 117 |
| 119 gfx::NativeView MediaRouterAction::GetPopupNativeView() { | 118 gfx::NativeView MediaRouterAction::GetPopupNativeView() { |
| 120 return nullptr; | 119 return nullptr; |
| 121 } | 120 } |
| 122 | 121 |
| 123 ui::MenuModel* MediaRouterAction::GetContextMenu() { | 122 ui::MenuModel* MediaRouterAction::GetContextMenu() { |
| 124 return contextual_menu_.menu_model(); | 123 return contextual_menu_.menu_model(); |
| 125 } | 124 } |
| 126 | 125 |
| 127 bool MediaRouterAction::ExecuteAction(bool by_user) { | 126 bool MediaRouterAction::ExecuteAction(bool by_user) { |
| 128 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click")); | 127 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click")); |
| 129 | 128 |
| 130 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) { | 129 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) { |
| 131 GetMediaRouterDialogController()->HideMediaRouterDialog(); | 130 GetMediaRouterDialogController()->HideMediaRouterDialog(); |
| 132 return false; | 131 return false; |
| 133 } | 132 } |
| 134 | 133 |
| 135 GetMediaRouterDialogController()->ShowMediaRouterDialog(); | 134 GetMediaRouterDialogController()->ShowMediaRouterDialog(); |
| 136 if (GetPlatformDelegate()) { | 135 if (GetPlatformDelegate()) { |
| 137 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin( | 136 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin( |
| 138 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ? | 137 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ? |
| 139 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU : | 138 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU : |
| 140 media_router::MediaRouterDialogOpenOrigin::TOOLBAR); | 139 media_router::MediaRouterDialogOpenOrigin::TOOLBAR); |
| 141 } | 140 } |
| 142 return true; | 141 return true; |
| 143 } | 142 } |
| 144 | 143 |
| 145 void MediaRouterAction::UpdateState() { | 144 void MediaRouterAction::UpdateState() { |
| 146 if (delegate_) | 145 delegate_->UpdateState(); |
| 147 delegate_->UpdateState(); | |
| 148 } | 146 } |
| 149 | 147 |
| 150 bool MediaRouterAction::DisabledClickOpensMenu() const { | 148 bool MediaRouterAction::DisabledClickOpensMenu() const { |
| 151 return false; | 149 return false; |
| 152 } | 150 } |
| 153 | 151 |
| 154 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { | 152 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { |
| 155 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); | 153 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); |
| 156 | |
| 157 MaybeUpdateIcon(); | 154 MaybeUpdateIcon(); |
| 158 } | 155 } |
| 159 | 156 |
| 160 void MediaRouterAction::OnRoutesUpdated( | 157 void MediaRouterAction::OnRoutesUpdated( |
| 161 const std::vector<media_router::MediaRoute>& routes, | 158 const std::vector<media_router::MediaRoute>& routes, |
| 162 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { | 159 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { |
| 163 has_local_display_route_ = | 160 has_local_display_route_ = |
| 164 std::find_if(routes.begin(), routes.end(), | 161 std::find_if(routes.begin(), routes.end(), |
| 165 [](const media_router::MediaRoute& route) { | 162 [](const media_router::MediaRoute& route) { |
| 166 return route.is_local() && route.for_display(); | 163 return route.is_local() && route.for_display(); |
| 167 }) != routes.end(); | 164 }) != routes.end(); |
| 168 MaybeUpdateIcon(); | 165 MaybeUpdateIcon(); |
| 169 } | 166 } |
| 170 | 167 |
| 171 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, | 168 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, |
| 172 content::WebContents* new_contents, | 169 content::WebContents* new_contents, |
| 173 int index, | 170 int index, |
| 174 int reason) { | 171 int reason) { |
| 175 UpdatePopupState(); | 172 RegisterWithDialogController(); |
| 173 UpdateDialogState(); |
| 176 } | 174 } |
| 177 | 175 |
| 178 void MediaRouterAction::OnPopupHidden() { | 176 void MediaRouterAction::OnToolbarActionsBarAnimationEnded() { |
| 179 if (delegate_) | 177 UpdateDialogState(); |
| 180 delegate_->OnPopupClosed(); | |
| 181 } | 178 } |
| 182 | 179 |
| 183 void MediaRouterAction::OnPopupShown() { | 180 void MediaRouterAction::OnDialogHidden() { |
| 184 // We depress the action regardless of whether ExecuteAction() was user | 181 if (has_dialog_) { |
| 185 // initiated. | 182 has_dialog_ = false; |
| 186 if (delegate_) | 183 delegate_->OnPopupClosed(); |
| 187 delegate_->OnPopupShown(true); | 184 } |
| 188 } | 185 } |
| 189 | 186 |
| 190 void MediaRouterAction::UpdatePopupState() { | 187 void MediaRouterAction::OnDialogShown() { |
| 188 if (!has_dialog_) { |
| 189 has_dialog_ = true; |
| 190 // We depress the action regardless of whether ExecuteAction() was user |
| 191 // initiated. |
| 192 delegate_->OnPopupShown(true); |
| 193 } |
| 194 } |
| 195 |
| 196 void MediaRouterAction::RegisterWithDialogController() { |
| 191 MediaRouterDialogControllerImpl* controller = | 197 MediaRouterDialogControllerImpl* controller = |
| 192 GetMediaRouterDialogController(); | 198 GetMediaRouterDialogController(); |
| 193 | 199 |
| 194 if (!controller) | 200 if (!controller) |
| 195 return; | 201 return; |
| 196 | 202 |
| 197 // When each browser window is created, its toolbar creates a | 203 // |controller| keeps track of |this| if |this| was created with the browser |
| 198 // MediaRouterAction that is only destroyed when the browser window is torn | 204 // window or ephemerally by activating the Cast functionality. If |this| was |
| 199 // down. |controller| will keep track of that instance. If |this| was created | 205 // created in overflow mode, it will be destroyed when the overflow menu is |
| 200 // in overflow mode, it will be destroyed when the overflow menu is closed. | 206 // closed. |
| 201 // If SetMediaRouterAction() was previously called, this is a no-op. | |
| 202 if (!toolbar_actions_bar_->in_overflow_mode()) | 207 if (!toolbar_actions_bar_->in_overflow_mode()) |
| 203 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); | 208 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); |
| 209 } |
| 204 | 210 |
| 205 // Update the button in case the pressed state is out of sync with dialog | 211 void MediaRouterAction::UpdateDialogState() { |
| 206 // visibility. | 212 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) |
| 207 if (controller->IsShowingMediaRouterDialog()) | 213 OnDialogShown(); |
| 208 OnPopupShown(); | |
| 209 else | 214 else |
| 210 OnPopupHidden(); | 215 OnDialogHidden(); |
| 211 } | 216 } |
| 212 | 217 |
| 213 MediaRouterDialogControllerImpl* | 218 MediaRouterDialogControllerImpl* |
| 214 MediaRouterAction::GetMediaRouterDialogController() { | 219 MediaRouterAction::GetMediaRouterDialogController() { |
| 215 DCHECK(delegate_); | 220 DCHECK(delegate_); |
| 216 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); | 221 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); |
| 217 DCHECK(web_contents); | 222 DCHECK(web_contents); |
| 218 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( | 223 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( |
| 219 web_contents); | 224 web_contents); |
| 220 } | 225 } |
| 221 | 226 |
| 222 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { | 227 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { |
| 223 return platform_delegate_.get(); | 228 return platform_delegate_.get(); |
| 224 } | 229 } |
| 225 | 230 |
| 226 void MediaRouterAction::MaybeUpdateIcon() { | 231 void MediaRouterAction::MaybeUpdateIcon() { |
| 227 gfx::VectorIconId new_icon = GetCurrentIcon(); | 232 gfx::VectorIconId new_icon = GetCurrentIcon(); |
| 228 | 233 |
| 229 // Update the current state if it has changed. | 234 // Update the current state if it has changed. |
| 230 if (new_icon != current_icon_) { | 235 if (new_icon != current_icon_) { |
| 231 current_icon_ = new_icon; | 236 current_icon_ = new_icon; |
| 232 | 237 |
| 233 // Tell the associated view to update its icon to reflect the change made | 238 // Tell the associated view to update its icon to reflect the change made |
| 234 // above. | 239 // above. If MaybeUpdateIcon() was called as a result of instantiating |
| 240 // |this|, then |delegate_| may not be set yet. |
| 235 if (delegate_) | 241 if (delegate_) |
| 236 delegate_->UpdateState(); | 242 UpdateState(); |
| 237 } | 243 } |
| 238 } | 244 } |
| 239 | 245 |
| 240 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { | 246 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { |
| 241 // Highest priority is to indicate whether there's an issue. | 247 // Highest priority is to indicate whether there's an issue. |
| 242 if (issue_) { | 248 if (issue_) { |
| 243 if (issue_->severity() == media_router::Issue::FATAL) | 249 if (issue_->severity() == media_router::Issue::FATAL) |
| 244 return gfx::VectorIconId::MEDIA_ROUTER_ERROR; | 250 return gfx::VectorIconId::MEDIA_ROUTER_ERROR; |
| 245 if (issue_->severity() == media_router::Issue::WARNING) | 251 if (issue_->severity() == media_router::Issue::WARNING) |
| 246 return gfx::VectorIconId::MEDIA_ROUTER_WARNING; | 252 return gfx::VectorIconId::MEDIA_ROUTER_WARNING; |
| 247 } | 253 } |
| 248 | 254 |
| 249 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE | 255 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE |
| 250 : gfx::VectorIconId::MEDIA_ROUTER_IDLE; | 256 : gfx::VectorIconId::MEDIA_ROUTER_IDLE; |
| 251 } | 257 } |
| OLD | NEW |