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 | 8 |
9 #include "base/containers/scoped_ptr_hash_map.h" | 9 #include "base/containers/scoped_ptr_hash_map.h" |
10 #include "base/containers/small_map.h" | 10 #include "base/containers/small_map.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 std::string GetDefaultPresentationId() const; | 76 std::string GetDefaultPresentationId() const; |
77 void ListenForConnectionStateChange( | 77 void ListenForConnectionStateChange( |
78 const content::PresentationSessionInfo& connection, | 78 const content::PresentationSessionInfo& connection, |
79 const content::PresentationConnectionStateChangedCallback& | 79 const content::PresentationConnectionStateChangedCallback& |
80 state_changed_cb); | 80 state_changed_cb); |
81 void ListenForSessionMessages( | 81 void ListenForSessionMessages( |
82 const content::PresentationSessionInfo& session, | 82 const content::PresentationSessionInfo& session, |
83 const content::PresentationSessionMessageCallback& message_cb); | 83 const content::PresentationSessionMessageCallback& message_cb); |
84 | 84 |
85 void Reset(); | 85 void Reset(); |
86 void RemoveConnection(const std::string& presentation_id, | |
87 const MediaRoute::Id& route_id); | |
86 | 88 |
87 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 89 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
88 const std::vector<MediaRoute::Id> GetRouteIds() const; | 90 const std::vector<MediaRoute::Id> GetRouteIds() const; |
89 | 91 |
90 void OnPresentationSessionStarted( | 92 void OnPresentationSessionStarted( |
91 const content::PresentationSessionInfo& session, | 93 const content::PresentationSessionInfo& session, |
92 const MediaRoute::Id& route_id); | 94 const MediaRoute::Id& route_id); |
93 void OnPresentationServiceDelegateDestroyed() const; | 95 void OnPresentationServiceDelegateDestroyed() const; |
94 | 96 |
95 void set_delegate_observer(DelegateObserver* observer) { | 97 void set_delegate_observer(DelegateObserver* observer) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 return false; | 179 return false; |
178 } | 180 } |
179 | 181 |
180 bool PresentationFrame::HasScreenAvailabilityListenerForTest( | 182 bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
181 const MediaSource::Id& source_id) const { | 183 const MediaSource::Id& source_id) const { |
182 return sinks_observer_ && sinks_observer_->source().id() == source_id; | 184 return sinks_observer_ && sinks_observer_->source().id() == source_id; |
183 } | 185 } |
184 | 186 |
185 void PresentationFrame::Reset() { | 187 void PresentationFrame::Reset() { |
186 for (const auto& pid_route_id : presentation_id_to_route_id_) | 188 for (const auto& pid_route_id : presentation_id_to_route_id_) |
187 router_->OnPresentationSessionDetached(pid_route_id.second); | 189 router_->DetachRoute(pid_route_id.second); |
188 | 190 |
189 presentation_id_to_route_id_.clear(); | 191 presentation_id_to_route_id_.clear(); |
190 sinks_observer_.reset(); | 192 sinks_observer_.reset(); |
191 connection_state_subscriptions_.clear(); | 193 connection_state_subscriptions_.clear(); |
192 session_messages_observers_.clear(); | 194 session_messages_observers_.clear(); |
193 } | 195 } |
194 | 196 |
197 void PresentationFrame::RemoveConnection(const std::string& presentation_id, | |
198 const MediaRoute::Id& route_id) { | |
199 // Remove the presentation id mapping so a later call to Reset is a no-op. | |
200 presentation_id_to_route_id_.erase(presentation_id); | |
201 // We no longer need to observe route messages. | |
imcheng
2015/12/10 19:50:46
nit: new line above comment
mark a. foltz
2015/12/10 23:46:49
Done
| |
202 auto observer_iter = std::find_if( | |
203 session_messages_observers_.begin(), session_messages_observers_.end(), | |
204 [&route_id](const PresentationSessionMessagesObserver* observer) { | |
205 return route_id == observer->route_id(); | |
206 }); | |
207 if (observer_iter != session_messages_observers_.end()) | |
208 session_messages_observers_.erase(observer_iter); | |
209 // We keep the PresentationConnectionStateChangedCallback registered with MR | |
imcheng
2015/12/10 19:50:46
nit: new line above comment
mark a. foltz
2015/12/10 23:46:49
Done
| |
210 // so the MRP can tell us when terminate() completed. | |
211 router_->DetachRoute(route_id); | |
imcheng
2015/12/10 19:50:46
Looks like you are already calling DetachRoute in
mark a. foltz
2015/12/10 23:46:49
You're right, good catch.
| |
212 } | |
213 | |
195 void PresentationFrame::ListenForConnectionStateChange( | 214 void PresentationFrame::ListenForConnectionStateChange( |
196 const content::PresentationSessionInfo& connection, | 215 const content::PresentationSessionInfo& connection, |
197 const content::PresentationConnectionStateChangedCallback& | 216 const content::PresentationConnectionStateChangedCallback& |
198 state_changed_cb) { | 217 state_changed_cb) { |
199 auto it = presentation_id_to_route_id_.find(connection.presentation_id); | 218 auto it = presentation_id_to_route_id_.find(connection.presentation_id); |
200 if (it == presentation_id_to_route_id_.end()) { | 219 if (it == presentation_id_to_route_id_.end()) { |
201 DLOG(ERROR) << __FUNCTION__ << "route id not found for presentation: " | 220 DLOG(ERROR) << __FUNCTION__ << "route id not found for presentation: " |
202 << connection.presentation_id; | 221 << connection.presentation_id; |
203 return; | 222 return; |
204 } | 223 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 292 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
274 DelegateObserver* observer); | 293 DelegateObserver* observer); |
275 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 294 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
276 void AddDefaultPresentationRequestObserver( | 295 void AddDefaultPresentationRequestObserver( |
277 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 296 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
278 observer); | 297 observer); |
279 void RemoveDefaultPresentationRequestObserver( | 298 void RemoveDefaultPresentationRequestObserver( |
280 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 299 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
281 observer); | 300 observer); |
282 void Reset(const RenderFrameHostId& render_frame_host_id); | 301 void Reset(const RenderFrameHostId& render_frame_host_id); |
302 void RemoveConnection(const RenderFrameHostId& render_frame_host_id, | |
303 const MediaRoute::Id& route_id, | |
304 const std::string& presentation_id); | |
283 bool HasScreenAvailabilityListenerForTest( | 305 bool HasScreenAvailabilityListenerForTest( |
284 const RenderFrameHostId& render_frame_host_id, | 306 const RenderFrameHostId& render_frame_host_id, |
285 const MediaSource::Id& source_id) const; | 307 const MediaSource::Id& source_id) const; |
286 void SetMediaRouterForTest(MediaRouter* router); | 308 void SetMediaRouterForTest(MediaRouter* router); |
287 | 309 |
288 void OnPresentationSessionStarted( | 310 void OnPresentationSessionStarted( |
289 const RenderFrameHostId& render_frame_host_id, | 311 const RenderFrameHostId& render_frame_host_id, |
290 const content::PresentationSessionInfo& session, | 312 const content::PresentationSessionInfo& session, |
291 const MediaRoute::Id& route_id); | 313 const MediaRoute::Id& route_id); |
292 void OnDefaultPresentationSessionStarted( | 314 void OnDefaultPresentationSessionStarted( |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 if (presentation_frame) | 520 if (presentation_frame) |
499 presentation_frame->Reset(); | 521 presentation_frame->Reset(); |
500 | 522 |
501 if (default_presentation_request_ && | 523 if (default_presentation_request_ && |
502 render_frame_host_id == | 524 render_frame_host_id == |
503 default_presentation_request_->render_frame_host_id()) { | 525 default_presentation_request_->render_frame_host_id()) { |
504 ClearDefaultPresentationRequest(); | 526 ClearDefaultPresentationRequest(); |
505 } | 527 } |
506 } | 528 } |
507 | 529 |
530 void PresentationFrameManager::RemoveConnection( | |
531 const RenderFrameHostId& render_frame_host_id, | |
532 const MediaRoute::Id& route_id, | |
533 const std::string& presentation_id) { | |
534 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | |
535 if (presentation_frame) | |
mlamouri (slow - plz ping)
2015/12/10 15:39:51
Is `presentation_frame == nullptr` even possible?
mark a. foltz
2015/12/10 23:46:49
My current understanding is a frame can call conne
| |
536 presentation_frame->RemoveConnection(route_id, presentation_id); | |
537 } | |
538 | |
508 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( | 539 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( |
509 const RenderFrameHostId& render_frame_host_id) { | 540 const RenderFrameHostId& render_frame_host_id) { |
510 if (!presentation_frames_.contains(render_frame_host_id)) { | 541 if (!presentation_frames_.contains(render_frame_host_id)) { |
511 presentation_frames_.add( | 542 presentation_frames_.add( |
512 render_frame_host_id, | 543 render_frame_host_id, |
513 scoped_ptr<PresentationFrame>( | 544 scoped_ptr<PresentationFrame>( |
514 new PresentationFrame(web_contents_, router_))); | 545 new PresentationFrame(web_contents_, router_))); |
515 } | 546 } |
516 return presentation_frames_.get(render_frame_host_id); | 547 return presentation_frames_.get(render_frame_host_id); |
517 } | 548 } |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
708 content::PresentationSessionInfo(presentation_url, presentation_id), | 739 content::PresentationSessionInfo(presentation_url, presentation_id), |
709 success_cb, error_cb)); | 740 success_cb, error_cb)); |
710 router_->JoinRoute( | 741 router_->JoinRoute( |
711 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, | 742 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, |
712 GetLastCommittedURLForFrame( | 743 GetLastCommittedURLForFrame( |
713 RenderFrameHostId(render_process_id, render_frame_id)) | 744 RenderFrameHostId(render_process_id, render_frame_id)) |
714 .GetOrigin(), | 745 .GetOrigin(), |
715 web_contents_, route_response_callbacks); | 746 web_contents_, route_response_callbacks); |
716 } | 747 } |
717 | 748 |
718 void PresentationServiceDelegateImpl::CloseSession( | 749 void PresentationServiceDelegateImpl::CloseConnection( |
719 int render_process_id, | 750 int render_process_id, |
720 int render_frame_id, | 751 int render_frame_id, |
721 const std::string& presentation_id) { | 752 const std::string& presentation_id) { |
722 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 753 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
723 RenderFrameHostId(render_process_id, render_frame_id), presentation_id); | 754 const MediaRoute::Id& route_id = |
755 frame_manager_->GetRouteId(rfh_id, presentation_id); | |
724 if (route_id.empty()) { | 756 if (route_id.empty()) { |
725 DVLOG(1) << "No active route for: " << presentation_id; | 757 DVLOG(1) << "No active route for: " << presentation_id; |
726 return; | 758 return; |
727 } | 759 } |
728 | 760 |
729 // TODO(mfoltz, mlamouri): implement CloseSession(). | 761 router_->DetachRoute(route_id); |
730 // This could call router_->OnPresentationSessionDetached(route_id). | 762 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); |
731 // PresentationFrame::Reset() should probably call CloseSession() too. | 763 // TODO(mfoltz): close() should always succeed so there is no need to keep the |
732 // Rename CloseRoute() to something else to avoid confusion? | 764 // state_changed_cb around - remove it and fire the ChangeEvent on the |
733 NOTIMPLEMENTED(); | 765 // PresentationConnection in Blink. |
734 } | 766 } |
735 | 767 |
736 void PresentationServiceDelegateImpl::TerminateSession( | 768 void PresentationServiceDelegateImpl::Terminate( |
737 int render_process_id, | 769 int render_process_id, |
738 int render_frame_id, | 770 int render_frame_id, |
739 const std::string& presentation_id) { | 771 const std::string& presentation_id) { |
740 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 772 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
741 RenderFrameHostId(render_process_id, render_frame_id), presentation_id); | 773 const MediaRoute::Id& route_id = |
774 frame_manager_->GetRouteId(rfh_id, presentation_id); | |
742 if (route_id.empty()) { | 775 if (route_id.empty()) { |
743 DVLOG(1) << "No active route for: " << presentation_id; | 776 DVLOG(1) << "No active route for: " << presentation_id; |
744 return; | 777 return; |
745 } | 778 } |
746 router_->CloseRoute(route_id); | 779 router_->TerminateRoute(route_id); |
780 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); | |
imcheng
2015/12/10 19:50:46
So we are calling TerminateRoute, and then DetachR
mark a. foltz
2015/12/10 23:46:49
Yeah removed call to router_->DetachRoute() from R
| |
747 } | 781 } |
748 | 782 |
749 void PresentationServiceDelegateImpl::ListenForSessionMessages( | 783 void PresentationServiceDelegateImpl::ListenForSessionMessages( |
750 int render_process_id, | 784 int render_process_id, |
751 int render_frame_id, | 785 int render_frame_id, |
752 const content::PresentationSessionInfo& session, | 786 const content::PresentationSessionInfo& session, |
753 const content::PresentationSessionMessageCallback& message_cb) { | 787 const content::PresentationSessionMessageCallback& message_cb) { |
754 frame_manager_->ListenForSessionMessages( | 788 frame_manager_->ListenForSessionMessages( |
755 RenderFrameHostId(render_process_id, render_frame_id), session, | 789 RenderFrameHostId(render_process_id, render_frame_id), session, |
756 message_cb); | 790 message_cb); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
838 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 872 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
839 int render_process_id, | 873 int render_process_id, |
840 int render_frame_id, | 874 int render_frame_id, |
841 const MediaSource::Id& source_id) const { | 875 const MediaSource::Id& source_id) const { |
842 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 876 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
843 return frame_manager_->HasScreenAvailabilityListenerForTest( | 877 return frame_manager_->HasScreenAvailabilityListenerForTest( |
844 render_frame_host_id, source_id); | 878 render_frame_host_id, source_id); |
845 } | 879 } |
846 | 880 |
847 } // namespace media_router | 881 } // namespace media_router |
OLD | NEW |