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_ui.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) | 50 if (base::StartsWith(host, "www.", base::CompareCase::INSENSITIVE_ASCII)) |
| 51 host = host.substr(4); | 51 host = host.substr(4); |
| 52 return host; | 52 return host; |
| 53 } | 53 } |
| 54 | 54 |
| 55 std::string GetTruncatedHostFromURL(const GURL& gurl) { | 55 std::string GetTruncatedHostFromURL(const GURL& gurl) { |
| 56 std::string host = GetHostFromURL(gurl); | 56 std::string host = GetHostFromURL(gurl); |
| 57 | 57 |
| 58 const std::string truncated = | 58 const std::string truncated = |
| 59 net::registry_controlled_domains::GetDomainAndRegistry( | 59 net::registry_controlled_domains::GetDomainAndRegistry( |
| 60 host, | 60 host, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 61 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 62 // The truncation will be empty in some scenarios (e.g. host is | 61 // The truncation will be empty in some scenarios (e.g. host is |
| 63 // simply an IP address). Fail gracefully. | 62 // simply an IP address). Fail gracefully. |
| 64 if (truncated.empty()) | 63 if (truncated.empty()) |
| 65 return host; | 64 return host; |
| 66 return truncated; | 65 return truncated; |
| 67 } | 66 } |
| 68 | 67 |
| 69 } // namespace | 68 } // namespace |
| 70 | 69 |
| 71 // This class calls to refresh the UI when the highest priority issue is | 70 // This class calls to refresh the UI when the highest priority issue is |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 100 | 99 |
| 101 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( | 100 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( |
| 102 const std::vector<MediaRoute>& routes) { | 101 const std::vector<MediaRoute>& routes) { |
| 103 std::vector<MediaRoute> routes_for_display; | 102 std::vector<MediaRoute> routes_for_display; |
| 104 for (const MediaRoute& route : routes) { | 103 for (const MediaRoute& route : routes) { |
| 105 if (route.for_display()) { | 104 if (route.for_display()) { |
| 106 #ifndef NDEBUG | 105 #ifndef NDEBUG |
| 107 for (const MediaRoute& existing_route : routes_for_display) { | 106 for (const MediaRoute& existing_route : routes_for_display) { |
| 108 if (existing_route.media_sink_id() == route.media_sink_id()) { | 107 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 109 DVLOG(2) << "Received another route for display with the same sink" | 108 DVLOG(2) << "Received another route for display with the same sink" |
| 110 << " id as an existing route. " | 109 << " id as an existing route. " << route.media_route_id() |
| 111 << route.media_route_id() << " has the same sink id as " | 110 << " has the same sink id as " |
| 112 << existing_route.media_sink_id() << "."; | 111 << existing_route.media_sink_id() << "."; |
| 113 } | 112 } |
| 114 } | 113 } |
| 115 #endif | 114 #endif |
| 116 routes_for_display.push_back(route); | 115 routes_for_display.push_back(route); |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 | 118 |
| 120 callback_.Run(routes_for_display); | 119 callback_.Run(routes_for_display); |
| 121 } | 120 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 | 208 |
| 210 // Register for MediaRoute updates. | 209 // Register for MediaRoute updates. |
| 211 routes_observer_.reset(new UIMediaRoutesObserver( | 210 routes_observer_.reset(new UIMediaRoutesObserver( |
| 212 router_, | 211 router_, |
| 213 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); | 212 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); |
| 214 | 213 |
| 215 query_result_manager_.reset(new QueryResultManager(router_)); | 214 query_result_manager_.reset(new QueryResultManager(router_)); |
| 216 query_result_manager_->AddObserver(this); | 215 query_result_manager_->AddObserver(this); |
| 217 | 216 |
| 218 // These modes are always available. | 217 // These modes are always available. |
| 219 query_result_manager_->StartSinksQuery( | 218 query_result_manager_->StartSinksQuery(MediaCastMode::DESKTOP_MIRROR, |
| 220 MediaCastMode::DESKTOP_MIRROR, MediaSourceForDesktop()); | 219 MediaSourceForDesktop()); |
| 221 initiator_ = initiator; | 220 initiator_ = initiator; |
| 222 MediaSource mirroring_source( | 221 MediaSource mirroring_source( |
| 223 MediaSourceForTab(SessionTabHelper::IdForTab(initiator))); | 222 MediaSourceForTab(SessionTabHelper::IdForTab(initiator))); |
| 224 query_result_manager_->StartSinksQuery( | 223 query_result_manager_->StartSinksQuery(MediaCastMode::TAB_MIRROR, |
| 225 MediaCastMode::TAB_MIRROR, mirroring_source); | 224 mirroring_source); |
| 226 | 225 |
| 227 OnDefaultMediaSourceChanged(default_source, default_frame_url); | 226 OnDefaultMediaSourceChanged(default_source, default_frame_url); |
| 228 } | 227 } |
| 229 | 228 |
| 230 void MediaRouterUI::OnDefaultMediaSourceChanged(const MediaSource& source, | 229 void MediaRouterUI::OnDefaultMediaSourceChanged(const MediaSource& source, |
| 231 const GURL& frame_url) { | 230 const GURL& frame_url) { |
| 232 if (source.Empty()) { | 231 if (source.Empty()) { |
| 233 query_result_manager_->StopSinksQuery(MediaCastMode::DEFAULT); | 232 query_result_manager_->StopSinksQuery(MediaCastMode::DEFAULT); |
| 234 } else { | 233 } else { |
| 235 query_result_manager_->StartSinksQuery(MediaCastMode::DEFAULT, source); | 234 query_result_manager_->StartSinksQuery(MediaCastMode::DEFAULT, source); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 // notification necessary. | 297 // notification necessary. |
| 299 // (2) Presentation route request for a Presentation API startSession call. | 298 // (2) Presentation route request for a Presentation API startSession call. |
| 300 // The startSession (CreatePresentationSessionRequest) will need to be | 299 // The startSession (CreatePresentationSessionRequest) will need to be |
| 301 // answered with the | 300 // answered with the |
| 302 // route response. | 301 // route response. |
| 303 // (3) Browser-initiated presentation route request. If successful, | 302 // (3) Browser-initiated presentation route request. If successful, |
| 304 // PresentationServiceDelegateImpl will have to be notified. Note that we | 303 // PresentationServiceDelegateImpl will have to be notified. Note that we |
| 305 // treat subsequent route requests from a Presentation API-initiated dialogs | 304 // treat subsequent route requests from a Presentation API-initiated dialogs |
| 306 // as browser-initiated. | 305 // as browser-initiated. |
| 307 std::vector<MediaRouteResponseCallback> route_response_callbacks; | 306 std::vector<MediaRouteResponseCallback> route_response_callbacks; |
| 308 route_response_callbacks.push_back( | 307 route_response_callbacks.push_back(base::Bind( |
| 309 base::Bind(&MediaRouterUI::OnRouteResponseReceived, | 308 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), |
| 310 weak_factory_.GetWeakPtr(), current_route_request_id_, | 309 current_route_request_id_, sink_id)); |
| 311 sink_id)); | |
| 312 if (requesting_route_for_default_source_) { | 310 if (requesting_route_for_default_source_) { |
| 313 if (presentation_request_) { | 311 if (presentation_request_) { |
| 314 // |presentation_request_| will be nullptr after this call, as the | 312 // |presentation_request_| will be nullptr after this call, as the |
| 315 // object will be transferred to the callback. | 313 // object will be transferred to the callback. |
| 316 route_response_callbacks.push_back( | 314 route_response_callbacks.push_back( |
| 317 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | 315 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, |
| 318 base::Passed(&presentation_request_))); | 316 base::Passed(&presentation_request_))); |
| 319 } else if (presentation_service_delegate_) { | 317 } else if (presentation_service_delegate_) { |
| 320 route_response_callbacks.push_back( | 318 route_response_callbacks.push_back( |
| 321 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | 319 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, |
| 322 presentation_service_delegate_)); | 320 presentation_service_delegate_)); |
| 323 } | 321 } |
| 324 } | 322 } |
| 325 | 323 |
| 326 // Start the timer. | 324 // Start the timer. |
| 327 route_creation_timer_.Start( | 325 route_creation_timer_.Start( |
| 328 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), | 326 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, |
| 329 this, &MediaRouterUI::RouteCreationTimeout); | 327 &MediaRouterUI::RouteCreationTimeout); |
| 330 | 328 |
| 331 router_->CreateRoute(source.id(), sink_id, origin, | 329 router_->CreateRoute(source.id(), sink_id, origin, |
| 332 SessionTabHelper::IdForTab(initiator_), | 330 SessionTabHelper::IdForTab(initiator_), |
| 333 route_response_callbacks); | 331 route_response_callbacks); |
| 334 return true; | 332 return true; |
| 335 } | 333 } |
| 336 | 334 |
| 335 bool MediaRouterUI::CanJoinRoute(const MediaRoute::Id& route_id) { | |
| 336 DCHECK(query_result_manager_.get()); | |
| 337 DCHECK(initiator_); | |
| 338 | |
| 339 DVLOG(0) << "MediaRouterUI::CanJoinRoute"; | |
| 340 return true; | |
| 341 } | |
| 342 | |
| 343 bool MediaRouterUI::JoinRoute(const MediaSink::Id& sink_id, | |
| 344 const MediaRoute::Id& route_id) { | |
| 345 DCHECK(query_result_manager_.get()); | |
| 346 DCHECK(initiator_); | |
| 347 | |
| 348 current_route_request_id_ = ++route_request_counter_; | |
| 349 GURL origin = frame_url_.GetOrigin(); | |
| 350 | |
| 351 MediaSource source = | |
| 352 query_result_manager_->GetSourceForCastMode(MediaCastMode::DEFAULT); | |
| 353 | |
| 354 if (source.Empty()) { | |
| 355 LOG(ERROR) << "No MediaSource to join"; | |
| 356 return false; | |
| 357 } | |
| 358 | |
| 359 DCHECK(origin.is_valid()); | |
| 360 | |
| 361 // There are 3 cases. In all cases the MediaRouterUI will need to be notified. | |
| 362 // (1) Non-presentation route request (e.g., mirroring). No additional | |
| 363 // notification necessary. | |
| 364 // (2) Presentation route request for a Presentation API startSession call. | |
| 365 // The startSession (CreatePresentationSessionRequest) will need to be | |
| 366 // answered with the | |
| 367 // route response. | |
| 368 // (3) Browser-initiated presentation route request. If successful, | |
| 369 // PresentationServiceDelegateImpl will have to be notified. Note that we | |
| 370 // treat subsequent route requests from a Presentation API-initiated dialogs | |
| 371 // as browser-initiated. | |
| 372 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 373 route_response_callbacks.push_back(base::Bind( | |
| 374 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), | |
| 375 current_route_request_id_, sink_id)); | |
| 376 if (presentation_request_) { | |
| 377 // |presentation_request_| will be nullptr after this call, as the | |
| 378 // object will be transferred to the callback. | |
| 379 route_response_callbacks.push_back( | |
|
imcheng
2015/11/03 19:28:03
We shouldn't need this callback. It's only for res
matt.boetger
2015/11/03 20:51:39
Yeah, this was obviously copy/pasted from CreateRo
| |
| 380 base::Bind(&CreatePresentationSessionRequest::HandleRouteResponse, | |
| 381 base::Passed(&presentation_request_))); | |
| 382 } else if (presentation_service_delegate_) { | |
| 383 route_response_callbacks.push_back( | |
| 384 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | |
| 385 presentation_service_delegate_)); | |
| 386 } | |
| 387 | |
| 388 // Start the timer. | |
| 389 route_creation_timer_.Start( | |
| 390 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, | |
| 391 &MediaRouterUI::RouteCreationTimeout); | |
| 392 std::string presentation_id("non-local-join_"); | |
| 393 presentation_id += route_id; | |
| 394 router_->JoinRoute(source.id(), presentation_id, origin, | |
| 395 SessionTabHelper::IdForTab(initiator_), | |
| 396 route_response_callbacks); | |
| 397 return true; | |
| 398 } | |
| 399 | |
| 337 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { | 400 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { |
| 338 router_->CloseRoute(route_id); | 401 router_->CloseRoute(route_id); |
| 339 } | 402 } |
| 340 | 403 |
| 341 void MediaRouterUI::AddIssue(const Issue& issue) { | 404 void MediaRouterUI::AddIssue(const Issue& issue) { |
| 342 router_->AddIssue(issue); | 405 router_->AddIssue(issue); |
| 343 } | 406 } |
| 344 | 407 |
| 345 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 408 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 346 router_->ClearIssue(issue_id); | 409 router_->ClearIssue(issue_id); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 363 if (ui_initialized_) | 426 if (ui_initialized_) |
| 364 handler_->UpdateRoutes(routes_); | 427 handler_->UpdateRoutes(routes_); |
| 365 } | 428 } |
| 366 | 429 |
| 367 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, | 430 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, |
| 368 const MediaSink::Id& sink_id, | 431 const MediaSink::Id& sink_id, |
| 369 const MediaRoute* route, | 432 const MediaRoute* route, |
| 370 const std::string& presentation_id, | 433 const std::string& presentation_id, |
| 371 const std::string& error) { | 434 const std::string& error) { |
| 372 DVLOG(1) << "OnRouteResponseReceived"; | 435 DVLOG(1) << "OnRouteResponseReceived"; |
| 436 | |
| 373 // If we receive a new route that we aren't expecting, do nothing. | 437 // If we receive a new route that we aren't expecting, do nothing. |
| 374 if (route_request_id != current_route_request_id_) | 438 if (route_request_id != current_route_request_id_) |
| 375 return; | 439 return; |
| 376 | 440 |
| 377 if (!route) { | 441 if (!route) { |
| 378 // The provider will handle sending an issue for a failed route request. | 442 // The provider will handle sending an issue for a failed route request. |
| 379 DVLOG(0) << "MediaRouteResponse returned error: " << error; | 443 DVLOG(0) << "MediaRouteResponse returned error: " << error; |
| 380 } | 444 } |
| 381 | 445 |
| 382 handler_->OnCreateRouteResponseReceived(sink_id, route); | 446 handler_->OnCreateRouteResponseReceived(sink_id, route); |
| 383 requesting_route_for_default_source_ = false; | 447 requesting_route_for_default_source_ = false; |
| 384 current_route_request_id_ = -1; | 448 current_route_request_id_ = -1; |
| 385 route_creation_timer_.Stop(); | 449 route_creation_timer_.Stop(); |
| 386 } | 450 } |
| 387 | 451 |
| 388 void MediaRouterUI::RouteCreationTimeout() { | 452 void MediaRouterUI::RouteCreationTimeout() { |
| 389 requesting_route_for_default_source_ = false; | 453 requesting_route_for_default_source_ = false; |
| 390 current_route_request_id_ = -1; | 454 current_route_request_id_ = -1; |
| 391 | 455 |
| 392 base::string16 host = base::UTF8ToUTF16(GetTruncatedHostFromURL(frame_url_)); | 456 base::string16 host = base::UTF8ToUTF16(GetTruncatedHostFromURL(frame_url_)); |
| 393 | 457 |
| 394 // TODO(apacible): Update error messages based on current cast mode | 458 // TODO(apacible): Update error messages based on current cast mode |
| 395 // (e.g. desktop). | 459 // (e.g. desktop). |
| 396 std::string issue_title = host.empty() ? | 460 std::string issue_title = |
| 397 l10n_util::GetStringUTF8( | 461 host.empty() ? l10n_util::GetStringUTF8( |
| 398 IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT_FOR_TAB) : | 462 IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT_FOR_TAB) |
| 399 l10n_util::GetStringFUTF8(IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, | 463 : l10n_util::GetStringFUTF8( |
| 400 host); | 464 IDS_MEDIA_ROUTER_ISSUE_CREATE_ROUTE_TIMEOUT, host); |
| 401 | 465 |
| 402 Issue issue(issue_title, std::string(), | 466 Issue issue(issue_title, std::string(), |
| 403 IssueAction(IssueAction::TYPE_DISMISS), | 467 IssueAction(IssueAction::TYPE_DISMISS), |
| 404 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, | 468 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, |
| 405 false, std::string()); | 469 false, std::string()); |
| 406 AddIssue(issue); | 470 AddIssue(issue); |
| 407 handler_->NotifyRouteCreationTimeout(); | 471 handler_->NotifyRouteCreationTimeout(); |
| 408 } | 472 } |
| 409 | 473 |
| 410 std::string MediaRouterUI::GetFrameURLHost() const { | 474 std::string MediaRouterUI::GetFrameURLHost() const { |
| 411 return GetHostFromURL(frame_url_); | 475 return GetHostFromURL(frame_url_); |
| 412 } | 476 } |
| 413 | 477 |
| 414 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 478 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
| 415 return router_->media_route_provider_extension_id(); | 479 return router_->media_route_provider_extension_id(); |
| 416 } | 480 } |
| 417 | 481 |
| 418 } // namespace media_router | 482 } // namespace media_router |
| OLD | NEW |