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/media_router_dialog_controller.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/browser/web_contents_delegate.h"
11 #if defined(OS_ANDROID)
12 #include "chrome/browser/media/android/router/media_router_dialog_controller_android.h"
14 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_impl.h"
17 namespace media_router
{
20 MediaRouterDialogController
*
21 MediaRouterDialogController::GetOrCreateForWebContents(
22 content::WebContents
* contents
) {
23 #if defined(OS_ANDROID)
24 return MediaRouterDialogControllerAndroid::GetOrCreateForWebContents(
27 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(contents
);
31 class MediaRouterDialogController::InitiatorWebContentsObserver
32 : public content::WebContentsObserver
{
34 InitiatorWebContentsObserver(
35 content::WebContents
* web_contents
,
36 MediaRouterDialogController
* dialog_controller
)
37 : content::WebContentsObserver(web_contents
),
38 dialog_controller_(dialog_controller
) {
39 DCHECK(dialog_controller_
);
43 void WebContentsDestroyed() override
{
44 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
45 dialog_controller_
->CloseMediaRouterDialog();
48 void NavigationEntryCommitted(
49 const content::LoadCommittedDetails
& load_details
) override
{
50 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
51 dialog_controller_
->CloseMediaRouterDialog();
54 void RenderProcessGone(base::TerminationStatus status
) override
{
55 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns.
56 dialog_controller_
->CloseMediaRouterDialog();
59 MediaRouterDialogController
* const dialog_controller_
;
62 MediaRouterDialogController::MediaRouterDialogController(
63 content::WebContents
* initiator
)
64 : initiator_(initiator
) {
65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
67 initiator_observer_
.reset(new InitiatorWebContentsObserver(initiator_
, this));
70 MediaRouterDialogController::~MediaRouterDialogController() {
71 DCHECK(thread_checker_
.CalledOnValidThread());
74 bool MediaRouterDialogController::ShowMediaRouterDialogForPresentation(
75 scoped_ptr
<CreatePresentationSessionRequest
> request
) {
76 DCHECK(thread_checker_
.CalledOnValidThread());
78 // Check if the media router dialog exists for |initiator| and return if so.
79 if (IsShowingMediaRouterDialog())
82 presentation_request_
= request
.Pass();
83 CreateMediaRouterDialog();
85 // Show the initiator holding the existing media router dialog.
86 ActivateInitiatorWebContents();
91 bool MediaRouterDialogController::ShowMediaRouterDialog() {
92 DCHECK(thread_checker_
.CalledOnValidThread());
94 // Don't create dialog if it already exists.
95 bool dialog_needs_creation
= !IsShowingMediaRouterDialog();
96 if (dialog_needs_creation
)
97 CreateMediaRouterDialog();
99 ActivateInitiatorWebContents();
100 return dialog_needs_creation
;
103 void MediaRouterDialogController::HideMediaRouterDialog() {
104 DCHECK(thread_checker_
.CalledOnValidThread());
105 CloseMediaRouterDialog();
109 void MediaRouterDialogController::ActivateInitiatorWebContents() {
110 initiator_
->GetDelegate()->ActivateContents(initiator_
);
113 scoped_ptr
<CreatePresentationSessionRequest
>
114 MediaRouterDialogController::TakePresentationRequest() {
115 return presentation_request_
.Pass();
118 void MediaRouterDialogController::Reset() {
119 initiator_observer_
.reset();
120 presentation_request_
.reset();
123 } // namespace media_router