Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / websocket / WebSocket.h
bloba290750cf0338a55fb385f0b9aa462c609587333
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/. */
7 #ifndef WebSocket_h__
8 #define WebSocket_h__
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/Mutex.h"
16 #include "nsCOMPtr.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsISupports.h"
19 #include "nsISupportsUtils.h"
20 #include "nsString.h"
21 #include "nsWrapperCache.h"
23 #define DEFAULT_WS_SCHEME_PORT 80
24 #define DEFAULT_WSS_SCHEME_PORT 443
26 class nsIInputStream;
27 class nsITransportProvider;
29 namespace mozilla {
30 class ErrorResult;
32 namespace dom {
34 class Blob;
35 class StringOrStringSequence;
36 class WebSocketImpl;
38 class WebSocket final : public DOMEventTargetHelper {
39 friend class WebSocketImpl;
41 public:
42 enum { CONNECTING = 0, OPEN = 1, CLOSING = 2, CLOSED = 3 };
44 public:
45 NS_DECL_ISUPPORTS_INHERITED
46 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WebSocket, DOMEventTargetHelper)
47 virtual bool IsCertainlyAliveForCC() const override;
49 // EventTarget
50 using EventTarget::EventListenerAdded;
51 virtual void EventListenerAdded(nsAtom* aType) override;
53 using EventTarget::EventListenerRemoved;
54 virtual void EventListenerRemoved(nsAtom* aType) override;
56 virtual void DisconnectFromOwner() override;
58 mozilla::Maybe<EventCallbackDebuggerNotificationType>
59 GetDebuggerNotificationType() const override;
61 // nsWrapperCache
62 virtual JSObject* WrapObject(JSContext* cx,
63 JS::Handle<JSObject*> aGivenProto) override;
65 public: // static helpers:
66 // Determine if preferences allow WebSocket
67 static bool PrefEnabled(JSContext* aCx = nullptr,
68 JSObject* aGlobal = nullptr);
70 public: // WebIDL interface:
71 // Constructor:
72 static already_AddRefed<WebSocket> Constructor(
73 const GlobalObject& aGlobal, const nsAString& aUrl,
74 const StringOrStringSequence& aProtocols, ErrorResult& rv);
76 static already_AddRefed<WebSocket> CreateServerWebSocket(
77 const GlobalObject& aGlobal, const nsAString& aUrl,
78 const Sequence<nsString>& aProtocols,
79 nsITransportProvider* aTransportProvider,
80 const nsAString& aNegotiatedExtensions, ErrorResult& rv);
82 static already_AddRefed<WebSocket> ConstructorCommon(
83 const GlobalObject& aGlobal, const nsAString& aUrl,
84 const Sequence<nsString>& aProtocols,
85 nsITransportProvider* aTransportProvider,
86 const nsACString& aNegotiatedExtensions, ErrorResult& rv);
88 // webIDL: readonly attribute DOMString url
89 void GetUrl(nsAString& aResult);
91 // webIDL: readonly attribute unsigned short readyState;
92 uint16_t ReadyState();
94 // webIDL: readonly attribute unsigned long long bufferedAmount;
95 uint64_t BufferedAmount() const;
97 // webIDL: attribute Function? onopen;
98 IMPL_EVENT_HANDLER(open)
100 // webIDL: attribute Function? onerror;
101 IMPL_EVENT_HANDLER(error)
103 // webIDL: attribute Function? onclose;
104 IMPL_EVENT_HANDLER(close)
106 // webIDL: readonly attribute DOMString extensions;
107 void GetExtensions(nsAString& aResult);
109 // webIDL: readonly attribute DOMString protocol;
110 void GetProtocol(nsAString& aResult);
112 // webIDL: void close(optional unsigned short code,
113 // optional DOMString reason):
114 void Close(const Optional<uint16_t>& aCode,
115 const Optional<nsAString>& aReason, ErrorResult& aRv);
117 // webIDL: attribute Function? onmessage;
118 IMPL_EVENT_HANDLER(message)
120 // webIDL: attribute DOMString binaryType;
121 dom::BinaryType BinaryType() const;
122 void SetBinaryType(dom::BinaryType aData);
124 // webIDL: void send(DOMString|Blob|ArrayBufferView data);
125 void Send(const nsAString& aData, ErrorResult& aRv);
126 void Send(Blob& aData, ErrorResult& aRv);
127 void Send(const ArrayBuffer& aData, ErrorResult& aRv);
128 void Send(const ArrayBufferView& aData, ErrorResult& aRv);
130 private: // constructor && destructor
131 explicit WebSocket(nsIGlobalObject* aGlobal);
132 virtual ~WebSocket();
134 void SetReadyState(uint16_t aReadyState);
136 // These methods actually do the dispatch for various events.
137 nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
138 nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
139 bool aIsBinary);
140 nsresult CreateAndDispatchCloseEvent(bool aWasClean, uint16_t aCode,
141 const nsAString& aReason);
143 static bool IsValidProtocolString(const nsString& aValue);
145 // if there are "strong event listeners" (see comment in WebSocket.cpp) or
146 // outgoing not sent messages then this method keeps the object alive
147 // when js doesn't have strong references to it.
148 void UpdateMustKeepAlive();
149 // ATTENTION, when calling this method the object can be released
150 // (and possibly collected).
151 void DontKeepAliveAnyMore();
153 private:
154 WebSocket(const WebSocket& x) = delete; // prevent bad usage
155 WebSocket& operator=(const WebSocket& x) = delete;
157 void Send(nsIInputStream* aMsgStream, const nsACString& aMsgString,
158 uint32_t aMsgLength, bool aIsBinary, ErrorResult& aRv);
160 void AssertIsOnTargetThread() const;
162 // Raw pointer because this WebSocketImpl is created, managed and destroyed by
163 // WebSocket.
164 WebSocketImpl* mImpl;
166 bool mIsMainThread;
168 bool mKeepingAlive;
169 bool mCheckMustKeepAlive;
171 CheckedUint64 mOutgoingBufferedAmount;
173 // related to the WebSocket constructor steps
174 nsString mURI;
175 nsString mEffectiveURL; // after redirects
176 nsCString mEstablishedExtensions;
177 nsCString mEstablishedProtocol;
179 dom::BinaryType mBinaryType;
181 // This mutex protects mReadyState that is the only variable that is used in
182 // different threads.
183 mozilla::Mutex mMutex;
185 // This value should not be used directly but use ReadyState() instead.
186 uint16_t mReadyState MOZ_GUARDED_BY(mMutex);
189 } // namespace dom
190 } // namespace mozilla
192 #endif