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.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" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 163 } |
164 | 164 |
165 void RenderProcessGone(base::TerminationStatus status) override { | 165 void RenderProcessGone(base::TerminationStatus status) override { |
166 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. | 166 // NOTE: |this| is deleted after CloseMediaRouterDialog() returns. |
167 dialog_controller_->CloseMediaRouterDialog(); | 167 dialog_controller_->CloseMediaRouterDialog(); |
168 } | 168 } |
169 | 169 |
170 MediaRouterDialogController* const dialog_controller_; | 170 MediaRouterDialogController* const dialog_controller_; |
171 }; | 171 }; |
172 | 172 |
| 173 MediaRouterDialogCallbacks::MediaRouterDialogCallbacks( |
| 174 const base::WeakPtr<PresentationServiceDelegateImpl>& delegate) |
| 175 : MediaRouterDialogCallbacks(nullptr, delegate) { |
| 176 } |
| 177 |
| 178 MediaRouterDialogCallbacks::MediaRouterDialogCallbacks( |
| 179 scoped_ptr<CreateSessionRequest> presentation_request, |
| 180 const base::WeakPtr<PresentationServiceDelegateImpl>& delegate) |
| 181 : presentation_request_(presentation_request.Pass()), |
| 182 pending_presentation_response_after_dialog_destroyed_(false), |
| 183 presentation_service_delegate_(delegate), |
| 184 weak_factory_(this) { |
| 185 } |
| 186 |
| 187 MediaRouterDialogCallbacks::~MediaRouterDialogCallbacks() { |
| 188 if (presentation_request_) { |
| 189 presentation_request_->MaybeInvokeErrorCallback(content::PresentationError( |
| 190 content::PRESENTATION_ERROR_UNKNOWN, "Unknown error.")); |
| 191 } |
| 192 } |
| 193 |
| 194 void MediaRouterDialogCallbacks::OnRouteResponseReceived( |
| 195 bool is_presentation, |
| 196 scoped_ptr<MediaRoute> route, |
| 197 const std::string& error) { |
| 198 if (is_presentation) { |
| 199 if (presentation_request_) { |
| 200 // Remove the presentation request after responding. Subsequent |
| 201 // responses from the same dialog will be treated as if they came from |
| 202 // browser-initiated requests. |
| 203 HandleRouteResponseForPresentation(route.get(), error); |
| 204 presentation_request_.reset(); |
| 205 } else if (route && presentation_service_delegate_) { |
| 206 // Response was due to a browser-initiated request. Let |
| 207 // PresentationServiceDelegateImpl perform the match against the default |
| 208 // presentation URL. |
| 209 presentation_service_delegate_->OnRouteCreated(*route); |
| 210 } |
| 211 } |
| 212 |
| 213 if (!route_response_cb_.is_null()) |
| 214 route_response_cb_.Run(route.get(), error); |
| 215 |
| 216 if (pending_presentation_response_after_dialog_destroyed_) |
| 217 delete this; |
| 218 } |
| 219 |
| 220 void MediaRouterDialogCallbacks::HandleRouteResponseForPresentation( |
| 221 const MediaRoute* route, |
| 222 const std::string& error) { |
| 223 if (!route) { |
| 224 presentation_request_->MaybeInvokeErrorCallback( |
| 225 content::PresentationError(content::PRESENTATION_ERROR_UNKNOWN, error)); |
| 226 } else { |
| 227 presentation_request_->MaybeInvokeSuccessCallback(route->media_route_id()); |
| 228 } |
| 229 } |
| 230 |
| 231 void MediaRouterDialogCallbacks::OnDialogDestroyed( |
| 232 bool has_pending_presentation_response) { |
| 233 if (has_pending_presentation_response) { |
| 234 pending_presentation_response_after_dialog_destroyed_ = true; |
| 235 } else { |
| 236 if (presentation_request_) { |
| 237 presentation_request_->MaybeInvokeErrorCallback( |
| 238 content::PresentationError( |
| 239 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED, |
| 240 "Dialog closed.")); |
| 241 presentation_request_.reset(); |
| 242 } |
| 243 delete this; |
| 244 } |
| 245 } |
| 246 |
| 247 base::WeakPtr<MediaRouterDialogCallbacks> |
| 248 MediaRouterDialogCallbacks::GetWeakPtr() { |
| 249 return weak_factory_.GetWeakPtr(); |
| 250 } |
| 251 |
173 MediaRouterDialogController::MediaRouterDialogController( | 252 MediaRouterDialogController::MediaRouterDialogController( |
174 WebContents* web_contents) | 253 WebContents* web_contents) |
175 : initiator_(web_contents), | 254 : initiator_(web_contents), |
176 media_router_dialog_pending_(false) { | 255 media_router_dialog_pending_(false) { |
177 DCHECK(initiator_); | 256 DCHECK(initiator_); |
178 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 257 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
179 } | 258 } |
180 | 259 |
181 MediaRouterDialogController::~MediaRouterDialogController() { | 260 MediaRouterDialogController::~MediaRouterDialogController() { |
182 DCHECK(thread_checker_.CalledOnValidThread()); | 261 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 } | 403 } |
325 | 404 |
326 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( | 405 MediaRouterUI* media_router_ui = static_cast<MediaRouterUI*>( |
327 media_router_dialog->GetWebUI()->GetController()); | 406 media_router_dialog->GetWebUI()->GetController()); |
328 DCHECK(media_router_ui); | 407 DCHECK(media_router_ui); |
329 if (!media_router_ui) { | 408 if (!media_router_ui) { |
330 Reset(); | 409 Reset(); |
331 return; | 410 return; |
332 } | 411 } |
333 | 412 |
| 413 base::WeakPtr<PresentationServiceDelegateImpl> delegate = |
| 414 PresentationServiceDelegateImpl::GetOrCreateForWebContents(initiator) |
| 415 ->GetWeakPtr(); |
| 416 CHECK(delegate); |
| 417 // TODO(imcheng): Currently it is assumed that MediaRouter requests will |
| 418 // always be eventually resolved, but this may not be true due to |
| 419 // implementation errors, in which case DialogCallbacks objects may be leaked. |
| 420 // We should have some timeout mechanism to make sure they self-destruct |
| 421 // after certain conditions and timeout are met. |
334 if (!presentation_request_.get()) { | 422 if (!presentation_request_.get()) { |
335 PresentationServiceDelegateImpl::CreateForWebContents(initiator); | 423 media_router_ui->InitWithDefaultMediaSource( |
336 PresentationServiceDelegateImpl* delegate = | 424 delegate, |
337 PresentationServiceDelegateImpl::FromWebContents(initiator); | 425 new MediaRouterDialogCallbacks(presentation_request_.Pass(), delegate)); |
338 CHECK(delegate); | |
339 media_router_ui->InitWithDefaultMediaSource(delegate); | |
340 } else { | 426 } else { |
341 media_router_ui->InitWithPresentationSessionRequest( | 427 media_router_ui->InitWithPresentationSessionRequest( |
342 initiator, presentation_request_.Pass()); | 428 initiator, new MediaRouterDialogCallbacks(delegate)); |
343 } | 429 } |
344 } | 430 } |
345 | 431 |
346 } // namespace media_router | 432 } // namespace media_router |
347 | 433 |
OLD | NEW |