OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | |
6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 #include <set> | |
10 | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #include "chrome/browser/media/router/discovery/mdns/dns_sd_delegate.h" | |
15 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" | |
16 #include "chrome/browser/media/router/discovery/media_sink_service_base.h" | |
17 #include "components/cast_channel/cast_channel_enum.h" | |
18 #include "components/cast_channel/cast_socket.h" | |
19 #include "net/base/ip_endpoint.h" | |
20 | |
21 namespace cast_channel { | |
22 class CastSocketService; | |
23 } // namespace cast_channel | |
24 | |
25 namespace content { | |
26 class BrowserContext; | |
27 } // namespace content | |
28 | |
29 namespace media_router { | |
30 | |
31 // A service which can be used to start background discovery and resolution of | |
32 // Cast devices. | |
33 // Public APIs should be invoked on the UI thread. | |
34 class CastMediaSinkService | |
35 : public MediaSinkServiceBase, | |
36 public DnsSdRegistry::DnsSdObserver, | |
37 public base::RefCountedThreadSafe<CastMediaSinkService> { | |
38 public: | |
39 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | |
40 content::BrowserContext* browser_context); | |
41 | |
42 // Used by unit tests. | |
43 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | |
44 cast_channel::CastSocketService* cast_socket_service); | |
45 | |
46 // MediaSinkService implementation | |
47 void Start() override; | |
48 void Stop() override; | |
49 | |
50 protected: | |
51 // Used to mock out the DnsSdRegistry for testing. | |
52 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); | |
imcheng
2017/06/29 23:15:44
Can you set DnsSdRegistry in the test constructor?
zhaobin
2017/07/05 18:01:37
We are going to keep this function and remove |tes
| |
53 | |
54 ~CastMediaSinkService() override; | |
55 | |
56 private: | |
57 // Receives incoming messages and errors and provides additional API context. | |
58 class CastSocketObserver : public cast_channel::CastSocket::Observer { | |
59 public: | |
60 CastSocketObserver(); | |
61 ~CastSocketObserver() override; | |
62 | |
63 // CastSocket::Observer implementation. | |
64 void OnError(const cast_channel::CastSocket& socket, | |
65 cast_channel::ChannelError error_state) override; | |
66 void OnMessage(const cast_channel::CastSocket& socket, | |
67 const cast_channel::CastMessage& message) override; | |
68 | |
69 private: | |
70 DISALLOW_COPY_AND_ASSIGN(CastSocketObserver); | |
71 }; | |
72 | |
73 friend class base::RefCountedThreadSafe<CastMediaSinkService>; | |
74 friend class CastMediaSinkServiceTest; | |
75 | |
76 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); | |
77 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | |
78 TestOnChannelOpenedOnIOThread); | |
79 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | |
80 TestMultipleOnChannelOpenedOnIOThread); | |
81 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); | |
82 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); | |
83 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); | |
84 | |
85 // DnsSdRegistry::DnsSdObserver implementation | |
86 void OnDnsSdEvent(const std::string& service_type, | |
87 const DnsSdRegistry::DnsSdServiceList& services) override; | |
88 | |
89 // Open cast channel on IO thread. | |
mark a. foltz
2017/06/28 22:43:13
nit: Opens
zhaobin
2017/07/05 18:01:36
Done.
| |
90 // |service|: mDNS service description. | |
91 // |ip_endpoint|: cast channel's target IP endpoint. | |
92 void OpenChannelOnIOThread(const DnsSdService& service, | |
93 const net::IPEndPoint& ip_endpoint); | |
94 | |
95 // Invoked when opening cast channel on IO thread completes. | |
96 // |service|: mDNS service description. | |
97 // |channel_id|: channel id of newly created cast channel. | |
98 // |channel_error|: error encounted when opending cast channel. | |
99 void OnChannelOpenedOnIOThread(const DnsSdService& service, | |
100 int channel_id, | |
101 cast_channel::ChannelError channel_error); | |
102 | |
103 // Invoked by |OnChannelOpenedOnIOThread| to post task on UI thread. | |
104 // |service|: mDNS service description. | |
105 // |channel_id|: channel id of newly created cast channel. | |
106 // |audio_only|: if cast channel is audio only or not. | |
107 void OnChannelOpenedOnUIThread(const DnsSdService& service, | |
108 int channel_id, | |
109 bool audio_only); | |
110 | |
111 // Raw pointer to DnsSdRegistry singleton. | |
mark a. foltz
2017/06/28 22:43:13
Can you comment about object lifetime here? I ass
zhaobin
2017/07/05 18:01:37
Done.
| |
112 DnsSdRegistry* dns_sd_registry_ = nullptr; | |
113 | |
114 // DnsSdRegistry for test. | |
115 DnsSdRegistry* test_dns_sd_registry_ = nullptr; | |
mark a. foltz
2017/06/28 22:43:13
Does this need to be held separately or can tests
zhaobin
2017/07/05 18:01:37
Yes, we can remove this and let SetDnsSdRegistryFo
| |
116 | |
117 // Service list from current round of discovery. | |
118 DnsSdRegistry::DnsSdServiceList current_services_; | |
119 | |
120 // Service managing creating and removing cast channels. | |
121 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; | |
122 | |
123 THREAD_CHECKER(thread_checker_); | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); | |
126 }; | |
127 | |
128 } // namespace media_router | |
129 | |
130 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | |
OLD | NEW |