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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/media/router/issue.h" | 8 #include "chrome/browser/media/router/issue.h" |
| 9 #include "chrome/browser/media/router/media_route.h" | 9 #include "chrome/browser/media/router/media_route.h" |
| 10 #include "chrome/browser/media/router/media_router.h" | 10 #include "chrome/browser/media/router/media_router.h" |
| 11 #include "chrome/browser/media/router/media_router_factory.h" | 11 #include "chrome/browser/media/router/media_router_factory.h" |
| 12 #include "chrome/browser/media/router/media_router_mojo_impl.h" | 12 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" | 16 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" |
| 17 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" | 17 #include "chrome/browser/ui/toolbar/media_router_action_platform_delegate.h" |
| 18 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 18 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| 19 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im pl.h" | 19 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im pl.h" |
| 20 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 21 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
| 25 | 25 |
| 26 using media_router::MediaRouterDialogControllerImpl; | 26 using media_router::MediaRouterDialogControllerImpl; |
| 27 | 27 |
| 28 MediaRouterAction::ActionMediaRoutesObserver::ActionMediaRoutesObserver( | |
| 29 media_router::MediaRouter* router, | |
| 30 MediaRouterAction* action) | |
| 31 : MediaRoutesObserver(router), | |
| 32 action_(action) { | |
| 33 DCHECK(action); | |
| 34 } | |
| 35 | |
| 36 MediaRouterAction::ActionMediaRoutesObserver::~ActionMediaRoutesObserver() { | |
| 37 } | |
| 38 | |
| 39 void MediaRouterAction::ActionMediaRoutesObserver::OnRoutesUpdated( | |
| 40 const std::vector<media_router::MediaRoute>& routes) { | |
| 41 bool has_local_route = | |
| 42 std::find_if(routes.begin(), routes.end(), | |
| 43 [](const media_router::MediaRoute& route) { | |
| 44 return route.is_local(); }) != | |
| 45 routes.end(); | |
| 46 | |
| 47 action_->SetHasLocalRoute(has_local_route); | |
|
imcheng
2015/10/01 18:46:50
there should be a comment that |this| will be dele
apacible
2015/10/01 23:21:47
Done (after moving this to MediaRouterMojoImpl).
| |
| 48 } | |
| 49 | |
| 28 MediaRouterAction::MediaRouterAction(Browser* browser) | 50 MediaRouterAction::MediaRouterAction(Browser* browser) |
| 29 : media_router::IssuesObserver(GetMediaRouter(browser)), | 51 : media_router::IssuesObserver(GetMediaRouter(browser)), |
| 30 media_router::MediaRoutesObserver(GetMediaRouter(browser)), | 52 media_router::LocalMediaRoutesObserver(GetMediaRouter(browser)), |
| 31 media_router_active_icon_( | 53 media_router_active_icon_( |
| 32 ui::ResourceBundle::GetSharedInstance() | 54 ui::ResourceBundle::GetSharedInstance() |
| 33 .GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON)), | 55 .GetImageNamed(IDR_MEDIA_ROUTER_ACTIVE_ICON)), |
| 34 media_router_error_icon_(ui::ResourceBundle::GetSharedInstance() | 56 media_router_error_icon_(ui::ResourceBundle::GetSharedInstance() |
| 35 .GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON)), | 57 .GetImageNamed(IDR_MEDIA_ROUTER_ERROR_ICON)), |
| 36 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance() | 58 media_router_idle_icon_(ui::ResourceBundle::GetSharedInstance() |
| 37 .GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON)), | 59 .GetImageNamed(IDR_MEDIA_ROUTER_IDLE_ICON)), |
| 38 media_router_warning_icon_( | 60 media_router_warning_icon_( |
| 39 ui::ResourceBundle::GetSharedInstance() | 61 ui::ResourceBundle::GetSharedInstance() |
| 40 .GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON)), | 62 .GetImageNamed(IDR_MEDIA_ROUTER_WARNING_ICON)), |
| 41 current_icon_(&media_router_idle_icon_), | 63 current_icon_(&media_router_idle_icon_), |
| 42 has_local_route_(false), | 64 has_local_route_(false), |
| 43 delegate_(nullptr), | 65 delegate_(nullptr), |
| 44 browser_(browser), | 66 browser_(browser), |
| 67 routes_observer_(new ActionMediaRoutesObserver( | |
|
imcheng
2015/10/01 18:46:50
So we are still sending a routes query on construc
apacible
2015/10/01 23:21:47
Talked offline. Done.
| |
| 68 GetMediaRouter(browser), this)), | |
| 45 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), | 69 platform_delegate_(MediaRouterActionPlatformDelegate::Create(browser)), |
| 46 contextual_menu_(browser), | 70 contextual_menu_(browser), |
| 47 tab_strip_model_observer_(this), | 71 tab_strip_model_observer_(this), |
| 48 weak_ptr_factory_(this) { | 72 weak_ptr_factory_(this) { |
| 49 DCHECK(browser_); | 73 DCHECK(browser_); |
| 50 tab_strip_model_observer_.Add(browser_->tab_strip_model()); | 74 tab_strip_model_observer_.Add(browser_->tab_strip_model()); |
| 51 } | 75 } |
| 52 | 76 |
| 53 MediaRouterAction::~MediaRouterAction() { | 77 MediaRouterAction::~MediaRouterAction() { |
| 78 routes_observer_.reset(); | |
|
imcheng
2015/10/01 18:46:50
not needed?
apacible
2015/10/01 23:21:47
Done.
| |
| 54 } | 79 } |
| 55 | 80 |
| 56 std::string MediaRouterAction::GetId() const { | 81 std::string MediaRouterAction::GetId() const { |
| 57 return ComponentToolbarActionsFactory::kMediaRouterActionId; | 82 return ComponentToolbarActionsFactory::kMediaRouterActionId; |
| 58 } | 83 } |
| 59 | 84 |
| 60 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { | 85 void MediaRouterAction::SetDelegate(ToolbarActionViewDelegate* delegate) { |
| 61 delegate_ = delegate; | 86 delegate_ = delegate; |
| 62 | 87 |
| 63 // Updates the current popup state if |delegate_| is non-null and has | 88 // Updates the current popup state if |delegate_| is non-null and has |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 bool MediaRouterAction::DisabledClickOpensMenu() const { | 156 bool MediaRouterAction::DisabledClickOpensMenu() const { |
| 132 return false; | 157 return false; |
| 133 } | 158 } |
| 134 | 159 |
| 135 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { | 160 void MediaRouterAction::OnIssueUpdated(const media_router::Issue* issue) { |
| 136 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); | 161 issue_.reset(issue ? new media_router::Issue(*issue) : nullptr); |
| 137 | 162 |
| 138 MaybeUpdateIcon(); | 163 MaybeUpdateIcon(); |
| 139 } | 164 } |
| 140 | 165 |
| 141 void MediaRouterAction::OnRoutesUpdated( | 166 void MediaRouterAction::OnLocalRouteCreated() { |
| 142 const std::vector<media_router::MediaRoute>& routes) { | 167 if (!routes_observer_) |
| 143 has_local_route_ = | 168 routes_observer_.reset(new ActionMediaRoutesObserver( |
| 144 std::find_if(routes.begin(), routes.end(), | 169 GetMediaRouter(browser_), this)); |
| 145 [](const media_router::MediaRoute& route) { | |
| 146 return route.is_local(); }) != | |
| 147 routes.end(); | |
| 148 MaybeUpdateIcon(); | |
| 149 } | 170 } |
| 150 | 171 |
| 151 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, | 172 void MediaRouterAction::ActiveTabChanged(content::WebContents* old_contents, |
| 152 content::WebContents* new_contents, | 173 content::WebContents* new_contents, |
| 153 int index, | 174 int index, |
| 154 int reason) { | 175 int reason) { |
| 155 UpdatePopupState(); | 176 UpdatePopupState(); |
| 156 } | 177 } |
| 157 | 178 |
| 158 void MediaRouterAction::OnPopupHidden() { | 179 void MediaRouterAction::OnPopupHidden() { |
| 159 if (delegate_) | 180 if (delegate_) |
| 160 delegate_->OnPopupClosed(); | 181 delegate_->OnPopupClosed(); |
| 161 } | 182 } |
| 162 | 183 |
| 163 void MediaRouterAction::OnPopupShown() { | 184 void MediaRouterAction::OnPopupShown() { |
| 164 // We depress the action regardless of whether ExecuteAction() was user | 185 // We depress the action regardless of whether ExecuteAction() was user |
| 165 // initiated. | 186 // initiated. |
| 166 if (delegate_) | 187 if (delegate_) |
| 167 delegate_->OnPopupShown(true); | 188 delegate_->OnPopupShown(true); |
| 168 } | 189 } |
| 169 | 190 |
| 191 void MediaRouterAction::SetHasLocalRoute(bool has_local_route) { | |
| 192 has_local_route_ = has_local_route; | |
| 193 | |
| 194 if (!has_local_route_) | |
| 195 routes_observer_.reset(); | |
| 196 | |
| 197 MaybeUpdateIcon(); | |
| 198 } | |
| 199 | |
| 170 void MediaRouterAction::UpdatePopupState() { | 200 void MediaRouterAction::UpdatePopupState() { |
| 171 MediaRouterDialogControllerImpl* controller = | 201 MediaRouterDialogControllerImpl* controller = |
| 172 GetMediaRouterDialogController(); | 202 GetMediaRouterDialogController(); |
| 173 | 203 |
| 174 if (!controller) | 204 if (!controller) |
| 175 return; | 205 return; |
| 176 | 206 |
| 177 // Immediately keep track of MediaRouterAction in the controller. If it was | 207 // Immediately keep track of MediaRouterAction in the controller. If it was |
| 178 // already set, this should be a no-op. | 208 // already set, this should be a no-op. |
| 179 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); | 209 controller->SetMediaRouterAction(weak_ptr_factory_.GetWeakPtr()); |
| 180 | 210 |
| 181 // Update the button in case the pressed state is out of sync with dialog | 211 // Update the button in case the pressed state is out of sync with dialog |
| 182 // visibility. | 212 // visibility. |
| 183 if (controller->IsShowingMediaRouterDialog()) | 213 if (controller->IsShowingMediaRouterDialog()) |
| 184 OnPopupShown(); | 214 OnPopupShown(); |
| 185 else | 215 else |
| 186 OnPopupHidden(); | 216 OnPopupHidden(); |
| 187 } | 217 } |
| 188 | 218 |
| 189 MediaRouterDialogControllerImpl* | 219 MediaRouterDialogControllerImpl* |
| 190 MediaRouterAction::GetMediaRouterDialogController() { | 220 MediaRouterAction::GetMediaRouterDialogController() { |
| 191 DCHECK(delegate_); | 221 DCHECK(delegate_); |
| 192 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); | 222 content::WebContents* web_contents = delegate_->GetCurrentWebContents(); |
| 193 DCHECK(web_contents); | 223 DCHECK(web_contents); |
| 194 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( | 224 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( |
| 195 web_contents); | 225 web_contents); |
| 196 } | 226 } |
| 197 | 227 |
| 198 media_router::MediaRouter* MediaRouterAction::GetMediaRouter(Browser* browser) { | 228 media_router::MediaRouter* MediaRouterAction::GetMediaRouter(Browser* browser) { |
|
imcheng
2015/10/01 18:46:50
Can this be a non-member function in anonymous nam
apacible
2015/10/01 23:21:47
Done.
| |
| 199 return media_router::MediaRouterFactory::GetApiForBrowserContext( | 229 return media_router::MediaRouterFactory::GetApiForBrowserContext( |
| 200 static_cast<content::BrowserContext*>(browser->profile())); | 230 static_cast<content::BrowserContext*>(browser->profile())); |
|
imcheng
2015/10/01 18:46:50
Is the static_cast necessary?
apacible
2015/10/01 23:21:47
Removed.
| |
| 201 } | 231 } |
| 202 | 232 |
| 203 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { | 233 MediaRouterActionPlatformDelegate* MediaRouterAction::GetPlatformDelegate() { |
| 204 return platform_delegate_.get(); | 234 return platform_delegate_.get(); |
| 205 } | 235 } |
| 206 | 236 |
| 207 void MediaRouterAction::MaybeUpdateIcon() { | 237 void MediaRouterAction::MaybeUpdateIcon() { |
| 208 const gfx::Image* new_icon = GetCurrentIcon(); | 238 const gfx::Image* new_icon = GetCurrentIcon(); |
| 209 | 239 |
| 210 // Update the current state if it has changed. | 240 // Update the current state if it has changed. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 223 if (issue_) { | 253 if (issue_) { |
| 224 if (issue_->severity() == media_router::Issue::FATAL) | 254 if (issue_->severity() == media_router::Issue::FATAL) |
| 225 return &media_router_error_icon_; | 255 return &media_router_error_icon_; |
| 226 if (issue_->severity() == media_router::Issue::WARNING) | 256 if (issue_->severity() == media_router::Issue::WARNING) |
| 227 return &media_router_warning_icon_; | 257 return &media_router_warning_icon_; |
| 228 } | 258 } |
| 229 | 259 |
| 230 return has_local_route_ ? | 260 return has_local_route_ ? |
| 231 &media_router_active_icon_ : &media_router_idle_icon_; | 261 &media_router_active_icon_ : &media_router_idle_icon_; |
| 232 } | 262 } |
| OLD | NEW |