| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } | 83 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 // Reference back to the owning MediaRouterUI instance. | 86 // Reference back to the owning MediaRouterUI instance. |
| 87 MediaRouterUI* ui_; | 87 MediaRouterUI* ui_; |
| 88 | 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); | 89 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( | 92 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( |
| 93 MediaRouter* router, | 93 MediaRouter* router, const MediaSource::Id source_id, |
| 94 const RoutesUpdatedCallback& callback) | 94 const RoutesUpdatedCallback& callback) |
| 95 : MediaRoutesObserver(router), callback_(callback) { | 95 : MediaRoutesObserver(router, source_id), callback_(callback) { |
| 96 DCHECK(!callback_.is_null()); | 96 DCHECK(!callback_.is_null()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} | 99 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} |
| 100 | 100 |
| 101 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( | 101 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( |
| 102 const std::vector<MediaRoute>& routes) { | 102 const MediaSource::Id source_id, |
| 103 const std::vector<MediaRoute>& routes, |
| 104 const std::vector<MediaRoute>& joinable_routes) { |
| 105 if (source_id.compare(this->source_id()) != 0) |
| 106 return; |
| 107 |
| 103 std::vector<MediaRoute> routes_for_display; | 108 std::vector<MediaRoute> routes_for_display; |
| 109 std::vector<MediaRoute> joinable_routes_for_display; |
| 104 for (const MediaRoute& route : routes) { | 110 for (const MediaRoute& route : routes) { |
| 105 if (route.for_display()) { | 111 if (route.for_display()) { |
| 106 #ifndef NDEBUG | 112 #ifndef NDEBUG |
| 107 for (const MediaRoute& existing_route : routes_for_display) { | 113 for (const MediaRoute& existing_route : routes_for_display) { |
| 108 if (existing_route.media_sink_id() == route.media_sink_id()) { | 114 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 109 DVLOG(2) << "Received another route for display with the same sink" | 115 DVLOG(2) << "Received another route for display with the same sink" |
| 110 << " id as an existing route. " | 116 << " id as an existing route. " |
| 111 << route.media_route_id() << " has the same sink id as " | 117 << route.media_route_id() << " has the same sink id as " |
| 112 << existing_route.media_sink_id() << "."; | 118 << existing_route.media_sink_id() << "."; |
| 113 } | 119 } |
| 114 } | 120 } |
| 115 #endif | 121 #endif |
| 116 routes_for_display.push_back(route); | 122 routes_for_display.push_back(route); |
| 117 } | 123 } |
| 118 } | 124 } |
| 125 for (const MediaRoute& route: joinable_routes) { |
| 126 if (route.for_display()) { |
| 127 #ifndef NDEBUG |
| 128 for (const MediaRoute& existing_route : joinable_routes_for_display) { |
| 129 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 130 DVLOG(2) << "Received another route for display with the same sink" |
| 131 << " id as an existing route. " |
| 132 << route.media_route_id() << " has the same sink id as " |
| 133 << existing_route.media_sink_id() << "."; |
| 134 } |
| 135 } |
| 136 #endif |
| 137 joinable_routes_for_display.push_back(route); |
| 138 } |
| 139 } |
| 119 | 140 |
| 120 callback_.Run(routes_for_display); | 141 callback_.Run(routes_for_display, joinable_routes_for_display); |
| 121 } | 142 } |
| 122 | 143 |
| 123 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 144 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
| 124 : ConstrainedWebDialogUI(web_ui), | 145 : ConstrainedWebDialogUI(web_ui), |
| 125 handler_(new MediaRouterWebUIMessageHandler(this)), | 146 handler_(new MediaRouterWebUIMessageHandler(this)), |
| 126 ui_initialized_(false), | 147 ui_initialized_(false), |
| 127 requesting_route_for_default_source_(false), | 148 requesting_route_for_default_source_(false), |
| 128 current_route_request_id_(-1), | 149 current_route_request_id_(-1), |
| 129 route_request_counter_(0), | 150 route_request_counter_(0), |
| 130 initiator_(nullptr), | 151 initiator_(nullptr), |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 223 } |
| 203 | 224 |
| 204 void MediaRouterUI::InitCommon(content::WebContents* initiator, | 225 void MediaRouterUI::InitCommon(content::WebContents* initiator, |
| 205 const MediaSource& default_source, | 226 const MediaSource& default_source, |
| 206 const GURL& default_frame_url) { | 227 const GURL& default_frame_url) { |
| 207 DCHECK(initiator); | 228 DCHECK(initiator); |
| 208 DCHECK(router_); | 229 DCHECK(router_); |
| 209 | 230 |
| 210 // Register for MediaRoute updates. | 231 // Register for MediaRoute updates. |
| 211 routes_observer_.reset(new UIMediaRoutesObserver( | 232 routes_observer_.reset(new UIMediaRoutesObserver( |
| 212 router_, | 233 router_, default_source.id(), |
| 213 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); | 234 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); |
| 214 | 235 |
| 215 query_result_manager_.reset(new QueryResultManager(router_)); | 236 query_result_manager_.reset(new QueryResultManager(router_)); |
| 216 query_result_manager_->AddObserver(this); | 237 query_result_manager_->AddObserver(this); |
| 217 | 238 |
| 218 // These modes are always available. | 239 // These modes are always available. |
| 219 query_result_manager_->StartSinksQuery( | 240 query_result_manager_->StartSinksQuery( |
| 220 MediaCastMode::DESKTOP_MIRROR, MediaSourceForDesktop()); | 241 MediaCastMode::DESKTOP_MIRROR, MediaSourceForDesktop()); |
| 221 initiator_ = initiator; | 242 initiator_ = initiator; |
| 222 MediaSource mirroring_source( | 243 MediaSource mirroring_source( |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 route_creation_timer_.Start( | 348 route_creation_timer_.Start( |
| 328 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), | 349 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), |
| 329 this, &MediaRouterUI::RouteCreationTimeout); | 350 this, &MediaRouterUI::RouteCreationTimeout); |
| 330 | 351 |
| 331 router_->CreateRoute(source.id(), sink_id, origin, | 352 router_->CreateRoute(source.id(), sink_id, origin, |
| 332 initiator_, | 353 initiator_, |
| 333 route_response_callbacks); | 354 route_response_callbacks); |
| 334 return true; | 355 return true; |
| 335 } | 356 } |
| 336 | 357 |
| 358 bool MediaRouterUI::JoinRoute(const MediaSink::Id& sink_id, |
| 359 const MediaRoute::Id& route_id) { |
| 360 DCHECK(query_result_manager_.get()); |
| 361 DCHECK(initiator_); |
| 362 |
| 363 current_route_request_id_ = ++route_request_counter_; |
| 364 GURL origin = frame_url_.GetOrigin(); |
| 365 |
| 366 MediaSource source = |
| 367 query_result_manager_->GetSourceForCastMode(MediaCastMode::DEFAULT); |
| 368 |
| 369 if (source.Empty()) { |
| 370 LOG(ERROR) << "No MediaSource to join"; |
| 371 return false; |
| 372 } |
| 373 |
| 374 DCHECK(origin.is_valid()); |
| 375 |
| 376 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 377 route_response_callbacks.push_back(base::Bind( |
| 378 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), |
| 379 current_route_request_id_, sink_id)); |
| 380 |
| 381 if (presentation_service_delegate_) { |
| 382 route_response_callbacks.push_back( |
| 383 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
| 384 presentation_service_delegate_)); |
| 385 } |
| 386 |
| 387 // Start the timer. |
| 388 route_creation_timer_.Start( |
| 389 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, |
| 390 &MediaRouterUI::RouteCreationTimeout); |
| 391 router_->JoinRouteByRouteId(source.id(), route_id, origin, |
| 392 initiator_, route_response_callbacks); |
| 393 return true; |
| 394 } |
| 395 |
| 337 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { | 396 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { |
| 338 router_->CloseRoute(route_id); | 397 router_->CloseRoute(route_id); |
| 339 } | 398 } |
| 340 | 399 |
| 341 void MediaRouterUI::AddIssue(const Issue& issue) { | 400 void MediaRouterUI::AddIssue(const Issue& issue) { |
| 342 router_->AddIssue(issue); | 401 router_->AddIssue(issue); |
| 343 } | 402 } |
| 344 | 403 |
| 345 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 404 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 346 router_->ClearIssue(issue_id); | 405 router_->ClearIssue(issue_id); |
| 347 } | 406 } |
| 348 | 407 |
| 349 void MediaRouterUI::OnResultsUpdated( | 408 void MediaRouterUI::OnResultsUpdated( |
| 350 const std::vector<MediaSinkWithCastModes>& sinks) { | 409 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 351 sinks_ = sinks; | 410 sinks_ = sinks; |
| 352 if (ui_initialized_) | 411 if (ui_initialized_) |
| 353 handler_->UpdateSinks(sinks_); | 412 handler_->UpdateSinks(sinks_); |
| 354 } | 413 } |
| 355 | 414 |
| 356 void MediaRouterUI::SetIssue(const Issue* issue) { | 415 void MediaRouterUI::SetIssue(const Issue* issue) { |
| 357 if (ui_initialized_) | 416 if (ui_initialized_) |
| 358 handler_->UpdateIssue(issue); | 417 handler_->UpdateIssue(issue); |
| 359 } | 418 } |
| 360 | 419 |
| 361 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { | 420 void MediaRouterUI::OnRoutesUpdated( |
| 421 const std::vector<MediaRoute>& routes, |
| 422 const std::vector<MediaRoute>& joinable_routes) { |
| 362 routes_ = routes; | 423 routes_ = routes; |
| 424 joinable_routes_ = joinable_routes; |
| 363 if (ui_initialized_) | 425 if (ui_initialized_) |
| 364 handler_->UpdateRoutes(routes_); | 426 handler_->UpdateRoutes(routes_, joinable_routes_); |
| 365 } | 427 } |
| 366 | 428 |
| 367 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, | 429 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, |
| 368 const MediaSink::Id& sink_id, | 430 const MediaSink::Id& sink_id, |
| 369 const MediaRoute* route, | 431 const MediaRoute* route, |
| 370 const std::string& presentation_id, | 432 const std::string& presentation_id, |
| 371 const std::string& error) { | 433 const std::string& error) { |
| 372 DVLOG(1) << "OnRouteResponseReceived"; | 434 DVLOG(1) << "OnRouteResponseReceived"; |
| 373 // If we receive a new route that we aren't expecting, do nothing. | 435 // If we receive a new route that we aren't expecting, do nothing. |
| 374 if (route_request_id != current_route_request_id_) | 436 if (route_request_id != current_route_request_id_) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 | 471 |
| 410 std::string MediaRouterUI::GetFrameURLHost() const { | 472 std::string MediaRouterUI::GetFrameURLHost() const { |
| 411 return GetHostFromURL(frame_url_); | 473 return GetHostFromURL(frame_url_); |
| 412 } | 474 } |
| 413 | 475 |
| 414 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 476 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
| 415 return router_->media_route_provider_extension_id(); | 477 return router_->media_route_provider_extension_id(); |
| 416 } | 478 } |
| 417 | 479 |
| 418 } // namespace media_router | 480 } // namespace media_router |
| OLD | NEW |