| 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 | 8 |
| 9 #include "base/containers/scoped_ptr_hash_map.h" | 9 #include "base/containers/scoped_ptr_hash_map.h" |
| 10 #include "base/containers/scoped_ptr_map.h" | 10 #include "base/containers/scoped_ptr_map.h" |
| 11 #include "base/containers/small_map.h" | 11 #include "base/containers/small_map.h" |
| 12 #include "base/guid.h" | |
| 13 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 15 #include "chrome/browser/media/router/create_presentation_session_request.h" | 14 #include "chrome/browser/media/router/create_presentation_session_request.h" |
| 16 #include "chrome/browser/media/router/media_route.h" | 15 #include "chrome/browser/media/router/media_route.h" |
| 17 #include "chrome/browser/media/router/media_router.h" | 16 #include "chrome/browser/media/router/media_router.h" |
| 18 #include "chrome/browser/media/router/media_router_dialog_controller.h" | 17 #include "chrome/browser/media/router/media_router_dialog_controller.h" |
| 19 #include "chrome/browser/media/router/media_router_factory.h" | 18 #include "chrome/browser/media/router/media_router_factory.h" |
| 20 #include "chrome/browser/media/router/media_sink.h" | 19 #include "chrome/browser/media/router/media_sink.h" |
| 21 #include "chrome/browser/media/router/media_source_helper.h" | 20 #include "chrome/browser/media/router/media_source_helper.h" |
| 21 #include "chrome/browser/media/router/offscreen_presentation_manager.h" |
| 22 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h" |
| 22 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" | 23 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" |
| 23 #include "chrome/browser/media/router/presentation_session_messages_observer.h" | 24 #include "chrome/browser/media/router/presentation_session_messages_observer.h" |
| 24 #include "chrome/browser/media/router/presentation_session_state_observer.h" | 25 #include "chrome/browser/media/router/presentation_session_state_observer.h" |
| 25 #include "chrome/browser/sessions/session_tab_helper.h" | 26 #include "chrome/browser/sessions/session_tab_helper.h" |
| 26 #include "content/public/browser/presentation_screen_availability_listener.h" | 27 #include "content/public/browser/presentation_screen_availability_listener.h" |
| 27 #include "content/public/browser/presentation_session.h" | 28 #include "content/public/browser/presentation_session.h" |
| 28 #include "content/public/browser/presentation_session_state_listener.h" | 29 #include "content/public/browser/presentation_session_state_listener.h" |
| 29 #include "content/public/browser/render_frame_host.h" | 30 #include "content/public/browser/render_frame_host.h" |
| 30 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
| 31 | 32 |
| 32 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 33 media_router::PresentationServiceDelegateImpl); | 34 media_router::PresentationServiceDelegateImpl); |
| 34 | 35 |
| 35 using content::RenderFrameHost; | 36 using content::RenderFrameHost; |
| 36 | 37 |
| 37 namespace media_router { | 38 namespace media_router { |
| 38 | 39 |
| 40 using OffscreenPresentationSession = |
| 41 OffscreenPresentationManager::OffscreenPresentationSession; |
| 42 |
| 39 namespace { | 43 namespace { |
| 40 | 44 |
| 41 using DelegateObserver = content::PresentationServiceDelegate::Observer; | 45 using DelegateObserver = content::PresentationServiceDelegate::Observer; |
| 42 using PresentationSessionErrorCallback = | 46 using PresentationSessionErrorCallback = |
| 43 content::PresentationServiceDelegate::PresentationSessionErrorCallback; | 47 content::PresentationServiceDelegate::PresentationSessionErrorCallback; |
| 44 using PresentationSessionSuccessCallback = | 48 using PresentationSessionSuccessCallback = |
| 45 content::PresentationServiceDelegate::PresentationSessionSuccessCallback; | 49 content::PresentationServiceDelegate::PresentationSessionSuccessCallback; |
| 46 | 50 |
| 47 using RenderFrameHostId = std::pair<int, int>; | |
| 48 | |
| 49 // Returns the unique identifier for the supplied RenderFrameHost. | 51 // Returns the unique identifier for the supplied RenderFrameHost. |
| 50 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { | 52 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) { |
| 51 int render_process_id = render_frame_host->GetProcess()->GetID(); | 53 int render_process_id = render_frame_host->GetProcess()->GetID(); |
| 52 int render_frame_id = render_frame_host->GetRoutingID(); | 54 int render_frame_id = render_frame_host->GetRoutingID(); |
| 53 return RenderFrameHostId(render_process_id, render_frame_id); | 55 return RenderFrameHostId(render_process_id, render_frame_id); |
| 54 } | 56 } |
| 55 | 57 |
| 56 // Gets the last committed URL for the render frame specified by | 58 // Gets the last committed URL for the render frame specified by |
| 57 // |render_frame_host_id|. | 59 // |render_frame_host_id|. |
| 58 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { | 60 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { |
| 59 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( | 61 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( |
| 60 render_frame_host_id.first, render_frame_host_id.second); | 62 render_frame_host_id.first, render_frame_host_id.second); |
| 61 DCHECK(render_frame_host); | 63 DCHECK(render_frame_host); |
| 62 return render_frame_host->GetLastCommittedURL(); | 64 return render_frame_host->GetLastCommittedURL(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 } // namespace | 67 } // namespace |
| 66 | 68 |
| 67 // Used by PresentationServiceDelegateImpl to manage | 69 // Used by PresentationServiceDelegateImpl to manage |
| 68 // listeners and default presentation info in a render frame. | 70 // listeners and default presentation info in a render frame. |
| 69 // Its lifetime: | 71 // Its lifetime: |
| 70 // * PresentationFrameManager AddDelegateObserver | 72 // * PresentationFrameManager AddDelegateObserver |
| 71 // * Reset 0+ times. | 73 // * Reset 0+ times. |
| 72 // * PresentationFrameManager.RemoveDelegateObserver. | 74 // * PresentationFrameManager.RemoveDelegateObserver. |
| 73 class PresentationFrame { | 75 class PresentationFrame { |
| 74 public: | 76 public: |
| 75 PresentationFrame(content::WebContents* web_contents, MediaRouter* router); | 77 PresentationFrame(content::WebContents* web_contents, |
| 78 const RenderFrameHostId& render_frame_host_id, |
| 79 MediaRouter* router); |
| 76 ~PresentationFrame(); | 80 ~PresentationFrame(); |
| 77 | 81 |
| 78 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 82 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 79 bool SetScreenAvailabilityListener( | 83 bool SetScreenAvailabilityListener( |
| 80 content::PresentationScreenAvailabilityListener* listener); | 84 content::PresentationScreenAvailabilityListener* listener); |
| 81 bool RemoveScreenAvailabilityListener( | 85 bool RemoveScreenAvailabilityListener( |
| 82 content::PresentationScreenAvailabilityListener* listener); | 86 content::PresentationScreenAvailabilityListener* listener); |
| 83 bool HasScreenAvailabilityListenerForTest( | 87 bool HasScreenAvailabilityListenerForTest( |
| 84 const MediaSource::Id& source_id) const; | 88 const MediaSource::Id& source_id) const; |
| 85 std::string GetDefaultPresentationId() const; | 89 std::string GetDefaultPresentationId() const; |
| 90 void SendMessage(const content::PresentationSessionInfo& session, |
| 91 scoped_ptr<content::PresentationSessionMessage> message, |
| 92 const content::SendMessageCallback send_message_cb); |
| 86 bool ListenForSessionStateChange( | 93 bool ListenForSessionStateChange( |
| 87 content::PresentationSessionStateListener* listener); | 94 content::PresentationSessionStateListener* listener); |
| 88 void ListenForSessionMessages( | 95 void ListenForSessionMessages( |
| 89 const content::PresentationSessionInfo& session, | 96 const content::PresentationSessionInfo& session, |
| 90 const content::PresentationSessionMessageCallback& message_cb); | 97 const content::PresentationSessionMessageCallback& message_cb); |
| 91 void Reset(); | 98 void Reset(); |
| 92 | 99 |
| 93 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; | 100 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; |
| 94 const std::vector<MediaRoute::Id> GetRouteIds() const; | |
| 95 void OnPresentationSessionClosed(const std::string& presentation_id); | 101 void OnPresentationSessionClosed(const std::string& presentation_id); |
| 96 | 102 |
| 97 void OnPresentationSessionStarted( | 103 void OnPresentationSessionStarted( |
| 98 bool is_default_presentation, | 104 bool is_default_presentation, |
| 99 const content::PresentationSessionInfo& session, | 105 const content::PresentationSessionInfo& session, |
| 100 const MediaRoute::Id& route_id); | 106 const MediaRoute& route); |
| 101 void OnPresentationServiceDelegateDestroyed() const; | 107 void OnPresentationServiceDelegateDestroyed() const; |
| 102 | 108 |
| 103 void set_delegate_observer(DelegateObserver* observer) { | 109 void set_delegate_observer(DelegateObserver* observer) { |
| 104 delegate_observer_ = observer; | 110 delegate_observer_ = observer; |
| 105 } | 111 } |
| 106 | 112 |
| 107 void set_default_presentation_url(const std::string& url) { | 113 void set_default_presentation_url(const std::string& url) { |
| 108 default_presentation_url_ = url; | 114 default_presentation_url_ = url; |
| 109 } | 115 } |
| 110 | 116 |
| 111 private: | 117 private: |
| 112 MediaSource GetMediaSourceFromListener( | 118 MediaSource GetMediaSourceFromListener( |
| 113 content::PresentationScreenAvailabilityListener* listener) const; | 119 content::PresentationScreenAvailabilityListener* listener) const; |
| 120 // Gets the OffscreenPresentationSession associated with |presentation_id| |
| 121 // within this frame. Returns nullptr if |presentation_id| is not |
| 122 // registered as an offscreen presentation. |
| 123 OffscreenPresentationSession* FindOffscreenSession( |
| 124 const std::string& presentation_id) const; |
| 125 |
| 114 base::SmallMap<std::map<std::string, MediaRoute::Id>> | 126 base::SmallMap<std::map<std::string, MediaRoute::Id>> |
| 115 presentation_id_to_route_id_; | 127 presentation_id_to_route_id_; |
| 116 std::string default_presentation_url_; | 128 std::string default_presentation_url_; |
| 117 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_; | 129 scoped_ptr<PresentationMediaSinksObserver> sinks_observer_; |
| 118 | 130 |
| 131 // Controller objects for offscreen presentations. Note that offscreen |
| 132 // presentations are manipulated with these objects instead of the observers |
| 133 // and MediaRouter objects below. |
| 134 // Maps from presentation ID to the corresponding OffscreenController within |
| 135 // this frame. |
| 136 base::ScopedPtrMap<std::string, scoped_ptr<OffscreenPresentationSession>> |
| 137 offscreen_presentation_sessions_; |
| 138 |
| 139 // For non-offscreen presentations. |
| 119 // Maps from presentation ID to the corresponding presentation session's | 140 // Maps from presentation ID to the corresponding presentation session's |
| 120 // state change observer within this frame. | 141 // state change observer within this frame. |
| 121 base::ScopedPtrMap<std::string, scoped_ptr<PresentationSessionStateObserver>> | 142 base::ScopedPtrMap<std::string, scoped_ptr<PresentationSessionStateObserver>> |
| 122 session_state_observers_; | 143 session_state_observers_; |
| 123 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; | 144 ScopedVector<PresentationSessionMessagesObserver> session_messages_observers_; |
| 124 | 145 |
| 125 // References to the owning WebContents, and the corresponding MediaRouter. | 146 // References to the owning WebContents. |
| 126 const content::WebContents* web_contents_; | 147 const content::WebContents* const web_contents_; |
| 127 MediaRouter* router_; | 148 |
| 149 // ID of owning RenderFrameHost. |
| 150 const RenderFrameHostId render_frame_host_id_; |
| 151 |
| 152 // References to MediaRouter / OffscreenPresentationManager owned by |
| 153 // the associated BrowserContext. They (and the BrowserContext) are |
| 154 // guaranteed to outlive this object. |
| 155 MediaRouter* const router_; |
| 156 OffscreenPresentationManager* const offscreen_presentation_manager_; |
| 128 | 157 |
| 129 DelegateObserver* delegate_observer_; | 158 DelegateObserver* delegate_observer_; |
| 130 }; | 159 }; |
| 131 | 160 |
| 132 PresentationFrame::PresentationFrame(content::WebContents* web_contents, | 161 PresentationFrame::PresentationFrame( |
| 133 MediaRouter* router) | 162 content::WebContents* web_contents, |
| 163 const RenderFrameHostId& render_frame_host_id, |
| 164 MediaRouter* router) |
| 134 : web_contents_(web_contents), | 165 : web_contents_(web_contents), |
| 166 render_frame_host_id_(render_frame_host_id), |
| 135 router_(router), | 167 router_(router), |
| 168 offscreen_presentation_manager_( |
| 169 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( |
| 170 web_contents_->GetBrowserContext())), |
| 136 delegate_observer_(nullptr) { | 171 delegate_observer_(nullptr) { |
| 137 DCHECK(web_contents_); | 172 DCHECK(web_contents_); |
| 138 DCHECK(router_); | 173 DCHECK(router_); |
| 174 DCHECK(offscreen_presentation_manager_); |
| 139 } | 175 } |
| 140 | 176 |
| 141 PresentationFrame::~PresentationFrame() { | 177 PresentationFrame::~PresentationFrame() { |
| 142 } | 178 } |
| 143 | 179 |
| 144 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { | 180 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { |
| 145 if (delegate_observer_) | 181 if (delegate_observer_) |
| 146 delegate_observer_->OnDelegateDestroyed(); | 182 delegate_observer_->OnDelegateDestroyed(); |
| 147 } | 183 } |
| 148 | 184 |
| 149 void PresentationFrame::OnPresentationSessionStarted( | 185 void PresentationFrame::OnPresentationSessionStarted( |
| 150 bool is_default_presentation, | 186 bool is_default_presentation, |
| 151 const content::PresentationSessionInfo& session, | 187 const content::PresentationSessionInfo& session, |
| 152 const MediaRoute::Id& route_id) { | 188 const MediaRoute& route) { |
| 153 presentation_id_to_route_id_[session.presentation_id] = route_id; | 189 const std::string& presentation_id = session.presentation_id; |
| 190 presentation_id_to_route_id_[presentation_id] = route.media_route_id(); |
| 154 if (is_default_presentation && delegate_observer_) | 191 if (is_default_presentation && delegate_observer_) |
| 155 delegate_observer_->OnDefaultPresentationStarted(session); | 192 delegate_observer_->OnDefaultPresentationStarted(session); |
| 193 if (route.is_offscreen_presentation()) { |
| 194 DCHECK(!ContainsKey(offscreen_presentation_sessions_, presentation_id)); |
| 195 scoped_ptr<OffscreenPresentationSession> offscreen_session = |
| 196 offscreen_presentation_manager_->ConnectToOffscreenPresentation( |
| 197 presentation_id, render_frame_host_id_); |
| 198 |
| 199 // If |offscreen_session| is nullptr, then the receiver is most likely |
| 200 // already gone, so the route will die soon. This should happen very rarely |
| 201 // since the receiver has to unregister itself between when offscreen |
| 202 // presentation was created and when the resulting route arrived at MR. |
| 203 if (!offscreen_session) { |
| 204 // TODO(imcheng): we should probably reject the route request in this |
| 205 // case. crbug.com/513859 |
| 206 LOG(ERROR) << "CreateOffscreenPresentationConnection returned nullptr."; |
| 207 } else { |
| 208 offscreen_presentation_sessions_.insert(presentation_id, |
| 209 offscreen_session.Pass()); |
| 210 } |
| 211 } |
| 156 } | 212 } |
| 157 | 213 |
| 158 void PresentationFrame::OnPresentationSessionClosed( | 214 void PresentationFrame::OnPresentationSessionClosed( |
| 159 const std::string& presentation_id) { | 215 const std::string& presentation_id) { |
| 160 auto it = presentation_id_to_route_id_.find(presentation_id); | 216 auto it = presentation_id_to_route_id_.find(presentation_id); |
| 161 if (it != presentation_id_to_route_id_.end()) { | 217 if (it != presentation_id_to_route_id_.end()) { |
| 162 presentation_id_to_route_id_.erase(it); | 218 presentation_id_to_route_id_.erase(it); |
| 163 } | 219 } |
| 164 // TODO(imcheng): Notify PresentationSessionStateObserver? | 220 // TODO(imcheng): Notify PresentationSessionStateObserver? |
| 165 } | 221 } |
| 166 | 222 |
| 167 const MediaRoute::Id PresentationFrame::GetRouteId( | 223 const MediaRoute::Id PresentationFrame::GetRouteId( |
| 168 const std::string& presentation_id) const { | 224 const std::string& presentation_id) const { |
| 169 auto it = presentation_id_to_route_id_.find(presentation_id); | 225 auto it = presentation_id_to_route_id_.find(presentation_id); |
| 170 return it != presentation_id_to_route_id_.end() ? it->second : ""; | 226 return it != presentation_id_to_route_id_.end() ? it->second : ""; |
| 171 } | 227 } |
| 172 | 228 |
| 173 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { | |
| 174 std::vector<MediaRoute::Id> route_ids; | |
| 175 for (const auto& e : presentation_id_to_route_id_) | |
| 176 route_ids.push_back(e.second); | |
| 177 return route_ids; | |
| 178 } | |
| 179 | |
| 180 bool PresentationFrame::SetScreenAvailabilityListener( | 229 bool PresentationFrame::SetScreenAvailabilityListener( |
| 181 content::PresentationScreenAvailabilityListener* listener) { | 230 content::PresentationScreenAvailabilityListener* listener) { |
| 182 if (sinks_observer_ && sinks_observer_->listener() == listener) { | 231 if (sinks_observer_ && sinks_observer_->listener() == listener) { |
| 183 return false; | 232 return false; |
| 184 } | 233 } |
| 185 MediaSource source(GetMediaSourceFromListener(listener)); | 234 MediaSource source(GetMediaSourceFromListener(listener)); |
| 186 sinks_observer_.reset( | 235 sinks_observer_.reset( |
| 187 new PresentationMediaSinksObserver(router_, listener, source)); | 236 new PresentationMediaSinksObserver(router_, listener, source)); |
| 188 return true; | 237 return true; |
| 189 } | 238 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 201 const MediaSource::Id& source_id) const { | 250 const MediaSource::Id& source_id) const { |
| 202 return sinks_observer_ && sinks_observer_->source().id() == source_id; | 251 return sinks_observer_ && sinks_observer_->source().id() == source_id; |
| 203 } | 252 } |
| 204 | 253 |
| 205 void PresentationFrame::Reset() { | 254 void PresentationFrame::Reset() { |
| 206 for (const auto& pid_route_id : presentation_id_to_route_id_) | 255 for (const auto& pid_route_id : presentation_id_to_route_id_) |
| 207 router_->OnPresentationSessionDetached(pid_route_id.second); | 256 router_->OnPresentationSessionDetached(pid_route_id.second); |
| 208 | 257 |
| 209 presentation_id_to_route_id_.clear(); | 258 presentation_id_to_route_id_.clear(); |
| 210 sinks_observer_.reset(); | 259 sinks_observer_.reset(); |
| 260 offscreen_presentation_sessions_.clear(); |
| 261 session_state_observers_.clear(); |
| 211 default_presentation_url_.clear(); | 262 default_presentation_url_.clear(); |
| 212 session_state_observers_.clear(); | 263 session_state_observers_.clear(); |
| 213 session_messages_observers_.clear(); | 264 session_messages_observers_.clear(); |
| 214 } | 265 } |
| 215 | 266 |
| 216 void PresentationFrame::ListenForSessionStateChange( | 267 void PresentationFrame::SendMessage( |
| 268 const content::PresentationSessionInfo& session, |
| 269 scoped_ptr<content::PresentationSessionMessage> message, |
| 270 const content::SendMessageCallback send_message_cb) { |
| 271 auto it = presentation_id_to_route_id_.find(session.presentation_id); |
| 272 if (it == presentation_id_to_route_id_.end()) { |
| 273 DVLOG(2) << "ListenForSessionMessages: no route for " |
| 274 << session.presentation_id; |
| 275 return; |
| 276 } |
| 277 |
| 278 OffscreenPresentationSession* offscreen_session = |
| 279 FindOffscreenSession(session.presentation_id); |
| 280 if (offscreen_session) { |
| 281 offscreen_session->SendMessage(message.Pass(), send_message_cb); |
| 282 } else { |
| 283 if (message->is_binary()) { |
| 284 router_->SendRouteBinaryMessage(it->second, message->data.Pass(), |
| 285 send_message_cb); |
| 286 } else { |
| 287 router_->SendRouteMessage(it->second, message->message, send_message_cb); |
| 288 } |
| 289 } |
| 290 } |
| 291 |
| 292 OffscreenPresentationSession* PresentationFrame::FindOffscreenSession( |
| 293 const std::string& presentation_id) const { |
| 294 auto offscreen_session_it = |
| 295 offscreen_presentation_sessions_.find(presentation_id); |
| 296 return offscreen_session_it != offscreen_presentation_sessions_.end() |
| 297 ? offscreen_session_it->second |
| 298 : nullptr; |
| 299 } |
| 300 |
| 301 bool PresentationFrame::ListenForSessionStateChange( |
| 217 content::PresentationSessionStateListener* listener) { | 302 content::PresentationSessionStateListener* listener) { |
| 218 std::string presentation_id(listener->GetSessionInfo().presentation_id); | 303 std::string presentation_id(listener->GetSessionInfo().presentation_id); |
| 219 auto it = presentation_id_to_route_id_.find(presentation_id); | 304 auto it = presentation_id_to_route_id_.find(presentation_id); |
| 220 if (it == presentation_id_to_route_id_.end()) { | 305 if (it == presentation_id_to_route_id_.end()) { |
| 221 DVLOG(2) << "ListenForSessionStateChange: no route for " << presentation_id; | 306 DVLOG(2) << "ListenForSessionStateChange: no route for " << presentation_id; |
| 222 return false; | 307 return false; |
| 223 } | 308 } |
| 224 | 309 |
| 225 if (ContainsKey(session_state_observers_, presentation_id)) { | 310 OffscreenPresentationSession* offscreen_session = |
| 226 DVLOG(2) << "ListenForSessionStateChange: already contains a state " | 311 FindOffscreenSession(presentation_id); |
| 227 << "observer for session " << presentation_id; | 312 if (offscreen_session) { |
| 228 return false; | 313 offscreen_session->ListenForStateChanges(listener); |
| 314 return true; |
| 315 } else { |
| 316 if (ContainsKey(session_state_observers_, presentation_id)) { |
| 317 DVLOG(2) << "ListenForSessionStateChange: already contains a state " |
| 318 << "observer for session " << presentation_id; |
| 319 return false; |
| 320 } |
| 321 |
| 322 session_state_observers_.insert( |
| 323 presentation_id, make_scoped_ptr(new PresentationSessionStateObserver( |
| 324 listener, it->second, router_))); |
| 325 return true; |
| 229 } | 326 } |
| 230 | |
| 231 session_state_observers_.insert( | |
| 232 presentation_id, make_scoped_ptr(new PresentationSessionStateObserver( | |
| 233 listener, it->second, router_))); | |
| 234 return true; | |
| 235 } | 327 } |
| 236 | 328 |
| 237 void PresentationFrame::ListenForSessionMessages( | 329 void PresentationFrame::ListenForSessionMessages( |
| 238 const content::PresentationSessionInfo& session, | 330 const content::PresentationSessionInfo& session, |
| 239 const content::PresentationSessionMessageCallback& message_cb) { | 331 const content::PresentationSessionMessageCallback& message_cb) { |
| 240 auto it = presentation_id_to_route_id_.find(session.presentation_id); | 332 auto it = presentation_id_to_route_id_.find(session.presentation_id); |
| 241 if (it == presentation_id_to_route_id_.end()) { | 333 if (it == presentation_id_to_route_id_.end()) { |
| 242 DVLOG(2) << "ListenForSessionMessages: no route for " | 334 DVLOG(2) << "ListenForSessionMessages: no route for " |
| 243 << session.presentation_id; | 335 << session.presentation_id; |
| 244 return; | 336 return; |
| 245 } | 337 } |
| 246 | 338 |
| 247 session_messages_observers_.push_back( | 339 OffscreenPresentationSession* offscreen_session = |
| 248 new PresentationSessionMessagesObserver(message_cb, it->second, router_)); | 340 FindOffscreenSession(session.presentation_id); |
| 341 if (offscreen_session) { |
| 342 offscreen_session->ListenForMessages(message_cb); |
| 343 } else { |
| 344 session_messages_observers_.push_back( |
| 345 new PresentationSessionMessagesObserver(message_cb, it->second, |
| 346 router_)); |
| 347 } |
| 249 } | 348 } |
| 250 | 349 |
| 251 MediaSource PresentationFrame::GetMediaSourceFromListener( | 350 MediaSource PresentationFrame::GetMediaSourceFromListener( |
| 252 content::PresentationScreenAvailabilityListener* listener) const { | 351 content::PresentationScreenAvailabilityListener* listener) const { |
| 253 // If the default presentation URL is empty then fall back to tab mirroring. | 352 // If the default presentation URL is empty then fall back to tab mirroring. |
| 254 std::string availability_url(listener->GetAvailabilityUrl()); | 353 std::string availability_url(listener->GetAvailabilityUrl()); |
| 255 return availability_url.empty() | 354 return availability_url.empty() |
| 256 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) | 355 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_)) |
| 257 : MediaSourceForPresentationUrl(availability_url); | 356 : MediaSourceForPresentationUrl(availability_url); |
| 258 } | 357 } |
| 259 | 358 |
| 260 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. | 359 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. |
| 261 class PresentationFrameManager { | 360 class PresentationFrameManager { |
| 262 public: | 361 public: |
| 263 PresentationFrameManager(content::WebContents* web_contents, | 362 PresentationFrameManager(content::WebContents* web_contents, |
| 264 MediaRouter* router); | 363 MediaRouter* router); |
| 265 ~PresentationFrameManager(); | 364 ~PresentationFrameManager(); |
| 266 | 365 |
| 267 // Mirror corresponding APIs in PresentationServiceDelegateImpl. | 366 // Mirror corresponding APIs in PresentationServiceDelegateImpl. |
| 268 bool SetScreenAvailabilityListener( | 367 bool SetScreenAvailabilityListener( |
| 269 const RenderFrameHostId& render_frame_host_id, | 368 const RenderFrameHostId& render_frame_host_id, |
| 270 content::PresentationScreenAvailabilityListener* listener); | 369 content::PresentationScreenAvailabilityListener* listener); |
| 271 bool RemoveScreenAvailabilityListener( | 370 bool RemoveScreenAvailabilityListener( |
| 272 const RenderFrameHostId& render_frame_host_id, | 371 const RenderFrameHostId& render_frame_host_id, |
| 273 content::PresentationScreenAvailabilityListener* listener); | 372 content::PresentationScreenAvailabilityListener* listener); |
| 274 void SetDefaultPresentationUrl(const RenderFrameHostId& render_frame_host_id, | 373 void SetDefaultPresentationUrl(const RenderFrameHostId& render_frame_host_id, |
| 275 const std::string& default_presentation_url); | 374 const std::string& default_presentation_url); |
| 375 void SendMessage(const RenderFrameHostId& render_frame_host_id, |
| 376 const content::PresentationSessionInfo& session, |
| 377 scoped_ptr<content::PresentationSessionMessage> message, |
| 378 const content::SendMessageCallback send_message_cb); |
| 276 bool ListenForSessionStateChange( | 379 bool ListenForSessionStateChange( |
| 277 const RenderFrameHostId& render_frame_host_id, | 380 const RenderFrameHostId& render_frame_host_id, |
| 278 content::PresentationSessionStateListener* listener); | 381 content::PresentationSessionStateListener* listener); |
| 279 void ListenForSessionMessages( | 382 void ListenForSessionMessages( |
| 280 const RenderFrameHostId& render_frame_host_id, | 383 const RenderFrameHostId& render_frame_host_id, |
| 281 const content::PresentationSessionInfo& session, | 384 const content::PresentationSessionInfo& session, |
| 282 const content::PresentationSessionMessageCallback& message_cb); | 385 const content::PresentationSessionMessageCallback& message_cb); |
| 283 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, | 386 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, |
| 284 DelegateObserver* observer); | 387 DelegateObserver* observer); |
| 285 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); | 388 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); |
| 286 void Reset(const RenderFrameHostId& render_frame_host_id); | 389 void Reset(const RenderFrameHostId& render_frame_host_id); |
| 287 bool HasScreenAvailabilityListenerForTest( | 390 bool HasScreenAvailabilityListenerForTest( |
| 288 const RenderFrameHostId& render_frame_host_id, | 391 const RenderFrameHostId& render_frame_host_id, |
| 289 const MediaSource::Id& source_id) const; | 392 const MediaSource::Id& source_id) const; |
| 290 void SetMediaRouterForTest(MediaRouter* router); | 393 void SetMediaRouterForTest(MediaRouter* router); |
| 291 | 394 |
| 292 void OnPresentationSessionStarted( | 395 void OnPresentationSessionStarted( |
| 293 const RenderFrameHostId& render_frame_host_id, | 396 const RenderFrameHostId& render_frame_host_id, |
| 294 bool is_default_presentation, | 397 bool is_default_presentation, |
| 295 const content::PresentationSessionInfo& session, | 398 const content::PresentationSessionInfo& session, |
| 296 const MediaRoute::Id& route_id); | 399 const MediaRoute& route); |
| 297 | 400 |
| 298 void OnPresentationSessionClosed( | 401 void OnPresentationSessionClosed( |
| 299 const RenderFrameHostId& render_frame_host_id, | 402 const RenderFrameHostId& render_frame_host_id, |
| 300 const std::string& presentation_id); | 403 const std::string& presentation_id); |
| 301 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, | 404 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, |
| 302 const std::string& presentation_id) const; | 405 const std::string& presentation_id) const; |
| 303 const std::vector<MediaRoute::Id> GetRouteIds( | |
| 304 const RenderFrameHostId& render_frame_host_id) const; | |
| 305 | 406 |
| 306 private: | 407 private: |
| 307 PresentationFrame* GetOrAddPresentationFrame( | 408 PresentationFrame* GetOrAddPresentationFrame( |
| 308 const RenderFrameHostId& render_frame_host_id); | 409 const RenderFrameHostId& render_frame_host_id); |
| 309 | 410 |
| 310 // Maps a frame identifier to a PresentationFrame object for frames | 411 // Maps a frame identifier to a PresentationFrame object for frames |
| 311 // that are using presentation API. | 412 // that are using presentation API. |
| 312 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> | 413 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> |
| 313 presentation_frames_; | 414 presentation_frames_; |
| 314 | 415 |
| 315 // References to the owning WebContents, and the corresponding MediaRouter. | 416 // References to the owning WebContents, and the corresponding MediaRouter. |
| 417 content::WebContents* const web_contents_; |
| 316 MediaRouter* router_; | 418 MediaRouter* router_; |
| 317 content::WebContents* web_contents_; | |
| 318 }; | 419 }; |
| 319 | 420 |
| 320 PresentationFrameManager::PresentationFrameManager( | 421 PresentationFrameManager::PresentationFrameManager( |
| 321 content::WebContents* web_contents, | 422 content::WebContents* web_contents, |
| 322 MediaRouter* router) | 423 MediaRouter* router) |
| 323 : router_(router), web_contents_(web_contents) { | 424 : web_contents_(web_contents), router_(router) { |
| 324 DCHECK(web_contents_); | 425 DCHECK(web_contents_); |
| 325 DCHECK(router_); | 426 DCHECK(router_); |
| 326 } | 427 } |
| 327 | 428 |
| 328 PresentationFrameManager::~PresentationFrameManager() { | 429 PresentationFrameManager::~PresentationFrameManager() { |
| 329 for (auto& frame : presentation_frames_) | 430 for (auto& frame : presentation_frames_) |
| 330 frame.second->OnPresentationServiceDelegateDestroyed(); | 431 frame.second->OnPresentationServiceDelegateDestroyed(); |
| 331 } | 432 } |
| 332 | 433 |
| 333 void PresentationFrameManager::OnPresentationSessionStarted( | 434 void PresentationFrameManager::OnPresentationSessionStarted( |
| 334 const RenderFrameHostId& render_frame_host_id, | 435 const RenderFrameHostId& render_frame_host_id, |
| 335 bool is_default_presentation, | 436 bool is_default_presentation, |
| 336 const content::PresentationSessionInfo& session, | 437 const content::PresentationSessionInfo& session, |
| 337 const MediaRoute::Id& route_id) { | 438 const MediaRoute& route) { |
| 338 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 439 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 339 if (presentation_frame) | 440 if (presentation_frame) { |
| 340 presentation_frame->OnPresentationSessionStarted(is_default_presentation, | 441 presentation_frame->OnPresentationSessionStarted(is_default_presentation, |
| 341 session, route_id); | 442 session, route); |
| 443 } |
| 342 } | 444 } |
| 343 | 445 |
| 344 void PresentationFrameManager::OnPresentationSessionClosed( | 446 void PresentationFrameManager::OnPresentationSessionClosed( |
| 345 const RenderFrameHostId& render_frame_host_id, | 447 const RenderFrameHostId& render_frame_host_id, |
| 346 const std::string& presentation_id) { | 448 const std::string& presentation_id) { |
| 347 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 449 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 348 if (presentation_frame) | 450 if (presentation_frame) |
| 349 presentation_frame->OnPresentationSessionClosed(presentation_id); | 451 presentation_frame->OnPresentationSessionClosed(presentation_id); |
| 350 } | 452 } |
| 351 | 453 |
| 352 const MediaRoute::Id PresentationFrameManager::GetRouteId( | 454 const MediaRoute::Id PresentationFrameManager::GetRouteId( |
| 353 const RenderFrameHostId& render_frame_host_id, | 455 const RenderFrameHostId& render_frame_host_id, |
| 354 const std::string& presentation_id) const { | 456 const std::string& presentation_id) const { |
| 355 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 457 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 356 return presentation_frame ? presentation_frame->GetRouteId(presentation_id) | 458 return presentation_frame ? presentation_frame->GetRouteId(presentation_id) |
| 357 : ""; | 459 : ""; |
| 358 } | 460 } |
| 359 | 461 |
| 360 const std::vector<MediaRoute::Id> PresentationFrameManager::GetRouteIds( | |
| 361 const RenderFrameHostId& render_frame_host_id) const { | |
| 362 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | |
| 363 return presentation_frame ? presentation_frame->GetRouteIds() | |
| 364 : std::vector<MediaRoute::Id>(); | |
| 365 } | |
| 366 | |
| 367 bool PresentationFrameManager::SetScreenAvailabilityListener( | 462 bool PresentationFrameManager::SetScreenAvailabilityListener( |
| 368 const RenderFrameHostId& render_frame_host_id, | 463 const RenderFrameHostId& render_frame_host_id, |
| 369 content::PresentationScreenAvailabilityListener* listener) { | 464 content::PresentationScreenAvailabilityListener* listener) { |
| 370 DCHECK(listener); | 465 DCHECK(listener); |
| 371 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 466 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 372 return presentation_frame->SetScreenAvailabilityListener(listener); | 467 return presentation_frame->SetScreenAvailabilityListener(listener); |
| 373 } | 468 } |
| 374 | 469 |
| 375 bool PresentationFrameManager::RemoveScreenAvailabilityListener( | 470 bool PresentationFrameManager::RemoveScreenAvailabilityListener( |
| 376 const RenderFrameHostId& render_frame_host_id, | 471 const RenderFrameHostId& render_frame_host_id, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 389 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); | 484 presentation_frame->HasScreenAvailabilityListenerForTest(source_id); |
| 390 } | 485 } |
| 391 | 486 |
| 392 void PresentationFrameManager::SetDefaultPresentationUrl( | 487 void PresentationFrameManager::SetDefaultPresentationUrl( |
| 393 const RenderFrameHostId& render_frame_host_id, | 488 const RenderFrameHostId& render_frame_host_id, |
| 394 const std::string& default_presentation_url) { | 489 const std::string& default_presentation_url) { |
| 395 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | 490 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); |
| 396 presentation_frame->set_default_presentation_url(default_presentation_url); | 491 presentation_frame->set_default_presentation_url(default_presentation_url); |
| 397 } | 492 } |
| 398 | 493 |
| 399 void PresentationFrameManager::ListenForSessionStateChange( | 494 void PresentationFrameManager::SendMessage( |
| 495 const RenderFrameHostId& render_frame_host_id, |
| 496 const content::PresentationSessionInfo& session, |
| 497 scoped_ptr<content::PresentationSessionMessage> message, |
| 498 const content::SendMessageCallback send_message_cb) { |
| 499 PresentationFrame* presentation_frame = |
| 500 presentation_frames_.get(render_frame_host_id); |
| 501 if (!presentation_frame) { |
| 502 DVLOG(2) << "SendMessage: PresentationFrame does not exist " |
| 503 << "for: (" << render_frame_host_id.first << ", " |
| 504 << render_frame_host_id.second << ")"; |
| 505 send_message_cb.Run(false); |
| 506 return; |
| 507 } |
| 508 |
| 509 presentation_frame->SendMessage(session, message.Pass(), send_message_cb); |
| 510 } |
| 511 |
| 512 bool PresentationFrameManager::ListenForSessionStateChange( |
| 400 const RenderFrameHostId& render_frame_host_id, | 513 const RenderFrameHostId& render_frame_host_id, |
| 401 content::PresentationSessionStateListener* listener) { | 514 content::PresentationSessionStateListener* listener) { |
| 402 PresentationFrame* presentation_frame = | 515 PresentationFrame* presentation_frame = |
| 403 GetOrAddPresentationFrame(render_frame_host_id); | 516 GetOrAddPresentationFrame(render_frame_host_id); |
| 404 return presentation_frame->ListenForSessionStateChange(listener); | 517 return presentation_frame->ListenForSessionStateChange(listener); |
| 405 } | 518 } |
| 406 | 519 |
| 407 void PresentationFrameManager::ListenForSessionMessages( | 520 void PresentationFrameManager::ListenForSessionMessages( |
| 408 const RenderFrameHostId& render_frame_host_id, | 521 const RenderFrameHostId& render_frame_host_id, |
| 409 const content::PresentationSessionInfo& session, | 522 const content::PresentationSessionInfo& session, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 440 auto presentation_frame = presentation_frames_.get(render_frame_host_id); | 553 auto presentation_frame = presentation_frames_.get(render_frame_host_id); |
| 441 if (presentation_frame) | 554 if (presentation_frame) |
| 442 presentation_frame->Reset(); | 555 presentation_frame->Reset(); |
| 443 } | 556 } |
| 444 | 557 |
| 445 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( | 558 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( |
| 446 const RenderFrameHostId& render_frame_host_id) { | 559 const RenderFrameHostId& render_frame_host_id) { |
| 447 if (!presentation_frames_.contains(render_frame_host_id)) { | 560 if (!presentation_frames_.contains(render_frame_host_id)) { |
| 448 presentation_frames_.add( | 561 presentation_frames_.add( |
| 449 render_frame_host_id, | 562 render_frame_host_id, |
| 450 scoped_ptr<PresentationFrame>( | 563 scoped_ptr<PresentationFrame>(new PresentationFrame( |
| 451 new PresentationFrame(web_contents_, router_))); | 564 web_contents_, render_frame_host_id, router_))); |
| 452 } | 565 } |
| 453 return presentation_frames_.get(render_frame_host_id); | 566 return presentation_frames_.get(render_frame_host_id); |
| 454 } | 567 } |
| 455 | 568 |
| 456 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { | 569 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { |
| 457 router_ = router; | 570 router_ = router; |
| 458 } | 571 } |
| 459 | 572 |
| 460 PresentationServiceDelegateImpl* | 573 PresentationServiceDelegateImpl* |
| 461 PresentationServiceDelegateImpl::GetOrCreateForWebContents( | 574 PresentationServiceDelegateImpl::GetOrCreateForWebContents( |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 const MediaRoute* route, | 684 const MediaRoute* route, |
| 572 const std::string& presentation_id, | 685 const std::string& presentation_id, |
| 573 const std::string& error_text) { | 686 const std::string& error_text) { |
| 574 if (!route) { | 687 if (!route) { |
| 575 error_cb.Run(content::PresentationError( | 688 error_cb.Run(content::PresentationError( |
| 576 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); | 689 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); |
| 577 } else { | 690 } else { |
| 578 DVLOG(1) << "OnJoinRouteResponse: " | 691 DVLOG(1) << "OnJoinRouteResponse: " |
| 579 << "route_id: " << route->media_route_id() | 692 << "route_id: " << route->media_route_id() |
| 580 << ", presentation URL: " << session.presentation_url | 693 << ", presentation URL: " << session.presentation_url |
| 581 << ", presentation ID: " << session.presentation_id; | 694 << ", presentation ID: " << session.presentation_id |
| 695 << ", offscreen? " << route->is_offscreen_presentation(); |
| 582 DCHECK_EQ(session.presentation_id, presentation_id); | 696 DCHECK_EQ(session.presentation_id, presentation_id); |
| 583 frame_manager_->OnPresentationSessionStarted( | 697 RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 584 RenderFrameHostId(render_process_id, render_frame_id), false, session, | 698 frame_manager_->OnPresentationSessionStarted(rfh_id, false, session, |
| 585 route->media_route_id()); | 699 *route); |
| 586 success_cb.Run(session); | 700 success_cb.Run(session); |
| 587 } | 701 } |
| 588 } | 702 } |
| 589 | 703 |
| 590 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( | 704 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( |
| 591 int render_process_id, | 705 int render_process_id, |
| 592 int render_frame_id, | 706 int render_frame_id, |
| 593 const PresentationSessionSuccessCallback& success_cb, | 707 const PresentationSessionSuccessCallback& success_cb, |
| 594 const content::PresentationSessionInfo& new_session, | 708 const content::PresentationSessionInfo& new_session, |
| 595 const MediaRoute::Id& route_id) { | 709 const MediaRoute& route) { |
| 710 const MediaRoute::Id& route_id = route.media_route_id(); |
| 596 DVLOG(1) << "OnStartSessionSucceeded: " | 711 DVLOG(1) << "OnStartSessionSucceeded: " |
| 597 << "route_id: " << route_id | 712 << "route_id: " << route_id |
| 598 << ", presentation URL: " << new_session.presentation_url | 713 << ", presentation URL: " << new_session.presentation_url |
| 599 << ", presentation ID: " << new_session.presentation_id; | 714 << ", presentation ID: " << new_session.presentation_id; |
| 600 frame_manager_->OnPresentationSessionStarted( | 715 RenderFrameHostId rfh_id(render_process_id, render_frame_id); |
| 601 RenderFrameHostId(render_process_id, render_frame_id), false, new_session, | 716 frame_manager_->OnPresentationSessionStarted(rfh_id, false, new_session, |
| 602 route_id); | 717 route); |
| 603 success_cb.Run(new_session); | 718 success_cb.Run(new_session); |
| 604 } | 719 } |
| 605 | 720 |
| 606 void PresentationServiceDelegateImpl::StartSession( | 721 void PresentationServiceDelegateImpl::StartSession( |
| 607 int render_process_id, | 722 int render_process_id, |
| 608 int render_frame_id, | 723 int render_frame_id, |
| 609 const std::string& presentation_url, | 724 const std::string& presentation_url, |
| 610 const PresentationSessionSuccessCallback& success_cb, | 725 const PresentationSessionSuccessCallback& success_cb, |
| 611 const PresentationSessionErrorCallback& error_cb) { | 726 const PresentationSessionErrorCallback& error_cb) { |
| 612 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { | 727 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 return; | 780 return; |
| 666 } | 781 } |
| 667 router_->CloseRoute(route_id); | 782 router_->CloseRoute(route_id); |
| 668 } | 783 } |
| 669 | 784 |
| 670 void PresentationServiceDelegateImpl::ListenForSessionMessages( | 785 void PresentationServiceDelegateImpl::ListenForSessionMessages( |
| 671 int render_process_id, | 786 int render_process_id, |
| 672 int render_frame_id, | 787 int render_frame_id, |
| 673 const content::PresentationSessionInfo& session, | 788 const content::PresentationSessionInfo& session, |
| 674 const content::PresentationSessionMessageCallback& message_cb) { | 789 const content::PresentationSessionMessageCallback& message_cb) { |
| 675 frame_manager_->ListenForSessionMessages( | 790 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 676 RenderFrameHostId(render_process_id, render_frame_id), session, | 791 frame_manager_->ListenForSessionMessages(render_frame_host_id, session, |
| 677 message_cb); | 792 message_cb); |
| 678 } | 793 } |
| 679 | 794 |
| 680 void PresentationServiceDelegateImpl::SendMessage( | 795 void PresentationServiceDelegateImpl::SendMessage( |
| 681 int render_process_id, | 796 int render_process_id, |
| 682 int render_frame_id, | 797 int render_frame_id, |
| 683 const content::PresentationSessionInfo& session, | 798 const content::PresentationSessionInfo& session, |
| 684 scoped_ptr<content::PresentationSessionMessage> message, | 799 scoped_ptr<content::PresentationSessionMessage> message, |
| 685 const SendMessageCallback& send_message_cb) { | 800 const content::SendMessageCallback& send_message_cb) { |
| 686 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( | 801 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 687 RenderFrameHostId(render_process_id, render_frame_id), | 802 frame_manager_->SendMessage(render_frame_host_id, session, message.Pass(), |
| 688 session.presentation_id); | 803 send_message_cb); |
| 689 if (route_id.empty()) { | |
| 690 DVLOG(1) << "No active route for " << session.presentation_id; | |
| 691 send_message_cb.Run(false); | |
| 692 return; | |
| 693 } | |
| 694 | |
| 695 if (message->is_binary()) { | |
| 696 router_->SendRouteBinaryMessage(route_id, message->data.Pass(), | |
| 697 send_message_cb); | |
| 698 } else { | |
| 699 router_->SendRouteMessage(route_id, message->message, send_message_cb); | |
| 700 } | |
| 701 } | 804 } |
| 702 | 805 |
| 703 bool PresentationServiceDelegateImpl::ListenForSessionStateChange( | 806 bool PresentationServiceDelegateImpl::ListenForSessionStateChange( |
| 704 int render_process_id, | 807 int render_process_id, |
| 705 int render_frame_id, | 808 int render_frame_id, |
| 706 content::PresentationSessionStateListener* listener) { | 809 content::PresentationSessionStateListener* listener) { |
| 707 return frame_manager_->ListenForSessionStateChange( | 810 return frame_manager_->ListenForSessionStateChange( |
| 708 RenderFrameHostId(render_process_id, render_frame_id), listener); | 811 RenderFrameHostId(render_process_id, render_frame_id), listener); |
| 709 } | 812 } |
| 710 | 813 |
| 814 void PresentationServiceDelegateImpl::GetPresentationReceiverSession( |
| 815 int render_process_id, |
| 816 int render_frame_id, |
| 817 const content::PresentationReceiverSessionAvailableCallback& callback) { |
| 818 // We only support receiver APIs in offscreen tabs created for offscreen |
| 819 // presentations. |
| 820 // See ReceiverPresentationServiceDelegateImpl for details. |
| 821 NOTIMPLEMENTED(); |
| 822 callback.Run(nullptr); |
| 823 } |
| 824 |
| 825 std::vector<content::PresentationSessionInfo> |
| 826 PresentationServiceDelegateImpl::GetPresentationReceiverSessions( |
| 827 int render_process_id, |
| 828 int render_frame_id) { |
| 829 // See comment in |GetPresentationReceiverSession()|. |
| 830 NOTIMPLEMENTED(); |
| 831 return std::vector<content::PresentationSessionInfo>(); |
| 832 } |
| 833 |
| 711 void PresentationServiceDelegateImpl::OnRouteResponse( | 834 void PresentationServiceDelegateImpl::OnRouteResponse( |
| 712 const MediaRoute* route, | 835 const MediaRoute* route, |
| 713 const std::string& presentation_id, | 836 const std::string& presentation_id, |
| 714 const std::string& error) { | 837 const std::string& error) { |
| 715 if (!route) | 838 if (!route) |
| 716 return; | 839 return; |
| 840 |
| 717 const MediaSource& source = route->media_source(); | 841 const MediaSource& source = route->media_source(); |
| 718 DCHECK(!source.Empty()); | 842 DCHECK(!source.Empty()); |
| 719 if (!default_source_.Equals(source)) | 843 if (!default_source_.Equals(source)) |
| 720 return; | 844 return; |
| 845 |
| 721 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); | 846 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); |
| 722 if (!main_frame) | 847 if (!main_frame) |
| 723 return; | 848 return; |
| 849 |
| 724 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); | 850 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame)); |
| 725 frame_manager_->OnPresentationSessionStarted( | 851 content::PresentationSessionInfo session( |
| 726 render_frame_host_id, true, | 852 PresentationUrlFromMediaSource(source), presentation_id); |
| 727 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source), | 853 |
| 728 presentation_id), | 854 frame_manager_->OnPresentationSessionStarted(render_frame_host_id, true, |
| 729 route->media_route_id()); | 855 session, *route); |
| 730 } | 856 } |
| 731 | 857 |
| 732 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( | 858 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( |
| 733 DefaultMediaSourceObserver* observer) { | 859 DefaultMediaSourceObserver* observer) { |
| 734 default_media_source_observers_.AddObserver(observer); | 860 default_media_source_observers_.AddObserver(observer); |
| 735 } | 861 } |
| 736 | 862 |
| 737 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( | 863 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( |
| 738 DefaultMediaSourceObserver* observer) { | 864 DefaultMediaSourceObserver* observer) { |
| 739 default_media_source_observers_.RemoveObserver(observer); | 865 default_media_source_observers_.RemoveObserver(observer); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 753 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 879 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
| 754 int render_process_id, | 880 int render_process_id, |
| 755 int render_frame_id, | 881 int render_frame_id, |
| 756 const MediaSource::Id& source_id) const { | 882 const MediaSource::Id& source_id) const { |
| 757 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 883 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
| 758 return frame_manager_->HasScreenAvailabilityListenerForTest( | 884 return frame_manager_->HasScreenAvailabilityListenerForTest( |
| 759 render_frame_host_id, source_id); | 885 render_frame_host_id, source_id); |
| 760 } | 886 } |
| 761 | 887 |
| 762 } // namespace media_router | 888 } // namespace media_router |
| OLD | NEW |