| 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/observer_list.h" | 10 #include "base/observer_list.h" |
| 11 #include "base/strings/stringprintf.h" |
| 11 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" | 12 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" |
| 12 #include "chrome/browser/media/router/media_router_type_converters.h" | 13 #include "chrome/browser/media/router/media_router_type_converters.h" |
| 13 #include "chrome/browser/media/router/media_routes_observer.h" | 14 #include "chrome/browser/media/router/media_routes_observer.h" |
| 14 #include "chrome/browser/media/router/media_sinks_observer.h" | 15 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 15 #include "extensions/browser/process_manager.h" | 16 #include "extensions/browser/process_manager.h" |
| 16 | 17 |
| 17 #define DVLOG_WITH_INSTANCE(level) \ | 18 #define DVLOG_WITH_INSTANCE(level) \ |
| 18 DVLOG(level) << "MR #" << instance_id_ << ": " | 19 DVLOG(level) << "MR #" << instance_id_ << ": " |
| 19 | 20 |
| 20 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " | 21 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " |
| 21 | 22 |
| 22 namespace media_router { | 23 namespace media_router { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Converts the callback result of calling Mojo CreateRoute() into a local | 26 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() |
| 26 // callback. | 27 // into a local callback. |
| 27 void CreateRouteFinished(const MediaSink::Id& sink_id, | 28 void RouteResponseReceived(const MediaRouteResponseCallback& callback, |
| 28 const MediaRouteResponseCallback& callback, | 29 interfaces::MediaRoutePtr media_route, |
| 29 interfaces::MediaRoutePtr media_route, | 30 const mojo::String& error_text) { |
| 30 const mojo::String& error_text) { | |
| 31 if (media_route.is_null()) { | 31 if (media_route.is_null()) { |
| 32 // An error occurred. | 32 // An error occurred. |
| 33 DCHECK(!error_text.is_null()); | 33 DCHECK(!error_text.is_null()); |
| 34 callback.Run(nullptr, !error_text.get().empty() ? error_text.get() | 34 callback.Run(nullptr, !error_text.get().empty() ? error_text.get() |
| 35 : "Unknown error."); | 35 : "Unknown error."); |
| 36 return; | 36 return; |
| 37 } | 37 } |
| 38 callback.Run(media_route.To<scoped_ptr<MediaRoute>>(), ""); | 38 callback.Run(media_route.To<scoped_ptr<MediaRoute>>(), ""); |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 routes_converted.push_back(routes[i].To<MediaRoute>()); | 153 routes_converted.push_back(routes[i].To<MediaRoute>()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, | 156 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, |
| 157 OnRoutesUpdated(routes_converted)); | 157 OnRoutesUpdated(routes_converted)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void MediaRouterMojoImpl::CreateRoute( | 160 void MediaRouterMojoImpl::CreateRoute( |
| 161 const MediaSource::Id& source_id, | 161 const MediaSource::Id& source_id, |
| 162 const MediaSink::Id& sink_id, | 162 const MediaSink::Id& sink_id, |
| 163 const GURL& origin, |
| 164 int tab_id, |
| 163 const MediaRouteResponseCallback& callback) { | 165 const MediaRouteResponseCallback& callback) { |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 | 167 |
| 166 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCreateRoute, | 168 if (!origin.is_valid()) { |
| 167 base::Unretained(this), source_id, sink_id, callback)); | 169 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
| 170 callback.Run(nullptr, "Invalid origin"); |
| 171 return; |
| 172 } |
| 173 RunOrDefer(base::Bind( |
| 174 &MediaRouterMojoImpl::DoCreateRoute, base::Unretained(this), source_id, |
| 175 sink_id, origin.is_empty() ? "" : origin.spec(), tab_id, callback)); |
| 176 } |
| 177 |
| 178 void MediaRouterMojoImpl::JoinRoute( |
| 179 const MediaSource::Id& source_id, |
| 180 const std::string& presentation_id, |
| 181 const GURL& origin, |
| 182 int tab_id, |
| 183 const MediaRouteResponseCallback& callback) { |
| 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 185 |
| 186 if (!origin.is_valid()) { |
| 187 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
| 188 callback.Run(nullptr, "Invalid origin"); |
| 189 return; |
| 190 } |
| 191 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, |
| 192 base::Unretained(this), source_id, presentation_id, |
| 193 origin.is_empty() ? "" : origin.spec(), tab_id, |
| 194 callback)); |
| 168 } | 195 } |
| 169 | 196 |
| 170 void MediaRouterMojoImpl::CloseRoute(const MediaRoute::Id& route_id) { | 197 void MediaRouterMojoImpl::CloseRoute(const MediaRoute::Id& route_id) { |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); | 198 DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 | 199 |
| 173 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCloseRoute, | 200 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCloseRoute, |
| 174 base::Unretained(this), route_id)); | 201 base::Unretained(this), route_id)); |
| 175 } | 202 } |
| 176 | 203 |
| 177 void MediaRouterMojoImpl::SendRouteMessage( | 204 void MediaRouterMojoImpl::SendRouteMessage( |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 } | 292 } |
| 266 | 293 |
| 267 void MediaRouterMojoImpl::RemoveIssuesObserver(IssuesObserver* observer) { | 294 void MediaRouterMojoImpl::RemoveIssuesObserver(IssuesObserver* observer) { |
| 268 // TODO(imcheng): Implement. (crbug.com/461815) | 295 // TODO(imcheng): Implement. (crbug.com/461815) |
| 269 NOTIMPLEMENTED(); | 296 NOTIMPLEMENTED(); |
| 270 } | 297 } |
| 271 | 298 |
| 272 void MediaRouterMojoImpl::DoCreateRoute( | 299 void MediaRouterMojoImpl::DoCreateRoute( |
| 273 const MediaSource::Id& source_id, | 300 const MediaSource::Id& source_id, |
| 274 const MediaSink::Id& sink_id, | 301 const MediaSink::Id& sink_id, |
| 302 const std::string& origin, |
| 303 int tab_id, |
| 275 const MediaRouteResponseCallback& callback) { | 304 const MediaRouteResponseCallback& callback) { |
| 276 DVLOG_WITH_INSTANCE(1) << "CreateRoute " << source_id << "=>" << sink_id; | 305 std::string presentation_id("mr_"); |
| 277 mojo_media_router_->CreateRoute( | 306 presentation_id += base::GenerateGUID(); |
| 278 source_id, sink_id, base::Bind(&CreateRouteFinished, sink_id, callback)); | 307 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id |
| 308 << ", presentation ID: " << presentation_id; |
| 309 mojo_media_router_->CreateRoute(source_id, sink_id, presentation_id, origin, |
| 310 tab_id, |
| 311 base::Bind(&RouteResponseReceived, callback)); |
| 312 } |
| 313 |
| 314 void MediaRouterMojoImpl::DoJoinRoute( |
| 315 const MediaSource::Id& source_id, |
| 316 const std::string& presentation_id, |
| 317 const std::string& origin, |
| 318 int tab_id, |
| 319 const MediaRouteResponseCallback& callback) { |
| 320 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id |
| 321 << ", presentation ID: " << presentation_id; |
| 322 mojo_media_router_->JoinRoute(source_id, presentation_id, origin, tab_id, |
| 323 base::Bind(&RouteResponseReceived, callback)); |
| 279 } | 324 } |
| 280 | 325 |
| 281 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { | 326 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { |
| 282 DVLOG_WITH_INSTANCE(1) << "CloseRoute " << route_id; | 327 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; |
| 283 mojo_media_router_->CloseRoute(route_id); | 328 mojo_media_router_->CloseRoute(route_id); |
| 284 } | 329 } |
| 285 | 330 |
| 286 void MediaRouterMojoImpl::DoSendSessionMessage( | 331 void MediaRouterMojoImpl::DoSendSessionMessage( |
| 287 const MediaRoute::Id& route_id, | 332 const MediaRoute::Id& route_id, |
| 288 const std::string& message, | 333 const std::string& message, |
| 289 const SendRouteMessageCallback& callback) { | 334 const SendRouteMessageCallback& callback) { |
| 290 DVLOG_WITH_INSTANCE(1) << "SendRouteMessage " << route_id; | 335 DVLOG_WITH_INSTANCE(1) << "SendRouteMessage " << route_id; |
| 291 mojo_media_router_->SendRouteMessage(route_id, message, callback); | 336 mojo_media_router_->SendRouteMessage(route_id, message, callback); |
| 292 } | 337 } |
| 293 | 338 |
| 294 void MediaRouterMojoImpl::DoClearIssue(const Issue::Id& issue_id) { | 339 void MediaRouterMojoImpl::DoClearIssue(const Issue::Id& issue_id) { |
| 295 DVLOG_WITH_INSTANCE(1) << "ClearIssue " << issue_id; | 340 DVLOG_WITH_INSTANCE(1) << "DoClearIssue " << issue_id; |
| 296 mojo_media_router_->ClearIssue(issue_id); | 341 mojo_media_router_->ClearIssue(issue_id); |
| 297 } | 342 } |
| 298 | 343 |
| 299 void MediaRouterMojoImpl::DoStartObservingMediaSinks( | 344 void MediaRouterMojoImpl::DoStartObservingMediaSinks( |
| 300 const std::string& source_id) { | 345 const MediaSource::Id& source_id) { |
| 301 DVLOG_WITH_INSTANCE(1) << "StartObservingMediaSinks: " << source_id; | 346 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaSinks: " << source_id; |
| 302 mojo_media_router_->StartObservingMediaSinks(source_id); | 347 mojo_media_router_->StartObservingMediaSinks(source_id); |
| 303 } | 348 } |
| 304 | 349 |
| 305 void MediaRouterMojoImpl::DoStopObservingMediaSinks( | 350 void MediaRouterMojoImpl::DoStopObservingMediaSinks( |
| 306 const std::string& source_id) { | 351 const MediaSource::Id& source_id) { |
| 307 DVLOG_WITH_INSTANCE(1) << "StopObservingMediaSinks: " << source_id; | 352 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaSinks: " << source_id; |
| 308 mojo_media_router_->StopObservingMediaSinks(source_id); | 353 mojo_media_router_->StopObservingMediaSinks(source_id); |
| 309 } | 354 } |
| 310 | 355 |
| 311 void MediaRouterMojoImpl::DoStartObservingMediaRoutes() { | 356 void MediaRouterMojoImpl::DoStartObservingMediaRoutes() { |
| 312 DVLOG_WITH_INSTANCE(1) << "StartObservingMediaRoutes"; | 357 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaRoutes"; |
| 313 mojo_media_router_->StartObservingMediaRoutes(); | 358 mojo_media_router_->StartObservingMediaRoutes(); |
| 314 } | 359 } |
| 315 | 360 |
| 316 void MediaRouterMojoImpl::DoStopObservingMediaRoutes() { | 361 void MediaRouterMojoImpl::DoStopObservingMediaRoutes() { |
| 317 DVLOG_WITH_INSTANCE(1) << "StopObservingMediaRoutes"; | 362 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaRoutes"; |
| 318 mojo_media_router_->StopObservingMediaRoutes(); | 363 mojo_media_router_->StopObservingMediaRoutes(); |
| 319 } | 364 } |
| 320 | 365 |
| 321 void MediaRouterMojoImpl::EnqueueTask(const base::Closure& closure) { | 366 void MediaRouterMojoImpl::EnqueueTask(const base::Closure& closure) { |
| 322 pending_requests_.push_back(closure); | 367 pending_requests_.push_back(closure); |
| 323 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length=" | 368 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length=" |
| 324 << pending_requests_.size() << ")"; | 369 << pending_requests_.size() << ")"; |
| 325 } | 370 } |
| 326 | 371 |
| 327 void MediaRouterMojoImpl::RunOrDefer(const base::Closure& request) { | 372 void MediaRouterMojoImpl::RunOrDefer(const base::Closure& request) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 return; | 407 return; |
| 363 } | 408 } |
| 364 | 409 |
| 365 for (const auto& next_request : pending_requests_) | 410 for (const auto& next_request : pending_requests_) |
| 366 next_request.Run(); | 411 next_request.Run(); |
| 367 | 412 |
| 368 pending_requests_.clear(); | 413 pending_requests_.clear(); |
| 369 } | 414 } |
| 370 | 415 |
| 371 } // namespace media_router | 416 } // namespace media_router |
| OLD | NEW |