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 "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.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 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 38 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
36 media_router::PresentationServiceDelegateImpl); | 39 media_router::PresentationServiceDelegateImpl); |
37 | 40 |
38 using content::RenderFrameHost; | 41 using content::RenderFrameHost; |
39 | 42 |
40 namespace media_router { | 43 namespace media_router { |
41 | 44 |
42 namespace { | 45 namespace { |
43 | 46 |
44 using DelegateObserver = content::PresentationServiceDelegate::Observer; | 47 using DelegateObserver = content::PresentationServiceDelegateBase::Observer; |
45 | 48 |
46 // Returns the unique identifier for the supplied RenderFrameHost. | 49 // Returns the unique identifier for the supplied RenderFrameHost. |
47 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { | 50 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { |
48 int render_process_id = render_frame_host->GetProcess()->GetID(); | 51 int render_process_id = render_frame_host->GetProcess()->GetID(); |
49 int render_frame_id = render_frame_host->GetRoutingID(); | 52 int render_frame_id = render_frame_host->GetRoutingID(); |
50 return RenderFrameHostId(render_process_id, render_frame_id); | 53 return RenderFrameHostId(render_process_id, render_frame_id); |
51 } | 54 } |
52 | 55 |
53 // Gets the last committed URL for the render frame specified by | 56 // Gets the last committed URL for the render frame specified by |
54 // |render_frame_host_id|. | 57 // |render_frame_host_id|. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 void Reset(); | 141 void Reset(); |
139 void RemoveConnection(const std::string& presentation_id, | 142 void RemoveConnection(const std::string& presentation_id, |
140 const MediaRoute::Id& route_id); | 143 const MediaRoute::Id& route_id); |
141 | 144 |
142 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 145 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
143 const std::vector<MediaRoute::Id> GetRouteIds() const; | 146 const std::vector<MediaRoute::Id> GetRouteIds() const; |
144 | 147 |
145 void OnPresentationSessionStarted( | 148 void OnPresentationSessionStarted( |
146 const content::PresentationSessionInfo& session, | 149 const content::PresentationSessionInfo& session, |
147 const MediaRoute::Id& route_id); | 150 const MediaRoute::Id& route_id); |
148 void OnPresentationServiceDelegateDestroyed() const; | |
149 | |
150 void set_delegate_observer(DelegateObserver* observer) { | |
151 delegate_observer_ = observer; | |
152 } | |
153 | 151 |
154 private: | 152 private: |
155 MediaSource GetMediaSourceFromListener( | 153 MediaSource GetMediaSourceFromListener( |
156 content::PresentationScreenAvailabilityListener* listener) const; | 154 content::PresentationScreenAvailabilityListener* listener) const; |
157 base::SmallMap<std::map<std::string, MediaRoute::Id>> | 155 base::SmallMap<std::map<std::string, MediaRoute::Id>> |
158 presentation_id_to_route_id_; | 156 presentation_id_to_route_id_; |
159 base::SmallMap< | 157 base::SmallMap< |
160 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> | 158 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> |
161 url_to_sinks_observer_; | 159 url_to_sinks_observer_; |
162 std::unordered_map< | 160 std::unordered_map< |
163 MediaRoute::Id, | 161 MediaRoute::Id, |
164 std::unique_ptr<PresentationConnectionStateSubscription>> | 162 std::unique_ptr<PresentationConnectionStateSubscription>> |
165 connection_state_subscriptions_; | 163 connection_state_subscriptions_; |
166 std::unordered_map< | 164 std::unordered_map< |
167 MediaRoute::Id, | 165 MediaRoute::Id, |
168 std::unique_ptr<PresentationSessionMessagesObserver>> | 166 std::unique_ptr<PresentationSessionMessagesObserver>> |
169 session_messages_observers_; | 167 session_messages_observers_; |
170 | 168 |
171 RenderFrameHostId render_frame_host_id_; | 169 RenderFrameHostId render_frame_host_id_; |
172 | 170 |
173 // References to the owning WebContents, and the corresponding MediaRouter. | 171 // References to the owning WebContents, and the corresponding MediaRouter. |
174 const content::WebContents* web_contents_; | 172 const content::WebContents* web_contents_; |
175 MediaRouter* router_; | 173 MediaRouter* router_; |
176 | |
177 DelegateObserver* delegate_observer_; | |
178 }; | 174 }; |
179 | 175 |
180 PresentationFrame::PresentationFrame( | 176 PresentationFrame::PresentationFrame( |
181 const RenderFrameHostId& render_frame_host_id, | 177 const RenderFrameHostId& render_frame_host_id, |
182 content::WebContents* web_contents, | 178 content::WebContents* web_contents, |
183 MediaRouter* router) | 179 MediaRouter* router) |
184 : render_frame_host_id_(render_frame_host_id), | 180 : render_frame_host_id_(render_frame_host_id), |
185 web_contents_(web_contents), | 181 web_contents_(web_contents), |
186 router_(router), | 182 router_(router) { |
187 delegate_observer_(nullptr) { | |
188 DCHECK(web_contents_); | 183 DCHECK(web_contents_); |
189 DCHECK(router_); | 184 DCHECK(router_); |
190 } | 185 } |
191 | 186 |
192 PresentationFrame::~PresentationFrame() { | 187 PresentationFrame::~PresentationFrame() { |
193 } | 188 } |
194 | 189 |
195 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { | |
196 if (delegate_observer_) | |
197 delegate_observer_->OnDelegateDestroyed(); | |
198 } | |
199 | |
200 void PresentationFrame::OnPresentationSessionStarted( | 190 void PresentationFrame::OnPresentationSessionStarted( |
201 const content::PresentationSessionInfo& session, | 191 const content::PresentationSessionInfo& session, |
202 const MediaRoute::Id& route_id) { | 192 const MediaRoute::Id& route_id) { |
203 presentation_id_to_route_id_[session.presentation_id] = route_id; | 193 presentation_id_to_route_id_[session.presentation_id] = route_id; |
204 } | 194 } |
205 | 195 |
206 const MediaRoute::Id PresentationFrame::GetRouteId( | 196 const MediaRoute::Id PresentationFrame::GetRouteId( |
207 const std::string& presentation_id) const { | 197 const std::string& presentation_id) const { |
208 auto it = presentation_id_to_route_id_.find(presentation_id); | 198 auto it = presentation_id_to_route_id_.find(presentation_id); |
209 return it != presentation_id_to_route_id_.end() ? it->second : ""; | 199 return it != presentation_id_to_route_id_.end() ? it->second : ""; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 }; | 430 }; |
441 | 431 |
442 PresentationFrameManager::PresentationFrameManager( | 432 PresentationFrameManager::PresentationFrameManager( |
443 content::WebContents* web_contents, | 433 content::WebContents* web_contents, |
444 MediaRouter* router) | 434 MediaRouter* router) |
445 : router_(router), web_contents_(web_contents) { | 435 : router_(router), web_contents_(web_contents) { |
446 DCHECK(web_contents_); | 436 DCHECK(web_contents_); |
447 DCHECK(router_); | 437 DCHECK(router_); |
448 } | 438 } |
449 | 439 |
450 PresentationFrameManager::~PresentationFrameManager() { | 440 PresentationFrameManager::~PresentationFrameManager() {} |
451 for (auto& frame : presentation_frames_) | |
452 frame.second->OnPresentationServiceDelegateDestroyed(); | |
453 } | |
454 | 441 |
455 void PresentationFrameManager::OnPresentationSessionStarted( | 442 void PresentationFrameManager::OnPresentationSessionStarted( |
456 const RenderFrameHostId& render_frame_host_id, | 443 const RenderFrameHostId& render_frame_host_id, |
457 const content::PresentationSessionInfo& session, | 444 const content::PresentationSessionInfo& session, |
458 const MediaRoute::Id& route_id) { | 445 const MediaRoute::Id& route_id) { |
459 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 446 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
460 presentation_frame->OnPresentationSessionStarted(session, route_id); | 447 presentation_frame->OnPresentationSessionStarted(session, route_id); |
461 } | 448 } |
462 | 449 |
463 void PresentationFrameManager::OnDefaultPresentationSessionStarted( | 450 void PresentationFrameManager::OnDefaultPresentationSessionStarted( |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 DCHECK(!callback.is_null()); | 538 DCHECK(!callback.is_null()); |
552 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); | 539 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
553 PresentationRequest request(render_frame_host_id, | 540 PresentationRequest request(render_frame_host_id, |
554 std::vector<GURL>({default_presentation_url}), | 541 std::vector<GURL>({default_presentation_url}), |
555 frame_url); | 542 frame_url); |
556 default_presentation_started_callback_ = callback; | 543 default_presentation_started_callback_ = callback; |
557 SetDefaultPresentationRequest(request); | 544 SetDefaultPresentationRequest(request); |
558 } | 545 } |
559 } | 546 } |
560 | 547 |
561 void PresentationFrameManager::AddDelegateObserver( | |
562 const RenderFrameHostId& render_frame_host_id, | |
563 DelegateObserver* observer) { | |
564 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
565 presentation_frame->set_delegate_observer(observer); | |
566 } | |
567 | |
568 void PresentationFrameManager::RemoveDelegateObserver( | |
569 const RenderFrameHostId& render_frame_host_id) { | |
570 const auto it = presentation_frames_.find(render_frame_host_id); | |
571 if (it != presentation_frames_.end()) { | |
572 it->second->set_delegate_observer(nullptr); | |
573 presentation_frames_.erase(it); | |
574 } | |
575 } | |
576 | |
577 void PresentationFrameManager::AddDefaultPresentationRequestObserver( | 548 void PresentationFrameManager::AddDefaultPresentationRequestObserver( |
578 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 549 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
579 observer) { | 550 observer) { |
580 default_presentation_request_observers_.AddObserver(observer); | 551 default_presentation_request_observers_.AddObserver(observer); |
581 } | 552 } |
582 | 553 |
583 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( | 554 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( |
584 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 555 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
585 observer) { | 556 observer) { |
586 default_presentation_request_observers_.RemoveObserver(observer); | 557 default_presentation_request_observers_.RemoveObserver(observer); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 DCHECK(web_contents_); | 641 DCHECK(web_contents_); |
671 DCHECK(router_); | 642 DCHECK(router_); |
672 } | 643 } |
673 | 644 |
674 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { | 645 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { |
675 } | 646 } |
676 | 647 |
677 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, | 648 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, |
678 int render_frame_id, | 649 int render_frame_id, |
679 DelegateObserver* observer) { | 650 DelegateObserver* observer) { |
680 DCHECK(observer); | 651 PresentationServiceDelegateBaseImpl::AddObserver(render_process_id, |
681 frame_manager_->AddDelegateObserver( | 652 render_frame_id, observer); |
682 RenderFrameHostId(render_process_id, render_frame_id), observer); | |
683 } | 653 } |
684 | 654 |
685 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, | 655 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, |
686 int render_frame_id) { | 656 int render_frame_id) { |
687 frame_manager_->RemoveDelegateObserver( | 657 PresentationServiceDelegateBaseImpl::RemoveObserver(render_process_id, |
688 RenderFrameHostId(render_process_id, render_frame_id)); | 658 render_frame_id); |
689 } | 659 } |
690 | 660 |
691 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( | 661 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( |
692 int render_process_id, | 662 int render_process_id, |
693 int render_frame_id, | 663 int render_frame_id, |
694 content::PresentationScreenAvailabilityListener* listener) { | 664 content::PresentationScreenAvailabilityListener* listener) { |
695 DCHECK(listener); | 665 DCHECK(listener); |
696 return frame_manager_->SetScreenAvailabilityListener( | 666 return frame_manager_->SetScreenAvailabilityListener( |
697 RenderFrameHostId(render_process_id, render_frame_id), listener); | 667 RenderFrameHostId(render_process_id, render_frame_id), listener); |
698 } | 668 } |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 int render_process_id, | 883 int render_process_id, |
914 int render_frame_id, | 884 int render_frame_id, |
915 const content::PresentationSessionInfo& connection, | 885 const content::PresentationSessionInfo& connection, |
916 const content::PresentationConnectionStateChangedCallback& | 886 const content::PresentationConnectionStateChangedCallback& |
917 state_changed_cb) { | 887 state_changed_cb) { |
918 frame_manager_->ListenForConnectionStateChange( | 888 frame_manager_->ListenForConnectionStateChange( |
919 RenderFrameHostId(render_process_id, render_frame_id), connection, | 889 RenderFrameHostId(render_process_id, render_frame_id), connection, |
920 state_changed_cb); | 890 state_changed_cb); |
921 } | 891 } |
922 | 892 |
| 893 void PresentationServiceDelegateImpl::ConnectToOffscreenPresentation( |
| 894 int render_process_id, |
| 895 int render_frame_id, |
| 896 const content::PresentationSessionInfo& session, |
| 897 content::PresentationConnectionPtr connection) { |
| 898 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 899 auto* const offscreen_presentation_manager = |
| 900 OffscreenPresentationManagerFactory:: |
| 901 GetOrCreateForControllerBrowserContext(web_contents_); |
| 902 offscreen_presentation_manager->RegisterOffscreenPresentationController( |
| 903 session.presentation_id, session.presentation_url, render_frame_host_id, |
| 904 std::move(connection)); |
| 905 } |
| 906 |
923 void PresentationServiceDelegateImpl::OnRouteResponse( | 907 void PresentationServiceDelegateImpl::OnRouteResponse( |
924 const PresentationRequest& presentation_request, | 908 const PresentationRequest& presentation_request, |
925 const RouteRequestResult& result) { | 909 const RouteRequestResult& result) { |
926 if (!result.route()) | 910 if (!result.route()) |
927 return; | 911 return; |
928 | 912 |
929 content::PresentationSessionInfo session_info( | 913 content::PresentationSessionInfo session_info( |
930 presentation_request.presentation_url(), result.presentation_id(), | 914 presentation_request.presentation_url(), result.presentation_id(), |
931 result.route()->is_offscreen_presentation()); | 915 result.route()->is_offscreen_presentation()); |
932 frame_manager_->OnDefaultPresentationSessionStarted( | 916 frame_manager_->OnDefaultPresentationSessionStarted( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
967 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 951 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
968 int render_process_id, | 952 int render_process_id, |
969 int render_frame_id, | 953 int render_frame_id, |
970 const MediaSource::Id& source_id) const { | 954 const MediaSource::Id& source_id) const { |
971 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 955 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
972 return frame_manager_->HasScreenAvailabilityListenerForTest( | 956 return frame_manager_->HasScreenAvailabilityListenerForTest( |
973 render_frame_host_id, source_id); | 957 render_frame_host_id, source_id); |
974 } | 958 } |
975 | 959 |
976 } // namespace media_router | 960 } // namespace media_router |
OLD | NEW |