| 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/media/router/media_router_mojo_impl.h" | 5 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/media/router/issues_observer.h" | 13 #include "chrome/browser/media/router/issues_observer.h" |
| 14 #include "chrome/browser/media/router/local_media_routes_observer.h" |
| 14 #include "chrome/browser/media/router/media_router_factory.h" | 15 #include "chrome/browser/media/router/media_router_factory.h" |
| 15 #include "chrome/browser/media/router/media_router_type_converters.h" | 16 #include "chrome/browser/media/router/media_router_type_converters.h" |
| 16 #include "chrome/browser/media/router/media_routes_observer.h" | 17 #include "chrome/browser/media/router/media_routes_observer.h" |
| 17 #include "chrome/browser/media/router/media_sinks_observer.h" | 18 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 18 #include "chrome/browser/media/router/presentation_session_messages_observer.h" | 19 #include "chrome/browser/media/router/presentation_session_messages_observer.h" |
| 19 #include "extensions/browser/process_manager.h" | 20 #include "extensions/browser/process_manager.h" |
| 20 | 21 |
| 21 #define DVLOG_WITH_INSTANCE(level) \ | 22 #define DVLOG_WITH_INSTANCE(level) \ |
| 22 DVLOG(level) << "MR #" << instance_id_ << ": " | 23 DVLOG(level) << "MR #" << instance_id_ << ": " |
| 23 | 24 |
| 24 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " | 25 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " |
| 25 | 26 |
| 26 namespace media_router { | 27 namespace media_router { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() | |
| 30 // into a local callback. | |
| 31 void RouteResponseReceived( | |
| 32 const std::string& presentation_id, | |
| 33 const std::vector<MediaRouteResponseCallback>& callbacks, | |
| 34 interfaces::MediaRoutePtr media_route, | |
| 35 const mojo::String& error_text) { | |
| 36 scoped_ptr<MediaRoute> route; | |
| 37 std::string actual_presentation_id; | |
| 38 std::string error; | |
| 39 if (media_route.is_null()) { | |
| 40 // An error occurred. | |
| 41 DCHECK(!error_text.is_null()); | |
| 42 error = !error_text.get().empty() ? error_text.get() : "Unknown error."; | |
| 43 } else { | |
| 44 route = media_route.To<scoped_ptr<MediaRoute>>(); | |
| 45 actual_presentation_id = presentation_id; | |
| 46 } | |
| 47 | |
| 48 for (const MediaRouteResponseCallback& callback : callbacks) | |
| 49 callback.Run(route.get(), actual_presentation_id, error); | |
| 50 } | |
| 51 | |
| 52 // TODO(imcheng): We should handle failure in this case. One way is to invoke | 30 // TODO(imcheng): We should handle failure in this case. One way is to invoke |
| 53 // all pending requests with failure. (crbug.com/490787) | 31 // all pending requests with failure. (crbug.com/490787) |
| 54 void EventPageWakeComplete(bool success) { | 32 void EventPageWakeComplete(bool success) { |
| 55 if (!success) | 33 if (!success) |
| 56 LOG(ERROR) << "An error encountered while waking the event page."; | 34 LOG(ERROR) << "An error encountered while waking the event page."; |
| 57 } | 35 } |
| 58 | 36 |
| 59 scoped_ptr<content::PresentationSessionMessage> | 37 scoped_ptr<content::PresentationSessionMessage> |
| 60 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { | 38 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { |
| 61 DCHECK(!input.is_null()); | 39 DCHECK(!input.is_null()); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 routes_converted.reserve(routes.size()); | 162 routes_converted.reserve(routes.size()); |
| 185 | 163 |
| 186 for (size_t i = 0; i < routes.size(); ++i) { | 164 for (size_t i = 0; i < routes.size(); ++i) { |
| 187 routes_converted.push_back(routes[i].To<MediaRoute>()); | 165 routes_converted.push_back(routes[i].To<MediaRoute>()); |
| 188 } | 166 } |
| 189 | 167 |
| 190 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, | 168 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, |
| 191 OnRoutesUpdated(routes_converted)); | 169 OnRoutesUpdated(routes_converted)); |
| 192 } | 170 } |
| 193 | 171 |
| 172 void MediaRouterMojoImpl::RouteResponseReceived( |
| 173 const std::string& presentation_id, |
| 174 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 175 interfaces::MediaRoutePtr media_route, |
| 176 const mojo::String& error_text) { |
| 177 scoped_ptr<MediaRoute> route; |
| 178 std::string actual_presentation_id; |
| 179 std::string error; |
| 180 if (media_route.is_null()) { |
| 181 // An error occurred. |
| 182 DCHECK(!error_text.is_null()); |
| 183 error = !error_text.get().empty() ? error_text.get() : "Unknown error."; |
| 184 } else { |
| 185 route = media_route.To<scoped_ptr<MediaRoute>>(); |
| 186 actual_presentation_id = presentation_id; |
| 187 FOR_EACH_OBSERVER(LocalMediaRoutesObserver, local_routes_observers_, |
| 188 OnLocalRouteCreated()); |
| 189 } |
| 190 |
| 191 for (const MediaRouteResponseCallback& callback : callbacks) |
| 192 callback.Run(route.get(), actual_presentation_id, error); |
| 193 } |
| 194 |
| 194 void MediaRouterMojoImpl::CreateRoute( | 195 void MediaRouterMojoImpl::CreateRoute( |
| 195 const MediaSource::Id& source_id, | 196 const MediaSource::Id& source_id, |
| 196 const MediaSink::Id& sink_id, | 197 const MediaSink::Id& sink_id, |
| 197 const GURL& origin, | 198 const GURL& origin, |
| 198 int tab_id, | 199 int tab_id, |
| 199 const std::vector<MediaRouteResponseCallback>& callbacks) { | 200 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 200 DCHECK(thread_checker_.CalledOnValidThread()); | 201 DCHECK(thread_checker_.CalledOnValidThread()); |
| 201 | 202 |
| 202 if (!origin.is_valid()) { | 203 if (!origin.is_valid()) { |
| 203 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; | 204 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 return; | 386 return; |
| 386 | 387 |
| 387 observer_list->RemoveObserver(observer); | 388 observer_list->RemoveObserver(observer); |
| 388 if (!observer_list->might_have_observers()) { | 389 if (!observer_list->might_have_observers()) { |
| 389 messages_observers_.erase(route_id); | 390 messages_observers_.erase(route_id); |
| 390 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopListeningForRouteMessages, | 391 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopListeningForRouteMessages, |
| 391 base::Unretained(this), route_id)); | 392 base::Unretained(this), route_id)); |
| 392 } | 393 } |
| 393 } | 394 } |
| 394 | 395 |
| 396 void MediaRouterMojoImpl::RegisterLocalMediaRoutesObserver( |
| 397 LocalMediaRoutesObserver* observer) { |
| 398 DCHECK(thread_checker_.CalledOnValidThread()); |
| 399 DCHECK(observer); |
| 400 |
| 401 DCHECK(!local_routes_observers_.HasObserver(observer)); |
| 402 local_routes_observers_.AddObserver(observer); |
| 403 } |
| 404 |
| 405 void MediaRouterMojoImpl::UnregisterLocalMediaRoutesObserver( |
| 406 LocalMediaRoutesObserver* observer) { |
| 407 DCHECK(thread_checker_.CalledOnValidThread()); |
| 408 DCHECK(observer); |
| 409 |
| 410 if (!local_routes_observers_.HasObserver(observer)) |
| 411 return; |
| 412 local_routes_observers_.RemoveObserver(observer); |
| 413 } |
| 414 |
| 395 void MediaRouterMojoImpl::DoCreateRoute( | 415 void MediaRouterMojoImpl::DoCreateRoute( |
| 396 const MediaSource::Id& source_id, | 416 const MediaSource::Id& source_id, |
| 397 const MediaSink::Id& sink_id, | 417 const MediaSink::Id& sink_id, |
| 398 const std::string& origin, | 418 const std::string& origin, |
| 399 int tab_id, | 419 int tab_id, |
| 400 const std::vector<MediaRouteResponseCallback>& callbacks) { | 420 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 401 std::string presentation_id("mr_"); | 421 std::string presentation_id("mr_"); |
| 402 presentation_id += base::GenerateGUID(); | 422 presentation_id += base::GenerateGUID(); |
| 403 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id | 423 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id |
| 404 << ", presentation ID: " << presentation_id; | 424 << ", presentation ID: " << presentation_id; |
| 405 media_route_provider_->CreateRoute( | 425 media_route_provider_->CreateRoute( |
| 406 source_id, sink_id, presentation_id, origin, tab_id, | 426 source_id, sink_id, presentation_id, origin, tab_id, |
| 407 base::Bind(&RouteResponseReceived, presentation_id, callbacks)); | 427 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 428 base::Unretained(this), presentation_id, callbacks)); |
| 408 } | 429 } |
| 409 | 430 |
| 410 void MediaRouterMojoImpl::DoJoinRoute( | 431 void MediaRouterMojoImpl::DoJoinRoute( |
| 411 const MediaSource::Id& source_id, | 432 const MediaSource::Id& source_id, |
| 412 const std::string& presentation_id, | 433 const std::string& presentation_id, |
| 413 const std::string& origin, | 434 const std::string& origin, |
| 414 int tab_id, | 435 int tab_id, |
| 415 const std::vector<MediaRouteResponseCallback>& callbacks) { | 436 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 416 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id | 437 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id |
| 417 << ", presentation ID: " << presentation_id; | 438 << ", presentation ID: " << presentation_id; |
| 418 media_route_provider_->JoinRoute( | 439 media_route_provider_->JoinRoute( |
| 419 source_id, presentation_id, origin, tab_id, | 440 source_id, presentation_id, origin, tab_id, |
| 420 base::Bind(&RouteResponseReceived, presentation_id, callbacks)); | 441 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 442 base::Unretained(this), presentation_id, callbacks)); |
| 421 } | 443 } |
| 422 | 444 |
| 423 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { | 445 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { |
| 424 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; | 446 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; |
| 425 media_route_provider_->CloseRoute(route_id); | 447 media_route_provider_->CloseRoute(route_id); |
| 426 } | 448 } |
| 427 | 449 |
| 428 void MediaRouterMojoImpl::DoSendSessionMessage( | 450 void MediaRouterMojoImpl::DoSendSessionMessage( |
| 429 const MediaRoute::Id& route_id, | 451 const MediaRoute::Id& route_id, |
| 430 const std::string& message, | 452 const std::string& message, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 return; | 605 return; |
| 584 } | 606 } |
| 585 | 607 |
| 586 for (const auto& next_request : pending_requests_) | 608 for (const auto& next_request : pending_requests_) |
| 587 next_request.Run(); | 609 next_request.Run(); |
| 588 | 610 |
| 589 pending_requests_.clear(); | 611 pending_requests_.clear(); |
| 590 } | 612 } |
| 591 | 613 |
| 592 } // namespace media_router | 614 } // namespace media_router |
| OLD | NEW |