Bumping manifests a=b2g-bump
[gecko.git] / dom / network / UDPSocket.h
blobe28ccb320976a1dd9f99f43e6585be8b519f77d9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #ifndef mozilla_dom_UDPSocket_h__
8 #define mozilla_dom_UDPSocket_h__
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"
19 struct JSContext;
21 namespace mozilla {
22 namespace dom {
24 struct UDPOptions;
25 class StringOrBlobOrArrayBufferOrArrayBufferView;
27 class UDPSocket MOZ_FINAL : public DOMEventTargetHelper
28 , public nsIUDPSocketListener
29 , public nsIUDPSocketInternal
31 public:
32 NS_DECL_ISUPPORTS_INHERITED
33 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UDPSocket, DOMEventTargetHelper)
34 NS_DECL_NSIUDPSOCKETLISTENER
35 NS_DECL_NSIUDPSOCKETINTERNAL
36 NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper)
38 public:
39 nsPIDOMWindow*
40 GetParentObject() const
42 return GetOwner();
45 virtual JSObject*
46 WrapObject(JSContext* aCx) MOZ_OVERRIDE;
48 virtual void
49 DisconnectFromOwner() MOZ_OVERRIDE;
51 static already_AddRefed<UDPSocket>
52 Constructor(const GlobalObject& aGlobal, const UDPOptions& aOptions, ErrorResult& aRv);
54 void
55 GetLocalAddress(nsString& aRetVal) const
57 aRetVal = mLocalAddress;
60 Nullable<uint16_t>
61 GetLocalPort() const
63 return mLocalPort;
66 void
67 GetRemoteAddress(nsString& aRetVal) const
69 if (mRemoteAddress.IsVoid()) {
70 SetDOMStringToNull(aRetVal);
71 return;
74 aRetVal = NS_ConvertUTF8toUTF16(mRemoteAddress);
77 Nullable<uint16_t>
78 GetRemotePort() const
80 return mRemotePort;
83 bool
84 AddressReuse() const
86 return mAddressReuse;
89 bool
90 Loopback() const
92 return mLoopback;
95 SocketReadyState
96 ReadyState() const
98 return mReadyState;
101 Promise*
102 Opened() const
104 return mOpened;
107 Promise*
108 Closed() const
110 return mClosed;
113 IMPL_EVENT_HANDLER(message)
115 already_AddRefed<Promise>
116 Close();
118 void
119 JoinMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
121 void
122 LeaveMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
124 bool
125 Send(const StringOrBlobOrArrayBufferOrArrayBufferView& aData,
126 const Optional<nsAString>& aRemoteAddress,
127 const Optional<Nullable<uint16_t>>& aRemotePort,
128 ErrorResult& aRv);
130 private:
131 class ListenerProxy : public nsIUDPSocketListener
132 , public nsIUDPSocketInternal
134 public:
135 NS_DECL_ISUPPORTS
136 NS_FORWARD_SAFE_NSIUDPSOCKETLISTENER(mSocket)
137 NS_FORWARD_SAFE_NSIUDPSOCKETINTERNAL(mSocket)
139 explicit ListenerProxy(UDPSocket* aSocket)
140 : mSocket(aSocket)
144 void Disconnect()
146 mSocket = nullptr;
149 private:
150 virtual ~ListenerProxy() {}
152 UDPSocket* mSocket;
155 UDPSocket(nsPIDOMWindow* aOwner,
156 const nsCString& aRemoteAddress,
157 const Nullable<uint16_t>& aRemotePort);
159 virtual ~UDPSocket();
161 nsresult
162 Init(const nsString& aLocalAddress,
163 const Nullable<uint16_t>& aLocalPort,
164 const bool& aAddressReuse,
165 const bool& aLoopback);
167 nsresult
168 InitLocal(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
170 nsresult
171 InitRemote(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
173 void
174 HandleReceivedData(const nsACString& aRemoteAddress,
175 const uint16_t& aRemotePort,
176 const uint8_t* aData,
177 const uint32_t& aDataLength);
179 nsresult
180 DispatchReceivedData(const nsACString& aRemoteAddress,
181 const uint16_t& aRemotePort,
182 const uint8_t* aData,
183 const uint32_t& aDataLength);
185 void
186 CloseWithReason(nsresult aReason);
188 nsresult
189 DoPendingMcastCommand();
191 nsString mLocalAddress;
192 Nullable<uint16_t> mLocalPort;
193 nsCString mRemoteAddress;
194 Nullable<uint16_t> mRemotePort;
195 bool mAddressReuse;
196 bool mLoopback;
197 SocketReadyState mReadyState;
198 nsRefPtr<Promise> mOpened;
199 nsRefPtr<Promise> mClosed;
201 nsCOMPtr<nsIUDPSocket> mSocket;
202 nsCOMPtr<nsIUDPSocketChild> mSocketChild;
203 nsRefPtr<ListenerProxy> mListenerProxy;
205 struct MulticastCommand {
206 enum CommandType { Join, Leave };
208 MulticastCommand(CommandType aCommand, const nsAString& aAddress)
209 : mCommand(aCommand), mAddress(aAddress)
212 CommandType mCommand;
213 nsString mAddress;
216 nsTArray<MulticastCommand> mPendingMcastCommands;
219 } // namespace dom
220 } // namespace mozilla
222 #endif // mozilla_dom_UDPSocket_h__