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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } | 83 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 // Reference back to the owning MediaRouterUI instance. | 86 // Reference back to the owning MediaRouterUI instance. |
| 87 MediaRouterUI* ui_; | 87 MediaRouterUI* ui_; |
| 88 | 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); | 89 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( | 92 MediaRouterUI::UIMediaRoutesObserver::UIMediaRoutesObserver( |
| 93 MediaRouter* router, | 93 MediaRouter* router, const MediaSource::Id& source_id, |
| 94 const RoutesUpdatedCallback& callback) | 94 const RoutesUpdatedCallback& callback) |
| 95 : MediaRoutesObserver(router), callback_(callback) { | 95 : MediaRoutesObserver(router, source_id), callback_(callback) { |
| 96 DCHECK(!callback_.is_null()); | 96 DCHECK(!callback_.is_null()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} | 99 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} |
| 100 | 100 |
| 101 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( | 101 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( |
| 102 const std::vector<MediaRoute>& routes) { | 102 const std::vector<MediaRoute>& routes, |
| 103 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 103 std::vector<MediaRoute> routes_for_display; | 104 std::vector<MediaRoute> routes_for_display; |
| 105 std::vector<MediaRoute::Id> joinable_route_ids_for_display; | |
| 104 for (const MediaRoute& route : routes) { | 106 for (const MediaRoute& route : routes) { |
| 105 if (route.for_display()) { | 107 if (route.for_display()) { |
| 106 #ifndef NDEBUG | 108 #ifndef NDEBUG |
| 107 for (const MediaRoute& existing_route : routes_for_display) { | 109 for (const MediaRoute& existing_route : routes_for_display) { |
| 108 if (existing_route.media_sink_id() == route.media_sink_id()) { | 110 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 109 DVLOG(2) << "Received another route for display with the same sink" | 111 DVLOG(2) << "Received another route for display with the same sink" |
| 110 << " id as an existing route. " | 112 << " id as an existing route. " |
| 111 << route.media_route_id() << " has the same sink id as " | 113 << route.media_route_id() << " has the same sink id as " |
| 112 << existing_route.media_sink_id() << "."; | 114 << existing_route.media_sink_id() << "."; |
| 113 } | 115 } |
| 114 } | 116 } |
| 115 #endif | 117 #endif |
| 118 if (std::find(joinable_route_ids.begin(), | |
| 119 joinable_route_ids.end(), route.media_route_id()) != | |
| 120 joinable_route_ids.end()) { | |
| 121 joinable_route_ids_for_display.push_back(route.media_route_id()); | |
| 122 } | |
| 123 | |
| 116 routes_for_display.push_back(route); | 124 routes_for_display.push_back(route); |
| 117 } | 125 } |
| 118 } | 126 } |
| 119 | 127 |
| 120 callback_.Run(routes_for_display); | 128 callback_.Run(routes_for_display, joinable_route_ids_for_display); |
| 121 } | 129 } |
| 122 | 130 |
| 123 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 131 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
| 124 : ConstrainedWebDialogUI(web_ui), | 132 : ConstrainedWebDialogUI(web_ui), |
| 125 handler_(new MediaRouterWebUIMessageHandler(this)), | 133 handler_(new MediaRouterWebUIMessageHandler(this)), |
| 126 ui_initialized_(false), | 134 ui_initialized_(false), |
| 127 requesting_route_for_default_source_(false), | 135 requesting_route_for_default_source_(false), |
| 128 current_route_request_id_(-1), | 136 current_route_request_id_(-1), |
| 129 route_request_counter_(0), | 137 route_request_counter_(0), |
| 130 initiator_(nullptr), | 138 initiator_(nullptr), |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 } | 210 } |
| 203 | 211 |
| 204 void MediaRouterUI::InitCommon(content::WebContents* initiator, | 212 void MediaRouterUI::InitCommon(content::WebContents* initiator, |
| 205 const MediaSource& default_source, | 213 const MediaSource& default_source, |
| 206 const GURL& default_frame_url) { | 214 const GURL& default_frame_url) { |
| 207 DCHECK(initiator); | 215 DCHECK(initiator); |
| 208 DCHECK(router_); | 216 DCHECK(router_); |
| 209 | 217 |
| 210 // Register for MediaRoute updates. | 218 // Register for MediaRoute updates. |
| 211 routes_observer_.reset(new UIMediaRoutesObserver( | 219 routes_observer_.reset(new UIMediaRoutesObserver( |
| 212 router_, | 220 router_, default_source.id(), |
|
imcheng
2015/12/01 23:45:06
The default source can change over the lifetime of
matt.boetger
2015/12/03 01:19:21
Done.
| |
| 213 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); | 221 base::Bind(&MediaRouterUI::OnRoutesUpdated, base::Unretained(this)))); |
| 214 | 222 |
| 215 query_result_manager_.reset(new QueryResultManager(router_)); | 223 query_result_manager_.reset(new QueryResultManager(router_)); |
| 216 query_result_manager_->AddObserver(this); | 224 query_result_manager_->AddObserver(this); |
| 217 | 225 |
| 218 // These modes are always available. | 226 // These modes are always available. |
| 219 query_result_manager_->StartSinksQuery( | 227 query_result_manager_->StartSinksQuery( |
| 220 MediaCastMode::DESKTOP_MIRROR, MediaSourceForDesktop()); | 228 MediaCastMode::DESKTOP_MIRROR, MediaSourceForDesktop()); |
| 221 initiator_ = initiator; | 229 initiator_ = initiator; |
| 222 MediaSource mirroring_source( | 230 MediaSource mirroring_source( |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 route_creation_timer_.Start( | 335 route_creation_timer_.Start( |
| 328 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), | 336 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), |
| 329 this, &MediaRouterUI::RouteCreationTimeout); | 337 this, &MediaRouterUI::RouteCreationTimeout); |
| 330 | 338 |
| 331 router_->CreateRoute(source.id(), sink_id, origin, | 339 router_->CreateRoute(source.id(), sink_id, origin, |
| 332 initiator_, | 340 initiator_, |
| 333 route_response_callbacks); | 341 route_response_callbacks); |
| 334 return true; | 342 return true; |
| 335 } | 343 } |
| 336 | 344 |
| 345 bool MediaRouterUI::JoinRoute(const MediaSink::Id& sink_id, | |
| 346 const MediaRoute::Id& route_id) { | |
| 347 DCHECK(query_result_manager_.get()); | |
| 348 DCHECK(initiator_); | |
| 349 | |
| 350 current_route_request_id_ = ++route_request_counter_; | |
| 351 GURL origin = frame_url_.GetOrigin(); | |
| 352 | |
| 353 MediaSource source = | |
| 354 query_result_manager_->GetSourceForCastMode(MediaCastMode::DEFAULT); | |
| 355 | |
| 356 if (source.Empty()) { | |
| 357 LOG(ERROR) << "No MediaSource to join"; | |
| 358 return false; | |
| 359 } | |
| 360 | |
| 361 DCHECK(origin.is_valid()); | |
|
imcheng
2015/12/01 23:45:06
Move this DCHECK to L352
matt.boetger
2015/12/03 01:19:21
Done.
| |
| 362 | |
| 363 std::vector<MediaRouteResponseCallback> route_response_callbacks; | |
| 364 route_response_callbacks.push_back(base::Bind( | |
| 365 &MediaRouterUI::OnRouteResponseReceived, weak_factory_.GetWeakPtr(), | |
| 366 current_route_request_id_, sink_id)); | |
| 367 | |
| 368 if (presentation_service_delegate_) { | |
| 369 route_response_callbacks.push_back( | |
| 370 base::Bind(&PresentationServiceDelegateImpl::OnRouteResponse, | |
| 371 presentation_service_delegate_)); | |
| 372 } | |
| 373 | |
| 374 // Start the timer. | |
| 375 route_creation_timer_.Start( | |
| 376 FROM_HERE, base::TimeDelta::FromSeconds(kCreateRouteTimeoutSeconds), this, | |
| 377 &MediaRouterUI::RouteCreationTimeout); | |
| 378 router_->JoinRouteByRouteId(source.id(), route_id, origin, | |
| 379 initiator_, route_response_callbacks); | |
| 380 return true; | |
| 381 } | |
| 382 | |
| 337 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { | 383 void MediaRouterUI::CloseRoute(const MediaRoute::Id& route_id) { |
| 338 router_->CloseRoute(route_id); | 384 router_->CloseRoute(route_id); |
| 339 } | 385 } |
| 340 | 386 |
| 341 void MediaRouterUI::AddIssue(const Issue& issue) { | 387 void MediaRouterUI::AddIssue(const Issue& issue) { |
| 342 router_->AddIssue(issue); | 388 router_->AddIssue(issue); |
| 343 } | 389 } |
| 344 | 390 |
| 345 void MediaRouterUI::ClearIssue(const std::string& issue_id) { | 391 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 346 router_->ClearIssue(issue_id); | 392 router_->ClearIssue(issue_id); |
| 347 } | 393 } |
| 348 | 394 |
| 349 void MediaRouterUI::OnResultsUpdated( | 395 void MediaRouterUI::OnResultsUpdated( |
| 350 const std::vector<MediaSinkWithCastModes>& sinks) { | 396 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 351 sinks_ = sinks; | 397 sinks_ = sinks; |
| 352 if (ui_initialized_) | 398 if (ui_initialized_) |
| 353 handler_->UpdateSinks(sinks_); | 399 handler_->UpdateSinks(sinks_); |
| 354 } | 400 } |
| 355 | 401 |
| 356 void MediaRouterUI::SetIssue(const Issue* issue) { | 402 void MediaRouterUI::SetIssue(const Issue* issue) { |
| 357 if (ui_initialized_) | 403 if (ui_initialized_) |
| 358 handler_->UpdateIssue(issue); | 404 handler_->UpdateIssue(issue); |
| 359 } | 405 } |
| 360 | 406 |
| 361 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { | 407 void MediaRouterUI::OnRoutesUpdated( |
| 408 const std::vector<MediaRoute>& routes, | |
| 409 const std::vector<MediaRoute::Id>& joinable_route_ids) { | |
| 362 routes_ = routes; | 410 routes_ = routes; |
| 411 joinable_route_ids_ = joinable_route_ids; | |
| 363 if (ui_initialized_) | 412 if (ui_initialized_) |
| 364 handler_->UpdateRoutes(routes_); | 413 handler_->UpdateRoutes(routes_, joinable_route_ids_); |
| 365 } | 414 } |
| 366 | 415 |
| 367 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, | 416 void MediaRouterUI::OnRouteResponseReceived(const int route_request_id, |
| 368 const MediaSink::Id& sink_id, | 417 const MediaSink::Id& sink_id, |
| 369 const MediaRoute* route, | 418 const MediaRoute* route, |
| 370 const std::string& presentation_id, | 419 const std::string& presentation_id, |
| 371 const std::string& error) { | 420 const std::string& error) { |
| 372 DVLOG(1) << "OnRouteResponseReceived"; | 421 DVLOG(1) << "OnRouteResponseReceived"; |
| 373 // If we receive a new route that we aren't expecting, do nothing. | 422 // If we receive a new route that we aren't expecting, do nothing. |
| 374 if (route_request_id != current_route_request_id_) | 423 if (route_request_id != current_route_request_id_) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 409 | 458 |
| 410 std::string MediaRouterUI::GetFrameURLHost() const { | 459 std::string MediaRouterUI::GetFrameURLHost() const { |
| 411 return GetHostFromURL(frame_url_); | 460 return GetHostFromURL(frame_url_); |
| 412 } | 461 } |
| 413 | 462 |
| 414 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { | 463 const std::string& MediaRouterUI::GetRouteProviderExtensionId() const { |
| 415 return router_->media_route_provider_extension_id(); | 464 return router_->media_route_provider_extension_id(); |
| 416 } | 465 } |
| 417 | 466 |
| 418 } // namespace media_router | 467 } // namespace media_router |
| OLD | NEW |