Chromium Code Reviews| 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 <string> | |
| 8 | |
| 7 #include "base/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/media/router/issue.h" | 11 #include "chrome/browser/media/router/issue.h" |
| 10 #include "chrome/browser/media/router/media_route.h" | 12 #include "chrome/browser/media/router/media_route.h" |
| 11 #include "chrome/browser/media/router/media_router.h" | 13 #include "chrome/browser/media/router/media_router.h" |
| 12 #include "chrome/browser/media/router/media_router_factory.h" | 14 #include "chrome/browser/media/router/media_router_factory.h" |
| 13 #include "chrome/browser/media/router/media_router_metrics.h" | 15 #include "chrome/browser/media/router/media_router_metrics.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 19 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 18 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" | 20 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" |
| 19 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 21 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| 22 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" | |
| 20 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im pl.h" | 23 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im pl.h" |
| 21 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/color_palette.h" | 26 #include "ui/gfx/color_palette.h" |
| 24 #include "ui/gfx/image/image_skia.h" | 27 #include "ui/gfx/image/image_skia.h" |
| 25 #include "ui/gfx/paint_vector_icon.h" | 28 #include "ui/gfx/paint_vector_icon.h" |
| 26 | 29 |
| 27 using media_router::MediaRouterDialogControllerImpl; | 30 using media_router::MediaRouterDialogControllerImpl; |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 media_router::MediaRouter* GetMediaRouter(Browser* browser) { | 34 media_router::MediaRouter* GetMediaRouter(Browser* browser) { |
| 32 return media_router::MediaRouterFactory::GetApiForBrowserContext( | 35 return media_router::MediaRouterFactory::GetApiForBrowserContext( |
| 33 browser->profile()); | 36 browser->profile()); |
| 34 } | 37 } |
| 35 | 38 |
| 36 } // namespace | 39 } // namespace |
| 37 | 40 |
| 38 MediaRouterAction::MediaRouterAction(Browser* browser, | 41 MediaRouterAction::MediaRouterAction(Browser* browser, |
| 39 ToolbarActionsBar* toolbar_actions_bar) | 42 ToolbarActionsBar* toolbar_actions_bar) |
| 40 : media_router::IssuesObserver(GetMediaRouter(browser)), | 43 : media_router::IssuesObserver(GetMediaRouter(browser)), |
| 41 media_router::MediaRoutesObserver(GetMediaRouter(browser)), | 44 media_router::MediaRoutesObserver(GetMediaRouter(browser)), |
| 42 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE), | 45 current_icon_(gfx::VectorIconId::MEDIA_ROUTER_IDLE), |
| 43 has_local_display_route_(false), | 46 has_local_display_route_(false), |
| 47 has_dialog_(false), | |
| 44 delegate_(nullptr), | 48 delegate_(nullptr), |
| 45 browser_(browser), | 49 browser_(browser), |
| 46 toolbar_actions_bar_(toolbar_actions_bar), | 50 toolbar_actions_bar_(toolbar_actions_bar), |
| 47 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), | 51 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), |
| 48 contextual_menu_(browser), | 52 contextual_menu_(browser, this), |
| 49 tab_strip_model_observer_(this), | 53 tab_strip_model_observer_(this), |
| 54 toolbar_actions_bar_observer_(this), | |
| 50 weak_ptr_factory_(this) { | 55 weak_ptr_factory_(this) { |
| 51 DCHECK(browser_); | 56 DCHECK(browser_); |
| 52 DCHECK(toolbar_actions_bar_); | 57 DCHECK(toolbar_actions_bar_); |
| 53 tab_strip_model_observer_.Add(browser_->tab_strip_model()); | 58 tab_strip_model_observer_.Add(browser_->tab_strip_model()); |
| 54 | 59 toolbar_actions_bar_observer_.Add(toolbar_actions_bar_); |
| 55 RegisterObserver(); | 60 RegisterObserver(); |
| 56 } | 61 } |
| 57 | 62 |
| 58 MediaRouterAction::~MediaRouterAction() { | 63 MediaRouterAction::~MediaRouterAction() { |
| 59 UnregisterObserver(); | 64 UnregisterObserver(); |
| 60 } | 65 } |
| 61 | 66 |
| 62 std::string MediaRouterAction::GetId() const { | 67 std::string MediaRouterAction::GetId() const { |
| 63 return ComponentToolbarActionsFactory::kMediaRouterActionId; | 68 return ComponentToolbarActionsFactory::kMediaRouterActionId; |
| 64 } | 69 } |
| 65 | 70 |
| 66 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { | 71 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { |
| 67 delegate_ = delegate; | 72 delegate_ = delegate; |
| 68 | 73 |
| 69 // Updates the current popup state if |delegate_| is non-null and has | 74 // Updates the current dialog state if |delegate_| has WebContents ready. |
| 70 // WebContents ready. | |
| 71 // In cases such as opening a new browser window, SetDelegate() will be | 75 // 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 | 76 // called before the WebContents is set. In those cases, we update the dialog |
| 73 // state when ActiveTabChanged() is called. | 77 // state when ActiveTabChanged() is called. |
| 74 if (delegate_ && delegate_->GetCurrentWebContents()) | 78 if (delegate_ && delegate_->GetCurrentWebContents()) { |
| 75 UpdatePopupState(); | 79 RegisterWithDialogController(); |
| 80 // Update the button in case the pressed state is out of sync with dialog | |
|
imcheng
2016/09/16 21:47:49
nit: blank line before comments
| |
| 81 // visibility. If we just added the ephemeral icon to the toolbar and need | |
| 82 // to depress it, we do that in |OnToolbarActionsBarAnimationEnded|. | |
| 83 if (GetAlwaysShowActionPref()) | |
|
imcheng
2016/09/16 21:47:48
question: So if I added the icon by preference (i.
takumif
2016/09/20 14:50:46
Actually, OnToolbarActionsBarAnimationEnded, OnAct
| |
| 84 UpdateDialogState(); | |
| 85 } | |
| 76 } | 86 } |
| 77 | 87 |
| 78 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, | 88 gfx::Image MediaRouterAction::GetIcon(content::WebContents* web_contents, |
| 79 const gfx::Size& size) { | 89 const gfx::Size& size) { |
| 80 // Color is defined in the icon. | 90 // Color is defined in the icon. |
| 81 return gfx::Image( | 91 return gfx::Image( |
| 82 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor)); | 92 gfx::CreateVectorIcon(current_icon_, gfx::kPlaceholderColor)); |
| 83 } | 93 } |
| 84 | 94 |
| 85 base::string16 MediaRouterAction::GetActionName() const { | 95 base::string16 MediaRouterAction::GetActionName() const { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 106 return false; | 116 return false; |
| 107 } | 117 } |
| 108 | 118 |
| 109 bool MediaRouterAction::HasPopup( | 119 bool MediaRouterAction::HasPopup( |
| 110 content::WebContents* web_contents) const { | 120 content::WebContents* web_contents) const { |
| 111 return true; | 121 return true; |
| 112 } | 122 } |
| 113 | 123 |
| 114 void MediaRouterAction::HidePopup() { | 124 void MediaRouterAction::HidePopup() { |
| 115 GetMediaRouterDialogController()->HideMediaRouterDialog(); | 125 GetMediaRouterDialogController()->HideMediaRouterDialog(); |
| 116 OnPopupHidden(); | |
| 117 } | 126 } |
| 118 | 127 |
| 119 gfx::NativeView MediaRouterAction::GetPopupNativeView() { | 128 gfx::NativeView MediaRouterAction::GetPopupNativeView() { |
| 120 return nullptr; | 129 return nullptr; |
| 121 } | 130 } |
| 122 | 131 |
| 123 ui::MenuModel* MediaRouterAction::GetContextMenu() { | 132 ui::MenuModel* MediaRouterAction::GetContextMenu() { |
| 124 return contextual_menu_.menu_model(); | 133 return contextual_menu_.menu_model(); |
| 125 } | 134 } |
| 126 | 135 |
| 127 bool MediaRouterAction::ExecuteAction(bool by_user) { | 136 bool MediaRouterAction::ExecuteAction(bool by_user) { |
| 128 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click")); | 137 base::RecordAction(base::UserMetricsAction("MediaRouter_Icon_Click")); |
| 129 | 138 |
| 130 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) { | 139 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) { |
| 131 GetMediaRouterDialogController()->HideMediaRouterDialog(); | 140 GetMediaRouterDialogController()->HideMediaRouterDialog(); |
| 132 return false; | 141 return false; |
| 133 } | 142 } |
| 134 | 143 |
| 135 GetMediaRouterDialogController()->ShowMediaRouterDialog(); | 144 GetMediaRouterDialogController()->ShowMediaRouterDialog(); |
| 136 if (GetPlatformDelegate()) { | 145 if (GetPlatformDelegate()) { |
| 137 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin( | 146 media_router::MediaRouterMetrics::RecordMediaRouterDialogOrigin( |
| 138 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ? | 147 GetPlatformDelegate()->CloseOverflowMenuIfOpen() ? |
| 139 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU : | 148 media_router::MediaRouterDialogOpenOrigin::OVERFLOW_MENU : |
| 140 media_router::MediaRouterDialogOpenOrigin::TOOLBAR); | 149 media_router::MediaRouterDialogOpenOrigin::TOOLBAR); |
| 141 } | 150 } |
| 142 return true; | 151 return true; |
| 143 } | 152 } |
| 144 | 153 |
| 145 void MediaRouterAction::UpdateState() { | 154 void MediaRouterAction::UpdateState() { |
| 146 if (delegate_) | 155 DCHECK(delegate_); |
| 147 delegate_->UpdateState(); | 156 delegate_->UpdateState(); |
| 148 } | 157 } |
| 149 | 158 |
| 150 bool MediaRouterAction::DisabledClickOpensMenu() const { | 159 bool MediaRouterAction::DisabledClickOpensMenu() const { |
| 151 return false; | 160 return false; |
| 152 } | 161 } |
| 153 | 162 |
| 154 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { | 163 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { |
| 155 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); | 164 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); |
| 156 | |
| 157 MaybeUpdateIcon(); | 165 MaybeUpdateIcon(); |
| 158 } | 166 } |
| 159 | 167 |
| 160 void MediaRouterAction::OnRoutesUpdated( | 168 void MediaRouterAction::OnRoutesUpdated( |
| 161 const std::vector<media_router::MediaRoute>& routes, | 169 const std::vector<media_router::MediaRoute>& routes, |
| 162 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { | 170 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { |
| 163 has_local_display_route_ = | 171 has_local_display_route_ = |
| 164 std::find_if(routes.begin(), routes.end(), | 172 std::find_if(routes.begin(), routes.end(), |
| 165 [](const media_router::MediaRoute& route) { | 173 [](const media_router::MediaRoute& route) { |
| 166 return route.is_local() && route.for_display(); | 174 return route.is_local() && route.for_display(); |
| 167 }) != routes.end(); | 175 }) != routes.end(); |
| 168 MaybeUpdateIcon(); | 176 MaybeUpdateIcon(); |
| 169 } | 177 } |
| 170 | 178 |
| 171 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, | 179 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, |
| 172 content::WebContents* new_contents, | 180 content::WebContents* new_contents, |
| 173 int index, | 181 int index, |
| 174 int reason) { | 182 int reason) { |
| 175 UpdatePopupState(); | 183 RegisterWithDialogController(); |
| 184 UpdateDialogState(); | |
| 176 } | 185 } |
| 177 | 186 |
| 178 void MediaRouterAction::OnPopupHidden() { | 187 void MediaRouterAction::OnToolbarActionsBarAnimationEnded() { |
| 179 if (delegate_) | 188 // Depress the action if the dialog is shown. |
| 180 delegate_->OnPopupClosed(); | 189 DCHECK(delegate_); |
| 190 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) | |
|
imcheng
2016/09/16 21:47:48
Is it possible for this to return false, and do we
takumif
2016/09/20 14:50:46
This can be false in cases such as receiving an is
| |
| 191 OnDialogShown(); | |
| 181 } | 192 } |
| 182 | 193 |
| 183 void MediaRouterAction::OnPopupShown() { | 194 void MediaRouterAction::OnDialogHidden() { |
| 184 // We depress the action regardless of whether ExecuteAction() was user | 195 if (has_dialog_) { |
| 185 // initiated. | 196 has_dialog_ = false; |
| 186 if (delegate_) | 197 DCHECK(delegate_); |
| 187 delegate_->OnPopupShown(true); | 198 delegate_->OnPopupClosed(); |
|
imcheng
2016/09/16 21:47:49
Will |has_dialog_| always be in sync with |delegat
takumif
2016/09/20 14:50:46
It should be. We only call OnPopupShown/Closed() w
| |
| 199 } | |
| 188 } | 200 } |
| 189 | 201 |
| 190 void MediaRouterAction::UpdatePopupState() { | 202 void MediaRouterAction::OnDialogShown() { |
| 203 if (!has_dialog_) { | |
| 204 has_dialog_ = true; | |
| 205 DCHECK(delegate_); | |
| 206 // We depress the action regardless of whether ExecuteAction() was user | |
| 207 // initiated. | |
| 208 delegate_->OnPopupShown(true); | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 bool MediaRouterAction::GetAlwaysShowActionPref() const { | |
| 213 return ToolbarActionsModel::Get(browser_->profile()) | |
| 214 ->component_migration_helper()->GetComponentActionPref( | |
| 215 ComponentToolbarActionsFactory::kMediaRouterActionId); | |
| 216 } | |
| 217 | |
| 218 void MediaRouterAction::SetAlwaysShowActionPref(bool always_show) { | |
| 219 ToolbarActionsModel::Get(browser_->profile())->component_migration_helper() | |
| 220 ->SetComponentActionPref( | |
| 221 ComponentToolbarActionsFactory::kMediaRouterActionId, always_show); | |
| 222 } | |
| 223 | |
| 224 base::WeakPtr<MediaRouterAction> MediaRouterAction::GetWeakPtr() { | |
| 225 return weak_ptr_factory_.GetWeakPtr(); | |
| 226 } | |
| 227 | |
| 228 void MediaRouterAction::RegisterWithDialogController() { | |
| 191 MediaRouterDialogControllerImpl* controller = | 229 MediaRouterDialogControllerImpl* controller = |
| 192 GetMediaRouterDialogController(); | 230 GetMediaRouterDialogController(); |
| 193 | 231 |
| 194 if (!controller) | 232 if (!controller) |
| 195 return; | 233 return; |
| 196 | 234 |
| 197 // When each browser window is created, its toolbar creates a | 235 // |controller| keeps track of |this| if |this| was created with the browser |
| 198 // MediaRouterAction that is only destroyed when the browser window is torn | 236 // window or ephemerally by activating the Cast functionality. If |this| was |
| 199 // down. |controller| will keep track of that instance. If |this| was created | 237 // 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. | 238 // closed. If SetMediaRouterAction() was previously called, this is a no-op. |
| 201 // If SetMediaRouterAction() was previously called, this is a no-op. | |
| 202 if (!toolbar_actions_bar_->in_overflow_mode()) | 239 if (!toolbar_actions_bar_->in_overflow_mode()) |
| 203 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); | 240 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); |
| 241 } | |
| 204 | 242 |
| 205 // Update the button in case the pressed state is out of sync with dialog | 243 void MediaRouterAction::UpdateDialogState() { |
| 206 // visibility. | 244 DCHECK(delegate_); |
|
imcheng
2016/09/16 21:47:49
nit: this DCHECK not needed as the methods that yo
takumif
2016/09/20 14:50:46
Removed.
| |
| 207 if (controller->IsShowingMediaRouterDialog()) | 245 if (GetMediaRouterDialogController()->IsShowingMediaRouterDialog()) |
| 208 OnPopupShown(); | 246 OnDialogShown(); |
| 209 else | 247 else |
| 210 OnPopupHidden(); | 248 OnDialogHidden(); |
| 211 } | 249 } |
| 212 | 250 |
| 213 MediaRouterDialogControllerImpl* | 251 MediaRouterDialogControllerImpl* |
| 214 MediaRouterAction::GetMediaRouterDialogController() { | 252 MediaRouterAction::GetMediaRouterDialogController() { |
| 215 DCHECK(delegate_); | 253 DCHECK(delegate_); |
| 216 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); | 254 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); |
| 217 DCHECK(web_contents); | 255 DCHECK(web_contents); |
| 218 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( | 256 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( |
| 219 web_contents); | 257 web_contents); |
| 220 } | 258 } |
| 221 | 259 |
| 222 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { | 260 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { |
| 223 return platform_delegate_.get(); | 261 return platform_delegate_.get(); |
| 224 } | 262 } |
| 225 | 263 |
| 226 void MediaRouterAction::MaybeUpdateIcon() { | 264 void MediaRouterAction::MaybeUpdateIcon() { |
| 227 gfx::VectorIconId new_icon = GetCurrentIcon(); | 265 gfx::VectorIconId new_icon = GetCurrentIcon(); |
| 228 | 266 |
| 229 // Update the current state if it has changed. | 267 // Update the current state if it has changed. |
| 230 if (new_icon != current_icon_) { | 268 if (new_icon != current_icon_) { |
| 231 current_icon_ = new_icon; | 269 current_icon_ = new_icon; |
| 232 | 270 |
| 233 // Tell the associated view to update its icon to reflect the change made | 271 // Tell the associated view to update its icon to reflect the change made |
| 234 // above. | 272 // above. If MaybeUpdateIcon() was called as a result of instantiating |
| 273 // |this|, then |delegate_| may not be set yet. | |
| 235 if (delegate_) | 274 if (delegate_) |
| 236 delegate_->UpdateState(); | 275 UpdateState(); |
| 237 } | 276 } |
| 238 } | 277 } |
| 239 | 278 |
| 240 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { | 279 gfx::VectorIconId MediaRouterAction::GetCurrentIcon() const { |
| 241 // Highest priority is to indicate whether there's an issue. | 280 // Highest priority is to indicate whether there's an issue. |
| 242 if (issue_) { | 281 if (issue_) { |
| 243 if (issue_->severity() == media_router::Issue::FATAL) | 282 if (issue_->severity() == media_router::Issue::FATAL) |
| 244 return gfx::VectorIconId::MEDIA_ROUTER_ERROR; | 283 return gfx::VectorIconId::MEDIA_ROUTER_ERROR; |
| 245 if (issue_->severity() == media_router::Issue::WARNING) | 284 if (issue_->severity() == media_router::Issue::WARNING) |
| 246 return gfx::VectorIconId::MEDIA_ROUTER_WARNING; | 285 return gfx::VectorIconId::MEDIA_ROUTER_WARNING; |
| 247 } | 286 } |
| 248 | 287 |
| 249 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE | 288 return has_local_display_route_ ? gfx::VectorIconId::MEDIA_ROUTER_ACTIVE |
| 250 : gfx::VectorIconId::MEDIA_ROUTER_IDLE; | 289 : gfx::VectorIconId::MEDIA_ROUTER_IDLE; |
| 251 } | 290 } |
| OLD | NEW |