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

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

Issue 1406013003: [Presentation API / Media Router] Clean up default pres URL logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments #20, #22 Created 5 years, 1 month 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 8
9 #include "base/containers/scoped_ptr_hash_map.h" 9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/containers/small_map.h" 10 #include "base/containers/small_map.h"
(...skipping 19 matching lines...) Expand all
30 DEFINE_WEB_CONTENTS_USER_DATA_KEY( 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
31 media_router::PresentationServiceDelegateImpl); 31 media_router::PresentationServiceDelegateImpl);
32 32
33 using content::RenderFrameHost; 33 using content::RenderFrameHost;
34 34
35 namespace media_router { 35 namespace media_router {
36 36
37 namespace { 37 namespace {
38 38
39 using DelegateObserver = content::PresentationServiceDelegate::Observer; 39 using DelegateObserver = content::PresentationServiceDelegate::Observer;
40 using PresentationSessionErrorCallback =
41 content::PresentationServiceDelegate::PresentationSessionErrorCallback;
42 using PresentationSessionSuccessCallback =
43 content::PresentationServiceDelegate::PresentationSessionSuccessCallback;
44
45 // Returns the unique identifier for the supplied RenderFrameHost.
46 RenderFrameHostId GetRenderFrameHostId(RenderFrameHost* render_frame_host) {
47 int render_process_id = render_frame_host->GetProcess()->GetID();
48 int render_frame_id = render_frame_host->GetRoutingID();
49 return RenderFrameHostId(render_process_id, render_frame_id);
50 }
51 40
52 // Gets the last committed URL for the render frame specified by 41 // Gets the last committed URL for the render frame specified by
53 // |render_frame_host_id|. 42 // |render_frame_host_id|.
54 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) { 43 GURL GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id) {
55 RenderFrameHost* render_frame_host = RenderFrameHost::FromID( 44 RenderFrameHost* render_frame_host = RenderFrameHost::FromID(
56 render_frame_host_id.first, render_frame_host_id.second); 45 render_frame_host_id.first, render_frame_host_id.second);
57 DCHECK(render_frame_host); 46 DCHECK(render_frame_host);
58 return render_frame_host->GetLastCommittedURL(); 47 return render_frame_host->GetLastCommittedURL();
59 } 48 }
60 49
(...skipping 16 matching lines...) Expand all
77 bool RemoveScreenAvailabilityListener( 66 bool RemoveScreenAvailabilityListener(
78 content::PresentationScreenAvailabilityListener* listener); 67 content::PresentationScreenAvailabilityListener* listener);
79 bool HasScreenAvailabilityListenerForTest( 68 bool HasScreenAvailabilityListenerForTest(
80 const MediaSource::Id& source_id) const; 69 const MediaSource::Id& source_id) const;
81 std::string GetDefaultPresentationId() const; 70 std::string GetDefaultPresentationId() const;
82 void ListenForSessionStateChange( 71 void ListenForSessionStateChange(
83 const content::SessionStateChangedCallback& state_changed_cb); 72 const content::SessionStateChangedCallback& state_changed_cb);
84 void ListenForSessionMessages( 73 void ListenForSessionMessages(
85 const content::PresentationSessionInfo& session, 74 const content::PresentationSessionInfo& session,
86 const content::PresentationSessionMessageCallback& message_cb); 75 const content::PresentationSessionMessageCallback& message_cb);
76
87 void Reset(); 77 void Reset();
88 78
89 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const; 79 const MediaRoute::Id GetRouteId(const std::string& presentation_id) const;
90 const std::vector<MediaRoute::Id> GetRouteIds() const; 80 const std::vector<MediaRoute::Id> GetRouteIds() const;
91 void OnPresentationSessionClosed(const std::string& presentation_id); 81 void OnPresentationSessionClosed(const std::string& presentation_id);
92 82
93 void OnPresentationSessionStarted( 83 void OnPresentationSessionStarted(
94 bool is_default_presentation,
95 const content::PresentationSessionInfo& session, 84 const content::PresentationSessionInfo& session,
96 const MediaRoute::Id& route_id); 85 const MediaRoute::Id& route_id);
97 void OnPresentationServiceDelegateDestroyed() const; 86 void OnPresentationServiceDelegateDestroyed() const;
98 87
99 void set_delegate_observer(DelegateObserver* observer) { 88 void set_delegate_observer(DelegateObserver* observer) {
100 delegate_observer_ = observer; 89 delegate_observer_ = observer;
101 } 90 }
102 91
103 private: 92 private:
104 MediaSource GetMediaSourceFromListener( 93 MediaSource GetMediaSourceFromListener(
(...skipping 23 matching lines...) Expand all
128 117
129 PresentationFrame::~PresentationFrame() { 118 PresentationFrame::~PresentationFrame() {
130 } 119 }
131 120
132 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const { 121 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const {
133 if (delegate_observer_) 122 if (delegate_observer_)
134 delegate_observer_->OnDelegateDestroyed(); 123 delegate_observer_->OnDelegateDestroyed();
135 } 124 }
136 125
137 void PresentationFrame::OnPresentationSessionStarted( 126 void PresentationFrame::OnPresentationSessionStarted(
138 bool is_default_presentation,
139 const content::PresentationSessionInfo& session, 127 const content::PresentationSessionInfo& session,
140 const MediaRoute::Id& route_id) { 128 const MediaRoute::Id& route_id) {
141 presentation_id_to_route_id_[session.presentation_id] = route_id; 129 presentation_id_to_route_id_[session.presentation_id] = route_id;
142 route_id_to_presentation_.Add(route_id, session); 130 route_id_to_presentation_.Add(route_id, session);
143 if (session_state_observer_) 131 if (session_state_observer_)
144 session_state_observer_->OnPresentationSessionConnected(route_id); 132 session_state_observer_->OnPresentationSessionConnected(route_id);
145 if (is_default_presentation && delegate_observer_)
146 delegate_observer_->OnDefaultPresentationStarted(session);
147 } 133 }
148 134
149 void PresentationFrame::OnPresentationSessionClosed( 135 void PresentationFrame::OnPresentationSessionClosed(
150 const std::string& presentation_id) { 136 const std::string& presentation_id) {
151 auto it = presentation_id_to_route_id_.find(presentation_id); 137 auto it = presentation_id_to_route_id_.find(presentation_id);
152 if (it != presentation_id_to_route_id_.end()) { 138 if (it != presentation_id_to_route_id_.end()) {
153 route_id_to_presentation_.Remove(it->second); 139 route_id_to_presentation_.Remove(it->second);
154 presentation_id_to_route_id_.erase(it); 140 presentation_id_to_route_id_.erase(it);
155 } 141 }
156 // TODO(imcheng): Notify |session_state_observer_|? 142 // TODO(imcheng): Notify |session_state_observer_|?
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 void PresentationFrame::Reset() { 190 void PresentationFrame::Reset() {
205 route_id_to_presentation_.Clear(); 191 route_id_to_presentation_.Clear();
206 192
207 for (const auto& pid_route_id : presentation_id_to_route_id_) 193 for (const auto& pid_route_id : presentation_id_to_route_id_)
208 router_->OnPresentationSessionDetached(pid_route_id.second); 194 router_->OnPresentationSessionDetached(pid_route_id.second);
209 195
210 presentation_id_to_route_id_.clear(); 196 presentation_id_to_route_id_.clear();
211 sinks_observer_.reset(); 197 sinks_observer_.reset();
212 if (session_state_observer_) 198 if (session_state_observer_)
213 session_state_observer_->Reset(); 199 session_state_observer_->Reset();
200
214 session_messages_observers_.clear(); 201 session_messages_observers_.clear();
215 } 202 }
216 203
217 void PresentationFrame::ListenForSessionStateChange( 204 void PresentationFrame::ListenForSessionStateChange(
218 const content::SessionStateChangedCallback& state_changed_cb) { 205 const content::SessionStateChangedCallback& state_changed_cb) {
219 CHECK(!session_state_observer_.get()); 206 CHECK(!session_state_observer_.get());
220 session_state_observer_.reset(new PresentationSessionStateObserver( 207 session_state_observer_.reset(new PresentationSessionStateObserver(
221 state_changed_cb, &route_id_to_presentation_, router_)); 208 state_changed_cb, &route_id_to_presentation_, router_));
222 } 209 }
223 210
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 bool RemoveScreenAvailabilityListener( 245 bool RemoveScreenAvailabilityListener(
259 const RenderFrameHostId& render_frame_host_id, 246 const RenderFrameHostId& render_frame_host_id,
260 content::PresentationScreenAvailabilityListener* listener); 247 content::PresentationScreenAvailabilityListener* listener);
261 void ListenForSessionStateChange( 248 void ListenForSessionStateChange(
262 const RenderFrameHostId& render_frame_host_id, 249 const RenderFrameHostId& render_frame_host_id,
263 const content::SessionStateChangedCallback& state_changed_cb); 250 const content::SessionStateChangedCallback& state_changed_cb);
264 void ListenForSessionMessages( 251 void ListenForSessionMessages(
265 const RenderFrameHostId& render_frame_host_id, 252 const RenderFrameHostId& render_frame_host_id,
266 const content::PresentationSessionInfo& session, 253 const content::PresentationSessionInfo& session,
267 const content::PresentationSessionMessageCallback& message_cb); 254 const content::PresentationSessionMessageCallback& message_cb);
255
256 // Sets or clears the default presentation request and callback for the given
257 // frame. Also sets / clears the default presentation requests for the owning
258 // tab WebContents.
259 void SetDefaultPresentationUrl(
260 const RenderFrameHostId& render_frame_host_id,
261 const std::string& default_presentation_url,
262 const content::PresentationSessionStartedCallback& callback);
268 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id, 263 void AddDelegateObserver(const RenderFrameHostId& render_frame_host_id,
269 DelegateObserver* observer); 264 DelegateObserver* observer);
270 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id); 265 void RemoveDelegateObserver(const RenderFrameHostId& render_frame_host_id);
266 void AddDefaultPresentationRequestObserver(
267 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
268 observer);
269 void RemoveDefaultPresentationRequestObserver(
270 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
271 observer);
271 void Reset(const RenderFrameHostId& render_frame_host_id); 272 void Reset(const RenderFrameHostId& render_frame_host_id);
272 bool HasScreenAvailabilityListenerForTest( 273 bool HasScreenAvailabilityListenerForTest(
273 const RenderFrameHostId& render_frame_host_id, 274 const RenderFrameHostId& render_frame_host_id,
274 const MediaSource::Id& source_id) const; 275 const MediaSource::Id& source_id) const;
275 void SetMediaRouterForTest(MediaRouter* router); 276 void SetMediaRouterForTest(MediaRouter* router);
276 277
277 void OnPresentationSessionStarted( 278 void OnPresentationSessionStarted(
278 const RenderFrameHostId& render_frame_host_id, 279 const RenderFrameHostId& render_frame_host_id,
279 bool is_default_presentation, 280 const content::PresentationSessionInfo& session,
281 const MediaRoute::Id& route_id);
282 void OnDefaultPresentationSessionStarted(
283 const PresentationRequest& request,
280 const content::PresentationSessionInfo& session, 284 const content::PresentationSessionInfo& session,
281 const MediaRoute::Id& route_id); 285 const MediaRoute::Id& route_id);
282 286
283 void OnPresentationSessionClosed( 287 void OnPresentationSessionClosed(
284 const RenderFrameHostId& render_frame_host_id, 288 const RenderFrameHostId& render_frame_host_id,
285 const std::string& presentation_id); 289 const std::string& presentation_id);
286 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id, 290 const MediaRoute::Id GetRouteId(const RenderFrameHostId& render_frame_host_id,
287 const std::string& presentation_id) const; 291 const std::string& presentation_id) const;
288 const std::vector<MediaRoute::Id> GetRouteIds( 292 const std::vector<MediaRoute::Id> GetRouteIds(
289 const RenderFrameHostId& render_frame_host_id) const; 293 const RenderFrameHostId& render_frame_host_id) const;
290 294
295 const PresentationRequest* default_presentation_request() const {
296 return default_presentation_request_.get();
297 }
298
291 private: 299 private:
292 PresentationFrame* GetOrAddPresentationFrame( 300 PresentationFrame* GetOrAddPresentationFrame(
293 const RenderFrameHostId& render_frame_host_id); 301 const RenderFrameHostId& render_frame_host_id);
294 302
303 // Sets the default presentation request for the owning WebContents and
304 // notifies observers of changes.
305 void SetDefaultPresentationRequest(
306 const PresentationRequest& default_presentation_request);
307
308 // Clears the default presentation request for the owning WebContents and
309 // notifies observers of changes. Also resets
310 // |default_presentation_started_callback_|.
311 void ClearDefaultPresentationRequest();
312
295 // Maps a frame identifier to a PresentationFrame object for frames 313 // Maps a frame identifier to a PresentationFrame object for frames
296 // that are using presentation API. 314 // that are using presentation API.
297 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>> 315 base::ScopedPtrHashMap<RenderFrameHostId, scoped_ptr<PresentationFrame>>
298 presentation_frames_; 316 presentation_frames_;
299 317
318 // Default presentation request for the owning tab WebContents.
319 scoped_ptr<PresentationRequest> default_presentation_request_;
320
321 // Callback to invoke when default presentation has started.
322 content::PresentationSessionStartedCallback
323 default_presentation_started_callback_;
324
325 // References to the observers listening for changes to this tab WebContent's
326 // default presentation.
327 base::ObserverList<
328 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver>
329 default_presentation_request_observers_;
330
300 // References to the owning WebContents, and the corresponding MediaRouter. 331 // References to the owning WebContents, and the corresponding MediaRouter.
301 MediaRouter* router_; 332 MediaRouter* router_;
302 content::WebContents* web_contents_; 333 content::WebContents* web_contents_;
303 }; 334 };
304 335
305 PresentationFrameManager::PresentationFrameManager( 336 PresentationFrameManager::PresentationFrameManager(
306 content::WebContents* web_contents, 337 content::WebContents* web_contents,
307 MediaRouter* router) 338 MediaRouter* router)
308 : router_(router), web_contents_(web_contents) { 339 : router_(router), web_contents_(web_contents) {
309 DCHECK(web_contents_); 340 DCHECK(web_contents_);
310 DCHECK(router_); 341 DCHECK(router_);
311 } 342 }
312 343
313 PresentationFrameManager::~PresentationFrameManager() { 344 PresentationFrameManager::~PresentationFrameManager() {
314 for (auto& frame : presentation_frames_) 345 for (auto& frame : presentation_frames_)
315 frame.second->OnPresentationServiceDelegateDestroyed(); 346 frame.second->OnPresentationServiceDelegateDestroyed();
316 } 347 }
317 348
318 void PresentationFrameManager::OnPresentationSessionStarted( 349 void PresentationFrameManager::OnPresentationSessionStarted(
319 const RenderFrameHostId& render_frame_host_id, 350 const RenderFrameHostId& render_frame_host_id,
320 bool is_default_presentation,
321 const content::PresentationSessionInfo& session, 351 const content::PresentationSessionInfo& session,
322 const MediaRoute::Id& route_id) { 352 const MediaRoute::Id& route_id) {
323 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 353 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
324 if (presentation_frame) 354 if (presentation_frame)
325 presentation_frame->OnPresentationSessionStarted(is_default_presentation, 355 presentation_frame->OnPresentationSessionStarted(session, route_id);
326 session, route_id); 356 }
357
358 void PresentationFrameManager::OnDefaultPresentationSessionStarted(
359 const PresentationRequest& request,
360 const content::PresentationSessionInfo& session,
361 const MediaRoute::Id& route_id) {
362 auto presentation_frame =
363 presentation_frames_.get(request.render_frame_host_id());
364 if (presentation_frame)
365 presentation_frame->OnPresentationSessionStarted(session, route_id);
366
367 if (default_presentation_request_ &&
368 default_presentation_request_->Equals(request)) {
369 default_presentation_started_callback_.Run(session);
370 }
327 } 371 }
328 372
329 void PresentationFrameManager::OnPresentationSessionClosed( 373 void PresentationFrameManager::OnPresentationSessionClosed(
330 const RenderFrameHostId& render_frame_host_id, 374 const RenderFrameHostId& render_frame_host_id,
331 const std::string& presentation_id) { 375 const std::string& presentation_id) {
332 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 376 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
333 if (presentation_frame) 377 if (presentation_frame)
334 presentation_frame->OnPresentationSessionClosed(presentation_id); 378 presentation_frame->OnPresentationSessionClosed(presentation_id);
335 } 379 }
336 380
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 presentation_frames_.get(render_frame_host_id); 434 presentation_frames_.get(render_frame_host_id);
391 if (!presentation_frame) { 435 if (!presentation_frame) {
392 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist " 436 DVLOG(2) << "ListenForSessionMessages: PresentationFrame does not exist "
393 << "for: (" << render_frame_host_id.first << ", " 437 << "for: (" << render_frame_host_id.first << ", "
394 << render_frame_host_id.second << ")"; 438 << render_frame_host_id.second << ")";
395 return; 439 return;
396 } 440 }
397 presentation_frame->ListenForSessionMessages(session, message_cb); 441 presentation_frame->ListenForSessionMessages(session, message_cb);
398 } 442 }
399 443
444 void PresentationFrameManager::SetDefaultPresentationUrl(
445 const RenderFrameHostId& render_frame_host_id,
446 const std::string& default_presentation_url,
447 const content::PresentationSessionStartedCallback& callback) {
448 if (default_presentation_url.empty()) {
449 ClearDefaultPresentationRequest();
450 } else {
451 DCHECK(!callback.is_null());
452
mark a. foltz 2015/11/09 18:33:35 Extra newline
imcheng 2015/11/10 18:49:13 Done.
453 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id));
454 PresentationRequest request(render_frame_host_id, default_presentation_url,
455 frame_url);
456 default_presentation_started_callback_ = callback;
457 SetDefaultPresentationRequest(request);
458 }
459 }
460
400 void PresentationFrameManager::AddDelegateObserver( 461 void PresentationFrameManager::AddDelegateObserver(
401 const RenderFrameHostId& render_frame_host_id, 462 const RenderFrameHostId& render_frame_host_id,
402 DelegateObserver* observer) { 463 DelegateObserver* observer) {
403 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); 464 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
404 presentation_frame->set_delegate_observer(observer); 465 presentation_frame->set_delegate_observer(observer);
405 } 466 }
406 467
407 void PresentationFrameManager::RemoveDelegateObserver( 468 void PresentationFrameManager::RemoveDelegateObserver(
408 const RenderFrameHostId& render_frame_host_id) { 469 const RenderFrameHostId& render_frame_host_id) {
409 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 470 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
410 if (presentation_frame) { 471 if (presentation_frame) {
411 presentation_frame->set_delegate_observer(nullptr); 472 presentation_frame->set_delegate_observer(nullptr);
412 presentation_frames_.erase(render_frame_host_id); 473 presentation_frames_.erase(render_frame_host_id);
413 } 474 }
414 } 475 }
415 476
477 void PresentationFrameManager::AddDefaultPresentationRequestObserver(
478 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
479 observer) {
480 default_presentation_request_observers_.AddObserver(observer);
481 }
482
483 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver(
484 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
485 observer) {
486 default_presentation_request_observers_.RemoveObserver(observer);
487 }
488
416 void PresentationFrameManager::Reset( 489 void PresentationFrameManager::Reset(
417 const RenderFrameHostId& render_frame_host_id) { 490 const RenderFrameHostId& render_frame_host_id) {
418 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 491 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
419 if (presentation_frame) 492 if (presentation_frame)
420 presentation_frame->Reset(); 493 presentation_frame->Reset();
494
495 if (default_presentation_request_ &&
496 render_frame_host_id ==
497 default_presentation_request_->render_frame_host_id()) {
498 ClearDefaultPresentationRequest();
499 }
421 } 500 }
422 501
423 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( 502 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame(
424 const RenderFrameHostId& render_frame_host_id) { 503 const RenderFrameHostId& render_frame_host_id) {
425 if (!presentation_frames_.contains(render_frame_host_id)) { 504 if (!presentation_frames_.contains(render_frame_host_id)) {
426 presentation_frames_.add( 505 presentation_frames_.add(
427 render_frame_host_id, 506 render_frame_host_id,
428 scoped_ptr<PresentationFrame>( 507 scoped_ptr<PresentationFrame>(
429 new PresentationFrame(web_contents_, router_))); 508 new PresentationFrame(web_contents_, router_)));
430 } 509 }
431 return presentation_frames_.get(render_frame_host_id); 510 return presentation_frames_.get(render_frame_host_id);
432 } 511 }
433 512
513 void PresentationFrameManager::ClearDefaultPresentationRequest() {
514 default_presentation_started_callback_.Reset();
515 if (!default_presentation_request_)
516 return;
517
518 default_presentation_request_.reset();
519 FOR_EACH_OBSERVER(
520 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
521 default_presentation_request_observers_, OnDefaultPresentationRemoved());
522 }
523
524 void PresentationFrameManager::SetDefaultPresentationRequest(
525 const PresentationRequest& default_presentation_request) {
mark a. foltz 2015/11/09 18:33:35 Is it possible to call this with an empty |default
imcheng 2015/11/10 18:49:13 No, because there's no empty ctor for Presentation
526 if (default_presentation_request_ &&
527 default_presentation_request_->Equals(default_presentation_request))
528 return;
529
530 default_presentation_request_.reset(
531 new PresentationRequest(default_presentation_request));
532 FOR_EACH_OBSERVER(
533 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
534 default_presentation_request_observers_,
535 OnDefaultPresentationChanged(*default_presentation_request_));
536 }
537
434 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { 538 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) {
435 router_ = router; 539 router_ = router;
436 } 540 }
437 541
438 PresentationServiceDelegateImpl* 542 PresentationServiceDelegateImpl*
439 PresentationServiceDelegateImpl::GetOrCreateForWebContents( 543 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
440 content::WebContents* web_contents) { 544 content::WebContents* web_contents) {
441 DCHECK(web_contents); 545 DCHECK(web_contents);
442 // CreateForWebContents does nothing if the delegate instance already exists. 546 // CreateForWebContents does nothing if the delegate instance already exists.
443 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); 547 PresentationServiceDelegateImpl::CreateForWebContents(web_contents);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 content::PresentationScreenAvailabilityListener* listener) { 591 content::PresentationScreenAvailabilityListener* listener) {
488 DCHECK(listener); 592 DCHECK(listener);
489 frame_manager_->RemoveScreenAvailabilityListener( 593 frame_manager_->RemoveScreenAvailabilityListener(
490 RenderFrameHostId(render_process_id, render_frame_id), listener); 594 RenderFrameHostId(render_process_id, render_frame_id), listener);
491 } 595 }
492 596
493 void PresentationServiceDelegateImpl::Reset(int render_process_id, 597 void PresentationServiceDelegateImpl::Reset(int render_process_id,
494 int render_frame_id) { 598 int render_frame_id) {
495 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 599 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
496 frame_manager_->Reset(render_frame_host_id); 600 frame_manager_->Reset(render_frame_host_id);
497 if (render_frame_host_id == default_presentation_render_frame_host_id_) {
498 UpdateDefaultMediaSourceAndNotifyObservers(RenderFrameHostId(),
499 MediaSource(), GURL());
500 }
501 } 601 }
502 602
503 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl( 603 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl(
504 int render_process_id, 604 int render_process_id,
505 int render_frame_id, 605 int render_frame_id,
506 const std::string& default_presentation_url) { 606 const std::string& default_presentation_url,
607 const content::PresentationSessionStartedCallback& callback) {
507 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 608 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
508 if (IsMainFrame(render_process_id, render_frame_id)) { 609 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id,
509 // This is the main frame, which means tab-level default presentation 610 default_presentation_url, callback);
510 // might have been updated.
511 MediaSource default_source;
512 if (!default_presentation_url.empty())
513 default_source = MediaSourceForPresentationUrl(default_presentation_url);
514
515 GURL default_frame_url = GetLastCommittedURLForFrame(render_frame_host_id);
516 UpdateDefaultMediaSourceAndNotifyObservers(
517 render_frame_host_id, default_source, default_frame_url);
518 }
519 }
520
521 bool PresentationServiceDelegateImpl::IsMainFrame(int render_process_id,
522 int render_frame_id) const {
523 RenderFrameHost* main_frame = web_contents_->GetMainFrame();
524 return main_frame &&
525 GetRenderFrameHostId(main_frame) ==
526 RenderFrameHostId(render_process_id, render_frame_id);
527 }
528
529 void PresentationServiceDelegateImpl::
530 UpdateDefaultMediaSourceAndNotifyObservers(
531 const RenderFrameHostId& render_frame_host_id,
532 const MediaSource& new_default_source,
533 const GURL& new_default_frame_url) {
534 default_presentation_render_frame_host_id_ = render_frame_host_id;
535 if (!new_default_source.Equals(default_source_) ||
536 new_default_frame_url != default_frame_url_) {
537 default_source_ = new_default_source;
538 default_frame_url_ = new_default_frame_url;
539 FOR_EACH_OBSERVER(
540 DefaultMediaSourceObserver, default_media_source_observers_,
541 OnDefaultMediaSourceChanged(default_source_, default_frame_url_));
542 }
543 } 611 }
544 612
545 void PresentationServiceDelegateImpl::OnJoinRouteResponse( 613 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
546 int render_process_id, 614 int render_process_id,
547 int render_frame_id, 615 int render_frame_id,
548 const content::PresentationSessionInfo& session, 616 const content::PresentationSessionInfo& session,
549 const PresentationSessionSuccessCallback& success_cb, 617 const content::PresentationSessionStartedCallback& success_cb,
550 const PresentationSessionErrorCallback& error_cb, 618 const content::PresentationSessionErrorCallback& error_cb,
551 const MediaRoute* route, 619 const MediaRoute* route,
552 const std::string& presentation_id, 620 const std::string& presentation_id,
553 const std::string& error_text) { 621 const std::string& error_text) {
554 if (!route) { 622 if (!route) {
555 error_cb.Run(content::PresentationError( 623 error_cb.Run(content::PresentationError(
556 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); 624 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text));
557 } else { 625 } else {
558 DVLOG(1) << "OnJoinRouteResponse: " 626 DVLOG(1) << "OnJoinRouteResponse: "
559 << "route_id: " << route->media_route_id() 627 << "route_id: " << route->media_route_id()
560 << ", presentation URL: " << session.presentation_url 628 << ", presentation URL: " << session.presentation_url
561 << ", presentation ID: " << session.presentation_id; 629 << ", presentation ID: " << session.presentation_id;
562 DCHECK_EQ(session.presentation_id, presentation_id); 630 DCHECK_EQ(session.presentation_id, presentation_id);
563 frame_manager_->OnPresentationSessionStarted( 631 frame_manager_->OnPresentationSessionStarted(
564 RenderFrameHostId(render_process_id, render_frame_id), false, session, 632 RenderFrameHostId(render_process_id, render_frame_id), session,
565 route->media_route_id()); 633 route->media_route_id());
566 success_cb.Run(session); 634 success_cb.Run(session);
567 } 635 }
568 } 636 }
569 637
570 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( 638 void PresentationServiceDelegateImpl::OnStartSessionSucceeded(
571 int render_process_id, 639 int render_process_id,
572 int render_frame_id, 640 int render_frame_id,
573 const PresentationSessionSuccessCallback& success_cb, 641 const content::PresentationSessionStartedCallback& success_cb,
574 const content::PresentationSessionInfo& new_session, 642 const content::PresentationSessionInfo& new_session,
575 const MediaRoute::Id& route_id) { 643 const MediaRoute::Id& route_id) {
576 DVLOG(1) << "OnStartSessionSucceeded: " 644 DVLOG(1) << "OnStartSessionSucceeded: "
577 << "route_id: " << route_id 645 << "route_id: " << route_id
578 << ", presentation URL: " << new_session.presentation_url 646 << ", presentation URL: " << new_session.presentation_url
579 << ", presentation ID: " << new_session.presentation_id; 647 << ", presentation ID: " << new_session.presentation_id;
580 frame_manager_->OnPresentationSessionStarted( 648 frame_manager_->OnPresentationSessionStarted(
581 RenderFrameHostId(render_process_id, render_frame_id), false, new_session, 649 RenderFrameHostId(render_process_id, render_frame_id), new_session,
582 route_id); 650 route_id);
583 success_cb.Run(new_session); 651 success_cb.Run(new_session);
584 } 652 }
585 653
586 void PresentationServiceDelegateImpl::StartSession( 654 void PresentationServiceDelegateImpl::StartSession(
587 int render_process_id, 655 int render_process_id,
588 int render_frame_id, 656 int render_frame_id,
589 const std::string& presentation_url, 657 const std::string& presentation_url,
590 const PresentationSessionSuccessCallback& success_cb, 658 const content::PresentationSessionStartedCallback& success_cb,
591 const PresentationSessionErrorCallback& error_cb) { 659 const content::PresentationSessionErrorCallback& error_cb) {
592 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { 660 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) {
593 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 661 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
594 "Invalid presentation arguments.")); 662 "Invalid presentation arguments."));
595 return; 663 return;
596 } 664 }
665
597 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 666 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
598
599 scoped_ptr<CreatePresentationSessionRequest> context( 667 scoped_ptr<CreatePresentationSessionRequest> context(
600 new CreatePresentationSessionRequest( 668 new CreatePresentationSessionRequest(
601 presentation_url, GetLastCommittedURLForFrame(render_frame_host_id), 669 render_frame_host_id, presentation_url,
670 GetLastCommittedURLForFrame(render_frame_host_id),
602 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, 671 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded,
603 weak_factory_.GetWeakPtr(), render_process_id, 672 weak_factory_.GetWeakPtr(), render_process_id,
604 render_frame_id, success_cb), 673 render_frame_id, success_cb),
605 error_cb)); 674 error_cb));
606 MediaRouterDialogController* controller = 675 MediaRouterDialogController* controller =
607 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); 676 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_);
608 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) { 677 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) {
609 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; 678 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession.";
610 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 679 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
611 "Unable to create dialog.")); 680 "Unable to create dialog."));
612 return; 681 return;
613 } 682 }
614 } 683 }
615 684
616 void PresentationServiceDelegateImpl::JoinSession( 685 void PresentationServiceDelegateImpl::JoinSession(
617 int render_process_id, 686 int render_process_id,
618 int render_frame_id, 687 int render_frame_id,
619 const std::string& presentation_url, 688 const std::string& presentation_url,
620 const std::string& presentation_id, 689 const std::string& presentation_id,
621 const PresentationSessionSuccessCallback& success_cb, 690 const content::PresentationSessionStartedCallback& success_cb,
622 const PresentationSessionErrorCallback& error_cb) { 691 const content::PresentationSessionErrorCallback& error_cb) {
623 std::vector<MediaRouteResponseCallback> route_response_callbacks; 692 std::vector<MediaRouteResponseCallback> route_response_callbacks;
624 route_response_callbacks.push_back(base::Bind( 693 route_response_callbacks.push_back(base::Bind(
625 &PresentationServiceDelegateImpl::OnJoinRouteResponse, 694 &PresentationServiceDelegateImpl::OnJoinRouteResponse,
626 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 695 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id,
627 content::PresentationSessionInfo(presentation_url, presentation_id), 696 content::PresentationSessionInfo(presentation_url, presentation_id),
628 success_cb, error_cb)); 697 success_cb, error_cb));
629 router_->JoinRoute( 698 router_->JoinRoute(
630 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, 699 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id,
631 GetLastCommittedURLForFrame( 700 GetLastCommittedURLForFrame(
632 RenderFrameHostId(render_process_id, render_frame_id)) 701 RenderFrameHostId(render_process_id, render_frame_id))
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 751
683 void PresentationServiceDelegateImpl::ListenForSessionStateChange( 752 void PresentationServiceDelegateImpl::ListenForSessionStateChange(
684 int render_process_id, 753 int render_process_id,
685 int render_frame_id, 754 int render_frame_id,
686 const content::SessionStateChangedCallback& state_changed_cb) { 755 const content::SessionStateChangedCallback& state_changed_cb) {
687 frame_manager_->ListenForSessionStateChange( 756 frame_manager_->ListenForSessionStateChange(
688 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb); 757 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb);
689 } 758 }
690 759
691 void PresentationServiceDelegateImpl::OnRouteResponse( 760 void PresentationServiceDelegateImpl::OnRouteResponse(
761 const PresentationRequest& presentation_request,
692 const MediaRoute* route, 762 const MediaRoute* route,
693 const std::string& presentation_id, 763 const std::string& presentation_id,
694 const std::string& error) { 764 const std::string& error) {
695 if (!route) 765 if (!route)
696 return; 766 return;
697 const MediaSource& source = route->media_source(); 767
698 DCHECK(!source.Empty()); 768 content::PresentationSessionInfo session_info(
699 if (!default_source_.Equals(source)) 769 presentation_request.presentation_url(), presentation_id);
700 return; 770 frame_manager_->OnDefaultPresentationSessionStarted(
701 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); 771 presentation_request, session_info, route->media_route_id());
702 if (!main_frame)
703 return;
704 RenderFrameHostId render_frame_host_id(GetRenderFrameHostId(main_frame));
705 frame_manager_->OnPresentationSessionStarted(
706 render_frame_host_id, true,
707 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source),
708 presentation_id),
709 route->media_route_id());
710 } 772 }
711 773
712 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( 774 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver(
713 DefaultMediaSourceObserver* observer) { 775 DefaultPresentationRequestObserver* observer) {
714 default_media_source_observers_.AddObserver(observer); 776 frame_manager_->AddDefaultPresentationRequestObserver(observer);
715 } 777 }
716 778
717 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( 779 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver(
718 DefaultMediaSourceObserver* observer) { 780 DefaultPresentationRequestObserver* observer) {
719 default_media_source_observers_.RemoveObserver(observer); 781 frame_manager_->RemoveDefaultPresentationRequestObserver(observer);
782 }
783
784 PresentationRequest
785 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const {
786 DCHECK(HasDefaultPresentationRequest());
787 return *frame_manager_->default_presentation_request();
788 }
789
790 bool PresentationServiceDelegateImpl::HasDefaultPresentationRequest() const {
791 return frame_manager_->default_presentation_request() != nullptr;
720 } 792 }
721 793
722 base::WeakPtr<PresentationServiceDelegateImpl> 794 base::WeakPtr<PresentationServiceDelegateImpl>
723 PresentationServiceDelegateImpl::GetWeakPtr() { 795 PresentationServiceDelegateImpl::GetWeakPtr() {
724 return weak_factory_.GetWeakPtr(); 796 return weak_factory_.GetWeakPtr();
725 } 797 }
726 798
727 void PresentationServiceDelegateImpl::SetMediaRouterForTest( 799 void PresentationServiceDelegateImpl::SetMediaRouterForTest(
728 MediaRouter* router) { 800 MediaRouter* router) {
729 router_ = router; 801 router_ = router;
730 frame_manager_->SetMediaRouterForTest(router); 802 frame_manager_->SetMediaRouterForTest(router);
731 } 803 }
732 804
733 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 805 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
734 int render_process_id, 806 int render_process_id,
735 int render_frame_id, 807 int render_frame_id,
736 const MediaSource::Id& source_id) const { 808 const MediaSource::Id& source_id) const {
737 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 809 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
738 return frame_manager_->HasScreenAvailabilityListenerForTest( 810 return frame_manager_->HasScreenAvailabilityListenerForTest(
739 render_frame_host_id, source_id); 811 render_frame_host_id, source_id);
740 } 812 }
741 813
742 } // namespace media_router 814 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698