| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/media_router/media_router_dialog_controller.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | |
| 12 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | |
| 13 #include "chrome/common/url_constants.h" | |
| 14 #include "components/web_modal/web_contents_modal_dialog_host.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/navigation_controller.h" | |
| 17 #include "content/public/browser/navigation_details.h" | |
| 18 #include "content/public/browser/navigation_entry.h" | |
| 19 #include "content/public/browser/render_frame_host.h" | |
| 20 #include "content/public/browser/render_process_host.h" | |
| 21 #include "content/public/browser/render_view_host.h" | |
| 22 #include "content/public/browser/web_contents.h" | |
| 23 #include "content/public/browser/web_contents_delegate.h" | |
| 24 #include "ui/web_dialogs/web_dialog_delegate.h" | |
| 25 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | |
| 26 #include "url/gurl.h" | |
| 27 | |
| 28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(media_router::MediaRouterDialogController); | |
| 29 | |
| 30 using content::LoadCommittedDetails; | |
| 31 using content::NavigationController; | |
| 32 using content::WebContents; | |
| 33 using content::WebUIMessageHandler; | |
| 34 using ui::WebDialogDelegate; | |
| 35 | |
| 36 namespace media_router { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 // WebDialogDelegate that specifies what the media router dialog | |
| 41 // will look like. | |
| 42 class MediaRouterDialogDelegate : public WebDialogDelegate { | |
| 43 public: | |
| 44 MediaRouterDialogDelegate() {} | |
| 45 ~MediaRouterDialogDelegate() override {} | |
| 46 | |
| 47 // WebDialogDelegate implementation. | |
| 48 ui::ModalType GetDialogModalType() const override { | |
| 49 // Not used, returning dummy value. | |
| 50 return ui::MODAL_TYPE_WINDOW; | |
| 51 } | |
| 52 | |
| 53 base::string16 GetDialogTitle() const override { | |
| 54 return base::string16(); | |
| 55 } | |
| 56 | |
| 57 GURL GetDialogContentURL() const override { | |
| 58 return GURL(chrome::kChromeUIMediaRouterURL); | |
| 59 } | |
| 60 | |
| 61 void GetWebUIMessageHandlers( | |
| 62 std::vector<WebUIMessageHandler*>* handlers) const override { | |
| 63 // MediaRouterUI adds its own message handlers. | |
| 64 } | |
| 65 | |
| 66 void GetDialogSize(gfx::Size* size) const override; | |
| 67 | |
| 68 std::string GetDialogArgs() const override { | |
| 69 return std::string(); | |
| 70 } | |
| 71 | |
| 72 void OnDialogClosed(const std::string& json_retval) override { | |
| 73 // We don't delete |this| here because this class is owned | |
| 74 // by ConstrainedWebDialogDelegate. | |
| 75 } | |
| 76 | |
| 77 void OnCloseContents(WebContents* source, bool* out_close_dialog) override { | |
| 78 if (out_close_dialog) | |
| 79 *out_close_dialog = true; | |
| 80 } | |
| 81 | |
| 82 bool ShouldShowDialogTitle() const override { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogDelegate); | |
| 88 }; | |
| 89 | |
| 90 void MediaRouterDialogDelegate::GetDialogSize(gfx::Size* size) const { | |
| 91 DCHECK(size); | |
| 92 // TODO(apacible): Remove after autoresizing is implemented for OSX. | |
| 93 #if defined(OS_MACOSX) | |
| 94 *size = gfx::Size(330, 400); | |
| 95 #else | |
| 96 // size is not used because the dialog is auto-resizeable. | |
| 97 *size = gfx::Size(); | |
| 98 #endif | |
| 99 } | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 // static | |
| 104 MediaRouterDialogController* | |
| 105 MediaRouterDialogController::GetOrCreateForWebContents( | |
| 106 WebContents* web_contents) { | |
| 107 DCHECK(web_contents); | |
| 108 // This call does nothing if the controller already exists. | |
| 109 MediaRouterDialogController::CreateForWebContents(web_contents); | |
| 110 return MediaRouterDialogController::FromWebContents(web_contents); | |
| 111 } | |
| 112 | |
| 113 class MediaRouterDialogController::DialogWebContentsObserver | |
| 114 : public content::WebContentsObserver { | |
| 115 public: | |
| 116 DialogWebContentsObserver( | |
| 117 WebContents* web_contents, | |
| 118 MediaRouterDialogController* dialog_controller) | |
| 119 : content::WebContentsObserver(web_contents), | |
| 120 dialog_controller_(dialog_controller) { | |
| 121 } | |
| 122 | |
| 123 private: | |
| 124 void WebContentsDestroyed() override { | |
| 125 // The dialog is already closed. No need to call Close() again. | |
| 126 // NOTE: |this| is deleted after Reset() returns. | |
| 127 dialog_controller_->Reset(); | |
| 128 } | |
| 129 | |
| 130 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) | |
| 131 override { | |
| 132 dialog_controller_->OnDialogNavigated(load_details); | |
| 133 } | |
| 134 | |
| 135 void RenderProcessGone(base::TerminationStatus status) override { | |
| 136 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 137 dialog_controller_->CloseMediaRouterDialog(); | |
| 138 } | |
| 139 | |
| 140 MediaRouterDialogController* const dialog_controller_; | |
| 141 }; | |
| 142 | |
| 143 class MediaRouterDialogController::InitiatorWebContentsObserver | |
| 144 : public content::WebContentsObserver { | |
| 145 public: | |
| 146 InitiatorWebContentsObserver( | |
| 147 WebContents* web_contents, | |
| 148 MediaRouterDialogController* dialog_controller) | |
| 149 : content::WebContentsObserver(web_contents), | |
| 150 dialog_controller_(dialog_controller) { | |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 void WebContentsDestroyed() override { | |
| 155 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 156 dialog_controller_->CloseMediaRouterDialog(); | |
| 157 } | |
| 158 | |
| 159 void NavigationEntryCommitted(const LoadCommittedDetails& load_details) | |
| 160 override { | |
| 161 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 162 dialog_controller_->CloseMediaRouterDialog(); | |
| 163 } | |
| 164 | |
| 165 void RenderProcessGone(base::TerminationStatus status) override { | |
| 166 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | |
| 167 dialog_controller_->CloseMediaRouterDialog(); | |
| 168 } | |
| 169 | |
| 170 MediaRouterDialogController* const dialog_controller_; | |
| 171 }; | |
| 172 | |
| 173 MediaRouterDialogController::MediaRouterDialogController( | |
| 174 WebContents* web_contents) | |
| 175 : initiator_(web_contents), | |
| 176 media_router_dialog_pending_(false) { | |
| 177 DCHECK(initiator_); | |
| 178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 179 } | |
| 180 | |
| 181 MediaRouterDialogController::~MediaRouterDialogController() { | |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 183 } | |
| 184 | |
| 185 WebContents* MediaRouterDialogController::ShowMediaRouterDialog() { | |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 187 | |
| 188 // Get the media router dialog for |initiator|, or create a new dialog | |
| 189 // if not found. | |
| 190 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 191 if (!media_router_dialog) { | |
| 192 CreateMediaRouterDialog(); | |
| 193 return GetMediaRouterDialog(); | |
| 194 } | |
| 195 | |
| 196 // Show the initiator holding the existing media router dialog. | |
| 197 initiator_->GetDelegate()->ActivateContents(initiator_); | |
| 198 return media_router_dialog; | |
| 199 } | |
| 200 | |
| 201 WebContents* MediaRouterDialogController::ShowMediaRouterDialogForPresentation( | |
| 202 scoped_ptr<CreateSessionRequest> request) { | |
| 203 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 204 | |
| 205 // Get the media router dialog for |initiator|, or create a new dialog | |
| 206 // if not found. | |
| 207 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 208 if (!media_router_dialog) { | |
| 209 CreateMediaRouterDialog(); | |
| 210 media_router_dialog = GetMediaRouterDialog(); | |
| 211 presentation_request_ = request.Pass(); | |
| 212 return media_router_dialog; | |
| 213 } | |
| 214 | |
| 215 // Show the initiator holding the existing media router dialog. | |
| 216 initiator_->GetDelegate()->ActivateContents(initiator_); | |
| 217 return nullptr; | |
| 218 } | |
| 219 | |
| 220 WebContents* MediaRouterDialogController::GetMediaRouterDialog() const { | |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 222 return dialog_observer_.get() ? dialog_observer_->web_contents() : nullptr; | |
| 223 } | |
| 224 | |
| 225 void MediaRouterDialogController::CloseMediaRouterDialog() { | |
| 226 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 227 DCHECK(initiator_observer_.get()); | |
| 228 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 229 CHECK(media_router_dialog); | |
| 230 Reset(); | |
| 231 | |
| 232 content::WebUI* web_ui = media_router_dialog->GetWebUI(); | |
| 233 if (web_ui) { | |
| 234 MediaRouterUI* media_router_ui = | |
| 235 static_cast<MediaRouterUI*>(web_ui->GetController()); | |
| 236 if (media_router_ui) | |
| 237 media_router_ui->Close(); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void MediaRouterDialogController::CreateMediaRouterDialog() { | |
| 242 DCHECK(!initiator_observer_.get()); | |
| 243 DCHECK(!dialog_observer_.get()); | |
| 244 | |
| 245 Profile* profile = | |
| 246 Profile::FromBrowserContext(initiator_->GetBrowserContext()); | |
| 247 DCHECK(profile); | |
| 248 | |
| 249 WebDialogDelegate* web_dialog_delegate = new MediaRouterDialogDelegate; | |
| 250 | |
| 251 // |web_dialog_delegate|'s owner is |constrained_delegate|. | |
| 252 // |constrained_delegate| is owned by the parent |views::View|. | |
| 253 // TODO(apacible): Remove after autoresizing is implemented for OSX. | |
| 254 #if defined(OS_MACOSX) | |
| 255 ConstrainedWebDialogDelegate* constrained_delegate = | |
| 256 ShowConstrainedWebDialog(profile, web_dialog_delegate, initiator_); | |
| 257 #else | |
| 258 // TODO(apacible): Adjust min and max sizes based on new WebUI design. | |
| 259 gfx::Size min_size = gfx::Size(330, 1); | |
| 260 gfx::Size max_size = gfx::Size(500, 500); | |
| 261 // |ShowConstrainedWebDialogWithAutoResize()| will end up creating | |
| 262 // ConstrainedWebDialogDelegateViewViews containing a WebContents containing | |
| 263 // the MediaRouterUI, using the provided |web_dialog_delegate|. Then, the | |
| 264 // view is shown as a modal dialog constrained to the |initiator| WebContents. | |
| 265 // The dialog will resize between the |min_size| and |max_size| bounds based | |
| 266 // on the currently rendered contents. | |
| 267 ConstrainedWebDialogDelegate* constrained_delegate = | |
| 268 ShowConstrainedWebDialogWithAutoResize( | |
| 269 profile, web_dialog_delegate, initiator_, min_size, max_size); | |
| 270 #endif | |
| 271 | |
| 272 WebContents* media_router_dialog = constrained_delegate->GetWebContents(); | |
| 273 | |
| 274 media_router_dialog_pending_ = true; | |
| 275 | |
| 276 initiator_observer_.reset(new InitiatorWebContentsObserver( | |
| 277 initiator_, this)); | |
| 278 dialog_observer_.reset(new DialogWebContentsObserver( | |
| 279 media_router_dialog, this)); | |
| 280 } | |
| 281 | |
| 282 void MediaRouterDialogController::Reset() { | |
| 283 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 284 DCHECK(initiator_observer_.get()); | |
| 285 DCHECK(dialog_observer_.get()); | |
| 286 initiator_observer_.reset(); | |
| 287 dialog_observer_.reset(); | |
| 288 presentation_request_.reset(); | |
| 289 } | |
| 290 | |
| 291 void MediaRouterDialogController::OnDialogNavigated( | |
| 292 const content::LoadCommittedDetails& details) { | |
| 293 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 294 WebContents* media_router_dialog = GetMediaRouterDialog(); | |
| 295 CHECK(media_router_dialog); | |
| 296 ui::PageTransition transition_type = details.entry->GetTransitionType(); | |
| 297 content::NavigationType nav_type = details.type; | |
| 298 | |
| 299 // New |media_router_dialog| is created. | |
| 300 DCHECK(media_router_dialog_pending_); | |
| 301 DCHECK(transition_type == ui::PAGE_TRANSITION_AUTO_TOPLEVEL && | |
| 302 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) | |
| 303 << "transition_type: " << transition_type << ", " | |
| 304 << "nav_type: " << nav_type; | |
| 305 | |
| 306 media_router_dialog_pending_ = false; | |
| 307 | |
| 308 PopulateDialog(media_router_dialog); | |
| 309 } | |
| 310 | |
| 311 void MediaRouterDialogController::PopulateDialog( | |
| 312 content::WebContents* media_router_dialog) { | |
| 313 DCHECK(media_router_dialog); | |
| 314 DCHECK(initiator_observer_); | |
| 315 if (!initiator_observer_) { | |
| 316 Reset(); | |
| 317 return; | |
| 318 } | |
| 319 content::WebContents* initiator = initiator_observer_->web_contents(); | |
| 320 DCHECK(initiator); | |
| 321 if (!initiator || !media_router_dialog->GetWebUI()) { | |
| 322 Reset(); | |
| 323 return; | |
| 324 } | |
| 325 | |
| 326 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( | |
| 327 media_router_dialog->GetWebUI()->GetController()); | |
| 328 DCHECK(media_router_ui); | |
| 329 if (!media_router_ui) { | |
| 330 Reset(); | |
| 331 return; | |
| 332 } | |
| 333 | |
| 334 if (!presentation_request_.get()) { | |
| 335 PresentationServiceDelegateImpl::CreateForWebContents(initiator); | |
| 336 PresentationServiceDelegateImpl* delegate = | |
| 337 PresentationServiceDelegateImpl::FromWebContents(initiator); | |
| 338 CHECK(delegate); | |
| 339 media_router_ui->InitWithDefaultMediaSource(delegate); | |
| 340 } else { | |
| 341 media_router_ui->InitWithPresentationSessionRequest( | |
| 342 initiator, presentation_request_.Pass()); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 } // namespace media_router | |
| 347 | |
| OLD | NEW |