| 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/media/router/presentation_service_delegate_impl.h" | 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 const content::PresentationConnectionStateChangedCallback& | 359 const content::PresentationConnectionStateChangedCallback& |
| 360 state_changed_cb); | 360 state_changed_cb); |
| 361 void ListenForSessionMessages( | 361 void ListenForSessionMessages( |
| 362 const RenderFrameHostId& render_frame_host_id, | 362 const RenderFrameHostId& render_frame_host_id, |
| 363 const content::PresentationSessionInfo& session, | 363 const content::PresentationSessionInfo& session, |
| 364 const content::PresentationSessionMessageCallback& message_cb); | 364 const content::PresentationSessionMessageCallback& message_cb); |
| 365 | 365 |
| 366 // Sets or clears the default presentation request and callback for the given | 366 // Sets or clears the default presentation request and callback for the given |
| 367 // frame. Also sets / clears the default presentation requests for the owning | 367 // frame. Also sets / clears the default presentation requests for the owning |
| 368 // tab WebContents. | 368 // tab WebContents. |
| 369 void SetDefaultPresentationUrl( | 369 void SetDefaultPresentationUrls( |
| 370 const RenderFrameHostId& render_frame_host_id, | 370 const RenderFrameHostId& render_frame_host_id, |
| 371 const GURL& default_presentation_url, | 371 const std::vector<GURL>& default_presentation_urls, |
| 372 const content::PresentationSessionStartedCallback& callback); | 372 const content::PresentationSessionStartedCallback& callback); |
| 373 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 373 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| 374 DelegateObserver* observer); | 374 DelegateObserver* observer); |
| 375 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 375 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| 376 void AddDefaultPresentationRequestObserver( | 376 void AddDefaultPresentationRequestObserver( |
| 377 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 377 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 378 observer); | 378 observer); |
| 379 void RemoveDefaultPresentationRequestObserver( | 379 void RemoveDefaultPresentationRequestObserver( |
| 380 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 380 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 381 observer); | 381 observer); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 const auto it = presentation_frames_.find(render_frame_host_id); | 538 const auto it = presentation_frames_.find(render_frame_host_id); |
| 539 if (it == presentation_frames_.end()) { | 539 if (it == presentation_frames_.end()) { |
| 540 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " | 540 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " |
| 541 << "for: (" << render_frame_host_id.first << ", " | 541 << "for: (" << render_frame_host_id.first << ", " |
| 542 << render_frame_host_id.second << ")"; | 542 << render_frame_host_id.second << ")"; |
| 543 return; | 543 return; |
| 544 } | 544 } |
| 545 it->second->ListenForSessionMessages(session, message_cb); | 545 it->second->ListenForSessionMessages(session, message_cb); |
| 546 } | 546 } |
| 547 | 547 |
| 548 void PresentationFrameManager::SetDefaultPresentationUrl( | 548 void PresentationFrameManager::SetDefaultPresentationUrls( |
| 549 const RenderFrameHostId& render_frame_host_id, | 549 const RenderFrameHostId& render_frame_host_id, |
| 550 const GURL& default_presentation_url, | 550 const std::vector<GURL>& default_presentation_urls, |
| 551 const content::PresentationSessionStartedCallback& callback) { | 551 const content::PresentationSessionStartedCallback& callback) { |
| 552 if (!IsMainFrame(render_frame_host_id)) | 552 if (!IsMainFrame(render_frame_host_id)) |
| 553 return; | 553 return; |
| 554 | 554 |
| 555 if (default_presentation_url.is_empty()) { | 555 if (default_presentation_urls.empty()) { |
| 556 ClearDefaultPresentationRequest(); | 556 ClearDefaultPresentationRequest(); |
| 557 } else { | 557 } else { |
| 558 DCHECK(!callback.is_null()); | 558 DCHECK(!callback.is_null()); |
| 559 GURL frame_url( | 559 GURL frame_url( |
| 560 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_)); | 560 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_)); |
| 561 PresentationRequest request(render_frame_host_id, | 561 PresentationRequest request(render_frame_host_id, default_presentation_urls, |
| 562 std::vector<GURL>({default_presentation_url}), | |
| 563 frame_url); | 562 frame_url); |
| 564 default_presentation_started_callback_ = callback; | 563 default_presentation_started_callback_ = callback; |
| 565 SetDefaultPresentationRequest(request); | 564 SetDefaultPresentationRequest(request); |
| 566 } | 565 } |
| 567 } | 566 } |
| 568 | 567 |
| 569 void PresentationFrameManager::AddDelegateObserver( | 568 void PresentationFrameManager::AddDelegateObserver( |
| 570 const RenderFrameHostId& render_frame_host_id, | 569 const RenderFrameHostId& render_frame_host_id, |
| 571 DelegateObserver* observer) { | 570 DelegateObserver* observer) { |
| 572 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 571 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 718 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 720 frame_manager_->Reset(render_frame_host_id); | 719 frame_manager_->Reset(render_frame_host_id); |
| 721 } | 720 } |
| 722 | 721 |
| 723 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( | 722 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( |
| 724 int render_process_id, | 723 int render_process_id, |
| 725 int render_frame_id, | 724 int render_frame_id, |
| 726 const std::vector<GURL>& default_presentation_urls, | 725 const std::vector<GURL>& default_presentation_urls, |
| 727 const content::PresentationSessionStartedCallback& callback) { | 726 const content::PresentationSessionStartedCallback& callback) { |
| 728 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 727 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 729 if (default_presentation_urls.empty()) { | 728 frame_manager_->SetDefaultPresentationUrls( |
| 730 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, GURL(), | 729 render_frame_host_id, default_presentation_urls, callback); |
| 731 callback); | |
| 732 } else { | |
| 733 // TODO(crbug.com/627655): Handle multiple URLs. | |
| 734 frame_manager_->SetDefaultPresentationUrl( | |
| 735 render_frame_host_id, default_presentation_urls[0], callback); | |
| 736 } | |
| 737 } | 730 } |
| 738 | 731 |
| 739 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 732 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| 740 int render_process_id, | 733 int render_process_id, |
| 741 int render_frame_id, | 734 int render_frame_id, |
| 742 const GURL& presentation_url, | 735 const GURL& presentation_url, |
| 743 const std::string& presentation_id, | 736 const std::string& presentation_id, |
| 744 const content::PresentationSessionStartedCallback& success_cb, | 737 const content::PresentationSessionStartedCallback& success_cb, |
| 745 const content::PresentationSessionErrorCallback& error_cb, | 738 const content::PresentationSessionErrorCallback& error_cb, |
| 746 const RouteRequestResult& result) { | 739 const RouteRequestResult& result) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 int render_frame_id, | 776 int render_frame_id, |
| 784 const std::vector<GURL>& presentation_urls, | 777 const std::vector<GURL>& presentation_urls, |
| 785 const content::PresentationSessionStartedCallback& success_cb, | 778 const content::PresentationSessionStartedCallback& success_cb, |
| 786 const content::PresentationSessionErrorCallback& error_cb) { | 779 const content::PresentationSessionErrorCallback& error_cb) { |
| 787 if (presentation_urls.empty()) { | 780 if (presentation_urls.empty()) { |
| 788 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 781 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 789 "Invalid presentation arguments.")); | 782 "Invalid presentation arguments.")); |
| 790 return; | 783 return; |
| 791 } | 784 } |
| 792 | 785 |
| 793 // TODO(crbug.com/627655): Handle multiple URLs. | 786 // TODO(crbug.com/670848): Improve handling of invalid URLs in |
| 794 const GURL& presentation_url = presentation_urls[0]; | 787 // PresentationService::start(). |
| 795 if (presentation_url.is_empty() || | 788 if (presentation_urls.empty() || |
| 796 !IsValidPresentationUrl(presentation_url)) { | 789 std::find_if_not(presentation_urls.begin(), presentation_urls.end(), |
| 790 IsValidPresentationUrl) != presentation_urls.end()) { |
| 797 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 791 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 798 "Invalid presentation arguments.")); | 792 "Invalid presentation URL.")); |
| 799 return; | 793 return; |
| 800 } | 794 } |
| 801 | 795 |
| 802 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 796 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 803 std::unique_ptr<CreatePresentationConnectionRequest> request( | 797 std::unique_ptr<CreatePresentationConnectionRequest> request( |
| 804 new CreatePresentationConnectionRequest( | 798 new CreatePresentationConnectionRequest( |
| 805 render_frame_host_id, presentation_url, | 799 render_frame_host_id, presentation_urls, |
| 806 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_), | 800 GetLastCommittedURLForFrame(render_frame_host_id, web_contents_), |
| 807 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, | 801 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |
| 808 weak_factory_.GetWeakPtr(), render_process_id, | 802 weak_factory_.GetWeakPtr(), render_process_id, |
| 809 render_frame_id, success_cb), | 803 render_frame_id, success_cb), |
| 810 error_cb)); | 804 error_cb)); |
| 811 MediaRouterDialogController* controller = | 805 MediaRouterDialogController* controller = |
| 812 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); | 806 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); |
| 813 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { | 807 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { |
| 814 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; | 808 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; |
| 815 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 809 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 816 "Unable to create dialog.")); | 810 "Unable to create dialog.")); |
| 817 return; | 811 return; |
| 818 } | 812 } |
| 819 } | 813 } |
| 820 | 814 |
| 821 void PresentationServiceDelegateImpl::JoinSession( | 815 void PresentationServiceDelegateImpl::JoinSession( |
| 822 int render_process_id, | 816 int render_process_id, |
| 823 int render_frame_id, | 817 int render_frame_id, |
| 824 const std::vector<GURL>& presentation_urls, | 818 const std::vector<GURL>& presentation_urls, |
| 825 const std::string& presentation_id, | 819 const std::string& presentation_id, |
| 826 const content::PresentationSessionStartedCallback& success_cb, | 820 const content::PresentationSessionStartedCallback& success_cb, |
| 827 const content::PresentationSessionErrorCallback& error_cb) { | 821 const content::PresentationSessionErrorCallback& error_cb) { |
| 822 DVLOG(2) << "PresentationServiceDelegateImpl::JoinSession"; |
| 828 if (presentation_urls.empty()) { | 823 if (presentation_urls.empty()) { |
| 829 error_cb.Run(content::PresentationError( | 824 error_cb.Run(content::PresentationError( |
| 830 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 825 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 831 "Invalid presentation arguments.")); | 826 "Invalid presentation arguments.")); |
| 827 return; |
| 832 } | 828 } |
| 833 | 829 |
| 834 // TODO(crbug.com/627655): Handle multiple URLs. | 830 // TODO(crbug.com/627655): Handle multiple URLs. |
| 835 const GURL& presentation_url = presentation_urls[0]; | 831 const GURL& presentation_url = presentation_urls[0]; |
| 836 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 832 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| 837 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 833 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 838 route_response_callbacks.push_back( | 834 route_response_callbacks.push_back( |
| 839 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, | 835 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| 840 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 836 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| 841 presentation_url, presentation_id, success_cb, error_cb)); | 837 presentation_url, presentation_id, success_cb, error_cb)); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 const content::PresentationConnectionStateChangedCallback& | 917 const content::PresentationConnectionStateChangedCallback& |
| 922 state_changed_cb) { | 918 state_changed_cb) { |
| 923 frame_manager_->ListenForConnectionStateChange( | 919 frame_manager_->ListenForConnectionStateChange( |
| 924 RenderFrameHostId(render_process_id, render_frame_id), connection, | 920 RenderFrameHostId(render_process_id, render_frame_id), connection, |
| 925 state_changed_cb); | 921 state_changed_cb); |
| 926 } | 922 } |
| 927 | 923 |
| 928 void PresentationServiceDelegateImpl::OnRouteResponse( | 924 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 929 const PresentationRequest& presentation_request, | 925 const PresentationRequest& presentation_request, |
| 930 const RouteRequestResult& result) { | 926 const RouteRequestResult& result) { |
| 931 if (!result.route()) | 927 if (!result.route() || |
| 928 !base::ContainsValue(presentation_request.presentation_urls(), |
| 929 result.presentation_url())) { |
| 932 return; | 930 return; |
| 931 } |
| 933 | 932 |
| 934 content::PresentationSessionInfo session_info( | 933 content::PresentationSessionInfo session_info(result.presentation_url(), |
| 935 presentation_request.presentation_url(), result.presentation_id()); | 934 result.presentation_id()); |
| 936 frame_manager_->OnDefaultPresentationSessionStarted( | 935 frame_manager_->OnDefaultPresentationSessionStarted( |
| 937 presentation_request, session_info, *result.route()); | 936 presentation_request, session_info, *result.route()); |
| 938 } | 937 } |
| 939 | 938 |
| 940 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( | 939 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |
| 941 DefaultPresentationRequestObserver* observer) { | 940 DefaultPresentationRequestObserver* observer) { |
| 942 frame_manager_->AddDefaultPresentationRequestObserver(observer); | 941 frame_manager_->AddDefaultPresentationRequestObserver(observer); |
| 943 } | 942 } |
| 944 | 943 |
| 945 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( | 944 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 971 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 970 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 972 int render_process_id, | 971 int render_process_id, |
| 973 int render_frame_id, | 972 int render_frame_id, |
| 974 const MediaSource::Id& source_id) const { | 973 const MediaSource::Id& source_id) const { |
| 975 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 974 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 976 return frame_manager_->HasScreenAvailabilityListenerForTest( | 975 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 977 render_frame_host_id, source_id); | 976 render_frame_host_id, source_id); |
| 978 } | 977 } |
| 979 | 978 |
| 980 } // namespace media_router | 979 } // namespace media_router |
| OLD | NEW |