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 0597e4993ab83b00d73a61d969418b10e55334a0..e94df5bcc8430f73dba94b8dfe1f9bfba08fd8f8 100644 |
| --- a/chrome/browser/media/router/presentation_service_delegate_impl.cc |
| +++ b/chrome/browser/media/router/presentation_service_delegate_impl.cc |
| @@ -37,17 +37,6 @@ namespace media_router { |
| namespace { |
| using DelegateObserver = content::PresentationServiceDelegate::Observer; |
| -using PresentationSessionErrorCallback = |
| - content::PresentationServiceDelegate::PresentationSessionErrorCallback; |
| -using PresentationSessionSuccessCallback = |
| - content::PresentationServiceDelegate::PresentationSessionSuccessCallback; |
| - |
| -// Returns the unique identifier for the supplied RenderFrameHost. |
| -RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { |
| - int render_process_id = render_frame_host->GetProcess()->GetID(); |
| - int render_frame_id = render_frame_host->GetRoutingID(); |
| - return RenderFrameHostId(render_process_id, render_frame_id); |
| -} |
| // Gets the last committed URL for the render frame specified by |
| // |render_frame_host_id|. |
| @@ -84,6 +73,14 @@ class PresentationFrame { |
| void ListenForSessionMessages( |
| const content::PresentationSessionInfo& session, |
| const content::PresentationSessionMessageCallback& message_cb); |
| + |
| + // Sets this frame's default prsentation request and callback. |
|
mark a. foltz
2015/10/20 20:15:25
Typo in presentation request.
Also a slight prefe
imcheng
2015/10/24 00:41:19
Done.
|
| + void SetDefaultPresentationRequest( |
| + const PresentationRequest& default_presentation_request, |
| + const content::PresentationSessionStartedCallback& callback); |
| + |
| + // Clear this frame's default prsentation request and callback. |
|
mark a. foltz
2015/10/20 20:15:25
Typo here
imcheng
2015/10/24 00:41:19
Done.
|
| + void ClearDefaultPresentationRequest(); |
| void Reset(); |
| const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| @@ -91,7 +88,10 @@ class PresentationFrame { |
| void OnPresentationSessionClosed(const std::string& presentation_id); |
| void OnPresentationSessionStarted( |
| - bool is_default_presentation, |
| + const content::PresentationSessionInfo& session, |
| + const MediaRoute::Id& route_id); |
| + void OnDefaultPresentationSessionStarted( |
|
mark a. foltz
2015/10/20 20:15:25
I wonder if this class needs to expose two differe
imcheng
2015/10/24 00:41:19
This is motivated by the need to pass in a callbac
|
| + const PresentationRequest& request, |
| const content::PresentationSessionInfo& session, |
| const MediaRoute::Id& route_id); |
| void OnPresentationServiceDelegateDestroyed() const; |
| @@ -110,6 +110,13 @@ class PresentationFrame { |
| scoped_ptr<PresentationSessionStateObserver> session_state_observer_; |
| ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; |
| + // Default presentation request for this frame, if any. |
| + scoped_ptr<PresentationRequest> default_presentation_request_; |
| + |
| + // Callback to invoke when default presentation has started. |
| + content::PresentationSessionStartedCallback |
| + default_presentation_started_callback_; |
| + |
| // References to the owning WebContents, and the corresponding MediaRouter. |
| const content::WebContents* web_contents_; |
| MediaRouter* router_; |
| @@ -135,15 +142,23 @@ void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { |
| } |
| void PresentationFrame::OnPresentationSessionStarted( |
| - bool is_default_presentation, |
| const content::PresentationSessionInfo& session, |
| const MediaRoute::Id& route_id) { |
| presentation_id_to_route_id_[session.presentation_id] = route_id; |
| route_id_to_presentation_.Add(route_id, session); |
| if (session_state_observer_) |
| session_state_observer_->OnPresentationSessionConnected(route_id); |
| - if (is_default_presentation && delegate_observer_) |
| - delegate_observer_->OnDefaultPresentationStarted(session); |
| +} |
| + |
| +void PresentationFrame::OnDefaultPresentationSessionStarted( |
|
mark a. foltz
2015/10/20 20:15:25
Is this called only for browser initiated requests
imcheng
2015/10/24 00:41:19
Only for browser initiated requests.
|
| + const PresentationRequest& request, |
| + const content::PresentationSessionInfo& session, |
| + const MediaRoute::Id& route_id) { |
| + OnPresentationSessionStarted(session, route_id); |
| + if (default_presentation_request_ && |
| + default_presentation_request_->Equals(request)) { |
| + default_presentation_started_callback_.Run(session); |
| + } |
| } |
| void PresentationFrame::OnPresentationSessionClosed( |
| @@ -212,6 +227,8 @@ void PresentationFrame::Reset() { |
| if (session_state_observer_) |
| session_state_observer_->Reset(); |
| session_messages_observers_.clear(); |
| + |
|
mark a. foltz
2015/10/20 20:15:25
Extra newline
imcheng
2015/10/24 00:41:19
Ok. But added a new line after L228 for the if blo
|
| + ClearDefaultPresentationRequest(); |
| } |
| void PresentationFrame::ListenForSessionStateChange( |
| @@ -235,6 +252,22 @@ void PresentationFrame::ListenForSessionMessages( |
| new PresentationSessionMessagesObserver(message_cb, it->second, router_)); |
| } |
| +void PresentationFrame::SetDefaultPresentationRequest( |
| + const PresentationRequest& default_presentation_request, |
| + const content::PresentationSessionStartedCallback& callback) { |
| + default_presentation_started_callback_ = callback; |
|
mark a. foltz
2015/10/20 20:15:25
Do we need to reset default_presentation_started_c
imcheng
2015/10/24 00:41:19
The Equals check below is just an optimization. Th
|
| + if (!default_presentation_request_ || |
| + !default_presentation_request_->Equals(default_presentation_request)) { |
| + default_presentation_request_.reset( |
| + new PresentationRequest(default_presentation_request)); |
| + } |
| +} |
| + |
| +void PresentationFrame::ClearDefaultPresentationRequest() { |
| + default_presentation_request_.reset(); |
| + default_presentation_started_callback_.Reset(); |
| +} |
| + |
| MediaSource PresentationFrame::GetMediaSourceFromListener( |
| content::PresentationScreenAvailabilityListener* listener) const { |
| // If the default presentation URL is empty then fall back to tab mirroring. |
| @@ -265,9 +298,23 @@ class PresentationFrameManager { |
| const RenderFrameHostId& render_frame_host_id, |
| const content::PresentationSessionInfo& session, |
| const content::PresentationSessionMessageCallback& message_cb); |
| + |
| + // Sets or clears the default presentation request and callback for the given |
| + // frame. Also sets / clears the default presentation requests for the owning |
| + // tab WebContents. |
| + void SetDefaultPresentationUrl( |
| + const RenderFrameHostId& render_frame_host_id, |
| + const std::string& default_presentation_url, |
| + const content::PresentationSessionStartedCallback& callback); |
| void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| DelegateObserver* observer); |
| void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| + void AddDefaultPresentationRequestObserver( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| + observer); |
| + void RemoveDefaultPresentationRequestObserver( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| + observer); |
| void Reset(const RenderFrameHostId& render_frame_host_id); |
| bool HasScreenAvailabilityListenerForTest( |
| const RenderFrameHostId& render_frame_host_id, |
| @@ -276,7 +323,10 @@ class PresentationFrameManager { |
| void OnPresentationSessionStarted( |
| const RenderFrameHostId& render_frame_host_id, |
| - bool is_default_presentation, |
| + const content::PresentationSessionInfo& session, |
| + const MediaRoute::Id& route_id); |
| + void OnDefaultPresentationSessionStarted( |
| + const PresentationRequest& request, |
| const content::PresentationSessionInfo& session, |
| const MediaRoute::Id& route_id); |
| @@ -288,15 +338,37 @@ class PresentationFrameManager { |
| const std::vector<MediaRoute::Id> GetRouteIds( |
| const RenderFrameHostId& render_frame_host_id) const; |
| + const PresentationRequest* default_presentation_request() const { |
|
mark a. foltz
2015/10/20 20:15:25
Which request is this returning? Isn't there pote
imcheng
2015/10/24 00:41:19
This returns the "tab-level" default presentation
|
| + return default_presentation_request_.get(); |
| + } |
| + |
| private: |
| PresentationFrame* GetOrAddPresentationFrame( |
| const RenderFrameHostId& render_frame_host_id); |
| + // Sets the default presentation request for the owning WebContents and |
| + // notifies observers of changes. |
| + void SetDefaultPresentationRequest( |
| + const PresentationRequest& default_presentation_request); |
| + |
| + // Clears the default presentation request for the owning WebContents and |
| + // notifies observers of changes. |
| + void ClearDefaultPresentationRequest(); |
| + |
| // Maps a frame identifier to a PresentationFrame object for frames |
| // that are using presentation API. |
| base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> |
| presentation_frames_; |
| + // Default presentation request for the owning tab WebContents. |
| + scoped_ptr<PresentationRequest> default_presentation_request_; |
| + |
| + // References to the observers listening for changes to this tab WebContent's |
| + // default presentation. |
| + base::ObserverList< |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver> |
| + default_presentation_request_observers_; |
| + |
| // References to the owning WebContents, and the corresponding MediaRouter. |
| MediaRouter* router_; |
| content::WebContents* web_contents_; |
| @@ -317,13 +389,22 @@ PresentationFrameManager::~PresentationFrameManager() { |
| void PresentationFrameManager::OnPresentationSessionStarted( |
| const RenderFrameHostId& render_frame_host_id, |
| - bool is_default_presentation, |
| const content::PresentationSessionInfo& session, |
| const MediaRoute::Id& route_id) { |
| auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| if (presentation_frame) |
| - presentation_frame->OnPresentationSessionStarted(is_default_presentation, |
| - session, route_id); |
| + presentation_frame->OnPresentationSessionStarted(session, route_id); |
| +} |
| + |
| +void PresentationFrameManager::OnDefaultPresentationSessionStarted( |
| + const PresentationRequest& request, |
| + const content::PresentationSessionInfo& session, |
| + const MediaRoute::Id& route_id) { |
| + auto presentation_frame = |
| + presentation_frames_.get(request.render_frame_host_id()); |
| + if (presentation_frame) |
| + presentation_frame->OnDefaultPresentationSessionStarted(request, session, |
| + route_id); |
| } |
| void PresentationFrameManager::OnPresentationSessionClosed( |
| @@ -397,6 +478,26 @@ void PresentationFrameManager::ListenForSessionMessages( |
| presentation_frame->ListenForSessionMessages(session, message_cb); |
| } |
| +void PresentationFrameManager::SetDefaultPresentationUrl( |
| + const RenderFrameHostId& render_frame_host_id, |
| + const std::string& default_presentation_url, |
| + const content::PresentationSessionStartedCallback& callback) { |
| + PresentationFrame* presentation_frame = |
| + GetOrAddPresentationFrame(render_frame_host_id); |
| + if (default_presentation_url.empty()) { |
| + ClearDefaultPresentationRequest(); |
|
mark a. foltz
2015/10/20 20:15:25
This design keeps track of default requests at two
imcheng
2015/10/24 00:41:19
This patch fixes the issue where the default reque
|
| + presentation_frame->ClearDefaultPresentationRequest(); |
| + } else { |
| + DCHECK(!callback.is_null()); |
| + |
| + GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id)); |
| + PresentationRequest request(render_frame_host_id, default_presentation_url, |
| + frame_url); |
| + presentation_frame->SetDefaultPresentationRequest(request, callback); |
| + SetDefaultPresentationRequest(request); |
| + } |
| +} |
| + |
| void PresentationFrameManager::AddDelegateObserver( |
| const RenderFrameHostId& render_frame_host_id, |
| DelegateObserver* observer) { |
| @@ -413,11 +514,29 @@ void PresentationFrameManager::RemoveDelegateObserver( |
| } |
| } |
| +void PresentationFrameManager::AddDefaultPresentationRequestObserver( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| + observer) { |
| + default_presentation_request_observers_.AddObserver(observer); |
| +} |
| + |
| +void PresentationFrameManager::RemoveDefaultPresentationRequestObserver( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver* |
| + observer) { |
| + default_presentation_request_observers_.RemoveObserver(observer); |
| +} |
| + |
| void PresentationFrameManager::Reset( |
| const RenderFrameHostId& render_frame_host_id) { |
| auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| if (presentation_frame) |
| presentation_frame->Reset(); |
| + |
| + if (default_presentation_request_ && |
| + render_frame_host_id == |
| + default_presentation_request_->render_frame_host_id()) { |
| + ClearDefaultPresentationRequest(); |
| + } |
| } |
| PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( |
| @@ -431,6 +550,31 @@ PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( |
| return presentation_frames_.get(render_frame_host_id); |
| } |
| +void PresentationFrameManager::ClearDefaultPresentationRequest() { |
| + if (!default_presentation_request_) |
| + return; |
| + |
| + default_presentation_request_.reset(); |
| + FOR_EACH_OBSERVER( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver, |
| + default_presentation_request_observers_, |
| + OnDefaultPresentationChanged(nullptr)); |
| +} |
| + |
| +void PresentationFrameManager::SetDefaultPresentationRequest( |
| + const PresentationRequest& default_presentation_request) { |
| + if (default_presentation_request_ && |
| + default_presentation_request_->Equals(default_presentation_request)) |
| + return; |
| + |
| + default_presentation_request_.reset( |
| + new PresentationRequest(default_presentation_request)); |
| + FOR_EACH_OBSERVER( |
| + PresentationServiceDelegateImpl::DefaultPresentationRequestObserver, |
| + default_presentation_request_observers_, |
| + OnDefaultPresentationChanged(default_presentation_request_.get())); |
| +} |
| + |
| void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { |
| router_ = router; |
| } |
| @@ -494,60 +638,24 @@ void PresentationServiceDelegateImpl::Reset(int render_process_id, |
| int render_frame_id) { |
| RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| frame_manager_->Reset(render_frame_host_id); |
| - if (render_frame_host_id == default_presentation_render_frame_host_id_) { |
| - UpdateDefaultMediaSourceAndNotifyObservers(RenderFrameHostId(), |
| - MediaSource(), GURL()); |
| - } |
| } |
| void PresentationServiceDelegateImpl::SetDefaultPresentationUrl( |
| int render_process_id, |
| int render_frame_id, |
| - const std::string& default_presentation_url) { |
| + const std::string& default_presentation_url, |
| + const content::PresentationSessionStartedCallback& callback) { |
| RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| - if (IsMainFrame(render_process_id, render_frame_id)) { |
| - // This is the main frame, which means tab-level default presentation |
| - // might have been updated. |
| - MediaSource default_source; |
| - if (!default_presentation_url.empty()) |
| - default_source = MediaSourceForPresentationUrl(default_presentation_url); |
| - |
| - GURL default_frame_url = GetLastCommittedURLForFrame(render_frame_host_id); |
| - UpdateDefaultMediaSourceAndNotifyObservers( |
| - render_frame_host_id, default_source, default_frame_url); |
| - } |
| -} |
| - |
| -bool PresentationServiceDelegateImpl::IsMainFrame(int render_process_id, |
| - int render_frame_id) const { |
| - RenderFrameHost* main_frame = web_contents_->GetMainFrame(); |
| - return main_frame && |
| - GetRenderFrameHostId(main_frame) == |
| - RenderFrameHostId(render_process_id, render_frame_id); |
| -} |
| - |
| -void PresentationServiceDelegateImpl:: |
| - UpdateDefaultMediaSourceAndNotifyObservers( |
| - const RenderFrameHostId& render_frame_host_id, |
| - const MediaSource& new_default_source, |
| - const GURL& new_default_frame_url) { |
| - default_presentation_render_frame_host_id_ = render_frame_host_id; |
| - if (!new_default_source.Equals(default_source_) || |
| - new_default_frame_url != default_frame_url_) { |
| - default_source_ = new_default_source; |
| - default_frame_url_ = new_default_frame_url; |
| - FOR_EACH_OBSERVER( |
| - DefaultMediaSourceObserver, default_media_source_observers_, |
| - OnDefaultMediaSourceChanged(default_source_, default_frame_url_)); |
| - } |
| + frame_manager_->SetDefaultPresentationUrl(render_frame_host_id, |
| + default_presentation_url, callback); |
| } |
| void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| int render_process_id, |
| int render_frame_id, |
| const content::PresentationSessionInfo& session, |
| - const PresentationSessionSuccessCallback& success_cb, |
| - const PresentationSessionErrorCallback& error_cb, |
| + const content::PresentationSessionStartedCallback& success_cb, |
| + const content::PresentationSessionErrorCallback& error_cb, |
| const MediaRoute* route, |
| const std::string& presentation_id, |
| const std::string& error_text) { |
| @@ -561,7 +669,7 @@ void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| << ", presentation ID: " << session.presentation_id; |
| DCHECK_EQ(session.presentation_id, presentation_id); |
| frame_manager_->OnPresentationSessionStarted( |
| - RenderFrameHostId(render_process_id, render_frame_id), false, session, |
| + RenderFrameHostId(render_process_id, render_frame_id), session, |
| route->media_route_id()); |
| success_cb.Run(session); |
| } |
| @@ -570,7 +678,7 @@ void PresentationServiceDelegateImpl::OnJoinRouteResponse( |
| void PresentationServiceDelegateImpl::OnStartSessionSucceeded( |
| int render_process_id, |
| int render_frame_id, |
| - const PresentationSessionSuccessCallback& success_cb, |
| + const content::PresentationSessionStartedCallback& success_cb, |
| const content::PresentationSessionInfo& new_session, |
| const MediaRoute::Id& route_id) { |
| DVLOG(1) << "OnStartSessionSucceeded: " |
| @@ -578,7 +686,7 @@ void PresentationServiceDelegateImpl::OnStartSessionSucceeded( |
| << ", presentation URL: " << new_session.presentation_url |
| << ", presentation ID: " << new_session.presentation_id; |
| frame_manager_->OnPresentationSessionStarted( |
| - RenderFrameHostId(render_process_id, render_frame_id), false, new_session, |
| + RenderFrameHostId(render_process_id, render_frame_id), new_session, |
| route_id); |
| success_cb.Run(new_session); |
| } |
| @@ -587,18 +695,19 @@ void PresentationServiceDelegateImpl::StartSession( |
| int render_process_id, |
| int render_frame_id, |
| const std::string& presentation_url, |
| - const PresentationSessionSuccessCallback& success_cb, |
| - const PresentationSessionErrorCallback& error_cb) { |
| + const content::PresentationSessionStartedCallback& success_cb, |
| + const content::PresentationSessionErrorCallback& error_cb) { |
| if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { |
| error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, |
| "Invalid presentation arguments.")); |
| return; |
| } |
| - RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| + RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| scoped_ptr<CreatePresentationSessionRequest> context( |
| new CreatePresentationSessionRequest( |
| - presentation_url, GetLastCommittedURLForFrame(render_frame_host_id), |
| + render_frame_host_id, presentation_url, |
| + GetLastCommittedURLForFrame(render_frame_host_id), |
| base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, |
| weak_factory_.GetWeakPtr(), render_process_id, |
| render_frame_id, success_cb), |
| @@ -618,8 +727,8 @@ void PresentationServiceDelegateImpl::JoinSession( |
| int render_frame_id, |
| const std::string& presentation_url, |
| const std::string& presentation_id, |
| - const PresentationSessionSuccessCallback& success_cb, |
| - const PresentationSessionErrorCallback& error_cb) { |
| + const content::PresentationSessionStartedCallback& success_cb, |
| + const content::PresentationSessionErrorCallback& error_cb) { |
| std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| route_response_callbacks.push_back(base::Bind( |
| &PresentationServiceDelegateImpl::OnJoinRouteResponse, |
| @@ -689,34 +798,32 @@ void PresentationServiceDelegateImpl::ListenForSessionStateChange( |
| } |
| void PresentationServiceDelegateImpl::OnRouteResponse( |
| + const PresentationRequest& presentation_request, |
| const MediaRoute* route, |
| const std::string& presentation_id, |
| const std::string& error) { |
| if (!route) |
| return; |
| - const MediaSource& source = route->media_source(); |
| - DCHECK(!source.Empty()); |
| - if (!default_source_.Equals(source)) |
| - return; |
| - RenderFrameHost* main_frame = web_contents_->GetMainFrame(); |
| - if (!main_frame) |
| - return; |
| - RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); |
| - frame_manager_->OnPresentationSessionStarted( |
| - render_frame_host_id, true, |
| - content::PresentationSessionInfo(PresentationUrlFromMediaSource(source), |
| - presentation_id), |
| - route->media_route_id()); |
| + |
| + content::PresentationSessionInfo session_info( |
| + presentation_request.presentation_url(), presentation_id); |
| + frame_manager_->OnDefaultPresentationSessionStarted( |
| + presentation_request, session_info, route->media_route_id()); |
| +} |
| + |
| +void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( |
| + DefaultPresentationRequestObserver* observer) { |
| + frame_manager_->AddDefaultPresentationRequestObserver(observer); |
| } |
| -void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( |
| - DefaultMediaSourceObserver* observer) { |
| - default_media_source_observers_.AddObserver(observer); |
| +void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( |
| + DefaultPresentationRequestObserver* observer) { |
| + frame_manager_->RemoveDefaultPresentationRequestObserver(observer); |
| } |
| -void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( |
| - DefaultMediaSourceObserver* observer) { |
| - default_media_source_observers_.RemoveObserver(observer); |
| +const PresentationRequest* |
| +PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const { |
| + return frame_manager_->default_presentation_request(); |
| } |
| base::WeakPtr<PresentationServiceDelegateImpl> |