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