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

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

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

Powered by Google App Engine
This is Rietveld 408576698