Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: chrome/browser/media/router/presentation_service_delegate_impl.cc

Issue 2958663002: [MediaRouter] PresentationServiceDelegateImpl cleanup. (Closed)
Patch Set: rebase Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" 5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
6 6
7 #include <string> 7 #include <string>
8 #include <unordered_map> 8 #include <unordered_map>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 url::Origin GetLastCommittedURLForFrame( 63 url::Origin GetLastCommittedURLForFrame(
64 RenderFrameHostId render_frame_host_id) { 64 RenderFrameHostId render_frame_host_id) {
65 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( 65 RenderFrameHost* render_frame_host = RenderFrameHost::FromID(
66 render_frame_host_id.first, render_frame_host_id.second); 66 render_frame_host_id.first, render_frame_host_id.second);
67 DCHECK(render_frame_host); 67 DCHECK(render_frame_host);
68 return render_frame_host->GetLastCommittedOrigin(); 68 return render_frame_host->GetLastCommittedOrigin();
69 } 69 }
70 70
71 } // namespace 71 } // namespace
72 72
73 // Used by PresentationServiceDelegateImpl to manage 73 // PresentationFrame interfaces with MediaRouter to maintain the current state
74 // listeners and default presentation info in a render frame. 74 // of Presentation API within a single render frame, such as the set of
75 // Its lifetime: 75 // PresentationAvailability listeners and PresentationConnections.
76 // * Create an instance with |render_frame_host_id_| if no instance with the 76 // Instances are lazily created when certain Presentation API is invoked on a
77 // same |render_frame_host_id_| exists in: 77 // frame, and are owned by PresentationServiceDelegateImpl.
78 // PresentationFrameManager::OnPresentationConnection 78 // Instances are destroyed when the corresponding frame navigates, or when it
79 // PresentationFrameManager::OnDefaultPresentationStarted 79 // is destroyed.
80 // PresentationFrameManager::SetScreenAvailabilityListener
81 // * Destroy the instance in:
82 // PresentationFrameManager::Reset
83 class PresentationFrame { 80 class PresentationFrame {
84 public: 81 public:
85 PresentationFrame(const RenderFrameHostId& render_frame_host_id, 82 PresentationFrame(const RenderFrameHostId& render_frame_host_id,
86 content::WebContents* web_contents, 83 content::WebContents* web_contents,
87 MediaRouter* router); 84 MediaRouter* router);
88 ~PresentationFrame(); 85 ~PresentationFrame();
89 86
90 // Mirror corresponding APIs in PresentationServiceDelegateImpl. 87 // Mirror corresponding APIs in PresentationServiceDelegateImpl.
91 bool SetScreenAvailabilityListener( 88 bool SetScreenAvailabilityListener(
92 content::PresentationScreenAvailabilityListener* listener); 89 content::PresentationScreenAvailabilityListener* listener);
93 void RemoveScreenAvailabilityListener( 90 void RemoveScreenAvailabilityListener(
94 content::PresentationScreenAvailabilityListener* listener); 91 content::PresentationScreenAvailabilityListener* listener);
95 bool HasScreenAvailabilityListenerForTest( 92 bool HasScreenAvailabilityListenerForTest(
96 const MediaSource::Id& source_id) const; 93 const MediaSource::Id& source_id) const;
97 std::string GetDefaultPresentationId() const;
98 void ListenForConnectionStateChange( 94 void ListenForConnectionStateChange(
99 const content::PresentationInfo& connection, 95 const content::PresentationInfo& connection,
100 const content::PresentationConnectionStateChangedCallback& 96 const content::PresentationConnectionStateChangedCallback&
101 state_changed_cb); 97 state_changed_cb);
102 98
103 void Reset(); 99 void Reset();
104 void RemoveConnection(const std::string& presentation_id,
105 const MediaRoute::Id& route_id);
106 100
107 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; 101 MediaRoute::Id GetRouteId(const std::string& presentation_id) const;
108 102
109 void OnPresentationConnection( 103 void AddPresentation(const content::PresentationInfo& presentation_info,
110 const content::PresentationInfo& presentation_info, 104 const MediaRoute& route);
111 const MediaRoute& route);
112 void OnPresentationServiceDelegateDestroyed() const;
113
114 void ConnectToPresentation( 105 void ConnectToPresentation(
115 const content::PresentationInfo& presentation_info, 106 const content::PresentationInfo& presentation_info,
116 content::PresentationConnectionPtr controller_connection_ptr, 107 content::PresentationConnectionPtr controller_connection_ptr,
117 content::PresentationConnectionRequest receiver_connection_request); 108 content::PresentationConnectionRequest receiver_connection_request);
109 void RemovePresentation(const std::string& presentation_id);
118 110
119 private: 111 private:
120 MediaSource GetMediaSourceFromListener(
121 content::PresentationScreenAvailabilityListener* listener) const;
122 base::small_map<std::map<std::string, MediaRoute>> presentation_id_to_route_; 112 base::small_map<std::map<std::string, MediaRoute>> presentation_id_to_route_;
123 base::small_map< 113 base::small_map<
124 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>> 114 std::map<std::string, std::unique_ptr<PresentationMediaSinksObserver>>>
125 url_to_sinks_observer_; 115 url_to_sinks_observer_;
126 std::unordered_map< 116 std::unordered_map<
127 MediaRoute::Id, 117 MediaRoute::Id,
128 std::unique_ptr<PresentationConnectionStateSubscription>> 118 std::unique_ptr<PresentationConnectionStateSubscription>>
129 connection_state_subscriptions_; 119 connection_state_subscriptions_;
130 std::unordered_map<MediaRoute::Id, 120 std::unordered_map<MediaRoute::Id,
131 std::unique_ptr<BrowserPresentationConnectionProxy>> 121 std::unique_ptr<BrowserPresentationConnectionProxy>>
(...skipping 10 matching lines...) Expand all
142 const RenderFrameHostId& render_frame_host_id, 132 const RenderFrameHostId& render_frame_host_id,
143 content::WebContents* web_contents, 133 content::WebContents* web_contents,
144 MediaRouter* router) 134 MediaRouter* router)
145 : render_frame_host_id_(render_frame_host_id), 135 : render_frame_host_id_(render_frame_host_id),
146 web_contents_(web_contents), 136 web_contents_(web_contents),
147 router_(router) { 137 router_(router) {
148 DCHECK(web_contents_); 138 DCHECK(web_contents_);
149 DCHECK(router_); 139 DCHECK(router_);
150 } 140 }
151 141
152 PresentationFrame::~PresentationFrame() { 142 PresentationFrame::~PresentationFrame() = default;
153 }
154 143
155 void PresentationFrame::OnPresentationConnection( 144 MediaRoute::Id PresentationFrame::GetRouteId(
156 const content::PresentationInfo& presentation_info,
157 const MediaRoute& route) {
158 presentation_id_to_route_.insert(
159 std::make_pair(presentation_info.presentation_id, route));
160 }
161
162 const MediaRoute::Id PresentationFrame::GetRouteId(
163 const std::string& presentation_id) const { 145 const std::string& presentation_id) const {
164 auto it = presentation_id_to_route_.find(presentation_id); 146 auto it = presentation_id_to_route_.find(presentation_id);
165 return it != presentation_id_to_route_.end() ? it->second.media_route_id() 147 return it != presentation_id_to_route_.end() ? it->second.media_route_id()
166 : ""; 148 : "";
167 } 149 }
168 150
169 bool PresentationFrame::SetScreenAvailabilityListener( 151 bool PresentationFrame::SetScreenAvailabilityListener(
170 content::PresentationScreenAvailabilityListener* listener) { 152 content::PresentationScreenAvailabilityListener* listener) {
171 MediaSource source(GetMediaSourceFromListener(listener)); 153 MediaSource source =
154 MediaSourceForPresentationUrl(listener->GetAvailabilityUrl());
172 if (!IsValidPresentationUrl(source.url())) { 155 if (!IsValidPresentationUrl(source.url())) {
173 listener->OnScreenAvailabilityChanged( 156 listener->OnScreenAvailabilityChanged(
174 blink::mojom::ScreenAvailability::SOURCE_NOT_SUPPORTED); 157 blink::mojom::ScreenAvailability::SOURCE_NOT_SUPPORTED);
175 return false; 158 return false;
176 } 159 }
177 160
178 auto& sinks_observer = url_to_sinks_observer_[source.id()]; 161 auto& sinks_observer = url_to_sinks_observer_[source.id()];
179 if (sinks_observer && sinks_observer->listener() == listener) 162 if (sinks_observer && sinks_observer->listener() == listener)
180 return false; 163 return false;
181 164
182 sinks_observer.reset(new PresentationMediaSinksObserver( 165 sinks_observer.reset(new PresentationMediaSinksObserver(
183 router_, listener, source, 166 router_, listener, source,
184 GetLastCommittedURLForFrame(render_frame_host_id_))); 167 GetLastCommittedURLForFrame(render_frame_host_id_)));
185 168
186 if (!sinks_observer->Init()) { 169 if (!sinks_observer->Init()) {
187 url_to_sinks_observer_.erase(source.id()); 170 url_to_sinks_observer_.erase(source.id());
188 listener->OnScreenAvailabilityChanged( 171 listener->OnScreenAvailabilityChanged(
189 blink::mojom::ScreenAvailability::DISABLED); 172 blink::mojom::ScreenAvailability::DISABLED);
190 return false; 173 return false;
191 } 174 }
192 175
193 return true; 176 return true;
194 } 177 }
195 178
196 void PresentationFrame::RemoveScreenAvailabilityListener( 179 void PresentationFrame::RemoveScreenAvailabilityListener(
197 content::PresentationScreenAvailabilityListener* listener) { 180 content::PresentationScreenAvailabilityListener* listener) {
198 MediaSource source(GetMediaSourceFromListener(listener)); 181 MediaSource source =
182 MediaSourceForPresentationUrl(listener->GetAvailabilityUrl());
199 auto sinks_observer_it = url_to_sinks_observer_.find(source.id()); 183 auto sinks_observer_it = url_to_sinks_observer_.find(source.id());
200 if (sinks_observer_it != url_to_sinks_observer_.end() && 184 if (sinks_observer_it != url_to_sinks_observer_.end() &&
201 sinks_observer_it->second->listener() == listener) { 185 sinks_observer_it->second->listener() == listener) {
202 url_to_sinks_observer_.erase(sinks_observer_it); 186 url_to_sinks_observer_.erase(sinks_observer_it);
203 } 187 }
204 } 188 }
205 189
206 bool PresentationFrame::HasScreenAvailabilityListenerForTest( 190 bool PresentationFrame::HasScreenAvailabilityListenerForTest(
207 const MediaSource::Id& source_id) const { 191 const MediaSource::Id& source_id) const {
208 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end(); 192 return url_to_sinks_observer_.find(source_id) != url_to_sinks_observer_.end();
(...skipping 11 matching lines...) Expand all
220 router_->DetachRoute(pid_route.second.media_route_id()); 204 router_->DetachRoute(pid_route.second.media_route_id());
221 } 205 }
222 } 206 }
223 207
224 presentation_id_to_route_.clear(); 208 presentation_id_to_route_.clear();
225 url_to_sinks_observer_.clear(); 209 url_to_sinks_observer_.clear();
226 connection_state_subscriptions_.clear(); 210 connection_state_subscriptions_.clear();
227 browser_connection_proxies_.clear(); 211 browser_connection_proxies_.clear();
228 } 212 }
229 213
230 void PresentationFrame::RemoveConnection(const std::string& presentation_id, 214 void PresentationFrame::AddPresentation(
231 const MediaRoute::Id& route_id) { 215 const content::PresentationInfo& presentation_info,
232 // Remove the presentation id mapping so a later call to Reset is a no-op. 216 const MediaRoute& route) {
233 presentation_id_to_route_.erase(presentation_id); 217 presentation_id_to_route_.insert(
234 218 std::make_pair(presentation_info.presentation_id, route));
235 browser_connection_proxies_.erase(route_id);
236 // We keep the PresentationConnectionStateChangedCallback registered with MR
237 // so the MRP can tell us when terminate() completed.
238 }
239
240 void PresentationFrame::ListenForConnectionStateChange(
241 const content::PresentationInfo& connection,
242 const content::PresentationConnectionStateChangedCallback&
243 state_changed_cb) {
244 auto it = presentation_id_to_route_.find(connection.presentation_id);
245 if (it == presentation_id_to_route_.end()) {
246 DLOG(ERROR) << __func__ << "route id not found for presentation: "
247 << connection.presentation_id;
248 return;
249 }
250
251 const MediaRoute::Id& route_id = it->second.media_route_id();
252 if (connection_state_subscriptions_.find(route_id) !=
253 connection_state_subscriptions_.end()) {
254 DLOG(ERROR) << __func__
255 << "Already listening connection state change for route: "
256 << route_id;
257 return;
258 }
259
260 connection_state_subscriptions_.insert(std::make_pair(
261 route_id, router_->AddPresentationConnectionStateChangedCallback(
262 route_id, state_changed_cb)));
263 }
264
265 MediaSource PresentationFrame::GetMediaSourceFromListener(
266 content::PresentationScreenAvailabilityListener* listener) const {
267 // If the default presentation URL is empty then fall back to tab mirroring.
268 return listener->GetAvailabilityUrl().is_empty()
269 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_))
270 : MediaSourceForPresentationUrl(listener->GetAvailabilityUrl());
271 } 219 }
272 220
273 void PresentationFrame::ConnectToPresentation( 221 void PresentationFrame::ConnectToPresentation(
274 const content::PresentationInfo& presentation_info, 222 const content::PresentationInfo& presentation_info,
275 content::PresentationConnectionPtr controller_connection_ptr, 223 content::PresentationConnectionPtr controller_connection_ptr,
276 content::PresentationConnectionRequest receiver_connection_request) { 224 content::PresentationConnectionRequest receiver_connection_request) {
277 const auto pid_route_it = 225 const auto pid_route_it =
278 presentation_id_to_route_.find(presentation_info.presentation_id); 226 presentation_id_to_route_.find(presentation_info.presentation_id);
279 227
280 if (pid_route_it == presentation_id_to_route_.end()) { 228 if (pid_route_it == presentation_id_to_route_.end()) {
(...skipping 23 matching lines...) Expand all
304 } 252 }
305 253
306 auto* proxy = new BrowserPresentationConnectionProxy( 254 auto* proxy = new BrowserPresentationConnectionProxy(
307 router_, route_id, std::move(receiver_connection_request), 255 router_, route_id, std::move(receiver_connection_request),
308 std::move(controller_connection_ptr)); 256 std::move(controller_connection_ptr));
309 browser_connection_proxies_.insert( 257 browser_connection_proxies_.insert(
310 std::make_pair(route_id, base::WrapUnique(proxy))); 258 std::make_pair(route_id, base::WrapUnique(proxy)));
311 } 259 }
312 } 260 }
313 261
314 // Used by PresentationServiceDelegateImpl to manage PresentationFrames. 262 void PresentationFrame::RemovePresentation(const std::string& presentation_id) {
315 class PresentationFrameManager { 263 // Remove the presentation id mapping so a later call to Reset is a no-op.
316 public: 264 auto it = presentation_id_to_route_.find(presentation_id);
317 PresentationFrameManager(content::WebContents* web_contents, 265 if (it == presentation_id_to_route_.end())
318 MediaRouter* router); 266 return;
319 ~PresentationFrameManager();
320 267
321 // Mirror corresponding APIs in PresentationServiceDelegateImpl. 268 auto route_id = it->second.media_route_id();
322 bool SetScreenAvailabilityListener( 269 presentation_id_to_route_.erase(presentation_id);
323 const RenderFrameHostId& render_frame_host_id, 270 browser_connection_proxies_.erase(route_id);
324 content::PresentationScreenAvailabilityListener* listener); 271 // We keep the PresentationConnectionStateChangedCallback registered with MR
325 void RemoveScreenAvailabilityListener( 272 // so the MRP can tell us when terminate() completed.
326 const RenderFrameHostId& render_frame_host_id,
327 content::PresentationScreenAvailabilityListener* listener);
328 void ListenForConnectionStateChange(
329 const RenderFrameHostId& render_frame_host_id,
330 const content::PresentationInfo& connection,
331 const content::PresentationConnectionStateChangedCallback&
332 state_changed_cb);
333
334 // Sets or clears the default presentation request and callback for the given
335 // frame. Also sets / clears the default presentation requests for the owning
336 // tab WebContents.
337 void SetDefaultPresentationUrls(
338 const RenderFrameHostId& render_frame_host_id,
339 const std::vector<GURL>& default_presentation_urls,
340 content::DefaultPresentationConnectionCallback callback);
341 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id,
342 DelegateObserver* observer);
343 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id);
344 void AddDefaultPresentationRequestObserver(
345 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
346 observer);
347 void RemoveDefaultPresentationRequestObserver(
348 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
349 observer);
350 void Reset(const RenderFrameHostId& render_frame_host_id);
351 void RemoveConnection(const RenderFrameHostId& render_frame_host_id,
352 const MediaRoute::Id& route_id,
353 const std::string& presentation_id);
354 bool HasScreenAvailabilityListenerForTest(
355 const RenderFrameHostId& render_frame_host_id,
356 const MediaSource::Id& source_id) const;
357 void SetMediaRouterForTest(MediaRouter* router);
358
359 void OnPresentationConnection(
360 const RenderFrameHostId& render_frame_host_id,
361 const content::PresentationInfo& presentation_info,
362 const MediaRoute& route);
363 void OnDefaultPresentationStarted(
364 const PresentationRequest& request,
365 const content::PresentationInfo& presentation_info,
366 const MediaRoute& route);
367
368 void ConnectToPresentation(
369 const RenderFrameHostId& render_frame_host_id,
370 const content::PresentationInfo& presentation_info,
371 content::PresentationConnectionPtr controller_connection_ptr,
372 content::PresentationConnectionRequest receiver_connection_request);
373
374 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id,
375 const std::string& presentation_id) const;
376
377 const PresentationRequest* default_presentation_request() const {
378 return default_presentation_request_.get();
379 }
380
381 private:
382 PresentationFrame* GetOrAddPresentationFrame(
383 const RenderFrameHostId& render_frame_host_id);
384
385 // Sets the default presentation request for the owning WebContents and
386 // notifies observers of changes.
387 void SetDefaultPresentationRequest(
388 const PresentationRequest& default_presentation_request);
389
390 // Clears the default presentation request for the owning WebContents and
391 // notifies observers of changes. Also resets
392 // |default_presentation_started_callback_|.
393 void ClearDefaultPresentationRequest();
394
395 bool IsMainFrame(const RenderFrameHostId& render_frame_host_id) const;
396
397 // Maps a frame identifier to a PresentationFrame object for frames
398 // that are using presentation API.
399 std::unordered_map<RenderFrameHostId, std::unique_ptr<PresentationFrame>,
400 RenderFrameHostIdHasher>
401 presentation_frames_;
402
403 // Default presentation request for the owning tab WebContents.
404 std::unique_ptr<PresentationRequest> default_presentation_request_;
405
406 // Callback to invoke when default presentation has started.
407 content::DefaultPresentationConnectionCallback
408 default_presentation_started_callback_;
409
410 // References to the observers listening for changes to this tab WebContent's
411 // default presentation.
412 base::ObserverList<
413 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver>
414 default_presentation_request_observers_;
415
416 // References to the owning WebContents, and the corresponding MediaRouter.
417 MediaRouter* router_;
418 content::WebContents* web_contents_;
419 };
420
421 PresentationFrameManager::PresentationFrameManager(
422 content::WebContents* web_contents,
423 MediaRouter* router)
424 : router_(router), web_contents_(web_contents) {
425 DCHECK(web_contents_);
426 DCHECK(router_);
427 } 273 }
428 274
429 PresentationFrameManager::~PresentationFrameManager() {} 275 void PresentationFrame::ListenForConnectionStateChange(
430
431 void PresentationFrameManager::OnPresentationConnection(
432 const RenderFrameHostId& render_frame_host_id,
433 const content::PresentationInfo& presentation_info,
434 const MediaRoute& route) {
435 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
436 presentation_frame->OnPresentationConnection(presentation_info, route);
437 }
438
439 void PresentationFrameManager::OnDefaultPresentationStarted(
440 const PresentationRequest& request,
441 const content::PresentationInfo& presentation_info,
442 const MediaRoute& route) {
443 OnPresentationConnection(request.render_frame_host_id(), presentation_info,
444 route);
445
446 if (default_presentation_request_ &&
447 default_presentation_request_->Equals(request)) {
448 default_presentation_started_callback_.Run(presentation_info);
449 }
450 }
451
452 void PresentationFrameManager::ConnectToPresentation(
453 const RenderFrameHostId& render_frame_host_id,
454 const content::PresentationInfo& presentation_info,
455 content::PresentationConnectionPtr controller_connection_ptr,
456 content::PresentationConnectionRequest receiver_connection_request) {
457 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
458 presentation_frame->ConnectToPresentation(
459 presentation_info, std::move(controller_connection_ptr),
460 std::move(receiver_connection_request));
461 }
462
463 const MediaRoute::Id PresentationFrameManager::GetRouteId(
464 const RenderFrameHostId& render_frame_host_id,
465 const std::string& presentation_id) const {
466 const auto it = presentation_frames_.find(render_frame_host_id);
467 return it != presentation_frames_.end()
468 ? it->second->GetRouteId(presentation_id)
469 : MediaRoute::Id();
470 }
471
472 bool PresentationFrameManager::SetScreenAvailabilityListener(
473 const RenderFrameHostId& render_frame_host_id,
474 content::PresentationScreenAvailabilityListener* listener) {
475 DCHECK(listener);
476 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
477 return presentation_frame->SetScreenAvailabilityListener(listener);
478 }
479
480 void PresentationFrameManager::RemoveScreenAvailabilityListener(
481 const RenderFrameHostId& render_frame_host_id,
482 content::PresentationScreenAvailabilityListener* listener) {
483 DCHECK(listener);
484 const auto it = presentation_frames_.find(render_frame_host_id);
485 if (it != presentation_frames_.end())
486 it->second->RemoveScreenAvailabilityListener(listener);
487 }
488
489 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest(
490 const RenderFrameHostId& render_frame_host_id,
491 const MediaSource::Id& source_id) const {
492 const auto it = presentation_frames_.find(render_frame_host_id);
493 return it != presentation_frames_.end() &&
494 it->second->HasScreenAvailabilityListenerForTest(source_id);
495 }
496
497 void PresentationFrameManager::ListenForConnectionStateChange(
498 const RenderFrameHostId& render_frame_host_id,
499 const content::PresentationInfo& connection, 276 const content::PresentationInfo& connection,
500 const content::PresentationConnectionStateChangedCallback& 277 const content::PresentationConnectionStateChangedCallback&
501 state_changed_cb) { 278 state_changed_cb) {
502 const auto it = presentation_frames_.find(render_frame_host_id); 279 auto it = presentation_id_to_route_.find(connection.presentation_id);
503 if (it != presentation_frames_.end()) 280 if (it == presentation_id_to_route_.end()) {
504 it->second->ListenForConnectionStateChange(connection, state_changed_cb); 281 DLOG(ERROR) << __func__ << "route id not found for presentation: "
505 } 282 << connection.presentation_id;
506
507 void PresentationFrameManager::SetDefaultPresentationUrls(
508 const RenderFrameHostId& render_frame_host_id,
509 const std::vector<GURL>& default_presentation_urls,
510 content::DefaultPresentationConnectionCallback callback) {
511 if (!IsMainFrame(render_frame_host_id))
512 return; 283 return;
513
514 if (default_presentation_urls.empty()) {
515 ClearDefaultPresentationRequest();
516 } else {
517 DCHECK(!callback.is_null());
518 const auto& frame_origin =
519 GetLastCommittedURLForFrame(render_frame_host_id);
520 PresentationRequest request(render_frame_host_id, default_presentation_urls,
521 frame_origin);
522 default_presentation_started_callback_ = callback;
523 SetDefaultPresentationRequest(request);
524 }
525 }
526
527 void PresentationFrameManager::AddDefaultPresentationRequestObserver(
528 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
529 observer) {
530 default_presentation_request_observers_.AddObserver(observer);
531 }
532
533 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver(
534 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
535 observer) {
536 default_presentation_request_observers_.RemoveObserver(observer);
537 }
538
539 void PresentationFrameManager::Reset(
540 const RenderFrameHostId& render_frame_host_id) {
541 const auto it = presentation_frames_.find(render_frame_host_id);
542 if (it != presentation_frames_.end()) {
543 it->second->Reset();
544 presentation_frames_.erase(it);
545 } 284 }
546 285
547 if (default_presentation_request_ && 286 const MediaRoute::Id& route_id = it->second.media_route_id();
548 render_frame_host_id == 287 if (connection_state_subscriptions_.find(route_id) !=
549 default_presentation_request_->render_frame_host_id()) { 288 connection_state_subscriptions_.end()) {
550 ClearDefaultPresentationRequest(); 289 DLOG(ERROR) << __func__
290 << "Already listening connection state change for route: "
291 << route_id;
292 return;
551 } 293 }
552 }
553 294
554 void PresentationFrameManager::RemoveConnection( 295 connection_state_subscriptions_.insert(std::make_pair(
555 const RenderFrameHostId& render_frame_host_id, 296 route_id, router_->AddPresentationConnectionStateChangedCallback(
556 const MediaRoute::Id& route_id, 297 route_id, state_changed_cb)));
557 const std::string& presentation_id) {
558 const auto it = presentation_frames_.find(render_frame_host_id);
559 if (it != presentation_frames_.end())
560 it->second->RemoveConnection(route_id, presentation_id);
561 }
562
563 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame(
564 const RenderFrameHostId& render_frame_host_id) {
565 std::unique_ptr<PresentationFrame>& presentation_frame =
566 presentation_frames_[render_frame_host_id];
567 if (!presentation_frame) {
568 presentation_frame.reset(new PresentationFrame(
569 render_frame_host_id, web_contents_, router_));
570 }
571 return presentation_frame.get();
572 }
573
574 void PresentationFrameManager::ClearDefaultPresentationRequest() {
575 default_presentation_started_callback_.Reset();
576 if (!default_presentation_request_)
577 return;
578
579 default_presentation_request_.reset();
580 for (auto& observer : default_presentation_request_observers_)
581 observer.OnDefaultPresentationRemoved();
582 }
583
584 bool PresentationFrameManager::IsMainFrame(
585 const RenderFrameHostId& render_frame_host_id) const {
586 RenderFrameHost* main_frame = web_contents_->GetMainFrame();
587 return main_frame && GetRenderFrameHostId(main_frame) == render_frame_host_id;
588 }
589
590 void PresentationFrameManager::SetDefaultPresentationRequest(
591 const PresentationRequest& default_presentation_request) {
592 if (default_presentation_request_ &&
593 default_presentation_request_->Equals(default_presentation_request))
594 return;
595
596 default_presentation_request_.reset(
597 new PresentationRequest(default_presentation_request));
598 for (auto& observer : default_presentation_request_observers_)
599 observer.OnDefaultPresentationChanged(*default_presentation_request_);
600 }
601
602 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) {
603 router_ = router;
604 } 298 }
605 299
606 PresentationServiceDelegateImpl* 300 PresentationServiceDelegateImpl*
607 PresentationServiceDelegateImpl::GetOrCreateForWebContents( 301 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
608 content::WebContents* web_contents) { 302 content::WebContents* web_contents) {
609 DCHECK(web_contents); 303 DCHECK(web_contents);
610 // CreateForWebContents does nothing if the delegate instance already exists. 304 // CreateForWebContents does nothing if the delegate instance already exists.
611 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); 305 PresentationServiceDelegateImpl::CreateForWebContents(web_contents);
612 return PresentationServiceDelegateImpl::FromWebContents(web_contents); 306 return PresentationServiceDelegateImpl::FromWebContents(web_contents);
613 } 307 }
614 308
615 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl( 309 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl(
616 content::WebContents* web_contents) 310 content::WebContents* web_contents)
617 : web_contents_(web_contents), 311 : web_contents_(web_contents),
618 router_(MediaRouterFactory::GetApiForBrowserContext( 312 router_(MediaRouterFactory::GetApiForBrowserContext(
619 web_contents_->GetBrowserContext())), 313 web_contents_->GetBrowserContext())),
620 frame_manager_(new PresentationFrameManager(web_contents, router_)),
621 weak_factory_(this) { 314 weak_factory_(this) {
622 DCHECK(web_contents_); 315 DCHECK(web_contents_);
623 DCHECK(router_); 316 DCHECK(router_);
624 } 317 }
625 318
626 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() { 319 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() = default;
627 }
628 320
629 void PresentationServiceDelegateImpl::AddObserver(int render_process_id, 321 void PresentationServiceDelegateImpl::AddObserver(int render_process_id,
630 int render_frame_id, 322 int render_frame_id,
631 DelegateObserver* observer) { 323 DelegateObserver* observer) {
632 DCHECK(observer); 324 DCHECK(observer);
633 observers_.AddObserver(render_process_id, render_frame_id, observer); 325 observers_.AddObserver(render_process_id, render_frame_id, observer);
634 } 326 }
635 327
636 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id, 328 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id,
637 int render_frame_id) { 329 int render_frame_id) {
638 observers_.RemoveObserver(render_process_id, render_frame_id); 330 observers_.RemoveObserver(render_process_id, render_frame_id);
639 } 331 }
640 332
641 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener( 333 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener(
642 int render_process_id, 334 int render_process_id,
643 int render_frame_id, 335 int render_frame_id,
644 content::PresentationScreenAvailabilityListener* listener) { 336 content::PresentationScreenAvailabilityListener* listener) {
645 DCHECK(listener); 337 DCHECK(listener);
646 return frame_manager_->SetScreenAvailabilityListener( 338 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
647 RenderFrameHostId(render_process_id, render_frame_id), listener); 339 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
340 return presentation_frame->SetScreenAvailabilityListener(listener);
648 } 341 }
649 342
650 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener( 343 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener(
651 int render_process_id, 344 int render_process_id,
652 int render_frame_id, 345 int render_frame_id,
653 content::PresentationScreenAvailabilityListener* listener) { 346 content::PresentationScreenAvailabilityListener* listener) {
654 DCHECK(listener); 347 DCHECK(listener);
655 frame_manager_->RemoveScreenAvailabilityListener( 348 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
656 RenderFrameHostId(render_process_id, render_frame_id), listener); 349 const auto it = presentation_frames_.find(render_frame_host_id);
350 if (it != presentation_frames_.end())
351 it->second->RemoveScreenAvailabilityListener(listener);
657 } 352 }
658 353
659 void PresentationServiceDelegateImpl::Reset(int render_process_id, 354 void PresentationServiceDelegateImpl::Reset(int render_process_id,
660 int render_frame_id) { 355 int render_frame_id) {
661 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 356 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
662 frame_manager_->Reset(render_frame_host_id); 357 const auto it = presentation_frames_.find(render_frame_host_id);
358 if (it != presentation_frames_.end()) {
359 it->second->Reset();
360 presentation_frames_.erase(it);
361 }
362
363 if (default_presentation_request_ &&
364 render_frame_host_id ==
365 default_presentation_request_->render_frame_host_id()) {
366 ClearDefaultPresentationRequest();
367 }
368 }
369
370 PresentationFrame* PresentationServiceDelegateImpl::GetOrAddPresentationFrame(
371 const RenderFrameHostId& render_frame_host_id) {
372 auto& presentation_frame = presentation_frames_[render_frame_host_id];
373 if (!presentation_frame) {
374 presentation_frame.reset(
375 new PresentationFrame(render_frame_host_id, web_contents_, router_));
376 }
377 return presentation_frame.get();
663 } 378 }
664 379
665 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls( 380 void PresentationServiceDelegateImpl::SetDefaultPresentationUrls(
666 int render_process_id, 381 int render_process_id,
667 int render_frame_id, 382 int render_frame_id,
668 const std::vector<GURL>& default_presentation_urls, 383 const std::vector<GURL>& default_presentation_urls,
669 content::DefaultPresentationConnectionCallback callback) { 384 content::DefaultPresentationConnectionCallback callback) {
670 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 385 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
671 frame_manager_->SetDefaultPresentationUrls( 386 if (!IsMainFrame(render_frame_host_id))
672 render_frame_host_id, default_presentation_urls, std::move(callback)); 387 return;
388
389 if (default_presentation_urls.empty()) {
390 ClearDefaultPresentationRequest();
391 return;
392 }
393
394 DCHECK(!callback.is_null());
395 auto frame_origin = GetLastCommittedURLForFrame(render_frame_host_id);
396 PresentationRequest request(render_frame_host_id, default_presentation_urls,
397 frame_origin);
398 default_presentation_started_callback_ = std::move(callback);
399 SetDefaultPresentationRequest(request);
673 } 400 }
674 401
675 void PresentationServiceDelegateImpl::OnJoinRouteResponse( 402 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
676 int render_process_id, 403 int render_process_id,
677 int render_frame_id, 404 int render_frame_id,
678 const GURL& presentation_url, 405 const GURL& presentation_url,
679 const std::string& presentation_id, 406 const std::string& presentation_id,
680 content::PresentationConnectionCallback success_cb, 407 content::PresentationConnectionCallback success_cb,
681 content::PresentationConnectionErrorCallback error_cb, 408 content::PresentationConnectionErrorCallback error_cb,
682 const RouteRequestResult& result) { 409 const RouteRequestResult& result) {
683 if (!result.route()) { 410 if (!result.route()) {
684 std::move(error_cb).Run(content::PresentationError( 411 std::move(error_cb).Run(content::PresentationError(
685 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error())); 412 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, result.error()));
686 } else { 413 } else {
687 DVLOG(1) << "OnJoinRouteResponse: " 414 DVLOG(1) << "OnJoinRouteResponse: "
688 << "route_id: " << result.route()->media_route_id() 415 << "route_id: " << result.route()->media_route_id()
689 << ", presentation URL: " << presentation_url 416 << ", presentation URL: " << presentation_url
690 << ", presentation ID: " << presentation_id; 417 << ", presentation ID: " << presentation_id;
691 DCHECK_EQ(presentation_id, result.presentation_id()); 418 DCHECK_EQ(presentation_id, result.presentation_id());
692 content::PresentationInfo presentation_info(presentation_url, 419 content::PresentationInfo presentation_info(presentation_url,
693 result.presentation_id()); 420 result.presentation_id());
694 frame_manager_->OnPresentationConnection( 421 AddPresentation(RenderFrameHostId(render_process_id, render_frame_id),
695 RenderFrameHostId(render_process_id, render_frame_id), 422 presentation_info, *result.route());
696 presentation_info, *result.route());
697 std::move(success_cb).Run(presentation_info); 423 std::move(success_cb).Run(presentation_info);
698 } 424 }
699 } 425 }
700 426
701 void PresentationServiceDelegateImpl::OnStartPresentationSucceeded( 427 void PresentationServiceDelegateImpl::OnStartPresentationSucceeded(
702 int render_process_id, 428 int render_process_id,
703 int render_frame_id, 429 int render_frame_id,
704 content::PresentationConnectionCallback success_cb, 430 content::PresentationConnectionCallback success_cb,
705 const content::PresentationInfo& new_presentation_info, 431 const content::PresentationInfo& new_presentation_info,
706 const MediaRoute& route) { 432 const MediaRoute& route) {
707 DVLOG(1) << "OnStartPresentationSucceeded: " 433 DVLOG(1) << "OnStartPresentationSucceeded: "
708 << "route_id: " << route.media_route_id() 434 << "route_id: " << route.media_route_id()
709 << ", presentation URL: " << new_presentation_info.presentation_url 435 << ", presentation URL: " << new_presentation_info.presentation_url
710 << ", presentation ID: " << new_presentation_info.presentation_id; 436 << ", presentation ID: " << new_presentation_info.presentation_id;
711 frame_manager_->OnPresentationConnection( 437 AddPresentation(RenderFrameHostId(render_process_id, render_frame_id),
712 RenderFrameHostId(render_process_id, render_frame_id), 438 new_presentation_info, route);
713 new_presentation_info, route);
714 std::move(success_cb).Run(new_presentation_info); 439 std::move(success_cb).Run(new_presentation_info);
715 } 440 }
716 441
442 void PresentationServiceDelegateImpl::AddPresentation(
443 const RenderFrameHostId& render_frame_host_id,
444 const content::PresentationInfo& presentation_info,
445 const MediaRoute& route) {
446 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
447 presentation_frame->AddPresentation(presentation_info, route);
448 }
449
450 void PresentationServiceDelegateImpl::RemovePresentation(
451 const RenderFrameHostId& render_frame_host_id,
452 const std::string& presentation_id) {
453 const auto it = presentation_frames_.find(render_frame_host_id);
454 if (it != presentation_frames_.end())
455 it->second->RemovePresentation(presentation_id);
456 }
457
717 void PresentationServiceDelegateImpl::StartPresentation( 458 void PresentationServiceDelegateImpl::StartPresentation(
718 int render_process_id, 459 int render_process_id,
719 int render_frame_id, 460 int render_frame_id,
720 const std::vector<GURL>& presentation_urls, 461 const std::vector<GURL>& presentation_urls,
721 content::PresentationConnectionCallback success_cb, 462 content::PresentationConnectionCallback success_cb,
722 content::PresentationConnectionErrorCallback error_cb) { 463 content::PresentationConnectionErrorCallback error_cb) {
723 if (presentation_urls.empty()) { 464 if (presentation_urls.empty()) {
724 std::move(error_cb).Run( 465 std::move(error_cb).Run(
725 content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 466 content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
726 "Invalid presentation arguments.")); 467 "Invalid presentation arguments."));
(...skipping 11 matching lines...) Expand all
738 return; 479 return;
739 } 480 }
740 481
741 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 482 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
742 std::unique_ptr<CreatePresentationConnectionRequest> request( 483 std::unique_ptr<CreatePresentationConnectionRequest> request(
743 new CreatePresentationConnectionRequest( 484 new CreatePresentationConnectionRequest(
744 render_frame_host_id, presentation_urls, 485 render_frame_host_id, presentation_urls,
745 GetLastCommittedURLForFrame(render_frame_host_id), 486 GetLastCommittedURLForFrame(render_frame_host_id),
746 base::BindOnce( 487 base::BindOnce(
747 &PresentationServiceDelegateImpl::OnStartPresentationSucceeded, 488 &PresentationServiceDelegateImpl::OnStartPresentationSucceeded,
748 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 489 GetWeakPtr(), render_process_id, render_frame_id,
749 std::move(success_cb)), 490 std::move(success_cb)),
750 std::move(error_cb))); 491 std::move(error_cb)));
751 MediaRouterDialogController* controller = 492 MediaRouterDialogController* controller =
752 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); 493 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_);
753 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) { 494 if (!controller->ShowMediaRouterDialogForPresentation(std::move(request))) {
754 LOG(ERROR) 495 LOG(ERROR)
755 << "Media router dialog already exists. Ignoring StartPresentation."; 496 << "Media router dialog already exists. Ignoring StartPresentation.";
756 std::move(error_cb).Run(content::PresentationError( 497 std::move(error_cb).Run(content::PresentationError(
757 content::PRESENTATION_ERROR_UNKNOWN, "Unable to create dialog.")); 498 content::PRESENTATION_ERROR_UNKNOWN, "Unable to create dialog."));
758 return; 499 return;
759 } 500 }
760 } 501 }
761 502
762 void PresentationServiceDelegateImpl::ReconnectPresentation( 503 void PresentationServiceDelegateImpl::ReconnectPresentation(
763 int render_process_id, 504 int render_process_id,
764 int render_frame_id, 505 int render_frame_id,
765 const std::vector<GURL>& presentation_urls, 506 const std::vector<GURL>& presentation_urls,
766 const std::string& presentation_id, 507 const std::string& presentation_id,
767 content::PresentationConnectionCallback success_cb, 508 content::PresentationConnectionCallback success_cb,
768 content::PresentationConnectionErrorCallback error_cb) { 509 content::PresentationConnectionErrorCallback error_cb) {
769 DVLOG(2) << "PresentationServiceDelegateImpl::ReconnectPresentation"; 510 DVLOG(2) << "PresentationServiceDelegateImpl::ReconnectPresentation";
770 if (presentation_urls.empty()) { 511 if (presentation_urls.empty()) {
771 std::move(error_cb).Run(content::PresentationError( 512 std::move(error_cb).Run(content::PresentationError(
772 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, 513 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND,
773 "Invalid presentation arguments.")); 514 "Invalid presentation arguments."));
774 return; 515 return;
775 } 516 }
776 517
777 const url::Origin& origin = GetLastCommittedURLForFrame( 518 auto origin = GetLastCommittedURLForFrame(
778 RenderFrameHostId(render_process_id, render_frame_id)); 519 RenderFrameHostId(render_process_id, render_frame_id));
779 520
780 #if !defined(OS_ANDROID) 521 #if !defined(OS_ANDROID)
781 if (IsAutoJoinPresentationId(presentation_id) && 522 if (IsAutoJoinPresentationId(presentation_id) &&
782 ShouldCancelAutoJoinForOrigin(origin)) { 523 ShouldCancelAutoJoinForOrigin(origin)) {
783 std::move(error_cb).Run(content::PresentationError( 524 std::move(error_cb).Run(content::PresentationError(
784 content::PRESENTATION_ERROR_PRESENTATION_REQUEST_CANCELLED, 525 content::PRESENTATION_ERROR_PRESENTATION_REQUEST_CANCELLED,
785 "Auto-join request cancelled by user preferences.")); 526 "Auto-join request cancelled by user preferences."));
786 return; 527 return;
787 } 528 }
(...skipping 21 matching lines...) Expand all
809 550
810 auto result = RouteRequestResult::FromSuccess(*route, presentation_id); 551 auto result = RouteRequestResult::FromSuccess(*route, presentation_id);
811 OnJoinRouteResponse(render_process_id, render_frame_id, 552 OnJoinRouteResponse(render_process_id, render_frame_id,
812 presentation_urls[0], presentation_id, 553 presentation_urls[0], presentation_id,
813 std::move(success_cb), std::move(error_cb), *result); 554 std::move(success_cb), std::move(error_cb), *result);
814 } else { 555 } else {
815 // TODO(crbug.com/627655): Handle multiple URLs. 556 // TODO(crbug.com/627655): Handle multiple URLs.
816 const GURL& presentation_url = presentation_urls[0]; 557 const GURL& presentation_url = presentation_urls[0];
817 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord(); 558 bool incognito = web_contents_->GetBrowserContext()->IsOffTheRecord();
818 std::vector<MediaRouteResponseCallback> route_response_callbacks; 559 std::vector<MediaRouteResponseCallback> route_response_callbacks;
819 route_response_callbacks.push_back( 560 route_response_callbacks.push_back(base::BindOnce(
820 base::BindOnce(&PresentationServiceDelegateImpl::OnJoinRouteResponse, 561 &PresentationServiceDelegateImpl::OnJoinRouteResponse, GetWeakPtr(),
821 weak_factory_.GetWeakPtr(), render_process_id, 562 render_process_id, render_frame_id, presentation_url, presentation_id,
822 render_frame_id, presentation_url, presentation_id, 563 std::move(success_cb), std::move(error_cb)));
823 std::move(success_cb), std::move(error_cb)));
824 router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(), 564 router_->JoinRoute(MediaSourceForPresentationUrl(presentation_url).id(),
825 presentation_id, origin, web_contents_, 565 presentation_id, origin, web_contents_,
826 std::move(route_response_callbacks), base::TimeDelta(), 566 std::move(route_response_callbacks), base::TimeDelta(),
827 incognito); 567 incognito);
828 } 568 }
829 } 569 }
830 570
831 void PresentationServiceDelegateImpl::CloseConnection( 571 void PresentationServiceDelegateImpl::CloseConnection(
832 int render_process_id, 572 int render_process_id,
833 int render_frame_id, 573 int render_frame_id,
834 const std::string& presentation_id) { 574 const std::string& presentation_id) {
835 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); 575 const RenderFrameHostId rfh_id(render_process_id, render_frame_id);
836 const MediaRoute::Id& route_id = 576 auto route_id = GetRouteId(rfh_id, presentation_id);
837 frame_manager_->GetRouteId(rfh_id, presentation_id);
838 if (route_id.empty()) { 577 if (route_id.empty()) {
839 DVLOG(1) << "No active route for: " << presentation_id; 578 DVLOG(1) << "No active route for: " << presentation_id;
840 return; 579 return;
841 } 580 }
842 581
843 auto* offscreen_presentation_manager = 582 auto* offscreen_presentation_manager =
844 OffscreenPresentationManagerFactory::GetOrCreateForWebContents( 583 OffscreenPresentationManagerFactory::GetOrCreateForWebContents(
845 web_contents_); 584 web_contents_);
846 585
847 if (offscreen_presentation_manager->IsOffscreenPresentation( 586 if (offscreen_presentation_manager->IsOffscreenPresentation(
848 presentation_id)) { 587 presentation_id)) {
849 offscreen_presentation_manager->UnregisterOffscreenPresentationController( 588 offscreen_presentation_manager->UnregisterOffscreenPresentationController(
850 presentation_id, rfh_id); 589 presentation_id, rfh_id);
851 } else { 590 } else {
852 router_->DetachRoute(route_id); 591 router_->DetachRoute(route_id);
853 } 592 }
854 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); 593 RemovePresentation(rfh_id, presentation_id);
855 // TODO(mfoltz): close() should always succeed so there is no need to keep the 594 // TODO(mfoltz): close() should always succeed so there is no need to keep the
856 // state_changed_cb around - remove it and fire the ChangeEvent on the 595 // state_changed_cb around - remove it and fire the ChangeEvent on the
857 // PresentationConnection in Blink. 596 // PresentationConnection in Blink.
858 } 597 }
859 598
860 void PresentationServiceDelegateImpl::Terminate( 599 void PresentationServiceDelegateImpl::Terminate(
861 int render_process_id, 600 int render_process_id,
862 int render_frame_id, 601 int render_frame_id,
863 const std::string& presentation_id) { 602 const std::string& presentation_id) {
864 const RenderFrameHostId rfh_id(render_process_id, render_frame_id); 603 const RenderFrameHostId rfh_id(render_process_id, render_frame_id);
865 const MediaRoute::Id& route_id = 604 auto route_id = GetRouteId(rfh_id, presentation_id);
866 frame_manager_->GetRouteId(rfh_id, presentation_id);
867 if (route_id.empty()) { 605 if (route_id.empty()) {
868 DVLOG(1) << "No active route for: " << presentation_id; 606 DVLOG(1) << "No active route for: " << presentation_id;
869 return; 607 return;
870 } 608 }
871 router_->TerminateRoute(route_id); 609 router_->TerminateRoute(route_id);
872 frame_manager_->RemoveConnection(rfh_id, presentation_id, route_id); 610 RemovePresentation(rfh_id, presentation_id);
873 } 611 }
874 612
875 void PresentationServiceDelegateImpl::SendMessage( 613 void PresentationServiceDelegateImpl::SendMessage(
876 int render_process_id, 614 int render_process_id,
877 int render_frame_id, 615 int render_frame_id,
878 const content::PresentationInfo& presentation_info, 616 const content::PresentationInfo& presentation_info,
879 content::PresentationConnectionMessage message, 617 content::PresentationConnectionMessage message,
880 SendMessageCallback send_message_cb) { 618 SendMessageCallback send_message_cb) {
881 const MediaRoute::Id& route_id = frame_manager_->GetRouteId( 619 auto route_id =
882 RenderFrameHostId(render_process_id, render_frame_id), 620 GetRouteId(RenderFrameHostId(render_process_id, render_frame_id),
883 presentation_info.presentation_id); 621 presentation_info.presentation_id);
884 if (route_id.empty()) { 622 if (route_id.empty()) {
885 DVLOG(1) << "No active route for " << presentation_info.presentation_id; 623 DVLOG(1) << "No active route for " << presentation_info.presentation_id;
886 std::move(send_message_cb).Run(false); 624 std::move(send_message_cb).Run(false);
887 return; 625 return;
888 } 626 }
889 627
890 if (message.is_binary()) { 628 if (message.is_binary()) {
891 router_->SendRouteBinaryMessage( 629 router_->SendRouteBinaryMessage(
892 route_id, 630 route_id,
893 base::MakeUnique<std::vector<uint8_t>>(std::move(message.data.value())), 631 base::MakeUnique<std::vector<uint8_t>>(std::move(message.data.value())),
894 std::move(send_message_cb)); 632 std::move(send_message_cb));
895 } else { 633 } else {
896 router_->SendRouteMessage(route_id, message.message.value(), 634 router_->SendRouteMessage(route_id, message.message.value(),
897 std::move(send_message_cb)); 635 std::move(send_message_cb));
898 } 636 }
899 } 637 }
900 638
901 void PresentationServiceDelegateImpl::ListenForConnectionStateChange( 639 void PresentationServiceDelegateImpl::ListenForConnectionStateChange(
902 int render_process_id, 640 int render_process_id,
903 int render_frame_id, 641 int render_frame_id,
904 const content::PresentationInfo& connection, 642 const content::PresentationInfo& connection,
905 const content::PresentationConnectionStateChangedCallback& 643 const content::PresentationConnectionStateChangedCallback&
906 state_changed_cb) { 644 state_changed_cb) {
907 frame_manager_->ListenForConnectionStateChange( 645 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
908 RenderFrameHostId(render_process_id, render_frame_id), connection, 646 const auto it = presentation_frames_.find(render_frame_host_id);
909 state_changed_cb); 647 if (it != presentation_frames_.end())
648 it->second->ListenForConnectionStateChange(connection, state_changed_cb);
910 } 649 }
911 650
912 void PresentationServiceDelegateImpl::ConnectToPresentation( 651 void PresentationServiceDelegateImpl::ConnectToPresentation(
913 int render_process_id, 652 int render_process_id,
914 int render_frame_id, 653 int render_frame_id,
915 const content::PresentationInfo& presentation_info, 654 const content::PresentationInfo& presentation_info,
916 content::PresentationConnectionPtr controller_connection_ptr, 655 content::PresentationConnectionPtr controller_connection_ptr,
917 content::PresentationConnectionRequest receiver_connection_request) { 656 content::PresentationConnectionRequest receiver_connection_request) {
918 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 657 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
919 frame_manager_->ConnectToPresentation(render_frame_host_id, presentation_info, 658 auto* presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
920 std::move(controller_connection_ptr), 659 presentation_frame->ConnectToPresentation(
921 std::move(receiver_connection_request)); 660 presentation_info, std::move(controller_connection_ptr),
661 std::move(receiver_connection_request));
922 } 662 }
923 663
924 void PresentationServiceDelegateImpl::OnRouteResponse( 664 void PresentationServiceDelegateImpl::OnRouteResponse(
925 const PresentationRequest& presentation_request, 665 const PresentationRequest& presentation_request,
926 const RouteRequestResult& result) { 666 const RouteRequestResult& result) {
927 if (!result.route() || 667 if (!result.route() ||
928 !base::ContainsValue(presentation_request.presentation_urls(), 668 !base::ContainsValue(presentation_request.presentation_urls(),
929 result.presentation_url())) { 669 result.presentation_url())) {
930 return; 670 return;
931 } 671 }
932 672
933 content::PresentationInfo presentation_info(result.presentation_url(), 673 content::PresentationInfo presentation_info(result.presentation_url(),
934 result.presentation_id()); 674 result.presentation_id());
935 frame_manager_->OnDefaultPresentationStarted( 675 AddPresentation(presentation_request.render_frame_host_id(),
936 presentation_request, presentation_info, *result.route()); 676 presentation_info, *result.route());
677 if (default_presentation_request_ &&
678 default_presentation_request_->Equals(presentation_request)) {
679 default_presentation_started_callback_.Run(presentation_info);
680 }
937 } 681 }
938 682
939 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver( 683 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver(
940 DefaultPresentationRequestObserver* observer) { 684 DefaultPresentationRequestObserver* observer) {
941 frame_manager_->AddDefaultPresentationRequestObserver(observer); 685 default_presentation_request_observers_.AddObserver(observer);
942 } 686 }
943 687
944 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver( 688 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver(
945 DefaultPresentationRequestObserver* observer) { 689 DefaultPresentationRequestObserver* observer) {
946 frame_manager_->RemoveDefaultPresentationRequestObserver(observer); 690 default_presentation_request_observers_.RemoveObserver(observer);
947 } 691 }
948 692
949 PresentationRequest 693 PresentationRequest
950 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const { 694 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const {
951 DCHECK(HasDefaultPresentationRequest()); 695 DCHECK(HasDefaultPresentationRequest());
952 return *frame_manager_->default_presentation_request(); 696 return *default_presentation_request_;
953 } 697 }
954 698
955 bool PresentationServiceDelegateImpl::HasDefaultPresentationRequest() const { 699 bool PresentationServiceDelegateImpl::HasDefaultPresentationRequest() const {
956 return frame_manager_->default_presentation_request() != nullptr; 700 return default_presentation_request_ != nullptr;
957 } 701 }
958 702
959 base::WeakPtr<PresentationServiceDelegateImpl> 703 base::WeakPtr<PresentationServiceDelegateImpl>
960 PresentationServiceDelegateImpl::GetWeakPtr() { 704 PresentationServiceDelegateImpl::GetWeakPtr() {
961 return weak_factory_.GetWeakPtr(); 705 return weak_factory_.GetWeakPtr();
962 } 706 }
963 707
964 void PresentationServiceDelegateImpl::SetMediaRouterForTest( 708 void PresentationServiceDelegateImpl::SetMediaRouterForTest(
965 MediaRouter* router) { 709 MediaRouter* router) {
966 router_ = router; 710 router_ = router;
967 frame_manager_->SetMediaRouterForTest(router);
968 } 711 }
969 712
970 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 713 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
971 int render_process_id, 714 int render_process_id,
972 int render_frame_id, 715 int render_frame_id,
973 const MediaSource::Id& source_id) const { 716 const MediaSource::Id& source_id) const {
974 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 717 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
975 return frame_manager_->HasScreenAvailabilityListenerForTest( 718 const auto it = presentation_frames_.find(render_frame_host_id);
976 render_frame_host_id, source_id); 719 return it != presentation_frames_.end() &&
720 it->second->HasScreenAvailabilityListenerForTest(source_id);
721 }
722
723 void PresentationServiceDelegateImpl::SetDefaultPresentationRequest(
724 const PresentationRequest& default_presentation_request) {
725 if (default_presentation_request_ &&
726 default_presentation_request_->Equals(default_presentation_request))
727 return;
728
729 default_presentation_request_.reset(
730 new PresentationRequest(default_presentation_request));
731 for (auto& observer : default_presentation_request_observers_)
732 observer.OnDefaultPresentationChanged(*default_presentation_request_);
733 }
734
735 void PresentationServiceDelegateImpl::ClearDefaultPresentationRequest() {
736 default_presentation_started_callback_.Reset();
737 if (!default_presentation_request_)
738 return;
739
740 default_presentation_request_.reset();
741 for (auto& observer : default_presentation_request_observers_)
742 observer.OnDefaultPresentationRemoved();
743 }
744
745 // TODO(imcheng): Move this check to PresentationServiceImpl.
746 bool PresentationServiceDelegateImpl::IsMainFrame(
747 const RenderFrameHostId& render_frame_host_id) const {
748 RenderFrameHost* main_frame = web_contents_->GetMainFrame();
749 return main_frame && GetRenderFrameHostId(main_frame) == render_frame_host_id;
750 }
751
752 MediaRoute::Id PresentationServiceDelegateImpl::GetRouteId(
753 const RenderFrameHostId& render_frame_host_id,
754 const std::string& presentation_id) const {
755 const auto it = presentation_frames_.find(render_frame_host_id);
756 return it != presentation_frames_.end()
757 ? it->second->GetRouteId(presentation_id)
758 : MediaRoute::Id();
977 } 759 }
978 760
979 #if !defined(OS_ANDROID) 761 #if !defined(OS_ANDROID)
980 bool PresentationServiceDelegateImpl::ShouldCancelAutoJoinForOrigin( 762 bool PresentationServiceDelegateImpl::ShouldCancelAutoJoinForOrigin(
981 const url::Origin& origin) const { 763 const url::Origin& origin) const {
982 const base::ListValue* origins = 764 const base::ListValue* origins =
983 Profile::FromBrowserContext(web_contents_->GetBrowserContext()) 765 Profile::FromBrowserContext(web_contents_->GetBrowserContext())
984 ->GetPrefs() 766 ->GetPrefs()
985 ->GetList(prefs::kMediaRouterTabMirroringSources); 767 ->GetList(prefs::kMediaRouterTabMirroringSources);
986 return origins && 768 return origins &&
987 origins->Find(base::Value(origin.Serialize())) != origins->end(); 769 origins->Find(base::Value(origin.Serialize())) != origins->end();
988 } 770 }
989 #endif // !defined(OS_ANDROID) 771 #endif // !defined(OS_ANDROID)
990 772
991 } // namespace media_router 773 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698