• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "libcef/browser/media_router/media_route_impl.h"
6 
7 #include "libcef/browser/media_router/media_router_manager.h"
8 #include "libcef/browser/media_router/media_sink_impl.h"
9 #include "libcef/browser/media_router/media_source_impl.h"
10 #include "libcef/browser/thread_util.h"
11 
12 namespace {
13 
14 // Do not keep a reference to the object returned by this method.
GetBrowserContext(const CefBrowserContext::Getter & getter)15 CefBrowserContext* GetBrowserContext(const CefBrowserContext::Getter& getter) {
16   CEF_REQUIRE_UIT();
17   DCHECK(!getter.is_null());
18 
19   // Will return nullptr if the BrowserContext has been shut down.
20   return getter.Run();
21 }
22 
23 }  // namespace
24 
CefMediaRouteImpl(const media_router::MediaRoute & route,const CefBrowserContext::Getter & browser_context_getter)25 CefMediaRouteImpl::CefMediaRouteImpl(
26     const media_router::MediaRoute& route,
27     const CefBrowserContext::Getter& browser_context_getter)
28     : route_(route), browser_context_getter_(browser_context_getter) {
29   CEF_REQUIRE_UIT();
30 }
31 
GetId()32 CefString CefMediaRouteImpl::GetId() {
33   return route_.media_route_id();
34 }
35 
GetSource()36 CefRefPtr<CefMediaSource> CefMediaRouteImpl::GetSource() {
37   return new CefMediaSourceImpl(route_.media_source().id());
38 }
39 
GetSink()40 CefRefPtr<CefMediaSink> CefMediaRouteImpl::GetSink() {
41   return new CefMediaSinkImpl(
42       route_.media_sink_id(), route_.media_sink_name(),
43       route_.media_source().IsDialSource()
44           ? media_router::mojom::MediaRouteProviderId::DIAL
45           : media_router::mojom::MediaRouteProviderId::CAST);
46 }
47 
SendRouteMessage(const void * message,size_t message_size)48 void CefMediaRouteImpl::SendRouteMessage(const void* message,
49                                          size_t message_size) {
50   std::string message_str(reinterpret_cast<const char*>(message), message_size);
51 
52   if (!CEF_CURRENTLY_ON_UIT()) {
53     CEF_POST_TASK(
54         CEF_UIT,
55         base::BindOnce(
56             [](CefRefPtr<CefMediaRouteImpl> self, std::string message_str) {
57               self->SendRouteMessageInternal(std::move(message_str));
58             },
59             CefRefPtr<CefMediaRouteImpl>(this), std::move(message_str)));
60     return;
61   }
62 
63   SendRouteMessageInternal(std::move(message_str));
64 }
65 
Terminate()66 void CefMediaRouteImpl::Terminate() {
67   if (!CEF_CURRENTLY_ON_UIT()) {
68     CEF_POST_TASK(CEF_UIT, base::BindOnce(&CefMediaRouteImpl::Terminate, this));
69     return;
70   }
71 
72   auto browser_context = GetBrowserContext(browser_context_getter_);
73   if (!browser_context)
74     return;
75 
76   browser_context->GetMediaRouterManager()->TerminateRoute(
77       route_.media_route_id());
78 }
79 
SendRouteMessageInternal(std::string message)80 void CefMediaRouteImpl::SendRouteMessageInternal(std::string message) {
81   auto browser_context = GetBrowserContext(browser_context_getter_);
82   if (!browser_context)
83     return;
84 
85   browser_context->GetMediaRouterManager()->SendRouteMessage(
86       route_.media_route_id(), message);
87 }
88