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

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 mfoltz's comments Created 5 years, 2 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 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 PresentationFrame* presentation_frame =
449 GetOrAddPresentationFrame(render_frame_host_id);
450 if (default_presentation_url.empty()) {
451 ClearDefaultPresentationRequest();
452 } else {
453 DCHECK(!callback.is_null());
454
455 GURL frame_url(GetLastCommittedURLForFrame(render_frame_host_id));
456 PresentationRequest request(render_frame_host_id, default_presentation_url,
457 frame_url);
458 default_presentation_started_callback_ = callback;
459 SetDefaultPresentationRequest(request);
460 }
461 }
462
400 void PresentationFrameManager::AddDelegateObserver( 463 void PresentationFrameManager::AddDelegateObserver(
401 const RenderFrameHostId& render_frame_host_id, 464 const RenderFrameHostId& render_frame_host_id,
402 DelegateObserver* observer) { 465 DelegateObserver* observer) {
403 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id); 466 auto presentation_frame = GetOrAddPresentationFrame(render_frame_host_id);
404 presentation_frame->set_delegate_observer(observer); 467 presentation_frame->set_delegate_observer(observer);
405 } 468 }
406 469
407 void PresentationFrameManager::RemoveDelegateObserver( 470 void PresentationFrameManager::RemoveDelegateObserver(
408 const RenderFrameHostId& render_frame_host_id) { 471 const RenderFrameHostId& render_frame_host_id) {
409 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 472 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
410 if (presentation_frame) { 473 if (presentation_frame) {
411 presentation_frame->set_delegate_observer(nullptr); 474 presentation_frame->set_delegate_observer(nullptr);
412 presentation_frames_.erase(render_frame_host_id); 475 presentation_frames_.erase(render_frame_host_id);
413 } 476 }
414 } 477 }
415 478
479 void PresentationFrameManager::AddDefaultPresentationRequestObserver(
480 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
481 observer) {
482 default_presentation_request_observers_.AddObserver(observer);
483 }
484
485 void PresentationFrameManager::RemoveDefaultPresentationRequestObserver(
486 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver*
487 observer) {
488 default_presentation_request_observers_.RemoveObserver(observer);
489 }
490
416 void PresentationFrameManager::Reset( 491 void PresentationFrameManager::Reset(
417 const RenderFrameHostId& render_frame_host_id) { 492 const RenderFrameHostId& render_frame_host_id) {
418 auto presentation_frame = presentation_frames_.get(render_frame_host_id); 493 auto presentation_frame = presentation_frames_.get(render_frame_host_id);
419 if (presentation_frame) 494 if (presentation_frame)
420 presentation_frame->Reset(); 495 presentation_frame->Reset();
496
497 if (default_presentation_request_ &&
498 render_frame_host_id ==
499 default_presentation_request_->render_frame_host_id()) {
500 ClearDefaultPresentationRequest();
501 }
421 } 502 }
422 503
423 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame( 504 PresentationFrame* PresentationFrameManager::GetOrAddPresentationFrame(
424 const RenderFrameHostId& render_frame_host_id) { 505 const RenderFrameHostId& render_frame_host_id) {
425 if (!presentation_frames_.contains(render_frame_host_id)) { 506 if (!presentation_frames_.contains(render_frame_host_id)) {
426 presentation_frames_.add( 507 presentation_frames_.add(
427 render_frame_host_id, 508 render_frame_host_id,
428 scoped_ptr<PresentationFrame>( 509 scoped_ptr<PresentationFrame>(
429 new PresentationFrame(web_contents_, router_))); 510 new PresentationFrame(web_contents_, router_)));
430 } 511 }
431 return presentation_frames_.get(render_frame_host_id); 512 return presentation_frames_.get(render_frame_host_id);
432 } 513 }
433 514
515 void PresentationFrameManager::ClearDefaultPresentationRequest() {
516 default_presentation_started_callback_.Reset();
517 if (!default_presentation_request_)
518 return;
519
520 default_presentation_request_.reset();
521 FOR_EACH_OBSERVER(
522 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
523 default_presentation_request_observers_,
524 OnDefaultPresentationChanged(nullptr));
525 }
526
527 void PresentationFrameManager::SetDefaultPresentationRequest(
528 const PresentationRequest& default_presentation_request) {
529 if (default_presentation_request_ &&
530 default_presentation_request_->Equals(default_presentation_request))
531 return;
532
533 default_presentation_request_.reset(
534 new PresentationRequest(default_presentation_request));
535 FOR_EACH_OBSERVER(
536 PresentationServiceDelegateImpl::DefaultPresentationRequestObserver,
537 default_presentation_request_observers_,
538 OnDefaultPresentationChanged(default_presentation_request_.get()));
539 }
540
434 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) { 541 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter* router) {
435 router_ = router; 542 router_ = router;
436 } 543 }
437 544
438 PresentationServiceDelegateImpl* 545 PresentationServiceDelegateImpl*
439 PresentationServiceDelegateImpl::GetOrCreateForWebContents( 546 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
440 content::WebContents* web_contents) { 547 content::WebContents* web_contents) {
441 DCHECK(web_contents); 548 DCHECK(web_contents);
442 // CreateForWebContents does nothing if the delegate instance already exists. 549 // CreateForWebContents does nothing if the delegate instance already exists.
443 PresentationServiceDelegateImpl::CreateForWebContents(web_contents); 550 PresentationServiceDelegateImpl::CreateForWebContents(web_contents);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 content::PresentationScreenAvailabilityListener* listener) { 594 content::PresentationScreenAvailabilityListener* listener) {
488 DCHECK(listener); 595 DCHECK(listener);
489 frame_manager_->RemoveScreenAvailabilityListener( 596 frame_manager_->RemoveScreenAvailabilityListener(
490 RenderFrameHostId(render_process_id, render_frame_id), listener); 597 RenderFrameHostId(render_process_id, render_frame_id), listener);
491 } 598 }
492 599
493 void PresentationServiceDelegateImpl::Reset(int render_process_id, 600 void PresentationServiceDelegateImpl::Reset(int render_process_id,
494 int render_frame_id) { 601 int render_frame_id) {
495 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 602 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
496 frame_manager_->Reset(render_frame_host_id); 603 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 } 604 }
502 605
503 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl( 606 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl(
504 int render_process_id, 607 int render_process_id,
505 int render_frame_id, 608 int render_frame_id,
506 const std::string& default_presentation_url) { 609 const std::string& default_presentation_url,
610 const content::PresentationSessionStartedCallback& callback) {
507 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 611 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
508 if (IsMainFrame(render_process_id, render_frame_id)) { 612 frame_manager_->SetDefaultPresentationUrl(render_frame_host_id,
509 // This is the main frame, which means tab-level default presentation 613 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 } 614 }
544 615
545 void PresentationServiceDelegateImpl::OnJoinRouteResponse( 616 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
546 int render_process_id, 617 int render_process_id,
547 int render_frame_id, 618 int render_frame_id,
548 const content::PresentationSessionInfo& session, 619 const content::PresentationSessionInfo& session,
549 const PresentationSessionSuccessCallback& success_cb, 620 const content::PresentationSessionStartedCallback& success_cb,
550 const PresentationSessionErrorCallback& error_cb, 621 const content::PresentationSessionErrorCallback& error_cb,
551 const MediaRoute* route, 622 const MediaRoute* route,
552 const std::string& presentation_id, 623 const std::string& presentation_id,
553 const std::string& error_text) { 624 const std::string& error_text) {
554 if (!route) { 625 if (!route) {
555 error_cb.Run(content::PresentationError( 626 error_cb.Run(content::PresentationError(
556 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text)); 627 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND, error_text));
557 } else { 628 } else {
558 DVLOG(1) << "OnJoinRouteResponse: " 629 DVLOG(1) << "OnJoinRouteResponse: "
559 << "route_id: " << route->media_route_id() 630 << "route_id: " << route->media_route_id()
560 << ", presentation URL: " << session.presentation_url 631 << ", presentation URL: " << session.presentation_url
561 << ", presentation ID: " << session.presentation_id; 632 << ", presentation ID: " << session.presentation_id;
562 DCHECK_EQ(session.presentation_id, presentation_id); 633 DCHECK_EQ(session.presentation_id, presentation_id);
563 frame_manager_->OnPresentationSessionStarted( 634 frame_manager_->OnPresentationSessionStarted(
564 RenderFrameHostId(render_process_id, render_frame_id), false, session, 635 RenderFrameHostId(render_process_id, render_frame_id), session,
565 route->media_route_id()); 636 route->media_route_id());
566 success_cb.Run(session); 637 success_cb.Run(session);
567 } 638 }
568 } 639 }
569 640
570 void PresentationServiceDelegateImpl::OnStartSessionSucceeded( 641 void PresentationServiceDelegateImpl::OnStartSessionSucceeded(
571 int render_process_id, 642 int render_process_id,
572 int render_frame_id, 643 int render_frame_id,
573 const PresentationSessionSuccessCallback& success_cb, 644 const content::PresentationSessionStartedCallback& success_cb,
574 const content::PresentationSessionInfo& new_session, 645 const content::PresentationSessionInfo& new_session,
575 const MediaRoute::Id& route_id) { 646 const MediaRoute::Id& route_id) {
576 DVLOG(1) << "OnStartSessionSucceeded: " 647 DVLOG(1) << "OnStartSessionSucceeded: "
577 << "route_id: " << route_id 648 << "route_id: " << route_id
578 << ", presentation URL: " << new_session.presentation_url 649 << ", presentation URL: " << new_session.presentation_url
579 << ", presentation ID: " << new_session.presentation_id; 650 << ", presentation ID: " << new_session.presentation_id;
580 frame_manager_->OnPresentationSessionStarted( 651 frame_manager_->OnPresentationSessionStarted(
581 RenderFrameHostId(render_process_id, render_frame_id), false, new_session, 652 RenderFrameHostId(render_process_id, render_frame_id), new_session,
582 route_id); 653 route_id);
583 success_cb.Run(new_session); 654 success_cb.Run(new_session);
584 } 655 }
585 656
586 void PresentationServiceDelegateImpl::StartSession( 657 void PresentationServiceDelegateImpl::StartSession(
587 int render_process_id, 658 int render_process_id,
588 int render_frame_id, 659 int render_frame_id,
589 const std::string& presentation_url, 660 const std::string& presentation_url,
590 const PresentationSessionSuccessCallback& success_cb, 661 const content::PresentationSessionStartedCallback& success_cb,
591 const PresentationSessionErrorCallback& error_cb) { 662 const content::PresentationSessionErrorCallback& error_cb) {
592 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) { 663 if (presentation_url.empty() || !IsValidPresentationUrl(presentation_url)) {
593 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 664 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
594 "Invalid presentation arguments.")); 665 "Invalid presentation arguments."));
595 return; 666 return;
596 } 667 }
668
597 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 669 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
598
599 scoped_ptr<CreatePresentationSessionRequest> context( 670 scoped_ptr<CreatePresentationSessionRequest> context(
600 new CreatePresentationSessionRequest( 671 new CreatePresentationSessionRequest(
601 presentation_url, GetLastCommittedURLForFrame(render_frame_host_id), 672 render_frame_host_id, presentation_url,
673 GetLastCommittedURLForFrame(render_frame_host_id),
602 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded, 674 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded,
603 weak_factory_.GetWeakPtr(), render_process_id, 675 weak_factory_.GetWeakPtr(), render_process_id,
604 render_frame_id, success_cb), 676 render_frame_id, success_cb),
605 error_cb)); 677 error_cb));
606 MediaRouterDialogController* controller = 678 MediaRouterDialogController* controller =
607 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_); 679 MediaRouterDialogController::GetOrCreateForWebContents(web_contents_);
608 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) { 680 if (!controller->ShowMediaRouterDialogForPresentation(context.Pass())) {
609 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession."; 681 LOG(ERROR) << "Media router dialog already exists. Ignoring StartSession.";
610 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, 682 error_cb.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN,
611 "Unable to create dialog.")); 683 "Unable to create dialog."));
612 return; 684 return;
613 } 685 }
614 } 686 }
615 687
616 void PresentationServiceDelegateImpl::JoinSession( 688 void PresentationServiceDelegateImpl::JoinSession(
617 int render_process_id, 689 int render_process_id,
618 int render_frame_id, 690 int render_frame_id,
619 const std::string& presentation_url, 691 const std::string& presentation_url,
620 const std::string& presentation_id, 692 const std::string& presentation_id,
621 const PresentationSessionSuccessCallback& success_cb, 693 const content::PresentationSessionStartedCallback& success_cb,
622 const PresentationSessionErrorCallback& error_cb) { 694 const content::PresentationSessionErrorCallback& error_cb) {
623 std::vector<MediaRouteResponseCallback> route_response_callbacks; 695 std::vector<MediaRouteResponseCallback> route_response_callbacks;
624 route_response_callbacks.push_back(base::Bind( 696 route_response_callbacks.push_back(base::Bind(
625 &PresentationServiceDelegateImpl::OnJoinRouteResponse, 697 &PresentationServiceDelegateImpl::OnJoinRouteResponse,
626 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id, 698 weak_factory_.GetWeakPtr(), render_process_id, render_frame_id,
627 content::PresentationSessionInfo(presentation_url, presentation_id), 699 content::PresentationSessionInfo(presentation_url, presentation_id),
628 success_cb, error_cb)); 700 success_cb, error_cb));
629 router_->JoinRoute( 701 router_->JoinRoute(
630 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id, 702 MediaSourceForPresentationUrl(presentation_url).id(), presentation_id,
631 GetLastCommittedURLForFrame( 703 GetLastCommittedURLForFrame(
632 RenderFrameHostId(render_process_id, render_frame_id)) 704 RenderFrameHostId(render_process_id, render_frame_id))
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 754
683 void PresentationServiceDelegateImpl::ListenForSessionStateChange( 755 void PresentationServiceDelegateImpl::ListenForSessionStateChange(
684 int render_process_id, 756 int render_process_id,
685 int render_frame_id, 757 int render_frame_id,
686 const content::SessionStateChangedCallback& state_changed_cb) { 758 const content::SessionStateChangedCallback& state_changed_cb) {
687 frame_manager_->ListenForSessionStateChange( 759 frame_manager_->ListenForSessionStateChange(
688 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb); 760 RenderFrameHostId(render_process_id, render_frame_id), state_changed_cb);
689 } 761 }
690 762
691 void PresentationServiceDelegateImpl::OnRouteResponse( 763 void PresentationServiceDelegateImpl::OnRouteResponse(
764 const PresentationRequest& presentation_request,
692 const MediaRoute* route, 765 const MediaRoute* route,
693 const std::string& presentation_id, 766 const std::string& presentation_id,
694 const std::string& error) { 767 const std::string& error) {
695 if (!route) 768 if (!route)
696 return; 769 return;
697 const MediaSource& source = route->media_source(); 770
698 DCHECK(!source.Empty()); 771 content::PresentationSessionInfo session_info(
699 if (!default_source_.Equals(source)) 772 presentation_request.presentation_url(), presentation_id);
700 return; 773 frame_manager_->OnDefaultPresentationSessionStarted(
701 RenderFrameHost* main_frame = web_contents_->GetMainFrame(); 774 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 } 775 }
711 776
712 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver( 777 void PresentationServiceDelegateImpl::AddDefaultPresentationRequestObserver(
713 DefaultMediaSourceObserver* observer) { 778 DefaultPresentationRequestObserver* observer) {
714 default_media_source_observers_.AddObserver(observer); 779 frame_manager_->AddDefaultPresentationRequestObserver(observer);
715 } 780 }
716 781
717 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver( 782 void PresentationServiceDelegateImpl::RemoveDefaultPresentationRequestObserver(
718 DefaultMediaSourceObserver* observer) { 783 DefaultPresentationRequestObserver* observer) {
719 default_media_source_observers_.RemoveObserver(observer); 784 frame_manager_->RemoveDefaultPresentationRequestObserver(observer);
785 }
786
787 const PresentationRequest*
788 PresentationServiceDelegateImpl::GetDefaultPresentationRequest() const {
789 return frame_manager_->default_presentation_request();
720 } 790 }
721 791
722 base::WeakPtr<PresentationServiceDelegateImpl> 792 base::WeakPtr<PresentationServiceDelegateImpl>
723 PresentationServiceDelegateImpl::GetWeakPtr() { 793 PresentationServiceDelegateImpl::GetWeakPtr() {
724 return weak_factory_.GetWeakPtr(); 794 return weak_factory_.GetWeakPtr();
725 } 795 }
726 796
727 void PresentationServiceDelegateImpl::SetMediaRouterForTest( 797 void PresentationServiceDelegateImpl::SetMediaRouterForTest(
728 MediaRouter* router) { 798 MediaRouter* router) {
729 router_ = router; 799 router_ = router;
730 frame_manager_->SetMediaRouterForTest(router); 800 frame_manager_->SetMediaRouterForTest(router);
731 } 801 }
732 802
733 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest( 803 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
734 int render_process_id, 804 int render_process_id,
735 int render_frame_id, 805 int render_frame_id,
736 const MediaSource::Id& source_id) const { 806 const MediaSource::Id& source_id) const {
737 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id); 807 RenderFrameHostId render_frame_host_id(render_process_id, render_frame_id);
738 return frame_manager_->HasScreenAvailabilityListenerForTest( 808 return frame_manager_->HasScreenAvailabilityListenerForTest(
739 render_frame_host_id, source_id); 809 render_frame_host_id, source_id);
740 } 810 }
741 811
742 } // namespace media_router 812 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698