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> |
11 | 11 |
12 #include "base/containers/small_map.h" | 12 #include "base/containers/small_map.h" |
13 #include "base/guid.h" | 13 #include "base/guid.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
17 #include "chrome/browser/media/router/create_presentation_connection_request.h" | 17 #include "chrome/browser/media/router/create_presentation_connection_request.h" |
18 #include "chrome/browser/media/router/media_route.h" | 18 #include "chrome/browser/media/router/media_route.h" |
19 #include "chrome/browser/media/router/media_router.h" | 19 #include "chrome/browser/media/router/media_router.h" |
20 #include "chrome/browser/media/router/media_router_dialog_controller.h" | 20 #include "chrome/browser/media/router/media_router_dialog_controller.h" |
21 #include "chrome/browser/media/router/media_router_factory.h" | 21 #include "chrome/browser/media/router/media_router_factory.h" |
22 #include "chrome/browser/media/router/media_sink.h" | 22 #include "chrome/browser/media/router/media_sink.h" |
23 #include "chrome/browser/media/router/media_source_helper.h" | 23 #include "chrome/browser/media/router/media_source_helper.h" |
24 #include "chrome/browser/media/router/offscreen_presentation_manager.h" | |
25 #include "chrome/browser/media/router/offscreen_presentation_manager_factory.h" | |
24 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" | 26 #include "chrome/browser/media/router/presentation_media_sinks_observer.h" |
25 #include "chrome/browser/media/router/route_message.h" | 27 #include "chrome/browser/media/router/route_message.h" |
26 #include "chrome/browser/media/router/route_message_observer.h" | 28 #include "chrome/browser/media/router/route_message_observer.h" |
27 #include "chrome/browser/media/router/route_request_result.h" | 29 #include "chrome/browser/media/router/route_request_result.h" |
28 #include "chrome/browser/sessions/session_tab_helper.h" | 30 #include "chrome/browser/sessions/session_tab_helper.h" |
29 #include "content/public/browser/browser_context.h" | 31 #include "content/public/browser/browser_context.h" |
30 #include "content/public/browser/presentation_screen_availability_listener.h" | 32 #include "content/public/browser/presentation_screen_availability_listener.h" |
31 #include "content/public/browser/presentation_session.h" | 33 #include "content/public/browser/presentation_session.h" |
32 #include "content/public/browser/render_frame_host.h" | 34 #include "content/public/browser/render_frame_host.h" |
33 #include "content/public/browser/render_process_host.h" | 35 #include "content/public/browser/render_process_host.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 143 |
142 void OnPresentationSessionStarted( | 144 void OnPresentationSessionStarted( |
143 const content::PresentationSessionInfo& session, | 145 const content::PresentationSessionInfo& session, |
144 const MediaRoute::Id& route_id); | 146 const MediaRoute::Id& route_id); |
145 void OnPresentationServiceDelegateDestroyed() const; | 147 void OnPresentationServiceDelegateDestroyed() const; |
146 | 148 |
147 void set_delegate_observer(DelegateObserver* observer) { | 149 void set_delegate_observer(DelegateObserver* observer) { |
148 delegate_observer_ = observer; | 150 delegate_observer_ = observer; |
149 } | 151 } |
150 | 152 |
153 void SetOffscreenPresentationClient( | |
154 content::OffscreenPresentationClient* client) { | |
155 offscreen_presentation_client_ = client; | |
156 } | |
157 | |
151 private: | 158 private: |
152 MediaSource GetMediaSourceFromListener( | 159 MediaSource GetMediaSourceFromListener( |
153 content::PresentationScreenAvailabilityListener* listener) const; | 160 content::PresentationScreenAvailabilityListener* listener) const; |
154 base::SmallMap<std::map<std::string, MediaRoute::Id>> | 161 base::SmallMap<std::map<std::string, MediaRoute::Id>> |
155 presentation_id_to_route_id_; | 162 presentation_id_to_route_id_; |
156 base::SmallMap< | 163 base::SmallMap< |
157 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> | 164 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> |
158 url_to_sinks_observer_; | 165 url_to_sinks_observer_; |
159 std::unordered_map< | 166 std::unordered_map< |
160 MediaRoute::Id, | 167 MediaRoute::Id, |
161 std::unique_ptr<PresentationConnectionStateSubscription>> | 168 std::unique_ptr<PresentationConnectionStateSubscription>> |
162 connection_state_subscriptions_; | 169 connection_state_subscriptions_; |
163 std::unordered_map< | 170 std::unordered_map< |
164 MediaRoute::Id, | 171 MediaRoute::Id, |
165 std::unique_ptr<PresentationSessionMessagesObserver>> | 172 std::unique_ptr<PresentationSessionMessagesObserver>> |
166 session_messages_observers_; | 173 session_messages_observers_; |
167 | 174 |
168 RenderFrameHostId render_frame_host_id_; | 175 RenderFrameHostId render_frame_host_id_; |
169 | 176 |
170 // References to the owning WebContents, and the corresponding MediaRouter. | 177 // References to the owning WebContents, and the corresponding MediaRouter. |
171 const content::WebContents* web_contents_; | 178 const content::WebContents* web_contents_; |
172 MediaRouter* router_; | 179 MediaRouter* router_; |
173 | 180 |
181 OffscreenPresentationManager* const offscreen_presentation_manager_; | |
174 DelegateObserver* delegate_observer_; | 182 DelegateObserver* delegate_observer_; |
183 content::OffscreenPresentationClient* offscreen_presentation_client_; | |
175 }; | 184 }; |
176 | 185 |
177 PresentationFrame::PresentationFrame( | 186 PresentationFrame::PresentationFrame( |
178 const RenderFrameHostId& render_frame_host_id, | 187 const RenderFrameHostId& render_frame_host_id, |
179 content::WebContents* web_contents, | 188 content::WebContents* web_contents, |
180 MediaRouter* router) | 189 MediaRouter* router) |
181 : render_frame_host_id_(render_frame_host_id), | 190 : render_frame_host_id_(render_frame_host_id), |
182 web_contents_(web_contents), | 191 web_contents_(web_contents), |
183 router_(router), | 192 router_(router), |
184 delegate_observer_(nullptr) { | 193 offscreen_presentation_manager_( |
194 OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext( | |
195 web_contents_->GetBrowserContext())), | |
196 delegate_observer_(nullptr), | |
197 offscreen_presentation_client_(nullptr) { | |
185 DCHECK(web_contents_); | 198 DCHECK(web_contents_); |
186 DCHECK(router_); | 199 DCHECK(router_); |
187 } | 200 } |
188 | 201 |
189 PresentationFrame::~PresentationFrame() { | 202 PresentationFrame::~PresentationFrame() { |
190 } | 203 } |
191 | 204 |
192 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { | 205 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { |
193 if (delegate_observer_) | 206 if (delegate_observer_) |
194 delegate_observer_->OnDelegateDestroyed(); | 207 delegate_observer_->OnDelegateDestroyed(); |
195 } | 208 } |
196 | 209 |
197 void PresentationFrame::OnPresentationSessionStarted( | 210 void PresentationFrame::OnPresentationSessionStarted( |
198 const content::PresentationSessionInfo& session, | 211 const content::PresentationSessionInfo& session, |
199 const MediaRoute::Id& route_id) { | 212 const MediaRoute::Id& route_id) { |
200 presentation_id_to_route_id_[session.presentation_id] = route_id; | 213 presentation_id_to_route_id_[session.presentation_id] = route_id; |
214 offscreen_presentation_manager_->RegisterOffscreenPresentationController( | |
imcheng
2016/09/28 07:28:37
Don't we need to check the started presentation is
zhaobin
2016/09/29 17:20:44
Yes, we need to check if it is offscreen presentat
| |
215 session.presentation_id, render_frame_host_id_.second, | |
216 offscreen_presentation_client_); | |
201 } | 217 } |
202 | 218 |
203 const MediaRoute::Id PresentationFrame::GetRouteId( | 219 const MediaRoute::Id PresentationFrame::GetRouteId( |
204 const std::string& presentation_id) const { | 220 const std::string& presentation_id) const { |
205 auto it = presentation_id_to_route_id_.find(presentation_id); | 221 auto it = presentation_id_to_route_id_.find(presentation_id); |
206 return it != presentation_id_to_route_id_.end() ? it->second : ""; | 222 return it != presentation_id_to_route_id_.end() ? it->second : ""; |
207 } | 223 } |
208 | 224 |
209 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { | 225 const std::vector<MediaRoute::Id> PresentationFrame::GetRouteIds() const { |
210 std::vector<MediaRoute::Id> route_ids; | 226 std::vector<MediaRoute::Id> route_ids; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 } | 260 } |
245 return false; | 261 return false; |
246 } | 262 } |
247 | 263 |
248 bool PresentationFrame::HasScreenAvailabilityListenerForTest( | 264 bool PresentationFrame::HasScreenAvailabilityListenerForTest( |
249 const MediaSource::Id& source_id) const { | 265 const MediaSource::Id& source_id) const { |
250 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); | 266 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); |
251 } | 267 } |
252 | 268 |
253 void PresentationFrame::Reset() { | 269 void PresentationFrame::Reset() { |
254 for (const auto& pid_route_id : presentation_id_to_route_id_) | 270 for (const auto& pid_route_id : presentation_id_to_route_id_) { |
255 router_->DetachRoute(pid_route_id.second); | 271 router_->DetachRoute(pid_route_id.second); |
272 offscreen_presentation_manager_->UnregisterOffscreenPresentationController( | |
imcheng
2016/09/28 07:28:37
Something to think about: how do we tell the recei
zhaobin
2016/09/29 17:20:44
Original plan: In receiver's PSImpl::CloseConnecti
| |
273 pid_route_id.first, render_frame_host_id_.second); | |
274 } | |
256 | 275 |
257 presentation_id_to_route_id_.clear(); | 276 presentation_id_to_route_id_.clear(); |
258 url_to_sinks_observer_.clear(); | 277 url_to_sinks_observer_.clear(); |
259 connection_state_subscriptions_.clear(); | 278 connection_state_subscriptions_.clear(); |
260 session_messages_observers_.clear(); | 279 session_messages_observers_.clear(); |
261 } | 280 } |
262 | 281 |
263 void PresentationFrame::RemoveConnection(const std::string& presentation_id, | 282 void PresentationFrame::RemoveConnection(const std::string& presentation_id, |
264 const MediaRoute::Id& route_id) { | 283 const MediaRoute::Id& route_id) { |
265 // Remove the presentation id mapping so a later call to Reset is a no-op. | 284 // Remove the presentation id mapping so a later call to Reset is a no-op. |
266 presentation_id_to_route_id_.erase(presentation_id); | 285 presentation_id_to_route_id_.erase(presentation_id); |
267 | 286 |
268 // We no longer need to observe route messages. | 287 // We no longer need to observe route messages. |
269 session_messages_observers_.erase(route_id); | 288 session_messages_observers_.erase(route_id); |
270 | 289 |
271 // We keep the PresentationConnectionStateChangedCallback registered with MR | 290 // We keep the PresentationConnectionStateChangedCallback registered with MR |
272 // so the MRP can tell us when terminate() completed. | 291 // so the MRP can tell us when terminate() completed. |
292 | |
293 offscreen_presentation_manager_->UnregisterOffscreenPresentationController( | |
294 presentation_id, render_frame_host_id_.second); | |
273 } | 295 } |
274 | 296 |
275 void PresentationFrame::ListenForConnectionStateChange( | 297 void PresentationFrame::ListenForConnectionStateChange( |
276 const content::PresentationSessionInfo& connection, | 298 const content::PresentationSessionInfo& connection, |
277 const content::PresentationConnectionStateChangedCallback& | 299 const content::PresentationConnectionStateChangedCallback& |
278 state_changed_cb) { | 300 state_changed_cb) { |
279 auto it = presentation_id_to_route_id_.find(connection.presentation_id); | 301 auto it = presentation_id_to_route_id_.find(connection.presentation_id); |
280 if (it == presentation_id_to_route_id_.end()) { | 302 if (it == presentation_id_to_route_id_.end()) { |
281 DLOG(ERROR) << __func__ << "route id not found for presentation: " | 303 DLOG(ERROR) << __func__ << "route id not found for presentation: " |
282 << connection.presentation_id; | 304 << connection.presentation_id; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 void SetMediaRouterForTest(MediaRouter* router); | 402 void SetMediaRouterForTest(MediaRouter* router); |
381 | 403 |
382 void OnPresentationSessionStarted( | 404 void OnPresentationSessionStarted( |
383 const RenderFrameHostId& render_frame_host_id, | 405 const RenderFrameHostId& render_frame_host_id, |
384 const content::PresentationSessionInfo& session, | 406 const content::PresentationSessionInfo& session, |
385 const MediaRoute::Id& route_id); | 407 const MediaRoute::Id& route_id); |
386 void OnDefaultPresentationSessionStarted( | 408 void OnDefaultPresentationSessionStarted( |
387 const PresentationRequest& request, | 409 const PresentationRequest& request, |
388 const content::PresentationSessionInfo& session, | 410 const content::PresentationSessionInfo& session, |
389 const MediaRoute::Id& route_id); | 411 const MediaRoute::Id& route_id); |
412 void RegisterOffscreenPresentationClient( | |
413 const RenderFrameHostId& render_frame_host_id, | |
414 content::OffscreenPresentationClient* client); | |
390 | 415 |
391 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, | 416 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, |
392 const std::string& presentation_id) const; | 417 const std::string& presentation_id) const; |
393 const std::vector<MediaRoute::Id> GetRouteIds( | 418 const std::vector<MediaRoute::Id> GetRouteIds( |
394 const RenderFrameHostId& render_frame_host_id) const; | 419 const RenderFrameHostId& render_frame_host_id) const; |
395 | 420 |
396 const PresentationRequest* default_presentation_request() const { | 421 const PresentationRequest* default_presentation_request() const { |
397 return default_presentation_request_.get(); | 422 return default_presentation_request_.get(); |
398 } | 423 } |
399 | 424 |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
644 FOR_EACH_OBSERVER( | 669 FOR_EACH_OBSERVER( |
645 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver, | 670 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver, |
646 default_presentation_request_observers_, | 671 default_presentation_request_observers_, |
647 OnDefaultPresentationChanged(*default_presentation_request_)); | 672 OnDefaultPresentationChanged(*default_presentation_request_)); |
648 } | 673 } |
649 | 674 |
650 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { | 675 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { |
651 router_ = router; | 676 router_ = router; |
652 } | 677 } |
653 | 678 |
679 void PresentationFrameManager::RegisterOffscreenPresentationClient( | |
680 const RenderFrameHostId& render_frame_host_id, | |
681 content::OffscreenPresentationClient* client) { | |
682 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); | |
683 presentation_frame->SetOffscreenPresentationClient(client); | |
684 } | |
685 | |
654 PresentationServiceDelegateImpl* | 686 PresentationServiceDelegateImpl* |
655 PresentationServiceDelegateImpl::GetOrCreateForWebContents( | 687 PresentationServiceDelegateImpl::GetOrCreateForWebContents( |
656 content::WebContents* web_contents) { | 688 content::WebContents* web_contents) { |
657 DCHECK(web_contents); | 689 DCHECK(web_contents); |
658 // CreateForWebContents does nothing if the delegate instance already exists. | 690 // CreateForWebContents does nothing if the delegate instance already exists. |
659 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); | 691 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); |
660 return PresentationServiceDelegateImpl::FromWebContents(web_contents); | 692 return PresentationServiceDelegateImpl::FromWebContents(web_contents); |
661 } | 693 } |
662 | 694 |
663 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl( | 695 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl( |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
959 | 991 |
960 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( | 992 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( |
961 int render_process_id, | 993 int render_process_id, |
962 int render_frame_id, | 994 int render_frame_id, |
963 const MediaSource::Id& source_id) const { | 995 const MediaSource::Id& source_id) const { |
964 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | 996 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); |
965 return frame_manager_->HasScreenAvailabilityListenerForTest( | 997 return frame_manager_->HasScreenAvailabilityListenerForTest( |
966 render_frame_host_id, source_id); | 998 render_frame_host_id, source_id); |
967 } | 999 } |
968 | 1000 |
1001 void PresentationServiceDelegateImpl::RegisterOffscreenPresentationClient( | |
1002 int render_process_id, | |
1003 int render_frame_id, | |
1004 content::OffscreenPresentationClient* client) { | |
1005 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); | |
1006 frame_manager_->RegisterOffscreenPresentationClient(render_frame_host_id, | |
1007 client); | |
1008 } | |
1009 | |
969 } // namespace media_router | 1010 } // namespace media_router |
OLD | NEW |