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/media_router_mojo_impl_factory.h" | 13 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" |
14 #include "chrome/browser/media/router/media_router_type_converters.h" | 14 #include "chrome/browser/media/router/media_router_type_converters.h" |
15 #include "chrome/browser/media/router/media_routes_observer.h" | 15 #include "chrome/browser/media/router/media_routes_observer.h" |
16 #include "chrome/browser/media/router/media_sinks_observer.h" | 16 #include "chrome/browser/media/router/media_sinks_observer.h" |
17 #include "extensions/browser/process_manager.h" | 17 #include "extensions/browser/process_manager.h" |
18 | 18 |
19 #define DVLOG_WITH_INSTANCE(level) \ | 19 #define DVLOG_WITH_INSTANCE(level) \ |
20 DVLOG(level) << "MR #" << instance_id_ << ": " | 20 DVLOG(level) << "MR #" << instance_id_ << ": " |
21 | 21 |
22 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " | 22 #define DLOG_WITH_INSTANCE(level) DLOG(level) << "MR #" << instance_id_ << ": " |
23 | 23 |
24 namespace media_router { | 24 namespace media_router { |
25 namespace { | 25 namespace { |
26 | 26 |
27 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() | 27 // Converts the callback result of calling Mojo CreateRoute()/JoinRoute() |
28 // into a local callback. | 28 // into a local callback. |
29 void RouteResponseReceived(const MediaRouteResponseCallback& callback, | 29 void RouteResponseReceived( |
30 interfaces::MediaRoutePtr media_route, | 30 const std::vector<MediaRouteResponseCallback>& callbacks, |
31 const mojo::String& error_text) { | 31 interfaces::MediaRoutePtr media_route, |
| 32 const mojo::String& error_text) { |
| 33 scoped_ptr<MediaRoute> route; |
| 34 std::string error; |
32 if (media_route.is_null()) { | 35 if (media_route.is_null()) { |
33 // An error occurred. | 36 // An error occurred. |
34 DCHECK(!error_text.is_null()); | 37 DCHECK(!error_text.is_null()); |
35 callback.Run(nullptr, !error_text.get().empty() ? error_text.get() | 38 error = !error_text.get().empty() ? error_text.get() : "Unknown error."; |
36 : "Unknown error."); | 39 } else { |
37 return; | 40 route = media_route.To<scoped_ptr<MediaRoute>>(); |
38 } | 41 } |
39 callback.Run(media_route.To<scoped_ptr<MediaRoute>>(), ""); | 42 |
| 43 for (const MediaRouteResponseCallback& callback : callbacks) |
| 44 callback.Run(route.get(), error); |
40 } | 45 } |
41 | 46 |
42 // TODO(imcheng): We should handle failure in this case. One way is to invoke | 47 // TODO(imcheng): We should handle failure in this case. One way is to invoke |
43 // all pending requests with failure. (crbug.com/490787) | 48 // all pending requests with failure. (crbug.com/490787) |
44 void EventPageWakeComplete(bool success) { | 49 void EventPageWakeComplete(bool success) { |
45 if (!success) | 50 if (!success) |
46 LOG(ERROR) << "An error encountered while waking the event page."; | 51 LOG(ERROR) << "An error encountered while waking the event page."; |
47 } | 52 } |
48 | 53 |
49 scoped_ptr<content::PresentationSessionMessage> | 54 scoped_ptr<content::PresentationSessionMessage> |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 186 |
182 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, | 187 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, |
183 OnRoutesUpdated(routes_converted)); | 188 OnRoutesUpdated(routes_converted)); |
184 } | 189 } |
185 | 190 |
186 void MediaRouterMojoImpl::CreateRoute( | 191 void MediaRouterMojoImpl::CreateRoute( |
187 const MediaSource::Id& source_id, | 192 const MediaSource::Id& source_id, |
188 const MediaSink::Id& sink_id, | 193 const MediaSink::Id& sink_id, |
189 const GURL& origin, | 194 const GURL& origin, |
190 int tab_id, | 195 int tab_id, |
191 const MediaRouteResponseCallback& callback) { | 196 const std::vector<MediaRouteResponseCallback>& callbacks) { |
192 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
193 | 198 |
194 if (!origin.is_valid()) { | 199 if (!origin.is_valid()) { |
195 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; | 200 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
196 callback.Run(nullptr, "Invalid origin"); | 201 for (const MediaRouteResponseCallback& callback : callbacks) |
| 202 callback.Run(nullptr, "Invalid origin"); |
197 return; | 203 return; |
198 } | 204 } |
199 RunOrDefer(base::Bind( | 205 RunOrDefer(base::Bind( |
200 &MediaRouterMojoImpl::DoCreateRoute, base::Unretained(this), source_id, | 206 &MediaRouterMojoImpl::DoCreateRoute, base::Unretained(this), source_id, |
201 sink_id, origin.is_empty() ? "" : origin.spec(), tab_id, callback)); | 207 sink_id, origin.is_empty() ? "" : origin.spec(), tab_id, callbacks)); |
202 } | 208 } |
203 | 209 |
204 void MediaRouterMojoImpl::JoinRoute( | 210 void MediaRouterMojoImpl::JoinRoute( |
205 const MediaSource::Id& source_id, | 211 const MediaSource::Id& source_id, |
206 const std::string& presentation_id, | 212 const std::string& presentation_id, |
207 const GURL& origin, | 213 const GURL& origin, |
208 int tab_id, | 214 int tab_id, |
209 const MediaRouteResponseCallback& callback) { | 215 const std::vector<MediaRouteResponseCallback>& callbacks) { |
210 DCHECK(thread_checker_.CalledOnValidThread()); | 216 DCHECK(thread_checker_.CalledOnValidThread()); |
211 | 217 |
212 if (!origin.is_valid()) { | 218 if (!origin.is_valid()) { |
213 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; | 219 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
214 callback.Run(nullptr, "Invalid origin"); | 220 for (const MediaRouteResponseCallback& callback : callbacks) |
| 221 callback.Run(nullptr, "Invalid origin"); |
215 return; | 222 return; |
216 } | 223 } |
217 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, | 224 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, |
218 base::Unretained(this), source_id, presentation_id, | 225 base::Unretained(this), source_id, presentation_id, |
219 origin.is_empty() ? "" : origin.spec(), tab_id, | 226 origin.is_empty() ? "" : origin.spec(), tab_id, |
220 callback)); | 227 callbacks)); |
221 } | 228 } |
222 | 229 |
223 void MediaRouterMojoImpl::CloseRoute(const MediaRoute::Id& route_id) { | 230 void MediaRouterMojoImpl::CloseRoute(const MediaRoute::Id& route_id) { |
224 DCHECK(thread_checker_.CalledOnValidThread()); | 231 DCHECK(thread_checker_.CalledOnValidThread()); |
225 | 232 |
226 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCloseRoute, | 233 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCloseRoute, |
227 base::Unretained(this), route_id)); | 234 base::Unretained(this), route_id)); |
228 } | 235 } |
229 | 236 |
230 void MediaRouterMojoImpl::SendRouteMessage( | 237 void MediaRouterMojoImpl::SendRouteMessage( |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) { | 335 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) { |
329 // TODO(imcheng): Implement. (crbug.com/461815) | 336 // TODO(imcheng): Implement. (crbug.com/461815) |
330 NOTIMPLEMENTED(); | 337 NOTIMPLEMENTED(); |
331 } | 338 } |
332 | 339 |
333 void MediaRouterMojoImpl::DoCreateRoute( | 340 void MediaRouterMojoImpl::DoCreateRoute( |
334 const MediaSource::Id& source_id, | 341 const MediaSource::Id& source_id, |
335 const MediaSink::Id& sink_id, | 342 const MediaSink::Id& sink_id, |
336 const std::string& origin, | 343 const std::string& origin, |
337 int tab_id, | 344 int tab_id, |
338 const MediaRouteResponseCallback& callback) { | 345 const std::vector<MediaRouteResponseCallback>& callbacks) { |
339 std::string presentation_id("mr_"); | 346 std::string presentation_id("mr_"); |
340 presentation_id += base::GenerateGUID(); | 347 presentation_id += base::GenerateGUID(); |
341 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id | 348 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id |
342 << ", presentation ID: " << presentation_id; | 349 << ", presentation ID: " << presentation_id; |
343 media_route_provider_->CreateRoute( | 350 media_route_provider_->CreateRoute( |
344 source_id, sink_id, presentation_id, origin, tab_id, | 351 source_id, sink_id, presentation_id, origin, tab_id, |
345 base::Bind(&RouteResponseReceived, callback)); | 352 base::Bind(&RouteResponseReceived, callbacks)); |
346 } | 353 } |
347 | 354 |
348 void MediaRouterMojoImpl::DoJoinRoute( | 355 void MediaRouterMojoImpl::DoJoinRoute( |
349 const MediaSource::Id& source_id, | 356 const MediaSource::Id& source_id, |
350 const std::string& presentation_id, | 357 const std::string& presentation_id, |
351 const std::string& origin, | 358 const std::string& origin, |
352 int tab_id, | 359 int tab_id, |
353 const MediaRouteResponseCallback& callback) { | 360 const std::vector<MediaRouteResponseCallback>& callbacks) { |
354 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id | 361 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id |
355 << ", presentation ID: " << presentation_id; | 362 << ", presentation ID: " << presentation_id; |
356 media_route_provider_->JoinRoute( | 363 media_route_provider_->JoinRoute( |
357 source_id, presentation_id, origin, tab_id, | 364 source_id, presentation_id, origin, tab_id, |
358 base::Bind(&RouteResponseReceived, callback)); | 365 base::Bind(&RouteResponseReceived, callbacks)); |
359 } | 366 } |
360 | 367 |
361 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { | 368 void MediaRouterMojoImpl::DoCloseRoute(const MediaRoute::Id& route_id) { |
362 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; | 369 DVLOG_WITH_INSTANCE(1) << "DoCloseRoute " << route_id; |
363 media_route_provider_->CloseRoute(route_id); | 370 media_route_provider_->CloseRoute(route_id); |
364 } | 371 } |
365 | 372 |
366 void MediaRouterMojoImpl::DoSendSessionMessage( | 373 void MediaRouterMojoImpl::DoSendSessionMessage( |
367 const MediaRoute::Id& route_id, | 374 const MediaRoute::Id& route_id, |
368 const std::string& message, | 375 const std::string& message, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 return; | 471 return; |
465 } | 472 } |
466 | 473 |
467 for (const auto& next_request : pending_requests_) | 474 for (const auto& next_request : pending_requests_) |
468 next_request.Run(); | 475 next_request.Run(); |
469 | 476 |
470 pending_requests_.clear(); | 477 pending_requests_.clear(); |
471 } | 478 } |
472 | 479 |
473 } // namespace media_router | 480 } // namespace media_router |
OLD | NEW |