| 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 443 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  454  |  454  | 
|  455 void PresentationFrameManager::OnPresentationSessionStarted( |  455 void PresentationFrameManager::OnPresentationSessionStarted( | 
|  456     const RenderFrameHostId& render_frame_host_id, |  456     const RenderFrameHostId& render_frame_host_id, | 
|  457     const content::PresentationSessionInfo& session, |  457     const content::PresentationSessionInfo& session, | 
|  458     const MediaRoute::Id& route_id) { |  458     const MediaRoute::Id& route_id) { | 
|  459   auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |  459   auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 
|  460   presentation_frame->OnPresentationSessionStarted(session, route_id); |  460   presentation_frame->OnPresentationSessionStarted(session, route_id); | 
|  461 } |  461 } | 
|  462  |  462  | 
|  463 void PresentationFrameManager::OnDefaultPresentationSessionStarted( |  463 void PresentationFrameManager::OnDefaultPresentationSessionStarted( | 
|  464     const PresentationRequest& request, |  | 
|  465     const content::PresentationSessionInfo& session, |  464     const content::PresentationSessionInfo& session, | 
|  466     const MediaRoute::Id& route_id) { |  465     const MediaRoute::Id& route_id) { | 
|  467   const auto it = presentation_frames_.find(request.render_frame_host_id()); |  466   const auto it = presentation_frames_.find(request.render_frame_host_id()); | 
|  468   if (it != presentation_frames_.end()) |  467   if (it != presentation_frames_.end()) | 
|  469     it->second->OnPresentationSessionStarted(session, route_id); |  468     it->second->OnPresentationSessionStarted(session, route_id); | 
|  470  |  469  | 
|  471   if (default_presentation_request_ && |  470   if (default_presentation_request_ && | 
|  472       default_presentation_request_->Equals(request)) { |  471       default_presentation_request_->Equals(request)) { | 
|  473     default_presentation_started_callback_.Run(session); |  472     default_presentation_started_callback_.Run(session); | 
|  474   } |  473   } | 
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  531   const auto it = presentation_frames_.find(render_frame_host_id); |  530   const auto it = presentation_frames_.find(render_frame_host_id); | 
|  532   if (it == presentation_frames_.end()) { |  531   if (it == presentation_frames_.end()) { | 
|  533     DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " |  532     DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " | 
|  534              << "for: (" << render_frame_host_id.first << ", " |  533              << "for: (" << render_frame_host_id.first << ", " | 
|  535              << render_frame_host_id.second << ")"; |  534              << render_frame_host_id.second << ")"; | 
|  536     return; |  535     return; | 
|  537   } |  536   } | 
|  538   it->second->ListenForSessionMessages(session, message_cb); |  537   it->second->ListenForSessionMessages(session, message_cb); | 
|  539 } |  538 } | 
|  540  |  539  | 
|  541 void PresentationFrameManager::SetDefaultPresentationUrl( |  540 void PresentationFrameManager::SetDefaultPresentationUrls( | 
|  542     const RenderFrameHostId& render_frame_host_id, |  541     const RenderFrameHostId& render_frame_host_id, | 
|  543     const GURL& default_presentation_url, |  542     const vector<GURL>& default_presentation_urls, | 
|  544     const content::PresentationSessionStartedCallback& callback) { |  543     const content::PresentationSessionStartedCallback& callback) { | 
|  545   if (!IsMainFrame(render_frame_host_id)) |  544   if (!IsMainFrame(render_frame_host_id)) | 
|  546     return; |  545     return; | 
|  547  |  546  | 
|  548   if (default_presentation_url.is_empty()) { |  547   if (default_presentation_urls.is_empty()) { | 
|  549     ClearDefaultPresentationRequest(); |  548     ClearDefaultPresentationRequest(); | 
|  550   } else { |  549   } else { | 
|  551     DCHECK(!callback.is_null()); |  550     DCHECK(!callback.is_null()); | 
|  552     GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |  551     GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); | 
|  553     PresentationRequest request(render_frame_host_id, |  552     PresentationRequest request(render_frame_host_id, | 
|  554                                 std::vector<GURL>({default_presentation_url}), |  553                                 default_presentation_urls, | 
|  555                                 frame_url); |  554                                 frame_url); | 
|  556     default_presentation_started_callback_ = callback; |  555     default_presentation_started_callback_ = callback; | 
|  557     SetDefaultPresentationRequest(request); |  556     SetDefaultPresentationRequest(request); | 
|  558   } |  557   } | 
|  559 } |  558 } | 
|  560  |  559  | 
|  561 void PresentationFrameManager::AddDelegateObserver( |  560 void PresentationFrameManager::AddDelegateObserver( | 
|  562     const RenderFrameHostId& render_frame_host_id, |  561     const RenderFrameHostId& render_frame_host_id, | 
|  563     DelegateObserver* observer) { |  562     DelegateObserver* observer) { | 
|  564   auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |  563   auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  711   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |  710   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 
|  712   frame_manager_->Reset(render_frame_host_id); |  711   frame_manager_->Reset(render_frame_host_id); | 
|  713 } |  712 } | 
|  714  |  713  | 
|  715 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( |  714 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( | 
|  716     int render_process_id, |  715     int render_process_id, | 
|  717     int render_frame_id, |  716     int render_frame_id, | 
|  718     const std::vector<GURL>& default_presentation_urls, |  717     const std::vector<GURL>& default_presentation_urls, | 
|  719     const content::PresentationSessionStartedCallback& callback) { |  718     const content::PresentationSessionStartedCallback& callback) { | 
|  720   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |  719   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 
|  721   if (default_presentation_urls.empty()) { |  720   frame_manager_->SetDefaultPresentationUrls( | 
|  722     frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, GURL(), |  721       render_frame_host_id, default_presentation_urls, callback); | 
|  723                                               callback); |  | 
|  724   } else { |  | 
|  725     // TODO(crbug.com/627655): Handle multiple URLs. |  | 
|  726     frame_manager_->SetDefaultPresentationUrl( |  | 
|  727         render_frame_host_id, default_presentation_urls[0], callback); |  | 
|  728   } |  | 
|  729 } |  722 } | 
|  730  |  723  | 
|  731 void PresentationServiceDelegateImpl::OnJoinRouteResponse( |  724 void PresentationServiceDelegateImpl::OnJoinRouteResponse( | 
|  732     int render_process_id, |  725     int render_process_id, | 
|  733     int render_frame_id, |  726     int render_frame_id, | 
|  734     const content::PresentationSessionInfo& session, |  727     const content::PresentationSessionInfo& session, | 
|  735     const content::PresentationSessionStartedCallback& success_cb, |  728     const content::PresentationSessionStartedCallback& success_cb, | 
|  736     const content::PresentationSessionErrorCallback& error_cb, |  729     const content::PresentationSessionErrorCallback& error_cb, | 
|  737     const RouteRequestResult& result) { |  730     const RouteRequestResult& result) { | 
|  738   if (!result.route()) { |  731   if (!result.route()) { | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  772     int render_frame_id, |  765     int render_frame_id, | 
|  773     const std::vector<GURL>& presentation_urls, |  766     const std::vector<GURL>& presentation_urls, | 
|  774     const content::PresentationSessionStartedCallback& success_cb, |  767     const content::PresentationSessionStartedCallback& success_cb, | 
|  775     const content::PresentationSessionErrorCallback& error_cb) { |  768     const content::PresentationSessionErrorCallback& error_cb) { | 
|  776   if (presentation_urls.empty()) { |  769   if (presentation_urls.empty()) { | 
|  777     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |  770     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 
|  778                                             "Invalid presentation arguments.")); |  771                                             "Invalid presentation arguments.")); | 
|  779     return; |  772     return; | 
|  780   } |  773   } | 
|  781  |  774  | 
|  782   // TODO(crbug.com/627655): Handle multiple URLs. |  775   if (presentation_urls.is_empty() || | 
|  783   const GURL& presentation_url = presentation_urls[0]; |  776       std::find_if( | 
|  784   if (presentation_url.is_empty() || |  | 
|  785       !IsValidPresentationUrl(presentation_url)) { |  777       !IsValidPresentationUrl(presentation_url)) { | 
|  786     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |  778     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 
|  787                                             "Invalid presentation arguments.")); |  779                                             "Invalid presentation arguments.")); | 
|  788     return; |  780     return; | 
|  789   } |  781   } | 
|  790  |  782  | 
|  791   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |  783   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 
|  792   std::unique_ptr<CreatePresentationConnectionRequest> request( |  784   std::unique_ptr<CreatePresentationConnectionRequest> request( | 
|  793       new CreatePresentationConnectionRequest( |  785       new CreatePresentationConnectionRequest( | 
|  794           render_frame_host_id, presentation_url, |  786           render_frame_host_id, presentation_urls, | 
|  795           GetLastCommittedURLForFrame(render_frame_host_id), |  787           GetLastCommittedURLForFrame(render_frame_host_id), | 
|  796           base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |  788           base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, | 
|  797                      weak_factory_.GetWeakPtr(), render_process_id, |  789                      weak_factory_.GetWeakPtr(), render_process_id, | 
|  798                      render_frame_id, success_cb), |  790                      render_frame_id, success_cb), | 
|  799           error_cb)); |  791           error_cb)); | 
|  800   MediaRouterDialogController* controller = |  792   MediaRouterDialogController* controller = | 
|  801       MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); |  793       MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); | 
|  802   if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { |  794   if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { | 
|  803     LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; |  795     LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; | 
|  804     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |  796     error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, | 
|  805                                             "Unable to create dialog.")); |  797                                             "Unable to create dialog.")); | 
|  806     return; |  798     return; | 
|  807   } |  799   } | 
|  808 } |  800 } | 
|  809  |  801  | 
|  810 void PresentationServiceDelegateImpl::JoinSession( |  802 void PresentationServiceDelegateImpl::JoinSession( | 
|  811     int render_process_id, |  803     int render_process_id, | 
|  812     int render_frame_id, |  804     int render_frame_id, | 
|  813     const std::vector<GURL>& presentation_urls, |  805     const std::vector<GURL>& presentation_urls, | 
|  814     const std::string& presentation_id, |  806     const std::string& presentation_id, | 
|  815     const content::PresentationSessionStartedCallback& success_cb, |  807     const content::PresentationSessionStartedCallback& success_cb, | 
|  816     const content::PresentationSessionErrorCallback& error_cb) { |  808     const content::PresentationSessionErrorCallback& error_cb) { | 
|  817   if (presentation_urls.empty()) { |  809   if (presentation_urls.empty()) { | 
|  818     error_cb.Run(content::PresentationError( |  810     error_cb.Run(content::PresentationError( | 
|  819         content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, |  811         content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, | 
|  820         "Invalid presentation arguments.")); |  812         "Invalid presentation arguments.")); | 
|  821   } |  813   } | 
|  822  |  814  | 
|  823   // TODO(crbug.com/627655): Handle multiple URLs. |  815   // TODO: Needs work | 
|  824   const GURL& presentation_url = presentation_urls[0]; |  816   // bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); | 
|  825   bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); |  817   // std::vector<MediaRouteResponseCallback> route_response_callbacks; | 
|  826   std::vector<MediaRouteResponseCallback> route_response_callbacks; |  818   // route_response_callbacks.push_back(base::Bind( | 
|  827   route_response_callbacks.push_back(base::Bind( |  819   //     &PresentationServiceDelegateImpl::OnJoinRouteResponse, | 
|  828       &PresentationServiceDelegateImpl::OnJoinRouteResponse, |  820   //     weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, | 
|  829       weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, |  821   //     content::PresentationSessionInfo(presentation_urls, presentation_id), | 
|  830       content::PresentationSessionInfo(presentation_url, presentation_id), |  822   //     success_cb, error_cb)); | 
|  831       success_cb, error_cb)); |  823   // router_->JoinRoute( | 
|  832   router_->JoinRoute( |  824   //     MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, | 
|  833       MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, |  825   //     GetLastCommittedURLForFrame( | 
|  834       GetLastCommittedURLForFrame( |  826   //         RenderFrameHostId(render_process_id, render_frame_id)) | 
|  835           RenderFrameHostId(render_process_id, render_frame_id)) |  827   //         .GetOrigin(), | 
|  836           .GetOrigin(), |  828   //     web_contents_, route_response_callbacks, base::TimeDelta(), incognito); | 
|  837       web_contents_, route_response_callbacks, base::TimeDelta(), incognito); |  | 
|  838 } |  829 } | 
|  839  |  830  | 
|  840 void PresentationServiceDelegateImpl::CloseConnection( |  831 void PresentationServiceDelegateImpl::CloseConnection( | 
|  841     int render_process_id, |  832     int render_process_id, | 
|  842     int render_frame_id, |  833     int render_frame_id, | 
|  843     const std::string& presentation_id) { |  834     const std::string& presentation_id) { | 
|  844   const RenderFrameHostId rfh_id(render_process_id, render_frame_id); |  835   const RenderFrameHostId rfh_id(render_process_id, render_frame_id); | 
|  845   const MediaRoute::Id& route_id = |  836   const MediaRoute::Id& route_id = | 
|  846       frame_manager_->GetRouteId(rfh_id, presentation_id); |  837       frame_manager_->GetRouteId(rfh_id, presentation_id); | 
|  847   if (route_id.empty()) { |  838   if (route_id.empty()) { | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  910     const content::PresentationSessionInfo& connection, |  901     const content::PresentationSessionInfo& connection, | 
|  911     const content::PresentationConnectionStateChangedCallback& |  902     const content::PresentationConnectionStateChangedCallback& | 
|  912         state_changed_cb) { |  903         state_changed_cb) { | 
|  913   frame_manager_->ListenForConnectionStateChange( |  904   frame_manager_->ListenForConnectionStateChange( | 
|  914       RenderFrameHostId(render_process_id, render_frame_id), connection, |  905       RenderFrameHostId(render_process_id, render_frame_id), connection, | 
|  915       state_changed_cb); |  906       state_changed_cb); | 
|  916 } |  907 } | 
|  917  |  908  | 
|  918 void PresentationServiceDelegateImpl::OnRouteResponse( |  909 void PresentationServiceDelegateImpl::OnRouteResponse( | 
|  919     const PresentationRequest& presentation_request, |  910     const PresentationRequest& presentation_request, | 
 |  911     const GURL& presentation_url, | 
|  920     const RouteRequestResult& result) { |  912     const RouteRequestResult& result) { | 
|  921   if (!result.route()) |  913   if (!result.route() || | 
 |  914       base::STLCount(presentation_request.presentation_urls(), | 
 |  915                      presentation_url) == 0) { | 
|  922     return; |  916     return; | 
 |  917   } | 
|  923  |  918  | 
|  924   content::PresentationSessionInfo session_info( |  919   content::PresentationSessionInfo session_info( | 
|  925       presentation_request.presentation_url(), result.presentation_id()); |  920       presentation_url, result.presentation_id()); | 
|  926   frame_manager_->OnDefaultPresentationSessionStarted( |  921   frame_manager_->OnDefaultPresentationSessionStarted( | 
|  927       presentation_request, session_info, result.route()->media_route_id()); |  922       presentation_request, session_info, result.route()->media_route_id()); | 
|  928 } |  923 } | 
|  929  |  924  | 
|  930 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |  925 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( | 
|  931     DefaultPresentationRequestObserver* observer) { |  926     DefaultPresentationRequestObserver* observer) { | 
|  932   frame_manager_->AddDefaultPresentationRequestObserver(observer); |  927   frame_manager_->AddDefaultPresentationRequestObserver(observer); | 
|  933 } |  928 } | 
|  934  |  929  | 
|  935 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |  930 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
|  961 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |  956 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 
|  962     int render_process_id, |  957     int render_process_id, | 
|  963     int render_frame_id, |  958     int render_frame_id, | 
|  964     const MediaSource::Id& source_id) const { |  959     const MediaSource::Id& source_id) const { | 
|  965   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |  960   RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 
|  966   return frame_manager_->HasScreenAvailabilityListenerForTest( |  961   return frame_manager_->HasScreenAvailabilityListenerForTest( | 
|  967       render_frame_host_id, source_id); |  962       render_frame_host_id, source_id); | 
|  968 } |  963 } | 
|  969  |  964  | 
|  970 }  // namespace media_router |  965 }  // namespace media_router | 
| OLD | NEW |