/work/obj-fuzz/dist/include/mozilla/dom/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 | | #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 | 0 | { |
51 | 0 | return GetOwner(); |
52 | 0 | } |
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 | 0 | { |
66 | 0 | aRetVal = mLocalAddress; |
67 | 0 | } |
68 | | |
69 | | Nullable<uint16_t> |
70 | | GetLocalPort() const |
71 | 0 | { |
72 | 0 | return mLocalPort; |
73 | 0 | } |
74 | | |
75 | | void |
76 | | GetRemoteAddress(nsString& aRetVal) const |
77 | 0 | { |
78 | 0 | if (mRemoteAddress.IsVoid()) { |
79 | 0 | SetDOMStringToNull(aRetVal); |
80 | 0 | return; |
81 | 0 | } |
82 | 0 | |
83 | 0 | aRetVal = NS_ConvertUTF8toUTF16(mRemoteAddress); |
84 | 0 | } |
85 | | |
86 | | Nullable<uint16_t> |
87 | | GetRemotePort() const |
88 | 0 | { |
89 | 0 | return mRemotePort; |
90 | 0 | } |
91 | | |
92 | | bool |
93 | | AddressReuse() const |
94 | 0 | { |
95 | 0 | return mAddressReuse; |
96 | 0 | } |
97 | | |
98 | | bool |
99 | | Loopback() const |
100 | 0 | { |
101 | 0 | return mLoopback; |
102 | 0 | } |
103 | | |
104 | | SocketReadyState |
105 | | ReadyState() const |
106 | 0 | { |
107 | 0 | return mReadyState; |
108 | 0 | } |
109 | | |
110 | | Promise* |
111 | | Opened() const |
112 | 0 | { |
113 | 0 | return mOpened; |
114 | 0 | } |
115 | | |
116 | | Promise* |
117 | | Closed() const |
118 | 0 | { |
119 | 0 | return mClosed; |
120 | 0 | } |
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 | | { |
151 | | } |
152 | | |
153 | | void Disconnect() |
154 | | { |
155 | | mSocket = nullptr; |
156 | | } |
157 | | |
158 | | private: |
159 | | 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 | | { } |
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__ |