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