/src/mozilla-central/dom/presentation/provider/MulticastDNSDeviceProvider.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h |
8 | | #define mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h |
9 | | |
10 | | #include "mozilla/RefPtr.h" |
11 | | #include "nsCOMPtr.h" |
12 | | #include "nsICancelable.h" |
13 | | #include "nsIDNSServiceDiscovery.h" |
14 | | #include "nsIObserver.h" |
15 | | #include "nsIPresentationDevice.h" |
16 | | #include "nsIPresentationDeviceProvider.h" |
17 | | #include "nsIPresentationControlService.h" |
18 | | #include "nsITimer.h" |
19 | | #include "nsString.h" |
20 | | #include "nsTArray.h" |
21 | | #include "nsWeakPtr.h" |
22 | | |
23 | | class nsITCPDeviceInfo; |
24 | | |
25 | | namespace mozilla { |
26 | | namespace dom { |
27 | | namespace presentation { |
28 | | |
29 | | class DNSServiceWrappedListener; |
30 | | class MulticastDNSService; |
31 | | |
32 | | class MulticastDNSDeviceProvider final |
33 | | : public nsIPresentationDeviceProvider |
34 | | , public nsIDNSServiceDiscoveryListener |
35 | | , public nsIDNSRegistrationListener |
36 | | , public nsIDNSServiceResolveListener |
37 | | , public nsIPresentationControlServerListener |
38 | | , public nsIObserver |
39 | | { |
40 | | public: |
41 | | NS_DECL_ISUPPORTS |
42 | | NS_DECL_NSIPRESENTATIONDEVICEPROVIDER |
43 | | NS_DECL_NSIDNSSERVICEDISCOVERYLISTENER |
44 | | NS_DECL_NSIDNSREGISTRATIONLISTENER |
45 | | NS_DECL_NSIDNSSERVICERESOLVELISTENER |
46 | | NS_DECL_NSIPRESENTATIONCONTROLSERVERLISTENER |
47 | | NS_DECL_NSIOBSERVER |
48 | | |
49 | 0 | explicit MulticastDNSDeviceProvider() = default; |
50 | | nsresult Init(); |
51 | | nsresult Uninit(); |
52 | | |
53 | | private: |
54 | | enum class DeviceState : uint32_t { |
55 | | eUnknown, |
56 | | eActive |
57 | | }; |
58 | | |
59 | | class Device final : public nsIPresentationDevice |
60 | | { |
61 | | public: |
62 | | NS_DECL_ISUPPORTS |
63 | | NS_DECL_NSIPRESENTATIONDEVICE |
64 | | |
65 | | explicit Device(const nsACString& aId, |
66 | | const nsACString& aName, |
67 | | const nsACString& aType, |
68 | | const nsACString& aAddress, |
69 | | const uint16_t aPort, |
70 | | const nsACString& aCertFingerprint, |
71 | | DeviceState aState, |
72 | | MulticastDNSDeviceProvider* aProvider) |
73 | | : mId(aId) |
74 | | , mName(aName) |
75 | | , mType(aType) |
76 | | , mAddress(aAddress) |
77 | | , mPort(aPort) |
78 | | , mCertFingerprint(aCertFingerprint) |
79 | | , mState(aState) |
80 | | , mProvider(aProvider) |
81 | 0 | { |
82 | 0 | } |
83 | | |
84 | | const nsCString& Id() const |
85 | 0 | { |
86 | 0 | return mId; |
87 | 0 | } |
88 | | |
89 | | const nsCString& Address() const |
90 | 0 | { |
91 | 0 | return mAddress; |
92 | 0 | } |
93 | | |
94 | | uint16_t Port() const |
95 | 0 | { |
96 | 0 | return mPort; |
97 | 0 | } |
98 | | |
99 | | const nsCString& CertFingerprint() const |
100 | 0 | { |
101 | 0 | return mCertFingerprint; |
102 | 0 | } |
103 | | |
104 | | DeviceState State() const |
105 | 0 | { |
106 | 0 | return mState; |
107 | 0 | } |
108 | | |
109 | | void ChangeState(DeviceState aState) |
110 | 0 | { |
111 | 0 | mState = aState; |
112 | 0 | } |
113 | | |
114 | | void Update(const nsACString& aName, |
115 | | const nsACString& aType, |
116 | | const nsACString& aAddress, |
117 | | const uint16_t aPort, |
118 | | const nsACString& aCertFingerprint) |
119 | 0 | { |
120 | 0 | mName = aName; |
121 | 0 | mType = aType; |
122 | 0 | mAddress = aAddress; |
123 | 0 | mPort = aPort; |
124 | 0 | mCertFingerprint = aCertFingerprint; |
125 | 0 | } |
126 | | |
127 | | private: |
128 | 0 | virtual ~Device() = default; |
129 | | |
130 | | nsCString mId; |
131 | | nsCString mName; |
132 | | nsCString mType; |
133 | | nsCString mAddress; |
134 | | uint16_t mPort; |
135 | | nsCString mCertFingerprint; |
136 | | DeviceState mState; |
137 | | MulticastDNSDeviceProvider* mProvider; |
138 | | }; |
139 | | |
140 | | struct DeviceIdComparator { |
141 | 0 | bool Equals(const RefPtr<Device>& aA, const RefPtr<Device>& aB) const { |
142 | 0 | return aA->Id() == aB->Id(); |
143 | 0 | } |
144 | | }; |
145 | | |
146 | | struct DeviceAddressComparator { |
147 | 0 | bool Equals(const RefPtr<Device>& aA, const RefPtr<Device>& aB) const { |
148 | 0 | return aA->Address() == aB->Address(); |
149 | 0 | } |
150 | | }; |
151 | | |
152 | | virtual ~MulticastDNSDeviceProvider(); |
153 | | nsresult StartServer(); |
154 | | nsresult StopServer(); |
155 | | void AbortServerRetry(); |
156 | | nsresult RegisterMDNSService(); |
157 | | nsresult UnregisterMDNSService(nsresult aReason); |
158 | | nsresult StopDiscovery(nsresult aReason); |
159 | | nsresult Connect(Device* aDevice, |
160 | | nsIPresentationControlChannel** aRetVal); |
161 | | bool IsCompatibleServer(nsIDNSServiceInfo* aServiceInfo); |
162 | | |
163 | | // device manipulation |
164 | | nsresult AddDevice(const nsACString& aId, |
165 | | const nsACString& aServiceName, |
166 | | const nsACString& aServiceType, |
167 | | const nsACString& aAddress, |
168 | | const uint16_t aPort, |
169 | | const nsACString& aCertFingerprint); |
170 | | nsresult UpdateDevice(const uint32_t aIndex, |
171 | | const nsACString& aServiceName, |
172 | | const nsACString& aServiceType, |
173 | | const nsACString& aAddress, |
174 | | const uint16_t aPort, |
175 | | const nsACString& aCertFingerprint); |
176 | | nsresult RemoveDevice(const uint32_t aIndex); |
177 | | bool FindDeviceById(const nsACString& aId, |
178 | | uint32_t& aIndex); |
179 | | |
180 | | bool FindDeviceByAddress(const nsACString& aAddress, |
181 | | uint32_t& aIndex); |
182 | | |
183 | | already_AddRefed<Device> |
184 | | GetOrCreateDevice(nsITCPDeviceInfo* aDeviceInfo); |
185 | | |
186 | | void MarkAllDevicesUnknown(); |
187 | | void ClearUnknownDevices(); |
188 | | void ClearDevices(); |
189 | | |
190 | | // preferences |
191 | | nsresult OnDiscoveryChanged(bool aEnabled); |
192 | | nsresult OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs); |
193 | | nsresult OnDiscoverableChanged(bool aEnabled); |
194 | | nsresult OnServiceNameChanged(const nsACString& aServiceName); |
195 | | |
196 | | bool mInitialized = false; |
197 | | nsWeakPtr mDeviceListener; |
198 | | nsCOMPtr<nsIPresentationControlService> mPresentationService; |
199 | | nsCOMPtr<nsIDNSServiceDiscovery> mMulticastDNS; |
200 | | RefPtr<DNSServiceWrappedListener> mWrappedListener; |
201 | | |
202 | | nsCOMPtr<nsICancelable> mDiscoveryRequest; |
203 | | nsCOMPtr<nsICancelable> mRegisterRequest; |
204 | | |
205 | | nsTArray<RefPtr<Device>> mDevices; |
206 | | |
207 | | bool mDiscoveryEnabled = false; |
208 | | bool mIsDiscovering = false; |
209 | | uint32_t mDiscoveryTimeoutMs; |
210 | | nsCOMPtr<nsITimer> mDiscoveryTimer; |
211 | | |
212 | | bool mDiscoverable = false; |
213 | | bool mDiscoverableEncrypted = false; |
214 | | bool mIsServerRetrying = false; |
215 | | uint32_t mServerRetryMs; |
216 | | nsCOMPtr<nsITimer> mServerRetryTimer; |
217 | | |
218 | | nsCString mServiceName; |
219 | | nsCString mRegisteredName; |
220 | | }; |
221 | | |
222 | | } // namespace presentation |
223 | | } // namespace dom |
224 | | } // namespace mozilla |
225 | | |
226 | | #endif // mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h |