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