summaryrefslogtreecommitdiffstats
path: root/dom/presentation/provider/LegacyMDNSDeviceProvider.h
blob: 33ba877d332e77b3a71f4cffaf9ede77ce23f1bb (plain)
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
/* -*- 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_LegacyMDNSDeviceProvider_h
#define mozilla_dom_presentation_provider_LegacyMDNSDeviceProvider_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"

namespace mozilla {
namespace dom {
namespace presentation {
namespace legacy {

class DNSServiceWrappedListener;
class MulticastDNSService;

class LegacyMDNSDeviceProvider final
  : public nsIPresentationDeviceProvider
  , public nsIDNSServiceDiscoveryListener
  , public nsIDNSServiceResolveListener
  , public nsIObserver
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIPRESENTATIONDEVICEPROVIDER
  NS_DECL_NSIDNSSERVICEDISCOVERYLISTENER
  NS_DECL_NSIDNSSERVICERESOLVELISTENER
  NS_DECL_NSIOBSERVER

  explicit LegacyMDNSDeviceProvider() = 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,
                    DeviceState aState,
                    LegacyMDNSDeviceProvider* aProvider)
      : mId(aId)
      , mName(aName)
      , mType(aType)
      , mAddress(aAddress)
      , mPort(aPort)
      , mState(aState)
      , mProvider(aProvider)
    {
    }

    const nsCString& Id() const
    {
      return mId;
    }

    const nsCString& Address() const
    {
      return mAddress;
    }

    uint16_t Port() const
    {
      return mPort;
    }

    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)
    {
      mName = aName;
      mType = aType;
      mAddress = aAddress;
      mPort = aPort;
    }

  private:
    virtual ~Device() = default;

    nsCString mId;
    nsCString mName;
    nsCString mType;
    nsCString mAddress;
    uint16_t mPort;
    DeviceState mState;
    LegacyMDNSDeviceProvider* 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 ~LegacyMDNSDeviceProvider();
  nsresult StopDiscovery(nsresult aReason);
  nsresult Connect(Device* aDevice,
                   nsIPresentationControlChannel** aRetVal);

  // device manipulation
  nsresult AddDevice(const nsACString& aId,
                     const nsACString& aServiceName,
                     const nsACString& aServiceType,
                     const nsACString& aAddress,
                     const uint16_t aPort);
  nsresult UpdateDevice(const uint32_t aIndex,
                        const nsACString& aServiceName,
                        const nsACString& aServiceType,
                        const nsACString& aAddress,
                        const uint16_t aPort);
  nsresult RemoveDevice(const uint32_t aIndex);
  bool FindDeviceById(const nsACString& aId,
                      uint32_t& aIndex);

  bool FindDeviceByAddress(const nsACString& aAddress,
                           uint32_t& aIndex);

  void MarkAllDevicesUnknown();
  void ClearUnknownDevices();
  void ClearDevices();

  // preferences
  nsresult OnDiscoveryChanged(bool aEnabled);
  nsresult OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs);
  nsresult OnServiceNameChanged(const nsACString& aServiceName);

  bool mInitialized = false;
  nsWeakPtr mDeviceListener;
  nsCOMPtr<nsIPresentationControlService> mPresentationService;
  nsCOMPtr<nsIDNSServiceDiscovery> mMulticastDNS;
  RefPtr<DNSServiceWrappedListener> mWrappedListener;

  nsCOMPtr<nsICancelable> mDiscoveryRequest;

  nsTArray<RefPtr<Device>> mDevices;

  bool mDiscoveryEnabled = false;
  bool mIsDiscovering = false;
  uint32_t mDiscoveryTimeoutMs;
  nsCOMPtr<nsITimer> mDiscoveryTimer;

  nsCString mServiceName;
};

} // namespace legacy
} // namespace presentation
} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_presentation_provider_LegacyMDNSDeviceProvider_h