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/webui/media_router/media_router_ui.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } | 91 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 // Reference back to the owning MediaRouterUI instance. | 94 // Reference back to the owning MediaRouterUI instance. |
| 95 MediaRouterUI* ui_; | 95 MediaRouterUI* ui_; |
| 96 | 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); | 97 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( | 100 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( |
| 101 MediaRouter* router, const RoutesUpdatedCallback& callback) | 101 MediaRouter* router, const MediaSource::Id& source_id, |
| 102 : MediaRoutesObserver(router), callback_(callback) { | 102 const RoutesUpdatedCallback& callback) |
| 103 : MediaRoutesObserver(router, source_id), callback_(callback) { | |
| 103 DCHECK(!callback_.is_null()); | 104 DCHECK(!callback_.is_null()); |
| 104 } | 105 } |
| 105 | 106 |
| 106 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} | 107 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} |
| 107 | 108 |
| 108 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( | 109 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( |
| 109 const std::vector<MediaRoute>& routes) { | 110 const std::vector<MediaRoute>& routes, |
| 111 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 110 std::vector<MediaRoute> routes_for_display; | 112 std::vector<MediaRoute> routes_for_display; |
| 113 std::vector<MediaRoute::Id> joinable_route_ids_for_display; | |
| 111 for (const MediaRoute& route : routes) { | 114 for (const MediaRoute& route : routes) { |
| 112 if (route.for_display()) { | 115 if (route.for_display()) { |
| 113 #ifndef NDEBUG | 116 #ifndef NDEBUG |
| 114 for (const MediaRoute& existing_route : routes_for_display) { | 117 for (const MediaRoute& existing_route : routes_for_display) { |
| 115 if (existing_route.media_sink_id() == route.media_sink_id()) { | 118 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 116 DVLOG(2) << "Received another route for display with the same sink" | 119 DVLOG(2) << "Received another route for display with the same sink" |
| 117 << " id as an existing route. " << route.media_route_id() | 120 << " id as an existing route. " << route.media_route_id() |
| 118 << " has the same sink id as " | 121 << " has the same sink id as " |
| 119 << existing_route.media_sink_id() << "."; | 122 << existing_route.media_sink_id() << "."; |
| 120 } | 123 } |
| 121 } | 124 } |
| 122 #endif | 125 #endif |
| 126 if (std::find(joinable_route_ids.begin(), | |
|
mark a. foltz
2015/12/09 00:48:17
Shorter as ContainsValue from base/stl_util.h
matt.boetger
2015/12/15 19:21:20
Done.
| |
| 127 joinable_route_ids.end(), route.media_route_id()) != | |
| 128 joinable_route_ids.end()) { | |
| 129 joinable_route_ids_for_display.push_back(route.media_route_id()); | |
| 130 } | |
| 131 | |
| 123 routes_for_display.push_back(route); | 132 routes_for_display.push_back(route); |
| 124 } | 133 } |
| 125 } | 134 } |
| 126 | 135 |
| 127 callback_.Run(routes_for_display); | 136 callback_.Run(routes_for_display, joinable_route_ids_for_display); |
| 128 } | 137 } |
| 129 | 138 |
| 130 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 139 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
| 131 : ConstrainedWebDialogUI(web_ui), | 140 : ConstrainedWebDialogUI(web_ui), |
| 132 handler_(new MediaRouterWebUIMessageHandler(this)), | 141 handler_(new MediaRouterWebUIMessageHandler(this)), |
| 133 ui_initialized_(false), | 142 ui_initialized_(false), |
| 134 requesting_route_for_default_source_(false), | 143 requesting_route_for_default_source_(false), |
| 135 current_route_request_id_(-1), | 144 current_route_request_id_(-1), |
| 136 route_request_counter_(0), | 145 route_request_counter_(0), |
| 137 initiator_(nullptr), | 146 initiator_(nullptr), |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 InitCommon(initiator); | 217 InitCommon(initiator); |
| 209 OnDefaultPresentationChanged(create_session_request_->presentation_request()); | 218 OnDefaultPresentationChanged(create_session_request_->presentation_request()); |
| 210 } | 219 } |
| 211 | 220 |
| 212 void MediaRouterUI::InitCommon(content::WebContents* initiator) { | 221 void MediaRouterUI::InitCommon(content::WebContents* initiator) { |
| 213 DCHECK(initiator); | 222 DCHECK(initiator); |
| 214 DCHECK(router_); | 223 DCHECK(router_); |
| 215 | 224 |
| 216 TRACE_EVENT_NESTABLE_ASYNC_INSTANT1("media_router", "UI", initiator, | 225 TRACE_EVENT_NESTABLE_ASYNC_INSTANT1("media_router", "UI", initiator, |
| 217 "MediaRouterUI::InitCommon", this); | 226 "MediaRouterUI::InitCommon", this); |
| 218 // Register for MediaRoute updates. | |
| 219 routes_observer_.reset(new UIMediaRoutesObserver( | |
| 220 router_, | |
| 221 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); | |
| 222 | |
| 223 query_result_manager_.reset(new QueryResultManager(router_)); | 227 query_result_manager_.reset(new QueryResultManager(router_)); |
| 224 query_result_manager_->AddObserver(this); | 228 query_result_manager_->AddObserver(this); |
| 225 | 229 |
| 226 // These modes are always available. | 230 // These modes are always available. |
| 227 query_result_manager_->StartSinksQuery(MediaCastMode::DESKTOP_MIRROR, | 231 query_result_manager_->StartSinksQuery(MediaCastMode::DESKTOP_MIRROR, |
| 228 MediaSourceForDesktop()); | 232 MediaSourceForDesktop()); |
| 229 initiator_ = initiator; | 233 initiator_ = initiator; |
| 230 MediaSource mirroring_source( | 234 MediaSource mirroring_source( |
| 231 MediaSourceForTab(SessionTabHelper::IdForTab(initiator))); | 235 MediaSourceForTab(SessionTabHelper::IdForTab(initiator))); |
| 232 query_result_manager_->StartSinksQuery(MediaCastMode::TAB_MIRROR, | 236 query_result_manager_->StartSinksQuery(MediaCastMode::TAB_MIRROR, |
| 233 mirroring_source); | 237 mirroring_source); |
| 234 UpdateCastModes(); | 238 UpdateCastModes(); |
| 235 } | 239 } |
| 236 | 240 |
| 237 void MediaRouterUI::OnDefaultPresentationChanged( | 241 void MediaRouterUI::OnDefaultPresentationChanged( |
| 238 const PresentationRequest& presentation_request) { | 242 const PresentationRequest& presentation_request) { |
| 243 MediaSource source = presentation_request.GetMediaSource(); | |
| 239 presentation_request_.reset(new PresentationRequest(presentation_request)); | 244 presentation_request_.reset(new PresentationRequest(presentation_request)); |
| 240 query_result_manager_->StartSinksQuery( | 245 query_result_manager_->StartSinksQuery(MediaCastMode::DEFAULT, source); |
| 241 MediaCastMode::DEFAULT, presentation_request_->GetMediaSource()); | 246 // Register for MediaRoute updates. |
| 247 routes_observer_.reset(new UIMediaRoutesObserver( | |
|
mark a. foltz
2015/12/09 00:48:17
I assume the UIMediaRoutesObserver dtor handles de
matt.boetger
2015/12/15 19:21:20
Yes, all route observers handle de-registration of
| |
| 248 router_, source.id(), | |
| 249 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); | |
| 250 | |
| 242 UpdateCastModes(); | 251 UpdateCastModes(); |
| 243 } | 252 } |
| 244 | 253 |
| 245 void MediaRouterUI::OnDefaultPresentationRemoved() { | 254 void MediaRouterUI::OnDefaultPresentationRemoved() { |
| 246 presentation_request_.reset(); | 255 presentation_request_.reset(); |
| 247 query_result_manager_->StopSinksQuery(MediaCastMode::DEFAULT); | 256 query_result_manager_->StopSinksQuery(MediaCastMode::DEFAULT); |
| 257 routes_observer_.reset(); | |
| 248 UpdateCastModes(); | 258 UpdateCastModes(); |
| 249 } | 259 } |
| 250 | 260 |
| 251 void MediaRouterUI::UpdateCastModes() { | 261 void MediaRouterUI::UpdateCastModes() { |
| 252 // Gets updated cast modes from |query_result_manager_| and forwards it to UI. | 262 // Gets updated cast modes from |query_result_manager_| and forwards it to UI. |
| 253 query_result_manager_->GetSupportedCastModes(&cast_modes_); | 263 query_result_manager_->GetSupportedCastModes(&cast_modes_); |
| 254 if (ui_initialized_) { | 264 if (ui_initialized_) { |
| 255 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName()); | 265 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName()); |
| 256 } | 266 } |
| 257 } | 267 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 // Start the timer. | 352 // Start the timer. |
| 343 route_creation_timer_.Start( | 353 route_creation_timer_.Start( |
| 344 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, | 354 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, |
| 345 &MediaRouterUI::RouteCreationTimeout); | 355 &MediaRouterUI::RouteCreationTimeout); |
| 346 | 356 |
| 347 router_->CreateRoute(source.id(), sink_id, origin, initiator_, | 357 router_->CreateRoute(source.id(), sink_id, origin, initiator_, |
| 348 route_response_callbacks); | 358 route_response_callbacks); |
| 349 return true; | 359 return true; |
| 350 } | 360 } |
| 351 | 361 |
| 362 bool MediaRouterUI::JoinRoute(const MediaSink::Id& sink_id, | |
|
mark a. foltz
2015/12/09 00:48:17
I would like to name this ConnectRemoteRoute to ma
matt.boetger
2015/12/15 19:21:20
Done.
| |
| 363 const MediaRoute::Id& route_id) { | |
| 364 DCHECK(query_result_manager_.get()); | |
| 365 DCHECK(initiator_); | |
| 366 | |
| 367 current_route_request_id_ = ++route_request_counter_; | |
| 368 GURL origin = presentation_request_->frame_url().GetOrigin(); | |
| 369 DCHECK(origin.is_valid()); | |
| 370 | |
| 371 MediaSource source = | |
|
mark a. foltz
2015/12/09 00:48:17
Don't you need to check presentation_request_ as w
matt.boetger
2015/12/15 19:21:20
Done.
| |
| 372 query_result_manager_->GetSourceForCastMode(MediaCastMode::DEFAULT); | |
| 373 | |
| 374 if (source.Empty()) { | |
| 375 LOG(ERROR) << "No MediaSource to join"; | |
| 376 return false; | |
| 377 } | |
| 378 | |
| 379 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 380 route_response_callbacks.push_back(base::Bind( | |
| 381 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), | |
| 382 current_route_request_id_, sink_id)); | |
| 383 | |
| 384 if (presentation_service_delegate_) { | |
| 385 route_response_callbacks.push_back( | |
| 386 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | |
| 387 presentation_service_delegate_, *presentation_request_)); | |
| 388 } | |
| 389 | |
| 390 // Start the timer. | |
| 391 route_creation_timer_.Start( | |
| 392 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, | |
| 393 &MediaRouterUI::RouteCreationTimeout); | |
| 394 router_->JoinRouteByRouteId(source.id(), route_id, origin, | |
| 395 initiator_, route_response_callbacks); | |
| 396 return true; | |
| 397 } | |
| 398 | |
| 352 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { | 399 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { |
| 353 router_->CloseRoute(route_id); | 400 router_->CloseRoute(route_id); |
| 354 } | 401 } |
| 355 | 402 |
| 356 void MediaRouterUI::AddIssue(const Issue& issue) { router_->AddIssue(issue); } | 403 void MediaRouterUI::AddIssue(const Issue& issue) { router_->AddIssue(issue); } |
| 357 | 404 |
| 358 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 405 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 359 router_->ClearIssue(issue_id); | 406 router_->ClearIssue(issue_id); |
| 360 } | 407 } |
| 361 | 408 |
| 362 void MediaRouterUI::OnResultsUpdated( | 409 void MediaRouterUI::OnResultsUpdated( |
| 363 const std::vector<MediaSinkWithCastModes>& sinks) { | 410 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 364 sinks_ = sinks; | 411 sinks_ = sinks; |
| 365 if (ui_initialized_) handler_->UpdateSinks(sinks_); | 412 if (ui_initialized_) handler_->UpdateSinks(sinks_); |
| 366 } | 413 } |
| 367 | 414 |
| 368 void MediaRouterUI::SetIssue(const Issue* issue) { | 415 void MediaRouterUI::SetIssue(const Issue* issue) { |
| 369 if (ui_initialized_) handler_->UpdateIssue(issue); | 416 if (ui_initialized_) handler_->UpdateIssue(issue); |
| 370 } | 417 } |
| 371 | 418 |
| 372 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { | 419 void MediaRouterUI::OnRoutesUpdated( |
| 420 const std::vector<MediaRoute>& routes, | |
| 421 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 373 routes_ = routes; | 422 routes_ = routes; |
| 374 if (ui_initialized_) handler_->UpdateRoutes(routes_); | 423 joinable_route_ids_ = joinable_route_ids; |
| 424 if (ui_initialized_) handler_->UpdateRoutes(routes_, joinable_route_ids_); | |
| 375 } | 425 } |
| 376 | 426 |
| 377 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, | 427 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, |
| 378 const MediaSink::Id& sink_id, | 428 const MediaSink::Id& sink_id, |
| 379 const MediaRoute* route, | 429 const MediaRoute* route, |
| 380 const std::string& presentation_id, | 430 const std::string& presentation_id, |
| 381 const std::string& error) { | 431 const std::string& error) { |
| 382 DVLOG(1) << "OnRouteResponseReceived"; | 432 DVLOG(1) << "OnRouteResponseReceived"; |
| 383 // If we receive a new route that we aren't expecting, do nothing. | 433 // If we receive a new route that we aren't expecting, do nothing. |
| 384 if (route_request_id != current_route_request_id_) return; | 434 if (route_request_id != current_route_request_id_) return; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 ? GetExtensionName(gurl, extensions::ExtensionRegistry::Get( | 485 ? GetExtensionName(gurl, extensions::ExtensionRegistry::Get( |
| 436 Profile::FromWebUI(web_ui()))) | 486 Profile::FromWebUI(web_ui()))) |
| 437 : TruncateHost(GetHostFromURL(gurl)); | 487 : TruncateHost(GetHostFromURL(gurl)); |
| 438 } | 488 } |
| 439 | 489 |
| 440 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 490 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
| 441 return router_->media_route_provider_extension_id(); | 491 return router_->media_route_provider_extension_id(); |
| 442 } | 492 } |
| 443 | 493 |
| 444 } // namespace media_router | 494 } // namespace media_router |
| OLD | NEW |