Chromium Code Reviews| Index: chrome/browser/media/router/presentation_service_delegate_impl.cc |
| diff --git a/chrome/browser/media/router/presentation_service_delegate_impl.cc b/chrome/browser/media/router/presentation_service_delegate_impl.cc |
| index 2cd8f5054c9af2c1cc13e1c7fec1623d186e01a7..f7e797196e70bf46c43e8f8cd6f5b04f5277704c 100644 |
| --- a/chrome/browser/media/router/presentation_service_delegate_impl.cc |
| +++ b/chrome/browser/media/router/presentation_service_delegate_impl.cc |
| @@ -155,14 +155,13 @@ class PresentationFrame { |
| void RemoveConnection(const std::string& presentation_id, |
| const MediaRoute::Id& route_id); |
| - const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| + const MediaRoute* GetRoute(const std::string& presentation_id) const; |
| void OnPresentationSessionStarted( |
| const content::PresentationSessionInfo& session, |
| const MediaRoute& route); |
| void OnPresentationServiceDelegateDestroyed() const; |
| - bool IsOffscreenPresentation(const std::string& presentation_id) const; |
| void ConnectToPresentation( |
| const content::PresentationSessionInfo& session, |
| content::PresentationConnectionPtr controller_connection_ptr, |
| @@ -192,6 +191,7 @@ class PresentationFrame { |
| // References to the owning WebContents, and the corresponding MediaRouter. |
| content::WebContents* web_contents_; |
| MediaRouter* router_; |
| + OffscreenPresentationManager* offscreen_presentation_manager_; |
| }; |
| PresentationFrame::PresentationFrame( |
| @@ -200,7 +200,10 @@ PresentationFrame::PresentationFrame( |
| MediaRouter* router) |
| : render_frame_host_id_(render_frame_host_id), |
| web_contents_(web_contents), |
| - router_(router) { |
| + router_(router), |
| + offscreen_presentation_manager_( |
| + OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| + web_contents_)) { |
| DCHECK(web_contents_); |
| DCHECK(router_); |
| } |
| @@ -213,13 +216,18 @@ void PresentationFrame::OnPresentationSessionStarted( |
| const MediaRoute& route) { |
| presentation_id_to_route_.insert( |
| std::make_pair(session.presentation_id, route)); |
| + |
| + if (route.is_offscreen_presentation() && |
| + !offscreen_presentation_manager_->GetRoute(session.presentation_id)) { |
| + offscreen_presentation_manager_->RegisterOffscreenPresentationRoute( |
| + session.presentation_id, session.presentation_url, route); |
| + } |
| } |
| -const MediaRoute::Id PresentationFrame::GetRouteId( |
| +const MediaRoute* PresentationFrame::GetRoute( |
| const std::string& presentation_id) const { |
| auto it = presentation_id_to_route_.find(presentation_id); |
| - return it != presentation_id_to_route_.end() ? it->second.media_route_id() |
| - : ""; |
| + return it != presentation_id_to_route_.end() ? &it->second : nullptr; |
| } |
| bool PresentationFrame::SetScreenAvailabilityListener( |
| @@ -260,16 +268,14 @@ bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
| } |
| void PresentationFrame::Reset() { |
| - auto* offscreen_presentation_manager = |
| - OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| - web_contents_); |
| - |
| for (const auto& pid_route : presentation_id_to_route_) { |
| - if (IsOffscreenPresentation(pid_route.first)) { |
| - offscreen_presentation_manager->UnregisterOffscreenPresentationController( |
| - pid_route.first, render_frame_host_id_); |
| + if (pid_route.second.is_offscreen_presentation()) { |
| + offscreen_presentation_manager_ |
| + ->UnregisterOffscreenPresentationController(pid_route.first, |
| + render_frame_host_id_); |
| + } else { |
| + router_->DetachRoute(pid_route.second.media_route_id()); |
| } |
| - router_->DetachRoute(pid_route.second.media_route_id()); |
| } |
| presentation_id_to_route_.clear(); |
| @@ -355,17 +361,6 @@ MediaSource PresentationFrame::GetMediaSourceFromListener( |
| : MediaSourceForPresentationUrl(listener->GetAvailabilityUrl()); |
| } |
| -bool PresentationFrame::IsOffscreenPresentation( |
| - const std::string& presentation_id) const { |
| - const auto it = presentation_id_to_route_.find(presentation_id); |
| - if (it == presentation_id_to_route_.end()) { |
| - DLOG(WARNING) << "No route for [presentation_id]: " << presentation_id; |
| - return false; |
| - } |
| - |
| - return it->second.is_offscreen_presentation(); |
| -} |
| - |
| void PresentationFrame::ConnectToPresentation( |
| const content::PresentationSessionInfo& session, |
| content::PresentationConnectionPtr controller_connection_ptr, |
| @@ -379,11 +374,8 @@ void PresentationFrame::ConnectToPresentation( |
| return; |
| } |
| - if (IsOffscreenPresentation(session.presentation_id)) { |
| - auto* const offscreen_presentation_manager = |
| - OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| - web_contents_); |
| - offscreen_presentation_manager->RegisterOffscreenPresentationController( |
| + if (pid_route_it->second.is_offscreen_presentation()) { |
| + offscreen_presentation_manager_->RegisterOffscreenPresentationController( |
| session.presentation_id, session.presentation_url, |
| render_frame_host_id_, std::move(controller_connection_ptr), |
| std::move(receiver_connection_request)); |
| @@ -465,8 +457,8 @@ class PresentationFrameManager { |
| content::PresentationConnectionPtr controller_connection_ptr, |
| content::PresentationConnectionRequest receiver_connection_request); |
| - const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, |
| - const std::string& presentation_id) const; |
| + const MediaRoute* GetRoute(const RenderFrameHostId& render_frame_host_id, |
| + const std::string& presentation_id) const; |
| const PresentationRequest* default_presentation_request() const { |
| return default_presentation_request_.get(); |
| @@ -553,12 +545,13 @@ void PresentationFrameManager::ConnectToPresentation( |
| std::move(receiver_connection_request)); |
| } |
| -const MediaRoute::Id PresentationFrameManager::GetRouteId( |
| +const MediaRoute* PresentationFrameManager::GetRoute( |
| const RenderFrameHostId& render_frame_host_id, |
| const std::string& presentation_id) const { |
| const auto it = presentation_frames_.find(render_frame_host_id); |
| return it != presentation_frames_.end() |
| - ? it->second->GetRouteId(presentation_id) : MediaRoute::Id(); |
| + ? it->second->GetRoute(presentation_id) |
| + : nullptr; |
| } |
| bool PresentationFrameManager::SetScreenAvailabilityListener( |
| @@ -890,17 +883,31 @@ void PresentationServiceDelegateImpl::JoinSession( |
| } |
| #endif // !defined(OS_ANDROID) |
| - // TODO(crbug.com/627655): Handle multiple URLs. |
| - const GURL& presentation_url = presentation_urls[0]; |
| - bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| - std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| - route_response_callbacks.push_back( |
| - base::Bind(&PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| - weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| - presentation_url, presentation_id, success_cb, error_cb)); |
| - router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), |
| - presentation_id, origin, web_contents_, |
| - route_response_callbacks, base::TimeDelta(), incognito); |
| + auto* offscreen_presentation_manager = |
| + OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| + web_contents_); |
| + // Check offscreen presentation across frames. |
| + if (offscreen_presentation_manager->IsOffscreenPresentation( |
| + presentation_id)) { |
| + auto* route = offscreen_presentation_manager->GetRoute(presentation_id); |
| + DCHECK(route); |
| + auto result = RouteRequestResult::FromSuccess(*route, presentation_id); |
| + OnJoinRouteResponse(render_process_id, render_frame_id, |
| + presentation_urls[0], presentation_id, success_cb, |
|
mark a. foltz
2017/03/01 06:26:32
Connection requires matching both presentation ID
zhaobin
2017/03/15 18:18:57
Done.
|
| + error_cb, *result); |
| + } else { |
| + // TODO(crbug.com/627655): Handle multiple URLs. |
| + const GURL& presentation_url = presentation_urls[0]; |
| + bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |
| + std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| + route_response_callbacks.push_back(base::Bind( |
| + &PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| + weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |
| + presentation_url, presentation_id, success_cb, error_cb)); |
| + router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), |
| + presentation_id, origin, web_contents_, |
| + route_response_callbacks, base::TimeDelta(), incognito); |
| + } |
| } |
| void PresentationServiceDelegateImpl::CloseConnection( |
| @@ -908,15 +915,23 @@ void PresentationServiceDelegateImpl::CloseConnection( |
| int render_frame_id, |
| const std::string& presentation_id) { |
| const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| - const MediaRoute::Id& route_id = |
| - frame_manager_->GetRouteId(rfh_id, presentation_id); |
| - if (route_id.empty()) { |
| + auto* route = frame_manager_->GetRoute(rfh_id, presentation_id); |
| + if (!route) { |
| DVLOG(1) << "No active route for: " << presentation_id; |
| return; |
| } |
| - router_->DetachRoute(route_id); |
| - frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); |
| + if (route->is_offscreen_presentation()) { |
| + auto* offscreen_presentation_manager = |
| + OffscreenPresentationManagerFactory::GetOrCreateForWebContents( |
| + web_contents_); |
| + offscreen_presentation_manager->UnregisterOffscreenPresentationController( |
|
imcheng
2017/02/24 01:37:46
Do we currently change state on close() for 1-UA o
zhaobin
2017/02/24 02:35:34
Have a different patch for change state on close()
|
| + presentation_id, rfh_id); |
| + } else { |
| + router_->DetachRoute(route->media_route_id()); |
| + } |
| + frame_manager_->RemoveConnection(rfh_id, presentation_id, |
| + route->media_route_id()); |
| // TODO(mfoltz): close() should always succeed so there is no need to keep the |
| // state_changed_cb around - remove it and fire the ChangeEvent on the |
| // PresentationConnection in Blink. |
| @@ -927,14 +942,14 @@ void PresentationServiceDelegateImpl::Terminate( |
| int render_frame_id, |
| const std::string& presentation_id) { |
| const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| - const MediaRoute::Id& route_id = |
| - frame_manager_->GetRouteId(rfh_id, presentation_id); |
| - if (route_id.empty()) { |
| + auto* route = frame_manager_->GetRoute(rfh_id, presentation_id); |
| + if (!route) { |
| DVLOG(1) << "No active route for: " << presentation_id; |
| return; |
| } |
| - router_->TerminateRoute(route_id); |
| - frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); |
| + router_->TerminateRoute(route->media_route_id()); |
| + frame_manager_->RemoveConnection(rfh_id, presentation_id, |
| + route->media_route_id()); |
| } |
| void PresentationServiceDelegateImpl::ListenForConnectionMessages( |
| @@ -953,20 +968,21 @@ void PresentationServiceDelegateImpl::SendMessage( |
| const content::PresentationSessionInfo& session, |
| std::unique_ptr<content::PresentationConnectionMessage> message, |
| const SendMessageCallback& send_message_cb) { |
| - const MediaRoute::Id& route_id = frame_manager_->GetRouteId( |
| + auto* route = frame_manager_->GetRoute( |
| RenderFrameHostId(render_process_id, render_frame_id), |
| session.presentation_id); |
| - if (route_id.empty()) { |
| + if (!route) { |
| DVLOG(1) << "No active route for " << session.presentation_id; |
| send_message_cb.Run(false); |
| return; |
| } |
| if (message->is_binary()) { |
| - router_->SendRouteBinaryMessage(route_id, std::move(message->data), |
| - send_message_cb); |
| + router_->SendRouteBinaryMessage(route->media_route_id(), |
| + std::move(message->data), send_message_cb); |
| } else { |
| - router_->SendRouteMessage(route_id, message->message, send_message_cb); |
| + router_->SendRouteMessage(route->media_route_id(), message->message, |
| + send_message_cb); |
| } |
| } |