OLD | NEW |
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/media_router_dialog_controller.h" | 5 #include "chrome/browser/media/router/media_router_dialog_controller.h" |
6 | 6 |
| 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" |
| 10 |
7 #if defined(OS_ANDROID) | 11 #if defined(OS_ANDROID) |
8 #include "chrome/browser/media/android/router/media_router_dialog_controller_and
roid.h" | 12 #include "chrome/browser/media/android/router/media_router_dialog_controller_and
roid.h" |
9 #else | 13 #else |
10 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" | 14 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" |
11 #endif | 15 #endif |
12 | 16 |
13 namespace media_router { | 17 namespace media_router { |
14 | 18 |
15 // static | 19 // static |
16 MediaRouterDialogController* | 20 MediaRouterDialogController* |
17 MediaRouterDialogController::GetOrCreateForWebContents( | 21 MediaRouterDialogController::GetOrCreateForWebContents( |
18 content::WebContents* contents) { | 22 content::WebContents* contents) { |
19 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
20 return MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( | 24 return MediaRouterDialogControllerAndroid::GetOrCreateForWebContents( |
21 contents); | 25 contents); |
22 #else | 26 #else |
23 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(contents); | 27 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents(contents); |
24 #endif | 28 #endif |
25 } | 29 } |
26 | 30 |
| 31 class MediaRouterDialogController::InitiatorWebContentsObserver |
| 32 : public content::WebContentsObserver { |
| 33 public: |
| 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_); |
| 40 } |
| 41 |
| 42 private: |
| 43 void WebContentsDestroyed() override { |
| 44 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 45 dialog_controller_->CloseMediaRouterDialog(); |
| 46 } |
| 47 |
| 48 void NavigationEntryCommitted( |
| 49 const content::LoadCommittedDetails& load_details) override { |
| 50 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 51 dialog_controller_->CloseMediaRouterDialog(); |
| 52 } |
| 53 |
| 54 void RenderProcessGone(base::TerminationStatus status) override { |
| 55 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 56 dialog_controller_->CloseMediaRouterDialog(); |
| 57 } |
| 58 |
| 59 MediaRouterDialogController* const dialog_controller_; |
| 60 }; |
| 61 |
| 62 MediaRouterDialogController::MediaRouterDialogController( |
| 63 content::WebContents* initiator) |
| 64 : initiator_(initiator) { |
| 65 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 66 DCHECK(initiator_); |
| 67 initiator_observer_.reset(new InitiatorWebContentsObserver(initiator_, this)); |
| 68 } |
| 69 |
| 70 MediaRouterDialogController::~MediaRouterDialogController() { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 } |
| 73 |
| 74 bool MediaRouterDialogController::ShowMediaRouterDialogForPresentation( |
| 75 scoped_ptr<CreatePresentationSessionRequest> request) { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 |
| 78 // Check if the media router dialog exists for |initiator| and return if so. |
| 79 if (IsShowingMediaRouterDialog()) |
| 80 return false; |
| 81 |
| 82 presentation_request_ = request.Pass(); |
| 83 CreateMediaRouterDialog(); |
| 84 |
| 85 // Show the initiator holding the existing media router dialog. |
| 86 ActivateInitiatorWebContents(); |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool MediaRouterDialogController::ShowMediaRouterDialog() { |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 |
| 94 // Don't create dialog if it already exists. |
| 95 bool dialog_needs_creation = !IsShowingMediaRouterDialog(); |
| 96 if (dialog_needs_creation) |
| 97 CreateMediaRouterDialog(); |
| 98 |
| 99 ActivateInitiatorWebContents(); |
| 100 return dialog_needs_creation; |
| 101 } |
| 102 |
| 103 void MediaRouterDialogController::HideMediaRouterDialog() { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 CloseMediaRouterDialog(); |
| 106 Reset(); |
| 107 } |
| 108 |
| 109 void MediaRouterDialogController::ActivateInitiatorWebContents() { |
| 110 initiator_->GetDelegate()->ActivateContents(initiator_); |
| 111 } |
| 112 |
| 113 scoped_ptr<CreatePresentationSessionRequest> |
| 114 MediaRouterDialogController::PassPresentationRequest() { |
| 115 return presentation_request_.Pass(); |
| 116 } |
| 117 |
| 118 void MediaRouterDialogController::Reset() { |
| 119 initiator_observer_.reset(); |
| 120 presentation_request_.reset(); |
| 121 } |
| 122 |
27 } // namespace media_router | 123 } // namespace media_router |
OLD | NEW |