| 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 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 } | 317 } |
| 318 | 318 |
| 319 session_messages_observers_.insert(std::make_pair( | 319 session_messages_observers_.insert(std::make_pair( |
| 320 route_id, base::MakeUnique<PresentationSessionMessagesObserver>( | 320 route_id, base::MakeUnique<PresentationSessionMessagesObserver>( |
| 321 router_, it->second, message_cb))); | 321 router_, it->second, message_cb))); |
| 322 } | 322 } |
| 323 | 323 |
| 324 MediaSource PresentationFrame::GetMediaSourceFromListener( | 324 MediaSource PresentationFrame::GetMediaSourceFromListener( |
| 325 content::PresentationScreenAvailabilityListener* listener) const { | 325 content::PresentationScreenAvailabilityListener* listener) const { |
| 326 // If the default presentation URL is empty then fall back to tab mirroring. | 326 // If the default presentation URL is empty then fall back to tab mirroring. |
| 327 std::string availability_url(listener->GetAvailabilityUrl()); | 327 return listener->GetAvailabilityUrl().is_empty() |
| 328 return availability_url.empty() | |
| 329 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) | 328 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) |
| 330 : MediaSourceForPresentationUrl(availability_url); | 329 : MediaSourceForPresentationUrl(listener->GetAvailabilityUrl()); |
| 331 } | 330 } |
| 332 | 331 |
| 333 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. | 332 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. |
| 334 class PresentationFrameManager { | 333 class PresentationFrameManager { |
| 335 public: | 334 public: |
| 336 PresentationFrameManager(content::WebContents* web_contents, | 335 PresentationFrameManager(content::WebContents* web_contents, |
| 337 MediaRouter* router); | 336 MediaRouter* router); |
| 338 ~PresentationFrameManager(); | 337 ~PresentationFrameManager(); |
| 339 | 338 |
| 340 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 339 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 352 void ListenForSessionMessages( | 351 void ListenForSessionMessages( |
| 353 const RenderFrameHostId& render_frame_host_id, | 352 const RenderFrameHostId& render_frame_host_id, |
| 354 const content::PresentationSessionInfo& session, | 353 const content::PresentationSessionInfo& session, |
| 355 const content::PresentationSessionMessageCallback& message_cb); | 354 const content::PresentationSessionMessageCallback& message_cb); |
| 356 | 355 |
| 357 // Sets or clears the default presentation request and callback for the given | 356 // Sets or clears the default presentation request and callback for the given |
| 358 // frame. Also sets / clears the default presentation requests for the owning | 357 // frame. Also sets / clears the default presentation requests for the owning |
| 359 // tab WebContents. | 358 // tab WebContents. |
| 360 void SetDefaultPresentationUrl( | 359 void SetDefaultPresentationUrl( |
| 361 const RenderFrameHostId& render_frame_host_id, | 360 const RenderFrameHostId& render_frame_host_id, |
| 362 const std::string& default_presentation_url, | 361 const GURL& default_presentation_url, |
| 363 const content::PresentationSessionStartedCallback& callback); | 362 const content::PresentationSessionStartedCallback& callback); |
| 364 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 363 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| 365 DelegateObserver* observer); | 364 DelegateObserver* observer); |
| 366 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 365 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| 367 void AddDefaultPresentationRequestObserver( | 366 void AddDefaultPresentationRequestObserver( |
| 368 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 367 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 369 observer); | 368 observer); |
| 370 void RemoveDefaultPresentationRequestObserver( | 369 void RemoveDefaultPresentationRequestObserver( |
| 371 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 370 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 372 observer); | 371 observer); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " | 530 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " |
| 532 << "for: (" << render_frame_host_id.first << ", " | 531 << "for: (" << render_frame_host_id.first << ", " |
| 533 << render_frame_host_id.second << ")"; | 532 << render_frame_host_id.second << ")"; |
| 534 return; | 533 return; |
| 535 } | 534 } |
| 536 it->second->ListenForSessionMessages(session, message_cb); | 535 it->second->ListenForSessionMessages(session, message_cb); |
| 537 } | 536 } |
| 538 | 537 |
| 539 void PresentationFrameManager::SetDefaultPresentationUrl( | 538 void PresentationFrameManager::SetDefaultPresentationUrl( |
| 540 const RenderFrameHostId& render_frame_host_id, | 539 const RenderFrameHostId& render_frame_host_id, |
| 541 const std::string& default_presentation_url, | 540 const GURL& default_presentation_url, |
| 542 const content::PresentationSessionStartedCallback& callback) { | 541 const content::PresentationSessionStartedCallback& callback) { |
| 543 if (!IsMainFrame(render_frame_host_id)) | 542 if (!IsMainFrame(render_frame_host_id)) |
| 544 return; | 543 return; |
| 545 | 544 |
| 546 if (default_presentation_url.empty()) { | 545 if (default_presentation_url.is_empty()) { |
| 547 ClearDefaultPresentationRequest(); | 546 ClearDefaultPresentationRequest(); |
| 548 } else { | 547 } else { |
| 549 DCHECK(!callback.is_null()); | 548 DCHECK(!callback.is_null()); |
| 550 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); | 549 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 551 PresentationRequest request(render_frame_host_id, | 550 PresentationRequest request(render_frame_host_id, |
| 552 {default_presentation_url}, frame_url); | 551 std::vector<GURL>({default_presentation_url}), |
| 552 frame_url); |
| 553 default_presentation_started_callback_ = callback; | 553 default_presentation_started_callback_ = callback; |
| 554 SetDefaultPresentationRequest(request); | 554 SetDefaultPresentationRequest(request); |
| 555 } | 555 } |
| 556 } | 556 } |
| 557 | 557 |
| 558 void PresentationFrameManager::AddDelegateObserver( | 558 void PresentationFrameManager::AddDelegateObserver( |
| 559 const RenderFrameHostId& render_frame_host_id, | 559 const RenderFrameHostId& render_frame_host_id, |
| 560 DelegateObserver* observer) { | 560 DelegateObserver* observer) { |
| 561 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 561 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 562 presentation_frame->set_delegate_observer(observer); | 562 presentation_frame->set_delegate_observer(observer); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 | 708 |
| 709 void PresentationServiceDelegateImpl::Reset(int render_process_id, | 709 void PresentationServiceDelegateImpl::Reset(int render_process_id, |
| 710 int render_frame_id) { | 710 int render_frame_id) { |
| 711 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 711 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 712 frame_manager_->Reset(render_frame_host_id); | 712 frame_manager_->Reset(render_frame_host_id); |
| 713 } | 713 } |
| 714 | 714 |
| 715 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( | 715 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( |
| 716 int render_process_id, | 716 int render_process_id, |
| 717 int render_frame_id, | 717 int render_frame_id, |
| 718 const std::vector<std::string>& default_presentation_urls, | 718 const std::vector<GURL>& default_presentation_urls, |
| 719 const content::PresentationSessionStartedCallback& callback) { | 719 const content::PresentationSessionStartedCallback& callback) { |
| 720 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 720 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 721 if (default_presentation_urls.empty()) { | 721 if (default_presentation_urls.empty()) { |
| 722 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, | 722 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, GURL(), |
| 723 std::string(), callback); | 723 callback); |
| 724 } else { | 724 } else { |
| 725 // TODO(crbug.com/627655): Handle multiple URLs. | 725 // TODO(crbug.com/627655): Handle multiple URLs. |
| 726 frame_manager_->SetDefaultPresentationUrl( | 726 frame_manager_->SetDefaultPresentationUrl( |
| 727 render_frame_host_id, default_presentation_urls[0], callback); | 727 render_frame_host_id, default_presentation_urls[0], callback); |
| 728 } | 728 } |
| 729 } | 729 } |
| 730 | 730 |
| 731 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 731 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| 732 int render_process_id, | 732 int render_process_id, |
| 733 int render_frame_id, | 733 int render_frame_id, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 763 << ", presentation ID: " << new_session.presentation_id; | 763 << ", presentation ID: " << new_session.presentation_id; |
| 764 frame_manager_->OnPresentationSessionStarted( | 764 frame_manager_->OnPresentationSessionStarted( |
| 765 RenderFrameHostId(render_process_id, render_frame_id), new_session, | 765 RenderFrameHostId(render_process_id, render_frame_id), new_session, |
| 766 route_id); | 766 route_id); |
| 767 success_cb.Run(new_session); | 767 success_cb.Run(new_session); |
| 768 } | 768 } |
| 769 | 769 |
| 770 void PresentationServiceDelegateImpl::StartSession( | 770 void PresentationServiceDelegateImpl::StartSession( |
| 771 int render_process_id, | 771 int render_process_id, |
| 772 int render_frame_id, | 772 int render_frame_id, |
| 773 const std::vector<std::string>& presentation_urls, | 773 const std::vector<GURL>& presentation_urls, |
| 774 const content::PresentationSessionStartedCallback& success_cb, | 774 const content::PresentationSessionStartedCallback& success_cb, |
| 775 const content::PresentationSessionErrorCallback& error_cb) { | 775 const content::PresentationSessionErrorCallback& error_cb) { |
| 776 if (presentation_urls.empty()) { | 776 if (presentation_urls.empty()) { |
| 777 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 777 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 778 "Invalid presentation arguments.")); | 778 "Invalid presentation arguments.")); |
| 779 return; | 779 return; |
| 780 } | 780 } |
| 781 | 781 |
| 782 // TODO(crbug.com/627655): Handle multiple URLs. | 782 // TODO(crbug.com/627655): Handle multiple URLs. |
| 783 const std::string& presentation_url = presentation_urls[0]; | 783 const GURL& presentation_url = presentation_urls[0]; |
| 784 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { | 784 if (presentation_url.is_empty() || |
| 785 !IsValidPresentationUrl(presentation_url)) { |
| 785 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 786 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 786 "Invalid presentation arguments.")); | 787 "Invalid presentation arguments.")); |
| 787 return; | 788 return; |
| 788 } | 789 } |
| 789 | 790 |
| 790 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 791 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 791 std::unique_ptr<CreatePresentationConnectionRequest> request( | 792 std::unique_ptr<CreatePresentationConnectionRequest> request( |
| 792 new CreatePresentationConnectionRequest( | 793 new CreatePresentationConnectionRequest( |
| 793 render_frame_host_id, presentation_url, | 794 render_frame_host_id, presentation_url, |
| 794 GetLastCommittedURLForFrame(render_frame_host_id), | 795 GetLastCommittedURLForFrame(render_frame_host_id), |
| 795 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, | 796 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |
| 796 weak_factory_.GetWeakPtr(), render_process_id, | 797 weak_factory_.GetWeakPtr(), render_process_id, |
| 797 render_frame_id, success_cb), | 798 render_frame_id, success_cb), |
| 798 error_cb)); | 799 error_cb)); |
| 799 MediaRouterDialogController* controller = | 800 MediaRouterDialogController* controller = |
| 800 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); | 801 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); |
| 801 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { | 802 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { |
| 802 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; | 803 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; |
| 803 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 804 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 804 "Unable to create dialog.")); | 805 "Unable to create dialog.")); |
| 805 return; | 806 return; |
| 806 } | 807 } |
| 807 } | 808 } |
| 808 | 809 |
| 809 void PresentationServiceDelegateImpl::JoinSession( | 810 void PresentationServiceDelegateImpl::JoinSession( |
| 810 int render_process_id, | 811 int render_process_id, |
| 811 int render_frame_id, | 812 int render_frame_id, |
| 812 const std::vector<std::string>& presentation_urls, | 813 const std::vector<GURL>& presentation_urls, |
| 813 const std::string& presentation_id, | 814 const std::string& presentation_id, |
| 814 const content::PresentationSessionStartedCallback& success_cb, | 815 const content::PresentationSessionStartedCallback& success_cb, |
| 815 const content::PresentationSessionErrorCallback& error_cb) { | 816 const content::PresentationSessionErrorCallback& error_cb) { |
| 816 if (presentation_urls.empty()) { | 817 if (presentation_urls.empty()) { |
| 817 error_cb.Run(content::PresentationError( | 818 error_cb.Run(content::PresentationError( |
| 818 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 819 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 819 "Invalid presentation arguments.")); | 820 "Invalid presentation arguments.")); |
| 820 } | 821 } |
| 821 | 822 |
| 822 // TODO(crbug.com/627655): Handle multiple URLs. | 823 // TODO(crbug.com/627655): Handle multiple URLs. |
| 823 const std::string& presentation_url = presentation_urls[0]; | 824 const GURL& presentation_url = presentation_urls[0]; |
| 824 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 825 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| 825 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 826 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 826 route_response_callbacks.push_back(base::Bind( | 827 route_response_callbacks.push_back(base::Bind( |
| 827 &PresentationServiceDelegateImpl::OnJoinRouteResponse, | 828 &PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| 828 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 829 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| 829 content::PresentationSessionInfo(presentation_url, presentation_id), | 830 content::PresentationSessionInfo(presentation_url, presentation_id), |
| 830 success_cb, error_cb)); | 831 success_cb, error_cb)); |
| 831 router_->JoinRoute( | 832 router_->JoinRoute( |
| 832 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, | 833 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, |
| 833 GetLastCommittedURLForFrame( | 834 GetLastCommittedURLForFrame( |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 961 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 961 int render_process_id, | 962 int render_process_id, |
| 962 int render_frame_id, | 963 int render_frame_id, |
| 963 const MediaSource::Id& source_id) const { | 964 const MediaSource::Id& source_id) const { |
| 964 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 965 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 965 return frame_manager_->HasScreenAvailabilityListenerForTest( | 966 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 966 render_frame_host_id, source_id); | 967 render_frame_host_id, source_id); |
| 967 } | 968 } |
| 968 | 969 |
| 969 } // namespace media_router | 970 } // namespace media_router |
| OLD | NEW |