Chromium Code Reviews| 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 } | |
| 68 | |
| 69 MediaRouterDialogController::~MediaRouterDialogController() { | |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 71 } | |
| 72 | |
| 73 void MediaRouterDialogController::ActivateInitiatorWebContents() { | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 75 initiator_->GetDelegate()->ActivateContents(initiator_); | |
| 76 } | |
| 77 | |
| 78 bool MediaRouterDialogController::ShowMediaRouterDialogForPresentation( | |
| 79 scoped_ptr<CreatePresentationSessionRequest> request) { | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 | |
| 82 // Check if the media router dialog exists for |initiator| and return if so. | |
| 83 if (IsShowingMediaRouterDialog()) | |
| 84 return false; | |
| 85 | |
| 86 TakePresentationRequest(request.Pass()); | |
| 87 CreateMediaRouterDialog(); | |
| 88 | |
| 89 // Show the initiator holding the existing media router dialog. | |
| 90 ActivateInitiatorWebContents(); | |
| 91 | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 void MediaRouterDialogController::ShowMediaRouterDialog() { | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 // Create a new dialog if it doesn't exist yet. | |
| 98 if (!IsShowingMediaRouterDialog()) | |
| 99 CreateMediaRouterDialog(); | |
| 100 | |
| 101 // Show the initiator holding the existing media router dialog. | |
| 102 ActivateInitiatorWebContents(); | |
| 103 } | |
| 104 | |
| 105 void MediaRouterDialogController::CloseMediaRouterDialog() { | |
| 106 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 107 DCHECK(initiator_observer_.get()); | |
|
mark a. foltz
2015/07/24 20:10:34
Why does the state of the observer matter for clos
whywhat
2015/07/28 18:52:53
I merely moved the code that was already there. I
| |
| 108 Reset(); | |
| 109 } | |
| 110 | |
| 111 void MediaRouterDialogController::ObserveInitiatorWebContents() { | |
|
mark a. foltz
2015/07/24 20:10:34
Shouldn't this be called when the dialog is create
whywhat
2015/07/28 18:52:53
My guess that it was done to avoid redundant Close
| |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
mark a. foltz
2015/07/24 20:10:34
Generally I don't bother DCHECKING on non-public m
whywhat
2015/07/28 18:52:53
Done.
| |
| 113 DCHECK(!initiator_observer_.get()); | |
| 114 initiator_observer_.reset(new InitiatorWebContentsObserver( | |
| 115 initiator_, this)); | |
| 116 } | |
| 117 | |
| 118 void MediaRouterDialogController::TakePresentationRequest( | |
|
mark a. foltz
2015/07/24 20:10:34
I think this method can be inlined. It's a one-li
whywhat
2015/07/28 18:52:53
Done.
| |
| 119 scoped_ptr<CreatePresentationSessionRequest> request) { | |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 121 presentation_request_ = request.Pass(); | |
| 122 } | |
| 123 | |
| 124 scoped_ptr<CreatePresentationSessionRequest> | |
| 125 MediaRouterDialogController::PassPresentationRequest() { | |
|
mark a. foltz
2015/07/24 20:10:34
This is a one-liner called once - consider inlinin
whywhat
2015/07/28 18:52:53
I think this will be used by the Android implement
| |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 127 return presentation_request_.Pass(); | |
| 128 } | |
| 129 | |
| 130 void MediaRouterDialogController::Reset() { | |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 132 DCHECK(initiator_observer_.get()); | |
| 133 initiator_observer_.reset(); | |
| 134 presentation_request_.reset(); | |
| 135 } | |
| 136 | |
| 27 } // namespace media_router | 137 } // namespace media_router |
| OLD | NEW |