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/. */
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"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsISupports.h"
19 #include "nsISupportsUtils.h"
21 #include "nsWrapperCache.h"
23 #define DEFAULT_WS_SCHEME_PORT 80
24 #define DEFAULT_WSS_SCHEME_PORT 443
39 class WebSocket MOZ_FINAL
: public DOMEventTargetHelper
41 friend class WebSocketImpl
;
52 NS_DECL_ISUPPORTS_INHERITED
53 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
54 WebSocket
, DOMEventTargetHelper
)
57 virtual void EventListenerAdded(nsIAtom
* aType
) MOZ_OVERRIDE
;
58 virtual void EventListenerRemoved(nsIAtom
* aType
) MOZ_OVERRIDE
;
60 virtual void DisconnectFromOwner() MOZ_OVERRIDE
;
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:
75 static already_AddRefed
<WebSocket
> Constructor(const GlobalObject
& aGlobal
,
76 const nsAString
& aUrl
,
79 static already_AddRefed
<WebSocket
> Constructor(const GlobalObject
& aGlobal
,
80 const nsAString
& aUrl
,
81 const nsAString
& aProtocol
,
84 static already_AddRefed
<WebSocket
> Constructor(const GlobalObject
& aGlobal
,
85 const nsAString
& aUrl
,
86 const Sequence
<nsString
>& aProtocols
,
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
,
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
,
128 void Send(File
& aData
,
130 void Send(const ArrayBuffer
& aData
,
132 void Send(const ArrayBufferView
& aData
,
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
,
145 nsresult
CreateAndDispatchMessageEvent(JSContext
* aCx
,
146 const nsACString
& aData
,
148 nsresult
CreateAndDispatchCloseEvent(bool aWasClean
,
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();
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
,
170 void AssertIsOnTargetThread() const;
172 // Raw pointer because this WebSocketImpl is created, managed and destroyed by
174 WebSocketImpl
* mImpl
;
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
;
200 } //namespace mozilla