/work/obj-fuzz/dist/include/mozilla/dom/WebSocket.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 WebSocket_h__ |
8 | | #define WebSocket_h__ |
9 | | |
10 | | #include "mozilla/Attributes.h" |
11 | | #include "mozilla/CheckedInt.h" |
12 | | #include "mozilla/dom/TypedArray.h" |
13 | | #include "mozilla/dom/WebSocketBinding.h" // for BinaryType |
14 | | #include "mozilla/DOMEventTargetHelper.h" |
15 | | #include "mozilla/ErrorResult.h" |
16 | | #include "mozilla/Mutex.h" |
17 | | #include "nsCOMPtr.h" |
18 | | #include "nsCycleCollectionParticipant.h" |
19 | | #include "nsISupports.h" |
20 | | #include "nsISupportsUtils.h" |
21 | | #include "nsString.h" |
22 | | #include "nsWrapperCache.h" |
23 | | |
24 | | #define DEFAULT_WS_SCHEME_PORT 80 |
25 | | #define DEFAULT_WSS_SCHEME_PORT 443 |
26 | | |
27 | | class nsIInputStream; |
28 | | class nsITransportProvider; |
29 | | |
30 | | namespace mozilla { |
31 | | namespace dom { |
32 | | |
33 | | class Blob; |
34 | | |
35 | | class WebSocketImpl; |
36 | | |
37 | | class WebSocket final : public DOMEventTargetHelper |
38 | | { |
39 | | friend class WebSocketImpl; |
40 | | |
41 | | public: |
42 | | enum { |
43 | | CONNECTING = 0, |
44 | | OPEN = 1, |
45 | | CLOSING = 2, |
46 | | CLOSED = 3 |
47 | | }; |
48 | | |
49 | | public: |
50 | | NS_DECL_ISUPPORTS_INHERITED |
51 | | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WebSocket, DOMEventTargetHelper) |
52 | | virtual bool IsCertainlyAliveForCC() const override; |
53 | | |
54 | | // EventTarget |
55 | | using EventTarget::EventListenerAdded; |
56 | | virtual void EventListenerAdded(nsAtom* aType) override; |
57 | | |
58 | | using EventTarget::EventListenerRemoved; |
59 | | virtual void EventListenerRemoved(nsAtom* aType) override; |
60 | | |
61 | | virtual void DisconnectFromOwner() override; |
62 | | |
63 | | // nsWrapperCache |
64 | 0 | nsPIDOMWindowInner* GetParentObject() { return GetOwner(); } |
65 | | |
66 | | virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto) override; |
67 | | |
68 | | // DOMEventTargetHelper |
69 | | void BindToOwner(nsIGlobalObject* aNew) override; |
70 | | |
71 | | public: // static helpers: |
72 | | |
73 | | // Determine if preferences allow WebSocket |
74 | | static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr); |
75 | | |
76 | | public: // WebIDL interface: |
77 | | |
78 | | // Constructor: |
79 | | static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal, |
80 | | const nsAString& aUrl, |
81 | | ErrorResult& rv); |
82 | | |
83 | | static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal, |
84 | | const nsAString& aUrl, |
85 | | const nsAString& aProtocol, |
86 | | ErrorResult& rv); |
87 | | |
88 | | static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal, |
89 | | const nsAString& aUrl, |
90 | | const Sequence<nsString>& aProtocols, |
91 | | ErrorResult& rv); |
92 | | |
93 | | static already_AddRefed<WebSocket> CreateServerWebSocket(const GlobalObject& aGlobal, |
94 | | const nsAString& aUrl, |
95 | | const Sequence<nsString>& aProtocols, |
96 | | nsITransportProvider* aTransportProvider, |
97 | | const nsAString& aNegotiatedExtensions, |
98 | | ErrorResult& rv); |
99 | | |
100 | | static already_AddRefed<WebSocket> ConstructorCommon(const GlobalObject& aGlobal, |
101 | | const nsAString& aUrl, |
102 | | const Sequence<nsString>& aProtocols, |
103 | | nsITransportProvider* aTransportProvider, |
104 | | const nsACString& aNegotiatedExtensions, |
105 | | ErrorResult& rv); |
106 | | |
107 | | // webIDL: readonly attribute DOMString url |
108 | | void GetUrl(nsAString& aResult); |
109 | | |
110 | | // webIDL: readonly attribute unsigned short readyState; |
111 | | uint16_t ReadyState(); |
112 | | |
113 | | // webIDL: readonly attribute unsigned long bufferedAmount; |
114 | | uint32_t BufferedAmount() const; |
115 | | |
116 | | // webIDL: attribute Function? onopen; |
117 | | IMPL_EVENT_HANDLER(open) |
118 | | |
119 | | // webIDL: attribute Function? onerror; |
120 | | IMPL_EVENT_HANDLER(error) |
121 | | |
122 | | // webIDL: attribute Function? onclose; |
123 | | IMPL_EVENT_HANDLER(close) |
124 | | |
125 | | // webIDL: readonly attribute DOMString extensions; |
126 | | void GetExtensions(nsAString& aResult); |
127 | | |
128 | | // webIDL: readonly attribute DOMString protocol; |
129 | | void GetProtocol(nsAString& aResult); |
130 | | |
131 | | // webIDL: void close(optional unsigned short code, optional DOMString reason): |
132 | | void Close(const Optional<uint16_t>& aCode, |
133 | | const Optional<nsAString>& aReason, |
134 | | ErrorResult& aRv); |
135 | | |
136 | | // webIDL: attribute Function? onmessage; |
137 | | IMPL_EVENT_HANDLER(message) |
138 | | |
139 | | // webIDL: attribute DOMString binaryType; |
140 | | dom::BinaryType BinaryType() const; |
141 | | void SetBinaryType(dom::BinaryType aData); |
142 | | |
143 | | // webIDL: void send(DOMString|Blob|ArrayBufferView data); |
144 | | void Send(const nsAString& aData, |
145 | | ErrorResult& aRv); |
146 | | void Send(Blob& aData, |
147 | | ErrorResult& aRv); |
148 | | void Send(const ArrayBuffer& aData, |
149 | | ErrorResult& aRv); |
150 | | void Send(const ArrayBufferView& aData, |
151 | | ErrorResult& aRv); |
152 | | |
153 | | private: // constructor && destructor |
154 | | explicit WebSocket(nsPIDOMWindowInner* aOwnerWindow); |
155 | | virtual ~WebSocket(); |
156 | | |
157 | | void SetReadyState(uint16_t aReadyState); |
158 | | |
159 | | // These methods actually do the dispatch for various events. |
160 | | nsresult CreateAndDispatchSimpleEvent(const nsAString& aName); |
161 | | nsresult CreateAndDispatchMessageEvent(const nsACString& aData, |
162 | | bool aIsBinary); |
163 | | nsresult CreateAndDispatchCloseEvent(bool aWasClean, |
164 | | uint16_t aCode, |
165 | | const nsAString& aReason); |
166 | | |
167 | | // if there are "strong event listeners" (see comment in WebSocket.cpp) or |
168 | | // outgoing not sent messages then this method keeps the object alive |
169 | | // when js doesn't have strong references to it. |
170 | | void UpdateMustKeepAlive(); |
171 | | // ATTENTION, when calling this method the object can be released |
172 | | // (and possibly collected). |
173 | | void DontKeepAliveAnyMore(); |
174 | | |
175 | | private: |
176 | | WebSocket(const WebSocket& x) = delete; // prevent bad usage |
177 | | WebSocket& operator=(const WebSocket& x) = delete; |
178 | | |
179 | | void Send(nsIInputStream* aMsgStream, |
180 | | const nsACString& aMsgString, |
181 | | uint32_t aMsgLength, |
182 | | bool aIsBinary, |
183 | | ErrorResult& aRv); |
184 | | |
185 | | void AssertIsOnTargetThread() const; |
186 | | |
187 | | // Raw pointer because this WebSocketImpl is created, managed and destroyed by |
188 | | // WebSocket. |
189 | | WebSocketImpl* mImpl; |
190 | | |
191 | | bool mIsMainThread; |
192 | | |
193 | | bool mKeepingAlive; |
194 | | bool mCheckMustKeepAlive; |
195 | | |
196 | | CheckedUint32 mOutgoingBufferedAmount; |
197 | | |
198 | | // related to the WebSocket constructor steps |
199 | | nsString mURI; |
200 | | nsString mEffectiveURL; // after redirects |
201 | | nsCString mEstablishedExtensions; |
202 | | nsCString mEstablishedProtocol; |
203 | | |
204 | | dom::BinaryType mBinaryType; |
205 | | |
206 | | // This mutex protects mReadyState that is the only variable that is used in |
207 | | // different threads. |
208 | | mozilla::Mutex mMutex; |
209 | | |
210 | | // This value should not be used directly but use ReadyState() instead. |
211 | | uint16_t mReadyState; |
212 | | }; |
213 | | |
214 | | } //namespace dom |
215 | | } //namespace mozilla |
216 | | |
217 | | #endif |