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
3 // found in the LICENSE file.
5 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/containers/small_map.h"
11 #include "base/guid.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/media/router/create_session_request.h"
15 #include "chrome/browser/media/router/media_route.h"
16 #include "chrome/browser/media/router/media_router.h"
17 #include "chrome/browser/media/router/media_router_mojo_impl.h"
18 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h"
19 #include "chrome/browser/media/router/media_sink.h"
20 #include "chrome/browser/media/router/media_source_helper.h"
21 #include "chrome/browser/media/router/presentation_media_sinks_observer.h"
22 #include "chrome/browser/sessions/session_tab_helper.h"
23 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller.h"
24 #include "content/public/browser/presentation_screen_availability_listener.h"
25 #include "content/public/browser/presentation_session.h"
26 #include "content/public/browser/render_frame_host.h"
27 #include "content/public/browser/render_process_host.h"
29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(
30 media_router::PresentationServiceDelegateImpl
);
32 using content::RenderFrameHost
;
34 namespace media_router
{
38 using DelegateObserver
= content::PresentationServiceDelegate::Observer
;
39 using PresentationSessionErrorCallback
=
40 content::PresentationServiceDelegate::PresentationSessionErrorCallback
;
41 using PresentationSessionSuccessCallback
=
42 content::PresentationServiceDelegate::PresentationSessionSuccessCallback
;
43 using RenderFrameHostId
= std::pair
<int, int>;
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
);
52 // Gets the last committed URL for the render frame specified by
53 // |render_frame_host_id|.
54 GURL
GetLastCommittedURLForFrame(RenderFrameHostId render_frame_host_id
) {
55 RenderFrameHost
* render_frame_host
= RenderFrameHost::FromID(
56 render_frame_host_id
.first
, render_frame_host_id
.second
);
57 DCHECK(render_frame_host
);
58 return render_frame_host
->GetLastCommittedURL();
63 // Used by PresentationServiceDelegateImpl to manage
64 // listeners and default presentation info in a render frame.
66 // * PresentationFrameManager AddDelegateObserver
68 // * PresentationFrameManager.RemoveDelegateObserver.
69 class PresentationFrame
{
71 PresentationFrame(content::WebContents
* web_contents
, MediaRouter
* router
);
74 // Mirror corresponding APIs in PresentationServiceDelegateImpl.
75 bool SetScreenAvailabilityListener(
76 content::PresentationScreenAvailabilityListener
* listener
);
77 bool RemoveScreenAvailabilityListener(
78 content::PresentationScreenAvailabilityListener
* listener
);
79 bool HasScreenAvailabilityListenerForTest(
80 const MediaSource::Id
& source_id
) const;
81 void SetDefaultPresentationInfo(const std::string
& default_presentation_url
,
82 const std::string
& default_presentation_id
);
83 std::string
GetDefaultPresentationId() const;
86 const MediaRoute::Id
GetRouteId(const std::string
& presentation_id
) const;
87 const std::vector
<MediaRoute::Id
> GetRouteIds() const;
88 void OnPresentationSessionClosed(const std::string
& presentation_id
);
90 void OnPresentationSessionStarted(
91 bool is_default_presentation
,
92 const content::PresentationSessionInfo
& session
,
93 const MediaRoute::Id
& route_id
);
94 void OnPresentationServiceDelegateDestroyed() const;
96 void set_delegate_observer(DelegateObserver
* observer
) {
97 delegate_observer_
= observer
;
101 MediaSource
GetMediaSourceFromListener(
102 content::PresentationScreenAvailabilityListener
* listener
) const;
104 base::SmallMap
<std::map
<std::string
, MediaRoute::Id
>>
105 presentation_id_to_route_id_
;
106 scoped_ptr
<content::PresentationSessionInfo
> default_presentation_info_
;
107 scoped_ptr
<PresentationMediaSinksObserver
> sinks_observer_
;
109 // References to the owning WebContents, and the corresponding MediaRouter.
110 const content::WebContents
* web_contents_
;
111 MediaRouter
* router_
;
113 DelegateObserver
* delegate_observer_
;
116 PresentationFrame::PresentationFrame(content::WebContents
* web_contents
,
118 : web_contents_(web_contents
),
120 delegate_observer_(nullptr) {
121 DCHECK(web_contents_
);
125 PresentationFrame::~PresentationFrame() {
128 void PresentationFrame::OnPresentationServiceDelegateDestroyed() const {
129 if (delegate_observer_
)
130 delegate_observer_
->OnDelegateDestroyed();
133 void PresentationFrame::OnPresentationSessionStarted(
134 bool is_default_presentation
,
135 const content::PresentationSessionInfo
& session
,
136 const MediaRoute::Id
& route_id
) {
137 presentation_id_to_route_id_
[session
.presentation_id
] = route_id
;
138 if (is_default_presentation
&& delegate_observer_
)
139 delegate_observer_
->OnDefaultPresentationStarted(session
);
142 void PresentationFrame::OnPresentationSessionClosed(
143 const std::string
& presentation_id
) {
144 presentation_id_to_route_id_
.erase(presentation_id
);
147 const MediaRoute::Id
PresentationFrame::GetRouteId(
148 const std::string
& presentation_id
) const {
149 auto it
= presentation_id_to_route_id_
.find(presentation_id
);
150 return it
!= presentation_id_to_route_id_
.end() ? it
->second
: "";
153 const std::vector
<MediaRoute::Id
> PresentationFrame::GetRouteIds() const {
154 std::vector
<MediaRoute::Id
> route_ids
;
155 for (const auto& e
: presentation_id_to_route_id_
)
156 route_ids
.push_back(e
.second
);
160 bool PresentationFrame::SetScreenAvailabilityListener(
161 content::PresentationScreenAvailabilityListener
* listener
) {
162 if (sinks_observer_
&& sinks_observer_
->listener() == listener
) {
165 MediaSource
source(GetMediaSourceFromListener(listener
));
166 sinks_observer_
.reset(
167 new PresentationMediaSinksObserver(router_
, listener
, source
));
171 bool PresentationFrame::RemoveScreenAvailabilityListener(
172 content::PresentationScreenAvailabilityListener
* listener
) {
173 if (sinks_observer_
&& sinks_observer_
->listener() == listener
) {
174 sinks_observer_
.reset();
180 bool PresentationFrame::HasScreenAvailabilityListenerForTest(
181 const MediaSource::Id
& source_id
) const {
182 return sinks_observer_
&& sinks_observer_
->source().id() == source_id
;
185 void PresentationFrame::Reset() {
186 presentation_id_to_route_id_
.clear();
187 sinks_observer_
.reset();
188 default_presentation_info_
.reset();
191 void PresentationFrame::SetDefaultPresentationInfo(
192 const std::string
& default_presentation_url
,
193 const std::string
& default_presentation_id
) {
194 if (default_presentation_url
.empty() && default_presentation_id
.empty()) {
195 default_presentation_info_
.reset();
197 default_presentation_info_
.reset(new content::PresentationSessionInfo(
198 default_presentation_url
, default_presentation_id
));
202 std::string
PresentationFrame::GetDefaultPresentationId() const {
203 return default_presentation_info_
204 ? default_presentation_info_
->presentation_id
208 MediaSource
PresentationFrame::GetMediaSourceFromListener(
209 content::PresentationScreenAvailabilityListener
* listener
) const {
210 // If the default presentation URL is empty then fall back to tab mirroring.
211 std::string
presentation_url(listener
->GetPresentationUrl());
212 return presentation_url
.empty()
213 ? MediaSourceForTab(SessionTabHelper::IdForTab(web_contents_
))
214 : MediaSourceForPresentationUrl(presentation_url
);
217 // Used by PresentationServiceDelegateImpl to manage PresentationFrames.
218 class PresentationFrameManager
{
220 PresentationFrameManager(content::WebContents
* web_contents
,
221 MediaRouter
* router
);
222 ~PresentationFrameManager();
224 // Mirror corresponding APIs in PresentationServiceDelegateImpl.
225 bool SetScreenAvailabilityListener(
226 const RenderFrameHostId
& render_frame_host_id
,
227 content::PresentationScreenAvailabilityListener
* listener
);
228 bool RemoveScreenAvailabilityListener(
229 const RenderFrameHostId
& render_frame_host_id
,
230 content::PresentationScreenAvailabilityListener
* listener
);
231 void SetDefaultPresentationInfo(const RenderFrameHostId
& render_frame_host_id
,
232 const std::string
& default_presentation_url
,
233 const std::string
& default_presentation_id
);
234 void AddDelegateObserver(const RenderFrameHostId
& render_frame_host_id
,
235 DelegateObserver
* observer
);
236 void RemoveDelegateObserver(const RenderFrameHostId
& render_frame_host_id
);
237 void Reset(const RenderFrameHostId
& render_frame_host_id
);
238 bool HasScreenAvailabilityListenerForTest(
239 const RenderFrameHostId
& render_frame_host_id
,
240 const MediaSource::Id
& source_id
) const;
241 void SetMediaRouterForTest(MediaRouter
* router
);
243 // Returns default presentation ID, or empty string if no default
244 // presentation ID is set in the frame.
245 std::string
GetDefaultPresentationId(
246 const RenderFrameHostId
& render_frame_host_id
) const;
248 void OnPresentationSessionStarted(
249 const RenderFrameHostId
& render_frame_host_id
,
250 bool is_default_presentation
,
251 const content::PresentationSessionInfo
& session
,
252 const MediaRoute::Id
& route_id
);
254 void OnPresentationSessionClosed(
255 const RenderFrameHostId
& render_frame_host_id
,
256 const std::string
& presentation_id
);
257 const MediaRoute::Id
GetRouteId(const RenderFrameHostId
& render_frame_host_id
,
258 const std::string
& presentation_id
) const;
259 const std::vector
<MediaRoute::Id
> GetRouteIds(
260 const RenderFrameHostId
& render_frame_host_id
) const;
263 PresentationFrame
* GetOrAddPresentationFrame(
264 const RenderFrameHostId
& render_frame_host_id
);
266 // Maps a frame identifier to a PresentationFrame object for frames
267 // that are using presentation API.
268 base::ScopedPtrHashMap
<RenderFrameHostId
, scoped_ptr
<PresentationFrame
>>
269 presentation_frames_
;
271 // References to the owning WebContents, and the corresponding MediaRouter.
272 MediaRouter
* router_
;
273 content::WebContents
* web_contents_
;
276 PresentationFrameManager::PresentationFrameManager(
277 content::WebContents
* web_contents
,
279 : router_(router
), web_contents_(web_contents
) {
280 DCHECK(web_contents_
);
284 PresentationFrameManager::~PresentationFrameManager() {
285 for (auto& frame
: presentation_frames_
)
286 frame
.second
->OnPresentationServiceDelegateDestroyed();
289 void PresentationFrameManager::OnPresentationSessionStarted(
290 const RenderFrameHostId
& render_frame_host_id
,
291 bool is_default_presentation
,
292 const content::PresentationSessionInfo
& session
,
293 const MediaRoute::Id
& route_id
) {
294 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
295 if (presentation_frame
)
296 presentation_frame
->OnPresentationSessionStarted(is_default_presentation
,
300 void PresentationFrameManager::OnPresentationSessionClosed(
301 const RenderFrameHostId
& render_frame_host_id
,
302 const std::string
& presentation_id
) {
303 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
304 if (presentation_frame
)
305 presentation_frame
->OnPresentationSessionClosed(presentation_id
);
308 const MediaRoute::Id
PresentationFrameManager::GetRouteId(
309 const RenderFrameHostId
& render_frame_host_id
,
310 const std::string
& presentation_id
) const {
311 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
312 return presentation_frame
? presentation_frame
->GetRouteId(presentation_id
)
316 const std::vector
<MediaRoute::Id
> PresentationFrameManager::GetRouteIds(
317 const RenderFrameHostId
& render_frame_host_id
) const {
318 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
319 return presentation_frame
? presentation_frame
->GetRouteIds()
320 : std::vector
<MediaRoute::Id
>();
323 bool PresentationFrameManager::SetScreenAvailabilityListener(
324 const RenderFrameHostId
& render_frame_host_id
,
325 content::PresentationScreenAvailabilityListener
* listener
) {
327 auto presentation_frame
= GetOrAddPresentationFrame(render_frame_host_id
);
328 return presentation_frame
->SetScreenAvailabilityListener(listener
);
331 bool PresentationFrameManager::RemoveScreenAvailabilityListener(
332 const RenderFrameHostId
& render_frame_host_id
,
333 content::PresentationScreenAvailabilityListener
* listener
) {
335 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
336 return presentation_frame
&&
337 presentation_frame
->RemoveScreenAvailabilityListener(listener
);
340 bool PresentationFrameManager::HasScreenAvailabilityListenerForTest(
341 const RenderFrameHostId
& render_frame_host_id
,
342 const MediaSource::Id
& source_id
) const {
343 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
344 return presentation_frame
&&
345 presentation_frame
->HasScreenAvailabilityListenerForTest(source_id
);
348 void PresentationFrameManager::SetDefaultPresentationInfo(
349 const RenderFrameHostId
& render_frame_host_id
,
350 const std::string
& default_presentation_url
,
351 const std::string
& default_presentation_id
) {
352 auto presentation_frame
= GetOrAddPresentationFrame(render_frame_host_id
);
353 presentation_frame
->SetDefaultPresentationInfo(default_presentation_url
,
354 default_presentation_id
);
357 std::string
PresentationFrameManager::GetDefaultPresentationId(
358 const RenderFrameHostId
& render_frame_host_id
) const {
359 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
360 return presentation_frame
? presentation_frame
->GetDefaultPresentationId()
364 void PresentationFrameManager::AddDelegateObserver(
365 const RenderFrameHostId
& render_frame_host_id
,
366 DelegateObserver
* observer
) {
367 auto presentation_frame
= GetOrAddPresentationFrame(render_frame_host_id
);
368 presentation_frame
->set_delegate_observer(observer
);
371 void PresentationFrameManager::RemoveDelegateObserver(
372 const RenderFrameHostId
& render_frame_host_id
) {
373 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
374 if (presentation_frame
) {
375 presentation_frame
->set_delegate_observer(nullptr);
376 presentation_frames_
.erase(render_frame_host_id
);
380 void PresentationFrameManager::Reset(
381 const RenderFrameHostId
& render_frame_host_id
) {
382 auto presentation_frame
= presentation_frames_
.get(render_frame_host_id
);
383 if (presentation_frame
)
384 presentation_frame
->Reset();
387 PresentationFrame
* PresentationFrameManager::GetOrAddPresentationFrame(
388 const RenderFrameHostId
& render_frame_host_id
) {
389 if (!presentation_frames_
.contains(render_frame_host_id
)) {
390 presentation_frames_
.add(
391 render_frame_host_id
,
392 scoped_ptr
<PresentationFrame
>(
393 new PresentationFrame(web_contents_
, router_
)));
395 return presentation_frames_
.get(render_frame_host_id
);
398 void PresentationFrameManager::SetMediaRouterForTest(MediaRouter
* router
) {
402 PresentationServiceDelegateImpl
*
403 PresentationServiceDelegateImpl::GetOrCreateForWebContents(
404 content::WebContents
* web_contents
) {
405 DCHECK(web_contents
);
406 // CreateForWebContents does nothing if the delegate instance already exists.
407 PresentationServiceDelegateImpl::CreateForWebContents(web_contents
);
408 return PresentationServiceDelegateImpl::FromWebContents(web_contents
);
411 PresentationServiceDelegateImpl::PresentationServiceDelegateImpl(
412 content::WebContents
* web_contents
)
413 : web_contents_(web_contents
),
414 router_(MediaRouterMojoImplFactory::GetApiForBrowserContext(
415 web_contents_
->GetBrowserContext())),
416 frame_manager_(new PresentationFrameManager(web_contents
, router_
)),
417 weak_factory_(this) {
418 DCHECK(web_contents_
);
422 PresentationServiceDelegateImpl::~PresentationServiceDelegateImpl() {
425 void PresentationServiceDelegateImpl::AddObserver(int render_process_id
,
427 DelegateObserver
* observer
) {
429 frame_manager_
->AddDelegateObserver(
430 RenderFrameHostId(render_process_id
, render_frame_id
), observer
);
433 void PresentationServiceDelegateImpl::RemoveObserver(int render_process_id
,
434 int render_frame_id
) {
435 frame_manager_
->RemoveDelegateObserver(
436 RenderFrameHostId(render_process_id
, render_frame_id
));
439 bool PresentationServiceDelegateImpl::AddScreenAvailabilityListener(
440 int render_process_id
,
442 content::PresentationScreenAvailabilityListener
* listener
) {
444 return frame_manager_
->SetScreenAvailabilityListener(
445 RenderFrameHostId(render_process_id
, render_frame_id
), listener
);
448 void PresentationServiceDelegateImpl::RemoveScreenAvailabilityListener(
449 int render_process_id
,
451 content::PresentationScreenAvailabilityListener
* listener
) {
453 frame_manager_
->RemoveScreenAvailabilityListener(
454 RenderFrameHostId(render_process_id
, render_frame_id
), listener
);
457 void PresentationServiceDelegateImpl::Reset(int render_process_id
,
458 int render_frame_id
) {
459 RenderFrameHostId
render_frame_host_id(render_process_id
, render_frame_id
);
460 frame_manager_
->Reset(render_frame_host_id
);
461 if (IsMainFrame(render_process_id
, render_frame_id
))
462 UpdateDefaultMediaSourceAndNotifyObservers(MediaSource(), GURL());
465 void PresentationServiceDelegateImpl::SetDefaultPresentationUrl(
466 int render_process_id
,
468 const std::string
& default_presentation_url
,
469 const std::string
& default_presentation_id
) {
470 RenderFrameHostId
render_frame_host_id(render_process_id
, render_frame_id
);
471 frame_manager_
->SetDefaultPresentationInfo(
472 render_frame_host_id
, default_presentation_url
, default_presentation_id
);
473 if (IsMainFrame(render_process_id
, render_frame_id
)) {
474 // This is the main frame, which means tab-level default presentation
475 // might have been updated.
476 MediaSource default_source
;
477 if (!default_presentation_url
.empty())
478 default_source
= MediaSourceForPresentationUrl(default_presentation_url
);
480 GURL default_frame_url
= GetLastCommittedURLForFrame(render_frame_host_id
);
481 UpdateDefaultMediaSourceAndNotifyObservers(default_source
,
486 bool PresentationServiceDelegateImpl::IsMainFrame(int render_process_id
,
487 int render_frame_id
) const {
488 RenderFrameHost
* main_frame
= web_contents_
->GetMainFrame();
490 GetRenderFrameHostId(main_frame
) ==
491 RenderFrameHostId(render_process_id
, render_frame_id
);
494 void PresentationServiceDelegateImpl::
495 UpdateDefaultMediaSourceAndNotifyObservers(
496 const MediaSource
& new_default_source
,
497 const GURL
& new_default_frame_url
) {
498 if (!new_default_source
.Equals(default_source_
) ||
499 new_default_frame_url
!= default_frame_url_
) {
500 default_source_
= new_default_source
;
501 default_frame_url_
= new_default_frame_url
;
503 DefaultMediaSourceObserver
, default_media_source_observers_
,
504 OnDefaultMediaSourceChanged(default_source_
, default_frame_url_
));
508 void PresentationServiceDelegateImpl::OnJoinRouteResponse(
509 int render_process_id
,
511 const content::PresentationSessionInfo
& session
,
512 const PresentationSessionSuccessCallback
& success_cb
,
513 const PresentationSessionErrorCallback
& error_cb
,
514 scoped_ptr
<MediaRoute
> route
,
515 const std::string
& error_text
) {
517 error_cb
.Run(content::PresentationError(
518 content::PRESENTATION_ERROR_NO_PRESENTATION_FOUND
, error_text
));
520 DVLOG(1) << "OnJoinRouteResponse: "
521 << "route_id: " << route
->media_route_id()
522 << ", presentation URL: " << session
.presentation_url
523 << ", presentation ID: " << session
.presentation_id
;
524 frame_manager_
->OnPresentationSessionStarted(
525 RenderFrameHostId(render_process_id
, render_frame_id
), false, session
,
526 route
->media_route_id());
527 success_cb
.Run(session
);
531 void PresentationServiceDelegateImpl::OnStartSessionSucceeded(
532 int render_process_id
,
534 const PresentationSessionSuccessCallback
& success_cb
,
535 const content::PresentationSessionInfo
& new_session
,
536 const MediaRoute::Id
& route_id
) {
537 DVLOG(1) << "OnStartSessionSucceeded: "
538 << "route_id: " << route_id
539 << ", presentation URL: " << new_session
.presentation_url
540 << ", presentation ID: " << new_session
.presentation_id
;
541 frame_manager_
->OnPresentationSessionStarted(
542 RenderFrameHostId(render_process_id
, render_frame_id
), false, new_session
,
544 success_cb
.Run(new_session
);
547 void PresentationServiceDelegateImpl::StartSession(
548 int render_process_id
,
550 const std::string
& presentation_url
,
551 const std::string
& presentation_id
,
552 const PresentationSessionSuccessCallback
& success_cb
,
553 const PresentationSessionErrorCallback
& error_cb
) {
554 if (presentation_url
.empty() || !IsValidPresentationUrl(presentation_url
)) {
555 error_cb
.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN
,
556 "Invalid presentation arguments."));
559 RenderFrameHostId
render_frame_host_id(render_process_id
, render_frame_id
);
560 std::string final_presentation_id
=
561 presentation_id
.empty()
562 ? frame_manager_
->GetDefaultPresentationId(render_frame_host_id
)
564 if (final_presentation_id
.empty())
565 // TODO(imcheng): Remove presentation_id argument entirely if required
566 // by Presentation API spec.
567 final_presentation_id
= base::GenerateGUID();
568 scoped_ptr
<CreateSessionRequest
> context(new CreateSessionRequest(
569 presentation_url
, final_presentation_id
,
570 GetLastCommittedURLForFrame(render_frame_host_id
),
571 base::Bind(&PresentationServiceDelegateImpl::OnStartSessionSucceeded
,
572 weak_factory_
.GetWeakPtr(), render_process_id
, render_frame_id
,
575 // NOTE: Currently this request is ignored if a dialog is already open, e.g.
576 // via browser action. In practice, this should rarely happen, but log
577 // an error message in case it does.
578 MediaRouterDialogController::CreateForWebContents(web_contents_
);
579 MediaRouterDialogController
* controller
=
580 MediaRouterDialogController::FromWebContents(web_contents_
);
582 if (!controller
->ShowMediaRouterDialogForPresentation(context
.Pass())) {
583 LOG(ERROR
) << "Media router dialog already exists. Ignoring StartSession.";
584 error_cb
.Run(content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN
,
585 "Unable to create dialog."));
590 void PresentationServiceDelegateImpl::JoinSession(
591 int render_process_id
,
593 const std::string
& presentation_url
,
594 const std::string
& presentation_id
,
595 const PresentationSessionSuccessCallback
& success_cb
,
596 const PresentationSessionErrorCallback
& error_cb
) {
598 MediaSourceForPresentationUrl(presentation_url
).id(), presentation_id
,
599 GetLastCommittedURLForFrame(
600 RenderFrameHostId(render_process_id
, render_frame_id
)).GetOrigin(),
601 SessionTabHelper::IdForTab(web_contents_
),
603 &PresentationServiceDelegateImpl::OnJoinRouteResponse
,
604 weak_factory_
.GetWeakPtr(), render_process_id
, render_frame_id
,
605 content::PresentationSessionInfo(presentation_url
, presentation_id
),
606 success_cb
, error_cb
));
609 void PresentationServiceDelegateImpl::ListenForSessionMessages(
610 int render_process_id
,
612 const PresentationSessionMessageCallback
& message_cb
) {
617 void PresentationServiceDelegateImpl::SendMessage(
618 int render_process_id
,
620 scoped_ptr
<content::PresentationSessionMessage
> message_request
,
621 const SendMessageCallback
& send_message_cb
) {
626 void PresentationServiceDelegateImpl::OnRouteCreated(const MediaRoute
& route
) {
627 const MediaSource
& source
= route
.media_source();
628 DCHECK(!source
.Empty());
629 if (!default_source_
.Equals(source
))
631 RenderFrameHost
* main_frame
= web_contents_
->GetMainFrame();
634 RenderFrameHostId
render_frame_host_id(GetRenderFrameHostId(main_frame
));
635 // TODO(imcheng): Pass in valid default presentation ID once it is
636 // available from MediaRoute URN. BUG=493365
637 std::string presentation_id
;
638 frame_manager_
->OnPresentationSessionStarted(
639 render_frame_host_id
, true,
640 content::PresentationSessionInfo(PresentationUrlFromMediaSource(source
),
642 route
.media_route_id());
645 void PresentationServiceDelegateImpl::AddDefaultMediaSourceObserver(
646 DefaultMediaSourceObserver
* observer
) {
647 default_media_source_observers_
.AddObserver(observer
);
650 void PresentationServiceDelegateImpl::RemoveDefaultMediaSourceObserver(
651 DefaultMediaSourceObserver
* observer
) {
652 default_media_source_observers_
.RemoveObserver(observer
);
655 void PresentationServiceDelegateImpl::SetMediaRouterForTest(
656 MediaRouter
* router
) {
658 frame_manager_
->SetMediaRouterForTest(router
);
661 base::WeakPtr
<PresentationServiceDelegateImpl
>
662 PresentationServiceDelegateImpl::GetWeakPtr() {
663 return weak_factory_
.GetWeakPtr();
666 bool PresentationServiceDelegateImpl::HasScreenAvailabilityListenerForTest(
667 int render_process_id
,
669 const MediaSource::Id
& source_id
) const {
670 RenderFrameHostId
render_frame_host_id(render_process_id
, render_frame_id
);
671 return frame_manager_
->HasScreenAvailabilityListenerForTest(
672 render_frame_host_id
, source_id
);
675 } // namespace media_router