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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 const RoutesUpdatedCallback& callback) | 132 const RoutesUpdatedCallback& callback) |
| 133 : MediaRoutesObserver(router, source_id), callback_(callback) { | 133 : MediaRoutesObserver(router, source_id), callback_(callback) { |
| 134 DCHECK(!callback_.is_null()); | 134 DCHECK(!callback_.is_null()); |
| 135 } | 135 } |
| 136 | 136 |
| 137 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} | 137 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} |
| 138 | 138 |
| 139 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( | 139 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( |
| 140 const std::vector<MediaRoute>& routes, | 140 const std::vector<MediaRoute>& routes, |
| 141 const std::vector<MediaRoute::Id>& joinable_route_ids) { | 141 const std::vector<MediaRoute::Id>& joinable_route_ids) { |
| 142 std::vector<MediaRoute> routes_for_display; | 142 callback_.Run(routes, joinable_route_ids); |
| 143 std::vector<MediaRoute::Id> joinable_route_ids_for_display; | |
| 144 for (const MediaRoute& route : routes) { | |
| 145 if (route.for_display()) { | |
| 146 #ifndef NDEBUG | |
| 147 for (const MediaRoute& existing_route : routes_for_display) { | |
| 148 if (existing_route.media_sink_id() == route.media_sink_id()) { | |
| 149 DVLOG(2) << "Received another route for display with the same sink" | |
| 150 << " id as an existing route. " << route.media_route_id() | |
| 151 << " has the same sink id as " | |
| 152 << existing_route.media_sink_id() << "."; | |
| 153 } | |
| 154 } | |
| 155 #endif | |
| 156 if (base::ContainsValue(joinable_route_ids, route.media_route_id())) { | |
| 157 joinable_route_ids_for_display.push_back(route.media_route_id()); | |
| 158 } | |
| 159 | |
| 160 routes_for_display.push_back(route); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 callback_.Run(routes_for_display, joinable_route_ids_for_display); | |
| 165 } | 143 } |
| 166 | 144 |
| 167 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 145 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
| 168 : ConstrainedWebDialogUI(web_ui), | 146 : ConstrainedWebDialogUI(web_ui), |
| 169 handler_(new MediaRouterWebUIMessageHandler(this)), | 147 handler_(new MediaRouterWebUIMessageHandler(this)), |
| 170 ui_initialized_(false), | 148 ui_initialized_(false), |
| 171 current_route_request_id_(-1), | 149 current_route_request_id_(-1), |
| 172 route_request_counter_(0), | 150 route_request_counter_(0), |
| 173 initiator_(nullptr), | 151 initiator_(nullptr), |
| 174 router_(nullptr), | 152 router_(nullptr), |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 // Desktop mirror mode is always available. | 272 // Desktop mirror mode is always available. |
| 295 query_result_manager_->SetSourcesForCastMode( | 273 query_result_manager_->SetSourcesForCastMode( |
| 296 MediaCastMode::DESKTOP_MIRROR, {MediaSourceForDesktop()}, origin); | 274 MediaCastMode::DESKTOP_MIRROR, {MediaSourceForDesktop()}, origin); |
| 297 initiator_ = initiator; | 275 initiator_ = initiator; |
| 298 SessionID::id_type tab_id = SessionTabHelper::IdForTab(initiator); | 276 SessionID::id_type tab_id = SessionTabHelper::IdForTab(initiator); |
| 299 if (tab_id != -1) { | 277 if (tab_id != -1) { |
| 300 MediaSource mirroring_source(MediaSourceForTab(tab_id)); | 278 MediaSource mirroring_source(MediaSourceForTab(tab_id)); |
| 301 query_result_manager_->SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, | 279 query_result_manager_->SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, |
| 302 {mirroring_source}, origin); | 280 {mirroring_source}, origin); |
| 303 } | 281 } |
| 282 | |
| 304 UpdateCastModes(); | 283 UpdateCastModes(); |
| 284 | |
| 285 // Get the current list of media routes, so that the WebUI will have routes | |
| 286 // information at initialization. | |
| 287 OnRoutesUpdated(router_->GetCurrentRoutes(), std::vector<MediaRoute::Id>()); | |
| 305 } | 288 } |
| 306 | 289 |
| 307 void MediaRouterUI::InitForTest( | 290 void MediaRouterUI::InitForTest( |
| 308 MediaRouter* router, | 291 MediaRouter* router, |
| 309 content::WebContents* initiator, | 292 content::WebContents* initiator, |
| 310 MediaRouterWebUIMessageHandler* handler, | 293 MediaRouterWebUIMessageHandler* handler, |
| 311 std::unique_ptr<CreatePresentationConnectionRequest> | 294 std::unique_ptr<CreatePresentationConnectionRequest> |
| 312 create_session_request) { | 295 create_session_request) { |
| 313 router_ = router; | 296 router_ = router; |
| 314 handler_ = handler; | 297 handler_ = handler; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 } | 330 } |
| 348 | 331 |
| 349 void MediaRouterUI::UpdateCastModes() { | 332 void MediaRouterUI::UpdateCastModes() { |
| 350 // Gets updated cast modes from |query_result_manager_| and forwards it to UI. | 333 // Gets updated cast modes from |query_result_manager_| and forwards it to UI. |
| 351 cast_modes_ = query_result_manager_->GetSupportedCastModes(); | 334 cast_modes_ = query_result_manager_->GetSupportedCastModes(); |
| 352 if (ui_initialized_) { | 335 if (ui_initialized_) { |
| 353 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName()); | 336 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName()); |
| 354 } | 337 } |
| 355 } | 338 } |
| 356 | 339 |
| 340 void MediaRouterUI::UpdateRoutesToCastModesMapping() { | |
| 341 std::unordered_map<MediaSource::Id, MediaCastMode> available_source_map; | |
| 342 for (const auto& cast_mode : cast_modes_) { | |
| 343 for (const auto& source : | |
| 344 query_result_manager_->GetSourcesForCastMode(cast_mode)) { | |
| 345 available_source_map.insert(std::make_pair(source.id(), cast_mode)); | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 routes_and_cast_modes_.clear(); | |
| 350 for (const auto& route : routes_) { | |
| 351 auto source_entry = available_source_map.find(route.media_source().id()); | |
| 352 if (source_entry != available_source_map.end()) { | |
| 353 routes_and_cast_modes_.insert( | |
|
mark a. foltz
2016/12/02 23:29:44
I see this is existing code refactored, I just don
takumif
2016/12/03 00:11:25
It's for hiding the "Cast" button in dialog if cli
| |
| 354 std::make_pair(route.media_route_id(), source_entry->second)); | |
| 355 } | |
| 356 } | |
| 357 } | |
| 358 | |
| 357 void MediaRouterUI::Close() { | 359 void MediaRouterUI::Close() { |
| 358 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); | 360 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); |
| 359 if (delegate) { | 361 if (delegate) { |
| 360 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); | 362 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); |
| 361 delegate->OnDialogCloseFromWebUI(); | 363 delegate->OnDialogCloseFromWebUI(); |
| 362 } | 364 } |
| 363 } | 365 } |
| 364 | 366 |
| 365 void MediaRouterUI::UIInitialized() { | 367 void MediaRouterUI::UIInitialized() { |
| 366 TRACE_EVENT_NESTABLE_ASYNC_END0("media_router", "UI", initiator_); | 368 TRACE_EVENT_NESTABLE_ASYNC_END0("media_router", "UI", initiator_); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 if (ui_initialized_) handler_->UpdateSinks(sinks_); | 558 if (ui_initialized_) handler_->UpdateSinks(sinks_); |
| 557 } | 559 } |
| 558 | 560 |
| 559 void MediaRouterUI::SetIssue(const Issue* issue) { | 561 void MediaRouterUI::SetIssue(const Issue* issue) { |
| 560 if (ui_initialized_) handler_->UpdateIssue(issue); | 562 if (ui_initialized_) handler_->UpdateIssue(issue); |
| 561 } | 563 } |
| 562 | 564 |
| 563 void MediaRouterUI::OnRoutesUpdated( | 565 void MediaRouterUI::OnRoutesUpdated( |
| 564 const std::vector<MediaRoute>& routes, | 566 const std::vector<MediaRoute>& routes, |
| 565 const std::vector<MediaRoute::Id>& joinable_route_ids) { | 567 const std::vector<MediaRoute::Id>& joinable_route_ids) { |
| 566 routes_ = routes; | 568 routes_.clear(); |
| 567 joinable_route_ids_ = joinable_route_ids; | 569 joinable_route_ids_.clear(); |
| 568 | 570 |
| 569 std::unordered_map<MediaSource::Id, MediaCastMode> available_source_map; | 571 for (const MediaRoute& route : routes) { |
| 570 for (const auto& cast_mode : cast_modes_) { | 572 if (route.for_display()) { |
| 571 for (const auto& source : | 573 #ifndef NDEBUG |
| 572 query_result_manager_->GetSourcesForCastMode(cast_mode)) { | 574 for (const MediaRoute& existing_route : routes_) { |
| 573 available_source_map.insert(std::make_pair(source.id(), cast_mode)); | 575 if (existing_route.media_sink_id() == route.media_sink_id()) { |
| 576 DVLOG(2) << "Received another route for display with the same sink" | |
| 577 << " id as an existing route. " << route.media_route_id() | |
| 578 << " has the same sink id as " | |
| 579 << existing_route.media_sink_id() << "."; | |
| 580 } | |
| 581 } | |
| 582 #endif | |
| 583 if (base::ContainsValue(joinable_route_ids, route.media_route_id())) { | |
| 584 joinable_route_ids_.push_back(route.media_route_id()); | |
| 585 } | |
| 586 | |
| 587 routes_.push_back(route); | |
| 574 } | 588 } |
| 575 } | 589 } |
| 576 | 590 UpdateRoutesToCastModesMapping(); |
| 577 current_cast_modes_.clear(); | |
| 578 for (const auto& route : routes) { | |
| 579 auto source_entry = available_source_map.find(route.media_source().id()); | |
| 580 if (source_entry != available_source_map.end()) { | |
| 581 current_cast_modes_.insert( | |
| 582 std::make_pair(route.media_route_id(), source_entry->second)); | |
| 583 } | |
| 584 } | |
| 585 | 591 |
| 586 if (ui_initialized_) | 592 if (ui_initialized_) |
| 587 handler_->UpdateRoutes(routes_, joinable_route_ids_, current_cast_modes_); | 593 handler_->UpdateRoutes(routes_, joinable_route_ids_, |
| 594 routes_and_cast_modes_); | |
| 588 } | 595 } |
| 589 | 596 |
| 590 void MediaRouterUI::OnRouteResponseReceived( | 597 void MediaRouterUI::OnRouteResponseReceived( |
| 591 int route_request_id, | 598 int route_request_id, |
| 592 const MediaSink::Id& sink_id, | 599 const MediaSink::Id& sink_id, |
| 593 MediaCastMode cast_mode, | 600 MediaCastMode cast_mode, |
| 594 const base::string16& presentation_request_source_name, | 601 const base::string16& presentation_request_source_name, |
| 595 const RouteRequestResult& result) { | 602 const RouteRequestResult& result) { |
| 596 DVLOG(1) << "OnRouteResponseReceived"; | 603 DVLOG(1) << "OnRouteResponseReceived"; |
| 597 // If we receive a new route that we aren't expecting, do nothing. | 604 // If we receive a new route that we aren't expecting, do nothing. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 } | 739 } |
| 733 | 740 |
| 734 std::string MediaRouterUI::GetSerializedInitiatorOrigin() const { | 741 std::string MediaRouterUI::GetSerializedInitiatorOrigin() const { |
| 735 url::Origin origin = initiator_ | 742 url::Origin origin = initiator_ |
| 736 ? url::Origin(initiator_->GetLastCommittedURL()) | 743 ? url::Origin(initiator_->GetLastCommittedURL()) |
| 737 : url::Origin(); | 744 : url::Origin(); |
| 738 return origin.Serialize(); | 745 return origin.Serialize(); |
| 739 } | 746 } |
| 740 | 747 |
| 741 } // namespace media_router | 748 } // namespace media_router |
| OLD | NEW |