Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/network/UDPSocket.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_UDPSocket_h__
8
#define mozilla_dom_UDPSocket_h__
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/ErrorResult.h"
13
#include "mozilla/dom/Promise.h"
14
#include "mozilla/dom/SocketCommonBinding.h"
15
#include "nsIUDPSocket.h"
16
#include "nsIUDPSocketChild.h"
17
#include "nsTArray.h"
18
19
struct JSContext;
20
21
//
22
// set MOZ_LOG=UDPSocket:5
23
//
24
25
namespace mozilla {
26
namespace net {
27
extern LazyLogModule gUDPSocketLog;
28
0
#define UDPSOCKET_LOG(args)     MOZ_LOG(gUDPSocketLog, LogLevel::Debug, args)
29
#define UDPSOCKET_LOG_ENABLED() MOZ_LOG_TEST(gUDPSocketLog, LogLevel::Debug)
30
} // namespace net
31
32
namespace dom {
33
34
struct UDPOptions;
35
class StringOrBlobOrArrayBufferOrArrayBufferView;
36
37
class UDPSocket final : public DOMEventTargetHelper
38
                      , public nsIUDPSocketListener
39
                      , public nsIUDPSocketInternal
40
{
41
public:
42
  NS_DECL_ISUPPORTS_INHERITED
43
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UDPSocket, DOMEventTargetHelper)
44
  NS_DECL_NSIUDPSOCKETLISTENER
45
  NS_DECL_NSIUDPSOCKETINTERNAL
46
47
public:
48
  nsPIDOMWindowInner*
49
  GetParentObject() const
50
  {
51
    return GetOwner();
52
  }
53
54
  virtual JSObject*
55
  WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
56
57
  virtual void
58
  DisconnectFromOwner() override;
59
60
  static already_AddRefed<UDPSocket>
61
  Constructor(const GlobalObject& aGlobal, const UDPOptions& aOptions, ErrorResult& aRv);
62
63
  void
64
  GetLocalAddress(nsString& aRetVal) const
65
  {
66
    aRetVal = mLocalAddress;
67
  }
68
69
  Nullable<uint16_t>
70
  GetLocalPort() const
71
  {
72
    return mLocalPort;
73
  }
74
75
  void
76
  GetRemoteAddress(nsString& aRetVal) const
77
  {
78
    if (mRemoteAddress.IsVoid()) {
79
      SetDOMStringToNull(aRetVal);
80
      return;
81
    }
82
83
    aRetVal = NS_ConvertUTF8toUTF16(mRemoteAddress);
84
  }
85
86
  Nullable<uint16_t>
87
  GetRemotePort() const
88
  {
89
    return mRemotePort;
90
  }
91
92
  bool
93
  AddressReuse() const
94
  {
95
    return mAddressReuse;
96
  }
97
98
  bool
99
  Loopback() const
100
  {
101
    return mLoopback;
102
  }
103
104
  SocketReadyState
105
  ReadyState() const
106
  {
107
    return mReadyState;
108
  }
109
110
  Promise*
111
  Opened() const
112
  {
113
    return mOpened;
114
  }
115
116
  Promise*
117
  Closed() const
118
  {
119
    return mClosed;
120
  }
121
122
  IMPL_EVENT_HANDLER(message)
123
124
  already_AddRefed<Promise>
125
  Close();
126
127
  void
128
  JoinMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
129
130
  void
131
  LeaveMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
132
133
  bool
134
  Send(const StringOrBlobOrArrayBufferOrArrayBufferView& aData,
135
       const Optional<nsAString>& aRemoteAddress,
136
       const Optional<Nullable<uint16_t>>& aRemotePort,
137
       ErrorResult& aRv);
138
139
private:
140
  class ListenerProxy : public nsIUDPSocketListener
141
                      , public nsIUDPSocketInternal
142
  {
143
  public:
144
    NS_DECL_ISUPPORTS
145
    NS_FORWARD_SAFE_NSIUDPSOCKETLISTENER(mSocket)
146
    NS_FORWARD_SAFE_NSIUDPSOCKETINTERNAL(mSocket)
147
148
    explicit ListenerProxy(UDPSocket* aSocket)
149
      : mSocket(aSocket)
150
0
    {
151
0
    }
152
153
    void Disconnect()
154
0
    {
155
0
      mSocket = nullptr;
156
0
    }
157
158
  private:
159
0
    virtual ~ListenerProxy() {}
160
161
    UDPSocket* mSocket;
162
  };
163
164
  UDPSocket(nsPIDOMWindowInner* aOwner,
165
            const nsCString& aRemoteAddress,
166
            const Nullable<uint16_t>& aRemotePort);
167
168
  virtual ~UDPSocket();
169
170
  nsresult
171
  Init(const nsString& aLocalAddress,
172
       const Nullable<uint16_t>& aLocalPort,
173
       const bool& aAddressReuse,
174
       const bool& aLoopback);
175
176
  nsresult
177
  InitLocal(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
178
179
  nsresult
180
  InitRemote(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
181
182
  void
183
  HandleReceivedData(const nsACString& aRemoteAddress,
184
                     const uint16_t& aRemotePort,
185
                     const uint8_t* aData,
186
                     const uint32_t& aDataLength);
187
188
  nsresult
189
  DispatchReceivedData(const nsACString& aRemoteAddress,
190
                       const uint16_t& aRemotePort,
191
                       const uint8_t* aData,
192
                       const uint32_t& aDataLength);
193
194
  void
195
  CloseWithReason(nsresult aReason);
196
197
  nsresult
198
  DoPendingMcastCommand();
199
200
  nsString mLocalAddress;
201
  Nullable<uint16_t> mLocalPort;
202
  nsCString mRemoteAddress;
203
  Nullable<uint16_t> mRemotePort;
204
  bool mAddressReuse;
205
  bool mLoopback;
206
  SocketReadyState mReadyState;
207
  RefPtr<Promise> mOpened;
208
  RefPtr<Promise> mClosed;
209
210
  nsCOMPtr<nsIUDPSocket> mSocket;
211
  nsCOMPtr<nsIUDPSocketChild> mSocketChild;
212
  RefPtr<ListenerProxy> mListenerProxy;
213
214
  struct MulticastCommand {
215
    enum CommandType { Join, Leave };
216
217
    MulticastCommand(CommandType aCommand, const nsAString& aAddress)
218
      : mCommand(aCommand), mAddress(aAddress)
219
0
    { }
220
221
    CommandType mCommand;
222
    nsString mAddress;
223
  };
224
225
  nsTArray<MulticastCommand> mPendingMcastCommands;
226
};
227
228
} // namespace dom
229
} // namespace mozilla
230
231
#endif // mozilla_dom_UDPSocket_h__