| 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 <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // * PresentationFrameManager AddDelegateObserver | 65 // * PresentationFrameManager AddDelegateObserver |
| 66 // * Reset 0+ times. | 66 // * Reset 0+ times. |
| 67 // * PresentationFrameManager.RemoveDelegateObserver. | 67 // * PresentationFrameManager.RemoveDelegateObserver. |
| 68 class PresentationFrame { | 68 class PresentationFrame { |
| 69 public: | 69 public: |
| 70 PresentationFrame(content::WebContents* web_contents, MediaRouter* router); | 70 PresentationFrame(content::WebContents* web_contents, MediaRouter* router); |
| 71 ~PresentationFrame(); | 71 ~PresentationFrame(); |
| 72 | 72 |
| 73 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 73 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 74 bool SetScreenAvailabilityListener( | 74 bool SetScreenAvailabilityListener( |
| 75 content::PresentationScreenAvailabilityListener* listener); | 75 content::PresentationScreenAvailabilityListener* listener, |
| 76 const GURL& origin); |
| 76 bool RemoveScreenAvailabilityListener( | 77 bool RemoveScreenAvailabilityListener( |
| 77 content::PresentationScreenAvailabilityListener* listener); | 78 content::PresentationScreenAvailabilityListener* listener, |
| 79 const GURL& origin); |
| 78 bool HasScreenAvailabilityListenerForTest( | 80 bool HasScreenAvailabilityListenerForTest( |
| 79 const MediaSource::Id& source_id) const; | 81 const MediaSource& source) const; |
| 80 std::string GetDefaultPresentationId() const; | 82 std::string GetDefaultPresentationId() const; |
| 81 void ListenForConnectionStateChange( | 83 void ListenForConnectionStateChange( |
| 82 const content::PresentationSessionInfo& connection, | 84 const content::PresentationSessionInfo& connection, |
| 83 const content::PresentationConnectionStateChangedCallback& | 85 const content::PresentationConnectionStateChangedCallback& |
| 84 state_changed_cb); | 86 state_changed_cb); |
| 85 void ListenForSessionMessages( | 87 void ListenForSessionMessages( |
| 86 const content::PresentationSessionInfo& session, | 88 const content::PresentationSessionInfo& session, |
| 87 const content::PresentationSessionMessageCallback& message_cb); | 89 const content::PresentationSessionMessageCallback& message_cb); |
| 88 | 90 |
| 89 void Reset(); | 91 void Reset(); |
| 90 void RemoveConnection(const std::string& presentation_id, | 92 void RemoveConnection(const std::string& presentation_id, |
| 91 const MediaRoute::Id& route_id); | 93 const MediaRoute::Id& route_id); |
| 92 | 94 |
| 93 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 95 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| 94 const std::vector<MediaRoute::Id> GetRouteIds() const; | 96 const std::vector<MediaRoute::Id> GetRouteIds() const; |
| 95 | 97 |
| 96 void OnPresentationSessionStarted( | 98 void OnPresentationSessionStarted( |
| 97 const content::PresentationSessionInfo& session, | 99 const content::PresentationSessionInfo& session, |
| 98 const MediaRoute::Id& route_id); | 100 const MediaRoute::Id& route_id); |
| 99 void OnPresentationServiceDelegateDestroyed() const; | 101 void OnPresentationServiceDelegateDestroyed() const; |
| 100 | 102 |
| 101 void set_delegate_observer(DelegateObserver* observer) { | 103 void set_delegate_observer(DelegateObserver* observer) { |
| 102 delegate_observer_ = observer; | 104 delegate_observer_ = observer; |
| 103 } | 105 } |
| 104 | 106 |
| 105 private: | 107 private: |
| 106 MediaSource GetMediaSourceFromListener( | 108 MediaSource GetMediaSourceFromListener( |
| 107 content::PresentationScreenAvailabilityListener* listener) const; | 109 content::PresentationScreenAvailabilityListener* listener, |
| 110 const GURL& origin) const; |
| 108 base::SmallMap<std::map<std::string, MediaRoute::Id>> | 111 base::SmallMap<std::map<std::string, MediaRoute::Id>> |
| 109 presentation_id_to_route_id_; | 112 presentation_id_to_route_id_; |
| 110 base::SmallMap< | 113 base::SmallMap< |
| 111 std::map<std::string, scoped_ptr<PresentationMediaSinksObserver>>> | 114 std::map<MediaSource, scoped_ptr<PresentationMediaSinksObserver>>> |
| 112 url_to_sinks_observer_; | 115 media_source_to_sinks_observer_; |
| 113 base::ScopedPtrHashMap<MediaRoute::Id, | 116 base::ScopedPtrHashMap<MediaRoute::Id, |
| 114 scoped_ptr<PresentationConnectionStateSubscription>> | 117 scoped_ptr<PresentationConnectionStateSubscription>> |
| 115 connection_state_subscriptions_; | 118 connection_state_subscriptions_; |
| 116 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; | 119 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; |
| 117 | 120 |
| 118 // References to the owning WebContents, and the corresponding MediaRouter. | 121 // References to the owning WebContents, and the corresponding MediaRouter. |
| 119 const content::WebContents* web_contents_; | 122 const content::WebContents* web_contents_; |
| 120 MediaRouter* router_; | 123 MediaRouter* router_; |
| 121 | 124 |
| 122 DelegateObserver* delegate_observer_; | 125 DelegateObserver* delegate_observer_; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 152 } | 155 } |
| 153 | 156 |
| 154 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { | 157 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { |
| 155 std::vector<MediaRoute::Id> route_ids; | 158 std::vector<MediaRoute::Id> route_ids; |
| 156 for (const auto& e : presentation_id_to_route_id_) | 159 for (const auto& e : presentation_id_to_route_id_) |
| 157 route_ids.push_back(e.second); | 160 route_ids.push_back(e.second); |
| 158 return route_ids; | 161 return route_ids; |
| 159 } | 162 } |
| 160 | 163 |
| 161 bool PresentationFrame::SetScreenAvailabilityListener( | 164 bool PresentationFrame::SetScreenAvailabilityListener( |
| 162 content::PresentationScreenAvailabilityListener* listener) { | 165 content::PresentationScreenAvailabilityListener* listener, |
| 163 MediaSource source(GetMediaSourceFromListener(listener)); | 166 const GURL& origin) { |
| 164 auto& sinks_observer = url_to_sinks_observer_[source.id()]; | 167 MediaSource source(GetMediaSourceFromListener(listener, origin)); |
| 168 auto& sinks_observer = media_source_to_sinks_observer_[source]; |
| 165 if (sinks_observer && sinks_observer->listener() == listener) | 169 if (sinks_observer && sinks_observer->listener() == listener) |
| 166 return false; | 170 return false; |
| 167 | 171 |
| 168 sinks_observer.reset( | 172 sinks_observer.reset( |
| 169 new PresentationMediaSinksObserver(router_, listener, source)); | 173 new PresentationMediaSinksObserver(router_, listener, source)); |
| 170 | 174 |
| 171 if (!sinks_observer->Init()) { | 175 if (!sinks_observer->Init()) { |
| 172 url_to_sinks_observer_.erase(source.id()); | 176 media_source_to_sinks_observer_.erase(source); |
| 173 listener->OnScreenAvailabilityNotSupported(); | 177 listener->OnScreenAvailabilityNotSupported(); |
| 174 return false; | 178 return false; |
| 175 } | 179 } |
| 176 | 180 |
| 177 return true; | 181 return true; |
| 178 } | 182 } |
| 179 | 183 |
| 180 bool PresentationFrame::RemoveScreenAvailabilityListener( | 184 bool PresentationFrame::RemoveScreenAvailabilityListener( |
| 181 content::PresentationScreenAvailabilityListener* listener) { | 185 content::PresentationScreenAvailabilityListener* listener, |
| 182 MediaSource source(GetMediaSourceFromListener(listener)); | 186 const GURL& origin) { |
| 183 auto sinks_observer_it = url_to_sinks_observer_.find(source.id()); | 187 MediaSource source(GetMediaSourceFromListener(listener, origin)); |
| 184 if (sinks_observer_it != url_to_sinks_observer_.end() && | 188 auto sinks_observer_it = media_source_to_sinks_observer_.find(source); |
| 189 if (sinks_observer_it != media_source_to_sinks_observer_.end() && |
| 185 sinks_observer_it->second->listener() == listener) { | 190 sinks_observer_it->second->listener() == listener) { |
| 186 url_to_sinks_observer_.erase(sinks_observer_it); | 191 media_source_to_sinks_observer_.erase(sinks_observer_it); |
| 187 return true; | 192 return true; |
| 188 } | 193 } |
| 189 return false; | 194 return false; |
| 190 } | 195 } |
| 191 | 196 |
| 192 bool PresentationFrame::HasScreenAvailabilityListenerForTest( | 197 bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
| 193 const MediaSource::Id& source_id) const { | 198 const MediaSource& source) const { |
| 194 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); | 199 return media_source_to_sinks_observer_.find(source) != |
| 200 media_source_to_sinks_observer_.end(); |
| 195 } | 201 } |
| 196 | 202 |
| 197 void PresentationFrame::Reset() { | 203 void PresentationFrame::Reset() { |
| 198 for (const auto& pid_route_id : presentation_id_to_route_id_) | 204 for (const auto& pid_route_id : presentation_id_to_route_id_) |
| 199 router_->DetachRoute(pid_route_id.second); | 205 router_->DetachRoute(pid_route_id.second); |
| 200 | 206 |
| 201 presentation_id_to_route_id_.clear(); | 207 presentation_id_to_route_id_.clear(); |
| 202 url_to_sinks_observer_.clear(); | 208 media_source_to_sinks_observer_.clear(); |
| 203 connection_state_subscriptions_.clear(); | 209 connection_state_subscriptions_.clear(); |
| 204 session_messages_observers_.clear(); | 210 session_messages_observers_.clear(); |
| 205 } | 211 } |
| 206 | 212 |
| 207 void PresentationFrame::RemoveConnection(const std::string& presentation_id, | 213 void PresentationFrame::RemoveConnection(const std::string& presentation_id, |
| 208 const MediaRoute::Id& route_id) { | 214 const MediaRoute::Id& route_id) { |
| 209 // Remove the presentation id mapping so a later call to Reset is a no-op. | 215 // Remove the presentation id mapping so a later call to Reset is a no-op. |
| 210 presentation_id_to_route_id_.erase(presentation_id); | 216 presentation_id_to_route_id_.erase(presentation_id); |
| 211 | 217 |
| 212 // We no longer need to observe route messages. | 218 // We no longer need to observe route messages. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 DVLOG(2) << "ListenForSessionMessages: no route for " | 260 DVLOG(2) << "ListenForSessionMessages: no route for " |
| 255 << session.presentation_id; | 261 << session.presentation_id; |
| 256 return; | 262 return; |
| 257 } | 263 } |
| 258 | 264 |
| 259 session_messages_observers_.push_back( | 265 session_messages_observers_.push_back( |
| 260 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); | 266 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); |
| 261 } | 267 } |
| 262 | 268 |
| 263 MediaSource PresentationFrame::GetMediaSourceFromListener( | 269 MediaSource PresentationFrame::GetMediaSourceFromListener( |
| 264 content::PresentationScreenAvailabilityListener* listener) const { | 270 content::PresentationScreenAvailabilityListener* listener, |
| 271 const GURL& origin) const { |
| 265 // If the default presentation URL is empty then fall back to tab mirroring. | 272 // If the default presentation URL is empty then fall back to tab mirroring. |
| 266 std::string availability_url(listener->GetAvailabilityUrl()); | 273 std::string availability_url(listener->GetAvailabilityUrl()); |
| 267 return availability_url.empty() | 274 return availability_url.empty() |
| 268 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) | 275 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) |
| 269 : MediaSourceForPresentationUrl(availability_url); | 276 : MediaSourceForPresentationUrl(availability_url, origin.spec()); |
| 270 } | 277 } |
| 271 | 278 |
| 272 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. | 279 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. |
| 273 class PresentationFrameManager { | 280 class PresentationFrameManager { |
| 274 public: | 281 public: |
| 275 PresentationFrameManager(content::WebContents* web_contents, | 282 PresentationFrameManager(content::WebContents* web_contents, |
| 276 MediaRouter* router); | 283 MediaRouter* router); |
| 277 ~PresentationFrameManager(); | 284 ~PresentationFrameManager(); |
| 278 | 285 |
| 279 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 286 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 431 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 425 return presentation_frame ? presentation_frame->GetRouteIds() | 432 return presentation_frame ? presentation_frame->GetRouteIds() |
| 426 : std::vector<MediaRoute::Id>(); | 433 : std::vector<MediaRoute::Id>(); |
| 427 } | 434 } |
| 428 | 435 |
| 429 bool PresentationFrameManager::SetScreenAvailabilityListener( | 436 bool PresentationFrameManager::SetScreenAvailabilityListener( |
| 430 const RenderFrameHostId& render_frame_host_id, | 437 const RenderFrameHostId& render_frame_host_id, |
| 431 content::PresentationScreenAvailabilityListener* listener) { | 438 content::PresentationScreenAvailabilityListener* listener) { |
| 432 DCHECK(listener); | 439 DCHECK(listener); |
| 433 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 440 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 434 return presentation_frame->SetScreenAvailabilityListener(listener); | 441 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 442 return presentation_frame->SetScreenAvailabilityListener(listener, frame_url); |
| 435 } | 443 } |
| 436 | 444 |
| 437 bool PresentationFrameManager::RemoveScreenAvailabilityListener( | 445 bool PresentationFrameManager::RemoveScreenAvailabilityListener( |
| 438 const RenderFrameHostId& render_frame_host_id, | 446 const RenderFrameHostId& render_frame_host_id, |
| 439 content::PresentationScreenAvailabilityListener* listener) { | 447 content::PresentationScreenAvailabilityListener* listener) { |
| 440 DCHECK(listener); | 448 DCHECK(listener); |
| 441 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 449 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 450 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 442 return presentation_frame && | 451 return presentation_frame && |
| 443 presentation_frame->RemoveScreenAvailabilityListener(listener); | 452 presentation_frame->RemoveScreenAvailabilityListener(listener, |
| 453 frame_url); |
| 444 } | 454 } |
| 445 | 455 |
| 446 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( | 456 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( |
| 447 const RenderFrameHostId& render_frame_host_id, | 457 const RenderFrameHostId& render_frame_host_id, |
| 448 const MediaSource::Id& source_id) const { | 458 const MediaSource::Id& source_id) const { |
| 449 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 459 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 460 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| 461 MediaSource source(source_id); |
| 462 source.set_origin(frame_url.spec()); |
| 450 return presentation_frame && | 463 return presentation_frame && |
| 451 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); | 464 presentation_frame->HasScreenAvailabilityListenerForTest(source); |
| 452 } | 465 } |
| 453 | 466 |
| 454 void PresentationFrameManager::ListenForConnectionStateChange( | 467 void PresentationFrameManager::ListenForConnectionStateChange( |
| 455 const RenderFrameHostId& render_frame_host_id, | 468 const RenderFrameHostId& render_frame_host_id, |
| 456 const content::PresentationSessionInfo& connection, | 469 const content::PresentationSessionInfo& connection, |
| 457 const content::PresentationConnectionStateChangedCallback& | 470 const content::PresentationConnectionStateChangedCallback& |
| 458 state_changed_cb) { | 471 state_changed_cb) { |
| 459 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 472 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 460 if (presentation_frame) { | 473 if (presentation_frame) { |
| 461 presentation_frame->ListenForConnectionStateChange(connection, | 474 presentation_frame->ListenForConnectionStateChange(connection, |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 const std::string& presentation_id, | 754 const std::string& presentation_id, |
| 742 const content::PresentationSessionStartedCallback& success_cb, | 755 const content::PresentationSessionStartedCallback& success_cb, |
| 743 const content::PresentationSessionErrorCallback& error_cb) { | 756 const content::PresentationSessionErrorCallback& error_cb) { |
| 744 bool off_the_record = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 757 bool off_the_record = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| 745 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 758 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 746 route_response_callbacks.push_back(base::Bind( | 759 route_response_callbacks.push_back(base::Bind( |
| 747 &PresentationServiceDelegateImpl::OnJoinRouteResponse, | 760 &PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| 748 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 761 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| 749 content::PresentationSessionInfo(presentation_url, presentation_id), | 762 content::PresentationSessionInfo(presentation_url, presentation_id), |
| 750 success_cb, error_cb)); | 763 success_cb, error_cb)); |
| 751 router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), | 764 GURL origin(GetLastCommittedURLForFrame( |
| 752 presentation_id, | 765 RenderFrameHostId(render_process_id, render_frame_id)) |
| 753 GetLastCommittedURLForFrame( | 766 .GetOrigin()); |
| 754 RenderFrameHostId(render_process_id, render_frame_id)) | 767 router_->JoinRoute( |
| 755 .GetOrigin(), | 768 MediaSourceForPresentationUrl(presentation_url, origin.spec()).id(), |
| 756 web_contents_, route_response_callbacks, base::TimeDelta(), | 769 presentation_id, origin, web_contents_, route_response_callbacks, |
| 757 off_the_record); | 770 base::TimeDelta(), off_the_record); |
| 758 } | 771 } |
| 759 | 772 |
| 760 void PresentationServiceDelegateImpl::CloseConnection( | 773 void PresentationServiceDelegateImpl::CloseConnection( |
| 761 int render_process_id, | 774 int render_process_id, |
| 762 int render_frame_id, | 775 int render_frame_id, |
| 763 const std::string& presentation_id) { | 776 const std::string& presentation_id) { |
| 764 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 777 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 765 const MediaRoute::Id& route_id = | 778 const MediaRoute::Id& route_id = |
| 766 frame_manager_->GetRouteId(rfh_id, presentation_id); | 779 frame_manager_->GetRouteId(rfh_id, presentation_id); |
| 767 if (route_id.empty()) { | 780 if (route_id.empty()) { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 894 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 882 int render_process_id, | 895 int render_process_id, |
| 883 int render_frame_id, | 896 int render_frame_id, |
| 884 const MediaSource::Id& source_id) const { | 897 const MediaSource::Id& source_id) const { |
| 885 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 898 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 886 return frame_manager_->HasScreenAvailabilityListenerForTest( | 899 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 887 render_frame_host_id, source_id); | 900 render_frame_host_id, source_id); |
| 888 } | 901 } |
| 889 | 902 |
| 890 } // namespace media_router | 903 } // namespace media_router |
| OLD | NEW |