Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: chrome/browser/ui/webui/media_router/media_router_ui.cc

Issue 2558963003: Add GetCurrentRoutes() to MediaRouter API, ensure dialog has routes at init (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 const RoutesUpdatedCallback& callback) 128 const RoutesUpdatedCallback& callback)
129 : MediaRoutesObserver(router, source_id), callback_(callback) { 129 : MediaRoutesObserver(router, source_id), callback_(callback) {
130 DCHECK(!callback_.is_null()); 130 DCHECK(!callback_.is_null());
131 } 131 }
132 132
133 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {} 133 MediaRouterUI::UIMediaRoutesObserver::~UIMediaRoutesObserver() {}
134 134
135 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated( 135 void MediaRouterUI::UIMediaRoutesObserver::OnRoutesUpdated(
136 const std::vector<MediaRoute>& routes, 136 const std::vector<MediaRoute>& routes,
137 const std::vector<MediaRoute::Id>& joinable_route_ids) { 137 const std::vector<MediaRoute::Id>& joinable_route_ids) {
138 std::vector<MediaRoute> routes_for_display; 138 callback_.Run(routes, joinable_route_ids);
139 std::vector<MediaRoute::Id> joinable_route_ids_for_display;
140 for (const MediaRoute& route : routes) {
141 if (route.for_display()) {
142 #ifndef NDEBUG
143 for (const MediaRoute& existing_route : routes_for_display) {
144 if (existing_route.media_sink_id() == route.media_sink_id()) {
145 DVLOG(2) << "Received another route for display with the same sink"
146 << " id as an existing route. " << route.media_route_id()
147 << " has the same sink id as "
148 << existing_route.media_sink_id() << ".";
149 }
150 }
151 #endif
152 if (base::ContainsValue(joinable_route_ids, route.media_route_id())) {
153 joinable_route_ids_for_display.push_back(route.media_route_id());
154 }
155
156 routes_for_display.push_back(route);
157 }
158 }
159
160 callback_.Run(routes_for_display, joinable_route_ids_for_display);
161 } 139 }
162 140
163 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) 141 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui)
164 : ConstrainedWebDialogUI(web_ui), 142 : ConstrainedWebDialogUI(web_ui),
165 handler_(new MediaRouterWebUIMessageHandler(this)), 143 handler_(new MediaRouterWebUIMessageHandler(this)),
166 ui_initialized_(false), 144 ui_initialized_(false),
167 current_route_request_id_(-1), 145 current_route_request_id_(-1),
168 route_request_counter_(0), 146 route_request_counter_(0),
169 initiator_(nullptr), 147 initiator_(nullptr),
170 router_(nullptr), 148 router_(nullptr),
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 // Desktop mirror mode is always available. 268 // Desktop mirror mode is always available.
291 query_result_manager_->SetSourcesForCastMode( 269 query_result_manager_->SetSourcesForCastMode(
292 MediaCastMode::DESKTOP_MIRROR, {MediaSourceForDesktop()}, origin); 270 MediaCastMode::DESKTOP_MIRROR, {MediaSourceForDesktop()}, origin);
293 initiator_ = initiator; 271 initiator_ = initiator;
294 SessionID::id_type tab_id = SessionTabHelper::IdForTab(initiator); 272 SessionID::id_type tab_id = SessionTabHelper::IdForTab(initiator);
295 if (tab_id != -1) { 273 if (tab_id != -1) {
296 MediaSource mirroring_source(MediaSourceForTab(tab_id)); 274 MediaSource mirroring_source(MediaSourceForTab(tab_id));
297 query_result_manager_->SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, 275 query_result_manager_->SetSourcesForCastMode(MediaCastMode::TAB_MIRROR,
298 {mirroring_source}, origin); 276 {mirroring_source}, origin);
299 } 277 }
278
300 UpdateCastModes(); 279 UpdateCastModes();
280
281 // Get the current list of media routes, so that the WebUI will have routes
282 // information at initialization.
283 OnRoutesUpdated(router_->GetCurrentRoutes(), std::vector<MediaRoute::Id>());
301 } 284 }
302 285
303 void MediaRouterUI::InitForTest( 286 void MediaRouterUI::InitForTest(
304 MediaRouter* router, 287 MediaRouter* router,
305 content::WebContents* initiator, 288 content::WebContents* initiator,
306 MediaRouterWebUIMessageHandler* handler, 289 MediaRouterWebUIMessageHandler* handler,
307 std::unique_ptr<CreatePresentationConnectionRequest> 290 std::unique_ptr<CreatePresentationConnectionRequest>
308 create_session_request) { 291 create_session_request) {
309 router_ = router; 292 router_ = router;
310 handler_ = handler; 293 handler_ = handler;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 } 326 }
344 327
345 void MediaRouterUI::UpdateCastModes() { 328 void MediaRouterUI::UpdateCastModes() {
346 // Gets updated cast modes from |query_result_manager_| and forwards it to UI. 329 // Gets updated cast modes from |query_result_manager_| and forwards it to UI.
347 cast_modes_ = query_result_manager_->GetSupportedCastModes(); 330 cast_modes_ = query_result_manager_->GetSupportedCastModes();
348 if (ui_initialized_) { 331 if (ui_initialized_) {
349 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName()); 332 handler_->UpdateCastModes(cast_modes_, GetPresentationRequestSourceName());
350 } 333 }
351 } 334 }
352 335
336 void MediaRouterUI::UpdateRoutesToCastModesMapping() {
337 std::unordered_map<MediaSource::Id, MediaCastMode> available_source_map;
338 for (const auto& cast_mode : cast_modes_) {
339 for (const auto& source :
340 query_result_manager_->GetSourcesForCastMode(cast_mode)) {
341 available_source_map.insert(std::make_pair(source.id(), cast_mode));
342 }
343 }
344
345 routes_and_cast_modes_.clear();
346 for (const auto& route : routes_) {
347 auto source_entry = available_source_map.find(route.media_source().id());
348 if (source_entry != available_source_map.end()) {
349 routes_and_cast_modes_.insert(
350 std::make_pair(route.media_route_id(), source_entry->second));
351 }
352 }
353 }
354
353 void MediaRouterUI::Close() { 355 void MediaRouterUI::Close() {
354 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); 356 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
355 if (delegate) { 357 if (delegate) {
356 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); 358 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
357 delegate->OnDialogCloseFromWebUI(); 359 delegate->OnDialogCloseFromWebUI();
358 } 360 }
359 } 361 }
360 362
361 void MediaRouterUI::UIInitialized() { 363 void MediaRouterUI::UIInitialized() {
362 TRACE_EVENT_NESTABLE_ASYNC_END0("media_router", "UI", initiator_); 364 TRACE_EVENT_NESTABLE_ASYNC_END0("media_router", "UI", initiator_);
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 if (ui_initialized_) handler_->UpdateSinks(sinks_); 523 if (ui_initialized_) handler_->UpdateSinks(sinks_);
522 } 524 }
523 525
524 void MediaRouterUI::SetIssue(const Issue* issue) { 526 void MediaRouterUI::SetIssue(const Issue* issue) {
525 if (ui_initialized_) handler_->UpdateIssue(issue); 527 if (ui_initialized_) handler_->UpdateIssue(issue);
526 } 528 }
527 529
528 void MediaRouterUI::OnRoutesUpdated( 530 void MediaRouterUI::OnRoutesUpdated(
529 const std::vector<MediaRoute>& routes, 531 const std::vector<MediaRoute>& routes,
530 const std::vector<MediaRoute::Id>& joinable_route_ids) { 532 const std::vector<MediaRoute::Id>& joinable_route_ids) {
531 routes_ = routes; 533 routes_.clear();
532 joinable_route_ids_ = joinable_route_ids; 534 joinable_route_ids_.clear();
533 535
534 std::unordered_map<MediaSource::Id, MediaCastMode> available_source_map; 536 for (const MediaRoute& route : routes) {
535 for (const auto& cast_mode : cast_modes_) { 537 if (route.for_display()) {
536 for (const auto& source : 538 #ifndef NDEBUG
537 query_result_manager_->GetSourcesForCastMode(cast_mode)) { 539 for (const MediaRoute& existing_route : routes_) {
538 available_source_map.insert(std::make_pair(source.id(), cast_mode)); 540 if (existing_route.media_sink_id() == route.media_sink_id()) {
541 DVLOG(2) << "Received another route for display with the same sink"
542 << " id as an existing route. " << route.media_route_id()
543 << " has the same sink id as "
544 << existing_route.media_sink_id() << ".";
545 }
546 }
547 #endif
548 if (base::ContainsValue(joinable_route_ids, route.media_route_id())) {
549 joinable_route_ids_.push_back(route.media_route_id());
550 }
551
552 routes_.push_back(route);
539 } 553 }
540 } 554 }
541 555 UpdateRoutesToCastModesMapping();
542 current_cast_modes_.clear();
543 for (const auto& route : routes) {
544 auto source_entry = available_source_map.find(route.media_source().id());
545 if (source_entry != available_source_map.end()) {
546 current_cast_modes_.insert(
547 std::make_pair(route.media_route_id(), source_entry->second));
548 }
549 }
550 556
551 if (ui_initialized_) 557 if (ui_initialized_)
552 handler_->UpdateRoutes(routes_, joinable_route_ids_, current_cast_modes_); 558 handler_->UpdateRoutes(routes_, joinable_route_ids_,
559 routes_and_cast_modes_);
553 } 560 }
554 561
555 void MediaRouterUI::OnRouteResponseReceived( 562 void MediaRouterUI::OnRouteResponseReceived(
556 int route_request_id, 563 int route_request_id,
557 const MediaSink::Id& sink_id, 564 const MediaSink::Id& sink_id,
558 MediaCastMode cast_mode, 565 MediaCastMode cast_mode,
559 const base::string16& presentation_request_source_name, 566 const base::string16& presentation_request_source_name,
560 const RouteRequestResult& result) { 567 const RouteRequestResult& result) {
561 DVLOG(1) << "OnRouteResponseReceived"; 568 DVLOG(1) << "OnRouteResponseReceived";
562 // If we receive a new route that we aren't expecting, do nothing. 569 // If we receive a new route that we aren't expecting, do nothing.
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 base::Time::Now() - start_time_); 697 base::Time::Now() - start_time_);
691 start_time_ = base::Time(); 698 start_time_ = base::Time();
692 } 699 }
693 } 700 }
694 701
695 void MediaRouterUI::UpdateMaxDialogHeight(int height) { 702 void MediaRouterUI::UpdateMaxDialogHeight(int height) {
696 handler_->UpdateMaxDialogHeight(height); 703 handler_->UpdateMaxDialogHeight(height);
697 } 704 }
698 705
699 } // namespace media_router 706 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698