| 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 |
| 11 #include "base/containers/scoped_ptr_hash_map.h" | 11 #include "base/containers/scoped_ptr_hash_map.h" |
| 12 #include "base/containers/small_map.h" | 12 #include "base/containers/small_map.h" |
| 13 #include "base/guid.h" | |
| 14 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 16 #include "chrome/browser/media/router/create_presentation_connection_request.h" | 15 #include "chrome/browser/media/router/create_presentation_connection_request.h" |
| 17 #include "chrome/browser/media/router/media_route.h" | 16 #include "chrome/browser/media/router/media_route.h" |
| 18 #include "chrome/browser/media/router/media_router.h" | 17 #include "chrome/browser/media/router/media_router.h" |
| 19 #include "chrome/browser/media/router/media_router_dialog_controller.h" | 18 #include "chrome/browser/media/router/media_router_dialog_controller.h" |
| 20 #include "chrome/browser/media/router/media_router_factory.h" | 19 #include "chrome/browser/media/router/media_router_factory.h" |
| 21 #include "chrome/browser/media/router/media_sink.h" | 20 #include "chrome/browser/media/router/media_sink.h" |
| 22 #include "chrome/browser/media/router/media_source_helper.h" | 21 #include "chrome/browser/media/router/media_source_helper.h" |
| 22 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 23 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h" |
| 23 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" | 24 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" |
| 24 #include "chrome/browser/media/router/presentation_session_messages_observer.h" | 25 #include "chrome/browser/media/router/presentation_session_messages_observer.h" |
| 25 #include "chrome/browser/media/router/route_request_result.h" | 26 #include "chrome/browser/media/router/route_request_result.h" |
| 26 #include "chrome/browser/sessions/session_tab_helper.h" | 27 #include "chrome/browser/sessions/session_tab_helper.h" |
| 27 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
| 28 #include "content/public/browser/presentation_screen_availability_listener.h" | 29 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 29 #include "content/public/browser/presentation_session.h" | 30 #include "content/public/browser/presentation_session.h" |
| 30 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
| 31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
| 32 | 33 |
| 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 34 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 34 media_router::PresentationServiceDelegateImpl); | 35 media_router::PresentationServiceDelegateImpl); |
| 35 | 36 |
| 36 using content::RenderFrameHost; | 37 using content::RenderFrameHost; |
| 37 | 38 |
| 38 namespace media_router { | 39 namespace media_router { |
| 39 | 40 |
| 41 using OffscreenPresentationConnection = |
| 42 OffscreenPresentationManager::OffscreenPresentationConnection; |
| 43 |
| 40 namespace { | 44 namespace { |
| 41 | 45 |
| 42 using DelegateObserver = content::PresentationServiceDelegate::Observer; | 46 using DelegateObserver = content::PresentationServiceDelegate::Observer; |
| 43 | 47 |
| 44 // Returns the unique identifier for the supplied RenderFrameHost. | 48 // Returns the unique identifier for the supplied RenderFrameHost. |
| 45 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { | 49 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { |
| 46 int render_process_id = render_frame_host->GetProcess()->GetID(); | 50 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 47 int render_frame_id = render_frame_host->GetRoutingID(); | 51 int render_frame_id = render_frame_host->GetRoutingID(); |
| 48 return RenderFrameHostId(render_process_id, render_frame_id); | 52 return RenderFrameHostId(render_process_id, render_frame_id); |
| 49 } | 53 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 ~PresentationFrame(); | 76 ~PresentationFrame(); |
| 73 | 77 |
| 74 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 78 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 75 bool SetScreenAvailabilityListener( | 79 bool SetScreenAvailabilityListener( |
| 76 content::PresentationScreenAvailabilityListener* listener); | 80 content::PresentationScreenAvailabilityListener* listener); |
| 77 bool RemoveScreenAvailabilityListener( | 81 bool RemoveScreenAvailabilityListener( |
| 78 content::PresentationScreenAvailabilityListener* listener); | 82 content::PresentationScreenAvailabilityListener* listener); |
| 79 bool HasScreenAvailabilityListenerForTest( | 83 bool HasScreenAvailabilityListenerForTest( |
| 80 const MediaSource::Id& source_id) const; | 84 const MediaSource::Id& source_id) const; |
| 81 std::string GetDefaultPresentationId() const; | 85 std::string GetDefaultPresentationId() const; |
| 86 void SendMessage(const content::PresentationSessionInfo& session, |
| 87 std::unique_ptr<content::PresentationSessionMessage> message, |
| 88 const content::SendMessageCallback send_message_cb); |
| 82 void ListenForConnectionStateChange( | 89 void ListenForConnectionStateChange( |
| 83 const content::PresentationSessionInfo& connection, | 90 const content::PresentationSessionInfo& connection, |
| 84 const content::PresentationConnectionStateChangedCallback& | 91 const content::PresentationConnectionStateChangedCallback& |
| 85 state_changed_cb); | 92 state_changed_cb); |
| 86 void ListenForSessionMessages( | 93 void ListenForSessionMessages( |
| 87 const content::PresentationSessionInfo& session, | 94 const content::PresentationSessionInfo& session, |
| 88 const content::PresentationSessionMessageCallback& message_cb); | 95 const content::PresentationSessionMessageCallback& message_cb); |
| 89 | 96 |
| 90 void Reset(); | 97 void Reset(); |
| 91 void RemoveConnection(const std::string& presentation_id, | 98 void RemoveConnection( |
| 92 const MediaRoute::Id& route_id); | 99 const std::string& presentation_id, |
| 100 const content::PresentationConnectionStateChangeInfo& state_change_info); |
| 93 | 101 |
| 94 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 102 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| 95 const std::vector<MediaRoute::Id> GetRouteIds() const; | 103 const std::vector<MediaRoute::Id> GetRouteIds() const; |
| 96 | 104 |
| 97 void OnPresentationSessionStarted( | 105 void OnPresentationSessionStarted( |
| 98 const content::PresentationSessionInfo& session, | 106 const content::PresentationSessionInfo& session, |
| 99 const MediaRoute::Id& route_id); | 107 const MediaRoute& route); |
| 100 void OnPresentationServiceDelegateDestroyed() const; | 108 void OnPresentationServiceDelegateDestroyed() const; |
| 101 | 109 |
| 102 void set_delegate_observer(DelegateObserver* observer) { | 110 void set_delegate_observer(DelegateObserver* observer) { |
| 103 delegate_observer_ = observer; | 111 delegate_observer_ = observer; |
| 104 } | 112 } |
| 105 | 113 |
| 106 private: | 114 private: |
| 107 MediaSource GetMediaSourceFromListener( | 115 MediaSource GetMediaSourceFromListener( |
| 108 content::PresentationScreenAvailabilityListener* listener) const; | 116 content::PresentationScreenAvailabilityListener* listener) const; |
| 117 // Gets the OffscreenPresentationConnection associated with |presentation_id| |
| 118 // within this frame. Returns nullptr if |presentation_id| is not |
| 119 // registered as an offscreen presentation. |
| 120 OffscreenPresentationConnection* FindOffscreenConnection( |
| 121 const std::string& presentation_id) const; |
| 122 |
| 109 base::SmallMap<std::map<std::string, MediaRoute::Id>> | 123 base::SmallMap<std::map<std::string, MediaRoute::Id>> |
| 110 presentation_id_to_route_id_; | 124 presentation_id_to_route_id_; |
| 111 base::SmallMap< | 125 base::SmallMap< |
| 112 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> | 126 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> |
| 113 url_to_sinks_observer_; | 127 url_to_sinks_observer_; |
| 128 |
| 129 // Controller objects for offscreen presentations. Note that offscreen |
| 130 // presentations are manipulated with these objects instead of the observers |
| 131 // and MediaRouter objects below. Maps from presentation ID to the |
| 132 // corresponding OffscreenPresentationConnection within this frame. |
| 133 std::map<std::string, std::unique_ptr<OffscreenPresentationConnection>> |
| 134 offscreen_presentation_connections_; |
| 135 |
| 114 base::ScopedPtrHashMap< | 136 base::ScopedPtrHashMap< |
| 115 MediaRoute::Id, | 137 MediaRoute::Id, |
| 116 std::unique_ptr<PresentationConnectionStateSubscription>> | 138 std::unique_ptr<PresentationConnectionStateSubscription>> |
| 117 connection_state_subscriptions_; | 139 connection_state_subscriptions_; |
| 118 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; | 140 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; |
| 119 | 141 |
| 120 RenderFrameHostId render_frame_host_id_; | 142 RenderFrameHostId render_frame_host_id_; |
| 121 | 143 |
| 122 // References to the owning WebContents, and the corresponding MediaRouter. | 144 // References to the owning WebContents, the corresponding MediaRouter, and |
| 145 // OffscreenPresentationManager. |
| 123 const content::WebContents* web_contents_; | 146 const content::WebContents* web_contents_; |
| 124 MediaRouter* router_; | 147 MediaRouter* const router_; |
| 148 OffscreenPresentationManager* const offscreen_presentation_manager_; |
| 125 | 149 |
| 126 DelegateObserver* delegate_observer_; | 150 DelegateObserver* delegate_observer_; |
| 127 }; | 151 }; |
| 128 | 152 |
| 129 PresentationFrame::PresentationFrame( | 153 PresentationFrame::PresentationFrame( |
| 130 const RenderFrameHostId& render_frame_host_id, | 154 const RenderFrameHostId& render_frame_host_id, |
| 131 content::WebContents* web_contents, | 155 content::WebContents* web_contents, |
| 132 MediaRouter* router) | 156 MediaRouter* router) |
| 133 : render_frame_host_id_(render_frame_host_id), | 157 : render_frame_host_id_(render_frame_host_id), |
| 134 web_contents_(web_contents), | 158 web_contents_(web_contents), |
| 135 router_(router), | 159 router_(router), |
| 160 offscreen_presentation_manager_( |
| 161 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( |
| 162 web_contents_->GetBrowserContext())), |
| 136 delegate_observer_(nullptr) { | 163 delegate_observer_(nullptr) { |
| 137 DCHECK(web_contents_); | 164 DCHECK(web_contents_); |
| 138 DCHECK(router_); | 165 DCHECK(router_); |
| 166 DCHECK(offscreen_presentation_manager_); |
| 139 } | 167 } |
| 140 | 168 |
| 141 PresentationFrame::~PresentationFrame() { | 169 PresentationFrame::~PresentationFrame() { |
| 142 } | 170 } |
| 143 | 171 |
| 144 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { | 172 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { |
| 145 if (delegate_observer_) | 173 if (delegate_observer_) |
| 146 delegate_observer_->OnDelegateDestroyed(); | 174 delegate_observer_->OnDelegateDestroyed(); |
| 147 } | 175 } |
| 148 | 176 |
| 149 void PresentationFrame::OnPresentationSessionStarted( | 177 void PresentationFrame::OnPresentationSessionStarted( |
| 150 const content::PresentationSessionInfo& session, | 178 const content::PresentationSessionInfo& session, |
| 151 const MediaRoute::Id& route_id) { | 179 const MediaRoute& route) { |
| 152 presentation_id_to_route_id_[session.presentation_id] = route_id; | 180 const std::string& presentation_id = session.presentation_id; |
| 181 presentation_id_to_route_id_[presentation_id] = route.media_route_id(); |
| 182 if (route.is_offscreen_presentation()) { |
| 183 DCHECK(!ContainsKey(offscreen_presentation_connections_, presentation_id)); |
| 184 std::unique_ptr<OffscreenPresentationConnection> offscreen_connection = |
| 185 offscreen_presentation_manager_->ConnectToOffscreenPresentation( |
| 186 presentation_id, render_frame_host_id_); |
| 187 |
| 188 // |offscreen_connection| could be nullptr for a couple of reasons: |
| 189 // 1) An OffscreenPresentationConnection for presentation_id already exists. |
| 190 // This could be due to a race between two or more route requests. |
| 191 // 2) The receiver is already gone, so the route will die soon. This should |
| 192 // happen very rarely since the receiver has to unregister itself between |
| 193 // when offscreen presentation was created and when the resulting route |
| 194 // arrived at MR. |
| 195 if (!offscreen_connection) { |
| 196 // TODO(imcheng): we should probably reject the route request and detach |
| 197 // the route in this case. crbug.com/513859 |
| 198 DLOG(ERROR) << "CreateOffscreenPresentationConnection returned nullptr."; |
| 199 } else { |
| 200 offscreen_presentation_connections_.insert( |
| 201 std::make_pair(presentation_id, std::move(offscreen_connection))); |
| 202 } |
| 203 } |
| 153 } | 204 } |
| 154 | 205 |
| 155 const MediaRoute::Id PresentationFrame::GetRouteId( | 206 const MediaRoute::Id PresentationFrame::GetRouteId( |
| 156 const std::string& presentation_id) const { | 207 const std::string& presentation_id) const { |
| 157 auto it = presentation_id_to_route_id_.find(presentation_id); | 208 auto it = presentation_id_to_route_id_.find(presentation_id); |
| 158 return it != presentation_id_to_route_id_.end() ? it->second : ""; | 209 return it != presentation_id_to_route_id_.end() ? it->second : ""; |
| 159 } | 210 } |
| 160 | 211 |
| 161 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { | |
| 162 std::vector<MediaRoute::Id> route_ids; | |
| 163 for (const auto& e : presentation_id_to_route_id_) | |
| 164 route_ids.push_back(e.second); | |
| 165 return route_ids; | |
| 166 } | |
| 167 | |
| 168 bool PresentationFrame::SetScreenAvailabilityListener( | 212 bool PresentationFrame::SetScreenAvailabilityListener( |
| 169 content::PresentationScreenAvailabilityListener* listener) { | 213 content::PresentationScreenAvailabilityListener* listener) { |
| 170 MediaSource source(GetMediaSourceFromListener(listener)); | 214 MediaSource source(GetMediaSourceFromListener(listener)); |
| 171 auto& sinks_observer = url_to_sinks_observer_[source.id()]; | 215 auto& sinks_observer = url_to_sinks_observer_[source.id()]; |
| 172 if (sinks_observer && sinks_observer->listener() == listener) | 216 if (sinks_observer && sinks_observer->listener() == listener) |
| 173 return false; | 217 return false; |
| 174 | 218 |
| 175 sinks_observer.reset(new PresentationMediaSinksObserver( | 219 sinks_observer.reset(new PresentationMediaSinksObserver( |
| 176 router_, listener, source, | 220 router_, listener, source, |
| 177 GetLastCommittedURLForFrame(render_frame_host_id_).GetOrigin())); | 221 GetLastCommittedURLForFrame(render_frame_host_id_).GetOrigin())); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 198 } | 242 } |
| 199 | 243 |
| 200 bool PresentationFrame::HasScreenAvailabilityListenerForTest( | 244 bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
| 201 const MediaSource::Id& source_id) const { | 245 const MediaSource::Id& source_id) const { |
| 202 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); | 246 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); |
| 203 } | 247 } |
| 204 | 248 |
| 205 void PresentationFrame::Reset() { | 249 void PresentationFrame::Reset() { |
| 206 for (const auto& pid_route_id : presentation_id_to_route_id_) | 250 for (const auto& pid_route_id : presentation_id_to_route_id_) |
| 207 router_->DetachRoute(pid_route_id.second); | 251 router_->DetachRoute(pid_route_id.second); |
| 208 | |
| 209 presentation_id_to_route_id_.clear(); | 252 presentation_id_to_route_id_.clear(); |
| 210 url_to_sinks_observer_.clear(); | 253 url_to_sinks_observer_.clear(); |
| 254 offscreen_presentation_connections_.clear(); |
| 211 connection_state_subscriptions_.clear(); | 255 connection_state_subscriptions_.clear(); |
| 212 session_messages_observers_.clear(); | 256 session_messages_observers_.clear(); |
| 213 } | 257 } |
| 214 | 258 |
| 215 void PresentationFrame::RemoveConnection(const std::string& presentation_id, | 259 void PresentationFrame::SendMessage( |
| 216 const MediaRoute::Id& route_id) { | 260 const content::PresentationSessionInfo& session, |
| 261 std::unique_ptr<content::PresentationSessionMessage> message, |
| 262 const content::SendMessageCallback send_message_cb) { |
| 263 OffscreenPresentationConnection* offscreen_connection = |
| 264 FindOffscreenConnection(session.presentation_id); |
| 265 if (offscreen_connection) { |
| 266 offscreen_connection->SendMessage(std::move(message), send_message_cb); |
| 267 } else { |
| 268 auto it = presentation_id_to_route_id_.find(session.presentation_id); |
| 269 if (it == presentation_id_to_route_id_.end()) { |
| 270 DVLOG(2) << "ListenForSessionMessages: no route for " |
| 271 << session.presentation_id; |
| 272 return; |
| 273 } |
| 274 if (message->is_binary()) { |
| 275 router_->SendRouteBinaryMessage(it->second, std::move(message->data), |
| 276 send_message_cb); |
| 277 } else { |
| 278 router_->SendRouteMessage(it->second, message->message, send_message_cb); |
| 279 } |
| 280 } |
| 281 } |
| 282 |
| 283 OffscreenPresentationConnection* PresentationFrame::FindOffscreenConnection( |
| 284 const std::string& presentation_id) const { |
| 285 auto offscreen_connection_it = |
| 286 offscreen_presentation_connections_.find(presentation_id); |
| 287 return offscreen_connection_it != offscreen_presentation_connections_.end() |
| 288 ? offscreen_connection_it->second.get() |
| 289 : nullptr; |
| 290 } |
| 291 |
| 292 void PresentationFrame::RemoveConnection( |
| 293 const std::string& presentation_id, |
| 294 const content::PresentationConnectionStateChangeInfo& state_change_info) { |
| 295 auto it = presentation_id_to_route_id_.find(presentation_id); |
| 296 if (it == presentation_id_to_route_id_.end()) |
| 297 return; |
| 298 |
| 217 // Remove the presentation id mapping so a later call to Reset is a no-op. | 299 // Remove the presentation id mapping so a later call to Reset is a no-op. |
| 218 presentation_id_to_route_id_.erase(presentation_id); | 300 MediaRoute::Id route_id = it->second; |
| 301 presentation_id_to_route_id_.erase(it); |
| 219 | 302 |
| 303 if (OffscreenPresentationConnection* offscreen_connection = |
| 304 FindOffscreenConnection(presentation_id)) { |
| 305 offscreen_connection->RemoveFromPresentation(state_change_info); |
| 306 offscreen_presentation_connections_.erase(presentation_id); |
| 307 return; |
| 308 } |
| 309 |
| 310 // Not an offscreen presentation. |
| 220 // We no longer need to observe route messages. | 311 // We no longer need to observe route messages. |
| 221 auto observer_iter = std::find_if( | 312 auto observer_iter = std::find_if( |
| 222 session_messages_observers_.begin(), session_messages_observers_.end(), | 313 session_messages_observers_.begin(), session_messages_observers_.end(), |
| 223 [&route_id](const PresentationSessionMessagesObserver* observer) { | 314 [&route_id](const PresentationSessionMessagesObserver* observer) { |
| 224 return route_id == observer->route_id(); | 315 return route_id == observer->route_id(); |
| 225 }); | 316 }); |
| 226 if (observer_iter != session_messages_observers_.end()) | 317 if (observer_iter != session_messages_observers_.end()) |
| 227 session_messages_observers_.erase(observer_iter); | 318 session_messages_observers_.erase(observer_iter); |
| 228 | 319 |
| 229 // We keep the PresentationConnectionStateChangedCallback registered with MR | 320 // We keep the PresentationConnectionStateChangedCallback registered with MR |
| 230 // so the MRP can tell us when terminate() completed. | 321 // so the MRP can tell us when terminate() completed. |
| 231 } | 322 } |
| 232 | 323 |
| 233 void PresentationFrame::ListenForConnectionStateChange( | 324 void PresentationFrame::ListenForConnectionStateChange( |
| 234 const content::PresentationSessionInfo& connection, | 325 const content::PresentationSessionInfo& connection, |
| 235 const content::PresentationConnectionStateChangedCallback& | 326 const content::PresentationConnectionStateChangedCallback& |
| 236 state_changed_cb) { | 327 state_changed_cb) { |
| 237 auto it = presentation_id_to_route_id_.find(connection.presentation_id); | 328 auto it = presentation_id_to_route_id_.find(connection.presentation_id); |
| 238 if (it == presentation_id_to_route_id_.end()) { | 329 if (it == presentation_id_to_route_id_.end()) { |
| 239 DLOG(ERROR) << __FUNCTION__ << "route id not found for presentation: " | 330 DLOG(ERROR) << __FUNCTION__ << "route id not found for presentation: " |
| 240 << connection.presentation_id; | 331 << connection.presentation_id; |
| 241 return; | 332 return; |
| 242 } | 333 } |
| 243 | 334 |
| 335 // An offscreen controller presentation listens for state change from both |
| 336 // the receiver and MR. |
| 337 OffscreenPresentationConnection* offscreen_connection = |
| 338 FindOffscreenConnection(connection.presentation_id); |
| 339 if (offscreen_connection) { |
| 340 offscreen_connection->ListenForStateChange(state_changed_cb); |
| 341 } |
| 342 |
| 244 const MediaRoute::Id& route_id = it->second; | 343 const MediaRoute::Id& route_id = it->second; |
| 245 if (connection_state_subscriptions_.contains(route_id)) { | 344 if (connection_state_subscriptions_.contains(route_id)) { |
| 246 DLOG(ERROR) << __FUNCTION__ << "Already listening connection state change " | 345 DLOG(ERROR) << __FUNCTION__ << "Already listening connection state change " |
| 247 "for route: " | 346 "for route: " |
| 248 << route_id; | 347 << route_id; |
| 249 return; | 348 return; |
| 250 } | 349 } |
| 251 | 350 |
| 252 connection_state_subscriptions_.add( | 351 connection_state_subscriptions_.add( |
| 253 route_id, router_->AddPresentationConnectionStateChangedCallback( | 352 route_id, router_->AddPresentationConnectionStateChangedCallback( |
| 254 it->second, state_changed_cb)); | 353 it->second, state_changed_cb)); |
| 255 } | 354 } |
| 256 | 355 |
| 257 void PresentationFrame::ListenForSessionMessages( | 356 void PresentationFrame::ListenForSessionMessages( |
| 258 const content::PresentationSessionInfo& session, | 357 const content::PresentationSessionInfo& session, |
| 259 const content::PresentationSessionMessageCallback& message_cb) { | 358 const content::PresentationSessionMessageCallback& message_cb) { |
| 260 auto it = presentation_id_to_route_id_.find(session.presentation_id); | 359 auto it = presentation_id_to_route_id_.find(session.presentation_id); |
| 261 if (it == presentation_id_to_route_id_.end()) { | 360 if (it == presentation_id_to_route_id_.end()) { |
| 262 DVLOG(2) << "ListenForSessionMessages: no route for " | 361 DVLOG(2) << "ListenForSessionMessages: no route for " |
| 263 << session.presentation_id; | 362 << session.presentation_id; |
| 264 return; | 363 return; |
| 265 } | 364 } |
| 266 | 365 |
| 366 // An offscreen controller presentation listens for messages from both the |
| 367 // receiver and MR. |
| 368 OffscreenPresentationConnection* offscreen_connection = |
| 369 FindOffscreenConnection(session.presentation_id); |
| 370 if (offscreen_connection) { |
| 371 offscreen_connection->ListenForMessages(message_cb); |
| 372 } |
| 373 |
| 374 // TODO(imcheng): Limit to 1 observer per presentation. |
| 267 session_messages_observers_.push_back( | 375 session_messages_observers_.push_back( |
| 268 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); | 376 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); |
| 269 } | 377 } |
| 270 | 378 |
| 271 MediaSource PresentationFrame::GetMediaSourceFromListener( | 379 MediaSource PresentationFrame::GetMediaSourceFromListener( |
| 272 content::PresentationScreenAvailabilityListener* listener) const { | 380 content::PresentationScreenAvailabilityListener* listener) const { |
| 273 // If the default presentation URL is empty then fall back to tab mirroring. | 381 // If the default presentation URL is empty then fall back to tab mirroring. |
| 274 std::string availability_url(listener->GetAvailabilityUrl()); | 382 std::string availability_url(listener->GetAvailabilityUrl()); |
| 275 return availability_url.empty() | 383 return availability_url.empty() |
| 276 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) | 384 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) |
| 277 : MediaSourceForPresentationUrl(availability_url); | 385 : MediaSourceForPresentationUrl(availability_url); |
| 278 } | 386 } |
| 279 | 387 |
| 280 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. | 388 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. |
| 281 class PresentationFrameManager { | 389 class PresentationFrameManager { |
| 282 public: | 390 public: |
| 283 PresentationFrameManager(content::WebContents* web_contents, | 391 PresentationFrameManager(content::WebContents* web_contents, |
| 284 MediaRouter* router); | 392 MediaRouter* router); |
| 285 ~PresentationFrameManager(); | 393 ~PresentationFrameManager(); |
| 286 | 394 |
| 287 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 395 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 288 bool SetScreenAvailabilityListener( | 396 bool SetScreenAvailabilityListener( |
| 289 const RenderFrameHostId& render_frame_host_id, | 397 const RenderFrameHostId& render_frame_host_id, |
| 290 content::PresentationScreenAvailabilityListener* listener); | 398 content::PresentationScreenAvailabilityListener* listener); |
| 291 bool RemoveScreenAvailabilityListener( | 399 bool RemoveScreenAvailabilityListener( |
| 292 const RenderFrameHostId& render_frame_host_id, | 400 const RenderFrameHostId& render_frame_host_id, |
| 293 content::PresentationScreenAvailabilityListener* listener); | 401 content::PresentationScreenAvailabilityListener* listener); |
| 402 void SendMessage(const RenderFrameHostId& render_frame_host_id, |
| 403 const content::PresentationSessionInfo& session, |
| 404 std::unique_ptr<content::PresentationSessionMessage> message, |
| 405 const content::SendMessageCallback send_message_cb); |
| 294 void ListenForConnectionStateChange( | 406 void ListenForConnectionStateChange( |
| 295 const RenderFrameHostId& render_frame_host_id, | 407 const RenderFrameHostId& render_frame_host_id, |
| 296 const content::PresentationSessionInfo& connection, | 408 const content::PresentationSessionInfo& connection, |
| 297 const content::PresentationConnectionStateChangedCallback& | 409 const content::PresentationConnectionStateChangedCallback& |
| 298 state_changed_cb); | 410 state_changed_cb); |
| 299 void ListenForSessionMessages( | 411 void ListenForSessionMessages( |
| 300 const RenderFrameHostId& render_frame_host_id, | 412 const RenderFrameHostId& render_frame_host_id, |
| 301 const content::PresentationSessionInfo& session, | 413 const content::PresentationSessionInfo& session, |
| 302 const content::PresentationSessionMessageCallback& message_cb); | 414 const content::PresentationSessionMessageCallback& message_cb); |
| 303 | 415 |
| 304 // Sets or clears the default presentation request and callback for the given | 416 // Sets or clears the default presentation request and callback for the given |
| 305 // frame. Also sets / clears the default presentation requests for the owning | 417 // frame. Also sets / clears the default presentation requests for the owning |
| 306 // tab WebContents. | 418 // tab WebContents. |
| 307 void SetDefaultPresentationUrl( | 419 void SetDefaultPresentationUrl( |
| 308 const RenderFrameHostId& render_frame_host_id, | 420 const RenderFrameHostId& render_frame_host_id, |
| 309 const std::string& default_presentation_url, | 421 const std::string& default_presentation_url, |
| 310 const content::PresentationSessionStartedCallback& callback); | 422 const content::PresentationSessionStartedCallback& callback); |
| 311 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 423 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| 312 DelegateObserver* observer); | 424 DelegateObserver* observer); |
| 313 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 425 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| 314 void AddDefaultPresentationRequestObserver( | 426 void AddDefaultPresentationRequestObserver( |
| 315 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 427 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 316 observer); | 428 observer); |
| 317 void RemoveDefaultPresentationRequestObserver( | 429 void RemoveDefaultPresentationRequestObserver( |
| 318 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | 430 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| 319 observer); | 431 observer); |
| 320 void Reset(const RenderFrameHostId& render_frame_host_id); | 432 void Reset(const RenderFrameHostId& render_frame_host_id); |
| 321 void RemoveConnection(const RenderFrameHostId& render_frame_host_id, | 433 void RemoveConnection( |
| 322 const MediaRoute::Id& route_id, | 434 const RenderFrameHostId& render_frame_host_id, |
| 323 const std::string& presentation_id); | 435 const std::string& presentation_id, |
| 436 const content::PresentationConnectionStateChangeInfo& state_change_info); |
| 324 bool HasScreenAvailabilityListenerForTest( | 437 bool HasScreenAvailabilityListenerForTest( |
| 325 const RenderFrameHostId& render_frame_host_id, | 438 const RenderFrameHostId& render_frame_host_id, |
| 326 const MediaSource::Id& source_id) const; | 439 const MediaSource::Id& source_id) const; |
| 327 void SetMediaRouterForTest(MediaRouter* router); | 440 void SetMediaRouterForTest(MediaRouter* router); |
| 328 | 441 |
| 329 void OnPresentationSessionStarted( | 442 void OnPresentationSessionStarted( |
| 330 const RenderFrameHostId& render_frame_host_id, | 443 const RenderFrameHostId& render_frame_host_id, |
| 331 const content::PresentationSessionInfo& session, | 444 const content::PresentationSessionInfo& session, |
| 332 const MediaRoute::Id& route_id); | 445 const MediaRoute& route); |
| 333 void OnDefaultPresentationSessionStarted( | 446 void OnDefaultPresentationSessionStarted( |
| 334 const PresentationRequest& request, | 447 const PresentationRequest& request, |
| 335 const content::PresentationSessionInfo& session, | 448 const content::PresentationSessionInfo& session, |
| 336 const MediaRoute::Id& route_id); | 449 const MediaRoute& route); |
| 337 | 450 |
| 338 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, | 451 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, |
| 339 const std::string& presentation_id) const; | 452 const std::string& presentation_id) const; |
| 340 const std::vector<MediaRoute::Id> GetRouteIds( | |
| 341 const RenderFrameHostId& render_frame_host_id) const; | |
| 342 | 453 |
| 343 const PresentationRequest* default_presentation_request() const { | 454 const PresentationRequest* default_presentation_request() const { |
| 344 return default_presentation_request_.get(); | 455 return default_presentation_request_.get(); |
| 345 } | 456 } |
| 346 | 457 |
| 347 private: | 458 private: |
| 348 PresentationFrame* GetOrAddPresentationFrame( | 459 PresentationFrame* GetOrAddPresentationFrame( |
| 349 const RenderFrameHostId& render_frame_host_id); | 460 const RenderFrameHostId& render_frame_host_id); |
| 350 | 461 |
| 351 // Sets the default presentation request for the owning WebContents and | 462 // Sets the default presentation request for the owning WebContents and |
| (...skipping 20 matching lines...) Expand all Loading... |
| 372 content::PresentationSessionStartedCallback | 483 content::PresentationSessionStartedCallback |
| 373 default_presentation_started_callback_; | 484 default_presentation_started_callback_; |
| 374 | 485 |
| 375 // References to the observers listening for changes to this tab WebContent's | 486 // References to the observers listening for changes to this tab WebContent's |
| 376 // default presentation. | 487 // default presentation. |
| 377 base::ObserverList< | 488 base::ObserverList< |
| 378 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver> | 489 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver> |
| 379 default_presentation_request_observers_; | 490 default_presentation_request_observers_; |
| 380 | 491 |
| 381 // References to the owning WebContents, and the corresponding MediaRouter. | 492 // References to the owning WebContents, and the corresponding MediaRouter. |
| 493 content::WebContents* const web_contents_; |
| 382 MediaRouter* router_; | 494 MediaRouter* router_; |
| 383 content::WebContents* web_contents_; | |
| 384 }; | 495 }; |
| 385 | 496 |
| 386 PresentationFrameManager::PresentationFrameManager( | 497 PresentationFrameManager::PresentationFrameManager( |
| 387 content::WebContents* web_contents, | 498 content::WebContents* web_contents, |
| 388 MediaRouter* router) | 499 MediaRouter* router) |
| 389 : router_(router), web_contents_(web_contents) { | 500 : web_contents_(web_contents), router_(router) { |
| 390 DCHECK(web_contents_); | 501 DCHECK(web_contents_); |
| 391 DCHECK(router_); | 502 DCHECK(router_); |
| 392 } | 503 } |
| 393 | 504 |
| 394 PresentationFrameManager::~PresentationFrameManager() { | 505 PresentationFrameManager::~PresentationFrameManager() { |
| 395 for (auto& frame : presentation_frames_) | 506 for (auto& frame : presentation_frames_) |
| 396 frame.second->OnPresentationServiceDelegateDestroyed(); | 507 frame.second->OnPresentationServiceDelegateDestroyed(); |
| 397 } | 508 } |
| 398 | 509 |
| 399 void PresentationFrameManager::OnPresentationSessionStarted( | 510 void PresentationFrameManager::OnPresentationSessionStarted( |
| 400 const RenderFrameHostId& render_frame_host_id, | 511 const RenderFrameHostId& render_frame_host_id, |
| 401 const content::PresentationSessionInfo& session, | 512 const content::PresentationSessionInfo& session, |
| 402 const MediaRoute::Id& route_id) { | 513 const MediaRoute& route) { |
| 403 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 514 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 404 presentation_frame->OnPresentationSessionStarted(session, route_id); | 515 presentation_frame->OnPresentationSessionStarted(session, route); |
| 405 } | 516 } |
| 406 | 517 |
| 407 void PresentationFrameManager::OnDefaultPresentationSessionStarted( | 518 void PresentationFrameManager::OnDefaultPresentationSessionStarted( |
| 408 const PresentationRequest& request, | 519 const PresentationRequest& request, |
| 409 const content::PresentationSessionInfo& session, | 520 const content::PresentationSessionInfo& session, |
| 410 const MediaRoute::Id& route_id) { | 521 const MediaRoute& route) { |
| 411 auto presentation_frame = | 522 auto presentation_frame = |
| 412 presentation_frames_.get(request.render_frame_host_id()); | 523 presentation_frames_.get(request.render_frame_host_id()); |
| 413 if (presentation_frame) | 524 if (presentation_frame) |
| 414 presentation_frame->OnPresentationSessionStarted(session, route_id); | 525 presentation_frame->OnPresentationSessionStarted(session, route); |
| 415 | 526 |
| 416 if (default_presentation_request_ && | 527 if (default_presentation_request_ && |
| 417 default_presentation_request_->Equals(request)) { | 528 default_presentation_request_->Equals(request)) { |
| 418 default_presentation_started_callback_.Run(session); | 529 default_presentation_started_callback_.Run(session); |
| 419 } | 530 } |
| 420 } | 531 } |
| 421 | 532 |
| 422 const MediaRoute::Id PresentationFrameManager::GetRouteId( | 533 const MediaRoute::Id PresentationFrameManager::GetRouteId( |
| 423 const RenderFrameHostId& render_frame_host_id, | 534 const RenderFrameHostId& render_frame_host_id, |
| 424 const std::string& presentation_id) const { | 535 const std::string& presentation_id) const { |
| 425 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 536 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 426 return presentation_frame ? presentation_frame->GetRouteId(presentation_id) | 537 return presentation_frame ? presentation_frame->GetRouteId(presentation_id) |
| 427 : ""; | 538 : ""; |
| 428 } | 539 } |
| 429 | 540 |
| 430 const std::vector<MediaRoute::Id> PresentationFrameManager::GetRouteIds( | |
| 431 const RenderFrameHostId& render_frame_host_id) const { | |
| 432 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | |
| 433 return presentation_frame ? presentation_frame->GetRouteIds() | |
| 434 : std::vector<MediaRoute::Id>(); | |
| 435 } | |
| 436 | |
| 437 bool PresentationFrameManager::SetScreenAvailabilityListener( | 541 bool PresentationFrameManager::SetScreenAvailabilityListener( |
| 438 const RenderFrameHostId& render_frame_host_id, | 542 const RenderFrameHostId& render_frame_host_id, |
| 439 content::PresentationScreenAvailabilityListener* listener) { | 543 content::PresentationScreenAvailabilityListener* listener) { |
| 440 DCHECK(listener); | 544 DCHECK(listener); |
| 441 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 545 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 442 return presentation_frame->SetScreenAvailabilityListener(listener); | 546 return presentation_frame->SetScreenAvailabilityListener(listener); |
| 443 } | 547 } |
| 444 | 548 |
| 445 bool PresentationFrameManager::RemoveScreenAvailabilityListener( | 549 bool PresentationFrameManager::RemoveScreenAvailabilityListener( |
| 446 const RenderFrameHostId& render_frame_host_id, | 550 const RenderFrameHostId& render_frame_host_id, |
| 447 content::PresentationScreenAvailabilityListener* listener) { | 551 content::PresentationScreenAvailabilityListener* listener) { |
| 448 DCHECK(listener); | 552 DCHECK(listener); |
| 449 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 553 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 450 return presentation_frame && | 554 return presentation_frame && |
| 451 presentation_frame->RemoveScreenAvailabilityListener(listener); | 555 presentation_frame->RemoveScreenAvailabilityListener(listener); |
| 452 } | 556 } |
| 453 | 557 |
| 454 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( | 558 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( |
| 455 const RenderFrameHostId& render_frame_host_id, | 559 const RenderFrameHostId& render_frame_host_id, |
| 456 const MediaSource::Id& source_id) const { | 560 const MediaSource::Id& source_id) const { |
| 457 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 561 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 458 return presentation_frame && | 562 return presentation_frame && |
| 459 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); | 563 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); |
| 460 } | 564 } |
| 461 | 565 |
| 566 void PresentationFrameManager::SendMessage( |
| 567 const RenderFrameHostId& render_frame_host_id, |
| 568 const content::PresentationSessionInfo& session, |
| 569 std::unique_ptr<content::PresentationSessionMessage> message, |
| 570 const content::SendMessageCallback send_message_cb) { |
| 571 PresentationFrame* presentation_frame = |
| 572 presentation_frames_.get(render_frame_host_id); |
| 573 if (!presentation_frame) { |
| 574 DVLOG(2) << "SendMessage: PresentationFrame does not exist " |
| 575 << "for: (" << render_frame_host_id.first << ", " |
| 576 << render_frame_host_id.second << ")"; |
| 577 send_message_cb.Run(false); |
| 578 return; |
| 579 } |
| 580 |
| 581 presentation_frame->SendMessage(session, std::move(message), send_message_cb); |
| 582 } |
| 583 |
| 462 void PresentationFrameManager::ListenForConnectionStateChange( | 584 void PresentationFrameManager::ListenForConnectionStateChange( |
| 463 const RenderFrameHostId& render_frame_host_id, | 585 const RenderFrameHostId& render_frame_host_id, |
| 464 const content::PresentationSessionInfo& connection, | 586 const content::PresentationSessionInfo& connection, |
| 465 const content::PresentationConnectionStateChangedCallback& | 587 const content::PresentationConnectionStateChangedCallback& |
| 466 state_changed_cb) { | 588 state_changed_cb) { |
| 467 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 589 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 468 if (presentation_frame) { | 590 if (presentation_frame) { |
| 469 presentation_frame->ListenForConnectionStateChange(connection, | 591 presentation_frame->ListenForConnectionStateChange(connection, |
| 470 state_changed_cb); | 592 state_changed_cb); |
| 471 } | 593 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 | 663 |
| 542 if (default_presentation_request_ && | 664 if (default_presentation_request_ && |
| 543 render_frame_host_id == | 665 render_frame_host_id == |
| 544 default_presentation_request_->render_frame_host_id()) { | 666 default_presentation_request_->render_frame_host_id()) { |
| 545 ClearDefaultPresentationRequest(); | 667 ClearDefaultPresentationRequest(); |
| 546 } | 668 } |
| 547 } | 669 } |
| 548 | 670 |
| 549 void PresentationFrameManager::RemoveConnection( | 671 void PresentationFrameManager::RemoveConnection( |
| 550 const RenderFrameHostId& render_frame_host_id, | 672 const RenderFrameHostId& render_frame_host_id, |
| 551 const MediaRoute::Id& route_id, | 673 const std::string& presentation_id, |
| 552 const std::string& presentation_id) { | 674 const content::PresentationConnectionStateChangeInfo& state_change_info) { |
| 553 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 675 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 554 if (presentation_frame) | 676 if (presentation_frame) |
| 555 presentation_frame->RemoveConnection(route_id, presentation_id); | 677 presentation_frame->RemoveConnection(presentation_id, state_change_info); |
| 556 } | 678 } |
| 557 | 679 |
| 558 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( | 680 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( |
| 559 const RenderFrameHostId& render_frame_host_id) { | 681 const RenderFrameHostId& render_frame_host_id) { |
| 560 if (!presentation_frames_.contains(render_frame_host_id)) { | 682 if (!presentation_frames_.contains(render_frame_host_id)) { |
| 561 presentation_frames_.add( | 683 presentation_frames_.add( |
| 562 render_frame_host_id, | 684 render_frame_host_id, |
| 563 std::unique_ptr<PresentationFrame>(new PresentationFrame( | 685 std::unique_ptr<PresentationFrame>(new PresentationFrame( |
| 564 render_frame_host_id, web_contents_, router_))); | 686 render_frame_host_id, web_contents_, router_))); |
| 565 } | 687 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 const content::PresentationSessionStartedCallback& success_cb, | 801 const content::PresentationSessionStartedCallback& success_cb, |
| 680 const content::PresentationSessionErrorCallback& error_cb, | 802 const content::PresentationSessionErrorCallback& error_cb, |
| 681 const RouteRequestResult& result) { | 803 const RouteRequestResult& result) { |
| 682 if (!result.route()) { | 804 if (!result.route()) { |
| 683 error_cb.Run(content::PresentationError( | 805 error_cb.Run(content::PresentationError( |
| 684 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error())); | 806 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error())); |
| 685 } else { | 807 } else { |
| 686 DVLOG(1) << "OnJoinRouteResponse: " | 808 DVLOG(1) << "OnJoinRouteResponse: " |
| 687 << "route_id: " << result.route()->media_route_id() | 809 << "route_id: " << result.route()->media_route_id() |
| 688 << ", presentation URL: " << session.presentation_url | 810 << ", presentation URL: " << session.presentation_url |
| 689 << ", presentation ID: " << session.presentation_id; | 811 << ", presentation ID: " << session.presentation_id |
| 812 << ", offscreen? " << result.route()->is_offscreen_presentation(); |
| 690 DCHECK_EQ(session.presentation_id, result.presentation_id()); | 813 DCHECK_EQ(session.presentation_id, result.presentation_id()); |
| 691 frame_manager_->OnPresentationSessionStarted( | 814 frame_manager_->OnPresentationSessionStarted( |
| 692 RenderFrameHostId(render_process_id, render_frame_id), session, | 815 RenderFrameHostId(render_process_id, render_frame_id), session, |
| 693 result.route()->media_route_id()); | 816 *result.route()); |
| 694 success_cb.Run(session); | 817 success_cb.Run(session); |
| 695 } | 818 } |
| 696 } | 819 } |
| 697 | 820 |
| 698 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( | 821 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( |
| 699 int render_process_id, | 822 int render_process_id, |
| 700 int render_frame_id, | 823 int render_frame_id, |
| 701 const content::PresentationSessionStartedCallback& success_cb, | 824 const content::PresentationSessionStartedCallback& success_cb, |
| 702 const content::PresentationSessionInfo& new_session, | 825 const content::PresentationSessionInfo& new_session, |
| 703 const MediaRoute::Id& route_id) { | 826 const MediaRoute& route) { |
| 827 const MediaRoute::Id& route_id = route.media_route_id(); |
| 704 DVLOG(1) << "OnStartSessionSucceeded: " | 828 DVLOG(1) << "OnStartSessionSucceeded: " |
| 705 << "route_id: " << route_id | 829 << "route_id: " << route_id |
| 706 << ", presentation URL: " << new_session.presentation_url | 830 << ", presentation URL: " << new_session.presentation_url |
| 707 << ", presentation ID: " << new_session.presentation_id; | 831 << ", presentation ID: " << new_session.presentation_id; |
| 708 frame_manager_->OnPresentationSessionStarted( | 832 frame_manager_->OnPresentationSessionStarted( |
| 709 RenderFrameHostId(render_process_id, render_frame_id), new_session, | 833 RenderFrameHostId(render_process_id, render_frame_id), new_session, |
| 710 route_id); | 834 route); |
| 711 success_cb.Run(new_session); | 835 success_cb.Run(new_session); |
| 712 } | 836 } |
| 713 | 837 |
| 714 void PresentationServiceDelegateImpl::StartSession( | 838 void PresentationServiceDelegateImpl::StartSession( |
| 715 int render_process_id, | 839 int render_process_id, |
| 716 int render_frame_id, | 840 int render_frame_id, |
| 717 const std::string& presentation_url, | 841 const std::string& presentation_url, |
| 718 const content::PresentationSessionStartedCallback& success_cb, | 842 const content::PresentationSessionStartedCallback& success_cb, |
| 719 const content::PresentationSessionErrorCallback& error_cb) { | 843 const content::PresentationSessionErrorCallback& error_cb) { |
| 720 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { | 844 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 const std::string& presentation_id) { | 895 const std::string& presentation_id) { |
| 772 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 896 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 773 const MediaRoute::Id& route_id = | 897 const MediaRoute::Id& route_id = |
| 774 frame_manager_->GetRouteId(rfh_id, presentation_id); | 898 frame_manager_->GetRouteId(rfh_id, presentation_id); |
| 775 if (route_id.empty()) { | 899 if (route_id.empty()) { |
| 776 DVLOG(1) << "No active route for: " << presentation_id; | 900 DVLOG(1) << "No active route for: " << presentation_id; |
| 777 return; | 901 return; |
| 778 } | 902 } |
| 779 | 903 |
| 780 router_->DetachRoute(route_id); | 904 router_->DetachRoute(route_id); |
| 781 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); | 905 |
| 906 content::PresentationConnectionStateChangeInfo state_change_info( |
| 907 content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 908 state_change_info.close_reason = |
| 909 content::PRESENTATION_CONNECTION_CLOSE_REASON_CLOSED; |
| 910 frame_manager_->RemoveConnection(rfh_id, presentation_id, state_change_info); |
| 782 // TODO(mfoltz): close() should always succeed so there is no need to keep the | 911 // TODO(mfoltz): close() should always succeed so there is no need to keep the |
| 783 // state_changed_cb around - remove it and fire the ChangeEvent on the | 912 // state_changed_cb around - remove it and fire the ChangeEvent on the |
| 784 // PresentationConnection in Blink. | 913 // PresentationConnection in Blink. |
| 785 } | 914 } |
| 786 | 915 |
| 787 void PresentationServiceDelegateImpl::Terminate( | 916 void PresentationServiceDelegateImpl::Terminate( |
| 788 int render_process_id, | 917 int render_process_id, |
| 789 int render_frame_id, | 918 int render_frame_id, |
| 790 const std::string& presentation_id) { | 919 const std::string& presentation_id) { |
| 791 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 920 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 792 const MediaRoute::Id& route_id = | 921 const MediaRoute::Id& route_id = |
| 793 frame_manager_->GetRouteId(rfh_id, presentation_id); | 922 frame_manager_->GetRouteId(rfh_id, presentation_id); |
| 794 if (route_id.empty()) { | 923 if (route_id.empty()) { |
| 795 DVLOG(1) << "No active route for: " << presentation_id; | 924 DVLOG(1) << "No active route for: " << presentation_id; |
| 796 return; | 925 return; |
| 797 } | 926 } |
| 927 |
| 798 router_->TerminateRoute(route_id); | 928 router_->TerminateRoute(route_id); |
| 799 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); | 929 |
| 930 content::PresentationConnectionStateChangeInfo state_change_info( |
| 931 content::PRESENTATION_CONNECTION_STATE_TERMINATED); |
| 932 frame_manager_->RemoveConnection(rfh_id, presentation_id, state_change_info); |
| 800 } | 933 } |
| 801 | 934 |
| 802 void PresentationServiceDelegateImpl::ListenForSessionMessages( | 935 void PresentationServiceDelegateImpl::ListenForSessionMessages( |
| 803 int render_process_id, | 936 int render_process_id, |
| 804 int render_frame_id, | 937 int render_frame_id, |
| 805 const content::PresentationSessionInfo& session, | 938 const content::PresentationSessionInfo& session, |
| 806 const content::PresentationSessionMessageCallback& message_cb) { | 939 const content::PresentationSessionMessageCallback& message_cb) { |
| 807 frame_manager_->ListenForSessionMessages( | 940 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 808 RenderFrameHostId(render_process_id, render_frame_id), session, | 941 frame_manager_->ListenForSessionMessages(render_frame_host_id, session, |
| 809 message_cb); | 942 message_cb); |
| 810 } | 943 } |
| 811 | 944 |
| 812 void PresentationServiceDelegateImpl::SendMessage( | 945 void PresentationServiceDelegateImpl::SendMessage( |
| 813 int render_process_id, | 946 int render_process_id, |
| 814 int render_frame_id, | 947 int render_frame_id, |
| 815 const content::PresentationSessionInfo& session, | 948 const content::PresentationSessionInfo& session, |
| 816 std::unique_ptr<content::PresentationSessionMessage> message, | 949 std::unique_ptr<content::PresentationSessionMessage> message, |
| 817 const SendMessageCallback& send_message_cb) { | 950 const content::SendMessageCallback& send_message_cb) { |
| 818 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 951 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 819 RenderFrameHostId(render_process_id, render_frame_id), | 952 frame_manager_->SendMessage(render_frame_host_id, session, std::move(message), |
| 820 session.presentation_id); | 953 send_message_cb); |
| 821 if (route_id.empty()) { | |
| 822 DVLOG(1) << "No active route for " << session.presentation_id; | |
| 823 send_message_cb.Run(false); | |
| 824 return; | |
| 825 } | |
| 826 | |
| 827 if (message->is_binary()) { | |
| 828 router_->SendRouteBinaryMessage(route_id, std::move(message->data), | |
| 829 send_message_cb); | |
| 830 } else { | |
| 831 router_->SendRouteMessage(route_id, message->message, send_message_cb); | |
| 832 } | |
| 833 } | 954 } |
| 834 | 955 |
| 835 void PresentationServiceDelegateImpl::ListenForConnectionStateChange( | 956 void PresentationServiceDelegateImpl::ListenForConnectionStateChange( |
| 836 int render_process_id, | 957 int render_process_id, |
| 837 int render_frame_id, | 958 int render_frame_id, |
| 838 const content::PresentationSessionInfo& connection, | 959 const content::PresentationSessionInfo& connection, |
| 839 const content::PresentationConnectionStateChangedCallback& | 960 const content::PresentationConnectionStateChangedCallback& |
| 840 state_changed_cb) { | 961 state_changed_cb) { |
| 841 frame_manager_->ListenForConnectionStateChange( | 962 frame_manager_->ListenForConnectionStateChange( |
| 842 RenderFrameHostId(render_process_id, render_frame_id), connection, | 963 RenderFrameHostId(render_process_id, render_frame_id), connection, |
| 843 state_changed_cb); | 964 state_changed_cb); |
| 844 } | 965 } |
| 845 | 966 |
| 967 std::vector<content::PresentationSessionInfo> |
| 968 PresentationServiceDelegateImpl::GetReceiverConnections( |
| 969 int render_process_id, |
| 970 int render_frame_id, |
| 971 const content::PresentationSessionStartedCallback& callback) { |
| 972 // We only support receiver APIs in offscreen tabs created for offscreen |
| 973 // presentations. |
| 974 // See ReceiverPresentationServiceDelegateImpl for details. |
| 975 NOTIMPLEMENTED(); |
| 976 return std::vector<content::PresentationSessionInfo>(); |
| 977 } |
| 978 |
| 846 void PresentationServiceDelegateImpl::OnRouteResponse( | 979 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 847 const PresentationRequest& presentation_request, | 980 const PresentationRequest& presentation_request, |
| 848 const RouteRequestResult& result) { | 981 const RouteRequestResult& result) { |
| 849 if (!result.route()) | 982 if (!result.route()) |
| 850 return; | 983 return; |
| 851 | 984 |
| 852 content::PresentationSessionInfo session_info( | 985 content::PresentationSessionInfo session_info( |
| 853 presentation_request.presentation_url(), result.presentation_id()); | 986 presentation_request.presentation_url(), result.presentation_id()); |
| 854 frame_manager_->OnDefaultPresentationSessionStarted( | 987 frame_manager_->OnDefaultPresentationSessionStarted( |
| 855 presentation_request, session_info, result.route()->media_route_id()); | 988 presentation_request, session_info, *result.route()); |
| 856 } | 989 } |
| 857 | 990 |
| 858 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( | 991 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |
| 859 DefaultPresentationRequestObserver* observer) { | 992 DefaultPresentationRequestObserver* observer) { |
| 860 frame_manager_->AddDefaultPresentationRequestObserver(observer); | 993 frame_manager_->AddDefaultPresentationRequestObserver(observer); |
| 861 } | 994 } |
| 862 | 995 |
| 863 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( | 996 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |
| 864 DefaultPresentationRequestObserver* observer) { | 997 DefaultPresentationRequestObserver* observer) { |
| 865 frame_manager_->RemoveDefaultPresentationRequestObserver(observer); | 998 frame_manager_->RemoveDefaultPresentationRequestObserver(observer); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 889 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 1022 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 890 int render_process_id, | 1023 int render_process_id, |
| 891 int render_frame_id, | 1024 int render_frame_id, |
| 892 const MediaSource::Id& source_id) const { | 1025 const MediaSource::Id& source_id) const { |
| 893 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 1026 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 894 return frame_manager_->HasScreenAvailabilityListenerForTest( | 1027 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 895 render_frame_host_id, source_id); | 1028 render_frame_host_id, source_id); |
| 896 } | 1029 } |
| 897 | 1030 |
| 898 } // namespace media_router | 1031 } // namespace media_router |
| OLD | NEW |