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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 ui_initialized_ = true; | 338 ui_initialized_ = true; |
| 339 | 339 |
| 340 // Register for Issue updates. | 340 // Register for Issue updates. |
| 341 if (!issues_observer_) | 341 if (!issues_observer_) |
| 342 issues_observer_.reset(new UIIssuesObserver(router_, this)); | 342 issues_observer_.reset(new UIIssuesObserver(router_, this)); |
| 343 issues_observer_->RegisterObserver(); | 343 issues_observer_->RegisterObserver(); |
| 344 } | 344 } |
| 345 | 345 |
| 346 bool MediaRouterUI::CreateRoute(const MediaSink::Id& sink_id, | 346 bool MediaRouterUI::CreateRoute(const MediaSink::Id& sink_id, |
| 347 MediaCastMode cast_mode) { | 347 MediaCastMode cast_mode) { |
| 348 return CreateOrConnectRoute(sink_id, cast_mode, MediaRoute::Id()); | 348 MediaSource::Id source_id; |
| 349 GURL origin; | |
| 350 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 351 base::TimeDelta timeout; | |
| 352 bool off_the_record; | |
| 353 if (!GetCreateOrConnectRouteParameters(sink_id, cast_mode, &source_id, | |
| 354 &origin, &route_response_callbacks, | |
| 355 &timeout, &off_the_record)) { | |
| 356 return false; | |
| 357 } | |
| 358 router_->CreateRoute(source_id, sink_id, origin, initiator_, | |
| 359 route_response_callbacks, timeout, off_the_record); | |
| 360 return true; | |
| 349 } | 361 } |
| 350 | 362 |
| 351 bool MediaRouterUI::CreateOrConnectRoute(const MediaSink::Id& sink_id, | 363 bool MediaRouterUI::GetCreateOrConnectRouteParameters( |
|
amp
2016/04/06 21:29:36
Feels odd to have this return a bool when it says
btolsch
2016/04/08 09:31:25
I don't know that it's not accepted and I didn't w
| |
| 352 MediaCastMode cast_mode, | 364 const MediaSink::Id& sink_id, |
| 353 const MediaRoute::Id& route_id) { | 365 MediaCastMode cast_mode, |
| 366 MediaSource::Id* source_id, | |
| 367 GURL* origin, | |
| 368 std::vector<MediaRouteResponseCallback>* route_response_callbacks, | |
| 369 base::TimeDelta* timeout, | |
| 370 bool* off_the_record) { | |
| 354 DCHECK(query_result_manager_.get()); | 371 DCHECK(query_result_manager_.get()); |
| 355 DCHECK(initiator_); | 372 DCHECK(initiator_); |
| 356 | 373 |
| 357 // Note that there is a rarely-encountered bug, where the MediaCastMode to | 374 // Note that there is a rarely-encountered bug, where the MediaCastMode to |
| 358 // MediaSource mapping could have been updated, between when the user clicked | 375 // MediaSource mapping could have been updated, between when the user clicked |
| 359 // on the UI to start a create route request, and when this function is | 376 // on the UI to start a create route request, and when this function is |
| 360 // called. However, since the user does not have visibility into the | 377 // called. However, since the user does not have visibility into the |
| 361 // MediaSource, and that it occurs very rarely in practice, we leave it as-is | 378 // MediaSource, and that it occurs very rarely in practice, we leave it as-is |
| 362 // for now. | 379 // for now. |
| 363 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); | 380 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); |
| 364 if (source.Empty()) { | 381 if (source.Empty()) { |
| 365 LOG(ERROR) << "No corresponding MediaSource for cast mode " | 382 LOG(ERROR) << "No corresponding MediaSource for cast mode " |
| 366 << static_cast<int>(cast_mode); | 383 << static_cast<int>(cast_mode); |
| 367 return false; | 384 return false; |
| 368 } | 385 } |
| 386 *source_id = source.id(); | |
| 369 | 387 |
| 370 bool for_default_source = cast_mode == MediaCastMode::DEFAULT; | 388 bool for_default_source = cast_mode == MediaCastMode::DEFAULT; |
| 371 if (for_default_source && !presentation_request_) { | 389 if (for_default_source && !presentation_request_) { |
| 372 DLOG(ERROR) << "Requested to create a route for presentation, but " | 390 DLOG(ERROR) << "Requested to create a route for presentation, but " |
| 373 << "presentation request is missing."; | 391 << "presentation request is missing."; |
| 374 return false; | 392 return false; |
| 375 } | 393 } |
| 376 | 394 |
| 377 current_route_request_id_ = ++route_request_counter_; | 395 current_route_request_id_ = ++route_request_counter_; |
| 378 GURL origin = for_default_source | 396 *origin = for_default_source ? presentation_request_->frame_url().GetOrigin() |
| 379 ? presentation_request_->frame_url().GetOrigin() | 397 : GURL(chrome::kChromeUIMediaRouterURL); |
| 380 : GURL(chrome::kChromeUIMediaRouterURL); | 398 DCHECK(origin->is_valid()); |
| 381 DCHECK(origin.is_valid()); | |
| 382 | 399 |
| 383 DVLOG(1) << "DoCreateRoute: origin: " << origin; | 400 DVLOG(1) << "DoCreateRoute: origin: " << *origin; |
| 384 | 401 |
| 385 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. | 402 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. |
| 386 // (1) Non-presentation route request (e.g., mirroring). No additional | 403 // (1) Non-presentation route request (e.g., mirroring). No additional |
| 387 // notification necessary. | 404 // notification necessary. |
| 388 // (2) Presentation route request for a Presentation API startSession call. | 405 // (2) Presentation route request for a Presentation API startSession call. |
| 389 // The startSession (CreatePresentationConnectionRequest) will need to be | 406 // The startSession (CreatePresentationConnectionRequest) will need to be |
| 390 // answered with the | 407 // answered with the |
| 391 // route response. | 408 // route response. |
| 392 // (3) Browser-initiated presentation route request. If successful, | 409 // (3) Browser-initiated presentation route request. If successful, |
| 393 // PresentationServiceDelegateImpl will have to be notified. Note that we | 410 // PresentationServiceDelegateImpl will have to be notified. Note that we |
| 394 // treat subsequent route requests from a Presentation API-initiated dialogs | 411 // treat subsequent route requests from a Presentation API-initiated dialogs |
| 395 // as browser-initiated. | 412 // as browser-initiated. |
| 396 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 413 route_response_callbacks->push_back(base::Bind( |
| 397 route_response_callbacks.push_back(base::Bind( | |
| 398 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), | 414 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), |
| 399 current_route_request_id_, sink_id, cast_mode, | 415 current_route_request_id_, sink_id, cast_mode, |
| 400 base::UTF8ToUTF16(GetTruncatedPresentationRequestSourceName()))); | 416 base::UTF8ToUTF16(GetTruncatedPresentationRequestSourceName()))); |
| 401 if (for_default_source) { | 417 if (for_default_source) { |
| 402 if (create_session_request_) { | 418 if (create_session_request_) { |
| 403 // |create_session_request_| will be nullptr after this call, as the | 419 // |create_session_request_| will be nullptr after this call, as the |
| 404 // object will be transferred to the callback. | 420 // object will be transferred to the callback. |
| 405 route_response_callbacks.push_back( | 421 route_response_callbacks->push_back( |
| 406 base::Bind(&CreatePresentationConnectionRequest::HandleRouteResponse, | 422 base::Bind(&CreatePresentationConnectionRequest::HandleRouteResponse, |
| 407 base::Passed(&create_session_request_))); | 423 base::Passed(&create_session_request_))); |
| 408 } else if (presentation_service_delegate_) { | 424 } else if (presentation_service_delegate_) { |
| 409 route_response_callbacks.push_back( | 425 route_response_callbacks->push_back( |
| 410 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | 426 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
| 411 presentation_service_delegate_, *presentation_request_)); | 427 presentation_service_delegate_, *presentation_request_)); |
| 412 } | 428 } |
| 413 } | 429 } |
| 414 | 430 |
| 415 base::TimeDelta timeout = GetRouteRequestTimeout(cast_mode); | 431 *timeout = GetRouteRequestTimeout(cast_mode); |
| 416 bool off_the_record = Profile::FromWebUI(web_ui())->IsOffTheRecord(); | 432 *off_the_record = Profile::FromWebUI(web_ui())->IsOffTheRecord(); |
| 417 if (route_id.empty()) { | 433 |
| 418 router_->CreateRoute(source.id(), sink_id, origin, initiator_, | |
| 419 route_response_callbacks, timeout, | |
| 420 off_the_record); | |
| 421 } else { | |
| 422 router_->ConnectRouteByRouteId(source.id(), route_id, origin, initiator_, | |
| 423 route_response_callbacks, timeout, | |
| 424 off_the_record); | |
| 425 } | |
| 426 return true; | 434 return true; |
| 427 } | 435 } |
| 428 | 436 |
| 429 bool MediaRouterUI::ConnectRoute(const MediaSink::Id& sink_id, | 437 bool MediaRouterUI::ConnectRoute(const MediaSink::Id& sink_id, |
| 430 const MediaRoute::Id& route_id) { | 438 const MediaRoute::Id& route_id) { |
| 431 return CreateOrConnectRoute(sink_id, MediaCastMode::DEFAULT, route_id); | 439 MediaSource::Id source_id; |
| 440 GURL origin; | |
| 441 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 442 base::TimeDelta timeout; | |
| 443 bool off_the_record; | |
| 444 if (!GetCreateOrConnectRouteParameters( | |
| 445 sink_id, MediaCastMode::DEFAULT, &source_id, &origin, | |
| 446 &route_response_callbacks, &timeout, &off_the_record)) { | |
| 447 return false; | |
| 448 } | |
| 449 router_->ConnectRouteByRouteId(source_id, route_id, origin, initiator_, | |
| 450 route_response_callbacks, timeout, | |
| 451 off_the_record); | |
| 452 return true; | |
| 432 } | 453 } |
| 433 | 454 |
| 434 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { | 455 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { |
| 435 router_->TerminateRoute(route_id); | 456 router_->TerminateRoute(route_id); |
| 436 } | 457 } |
| 437 | 458 |
| 438 void MediaRouterUI::AddIssue(const Issue& issue) { router_->AddIssue(issue); } | 459 void MediaRouterUI::AddIssue(const Issue& issue) { router_->AddIssue(issue); } |
| 439 | 460 |
| 440 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 461 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 441 router_->ClearIssue(issue_id); | 462 router_->ClearIssue(issue_id); |
| 442 } | 463 } |
| 443 | 464 |
| 465 bool MediaRouterUI::SearchSinksAndCreateRoute( | |
| 466 const MediaSink::Id& sink_id, | |
| 467 const std::string& search_criteria, | |
| 468 const std::string& domain, | |
| 469 MediaCastMode cast_mode) { | |
| 470 MediaSource::Id source_id; | |
| 471 GURL origin; | |
| 472 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 473 base::TimeDelta timeout; | |
| 474 bool off_the_record; | |
| 475 if (!GetCreateOrConnectRouteParameters(sink_id, cast_mode, &source_id, | |
| 476 &origin, &route_response_callbacks, | |
| 477 &timeout, &off_the_record)) { | |
| 478 return false; | |
| 479 } | |
| 480 router_->SearchSinksAndCreateRoute( | |
| 481 sink_id, query_result_manager_->GetSourceForCastMode(cast_mode).id(), | |
| 482 search_criteria, domain, origin, initiator_, | |
| 483 std::move(route_response_callbacks), | |
| 484 base::Bind(&MediaRouterUI::OnSearchSinkResponseReceived, | |
| 485 weak_factory_.GetWeakPtr()), | |
| 486 timeout, off_the_record); | |
| 487 return true; | |
| 488 } | |
| 489 | |
| 444 void MediaRouterUI::OnResultsUpdated( | 490 void MediaRouterUI::OnResultsUpdated( |
| 445 const std::vector<MediaSinkWithCastModes>& sinks) { | 491 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 446 sinks_ = sinks; | 492 sinks_ = sinks; |
| 447 | 493 |
| 448 const icu::Collator* collator_ptr = collator_.get(); | 494 const icu::Collator* collator_ptr = collator_.get(); |
| 449 std::sort( | 495 std::sort( |
| 450 sinks_.begin(), sinks_.end(), | 496 sinks_.begin(), sinks_.end(), |
| 451 [collator_ptr](const MediaSinkWithCastModes& sink1, | 497 [collator_ptr](const MediaSinkWithCastModes& sink1, |
| 452 const MediaSinkWithCastModes& sink2) { | 498 const MediaSinkWithCastModes& sink2) { |
| 453 if (collator_ptr) { | 499 if (collator_ptr) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 DVLOG(1) << "MediaRouteResponse returned error: " << result.error(); | 545 DVLOG(1) << "MediaRouteResponse returned error: " << result.error(); |
| 500 } | 546 } |
| 501 | 547 |
| 502 handler_->OnCreateRouteResponseReceived(sink_id, route); | 548 handler_->OnCreateRouteResponseReceived(sink_id, route); |
| 503 current_route_request_id_ = -1; | 549 current_route_request_id_ = -1; |
| 504 | 550 |
| 505 if (result.result_code() == RouteRequestResult::TIMED_OUT) | 551 if (result.result_code() == RouteRequestResult::TIMED_OUT) |
| 506 SendIssueForRouteTimeout(cast_mode, presentation_request_source_name); | 552 SendIssueForRouteTimeout(cast_mode, presentation_request_source_name); |
| 507 } | 553 } |
| 508 | 554 |
| 555 void MediaRouterUI::OnSearchSinkResponseReceived(const std::string& sink_id) { | |
| 556 handler_->ReturnSearchResult(sink_id); | |
| 557 } | |
| 558 | |
| 509 void MediaRouterUI::SendIssueForRouteTimeout( | 559 void MediaRouterUI::SendIssueForRouteTimeout( |
| 510 MediaCastMode cast_mode, | 560 MediaCastMode cast_mode, |
| 511 const base::string16& presentation_request_source_name) { | 561 const base::string16& presentation_request_source_name) { |
| 512 std::string issue_title; | 562 std::string issue_title; |
| 513 switch (cast_mode) { | 563 switch (cast_mode) { |
| 514 case DEFAULT: | 564 case DEFAULT: |
| 515 DLOG_IF(ERROR, presentation_request_source_name.empty()) | 565 DLOG_IF(ERROR, presentation_request_source_name.empty()) |
| 516 << "Empty presentation request source name."; | 566 << "Empty presentation request source name."; |
| 517 issue_title = | 567 issue_title = |
| 518 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, | 568 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 base::Time::Now() - start_time_); | 631 base::Time::Now() - start_time_); |
| 582 start_time_ = base::Time(); | 632 start_time_ = base::Time(); |
| 583 } | 633 } |
| 584 } | 634 } |
| 585 | 635 |
| 586 void MediaRouterUI::UpdateMaxDialogHeight(int height) { | 636 void MediaRouterUI::UpdateMaxDialogHeight(int height) { |
| 587 handler_->UpdateMaxDialogHeight(height); | 637 handler_->UpdateMaxDialogHeight(height); |
| 588 } | 638 } |
| 589 | 639 |
| 590 } // namespace media_router | 640 } // namespace media_router |
| OLD | NEW |