Bumping manifests a=b2g-bump
[gecko.git] / dom / base / WebSocket.h
blob515f766d7c2a6f27359ef844ccbdd92b1627e40f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et 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/dom/TypedArray.h"
12 #include "mozilla/dom/WebSocketBinding.h" // for BinaryType
13 #include "mozilla/DOMEventTargetHelper.h"
14 #include "mozilla/ErrorResult.h"
15 #include "nsAutoPtr.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;
28 namespace mozilla {
29 namespace dom {
31 namespace workers {
32 class WorkerPrivate;
35 class File;
37 class WebSocketImpl;
39 class WebSocket MOZ_FINAL : public DOMEventTargetHelper
41 friend class WebSocketImpl;
43 public:
44 enum {
45 CONNECTING = 0,
46 OPEN = 1,
47 CLOSING = 2,
48 CLOSED = 3
51 public:
52 NS_DECL_ISUPPORTS_INHERITED
53 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
54 WebSocket, DOMEventTargetHelper)
56 // EventTarget
57 virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE;
58 virtual void EventListenerRemoved(nsIAtom* aType) MOZ_OVERRIDE;
60 virtual void DisconnectFromOwner() MOZ_OVERRIDE;
62 // nsWrapperCache
63 nsPIDOMWindow* GetParentObject() { return GetOwner(); }
65 virtual JSObject* WrapObject(JSContext* cx) MOZ_OVERRIDE;
67 public: // static helpers:
69 // Determine if preferences allow WebSocket
70 static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
72 public: // WebIDL interface:
74 // Constructor:
75 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
76 const nsAString& aUrl,
77 ErrorResult& rv);
79 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
80 const nsAString& aUrl,
81 const nsAString& aProtocol,
82 ErrorResult& rv);
84 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
85 const nsAString& aUrl,
86 const Sequence<nsString>& aProtocols,
87 ErrorResult& rv);
89 // webIDL: readonly attribute DOMString url
90 void GetUrl(nsAString& aResult);
92 // webIDL: readonly attribute unsigned short readyState;
93 uint16_t ReadyState();
95 // webIDL: readonly attribute unsigned long bufferedAmount;
96 uint32_t BufferedAmount() const;
98 // webIDL: attribute Function? onopen;
99 IMPL_EVENT_HANDLER(open)
101 // webIDL: attribute Function? onerror;
102 IMPL_EVENT_HANDLER(error)
104 // webIDL: attribute Function? onclose;
105 IMPL_EVENT_HANDLER(close)
107 // webIDL: readonly attribute DOMString extensions;
108 void GetExtensions(nsAString& aResult);
110 // webIDL: readonly attribute DOMString protocol;
111 void GetProtocol(nsAString& aResult);
113 // webIDL: void close(optional unsigned short code, optional DOMString reason):
114 void Close(const Optional<uint16_t>& aCode,
115 const Optional<nsAString>& aReason,
116 ErrorResult& aRv);
118 // webIDL: attribute Function? onmessage;
119 IMPL_EVENT_HANDLER(message)
121 // webIDL: attribute DOMString binaryType;
122 dom::BinaryType BinaryType() const;
123 void SetBinaryType(dom::BinaryType aData);
125 // webIDL: void send(DOMString|Blob|ArrayBufferView data);
126 void Send(const nsAString& aData,
127 ErrorResult& aRv);
128 void Send(File& aData,
129 ErrorResult& aRv);
130 void Send(const ArrayBuffer& aData,
131 ErrorResult& aRv);
132 void Send(const ArrayBufferView& aData,
133 ErrorResult& aRv);
135 private: // constructor && distructor
136 explicit WebSocket(nsPIDOMWindow* aOwnerWindow);
137 virtual ~WebSocket();
139 void SetReadyState(uint16_t aReadyState);
141 // These methods actually do the dispatch for various events.
142 nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
143 nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
144 bool aIsBinary);
145 nsresult CreateAndDispatchMessageEvent(JSContext* aCx,
146 const nsACString& aData,
147 bool aIsBinary);
148 nsresult CreateAndDispatchCloseEvent(bool aWasClean,
149 uint16_t aCode,
150 const nsAString& aReason);
152 // if there are "strong event listeners" (see comment in WebSocket.cpp) or
153 // outgoing not sent messages then this method keeps the object alive
154 // when js doesn't have strong references to it.
155 void UpdateMustKeepAlive();
156 // ATTENTION, when calling this method the object can be released
157 // (and possibly collected).
158 void DontKeepAliveAnyMore();
160 private:
161 WebSocket(const WebSocket& x) = delete; // prevent bad usage
162 WebSocket& operator=(const WebSocket& x) = delete;
164 void Send(nsIInputStream* aMsgStream,
165 const nsACString& aMsgString,
166 uint32_t aMsgLength,
167 bool aIsBinary,
168 ErrorResult& aRv);
170 void AssertIsOnTargetThread() const;
172 // Raw pointer because this WebSocketImpl is created, managed and destroyed by
173 // WebSocket.
174 WebSocketImpl* mImpl;
176 bool mIsMainThread;
178 bool mKeepingAlive;
179 bool mCheckMustKeepAlive;
181 uint32_t mOutgoingBufferedAmount;
183 // related to the WebSocket constructor steps
184 nsString mOriginalURL;
185 nsString mEffectiveURL; // after redirects
186 nsCString mEstablishedExtensions;
187 nsCString mEstablishedProtocol;
189 dom::BinaryType mBinaryType;
191 // This mutex protects mReadyState that is the only variable that is used in
192 // different threads.
193 mozilla::Mutex mMutex;
195 // This value should not be used directly but use ReadyState() instead.
196 uint16_t mReadyState;
199 } //namespace dom
200 } //namespace mozilla
202 #endif