| 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/media_router_factory.h" | 14 #include "chrome/browser/media/router/media_router_factory.h" |
| 15 #include "chrome/browser/media/router/media_router_type_converters.h" | 15 #include "chrome/browser/media/router/media_router_type_converters.h" |
| 16 #include "chrome/browser/media/router/media_routes_observer.h" | 16 #include "chrome/browser/media/router/media_routes_observer.h" |
| 17 #include "chrome/browser/media/router/media_sinks_observer.h" | 17 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 18 #include "extensions/browser/process_manager.h" | 18 #include "extensions/browser/process_manager.h" |
| 19 | 19 |
| 20 #define DVLOG_WITH_INSTANCE(level) \ | 20 #define DVLOG_WITH_INSTANCE(level) \ |
| 21 DVLOG(level) << "MR #" << instance_id_ << ": " | 21 DVLOG(level) << "MR #" << instance_id_ << ": " |
| 22 | 22 |
| 23 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " | 23 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " |
| 24 | 24 |
| 25 namespace media_router { | 25 namespace media_router { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() | |
| 29 // into a local callback. | |
| 30 void RouteResponseReceived( | |
| 31 const std::vector<MediaRouteResponseCallback>& callbacks, | |
| 32 interfaces::MediaRoutePtr media_route, | |
| 33 const mojo::String& error_text) { | |
| 34 scoped_ptr<MediaRoute> route; | |
| 35 std::string error; | |
| 36 if (media_route.is_null()) { | |
| 37 // An error occurred. | |
| 38 DCHECK(!error_text.is_null()); | |
| 39 error = !error_text.get().empty() ? error_text.get() : "Unknown error."; | |
| 40 } else { | |
| 41 route = media_route.To<scoped_ptr<MediaRoute>>(); | |
| 42 } | |
| 43 | |
| 44 for (const MediaRouteResponseCallback& callback : callbacks) | |
| 45 callback.Run(route.get(), error); | |
| 46 } | |
| 47 | |
| 48 // TODO(imcheng): We should handle failure in this case. One way is to invoke | 28 // TODO(imcheng): We should handle failure in this case. One way is to invoke |
| 49 // all pending requests with failure. (crbug.com/490787) | 29 // all pending requests with failure. (crbug.com/490787) |
| 50 void EventPageWakeComplete(bool success) { | 30 void EventPageWakeComplete(bool success) { |
| 51 if (!success) | 31 if (!success) |
| 52 LOG(ERROR) << "An error encountered while waking the event page."; | 32 LOG(ERROR) << "An error encountered while waking the event page."; |
| 53 } | 33 } |
| 54 | 34 |
| 55 scoped_ptr<content::PresentationSessionMessage> | 35 scoped_ptr<content::PresentationSessionMessage> |
| 56 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { | 36 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { |
| 57 DCHECK(!input.is_null()); | 37 DCHECK(!input.is_null()); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 int tab_id, | 177 int tab_id, |
| 198 const std::vector<MediaRouteResponseCallback>& callbacks) { | 178 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); | 179 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 | 180 |
| 201 if (!origin.is_valid()) { | 181 if (!origin.is_valid()) { |
| 202 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; | 182 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
| 203 for (const MediaRouteResponseCallback& callback : callbacks) | 183 for (const MediaRouteResponseCallback& callback : callbacks) |
| 204 callback.Run(nullptr, "Invalid origin"); | 184 callback.Run(nullptr, "Invalid origin"); |
| 205 return; | 185 return; |
| 206 } | 186 } |
| 187 |
| 188 sink_ids_in_launch_.insert(sink_id); |
| 189 |
| 207 RunOrDefer(base::Bind( | 190 RunOrDefer(base::Bind( |
| 208 &MediaRouterMojoImpl::DoCreateRoute, base::Unretained(this), source_id, | 191 &MediaRouterMojoImpl::DoCreateRoute, base::Unretained(this), source_id, |
| 209 sink_id, origin.is_empty() ? "" : origin.spec(), tab_id, callbacks)); | 192 sink_id, origin.is_empty() ? "" : origin.spec(), tab_id, callbacks)); |
| 210 } | 193 } |
| 211 | 194 |
| 212 void MediaRouterMojoImpl::JoinRoute( | 195 void MediaRouterMojoImpl::JoinRoute( |
| 213 const MediaSource::Id& source_id, | 196 const MediaSource::Id& source_id, |
| 214 const std::string& presentation_id, | 197 const std::string& presentation_id, |
| 215 const GURL& origin, | 198 const GURL& origin, |
| 216 int tab_id, | 199 int tab_id, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 const MediaSink::Id& sink_id, | 338 const MediaSink::Id& sink_id, |
| 356 const std::string& origin, | 339 const std::string& origin, |
| 357 int tab_id, | 340 int tab_id, |
| 358 const std::vector<MediaRouteResponseCallback>& callbacks) { | 341 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 359 std::string presentation_id("mr_"); | 342 std::string presentation_id("mr_"); |
| 360 presentation_id += base::GenerateGUID(); | 343 presentation_id += base::GenerateGUID(); |
| 361 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id | 344 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id |
| 362 << ", presentation ID: " << presentation_id; | 345 << ", presentation ID: " << presentation_id; |
| 363 media_route_provider_->CreateRoute( | 346 media_route_provider_->CreateRoute( |
| 364 source_id, sink_id, presentation_id, origin, tab_id, | 347 source_id, sink_id, presentation_id, origin, tab_id, |
| 365 base::Bind(&RouteResponseReceived, callbacks)); | 348 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 349 base::Unretained(this), callbacks, sink_id)); |
| 366 } | 350 } |
| 367 | 351 |
| 368 void MediaRouterMojoImpl::DoJoinRoute( | 352 void MediaRouterMojoImpl::DoJoinRoute( |
| 369 const MediaSource::Id& source_id, | 353 const MediaSource::Id& source_id, |
| 370 const std::string& presentation_id, | 354 const std::string& presentation_id, |
| 371 const std::string& origin, | 355 const std::string& origin, |
| 372 int tab_id, | 356 int tab_id, |
| 373 const std::vector<MediaRouteResponseCallback>& callbacks) { | 357 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 374 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id | 358 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id |
| 375 << ", presentation ID: " << presentation_id; | 359 << ", presentation ID: " << presentation_id; |
| 376 media_route_provider_->JoinRoute( | 360 media_route_provider_->JoinRoute( |
| 377 source_id, presentation_id, origin, tab_id, | 361 source_id, presentation_id, origin, tab_id, |
| 378 base::Bind(&RouteResponseReceived, callbacks)); | 362 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 363 base::Unretained(this), callbacks, "")); |
| 379 } | 364 } |
| 380 | 365 |
| 381 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { | 366 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { |
| 382 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; | 367 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; |
| 383 media_route_provider_->CloseRoute(route_id); | 368 media_route_provider_->CloseRoute(route_id); |
| 384 } | 369 } |
| 385 | 370 |
| 386 void MediaRouterMojoImpl::DoSendSessionMessage( | 371 void MediaRouterMojoImpl::DoSendSessionMessage( |
| 387 const MediaRoute::Id& route_id, | 372 const MediaRoute::Id& route_id, |
| 388 const std::string& message, | 373 const std::string& message, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 << "ExecutePendingRequests was called while extension is suspended."; | 479 << "ExecutePendingRequests was called while extension is suspended."; |
| 495 return; | 480 return; |
| 496 } | 481 } |
| 497 | 482 |
| 498 for (const auto& next_request : pending_requests_) | 483 for (const auto& next_request : pending_requests_) |
| 499 next_request.Run(); | 484 next_request.Run(); |
| 500 | 485 |
| 501 pending_requests_.clear(); | 486 pending_requests_.clear(); |
| 502 } | 487 } |
| 503 | 488 |
| 489 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() |
| 490 // into a local callback. |
| 491 void MediaRouterMojoImpl::RouteResponseReceived( |
| 492 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 493 const MediaSink::Id& sink_id, |
| 494 interfaces::MediaRoutePtr media_route, |
| 495 const mojo::String& error_text) { |
| 496 sink_ids_in_launch_.erase(sink_id); |
| 497 scoped_ptr<MediaRoute> route; |
| 498 std::string error; |
| 499 if (media_route.is_null()) { |
| 500 // An error occurred. |
| 501 DCHECK(!error_text.is_null()); |
| 502 error = !error_text.get().empty() ? error_text.get() : "Unknown error."; |
| 503 } else { |
| 504 route = media_route.To<scoped_ptr<MediaRoute>>(); |
| 505 } |
| 506 |
| 507 for (const MediaRouteResponseCallback& callback : callbacks) |
| 508 callback.Run(route.get(), error); |
| 509 } |
| 510 |
| 504 } // namespace media_router | 511 } // namespace media_router |
| OLD | NEW |