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