Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" | 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 url::Origin GetLastCommittedURLForFrame( | 64 url::Origin GetLastCommittedURLForFrame( |
| 65 RenderFrameHostId render_frame_host_id) { | 65 RenderFrameHostId render_frame_host_id) { |
| 66 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( | 66 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( |
| 67 render_frame_host_id.first, render_frame_host_id.second); | 67 render_frame_host_id.first, render_frame_host_id.second); |
| 68 DCHECK(render_frame_host); | 68 DCHECK(render_frame_host); |
| 69 return render_frame_host->GetLastCommittedOrigin(); | 69 return render_frame_host->GetLastCommittedOrigin(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 } // namespace | 72 } // namespace |
| 73 | 73 |
| 74 // Used by PresentationServiceDelegateImpl to manage | 74 // PresentationFrame interfaces with MediaRouter to maintain the current state |
| 75 // listeners and default presentation info in a render frame. | 75 // of Presentation API within a single render frame, such as the set of |
| 76 // Its lifetime: | 76 // PresentationAvailability listeners and PresentationConnections. |
| 77 // * Create an instance with |render_frame_host_id_| if no instance with the | 77 // Instances are created when certain Presentation API is invoked on a frame, |
| 78 // same |render_frame_host_id_| exists in: | 78 // and are owned by PresentationServiceDelegateImpl. |
| 79 // PresentationFrameManager::OnPresentationConnection | 79 // Instances are destroyed when the corresponding frame navigates, or when it |
| 80 // PresentationFrameManager::OnDefaultPresentationStarted | 80 // is destroyed. |
| 81 // PresentationFrameManager::SetScreenAvailabilityListener | |
| 82 // * Destroy the instance in: | |
| 83 // PresentationFrameManager::Reset | |
| 84 class PresentationFrame { | 81 class PresentationFrame { |
| 85 public: | 82 public: |
| 86 PresentationFrame(const RenderFrameHostId& render_frame_host_id, | 83 PresentationFrame(const RenderFrameHostId& render_frame_host_id, |
| 87 content::WebContents* web_contents, | 84 content::WebContents* web_contents, |
| 88 MediaRouter* router); | 85 MediaRouter* router); |
| 89 ~PresentationFrame(); | 86 ~PresentationFrame(); |
| 90 | 87 |
| 91 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 88 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 92 bool SetScreenAvailabilityListener( | 89 bool SetScreenAvailabilityListener( |
| 93 content::PresentationScreenAvailabilityListener* listener); | 90 content::PresentationScreenAvailabilityListener* listener); |
| 94 void RemoveScreenAvailabilityListener( | 91 void RemoveScreenAvailabilityListener( |
| 95 content::PresentationScreenAvailabilityListener* listener); | 92 content::PresentationScreenAvailabilityListener* listener); |
| 96 bool HasScreenAvailabilityListenerForTest( | 93 bool HasScreenAvailabilityListenerForTest( |
| 97 const MediaSource::Id& source_id) const; | 94 const MediaSource::Id& source_id) const; |
| 98 std::string GetDefaultPresentationId() const; | |
| 99 void ListenForConnectionStateChange( | 95 void ListenForConnectionStateChange( |
| 100 const content::PresentationInfo& connection, | 96 const content::PresentationInfo& connection, |
| 101 const content::PresentationConnectionStateChangedCallback& | 97 const content::PresentationConnectionStateChangedCallback& |
| 102 state_changed_cb); | 98 state_changed_cb); |
| 103 | 99 |
| 104 void Reset(); | 100 void Reset(); |
| 105 void RemoveConnection(const std::string& presentation_id, | |
| 106 const MediaRoute::Id& route_id); | |
| 107 | 101 |
| 108 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 102 MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| 109 | 103 |
| 110 void OnPresentationConnection( | 104 void AddPresentation(const content::PresentationInfo& presentation_info, |
| 111 const content::PresentationInfo& presentation_info, | 105 const MediaRoute& route); |
| 112 const MediaRoute& route); | |
| 113 void OnPresentationServiceDelegateDestroyed() const; | |
| 114 | |
| 115 void ConnectToPresentation( | 106 void ConnectToPresentation( |
| 116 const content::PresentationInfo& presentation_info, | 107 const content::PresentationInfo& presentation_info, |
| 117 content::PresentationConnectionPtr controller_connection_ptr, | 108 content::PresentationConnectionPtr controller_connection_ptr, |
| 118 content::PresentationConnectionRequest receiver_connection_request); | 109 content::PresentationConnectionRequest receiver_connection_request); |
| 110 void RemovePresentation(const std::string& presentation_id, | |
| 111 const MediaRoute::Id& route_id); | |
| 119 | 112 |
| 120 private: | 113 private: |
| 121 MediaSource GetMediaSourceFromListener( | |
| 122 content::PresentationScreenAvailabilityListener* listener) const; | |
| 123 base::small_map<std::map<std::string, MediaRoute>> presentation_id_to_route_; | 114 base::small_map<std::map<std::string, MediaRoute>> presentation_id_to_route_; |
| 124 base::small_map< | 115 base::small_map< |
| 125 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> | 116 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> |
| 126 url_to_sinks_observer_; | 117 url_to_sinks_observer_; |
| 127 std::unordered_map< | 118 std::unordered_map< |
| 128 MediaRoute::Id, | 119 MediaRoute::Id, |
| 129 std::unique_ptr<PresentationConnectionStateSubscription>> | 120 std::unique_ptr<PresentationConnectionStateSubscription>> |
| 130 connection_state_subscriptions_; | 121 connection_state_subscriptions_; |
| 131 std::unordered_map<MediaRoute::Id, | 122 std::unordered_map<MediaRoute::Id, |
| 132 std::unique_ptr<BrowserPresentationConnectionProxy>> | 123 std::unique_ptr<BrowserPresentationConnectionProxy>> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 143 const RenderFrameHostId& render_frame_host_id, | 134 const RenderFrameHostId& render_frame_host_id, |
| 144 content::WebContents* web_contents, | 135 content::WebContents* web_contents, |
| 145 MediaRouter* router) | 136 MediaRouter* router) |
| 146 : render_frame_host_id_(render_frame_host_id), | 137 : render_frame_host_id_(render_frame_host_id), |
| 147 web_contents_(web_contents), | 138 web_contents_(web_contents), |
| 148 router_(router) { | 139 router_(router) { |
| 149 DCHECK(web_contents_); | 140 DCHECK(web_contents_); |
| 150 DCHECK(router_); | 141 DCHECK(router_); |
| 151 } | 142 } |
| 152 | 143 |
| 153 PresentationFrame::~PresentationFrame() { | 144 PresentationFrame::~PresentationFrame() = default; |
| 154 } | |
| 155 | 145 |
| 156 void PresentationFrame::OnPresentationConnection( | 146 MediaRoute::Id PresentationFrame::GetRouteId( |
| 157 const content::PresentationInfo& presentation_info, | |
| 158 const MediaRoute& route) { | |
| 159 presentation_id_to_route_.insert( | |
| 160 std::make_pair(presentation_info.presentation_id, route)); | |
| 161 } | |
| 162 | |
| 163 const MediaRoute::Id PresentationFrame::GetRouteId( | |
| 164 const std::string& presentation_id) const { | 147 const std::string& presentation_id) const { |
| 165 auto it = presentation_id_to_route_.find(presentation_id); | 148 auto it = presentation_id_to_route_.find(presentation_id); |
| 166 return it != presentation_id_to_route_.end() ? it->second.media_route_id() | 149 return it != presentation_id_to_route_.end() ? it->second.media_route_id() |
| 167 : ""; | 150 : ""; |
| 168 } | 151 } |
| 169 | 152 |
| 170 bool PresentationFrame::SetScreenAvailabilityListener( | 153 bool PresentationFrame::SetScreenAvailabilityListener( |
| 171 content::PresentationScreenAvailabilityListener* listener) { | 154 content::PresentationScreenAvailabilityListener* listener) { |
| 172 MediaSource source(GetMediaSourceFromListener(listener)); | 155 MediaSource source = |
| 156 MediaSourceForPresentationUrl(listener->GetAvailabilityUrl()); | |
| 173 if (!IsValidPresentationUrl(source.url())) { | 157 if (!IsValidPresentationUrl(source.url())) { |
| 174 listener->OnScreenAvailabilityChanged( | 158 listener->OnScreenAvailabilityChanged( |
| 175 blink::mojom::ScreenAvailability::SOURCE_NOT_SUPPORTED); | 159 blink::mojom::ScreenAvailability::SOURCE_NOT_SUPPORTED); |
| 176 return false; | 160 return false; |
| 177 } | 161 } |
| 178 | 162 |
| 179 auto& sinks_observer = url_to_sinks_observer_[source.id()]; | 163 auto& sinks_observer = url_to_sinks_observer_[source.id()]; |
| 180 if (sinks_observer && sinks_observer->listener() == listener) | 164 if (sinks_observer && sinks_observer->listener() == listener) |
| 181 return false; | 165 return false; |
| 182 | 166 |
| 183 sinks_observer.reset(new PresentationMediaSinksObserver( | 167 sinks_observer.reset(new PresentationMediaSinksObserver( |
| 184 router_, listener, source, | 168 router_, listener, source, |
| 185 GetLastCommittedURLForFrame(render_frame_host_id_))); | 169 GetLastCommittedURLForFrame(render_frame_host_id_))); |
| 186 | 170 |
| 187 if (!sinks_observer->Init()) { | 171 if (!sinks_observer->Init()) { |
| 188 url_to_sinks_observer_.erase(source.id()); | 172 url_to_sinks_observer_.erase(source.id()); |
| 189 listener->OnScreenAvailabilityChanged( | 173 listener->OnScreenAvailabilityChanged( |
| 190 blink::mojom::ScreenAvailability::DISABLED); | 174 blink::mojom::ScreenAvailability::DISABLED); |
| 191 return false; | 175 return false; |
| 192 } | 176 } |
| 193 | 177 |
| 194 return true; | 178 return true; |
| 195 } | 179 } |
| 196 | 180 |
| 197 void PresentationFrame::RemoveScreenAvailabilityListener( | 181 void PresentationFrame::RemoveScreenAvailabilityListener( |
| 198 content::PresentationScreenAvailabilityListener* listener) { | 182 content::PresentationScreenAvailabilityListener* listener) { |
| 199 MediaSource source(GetMediaSourceFromListener(listener)); | 183 MediaSource source = |
| 184 MediaSourceForPresentationUrl(listener->GetAvailabilityUrl()); | |
| 200 auto sinks_observer_it = url_to_sinks_observer_.find(source.id()); | 185 auto sinks_observer_it = url_to_sinks_observer_.find(source.id()); |
| 201 if (sinks_observer_it != url_to_sinks_observer_.end() && | 186 if (sinks_observer_it != url_to_sinks_observer_.end() && |
| 202 sinks_observer_it->second->listener() == listener) { | 187 sinks_observer_it->second->listener() == listener) { |
| 203 url_to_sinks_observer_.erase(sinks_observer_it); | 188 url_to_sinks_observer_.erase(sinks_observer_it); |
| 204 } | 189 } |
| 205 } | 190 } |
| 206 | 191 |
| 207 bool PresentationFrame::HasScreenAvailabilityListenerForTest( | 192 bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
| 208 const MediaSource::Id& source_id) const { | 193 const MediaSource::Id& source_id) const { |
| 209 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); | 194 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 221 router_->DetachRoute(pid_route.second.media_route_id()); | 206 router_->DetachRoute(pid_route.second.media_route_id()); |
| 222 } | 207 } |
| 223 } | 208 } |
| 224 | 209 |
| 225 presentation_id_to_route_.clear(); | 210 presentation_id_to_route_.clear(); |
| 226 url_to_sinks_observer_.clear(); | 211 url_to_sinks_observer_.clear(); |
| 227 connection_state_subscriptions_.clear(); | 212 connection_state_subscriptions_.clear(); |
| 228 browser_connection_proxies_.clear(); | 213 browser_connection_proxies_.clear(); |
| 229 } | 214 } |
| 230 | 215 |
| 231 void PresentationFrame::RemoveConnection(const std::string& presentation_id, | 216 void PresentationFrame::AddPresentation( |
| 232 const MediaRoute::Id& route_id) { | 217 const content::PresentationInfo& presentation_info, |
| 233 // Remove the presentation id mapping so a later call to Reset is a no-op. | 218 const MediaRoute& route) { |
| 234 presentation_id_to_route_.erase(presentation_id); | 219 presentation_id_to_route_.insert( |
| 235 | 220 std::make_pair(presentation_info.presentation_id, route)); |
| 236 browser_connection_proxies_.erase(route_id); | |
| 237 // We keep the PresentationConnectionStateChangedCallback registered with MR | |
| 238 // so the MRP can tell us when terminate() completed. | |
| 239 } | |
| 240 | |
| 241 void PresentationFrame::ListenForConnectionStateChange( | |
| 242 const content::PresentationInfo& connection, | |
| 243 const content::PresentationConnectionStateChangedCallback& | |
| 244 state_changed_cb) { | |
| 245 auto it = presentation_id_to_route_.find(connection.presentation_id); | |
| 246 if (it == presentation_id_to_route_.end()) { | |
| 247 DLOG(ERROR) << __func__ << "route id not found for presentation: " | |
| 248 << connection.presentation_id; | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 const MediaRoute::Id& route_id = it->second.media_route_id(); | |
| 253 if (connection_state_subscriptions_.find(route_id) != | |
| 254 connection_state_subscriptions_.end()) { | |
| 255 DLOG(ERROR) << __func__ | |
| 256 << "Already listening connection state change for route: " | |
| 257 << route_id; | |
| 258 return; | |
| 259 } | |
| 260 | |
| 261 connection_state_subscriptions_.insert(std::make_pair( | |
| 262 route_id, router_->AddPresentationConnectionStateChangedCallback( | |
| 263 route_id, state_changed_cb))); | |
| 264 } | |
| 265 | |
| 266 MediaSource PresentationFrame::GetMediaSourceFromListener( | |
| 267 content::PresentationScreenAvailabilityListener* listener) const { | |
| 268 // If the default presentation URL is empty then fall back to tab mirroring. | |
| 269 return listener->GetAvailabilityUrl().is_empty() | |
| 270 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) | |
| 271 : MediaSourceForPresentationUrl(listener->GetAvailabilityUrl()); | |
| 272 } | 221 } |
| 273 | 222 |
| 274 void PresentationFrame::ConnectToPresentation( | 223 void PresentationFrame::ConnectToPresentation( |
| 275 const content::PresentationInfo& presentation_info, | 224 const content::PresentationInfo& presentation_info, |
| 276 content::PresentationConnectionPtr controller_connection_ptr, | 225 content::PresentationConnectionPtr controller_connection_ptr, |
| 277 content::PresentationConnectionRequest receiver_connection_request) { | 226 content::PresentationConnectionRequest receiver_connection_request) { |
| 278 const auto pid_route_it = | 227 const auto pid_route_it = |
| 279 presentation_id_to_route_.find(presentation_info.presentation_id); | 228 presentation_id_to_route_.find(presentation_info.presentation_id); |
| 280 | 229 |
| 281 if (pid_route_it == presentation_id_to_route_.end()) { | 230 if (pid_route_it == presentation_id_to_route_.end()) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 305 } | 254 } |
| 306 | 255 |
| 307 auto* proxy = new BrowserPresentationConnectionProxy( | 256 auto* proxy = new BrowserPresentationConnectionProxy( |
| 308 router_, route_id, std::move(receiver_connection_request), | 257 router_, route_id, std::move(receiver_connection_request), |
| 309 std::move(controller_connection_ptr)); | 258 std::move(controller_connection_ptr)); |
| 310 browser_connection_proxies_.insert( | 259 browser_connection_proxies_.insert( |
| 311 std::make_pair(route_id, base::WrapUnique(proxy))); | 260 std::make_pair(route_id, base::WrapUnique(proxy))); |
| 312 } | 261 } |
| 313 } | 262 } |
| 314 | 263 |
| 315 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. | 264 void PresentationFrame::RemovePresentation(const std::string& presentation_id, |
| 316 class PresentationFrameManager { | 265 const MediaRoute::Id& route_id) { |
| 317 public: | 266 // Remove the presentation id mapping so a later call to Reset is a no-op. |
| 318 PresentationFrameManager(content::WebContents* web_contents, | 267 presentation_id_to_route_.erase(presentation_id); |
| 319 MediaRouter* router); | |
| 320 ~PresentationFrameManager(); | |
| 321 | 268 |
| 322 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 269 browser_connection_proxies_.erase(route_id); |
| 323 bool SetScreenAvailabilityListener( | 270 // We keep the PresentationConnectionStateChangedCallback registered with MR |
| 324 const RenderFrameHostId& render_frame_host_id, | 271 // so the MRP can tell us when terminate() completed. |
| 325 content::PresentationScreenAvailabilityListener* listener); | |
| 326 void RemoveScreenAvailabilityListener( | |
| 327 const RenderFrameHostId& render_frame_host_id, | |
| 328 content::PresentationScreenAvailabilityListener* listener); | |
| 329 void ListenForConnectionStateChange( | |
| 330 const RenderFrameHostId& render_frame_host_id, | |
| 331 const content::PresentationInfo& connection, | |
| 332 const content::PresentationConnectionStateChangedCallback& | |
| 333 state_changed_cb); | |
| 334 | |
| 335 // Sets or clears the default presentation request and callback for the given | |
| 336 // frame. Also sets / clears the default presentation requests for the owning | |
| 337 // tab WebContents. | |
| 338 void SetDefaultPresentationUrls( | |
| 339 const RenderFrameHostId& render_frame_host_id, | |
| 340 const std::vector<GURL>& default_presentation_urls, | |
| 341 const content::PresentationConnectionCallback& callback); | |
| 342 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | |
| 343 DelegateObserver* observer); | |
| 344 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | |
| 345 void AddDefaultPresentationRequestObserver( | |
| 346 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | |
| 347 observer); | |
| 348 void RemoveDefaultPresentationRequestObserver( | |
| 349 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | |
| 350 observer); | |
| 351 void Reset(const RenderFrameHostId& render_frame_host_id); | |
| 352 void RemoveConnection(const RenderFrameHostId& render_frame_host_id, | |
| 353 const MediaRoute::Id& route_id, | |
| 354 const std::string& presentation_id); | |
| 355 bool HasScreenAvailabilityListenerForTest( | |
| 356 const RenderFrameHostId& render_frame_host_id, | |
| 357 const MediaSource::Id& source_id) const; | |
| 358 void SetMediaRouterForTest(MediaRouter* router); | |
| 359 | |
| 360 void OnPresentationConnection( | |
| 361 const RenderFrameHostId& render_frame_host_id, | |
| 362 const content::PresentationInfo& presentation_info, | |
| 363 const MediaRoute& route); | |
| 364 void OnDefaultPresentationStarted( | |
| 365 const PresentationRequest& request, | |
| 366 const content::PresentationInfo& presentation_info, | |
| 367 const MediaRoute& route); | |
| 368 | |
| 369 void ConnectToPresentation( | |
| 370 const RenderFrameHostId& render_frame_host_id, | |
| 371 const content::PresentationInfo& presentation_info, | |
| 372 content::PresentationConnectionPtr controller_connection_ptr, | |
| 373 content::PresentationConnectionRequest receiver_connection_request); | |
| 374 | |
| 375 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, | |
| 376 const std::string& presentation_id) const; | |
| 377 | |
| 378 const PresentationRequest* default_presentation_request() const { | |
| 379 return default_presentation_request_.get(); | |
| 380 } | |
| 381 | |
| 382 private: | |
| 383 PresentationFrame* GetOrAddPresentationFrame( | |
| 384 const RenderFrameHostId& render_frame_host_id); | |
| 385 | |
| 386 // Sets the default presentation request for the owning WebContents and | |
| 387 // notifies observers of changes. | |
| 388 void SetDefaultPresentationRequest( | |
| 389 const PresentationRequest& default_presentation_request); | |
| 390 | |
| 391 // Clears the default presentation request for the owning WebContents and | |
| 392 // notifies observers of changes. Also resets | |
| 393 // |default_presentation_started_callback_|. | |
| 394 void ClearDefaultPresentationRequest(); | |
| 395 | |
| 396 bool IsMainFrame(const RenderFrameHostId& render_frame_host_id) const; | |
| 397 | |
| 398 // Maps a frame identifier to a PresentationFrame object for frames | |
| 399 // that are using presentation API. | |
| 400 std::unordered_map<RenderFrameHostId, std::unique_ptr<PresentationFrame>, | |
| 401 RenderFrameHostIdHasher> | |
| 402 presentation_frames_; | |
| 403 | |
| 404 // Default presentation request for the owning tab WebContents. | |
| 405 std::unique_ptr<PresentationRequest> default_presentation_request_; | |
| 406 | |
| 407 // Callback to invoke when default presentation has started. | |
| 408 content::PresentationConnectionCallback | |
| 409 default_presentation_started_callback_; | |
| 410 | |
| 411 // References to the observers listening for changes to this tab WebContent's | |
| 412 // default presentation. | |
| 413 base::ObserverList< | |
| 414 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver> | |
| 415 default_presentation_request_observers_; | |
| 416 | |
| 417 // References to the owning WebContents, and the corresponding MediaRouter. | |
| 418 MediaRouter* router_; | |
| 419 content::WebContents* web_contents_; | |
| 420 }; | |
| 421 | |
| 422 PresentationFrameManager::PresentationFrameManager( | |
| 423 content::WebContents* web_contents, | |
| 424 MediaRouter* router) | |
| 425 : router_(router), web_contents_(web_contents) { | |
| 426 DCHECK(web_contents_); | |
| 427 DCHECK(router_); | |
| 428 } | 272 } |
| 429 | 273 |
| 430 PresentationFrameManager::~PresentationFrameManager() {} | 274 void PresentationFrame::ListenForConnectionStateChange( |
| 431 | |
| 432 void PresentationFrameManager::OnPresentationConnection( | |
| 433 const RenderFrameHostId& render_frame_host_id, | |
| 434 const content::PresentationInfo& presentation_info, | |
| 435 const MediaRoute& route) { | |
| 436 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
| 437 presentation_frame->OnPresentationConnection(presentation_info, route); | |
| 438 } | |
| 439 | |
| 440 void PresentationFrameManager::OnDefaultPresentationStarted( | |
| 441 const PresentationRequest& request, | |
| 442 const content::PresentationInfo& presentation_info, | |
| 443 const MediaRoute& route) { | |
| 444 OnPresentationConnection(request.render_frame_host_id(), presentation_info, | |
| 445 route); | |
| 446 | |
| 447 if (default_presentation_request_ && | |
| 448 default_presentation_request_->Equals(request)) { | |
| 449 default_presentation_started_callback_.Run(presentation_info); | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 void PresentationFrameManager::ConnectToPresentation( | |
| 454 const RenderFrameHostId& render_frame_host_id, | |
| 455 const content::PresentationInfo& presentation_info, | |
| 456 content::PresentationConnectionPtr controller_connection_ptr, | |
| 457 content::PresentationConnectionRequest receiver_connection_request) { | |
| 458 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
| 459 presentation_frame->ConnectToPresentation( | |
| 460 presentation_info, std::move(controller_connection_ptr), | |
| 461 std::move(receiver_connection_request)); | |
| 462 } | |
| 463 | |
| 464 const MediaRoute::Id PresentationFrameManager::GetRouteId( | |
| 465 const RenderFrameHostId& render_frame_host_id, | |
| 466 const std::string& presentation_id) const { | |
| 467 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 468 return it != presentation_frames_.end() | |
| 469 ? it->second->GetRouteId(presentation_id) | |
| 470 : MediaRoute::Id(); | |
| 471 } | |
| 472 | |
| 473 bool PresentationFrameManager::SetScreenAvailabilityListener( | |
| 474 const RenderFrameHostId& render_frame_host_id, | |
| 475 content::PresentationScreenAvailabilityListener* listener) { | |
| 476 DCHECK(listener); | |
| 477 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
| 478 return presentation_frame->SetScreenAvailabilityListener(listener); | |
| 479 } | |
| 480 | |
| 481 void PresentationFrameManager::RemoveScreenAvailabilityListener( | |
| 482 const RenderFrameHostId& render_frame_host_id, | |
| 483 content::PresentationScreenAvailabilityListener* listener) { | |
| 484 DCHECK(listener); | |
| 485 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 486 if (it != presentation_frames_.end()) | |
| 487 it->second->RemoveScreenAvailabilityListener(listener); | |
| 488 } | |
| 489 | |
| 490 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest( | |
| 491 const RenderFrameHostId& render_frame_host_id, | |
| 492 const MediaSource::Id& source_id) const { | |
| 493 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 494 return it != presentation_frames_.end() && | |
| 495 it->second->HasScreenAvailabilityListenerForTest(source_id); | |
| 496 } | |
| 497 | |
| 498 void PresentationFrameManager::ListenForConnectionStateChange( | |
| 499 const RenderFrameHostId& render_frame_host_id, | |
| 500 const content::PresentationInfo& connection, | 275 const content::PresentationInfo& connection, |
| 501 const content::PresentationConnectionStateChangedCallback& | 276 const content::PresentationConnectionStateChangedCallback& |
| 502 state_changed_cb) { | 277 state_changed_cb) { |
| 503 const auto it = presentation_frames_.find(render_frame_host_id); | 278 auto it = presentation_id_to_route_.find(connection.presentation_id); |
| 504 if (it != presentation_frames_.end()) | 279 if (it == presentation_id_to_route_.end()) { |
| 505 it->second->ListenForConnectionStateChange(connection, state_changed_cb); | 280 DLOG(ERROR) << __func__ << "route id not found for presentation: " |
| 506 } | 281 << connection.presentation_id; |
| 507 | |
| 508 void PresentationFrameManager::SetDefaultPresentationUrls( | |
| 509 const RenderFrameHostId& render_frame_host_id, | |
| 510 const std::vector<GURL>& default_presentation_urls, | |
| 511 const content::PresentationConnectionCallback& callback) { | |
| 512 if (!IsMainFrame(render_frame_host_id)) | |
| 513 return; | 282 return; |
| 514 | |
| 515 if (default_presentation_urls.empty()) { | |
| 516 ClearDefaultPresentationRequest(); | |
| 517 } else { | |
| 518 DCHECK(!callback.is_null()); | |
| 519 const auto& frame_origin = | |
| 520 GetLastCommittedURLForFrame(render_frame_host_id); | |
| 521 PresentationRequest request(render_frame_host_id, default_presentation_urls, | |
| 522 frame_origin); | |
| 523 default_presentation_started_callback_ = callback; | |
| 524 SetDefaultPresentationRequest(request); | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 void PresentationFrameManager::AddDefaultPresentationRequestObserver( | |
| 529 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | |
| 530 observer) { | |
| 531 default_presentation_request_observers_.AddObserver(observer); | |
| 532 } | |
| 533 | |
| 534 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( | |
| 535 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* | |
| 536 observer) { | |
| 537 default_presentation_request_observers_.RemoveObserver(observer); | |
| 538 } | |
| 539 | |
| 540 void PresentationFrameManager::Reset( | |
| 541 const RenderFrameHostId& render_frame_host_id) { | |
| 542 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 543 if (it != presentation_frames_.end()) { | |
| 544 it->second->Reset(); | |
| 545 presentation_frames_.erase(it); | |
| 546 } | 283 } |
| 547 | 284 |
| 548 if (default_presentation_request_ && | 285 const MediaRoute::Id& route_id = it->second.media_route_id(); |
| 549 render_frame_host_id == | 286 if (connection_state_subscriptions_.find(route_id) != |
| 550 default_presentation_request_->render_frame_host_id()) { | 287 connection_state_subscriptions_.end()) { |
| 551 ClearDefaultPresentationRequest(); | 288 DLOG(ERROR) << __func__ |
| 289 << "Already listening connection state change for route: " | |
| 290 << route_id; | |
| 291 return; | |
| 552 } | 292 } |
| 553 } | |
| 554 | 293 |
| 555 void PresentationFrameManager::RemoveConnection( | 294 connection_state_subscriptions_.insert(std::make_pair( |
| 556 const RenderFrameHostId& render_frame_host_id, | 295 route_id, router_->AddPresentationConnectionStateChangedCallback( |
| 557 const MediaRoute::Id& route_id, | 296 route_id, state_changed_cb))); |
| 558 const std::string& presentation_id) { | |
| 559 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 560 if (it != presentation_frames_.end()) | |
| 561 it->second->RemoveConnection(route_id, presentation_id); | |
| 562 } | |
| 563 | |
| 564 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( | |
| 565 const RenderFrameHostId& render_frame_host_id) { | |
| 566 std::unique_ptr<PresentationFrame>& presentation_frame = | |
| 567 presentation_frames_[render_frame_host_id]; | |
| 568 if (!presentation_frame) { | |
| 569 presentation_frame.reset(new PresentationFrame( | |
| 570 render_frame_host_id, web_contents_, router_)); | |
| 571 } | |
| 572 return presentation_frame.get(); | |
| 573 } | |
| 574 | |
| 575 void PresentationFrameManager::ClearDefaultPresentationRequest() { | |
| 576 default_presentation_started_callback_.Reset(); | |
| 577 if (!default_presentation_request_) | |
| 578 return; | |
| 579 | |
| 580 default_presentation_request_.reset(); | |
| 581 for (auto& observer : default_presentation_request_observers_) | |
| 582 observer.OnDefaultPresentationRemoved(); | |
| 583 } | |
| 584 | |
| 585 bool PresentationFrameManager::IsMainFrame( | |
| 586 const RenderFrameHostId& render_frame_host_id) const { | |
| 587 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); | |
| 588 return main_frame && GetRenderFrameHostId(main_frame) == render_frame_host_id; | |
| 589 } | |
| 590 | |
| 591 void PresentationFrameManager::SetDefaultPresentationRequest( | |
| 592 const PresentationRequest& default_presentation_request) { | |
| 593 if (default_presentation_request_ && | |
| 594 default_presentation_request_->Equals(default_presentation_request)) | |
| 595 return; | |
| 596 | |
| 597 default_presentation_request_.reset( | |
| 598 new PresentationRequest(default_presentation_request)); | |
| 599 for (auto& observer : default_presentation_request_observers_) | |
| 600 observer.OnDefaultPresentationChanged(*default_presentation_request_); | |
| 601 } | |
| 602 | |
| 603 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { | |
| 604 router_ = router; | |
| 605 } | 297 } |
| 606 | 298 |
| 607 PresentationServiceDelegateImpl* | 299 PresentationServiceDelegateImpl* |
| 608 PresentationServiceDelegateImpl::GetOrCreateForWebContents( | 300 PresentationServiceDelegateImpl::GetOrCreateForWebContents( |
| 609 content::WebContents* web_contents) { | 301 content::WebContents* web_contents) { |
| 610 DCHECK(web_contents); | 302 DCHECK(web_contents); |
| 611 // CreateForWebContents does nothing if the delegate instance already exists. | 303 // CreateForWebContents does nothing if the delegate instance already exists. |
| 612 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); | 304 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); |
| 613 return PresentationServiceDelegateImpl::FromWebContents(web_contents); | 305 return PresentationServiceDelegateImpl::FromWebContents(web_contents); |
| 614 } | 306 } |
| 615 | 307 |
| 616 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl( | 308 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl( |
| 617 content::WebContents* web_contents) | 309 content::WebContents* web_contents) |
| 618 : web_contents_(web_contents), | 310 : web_contents_(web_contents), |
| 619 router_(MediaRouterFactory::GetApiForBrowserContext( | 311 router_(MediaRouterFactory::GetApiForBrowserContext( |
| 620 web_contents_->GetBrowserContext())), | 312 web_contents_->GetBrowserContext())), |
| 621 frame_manager_(new PresentationFrameManager(web_contents, router_)), | |
| 622 weak_factory_(this) { | 313 weak_factory_(this) { |
| 623 DCHECK(web_contents_); | 314 DCHECK(web_contents_); |
| 624 DCHECK(router_); | 315 DCHECK(router_); |
| 625 } | 316 } |
| 626 | 317 |
| 627 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { | 318 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() = default; |
| 628 } | |
| 629 | 319 |
| 630 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, | 320 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, |
| 631 int render_frame_id, | 321 int render_frame_id, |
| 632 DelegateObserver* observer) { | 322 DelegateObserver* observer) { |
| 633 DCHECK(observer); | 323 DCHECK(observer); |
| 634 observers_.AddObserver(render_process_id, render_frame_id, observer); | 324 observers_.AddObserver(render_process_id, render_frame_id, observer); |
| 635 } | 325 } |
| 636 | 326 |
| 637 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, | 327 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, |
| 638 int render_frame_id) { | 328 int render_frame_id) { |
| 639 observers_.RemoveObserver(render_process_id, render_frame_id); | 329 observers_.RemoveObserver(render_process_id, render_frame_id); |
| 640 } | 330 } |
| 641 | 331 |
| 642 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( | 332 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( |
| 643 int render_process_id, | 333 int render_process_id, |
| 644 int render_frame_id, | 334 int render_frame_id, |
| 645 content::PresentationScreenAvailabilityListener* listener) { | 335 content::PresentationScreenAvailabilityListener* listener) { |
| 646 DCHECK(listener); | 336 DCHECK(listener); |
| 647 return frame_manager_->SetScreenAvailabilityListener( | 337 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 648 RenderFrameHostId(render_process_id, render_frame_id), listener); | 338 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 339 return presentation_frame->SetScreenAvailabilityListener(listener); | |
| 649 } | 340 } |
| 650 | 341 |
| 651 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener( | 342 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener( |
| 652 int render_process_id, | 343 int render_process_id, |
| 653 int render_frame_id, | 344 int render_frame_id, |
| 654 content::PresentationScreenAvailabilityListener* listener) { | 345 content::PresentationScreenAvailabilityListener* listener) { |
| 655 DCHECK(listener); | 346 DCHECK(listener); |
| 656 frame_manager_->RemoveScreenAvailabilityListener( | 347 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 657 RenderFrameHostId(render_process_id, render_frame_id), listener); | 348 const auto it = presentation_frames_.find(render_frame_host_id); |
| 349 if (it != presentation_frames_.end()) | |
| 350 it->second->RemoveScreenAvailabilityListener(listener); | |
| 658 } | 351 } |
| 659 | 352 |
| 660 void PresentationServiceDelegateImpl::Reset(int render_process_id, | 353 void PresentationServiceDelegateImpl::Reset(int render_process_id, |
| 661 int render_frame_id) { | 354 int render_frame_id) { |
| 662 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 355 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 663 frame_manager_->Reset(render_frame_host_id); | 356 const auto it = presentation_frames_.find(render_frame_host_id); |
| 357 if (it != presentation_frames_.end()) { | |
| 358 it->second->Reset(); | |
| 359 presentation_frames_.erase(it); | |
| 360 } | |
| 361 | |
| 362 if (default_presentation_request_ && | |
| 363 render_frame_host_id == | |
| 364 default_presentation_request_->render_frame_host_id()) { | |
| 365 ClearDefaultPresentationRequest(); | |
| 366 } | |
| 664 } | 367 } |
| 665 | 368 |
| 666 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( | 369 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( |
| 667 int render_process_id, | 370 int render_process_id, |
| 668 int render_frame_id, | 371 int render_frame_id, |
| 669 const std::vector<GURL>& default_presentation_urls, | 372 const std::vector<GURL>& default_presentation_urls, |
| 670 const content::PresentationConnectionCallback& callback) { | 373 const content::PresentationConnectionCallback& callback) { |
| 671 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 374 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 672 frame_manager_->SetDefaultPresentationUrls( | 375 if (!IsMainFrame(render_frame_host_id)) |
| 673 render_frame_host_id, default_presentation_urls, callback); | 376 return; |
| 377 | |
| 378 if (default_presentation_urls.empty()) { | |
| 379 ClearDefaultPresentationRequest(); | |
|
zhaobin
2017/07/05 23:18:10
nit: return here so we do not need else?
imcheng
2017/07/06 00:34:28
Done.
| |
| 380 } else { | |
| 381 DCHECK(!callback.is_null()); | |
| 382 auto frame_origin = GetLastCommittedURLForFrame(render_frame_host_id); | |
| 383 PresentationRequest request(render_frame_host_id, default_presentation_urls, | |
| 384 frame_origin); | |
| 385 default_presentation_started_callback_ = callback; | |
| 386 SetDefaultPresentationRequest(request); | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 PresentationFrame* PresentationServiceDelegateImpl::GetOrAddPresentationFrame( | |
| 391 const RenderFrameHostId& render_frame_host_id) { | |
| 392 auto& presentation_frame = presentation_frames_[render_frame_host_id]; | |
| 393 if (!presentation_frame) { | |
| 394 presentation_frame.reset( | |
| 395 new PresentationFrame(render_frame_host_id, web_contents_, router_)); | |
| 396 } | |
| 397 return presentation_frame.get(); | |
| 674 } | 398 } |
| 675 | 399 |
| 676 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 400 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| 677 int render_process_id, | 401 int render_process_id, |
| 678 int render_frame_id, | 402 int render_frame_id, |
| 679 const GURL& presentation_url, | 403 const GURL& presentation_url, |
| 680 const std::string& presentation_id, | 404 const std::string& presentation_id, |
| 681 const content::PresentationConnectionCallback& success_cb, | 405 const content::PresentationConnectionCallback& success_cb, |
| 682 const content::PresentationConnectionErrorCallback& error_cb, | 406 const content::PresentationConnectionErrorCallback& error_cb, |
| 683 const RouteRequestResult& result) { | 407 const RouteRequestResult& result) { |
| 684 if (!result.route()) { | 408 if (!result.route()) { |
| 685 error_cb.Run(content::PresentationError( | 409 error_cb.Run(content::PresentationError( |
| 686 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error())); | 410 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error())); |
| 687 } else { | 411 } else { |
| 688 DVLOG(1) << "OnJoinRouteResponse: " | 412 DVLOG(1) << "OnJoinRouteResponse: " |
| 689 << "route_id: " << result.route()->media_route_id() | 413 << "route_id: " << result.route()->media_route_id() |
| 690 << ", presentation URL: " << presentation_url | 414 << ", presentation URL: " << presentation_url |
| 691 << ", presentation ID: " << presentation_id; | 415 << ", presentation ID: " << presentation_id; |
| 692 DCHECK_EQ(presentation_id, result.presentation_id()); | 416 DCHECK_EQ(presentation_id, result.presentation_id()); |
| 693 content::PresentationInfo presentation_info(presentation_url, | 417 content::PresentationInfo presentation_info(presentation_url, |
| 694 result.presentation_id()); | 418 result.presentation_id()); |
| 695 frame_manager_->OnPresentationConnection( | 419 AddPresentation(RenderFrameHostId(render_process_id, render_frame_id), |
| 696 RenderFrameHostId(render_process_id, render_frame_id), | 420 presentation_info, *result.route()); |
| 697 presentation_info, *result.route()); | |
| 698 success_cb.Run(presentation_info); | 421 success_cb.Run(presentation_info); |
| 699 } | 422 } |
| 700 } | 423 } |
| 701 | 424 |
| 702 void PresentationServiceDelegateImpl::OnStartPresentationSucceeded( | 425 void PresentationServiceDelegateImpl::OnStartPresentationSucceeded( |
| 703 int render_process_id, | 426 int render_process_id, |
| 704 int render_frame_id, | 427 int render_frame_id, |
| 705 const content::PresentationConnectionCallback& success_cb, | 428 const content::PresentationConnectionCallback& success_cb, |
| 706 const content::PresentationInfo& new_presentation_info, | 429 const content::PresentationInfo& new_presentation_info, |
| 707 const MediaRoute& route) { | 430 const MediaRoute& route) { |
| 708 DVLOG(1) << "OnStartPresentationSucceeded: " | 431 DVLOG(1) << "OnStartPresentationSucceeded: " |
| 709 << "route_id: " << route.media_route_id() | 432 << "route_id: " << route.media_route_id() |
| 710 << ", presentation URL: " << new_presentation_info.presentation_url | 433 << ", presentation URL: " << new_presentation_info.presentation_url |
| 711 << ", presentation ID: " << new_presentation_info.presentation_id; | 434 << ", presentation ID: " << new_presentation_info.presentation_id; |
| 712 frame_manager_->OnPresentationConnection( | 435 AddPresentation(RenderFrameHostId(render_process_id, render_frame_id), |
| 713 RenderFrameHostId(render_process_id, render_frame_id), | 436 new_presentation_info, route); |
| 714 new_presentation_info, route); | |
| 715 success_cb.Run(new_presentation_info); | 437 success_cb.Run(new_presentation_info); |
| 716 } | 438 } |
| 717 | 439 |
| 440 void PresentationServiceDelegateImpl::AddPresentation( | |
| 441 const RenderFrameHostId& render_frame_host_id, | |
| 442 const content::PresentationInfo& presentation_info, | |
| 443 const MediaRoute& route) { | |
| 444 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
| 445 presentation_frame->AddPresentation(presentation_info, route); | |
| 446 } | |
| 447 | |
| 448 void PresentationServiceDelegateImpl::RemovePresentation( | |
| 449 const RenderFrameHostId& render_frame_host_id, | |
| 450 const MediaRoute::Id& route_id, | |
| 451 const std::string& presentation_id) { | |
|
zhaobin
2017/07/05 23:18:10
Shall we change parameter order here as well to be
imcheng
2017/07/06 00:34:28
Good catch. I removed the route_id parameter since
| |
| 452 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 453 if (it != presentation_frames_.end()) | |
| 454 it->second->RemovePresentation(route_id, presentation_id); | |
|
zhaobin
2017/07/05 23:18:10
Shall we change order of parameter to RemovePresen
imcheng
2017/07/06 00:34:28
Done.
| |
| 455 } | |
| 456 | |
| 718 void PresentationServiceDelegateImpl::StartPresentation( | 457 void PresentationServiceDelegateImpl::StartPresentation( |
| 719 int render_process_id, | 458 int render_process_id, |
| 720 int render_frame_id, | 459 int render_frame_id, |
| 721 const std::vector<GURL>& presentation_urls, | 460 const std::vector<GURL>& presentation_urls, |
| 722 const content::PresentationConnectionCallback& success_cb, | 461 const content::PresentationConnectionCallback& success_cb, |
| 723 const content::PresentationConnectionErrorCallback& error_cb) { | 462 const content::PresentationConnectionErrorCallback& error_cb) { |
| 724 if (presentation_urls.empty()) { | 463 if (presentation_urls.empty()) { |
| 725 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 464 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 726 "Invalid presentation arguments.")); | 465 "Invalid presentation arguments.")); |
| 727 return; | 466 return; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 738 return; | 477 return; |
| 739 } | 478 } |
| 740 | 479 |
| 741 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 480 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 742 std::unique_ptr<CreatePresentationConnectionRequest> request( | 481 std::unique_ptr<CreatePresentationConnectionRequest> request( |
| 743 new CreatePresentationConnectionRequest( | 482 new CreatePresentationConnectionRequest( |
| 744 render_frame_host_id, presentation_urls, | 483 render_frame_host_id, presentation_urls, |
| 745 GetLastCommittedURLForFrame(render_frame_host_id), | 484 GetLastCommittedURLForFrame(render_frame_host_id), |
| 746 base::Bind( | 485 base::Bind( |
| 747 &PresentationServiceDelegateImpl::OnStartPresentationSucceeded, | 486 &PresentationServiceDelegateImpl::OnStartPresentationSucceeded, |
| 748 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 487 GetWeakPtr(), render_process_id, render_frame_id, success_cb), |
| 749 success_cb), | |
| 750 error_cb)); | 488 error_cb)); |
| 751 MediaRouterDialogController* controller = | 489 MediaRouterDialogController* controller = |
| 752 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); | 490 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); |
| 753 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { | 491 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { |
| 754 LOG(ERROR) | 492 LOG(ERROR) |
| 755 << "Media router dialog already exists. Ignoring StartPresentation."; | 493 << "Media router dialog already exists. Ignoring StartPresentation."; |
| 756 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 494 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| 757 "Unable to create dialog.")); | 495 "Unable to create dialog.")); |
| 758 return; | 496 return; |
| 759 } | 497 } |
| 760 } | 498 } |
| 761 | 499 |
| 762 void PresentationServiceDelegateImpl::ReconnectPresentation( | 500 void PresentationServiceDelegateImpl::ReconnectPresentation( |
| 763 int render_process_id, | 501 int render_process_id, |
| 764 int render_frame_id, | 502 int render_frame_id, |
| 765 const std::vector<GURL>& presentation_urls, | 503 const std::vector<GURL>& presentation_urls, |
| 766 const std::string& presentation_id, | 504 const std::string& presentation_id, |
| 767 const content::PresentationConnectionCallback& success_cb, | 505 const content::PresentationConnectionCallback& success_cb, |
| 768 const content::PresentationConnectionErrorCallback& error_cb) { | 506 const content::PresentationConnectionErrorCallback& error_cb) { |
| 769 DVLOG(2) << "PresentationServiceDelegateImpl::ReconnectPresentation"; | 507 DVLOG(2) << "PresentationServiceDelegateImpl::ReconnectPresentation"; |
| 770 if (presentation_urls.empty()) { | 508 if (presentation_urls.empty()) { |
| 771 error_cb.Run(content::PresentationError( | 509 error_cb.Run(content::PresentationError( |
| 772 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 510 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |
| 773 "Invalid presentation arguments.")); | 511 "Invalid presentation arguments.")); |
| 774 return; | 512 return; |
| 775 } | 513 } |
| 776 | 514 |
| 777 const url::Origin& origin = GetLastCommittedURLForFrame( | 515 auto origin = GetLastCommittedURLForFrame( |
| 778 RenderFrameHostId(render_process_id, render_frame_id)); | 516 RenderFrameHostId(render_process_id, render_frame_id)); |
| 779 | 517 |
| 780 #if !defined(OS_ANDROID) | 518 #if !defined(OS_ANDROID) |
| 781 if (IsAutoJoinPresentationId(presentation_id) && | 519 if (IsAutoJoinPresentationId(presentation_id) && |
| 782 ShouldCancelAutoJoinForOrigin(origin)) { | 520 ShouldCancelAutoJoinForOrigin(origin)) { |
| 783 error_cb.Run(content::PresentationError( | 521 error_cb.Run(content::PresentationError( |
| 784 content::PRESENTATION_ERROR_PRESENTATION_REQUEST_CANCELLED, | 522 content::PRESENTATION_ERROR_PRESENTATION_REQUEST_CANCELLED, |
| 785 "Auto-join request cancelled by user preferences.")); | 523 "Auto-join request cancelled by user preferences.")); |
| 786 return; | 524 return; |
| 787 } | 525 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 810 auto result = RouteRequestResult::FromSuccess(*route, presentation_id); | 548 auto result = RouteRequestResult::FromSuccess(*route, presentation_id); |
| 811 OnJoinRouteResponse(render_process_id, render_frame_id, | 549 OnJoinRouteResponse(render_process_id, render_frame_id, |
| 812 presentation_urls[0], presentation_id, success_cb, | 550 presentation_urls[0], presentation_id, success_cb, |
| 813 error_cb, *result); | 551 error_cb, *result); |
| 814 } else { | 552 } else { |
| 815 // TODO(crbug.com/627655): Handle multiple URLs. | 553 // TODO(crbug.com/627655): Handle multiple URLs. |
| 816 const GURL& presentation_url = presentation_urls[0]; | 554 const GURL& presentation_url = presentation_urls[0]; |
| 817 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 555 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| 818 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 556 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 819 route_response_callbacks.push_back(base::BindOnce( | 557 route_response_callbacks.push_back(base::BindOnce( |
| 820 &PresentationServiceDelegateImpl::OnJoinRouteResponse, | 558 &PresentationServiceDelegateImpl::OnJoinRouteResponse, GetWeakPtr(), |
| 821 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 559 render_process_id, render_frame_id, presentation_url, presentation_id, |
| 822 presentation_url, presentation_id, success_cb, error_cb)); | 560 success_cb, error_cb)); |
| 823 router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), | 561 router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), |
| 824 presentation_id, origin, web_contents_, | 562 presentation_id, origin, web_contents_, |
| 825 std::move(route_response_callbacks), base::TimeDelta(), | 563 std::move(route_response_callbacks), base::TimeDelta(), |
| 826 incognito); | 564 incognito); |
| 827 } | 565 } |
| 828 } | 566 } |
| 829 | 567 |
| 830 void PresentationServiceDelegateImpl::CloseConnection( | 568 void PresentationServiceDelegateImpl::CloseConnection( |
| 831 int render_process_id, | 569 int render_process_id, |
| 832 int render_frame_id, | 570 int render_frame_id, |
| 833 const std::string& presentation_id) { | 571 const std::string& presentation_id) { |
| 834 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 572 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 835 const MediaRoute::Id& route_id = | 573 auto route_id = GetRouteId(rfh_id, presentation_id); |
| 836 frame_manager_->GetRouteId(rfh_id, presentation_id); | |
| 837 if (route_id.empty()) { | 574 if (route_id.empty()) { |
| 838 DVLOG(1) << "No active route for: " << presentation_id; | 575 DVLOG(1) << "No active route for: " << presentation_id; |
| 839 return; | 576 return; |
| 840 } | 577 } |
| 841 | 578 |
| 842 auto* offscreen_presentation_manager = | 579 auto* offscreen_presentation_manager = |
| 843 OffscreenPresentationManagerFactory::GetOrCreateForWebContents( | 580 OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| 844 web_contents_); | 581 web_contents_); |
| 845 | 582 |
| 846 if (offscreen_presentation_manager->IsOffscreenPresentation( | 583 if (offscreen_presentation_manager->IsOffscreenPresentation( |
| 847 presentation_id)) { | 584 presentation_id)) { |
| 848 offscreen_presentation_manager->UnregisterOffscreenPresentationController( | 585 offscreen_presentation_manager->UnregisterOffscreenPresentationController( |
| 849 presentation_id, rfh_id); | 586 presentation_id, rfh_id); |
| 850 } else { | 587 } else { |
| 851 router_->DetachRoute(route_id); | 588 router_->DetachRoute(route_id); |
| 852 } | 589 } |
| 853 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); | 590 RemovePresentation(rfh_id, presentation_id, route_id); |
| 854 // TODO(mfoltz): close() should always succeed so there is no need to keep the | 591 // TODO(mfoltz): close() should always succeed so there is no need to keep the |
| 855 // state_changed_cb around - remove it and fire the ChangeEvent on the | 592 // state_changed_cb around - remove it and fire the ChangeEvent on the |
| 856 // PresentationConnection in Blink. | 593 // PresentationConnection in Blink. |
| 857 } | 594 } |
| 858 | 595 |
| 859 void PresentationServiceDelegateImpl::Terminate( | 596 void PresentationServiceDelegateImpl::Terminate( |
| 860 int render_process_id, | 597 int render_process_id, |
| 861 int render_frame_id, | 598 int render_frame_id, |
| 862 const std::string& presentation_id) { | 599 const std::string& presentation_id) { |
| 863 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 600 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 864 const MediaRoute::Id& route_id = | 601 auto route_id = GetRouteId(rfh_id, presentation_id); |
| 865 frame_manager_->GetRouteId(rfh_id, presentation_id); | |
| 866 if (route_id.empty()) { | 602 if (route_id.empty()) { |
| 867 DVLOG(1) << "No active route for: " << presentation_id; | 603 DVLOG(1) << "No active route for: " << presentation_id; |
| 868 return; | 604 return; |
| 869 } | 605 } |
| 870 router_->TerminateRoute(route_id); | 606 router_->TerminateRoute(route_id); |
| 871 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); | 607 RemovePresentation(rfh_id, presentation_id, route_id); |
| 872 } | 608 } |
| 873 | 609 |
| 874 void PresentationServiceDelegateImpl::SendMessage( | 610 void PresentationServiceDelegateImpl::SendMessage( |
| 875 int render_process_id, | 611 int render_process_id, |
| 876 int render_frame_id, | 612 int render_frame_id, |
| 877 const content::PresentationInfo& presentation_info, | 613 const content::PresentationInfo& presentation_info, |
| 878 content::PresentationConnectionMessage message, | 614 content::PresentationConnectionMessage message, |
| 879 SendMessageCallback send_message_cb) { | 615 SendMessageCallback send_message_cb) { |
| 880 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 616 auto route_id = |
| 881 RenderFrameHostId(render_process_id, render_frame_id), | 617 GetRouteId(RenderFrameHostId(render_process_id, render_frame_id), |
| 882 presentation_info.presentation_id); | 618 presentation_info.presentation_id); |
| 883 if (route_id.empty()) { | 619 if (route_id.empty()) { |
| 884 DVLOG(1) << "No active route for " << presentation_info.presentation_id; | 620 DVLOG(1) << "No active route for " << presentation_info.presentation_id; |
| 885 std::move(send_message_cb).Run(false); | 621 std::move(send_message_cb).Run(false); |
| 886 return; | 622 return; |
| 887 } | 623 } |
| 888 | 624 |
| 889 if (message.is_binary()) { | 625 if (message.is_binary()) { |
| 890 router_->SendRouteBinaryMessage( | 626 router_->SendRouteBinaryMessage( |
| 891 route_id, | 627 route_id, |
| 892 base::MakeUnique<std::vector<uint8_t>>(std::move(message.data.value())), | 628 base::MakeUnique<std::vector<uint8_t>>(std::move(message.data.value())), |
| 893 std::move(send_message_cb)); | 629 std::move(send_message_cb)); |
| 894 } else { | 630 } else { |
| 895 router_->SendRouteMessage(route_id, message.message.value(), | 631 router_->SendRouteMessage(route_id, message.message.value(), |
| 896 std::move(send_message_cb)); | 632 std::move(send_message_cb)); |
| 897 } | 633 } |
| 898 } | 634 } |
| 899 | 635 |
| 900 void PresentationServiceDelegateImpl::ListenForConnectionStateChange( | 636 void PresentationServiceDelegateImpl::ListenForConnectionStateChange( |
| 901 int render_process_id, | 637 int render_process_id, |
| 902 int render_frame_id, | 638 int render_frame_id, |
| 903 const content::PresentationInfo& connection, | 639 const content::PresentationInfo& connection, |
| 904 const content::PresentationConnectionStateChangedCallback& | 640 const content::PresentationConnectionStateChangedCallback& |
| 905 state_changed_cb) { | 641 state_changed_cb) { |
| 906 frame_manager_->ListenForConnectionStateChange( | 642 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 907 RenderFrameHostId(render_process_id, render_frame_id), connection, | 643 const auto it = presentation_frames_.find(render_frame_host_id); |
| 908 state_changed_cb); | 644 if (it != presentation_frames_.end()) |
| 645 it->second->ListenForConnectionStateChange(connection, state_changed_cb); | |
| 909 } | 646 } |
| 910 | 647 |
| 911 void PresentationServiceDelegateImpl::ConnectToPresentation( | 648 void PresentationServiceDelegateImpl::ConnectToPresentation( |
| 912 int render_process_id, | 649 int render_process_id, |
| 913 int render_frame_id, | 650 int render_frame_id, |
| 914 const content::PresentationInfo& presentation_info, | 651 const content::PresentationInfo& presentation_info, |
| 915 content::PresentationConnectionPtr controller_connection_ptr, | 652 content::PresentationConnectionPtr controller_connection_ptr, |
| 916 content::PresentationConnectionRequest receiver_connection_request) { | 653 content::PresentationConnectionRequest receiver_connection_request) { |
| 917 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 654 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 918 frame_manager_->ConnectToPresentation(render_frame_host_id, presentation_info, | 655 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 919 std::move(controller_connection_ptr), | 656 presentation_frame->ConnectToPresentation( |
| 920 std::move(receiver_connection_request)); | 657 presentation_info, std::move(controller_connection_ptr), |
| 658 std::move(receiver_connection_request)); | |
| 921 } | 659 } |
| 922 | 660 |
| 923 void PresentationServiceDelegateImpl::OnRouteResponse( | 661 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 924 const PresentationRequest& presentation_request, | 662 const PresentationRequest& presentation_request, |
| 925 const RouteRequestResult& result) { | 663 const RouteRequestResult& result) { |
| 926 if (!result.route() || | 664 if (!result.route() || |
| 927 !base::ContainsValue(presentation_request.presentation_urls(), | 665 !base::ContainsValue(presentation_request.presentation_urls(), |
| 928 result.presentation_url())) { | 666 result.presentation_url())) { |
| 929 return; | 667 return; |
| 930 } | 668 } |
| 931 | 669 |
| 932 content::PresentationInfo presentation_info(result.presentation_url(), | 670 content::PresentationInfo presentation_info(result.presentation_url(), |
| 933 result.presentation_id()); | 671 result.presentation_id()); |
| 934 frame_manager_->OnDefaultPresentationStarted( | 672 AddPresentation(presentation_request.render_frame_host_id(), |
| 935 presentation_request, presentation_info, *result.route()); | 673 presentation_info, *result.route()); |
| 674 if (default_presentation_request_ && | |
| 675 default_presentation_request_->Equals(presentation_request)) { | |
| 676 default_presentation_started_callback_.Run(presentation_info); | |
| 677 } | |
| 936 } | 678 } |
| 937 | 679 |
| 938 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( | 680 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |
| 939 DefaultPresentationRequestObserver* observer) { | 681 DefaultPresentationRequestObserver* observer) { |
| 940 frame_manager_->AddDefaultPresentationRequestObserver(observer); | 682 default_presentation_request_observers_.AddObserver(observer); |
| 941 } | 683 } |
| 942 | 684 |
| 943 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( | 685 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |
| 944 DefaultPresentationRequestObserver* observer) { | 686 DefaultPresentationRequestObserver* observer) { |
| 945 frame_manager_->RemoveDefaultPresentationRequestObserver(observer); | 687 default_presentation_request_observers_.RemoveObserver(observer); |
| 946 } | 688 } |
| 947 | 689 |
| 948 PresentationRequest | 690 PresentationRequest |
| 949 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const { | 691 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const { |
| 950 DCHECK(HasDefaultPresentationRequest()); | 692 DCHECK(HasDefaultPresentationRequest()); |
| 951 return *frame_manager_->default_presentation_request(); | 693 return *default_presentation_request_; |
| 952 } | 694 } |
| 953 | 695 |
| 954 bool PresentationServiceDelegateImpl::HasDefaultPresentationRequest() const { | 696 bool PresentationServiceDelegateImpl::HasDefaultPresentationRequest() const { |
| 955 return frame_manager_->default_presentation_request() != nullptr; | 697 return default_presentation_request_ != nullptr; |
| 956 } | 698 } |
| 957 | 699 |
| 958 base::WeakPtr<PresentationServiceDelegateImpl> | 700 base::WeakPtr<PresentationServiceDelegateImpl> |
| 959 PresentationServiceDelegateImpl::GetWeakPtr() { | 701 PresentationServiceDelegateImpl::GetWeakPtr() { |
| 960 return weak_factory_.GetWeakPtr(); | 702 return weak_factory_.GetWeakPtr(); |
| 961 } | 703 } |
| 962 | 704 |
| 963 void PresentationServiceDelegateImpl::SetMediaRouterForTest( | 705 void PresentationServiceDelegateImpl::SetMediaRouterForTest( |
| 964 MediaRouter* router) { | 706 MediaRouter* router) { |
| 965 router_ = router; | 707 router_ = router; |
| 966 frame_manager_->SetMediaRouterForTest(router); | |
| 967 } | 708 } |
| 968 | 709 |
| 969 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 710 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 970 int render_process_id, | 711 int render_process_id, |
| 971 int render_frame_id, | 712 int render_frame_id, |
| 972 const MediaSource::Id& source_id) const { | 713 const MediaSource::Id& source_id) const { |
| 973 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 714 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 974 return frame_manager_->HasScreenAvailabilityListenerForTest( | 715 const auto it = presentation_frames_.find(render_frame_host_id); |
| 975 render_frame_host_id, source_id); | 716 return it != presentation_frames_.end() && |
| 717 it->second->HasScreenAvailabilityListenerForTest(source_id); | |
| 718 } | |
| 719 | |
| 720 void PresentationServiceDelegateImpl::SetDefaultPresentationRequest( | |
| 721 const PresentationRequest& default_presentation_request) { | |
| 722 if (default_presentation_request_ && | |
| 723 default_presentation_request_->Equals(default_presentation_request)) | |
| 724 return; | |
| 725 | |
| 726 default_presentation_request_.reset( | |
| 727 new PresentationRequest(default_presentation_request)); | |
| 728 for (auto& observer : default_presentation_request_observers_) | |
| 729 observer.OnDefaultPresentationChanged(*default_presentation_request_); | |
| 730 } | |
| 731 | |
| 732 void PresentationServiceDelegateImpl::ClearDefaultPresentationRequest() { | |
| 733 default_presentation_started_callback_.Reset(); | |
| 734 if (!default_presentation_request_) | |
| 735 return; | |
| 736 | |
| 737 default_presentation_request_.reset(); | |
| 738 for (auto& observer : default_presentation_request_observers_) | |
| 739 observer.OnDefaultPresentationRemoved(); | |
| 740 } | |
| 741 | |
| 742 // TODO(imcheng): Move this check to PresentationServiceImpl. | |
| 743 bool PresentationServiceDelegateImpl::IsMainFrame( | |
| 744 const RenderFrameHostId& render_frame_host_id) const { | |
| 745 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); | |
| 746 return main_frame && GetRenderFrameHostId(main_frame) == render_frame_host_id; | |
| 747 } | |
| 748 | |
| 749 MediaRoute::Id PresentationServiceDelegateImpl::GetRouteId( | |
| 750 const RenderFrameHostId& render_frame_host_id, | |
| 751 const std::string& presentation_id) const { | |
| 752 const auto it = presentation_frames_.find(render_frame_host_id); | |
| 753 return it != presentation_frames_.end() | |
| 754 ? it->second->GetRouteId(presentation_id) | |
| 755 : MediaRoute::Id(); | |
| 976 } | 756 } |
| 977 | 757 |
| 978 #if !defined(OS_ANDROID) | 758 #if !defined(OS_ANDROID) |
| 979 bool PresentationServiceDelegateImpl::ShouldCancelAutoJoinForOrigin( | 759 bool PresentationServiceDelegateImpl::ShouldCancelAutoJoinForOrigin( |
| 980 const url::Origin& origin) const { | 760 const url::Origin& origin) const { |
| 981 const base::ListValue* origins = | 761 const base::ListValue* origins = |
| 982 Profile::FromBrowserContext(web_contents_->GetBrowserContext()) | 762 Profile::FromBrowserContext(web_contents_->GetBrowserContext()) |
| 983 ->GetPrefs() | 763 ->GetPrefs() |
| 984 ->GetList(prefs::kMediaRouterTabMirroringSources); | 764 ->GetList(prefs::kMediaRouterTabMirroringSources); |
| 985 return origins && | 765 return origins && |
| 986 origins->Find(base::Value(origin.Serialize())) != origins->end(); | 766 origins->Find(base::Value(origin.Serialize())) != origins->end(); |
| 987 } | 767 } |
| 988 #endif // !defined(OS_ANDROID) | 768 #endif // !defined(OS_ANDROID) |
| 989 | 769 |
| 990 } // namespace media_router | 770 } // namespace media_router |
| OLD | NEW |