| 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/ui/webui/media_router/media_router_dialog_controller.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller_im
pl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | 11 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
| 12 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | 12 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 14 #include "components/web_modal/web_contents_modal_dialog_host.h" | 14 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_contents_delegate.h" | 23 #include "content/public/browser/web_contents_delegate.h" |
| 24 #include "ui/web_dialogs/web_dialog_delegate.h" | 24 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 25 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | 25 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(media_router::MediaRouterDialogController); | 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 29 media_router::MediaRouterDialogControllerImpl); |
| 29 | 30 |
| 30 using content::LoadCommittedDetails; | 31 using content::LoadCommittedDetails; |
| 31 using content::NavigationController; | 32 using content::NavigationController; |
| 32 using content::WebContents; | 33 using content::WebContents; |
| 33 using content::WebUIMessageHandler; | 34 using content::WebUIMessageHandler; |
| 34 using ui::WebDialogDelegate; | 35 using ui::WebDialogDelegate; |
| 35 | 36 |
| 36 namespace media_router { | 37 namespace media_router { |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 *size = gfx::Size(); | 98 *size = gfx::Size(); |
| 98 #endif | 99 #endif |
| 99 } | 100 } |
| 100 | 101 |
| 101 } // namespace | 102 } // namespace |
| 102 | 103 |
| 103 // static | 104 // static |
| 104 MediaRouterDialogController* | 105 MediaRouterDialogController* |
| 105 MediaRouterDialogController::GetOrCreateForWebContents( | 106 MediaRouterDialogController::GetOrCreateForWebContents( |
| 106 WebContents* web_contents) { | 107 WebContents* web_contents) { |
| 108 return MediaRouterDialogControllerImpl::GetOrCreateForWebContents( |
| 109 web_contents); |
| 110 } |
| 111 |
| 112 // static |
| 113 MediaRouterDialogControllerImpl* |
| 114 MediaRouterDialogControllerImpl::GetOrCreateForWebContents( |
| 115 WebContents* web_contents) { |
| 107 DCHECK(web_contents); | 116 DCHECK(web_contents); |
| 108 // This call does nothing if the controller already exists. | 117 // This call does nothing if the controller already exists. |
| 109 MediaRouterDialogController::CreateForWebContents(web_contents); | 118 MediaRouterDialogControllerImpl::CreateForWebContents(web_contents); |
| 110 return MediaRouterDialogController::FromWebContents(web_contents); | 119 return MediaRouterDialogControllerImpl::FromWebContents(web_contents); |
| 111 } | 120 } |
| 112 | 121 |
| 113 class MediaRouterDialogController::DialogWebContentsObserver | 122 class MediaRouterDialogControllerImpl::DialogWebContentsObserver |
| 114 : public content::WebContentsObserver { | 123 : public content::WebContentsObserver { |
| 115 public: | 124 public: |
| 116 DialogWebContentsObserver( | 125 DialogWebContentsObserver( |
| 117 WebContents* web_contents, | 126 WebContents* web_contents, |
| 118 MediaRouterDialogController* dialog_controller) | 127 MediaRouterDialogControllerImpl* dialog_controller) |
| 119 : content::WebContentsObserver(web_contents), | 128 : content::WebContentsObserver(web_contents), |
| 120 dialog_controller_(dialog_controller) { | 129 dialog_controller_(dialog_controller) { |
| 121 } | 130 } |
| 122 | 131 |
| 123 private: | 132 private: |
| 124 void WebContentsDestroyed() override { | 133 void WebContentsDestroyed() override { |
| 125 // The dialog is already closed. No need to call Close() again. | 134 // The dialog is already closed. No need to call Close() again. |
| 126 // NOTE: |this| is deleted after Reset() returns. | 135 // NOTE: |this| is deleted after Reset() returns. |
| 127 dialog_controller_->Reset(); | 136 dialog_controller_->Reset(); |
| 128 } | 137 } |
| 129 | 138 |
| 130 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) | 139 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) |
| 131 override { | 140 override { |
| 132 dialog_controller_->OnDialogNavigated(load_details); | 141 dialog_controller_->OnDialogNavigated(load_details); |
| 133 } | 142 } |
| 134 | 143 |
| 135 void RenderProcessGone(base::TerminationStatus status) override { | 144 void RenderProcessGone(base::TerminationStatus status) override { |
| 136 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 145 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 137 dialog_controller_->CloseMediaRouterDialog(); | 146 dialog_controller_->CloseMediaRouterDialog(); |
| 138 } | 147 } |
| 139 | 148 |
| 140 MediaRouterDialogController* const dialog_controller_; | 149 MediaRouterDialogControllerImpl* const dialog_controller_; |
| 141 }; | 150 }; |
| 142 | 151 |
| 143 class MediaRouterDialogController::InitiatorWebContentsObserver | 152 class MediaRouterDialogControllerImpl::InitiatorWebContentsObserver |
| 144 : public content::WebContentsObserver { | 153 : public content::WebContentsObserver { |
| 145 public: | 154 public: |
| 146 InitiatorWebContentsObserver( | 155 InitiatorWebContentsObserver( |
| 147 WebContents* web_contents, | 156 WebContents* web_contents, |
| 148 MediaRouterDialogController* dialog_controller) | 157 MediaRouterDialogControllerImpl* dialog_controller) |
| 149 : content::WebContentsObserver(web_contents), | 158 : content::WebContentsObserver(web_contents), |
| 150 dialog_controller_(dialog_controller) { | 159 dialog_controller_(dialog_controller) { |
| 151 } | 160 } |
| 152 | 161 |
| 153 private: | 162 private: |
| 154 void WebContentsDestroyed() override { | 163 void WebContentsDestroyed() override { |
| 155 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 164 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 156 dialog_controller_->CloseMediaRouterDialog(); | 165 dialog_controller_->CloseMediaRouterDialog(); |
| 157 } | 166 } |
| 158 | 167 |
| 159 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) | 168 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) |
| 160 override { | 169 override { |
| 161 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 170 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 162 dialog_controller_->CloseMediaRouterDialog(); | 171 dialog_controller_->CloseMediaRouterDialog(); |
| 163 } | 172 } |
| 164 | 173 |
| 165 void RenderProcessGone(base::TerminationStatus status) override { | 174 void RenderProcessGone(base::TerminationStatus status) override { |
| 166 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 175 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 167 dialog_controller_->CloseMediaRouterDialog(); | 176 dialog_controller_->CloseMediaRouterDialog(); |
| 168 } | 177 } |
| 169 | 178 |
| 170 MediaRouterDialogController* const dialog_controller_; | 179 MediaRouterDialogControllerImpl* const dialog_controller_; |
| 171 }; | 180 }; |
| 172 | 181 |
| 173 MediaRouterDialogController::MediaRouterDialogController( | 182 MediaRouterDialogControllerImpl::MediaRouterDialogControllerImpl( |
| 174 WebContents* web_contents) | 183 WebContents* web_contents) |
| 175 : initiator_(web_contents), | 184 : initiator_(web_contents), |
| 176 media_router_dialog_pending_(false) { | 185 media_router_dialog_pending_(false) { |
| 177 DCHECK(initiator_); | 186 DCHECK(initiator_); |
| 178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 187 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 179 } | 188 } |
| 180 | 189 |
| 181 MediaRouterDialogController::~MediaRouterDialogController() { | 190 MediaRouterDialogControllerImpl::~MediaRouterDialogControllerImpl() { |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 183 } | 192 } |
| 184 | 193 |
| 185 WebContents* MediaRouterDialogController::ShowMediaRouterDialog() { | 194 WebContents* MediaRouterDialogControllerImpl::ShowMediaRouterDialog() { |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); | 195 DCHECK(thread_checker_.CalledOnValidThread()); |
| 187 | 196 |
| 188 // Get the media router dialog for |initiator|, or create a new dialog | 197 // Get the media router dialog for |initiator|, or create a new dialog |
| 189 // if not found. | 198 // if not found. |
| 190 WebContents* media_router_dialog = GetMediaRouterDialog(); | 199 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 191 if (!media_router_dialog) { | 200 if (!media_router_dialog) { |
| 192 CreateMediaRouterDialog(); | 201 CreateMediaRouterDialog(); |
| 193 return GetMediaRouterDialog(); | 202 return GetMediaRouterDialog(); |
| 194 } | 203 } |
| 195 | 204 |
| 196 // Show the initiator holding the existing media router dialog. | 205 // Show the initiator holding the existing media router dialog. |
| 197 initiator_->GetDelegate()->ActivateContents(initiator_); | 206 initiator_->GetDelegate()->ActivateContents(initiator_); |
| 198 return media_router_dialog; | 207 return media_router_dialog; |
| 199 } | 208 } |
| 200 | 209 |
| 201 WebContents* MediaRouterDialogController::ShowMediaRouterDialogForPresentation( | 210 bool MediaRouterDialogControllerImpl::ShowMediaRouterDialogForPresentation( |
| 202 scoped_ptr<CreateSessionRequest> request) { | 211 scoped_ptr<CreateSessionRequest> request) { |
| 203 DCHECK(thread_checker_.CalledOnValidThread()); | 212 DCHECK(thread_checker_.CalledOnValidThread()); |
| 204 | 213 |
| 205 // Get the media router dialog for |initiator|, or create a new dialog | 214 // Get the media router dialog for |initiator|, or create a new dialog |
| 206 // if not found. | 215 // if not found. |
| 207 WebContents* media_router_dialog = GetMediaRouterDialog(); | 216 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 208 if (!media_router_dialog) { | 217 if (!media_router_dialog) { |
| 209 CreateMediaRouterDialog(); | 218 CreateMediaRouterDialog(); |
| 210 media_router_dialog = GetMediaRouterDialog(); | 219 media_router_dialog = GetMediaRouterDialog(); |
| 211 presentation_request_ = request.Pass(); | 220 presentation_request_ = request.Pass(); |
| 212 return media_router_dialog; | 221 return true; |
| 213 } | 222 } |
| 214 | 223 |
| 215 // Show the initiator holding the existing media router dialog. | 224 // Show the initiator holding the existing media router dialog. |
| 216 initiator_->GetDelegate()->ActivateContents(initiator_); | 225 initiator_->GetDelegate()->ActivateContents(initiator_); |
| 217 return nullptr; | 226 return false; |
| 218 } | 227 } |
| 219 | 228 |
| 220 WebContents* MediaRouterDialogController::GetMediaRouterDialog() const { | 229 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
| 222 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; | 231 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; |
| 223 } | 232 } |
| 224 | 233 |
| 225 void MediaRouterDialogController::CloseMediaRouterDialog() { | 234 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { |
| 226 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
| 227 DCHECK(initiator_observer_.get()); | 236 DCHECK(initiator_observer_.get()); |
| 228 WebContents* media_router_dialog = GetMediaRouterDialog(); | 237 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 229 CHECK(media_router_dialog); | 238 CHECK(media_router_dialog); |
| 230 Reset(); | 239 Reset(); |
| 231 | 240 |
| 232 content::WebUI* web_ui = media_router_dialog->GetWebUI(); | 241 content::WebUI* web_ui = media_router_dialog->GetWebUI(); |
| 233 if (web_ui) { | 242 if (web_ui) { |
| 234 MediaRouterUI* media_router_ui = | 243 MediaRouterUI* media_router_ui = |
| 235 static_cast<MediaRouterUI*>(web_ui->GetController()); | 244 static_cast<MediaRouterUI*>(web_ui->GetController()); |
| 236 if (media_router_ui) | 245 if (media_router_ui) |
| 237 media_router_ui->Close(); | 246 media_router_ui->Close(); |
| 238 } | 247 } |
| 239 } | 248 } |
| 240 | 249 |
| 241 void MediaRouterDialogController::CreateMediaRouterDialog() { | 250 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { |
| 242 DCHECK(!initiator_observer_.get()); | 251 DCHECK(!initiator_observer_.get()); |
| 243 DCHECK(!dialog_observer_.get()); | 252 DCHECK(!dialog_observer_.get()); |
| 244 | 253 |
| 245 Profile* profile = | 254 Profile* profile = |
| 246 Profile::FromBrowserContext(initiator_->GetBrowserContext()); | 255 Profile::FromBrowserContext(initiator_->GetBrowserContext()); |
| 247 DCHECK(profile); | 256 DCHECK(profile); |
| 248 | 257 |
| 249 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; | 258 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; |
| 250 | 259 |
| 251 // |web_dialog_delegate|'s owner is |constrained_delegate|. | 260 // |web_dialog_delegate|'s owner is |constrained_delegate|. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 272 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); | 281 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); |
| 273 | 282 |
| 274 media_router_dialog_pending_ = true; | 283 media_router_dialog_pending_ = true; |
| 275 | 284 |
| 276 initiator_observer_.reset(new InitiatorWebContentsObserver( | 285 initiator_observer_.reset(new InitiatorWebContentsObserver( |
| 277 initiator_, this)); | 286 initiator_, this)); |
| 278 dialog_observer_.reset(new DialogWebContentsObserver( | 287 dialog_observer_.reset(new DialogWebContentsObserver( |
| 279 media_router_dialog, this)); | 288 media_router_dialog, this)); |
| 280 } | 289 } |
| 281 | 290 |
| 282 void MediaRouterDialogController::Reset() { | 291 void MediaRouterDialogControllerImpl::Reset() { |
| 283 DCHECK(thread_checker_.CalledOnValidThread()); | 292 DCHECK(thread_checker_.CalledOnValidThread()); |
| 284 DCHECK(initiator_observer_.get()); | 293 DCHECK(initiator_observer_.get()); |
| 285 DCHECK(dialog_observer_.get()); | 294 DCHECK(dialog_observer_.get()); |
| 286 initiator_observer_.reset(); | 295 initiator_observer_.reset(); |
| 287 dialog_observer_.reset(); | 296 dialog_observer_.reset(); |
| 288 presentation_request_.reset(); | 297 presentation_request_.reset(); |
| 289 } | 298 } |
| 290 | 299 |
| 291 void MediaRouterDialogController::OnDialogNavigated( | 300 void MediaRouterDialogControllerImpl::OnDialogNavigated( |
| 292 const content::LoadCommittedDetails& details) { | 301 const content::LoadCommittedDetails& details) { |
| 293 DCHECK(thread_checker_.CalledOnValidThread()); | 302 DCHECK(thread_checker_.CalledOnValidThread()); |
| 294 WebContents* media_router_dialog = GetMediaRouterDialog(); | 303 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 295 CHECK(media_router_dialog); | 304 CHECK(media_router_dialog); |
| 296 ui::PageTransition transition_type = details.entry->GetTransitionType(); | 305 ui::PageTransition transition_type = details.entry->GetTransitionType(); |
| 297 content::NavigationType nav_type = details.type; | 306 content::NavigationType nav_type = details.type; |
| 298 | 307 |
| 299 // New |media_router_dialog| is created. | 308 // New |media_router_dialog| is created. |
| 300 DCHECK(media_router_dialog_pending_); | 309 DCHECK(media_router_dialog_pending_); |
| 301 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && | 310 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && |
| 302 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) | 311 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) |
| 303 << "transition_type: " << transition_type << ", " | 312 << "transition_type: " << transition_type << ", " |
| 304 << "nav_type: " << nav_type; | 313 << "nav_type: " << nav_type; |
| 305 | 314 |
| 306 media_router_dialog_pending_ = false; | 315 media_router_dialog_pending_ = false; |
| 307 | 316 |
| 308 PopulateDialog(media_router_dialog); | 317 PopulateDialog(media_router_dialog); |
| 309 } | 318 } |
| 310 | 319 |
| 311 void MediaRouterDialogController::PopulateDialog( | 320 void MediaRouterDialogControllerImpl::PopulateDialog( |
| 312 content::WebContents* media_router_dialog) { | 321 content::WebContents* media_router_dialog) { |
| 313 DCHECK(media_router_dialog); | 322 DCHECK(media_router_dialog); |
| 314 DCHECK(initiator_observer_); | 323 DCHECK(initiator_observer_); |
| 315 if (!initiator_observer_) { | 324 if (!initiator_observer_) { |
| 316 Reset(); | 325 Reset(); |
| 317 return; | 326 return; |
| 318 } | 327 } |
| 319 content::WebContents* initiator = initiator_observer_->web_contents(); | 328 content::WebContents* initiator = initiator_observer_->web_contents(); |
| 320 DCHECK(initiator); | 329 DCHECK(initiator); |
| 321 if (!initiator || !media_router_dialog->GetWebUI()) { | 330 if (!initiator || !media_router_dialog->GetWebUI()) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 338 CHECK(delegate); | 347 CHECK(delegate); |
| 339 media_router_ui->InitWithDefaultMediaSource(delegate); | 348 media_router_ui->InitWithDefaultMediaSource(delegate); |
| 340 } else { | 349 } else { |
| 341 media_router_ui->InitWithPresentationSessionRequest( | 350 media_router_ui->InitWithPresentationSessionRequest( |
| 342 initiator, presentation_request_.Pass()); | 351 initiator, presentation_request_.Pass()); |
| 343 } | 352 } |
| 344 } | 353 } |
| 345 | 354 |
| 346 } // namespace media_router | 355 } // namespace media_router |
| 347 | 356 |
| OLD | NEW |