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/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()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 193 } | 158 } |
| 194 | 159 |
| 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 } | |
| 229 | |
| 230 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { | 160 WebContents* MediaRouterDialogControllerImpl::GetMediaRouterDialog() const { |
| 231 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 232 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; | 162 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; |
| 233 } | 163 } |
| 234 | 164 |
| 165 bool MediaRouterDialogControllerImpl::IsShowingMediaRouterDialog() { | |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 167 return !!GetMediaRouterDialog(); | |
|
apacible
2015/07/24 20:41:07
Does this negate itself?
whywhat
2015/07/28 18:52:53
Yes, an old way of converting pointers/integers to
apacible
2015/07/28 21:27:25
Acknowledged, thanks!
| |
| 168 } | |
| 169 | |
| 235 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { | 170 void MediaRouterDialogControllerImpl::CloseMediaRouterDialog() { |
| 236 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 237 DCHECK(initiator_observer_.get()); | |
| 238 WebContents* media_router_dialog = GetMediaRouterDialog(); | 172 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 239 CHECK(media_router_dialog); | 173 CHECK(media_router_dialog); |
| 240 Reset(); | 174 |
| 175 // Resets the media router dialog observer so can't be called earlier. | |
| 176 MediaRouterDialogController::CloseMediaRouterDialog(); | |
| 241 | 177 |
| 242 content::WebUI* web_ui = media_router_dialog->GetWebUI(); | 178 content::WebUI* web_ui = media_router_dialog->GetWebUI(); |
| 243 if (web_ui) { | 179 if (web_ui) { |
| 244 MediaRouterUI* media_router_ui = | 180 MediaRouterUI* media_router_ui = |
| 245 static_cast<MediaRouterUI*>(web_ui->GetController()); | 181 static_cast<MediaRouterUI*>(web_ui->GetController()); |
| 246 if (media_router_ui) | 182 if (media_router_ui) |
| 247 media_router_ui->Close(); | 183 media_router_ui->Close(); |
| 248 } | 184 } |
| 249 } | 185 } |
| 250 | 186 |
| 251 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { | 187 void MediaRouterDialogControllerImpl::CreateMediaRouterDialog() { |
| 252 DCHECK(!initiator_observer_.get()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
| 253 DCHECK(!dialog_observer_.get()); | 189 DCHECK(!dialog_observer_.get()); |
| 254 | 190 |
| 255 Profile* profile = | 191 Profile* profile = |
| 256 Profile::FromBrowserContext(initiator_->GetBrowserContext()); | 192 Profile::FromBrowserContext(initiator()->GetBrowserContext()); |
| 257 DCHECK(profile); | 193 DCHECK(profile); |
| 258 | 194 |
| 259 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; | 195 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; |
| 260 | 196 |
| 261 // |web_dialog_delegate|'s owner is |constrained_delegate|. | 197 // |web_dialog_delegate|'s owner is |constrained_delegate|. |
| 262 // |constrained_delegate| is owned by the parent |views::View|. | 198 // |constrained_delegate| is owned by the parent |views::View|. |
| 263 // TODO(apacible): Remove after autoresizing is implemented for OSX. | 199 // TODO(apacible): Remove after autoresizing is implemented for OSX. |
| 264 #if defined(OS_MACOSX) | 200 #if defined(OS_MACOSX) |
| 265 ConstrainedWebDialogDelegate* constrained_delegate = | 201 ConstrainedWebDialogDelegate* constrained_delegate = |
| 266 ShowConstrainedWebDialog(profile, web_dialog_delegate, initiator_); | 202 ShowConstrainedWebDialog(profile, web_dialog_delegate, initiator()); |
| 267 #else | 203 #else |
| 268 // TODO(apacible): Adjust min and max sizes based on new WebUI design. | 204 // TODO(apacible): Adjust min and max sizes based on new WebUI design. |
| 269 gfx::Size min_size = gfx::Size(kWidth, kMinHeight); | 205 gfx::Size min_size = gfx::Size(kWidth, kMinHeight); |
| 270 gfx::Size max_size = gfx::Size(kWidth, kMaxHeight); | 206 gfx::Size max_size = gfx::Size(kWidth, kMaxHeight); |
| 271 | 207 |
| 272 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating | 208 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating |
| 273 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing | 209 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing |
| 274 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the | 210 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the |
| 275 // view is shown as a modal dialog constrained to the |initiator| WebContents. | 211 // 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 | 212 // The dialog will resize between the |min_size| and |max_size| bounds based |
| 277 // on the currently rendered contents. | 213 // on the currently rendered contents. |
| 278 ConstrainedWebDialogDelegate* constrained_delegate = | 214 ConstrainedWebDialogDelegate* constrained_delegate = |
| 279 ShowConstrainedWebDialogWithAutoResize( | 215 ShowConstrainedWebDialogWithAutoResize( |
| 280 profile, web_dialog_delegate, initiator_, min_size, max_size); | 216 profile, web_dialog_delegate, initiator(), min_size, max_size); |
| 281 #endif | 217 #endif |
| 282 | 218 |
| 283 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); | 219 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); |
| 284 | 220 |
| 285 media_router_dialog_pending_ = true; | 221 media_router_dialog_pending_ = true; |
| 286 | 222 |
| 287 initiator_observer_.reset(new InitiatorWebContentsObserver( | 223 MediaRouterDialogController::ObserveInitiatorWebContents(); |
| 288 initiator_, this)); | |
| 289 dialog_observer_.reset(new DialogWebContentsObserver( | 224 dialog_observer_.reset(new DialogWebContentsObserver( |
| 290 media_router_dialog, this)); | 225 media_router_dialog, this)); |
| 291 } | 226 } |
| 292 | 227 |
| 293 void MediaRouterDialogControllerImpl::Reset() { | 228 void MediaRouterDialogControllerImpl::Reset() { |
| 294 DCHECK(thread_checker_.CalledOnValidThread()); | 229 DCHECK(thread_checker_.CalledOnValidThread()); |
| 295 DCHECK(initiator_observer_.get()); | |
| 296 DCHECK(dialog_observer_.get()); | 230 DCHECK(dialog_observer_.get()); |
| 297 initiator_observer_.reset(); | 231 |
| 232 MediaRouterDialogController::Reset(); | |
| 298 dialog_observer_.reset(); | 233 dialog_observer_.reset(); |
| 299 presentation_request_.reset(); | |
| 300 } | 234 } |
| 301 | 235 |
| 302 void MediaRouterDialogControllerImpl::OnDialogNavigated( | 236 void MediaRouterDialogControllerImpl::OnDialogNavigated( |
| 303 const content::LoadCommittedDetails& details) { | 237 const content::LoadCommittedDetails& details) { |
| 304 DCHECK(thread_checker_.CalledOnValidThread()); | 238 DCHECK(thread_checker_.CalledOnValidThread()); |
| 305 WebContents* media_router_dialog = GetMediaRouterDialog(); | 239 WebContents* media_router_dialog = GetMediaRouterDialog(); |
| 306 CHECK(media_router_dialog); | 240 CHECK(media_router_dialog); |
| 307 ui::PageTransition transition_type = details.entry->GetTransitionType(); | 241 ui::PageTransition transition_type = details.entry->GetTransitionType(); |
| 308 content::NavigationType nav_type = details.type; | 242 content::NavigationType nav_type = details.type; |
| 309 | 243 |
| 310 // New |media_router_dialog| is created. | 244 // New |media_router_dialog| is created. |
| 311 DCHECK(media_router_dialog_pending_); | 245 DCHECK(media_router_dialog_pending_); |
| 312 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && | 246 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && |
| 313 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) | 247 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) |
| 314 << "transition_type: " << transition_type << ", " | 248 << "transition_type: " << transition_type << ", " |
| 315 << "nav_type: " << nav_type; | 249 << "nav_type: " << nav_type; |
| 316 | 250 |
| 317 media_router_dialog_pending_ = false; | 251 media_router_dialog_pending_ = false; |
| 318 | 252 |
| 319 PopulateDialog(media_router_dialog); | 253 PopulateDialog(media_router_dialog); |
| 320 } | 254 } |
| 321 | 255 |
| 322 void MediaRouterDialogControllerImpl::PopulateDialog( | 256 void MediaRouterDialogControllerImpl::PopulateDialog( |
| 323 content::WebContents* media_router_dialog) { | 257 content::WebContents* media_router_dialog) { |
| 258 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 324 DCHECK(media_router_dialog); | 259 DCHECK(media_router_dialog); |
| 325 DCHECK(initiator_observer_); | 260 DCHECK(initiator()); |
| 326 if (!initiator_observer_) { | 261 if (!initiator() || !media_router_dialog->GetWebUI()) { |
| 327 Reset(); | 262 Reset(); |
| 328 return; | 263 return; |
| 329 } | 264 } |
| 330 content::WebContents* initiator = initiator_observer_->web_contents(); | |
| 331 DCHECK(initiator); | |
| 332 if (!initiator || !media_router_dialog->GetWebUI()) { | |
| 333 Reset(); | |
| 334 return; | |
| 335 } | |
| 336 | 265 |
| 337 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( | 266 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( |
| 338 media_router_dialog->GetWebUI()->GetController()); | 267 media_router_dialog->GetWebUI()->GetController()); |
| 339 DCHECK(media_router_ui); | 268 DCHECK(media_router_ui); |
| 340 if (!media_router_ui) { | 269 if (!media_router_ui) { |
| 341 Reset(); | 270 Reset(); |
| 342 return; | 271 return; |
| 343 } | 272 } |
| 344 | 273 |
| 345 if (!presentation_request_.get()) { | 274 scoped_ptr<CreatePresentationSessionRequest> presentation_request( |
| 275 PassPresentationRequest()); | |
| 276 if (!presentation_request.get()) { | |
| 346 // TODO(imcheng): Don't create PresentationServiceDelegateImpl if it doesn't | 277 // TODO(imcheng): Don't create PresentationServiceDelegateImpl if it doesn't |
| 347 // exist (crbug.com/508695). | 278 // exist (crbug.com/508695). |
| 348 base::WeakPtr<PresentationServiceDelegateImpl> delegate = | 279 base::WeakPtr<PresentationServiceDelegateImpl> delegate = |
| 349 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator) | 280 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator()) |
| 350 ->GetWeakPtr(); | 281 ->GetWeakPtr(); |
| 351 media_router_ui->InitWithDefaultMediaSource(delegate); | 282 media_router_ui->InitWithDefaultMediaSource(delegate); |
| 352 } else { | 283 } else { |
| 353 media_router_ui->InitWithPresentationSessionRequest( | 284 media_router_ui->InitWithPresentationSessionRequest( |
| 354 initiator, presentation_request_.Pass()); | 285 initiator(), presentation_request.Pass()); |
| 355 } | 286 } |
| 356 } | 287 } |
| 357 | 288 |
| 358 } // namespace media_router | 289 } // namespace media_router |
| 359 | 290 |
| OLD | NEW |