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/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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 const content::PresentationConnectionStateChangedCallback& | 352 const content::PresentationConnectionStateChangedCallback& |
| 353 state_changed_cb); | 353 state_changed_cb); |
| 354 void ListenForSessionMessages( | 354 void ListenForSessionMessages( |
| 355 const RenderFrameHostId& render_frame_host_id, | 355 const RenderFrameHostId& render_frame_host_id, |
| 356 const content::PresentationSessionInfo& session, | 356 const content::PresentationSessionInfo& session, |
| 357 const content::PresentationSessionMessageCallback& message_cb); | 357 const content::PresentationSessionMessageCallback& message_cb); |
| 358 | 358 |
| 359 // Sets or clears the default presentation request and callback for the given | 359 // Sets or clears the default presentation request and callback for the given |
| 360 // frame. Also sets / clears the default presentation requests for the owning | 360 // frame. Also sets / clears the default presentation requests for the owning |
| 361 // tab WebContents. | 361 // tab WebContents. |
| 362 void SetDefaultPresentationUrl( | 362 void SetDefaultPresentationUrls( |
| 363 const RenderFrameHostId& render_frame_host_id, | 363 const RenderFrameHostId& render_frame_host_id, |
| 364 const GURL& default_presentation_url, | 364 const std::vector<GURL>& default_presentation_urls, |
| 365 const content::PresentationSessionStartedCallback& callback); | 365 const content::PresentationSessionStartedCallback& callback); |
| 366 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 366 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| 367 DelegateObserver* observer); | 367 DelegateObserver* observer); |
| 368 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 368 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| 369 void AddDefaultPresentationRequestObserver( | 369 void AddDefaultPresentationRequestObserver( |
| 370 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 370 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 371 observer); | 371 observer); |
| 372 void RemoveDefaultPresentationRequestObserver( | 372 void RemoveDefaultPresentationRequestObserver( |
| 373 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 373 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 374 observer); | 374 observer); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 const auto it = presentation_frames_.find(render_frame_host_id); | 531 const auto it = presentation_frames_.find(render_frame_host_id); |
| 532 if (it == presentation_frames_.end()) { | 532 if (it == presentation_frames_.end()) { |
| 533 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " | 533 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " |
| 534 << "for: (" << render_frame_host_id.first << ", " | 534 << "for: (" << render_frame_host_id.first << ", " |
| 535 << render_frame_host_id.second << ")"; | 535 << render_frame_host_id.second << ")"; |
| 536 return; | 536 return; |
| 537 } | 537 } |
| 538 it->second->ListenForSessionMessages(session, message_cb); | 538 it->second->ListenForSessionMessages(session, message_cb); |
| 539 } | 539 } |
| 540 | 540 |
| 541 void PresentationFrameManager::SetDefaultPresentationUrl( | 541 void PresentationFrameManager::SetDefaultPresentationUrls( |
| 542 const RenderFrameHostId& render_frame_host_id, | 542 const RenderFrameHostId& render_frame_host_id, |
| 543 const GURL& default_presentation_url, | 543 const std::vector<GURL>& default_presentation_urls, |
| 544 const content::PresentationSessionStartedCallback& callback) { | 544 const content::PresentationSessionStartedCallback& callback) { |
| 545 if (!IsMainFrame(render_frame_host_id)) | 545 if (!IsMainFrame(render_frame_host_id)) |
| 546 return; | 546 return; |
| 547 | 547 |
| 548 if (default_presentation_url.is_empty()) { | 548 if (default_presentation_urls.empty()) { |
| 549 ClearDefaultPresentationRequest(); | 549 ClearDefaultPresentationRequest(); |
| 550 } else { | 550 } else { |
| 551 DCHECK(!callback.is_null()); | 551 DCHECK(!callback.is_null()); |
| 552 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); | 552 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 553 PresentationRequest request(render_frame_host_id, | 553 PresentationRequest request(render_frame_host_id, default_presentation_urls, |
| 554 std::vector<GURL>({default_presentation_url}), | |
| 555 frame_url); | 554 frame_url); |
| 556 default_presentation_started_callback_ = callback; | 555 default_presentation_started_callback_ = callback; |
| 557 SetDefaultPresentationRequest(request); | 556 SetDefaultPresentationRequest(request); |
| 558 } | 557 } |
| 559 } | 558 } |
| 560 | 559 |
| 561 void PresentationFrameManager::AddDelegateObserver( | 560 void PresentationFrameManager::AddDelegateObserver( |
| 562 const RenderFrameHostId& render_frame_host_id, | 561 const RenderFrameHostId& render_frame_host_id, |
| 563 DelegateObserver* observer) { | 562 DelegateObserver* observer) { |
| 564 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 563 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 711 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 710 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 712 frame_manager_->Reset(render_frame_host_id); | 711 frame_manager_->Reset(render_frame_host_id); |
| 713 } | 712 } |
| 714 | 713 |
| 715 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( | 714 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( |
| 716 int render_process_id, | 715 int render_process_id, |
| 717 int render_frame_id, | 716 int render_frame_id, |
| 718 const std::vector<GURL>& default_presentation_urls, | 717 const std::vector<GURL>& default_presentation_urls, |
| 719 const content::PresentationSessionStartedCallback& callback) { | 718 const content::PresentationSessionStartedCallback& callback) { |
| 720 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 719 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 721 if (default_presentation_urls.empty()) { | 720 frame_manager_->SetDefaultPresentationUrls( |
| 722 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, GURL(), | 721 render_frame_host_id, default_presentation_urls, callback); |
| 723 callback); | |
| 724 } else { | |
| 725 // TODO(crbug.com/627655): Handle multiple URLs. | |
| 726 frame_manager_->SetDefaultPresentationUrl( | |
| 727 render_frame_host_id, default_presentation_urls[0], callback); | |
| 728 } | |
| 729 } | 722 } |
| 730 | 723 |
| 731 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 724 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| 732 int render_process_id, | 725 int render_process_id, |
| 733 int render_frame_id, | 726 int render_frame_id, |
| 734 const GURL& presentation_url, | 727 const GURL& presentation_url, |
| 735 const std::string& presentation_id, | 728 const std::string& presentation_id, |
| 736 const content::PresentationSessionStartedCallback& success_cb, | 729 const content::PresentationSessionStartedCallback& success_cb, |
| 737 const content::PresentationSessionErrorCallback& error_cb, | 730 const content::PresentationSessionErrorCallback& error_cb, |
| 738 const RouteRequestResult& result) { | 731 const RouteRequestResult& result) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 775 int render_frame_id, | 768 int render_frame_id, |
| 776 const std::vector<GURL>& presentation_urls, | 769 const std::vector<GURL>& presentation_urls, |
| 777 const content::PresentationSessionStartedCallback& success_cb, | 770 const content::PresentationSessionStartedCallback& success_cb, |
| 778 const content::PresentationSessionErrorCallback& error_cb) { | 771 const content::PresentationSessionErrorCallback& error_cb) { |
| 779 if (presentation_urls.empty()) { | 772 if (presentation_urls.empty()) { |
| 780 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 773 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 781 "Invalid presentation arguments.")); | 774 "Invalid presentation arguments.")); |
| 782 return; | 775 return; |
| 783 } | 776 } |
| 784 | 777 |
| 785 // TODO(crbug.com/627655): Handle multiple URLs. | 778 // TODO(crbug.com/670848): Improve handling of invalid URLs in |
| 786 const GURL& presentation_url = presentation_urls[0]; | 779 // PresentationService::start(). |
| 787 if (presentation_url.is_empty() || | 780 if (presentation_urls.empty() || |
| 788 !IsValidPresentationUrl(presentation_url)) { | 781 std::find_if_not(presentation_urls.begin(), presentation_urls.end(), |
| 782 IsValidPresentationUrl) != presentation_urls.end()) { | |
| 789 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 783 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 790 "Invalid presentation arguments.")); | 784 "Invalid presentation URL.")); |
| 791 return; | 785 return; |
| 792 } | 786 } |
| 793 | 787 |
| 794 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 788 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 795 std::unique_ptr<CreatePresentationConnectionRequest> request( | 789 std::unique_ptr<CreatePresentationConnectionRequest> request( |
| 796 new CreatePresentationConnectionRequest( | 790 new CreatePresentationConnectionRequest( |
| 797 render_frame_host_id, presentation_url, | 791 render_frame_host_id, presentation_urls, |
| 798 GetLastCommittedURLForFrame(render_frame_host_id), | 792 GetLastCommittedURLForFrame(render_frame_host_id), |
| 799 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, | 793 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |
| 800 weak_factory_.GetWeakPtr(), render_process_id, | 794 weak_factory_.GetWeakPtr(), render_process_id, |
| 801 render_frame_id, success_cb), | 795 render_frame_id, success_cb), |
| 802 error_cb)); | 796 error_cb)); |
| 803 MediaRouterDialogController* controller = | 797 MediaRouterDialogController* controller = |
| 804 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); | 798 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); |
| 805 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { | 799 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { |
| 806 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; | 800 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; |
| 807 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 801 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 808 "Unable to create dialog.")); | 802 "Unable to create dialog.")); |
| 809 return; | 803 return; |
| 810 } | 804 } |
| 811 } | 805 } |
| 812 | 806 |
| 813 void PresentationServiceDelegateImpl::JoinSession( | 807 void PresentationServiceDelegateImpl::JoinSession( |
| 814 int render_process_id, | 808 int render_process_id, |
| 815 int render_frame_id, | 809 int render_frame_id, |
| 816 const std::vector<GURL>& presentation_urls, | 810 const std::vector<GURL>& presentation_urls, |
| 817 const std::string& presentation_id, | 811 const std::string& presentation_id, |
| 818 const content::PresentationSessionStartedCallback& success_cb, | 812 const content::PresentationSessionStartedCallback& success_cb, |
| 819 const content::PresentationSessionErrorCallback& error_cb) { | 813 const content::PresentationSessionErrorCallback& error_cb) { |
| 814 DVLOG(2) << "PresentationServiceDelegateImpl::JoinSession"; | |
| 820 if (presentation_urls.empty()) { | 815 if (presentation_urls.empty()) { |
| 821 error_cb.Run(content::PresentationError( | 816 error_cb.Run(content::PresentationError( |
| 822 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 817 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 823 "Invalid presentation arguments.")); | 818 "Invalid presentation arguments.")); |
| 819 return; | |
| 824 } | 820 } |
| 825 | 821 |
| 826 // TODO(crbug.com/627655): Handle multiple URLs. | 822 // TODO(zhaobin): This may break if we have multiple cast specific urls. To |
| 827 const GURL& presentation_url = presentation_urls[0]; | 823 // fix this, we need the Media Router to keep track of which presentation URL |
| 824 // was used to start the presentation named by |presentation_id|. | |
| 825 // was used to start the presentation named by |presentation_id|. | |
|
mark a. foltz
2016/12/02 23:13:37
This line is doubled.
zhaobin
2016/12/06 20:06:37
Done.
| |
| 826 auto presentation_url = std::find_if( | |
|
mark a. foltz
2016/12/02 23:13:37
Note, we already have |presentation_id_to_route_id
zhaobin
2016/12/06 20:06:36
Acknowledged.
| |
| 827 presentation_urls.begin(), presentation_urls.end(), [](const GURL& url) { | |
| 828 return CanConnectToMediaSource(MediaSource(url)); | |
| 829 }); | |
| 830 if (presentation_url == presentation_urls.end()) { | |
| 831 error_cb.Run(content::PresentationError( | |
| 832 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | |
| 833 "Presentation urls do support reconnect.")); | |
| 834 return; | |
| 835 } | |
| 836 | |
| 828 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 837 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| 829 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 838 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 830 route_response_callbacks.push_back( | 839 route_response_callbacks.push_back( |
| 831 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, | 840 base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| 832 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 841 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| 833 presentation_url, presentation_id, success_cb, error_cb)); | 842 *presentation_url, presentation_id, success_cb, error_cb)); |
| 834 router_->JoinRoute( | 843 router_->JoinRoute( |
| 835 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, | 844 MediaSourceForPresentationUrl(*presentation_url).id(), presentation_id, |
| 836 GetLastCommittedURLForFrame( | 845 GetLastCommittedURLForFrame( |
| 837 RenderFrameHostId(render_process_id, render_frame_id)) | 846 RenderFrameHostId(render_process_id, render_frame_id)) |
| 838 .GetOrigin(), | 847 .GetOrigin(), |
| 839 web_contents_, route_response_callbacks, base::TimeDelta(), incognito); | 848 web_contents_, route_response_callbacks, base::TimeDelta(), incognito); |
| 840 } | 849 } |
| 841 | 850 |
| 842 void PresentationServiceDelegateImpl::CloseConnection( | 851 void PresentationServiceDelegateImpl::CloseConnection( |
| 843 int render_process_id, | 852 int render_process_id, |
| 844 int render_frame_id, | 853 int render_frame_id, |
| 845 const std::string& presentation_id) { | 854 const std::string& presentation_id) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 913 const content::PresentationConnectionStateChangedCallback& | 922 const content::PresentationConnectionStateChangedCallback& |
| 914 state_changed_cb) { | 923 state_changed_cb) { |
| 915 frame_manager_->ListenForConnectionStateChange( | 924 frame_manager_->ListenForConnectionStateChange( |
| 916 RenderFrameHostId(render_process_id, render_frame_id), connection, | 925 RenderFrameHostId(render_process_id, render_frame_id), connection, |
| 917 state_changed_cb); | 926 state_changed_cb); |
| 918 } | 927 } |
| 919 | 928 |
| 920 void PresentationServiceDelegateImpl::OnRouteResponse( | 929 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 921 const PresentationRequest& presentation_request, | 930 const PresentationRequest& presentation_request, |
| 922 const RouteRequestResult& result) { | 931 const RouteRequestResult& result) { |
| 923 if (!result.route()) | 932 if (!result.route() || |
| 933 base::STLCount(presentation_request.presentation_urls(), | |
|
imcheng
2016/12/05 21:58:39
!base::ContainsValue(...)
zhaobin
2016/12/06 20:06:36
Done.
| |
| 934 result.presentation_url()) == 0) { | |
| 924 return; | 935 return; |
| 936 } | |
| 925 | 937 |
| 926 content::PresentationSessionInfo session_info( | 938 content::PresentationSessionInfo session_info(result.presentation_url(), |
| 927 presentation_request.presentation_url(), result.presentation_id()); | 939 result.presentation_id()); |
| 928 frame_manager_->OnDefaultPresentationSessionStarted( | 940 frame_manager_->OnDefaultPresentationSessionStarted( |
| 929 presentation_request, session_info, *result.route()); | 941 presentation_request, session_info, *result.route()); |
| 930 } | 942 } |
| 931 | 943 |
| 932 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( | 944 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |
| 933 DefaultPresentationRequestObserver* observer) { | 945 DefaultPresentationRequestObserver* observer) { |
| 934 frame_manager_->AddDefaultPresentationRequestObserver(observer); | 946 frame_manager_->AddDefaultPresentationRequestObserver(observer); |
| 935 } | 947 } |
| 936 | 948 |
| 937 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( | 949 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 963 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 975 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 964 int render_process_id, | 976 int render_process_id, |
| 965 int render_frame_id, | 977 int render_frame_id, |
| 966 const MediaSource::Id& source_id) const { | 978 const MediaSource::Id& source_id) const { |
| 967 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 979 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 968 return frame_manager_->HasScreenAvailabilityListenerForTest( | 980 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 969 render_frame_host_id, source_id); | 981 render_frame_host_id, source_id); |
| 970 } | 982 } |
| 971 | 983 |
| 972 } // namespace media_router | 984 } // namespace media_router |
| OLD | NEW |