| 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> |
| 11 | 11 |
| 12 #include "base/containers/small_map.h" | 12 #include "base/containers/small_map.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "chrome/browser/media/router/create_presentation_connection_request.h" | 16 #include "chrome/browser/media/router/create_presentation_connection_request.h" |
| 17 #include "chrome/browser/media/router/media_route.h" | 17 #include "chrome/browser/media/router/media_route.h" |
| 18 #include "chrome/browser/media/router/media_router.h" | 18 #include "chrome/browser/media/router/media_router.h" |
| 19 #include "chrome/browser/media/router/media_router_dialog_controller.h" | 19 #include "chrome/browser/media/router/media_router_dialog_controller.h" |
| 20 #include "chrome/browser/media/router/media_router_factory.h" | 20 #include "chrome/browser/media/router/media_router_factory.h" |
| 21 #include "chrome/browser/media/router/media_sink.h" | 21 #include "chrome/browser/media/router/media_sink.h" |
| 22 #include "chrome/browser/media/router/media_source_helper.h" | 22 #include "chrome/browser/media/router/media_source_helper.h" |
| 23 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 24 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h" |
| 23 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" | 25 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" |
| 24 #include "chrome/browser/media/router/route_message.h" | 26 #include "chrome/browser/media/router/route_message.h" |
| 25 #include "chrome/browser/media/router/route_message_observer.h" | 27 #include "chrome/browser/media/router/route_message_observer.h" |
| 26 #include "chrome/browser/media/router/route_request_result.h" | 28 #include "chrome/browser/media/router/route_request_result.h" |
| 27 #include "chrome/browser/sessions/session_tab_helper.h" | 29 #include "chrome/browser/sessions/session_tab_helper.h" |
| 28 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/presentation_screen_availability_listener.h" | 31 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 30 #include "content/public/browser/presentation_session.h" | 32 #include "content/public/browser/presentation_session.h" |
| 31 #include "content/public/browser/render_frame_host.h" | 33 #include "content/public/browser/render_frame_host.h" |
| 32 #include "content/public/browser/render_process_host.h" | 34 #include "content/public/browser/render_process_host.h" |
| 35 #include "url/gurl.h" |
| 33 | 36 |
| 34 #if !defined(OS_ANDROID) | 37 #if !defined(OS_ANDROID) |
| 35 #include "chrome/browser/profiles/profile.h" | 38 #include "chrome/browser/profiles/profile.h" |
| 36 #include "chrome/common/pref_names.h" | 39 #include "chrome/common/pref_names.h" |
| 37 #include "components/prefs/pref_service.h" | 40 #include "components/prefs/pref_service.h" |
| 38 #endif | 41 #endif |
| 39 | 42 |
| 40 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 43 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 41 media_router::PresentationServiceDelegateImpl); | 44 media_router::PresentationServiceDelegateImpl); |
| 42 | 45 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 189 |
| 187 DelegateObserver* delegate_observer_; | 190 DelegateObserver* delegate_observer_; |
| 188 }; | 191 }; |
| 189 | 192 |
| 190 PresentationFrame::PresentationFrame( | 193 PresentationFrame::PresentationFrame( |
| 191 const RenderFrameHostId& render_frame_host_id, | 194 const RenderFrameHostId& render_frame_host_id, |
| 192 content::WebContents* web_contents, | 195 content::WebContents* web_contents, |
| 193 MediaRouter* router) | 196 MediaRouter* router) |
| 194 : render_frame_host_id_(render_frame_host_id), | 197 : render_frame_host_id_(render_frame_host_id), |
| 195 web_contents_(web_contents), | 198 web_contents_(web_contents), |
| 196 router_(router), | 199 router_(router) { |
| 197 delegate_observer_(nullptr) { | |
| 198 DCHECK(web_contents_); | 200 DCHECK(web_contents_); |
| 199 DCHECK(router_); | 201 DCHECK(router_); |
| 200 } | 202 } |
| 201 | 203 |
| 202 PresentationFrame::~PresentationFrame() { | 204 PresentationFrame::~PresentationFrame() { |
| 203 } | 205 } |
| 204 | 206 |
| 205 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { | |
| 206 if (delegate_observer_) | |
| 207 delegate_observer_->OnDelegateDestroyed(); | |
| 208 } | |
| 209 | |
| 210 void PresentationFrame::OnPresentationSessionStarted( | 207 void PresentationFrame::OnPresentationSessionStarted( |
| 211 const content::PresentationSessionInfo& session, | 208 const content::PresentationSessionInfo& session, |
| 212 const MediaRoute& route) { | 209 const MediaRoute& route) { |
| 213 presentation_id_to_route_id_[session.presentation_id] = | 210 presentation_id_to_route_id_[session.presentation_id] = |
| 214 route.media_route_id(); | 211 route.media_route_id(); |
| 215 } | 212 } |
| 216 | 213 |
| 217 const MediaRoute::Id PresentationFrame::GetRouteId( | 214 const MediaRoute::Id PresentationFrame::GetRouteId( |
| 218 const std::string& presentation_id) const { | 215 const std::string& presentation_id) const { |
| 219 auto it = presentation_id_to_route_id_.find(presentation_id); | 216 auto it = presentation_id_to_route_id_.find(presentation_id); |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 }; | 448 }; |
| 452 | 449 |
| 453 PresentationFrameManager::PresentationFrameManager( | 450 PresentationFrameManager::PresentationFrameManager( |
| 454 content::WebContents* web_contents, | 451 content::WebContents* web_contents, |
| 455 MediaRouter* router) | 452 MediaRouter* router) |
| 456 : router_(router), web_contents_(web_contents) { | 453 : router_(router), web_contents_(web_contents) { |
| 457 DCHECK(web_contents_); | 454 DCHECK(web_contents_); |
| 458 DCHECK(router_); | 455 DCHECK(router_); |
| 459 } | 456 } |
| 460 | 457 |
| 461 PresentationFrameManager::~PresentationFrameManager() { | 458 PresentationFrameManager::~PresentationFrameManager() {} |
| 462 for (auto& frame : presentation_frames_) | |
| 463 frame.second->OnPresentationServiceDelegateDestroyed(); | |
| 464 } | |
| 465 | 459 |
| 466 void PresentationFrameManager::OnPresentationSessionStarted( | 460 void PresentationFrameManager::OnPresentationSessionStarted( |
| 467 const RenderFrameHostId& render_frame_host_id, | 461 const RenderFrameHostId& render_frame_host_id, |
| 468 const content::PresentationSessionInfo& session, | 462 const content::PresentationSessionInfo& session, |
| 469 const MediaRoute& route) { | 463 const MediaRoute& route) { |
| 470 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 464 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 471 presentation_frame->OnPresentationSessionStarted(session, route); | 465 presentation_frame->OnPresentationSessionStarted(session, route); |
| 472 } | 466 } |
| 473 | 467 |
| 474 void PresentationFrameManager::OnDefaultPresentationSessionStarted( | 468 void PresentationFrameManager::OnDefaultPresentationSessionStarted( |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 } else { | 555 } else { |
| 562 DCHECK(!callback.is_null()); | 556 DCHECK(!callback.is_null()); |
| 563 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); | 557 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 564 PresentationRequest request(render_frame_host_id, default_presentation_urls, | 558 PresentationRequest request(render_frame_host_id, default_presentation_urls, |
| 565 frame_url); | 559 frame_url); |
| 566 default_presentation_started_callback_ = callback; | 560 default_presentation_started_callback_ = callback; |
| 567 SetDefaultPresentationRequest(request); | 561 SetDefaultPresentationRequest(request); |
| 568 } | 562 } |
| 569 } | 563 } |
| 570 | 564 |
| 571 void PresentationFrameManager::AddDelegateObserver( | |
| 572 const RenderFrameHostId& render_frame_host_id, | |
| 573 DelegateObserver* observer) { | |
| 574 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
| 575 presentation_frame->set_delegate_observer(observer); | |
| 576 } | |
| 577 | |
| 578 void PresentationFrameManager::RemoveDelegateObserver( | |
| 579 const RenderFrameHostId& render_frame_host_id) { | |
| 580 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 581 if (it != presentation_frames_.end()) { | |
| 582 it->second->set_delegate_observer(nullptr); | |
| 583 presentation_frames_.erase(it); | |
| 584 } | |
| 585 } | |
| 586 | |
| 587 void PresentationFrameManager::AddDefaultPresentationRequestObserver( | 565 void PresentationFrameManager::AddDefaultPresentationRequestObserver( |
| 588 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 566 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 589 observer) { | 567 observer) { |
| 590 default_presentation_request_observers_.AddObserver(observer); | 568 default_presentation_request_observers_.AddObserver(observer); |
| 591 } | 569 } |
| 592 | 570 |
| 593 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( | 571 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( |
| 594 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 572 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 595 observer) { | 573 observer) { |
| 596 default_presentation_request_observers_.RemoveObserver(observer); | 574 default_presentation_request_observers_.RemoveObserver(observer); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 DCHECK(router_); | 659 DCHECK(router_); |
| 682 } | 660 } |
| 683 | 661 |
| 684 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { | 662 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { |
| 685 } | 663 } |
| 686 | 664 |
| 687 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, | 665 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, |
| 688 int render_frame_id, | 666 int render_frame_id, |
| 689 DelegateObserver* observer) { | 667 DelegateObserver* observer) { |
| 690 DCHECK(observer); | 668 DCHECK(observer); |
| 691 frame_manager_->AddDelegateObserver( | 669 observers_.AddObserver(render_process_id, render_frame_id, observer); |
| 692 RenderFrameHostId(render_process_id, render_frame_id), observer); | |
| 693 } | 670 } |
| 694 | 671 |
| 695 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, | 672 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, |
| 696 int render_frame_id) { | 673 int render_frame_id) { |
| 697 frame_manager_->RemoveDelegateObserver( | 674 observers_.RemoveObserver(render_process_id, render_frame_id); |
| 698 RenderFrameHostId(render_process_id, render_frame_id)); | |
| 699 } | 675 } |
| 700 | 676 |
| 701 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( | 677 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( |
| 702 int render_process_id, | 678 int render_process_id, |
| 703 int render_frame_id, | 679 int render_frame_id, |
| 704 content::PresentationScreenAvailabilityListener* listener) { | 680 content::PresentationScreenAvailabilityListener* listener) { |
| 705 DCHECK(listener); | 681 DCHECK(listener); |
| 706 return frame_manager_->SetScreenAvailabilityListener( | 682 return frame_manager_->SetScreenAvailabilityListener( |
| 707 RenderFrameHostId(render_process_id, render_frame_id), listener); | 683 RenderFrameHostId(render_process_id, render_frame_id), listener); |
| 708 } | 684 } |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 928 int render_process_id, | 904 int render_process_id, |
| 929 int render_frame_id, | 905 int render_frame_id, |
| 930 const content::PresentationSessionInfo& connection, | 906 const content::PresentationSessionInfo& connection, |
| 931 const content::PresentationConnectionStateChangedCallback& | 907 const content::PresentationConnectionStateChangedCallback& |
| 932 state_changed_cb) { | 908 state_changed_cb) { |
| 933 frame_manager_->ListenForConnectionStateChange( | 909 frame_manager_->ListenForConnectionStateChange( |
| 934 RenderFrameHostId(render_process_id, render_frame_id), connection, | 910 RenderFrameHostId(render_process_id, render_frame_id), connection, |
| 935 state_changed_cb); | 911 state_changed_cb); |
| 936 } | 912 } |
| 937 | 913 |
| 914 void PresentationServiceDelegateImpl::ConnectToOffscreenPresentation( |
| 915 int render_process_id, |
| 916 int render_frame_id, |
| 917 const content::PresentationSessionInfo& session, |
| 918 content::PresentationConnectionPtr controller_conn_ptr, |
| 919 content::PresentationConnectionRequest receiver_conn_request) { |
| 920 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 921 auto* const offscreen_presentation_manager = |
| 922 OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| 923 web_contents_); |
| 924 offscreen_presentation_manager->RegisterOffscreenPresentationController( |
| 925 session.presentation_id, session.presentation_url, render_frame_host_id, |
| 926 std::move(controller_conn_ptr), std::move(receiver_conn_request)); |
| 927 } |
| 928 |
| 938 void PresentationServiceDelegateImpl::OnRouteResponse( | 929 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 939 const PresentationRequest& presentation_request, | 930 const PresentationRequest& presentation_request, |
| 940 const RouteRequestResult& result) { | 931 const RouteRequestResult& result) { |
| 941 if (!result.route() || | 932 if (!result.route() || |
| 942 !base::ContainsValue(presentation_request.presentation_urls(), | 933 !base::ContainsValue(presentation_request.presentation_urls(), |
| 943 result.presentation_url())) { | 934 result.presentation_url())) { |
| 944 return; | 935 return; |
| 945 } | 936 } |
| 946 | 937 |
| 947 content::PresentationSessionInfo session_info(result.presentation_url(), | 938 content::PresentationSessionInfo session_info(result.presentation_url(), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 996 const base::ListValue* origins = | 987 const base::ListValue* origins = |
| 997 Profile::FromBrowserContext(web_contents_->GetBrowserContext()) | 988 Profile::FromBrowserContext(web_contents_->GetBrowserContext()) |
| 998 ->GetPrefs() | 989 ->GetPrefs() |
| 999 ->GetList(prefs::kMediaRouterTabMirroringSources); | 990 ->GetList(prefs::kMediaRouterTabMirroringSources); |
| 1000 return origins && | 991 return origins && |
| 1001 origins->Find(base::StringValue(origin.Serialize())) != origins->end(); | 992 origins->Find(base::StringValue(origin.Serialize())) != origins->end(); |
| 1002 } | 993 } |
| 1003 #endif // !defined(OS_ANDROID) | 994 #endif // !defined(OS_ANDROID) |
| 1004 | 995 |
| 1005 } // namespace media_router | 996 } // namespace media_router |
| OLD | NEW |