| 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_im
pl.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/media/router/presentation_service_delegate_impl.h" | 10 #include "chrome/browser/media/router/presentation_service_delegate_impl.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | 12 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
| 13 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | 13 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "components/web_modal/web_contents_modal_dialog_host.h" | 15 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 19 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 21 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_contents.h" | |
| 24 #include "content/public/browser/web_contents_delegate.h" | |
| 25 #include "ui/web_dialogs/web_dialog_delegate.h" | 22 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 26 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | 23 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" |
| 27 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 28 | 25 |
| 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY( | 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 30 media_router::MediaRouterDialogControllerImpl); | 27 media_router::MediaRouterDialogControllerImpl); |
| 31 | 28 |
| 32 using content::LoadCommittedDetails; | 29 using content::LoadCommittedDetails; |
| 33 using content::NavigationController; | 30 using content::NavigationController; |
| 34 using content::WebContents; | 31 using content::WebContents; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 } | 140 } |
| 144 | 141 |
| 145 void RenderProcessGone(base::TerminationStatus status) override { | 142 void RenderProcessGone(base::TerminationStatus status) override { |
| 146 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 143 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
| 147 dialog_controller_->CloseMediaRouterDialog(); | 144 dialog_controller_->CloseMediaRouterDialog(); |
| 148 } | 145 } |
| 149 | 146 |
| 150 MediaRouterDialogControllerImpl* const dialog_controller_; | 147 MediaRouterDialogControllerImpl* const dialog_controller_; |
| 151 }; | 148 }; |
| 152 | 149 |
| 153 class MediaRouterDialogControllerImpl::InitiatorWebContentsObserver | |
| 154 : public content::WebContentsObserver { | |
| 155 public: | |
| 156 InitiatorWebContentsObserver( | |
| 157 WebContents* web_contents, | |
| 158 MediaRouterDialogControllerImpl* dialog_controller) | |
| 159 : content::WebContentsObserver(web_contents), | |
| 160 dialog_controller_(dialog_controller) { | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 void WebContentsDestroyed() override { | |
| 165 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 166 dialog_controller_->CloseMediaRouterDialog(); | |
| 167 } | |
| 168 | |
| 169 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) | |
| 170 override { | |
| 171 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 172 dialog_controller_->CloseMediaRouterDialog(); | |
| 173 } | |
| 174 | |
| 175 void RenderProcessGone(base::TerminationStatus status) override { | |
| 176 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 177 dialog_controller_->CloseMediaRouterDialog(); | |
| 178 } | |
| 179 | |
| 180 MediaRouterDialogControllerImpl* const dialog_controller_; | |
| 181 }; | |
| 182 | |
| 183 MediaRouterDialogControllerImpl::MediaRouterDialogControllerImpl( | 150 MediaRouterDialogControllerImpl::MediaRouterDialogControllerImpl( |
| 184 WebContents* web_contents) | 151 WebContents* web_contents) |
| 185 : initiator_(web_contents), | 152 : MediaRouterDialogController(web_contents), |
| 186 media_router_dialog_pending_(false) { | 153 media_router_dialog_pending_(false) { |
| 187 DCHECK(initiator_); | |
| 188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 189 } | 154 } |
| 190 | 155 |
| 191 MediaRouterDialogControllerImpl::~MediaRouterDialogControllerImpl() { | 156 MediaRouterDialogControllerImpl::~MediaRouterDialogControllerImpl() { |
| 192 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 193 } | |
| 194 | |
| 195 WebContents* MediaRouterDialogControllerImpl::ShowMediaRouterDialog() { | |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 197 | |
| 198 // Get the media router dialog for |initiator|, or create a new dialog | |
| 199 // if not found. | |
| 200 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 201 if (!media_router_dialog) { | |
| 202 CreateMediaRouterDialog(); | |
| 203 return GetMediaRouterDialog(); | |
| 204 } | |
| 205 | |
| 206 // Show the initiator holding the existing media router dialog. | |
| 207 initiator_->GetDelegate()->ActivateContents(initiator_); | |
| 208 return media_router_dialog; | |
| 209 } | |
| 210 | |
| 211 bool MediaRouterDialogControllerImpl::ShowMediaRouterDialogForPresentation( | |
| 212 scoped_ptr<CreatePresentationSessionRequest> request) { | |
| 213 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 214 | |
| 215 // Get the media router dialog for |initiator|, or create a new dialog | |
| 216 // if not found. | |
| 217 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 218 if (!media_router_dialog) { | |
| 219 CreateMediaRouterDialog(); | |
| 220 media_router_dialog = GetMediaRouterDialog(); | |
| 221 presentation_request_ = request.Pass(); | |
| 222 return true; | |
| 223 } | |
| 224 | |
| 225 // Show the initiator holding the existing media router dialog. | |
| 226 initiator_->GetDelegate()->ActivateContents(initiator_); | |
| 227 return false; | |
| 228 } | 157 } |
| 229 | 158 |
| 230 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { | 159 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { |
| 231 DCHECK(thread_checker_.CalledOnValidThread()); | 160 DCHECK(thread_checker_.CalledOnValidThread()); |
| 232 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; | 161 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; |
| 233 } | 162 } |
| 234 | 163 |
| 164 bool MediaRouterDialogControllerImpl::IsShowingMediaRouterDialog() const { |
| 165 return GetMediaRouterDialog() != nullptr; |
| 166 } |
| 167 |
| 235 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { | 168 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { |
| 236 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 237 DCHECK(initiator_observer_.get()); | |
| 238 WebContents* media_router_dialog = GetMediaRouterDialog(); | 169 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 239 CHECK(media_router_dialog); | 170 if (!media_router_dialog) |
| 240 Reset(); | 171 return; |
| 241 | 172 |
| 242 content::WebUI* web_ui = media_router_dialog->GetWebUI(); | 173 content::WebUI* web_ui = media_router_dialog->GetWebUI(); |
| 243 if (web_ui) { | 174 if (web_ui) { |
| 244 MediaRouterUI* media_router_ui = | 175 MediaRouterUI* media_router_ui = |
| 245 static_cast<MediaRouterUI*>(web_ui->GetController()); | 176 static_cast<MediaRouterUI*>(web_ui->GetController()); |
| 246 if (media_router_ui) | 177 if (media_router_ui) |
| 247 media_router_ui->Close(); | 178 media_router_ui->Close(); |
| 248 } | 179 } |
| 249 } | 180 } |
| 250 | 181 |
| 251 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { | 182 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { |
| 252 DCHECK(!initiator_observer_.get()); | |
| 253 DCHECK(!dialog_observer_.get()); | 183 DCHECK(!dialog_observer_.get()); |
| 254 | 184 |
| 255 Profile* profile = | 185 Profile* profile = |
| 256 Profile::FromBrowserContext(initiator_->GetBrowserContext()); | 186 Profile::FromBrowserContext(initiator()->GetBrowserContext()); |
| 257 DCHECK(profile); | 187 DCHECK(profile); |
| 258 | 188 |
| 259 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; | 189 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; |
| 260 | 190 |
| 261 // |web_dialog_delegate|'s owner is |constrained_delegate|. | 191 // |web_dialog_delegate|'s owner is |constrained_delegate|. |
| 262 // |constrained_delegate| is owned by the parent |views::View|. | 192 // |constrained_delegate| is owned by the parent |views::View|. |
| 263 // TODO(apacible): Remove after autoresizing is implemented for OSX. | 193 // TODO(apacible): Remove after autoresizing is implemented for OSX. |
| 264 #if defined(OS_MACOSX) | 194 #if defined(OS_MACOSX) |
| 265 ConstrainedWebDialogDelegate* constrained_delegate = | 195 ConstrainedWebDialogDelegate* constrained_delegate = |
| 266 ShowConstrainedWebDialog(profile, web_dialog_delegate, initiator_); | 196 ShowConstrainedWebDialog(profile, web_dialog_delegate, initiator()); |
| 267 #else | 197 #else |
| 268 // TODO(apacible): Adjust min and max sizes based on new WebUI design. | 198 // TODO(apacible): Adjust min and max sizes based on new WebUI design. |
| 269 gfx::Size min_size = gfx::Size(kWidth, kMinHeight); | 199 gfx::Size min_size = gfx::Size(kWidth, kMinHeight); |
| 270 gfx::Size max_size = gfx::Size(kWidth, kMaxHeight); | 200 gfx::Size max_size = gfx::Size(kWidth, kMaxHeight); |
| 271 | 201 |
| 272 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating | 202 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating |
| 273 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing | 203 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing |
| 274 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the | 204 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the |
| 275 // view is shown as a modal dialog constrained to the |initiator| WebContents. | 205 // view is shown as a modal dialog constrained to the |initiator| WebContents. |
| 276 // The dialog will resize between the |min_size| and |max_size| bounds based | 206 // The dialog will resize between the |min_size| and |max_size| bounds based |
| 277 // on the currently rendered contents. | 207 // on the currently rendered contents. |
| 278 ConstrainedWebDialogDelegate* constrained_delegate = | 208 ConstrainedWebDialogDelegate* constrained_delegate = |
| 279 ShowConstrainedWebDialogWithAutoResize( | 209 ShowConstrainedWebDialogWithAutoResize( |
| 280 profile, web_dialog_delegate, initiator_, min_size, max_size); | 210 profile, web_dialog_delegate, initiator(), min_size, max_size); |
| 281 #endif | 211 #endif |
| 282 | 212 |
| 283 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); | 213 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); |
| 284 | 214 |
| 285 media_router_dialog_pending_ = true; | 215 media_router_dialog_pending_ = true; |
| 286 | 216 |
| 287 initiator_observer_.reset(new InitiatorWebContentsObserver( | |
| 288 initiator_, this)); | |
| 289 dialog_observer_.reset(new DialogWebContentsObserver( | 217 dialog_observer_.reset(new DialogWebContentsObserver( |
| 290 media_router_dialog, this)); | 218 media_router_dialog, this)); |
| 291 } | 219 } |
| 292 | 220 |
| 293 void MediaRouterDialogControllerImpl::Reset() { | 221 void MediaRouterDialogControllerImpl::Reset() { |
| 294 DCHECK(thread_checker_.CalledOnValidThread()); | 222 MediaRouterDialogController::Reset(); |
| 295 DCHECK(initiator_observer_.get()); | |
| 296 DCHECK(dialog_observer_.get()); | |
| 297 initiator_observer_.reset(); | |
| 298 dialog_observer_.reset(); | 223 dialog_observer_.reset(); |
| 299 presentation_request_.reset(); | |
| 300 } | 224 } |
| 301 | 225 |
| 302 void MediaRouterDialogControllerImpl::OnDialogNavigated( | 226 void MediaRouterDialogControllerImpl::OnDialogNavigated( |
| 303 const content::LoadCommittedDetails& details) { | 227 const content::LoadCommittedDetails& details) { |
| 304 DCHECK(thread_checker_.CalledOnValidThread()); | 228 DCHECK(thread_checker_.CalledOnValidThread()); |
| 305 WebContents* media_router_dialog = GetMediaRouterDialog(); | 229 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 306 CHECK(media_router_dialog); | 230 CHECK(media_router_dialog); |
| 307 ui::PageTransition transition_type = details.entry->GetTransitionType(); | 231 ui::PageTransition transition_type = details.entry->GetTransitionType(); |
| 308 content::NavigationType nav_type = details.type; | 232 content::NavigationType nav_type = details.type; |
| 309 | 233 |
| 310 // New |media_router_dialog| is created. | 234 // New |media_router_dialog| is created. |
| 311 DCHECK(media_router_dialog_pending_); | 235 DCHECK(media_router_dialog_pending_); |
| 312 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && | 236 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && |
| 313 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) | 237 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) |
| 314 << "transition_type: " << transition_type << ", " | 238 << "transition_type: " << transition_type << ", " |
| 315 << "nav_type: " << nav_type; | 239 << "nav_type: " << nav_type; |
| 316 | 240 |
| 317 media_router_dialog_pending_ = false; | 241 media_router_dialog_pending_ = false; |
| 318 | 242 |
| 319 PopulateDialog(media_router_dialog); | 243 PopulateDialog(media_router_dialog); |
| 320 } | 244 } |
| 321 | 245 |
| 322 void MediaRouterDialogControllerImpl::PopulateDialog( | 246 void MediaRouterDialogControllerImpl::PopulateDialog( |
| 323 content::WebContents* media_router_dialog) { | 247 content::WebContents* media_router_dialog) { |
| 248 DCHECK(thread_checker_.CalledOnValidThread()); |
| 324 DCHECK(media_router_dialog); | 249 DCHECK(media_router_dialog); |
| 325 DCHECK(initiator_observer_); | 250 if (!initiator() || !media_router_dialog->GetWebUI()) { |
| 326 if (!initiator_observer_) { | |
| 327 Reset(); | 251 Reset(); |
| 328 return; | 252 return; |
| 329 } | 253 } |
| 330 content::WebContents* initiator = initiator_observer_->web_contents(); | |
| 331 DCHECK(initiator); | |
| 332 if (!initiator || !media_router_dialog->GetWebUI()) { | |
| 333 Reset(); | |
| 334 return; | |
| 335 } | |
| 336 | 254 |
| 337 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( | 255 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( |
| 338 media_router_dialog->GetWebUI()->GetController()); | 256 media_router_dialog->GetWebUI()->GetController()); |
| 339 DCHECK(media_router_ui); | 257 DCHECK(media_router_ui); |
| 340 if (!media_router_ui) { | 258 if (!media_router_ui) { |
| 341 Reset(); | 259 Reset(); |
| 342 return; | 260 return; |
| 343 } | 261 } |
| 344 | 262 |
| 345 if (!presentation_request_.get()) { | 263 scoped_ptr<CreatePresentationSessionRequest> presentation_request( |
| 264 PassPresentationRequest()); |
| 265 if (!presentation_request.get()) { |
| 346 // TODO(imcheng): Don't create PresentationServiceDelegateImpl if it doesn't | 266 // TODO(imcheng): Don't create PresentationServiceDelegateImpl if it doesn't |
| 347 // exist (crbug.com/508695). | 267 // exist (crbug.com/508695). |
| 348 base::WeakPtr<PresentationServiceDelegateImpl> delegate = | 268 base::WeakPtr<PresentationServiceDelegateImpl> delegate = |
| 349 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator) | 269 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator()) |
| 350 ->GetWeakPtr(); | 270 ->GetWeakPtr(); |
| 351 media_router_ui->InitWithDefaultMediaSource(delegate); | 271 media_router_ui->InitWithDefaultMediaSource(delegate); |
| 352 } else { | 272 } else { |
| 353 media_router_ui->InitWithPresentationSessionRequest( | 273 media_router_ui->InitWithPresentationSessionRequest( |
| 354 initiator, presentation_request_.Pass()); | 274 initiator(), presentation_request.Pass()); |
| 355 } | 275 } |
| 356 } | 276 } |
| 357 | 277 |
| 358 } // namespace media_router | 278 } // namespace media_router |
| 359 | 279 |
| OLD | NEW |