content/base/src/WebSocket.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set sw=2 ts=8 et tw=80 : */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef WebSocket_h__
michael@0 8 #define WebSocket_h__
michael@0 9
michael@0 10 #include "mozilla/Attributes.h"
michael@0 11 #include "mozilla/dom/TypedArray.h"
michael@0 12 #include "mozilla/dom/WebSocketBinding.h" // for BinaryType
michael@0 13 #include "mozilla/DOMEventTargetHelper.h"
michael@0 14 #include "mozilla/ErrorResult.h"
michael@0 15 #include "nsAutoPtr.h"
michael@0 16 #include "nsCOMPtr.h"
michael@0 17 #include "nsCycleCollectionParticipant.h"
michael@0 18 #include "nsIInterfaceRequestor.h"
michael@0 19 #include "nsIObserver.h"
michael@0 20 #include "nsIRequest.h"
michael@0 21 #include "nsISupports.h"
michael@0 22 #include "nsISupportsUtils.h"
michael@0 23 #include "nsIWebSocketChannel.h"
michael@0 24 #include "nsIWebSocketListener.h"
michael@0 25 #include "nsString.h"
michael@0 26 #include "nsWeakReference.h"
michael@0 27 #include "nsWrapperCache.h"
michael@0 28
michael@0 29 #define DEFAULT_WS_SCHEME_PORT 80
michael@0 30 #define DEFAULT_WSS_SCHEME_PORT 443
michael@0 31
michael@0 32 namespace mozilla {
michael@0 33 namespace dom {
michael@0 34
michael@0 35 class WebSocket : public DOMEventTargetHelper,
michael@0 36 public nsIInterfaceRequestor,
michael@0 37 public nsIWebSocketListener,
michael@0 38 public nsIObserver,
michael@0 39 public nsSupportsWeakReference,
michael@0 40 public nsIRequest
michael@0 41 {
michael@0 42 friend class CallDispatchConnectionCloseEvents;
michael@0 43 friend class nsAutoCloseWS;
michael@0 44
michael@0 45 public:
michael@0 46 enum {
michael@0 47 CONNECTING = 0,
michael@0 48 OPEN = 1,
michael@0 49 CLOSING = 2,
michael@0 50 CLOSED = 3
michael@0 51 };
michael@0 52
michael@0 53 public:
michael@0 54 NS_DECL_ISUPPORTS_INHERITED
michael@0 55 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
michael@0 56 WebSocket, DOMEventTargetHelper)
michael@0 57 NS_DECL_NSIINTERFACEREQUESTOR
michael@0 58 NS_DECL_NSIWEBSOCKETLISTENER
michael@0 59 NS_DECL_NSIOBSERVER
michael@0 60 NS_DECL_NSIREQUEST
michael@0 61
michael@0 62 // EventTarget
michael@0 63 virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE;
michael@0 64 virtual void EventListenerRemoved(nsIAtom* aType) MOZ_OVERRIDE;
michael@0 65
michael@0 66 virtual void DisconnectFromOwner() MOZ_OVERRIDE;
michael@0 67
michael@0 68 // nsWrapperCache
michael@0 69 nsPIDOMWindow* GetParentObject() { return GetOwner(); }
michael@0 70
michael@0 71 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
michael@0 72
michael@0 73 public: // static helpers:
michael@0 74
michael@0 75 // Determine if preferences allow WebSocket
michael@0 76 static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
michael@0 77
michael@0 78 public: // WebIDL interface:
michael@0 79
michael@0 80 // Constructor:
michael@0 81 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
michael@0 82 const nsAString& aUrl,
michael@0 83 ErrorResult& rv);
michael@0 84
michael@0 85 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
michael@0 86 const nsAString& aUrl,
michael@0 87 const nsAString& aProtocol,
michael@0 88 ErrorResult& rv);
michael@0 89
michael@0 90 static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
michael@0 91 const nsAString& aUrl,
michael@0 92 const Sequence<nsString>& aProtocols,
michael@0 93 ErrorResult& rv);
michael@0 94
michael@0 95 // webIDL: readonly attribute DOMString url
michael@0 96 void GetUrl(nsAString& aResult);
michael@0 97
michael@0 98 // webIDL: readonly attribute unsigned short readyState;
michael@0 99 uint16_t ReadyState() const { return mReadyState; }
michael@0 100
michael@0 101 // webIDL: readonly attribute unsigned long bufferedAmount;
michael@0 102 uint32_t BufferedAmount() const { return mOutgoingBufferedAmount; }
michael@0 103
michael@0 104 // webIDL: attribute Function? onopen;
michael@0 105 IMPL_EVENT_HANDLER(open)
michael@0 106
michael@0 107 // webIDL: attribute Function? onerror;
michael@0 108 IMPL_EVENT_HANDLER(error)
michael@0 109
michael@0 110 // webIDL: attribute Function? onclose;
michael@0 111 IMPL_EVENT_HANDLER(close)
michael@0 112
michael@0 113 // webIDL: readonly attribute DOMString extensions;
michael@0 114 void GetExtensions(nsAString& aResult);
michael@0 115
michael@0 116 // webIDL: readonly attribute DOMString protocol;
michael@0 117 void GetProtocol(nsAString& aResult);
michael@0 118
michael@0 119 // webIDL: void close(optional unsigned short code, optional DOMString reason):
michael@0 120 void Close(const Optional<uint16_t>& aCode,
michael@0 121 const Optional<nsAString>& aReason,
michael@0 122 ErrorResult& aRv);
michael@0 123
michael@0 124 // webIDL: attribute Function? onmessage;
michael@0 125 IMPL_EVENT_HANDLER(message)
michael@0 126
michael@0 127 // webIDL: attribute DOMString binaryType;
michael@0 128 dom::BinaryType BinaryType() const { return mBinaryType; }
michael@0 129 void SetBinaryType(dom::BinaryType aData) { mBinaryType = aData; }
michael@0 130
michael@0 131 // webIDL: void send(DOMString|Blob|ArrayBufferView data);
michael@0 132 void Send(const nsAString& aData,
michael@0 133 ErrorResult& aRv);
michael@0 134 void Send(nsIDOMBlob* aData,
michael@0 135 ErrorResult& aRv);
michael@0 136 void Send(const ArrayBuffer& aData,
michael@0 137 ErrorResult& aRv);
michael@0 138 void Send(const ArrayBufferView& aData,
michael@0 139 ErrorResult& aRv);
michael@0 140
michael@0 141 private: // constructor && distructor
michael@0 142 WebSocket(nsPIDOMWindow* aOwnerWindow);
michael@0 143 virtual ~WebSocket();
michael@0 144
michael@0 145 protected:
michael@0 146 nsresult Init(JSContext* aCx,
michael@0 147 nsIPrincipal* aPrincipal,
michael@0 148 const nsAString& aURL,
michael@0 149 nsTArray<nsString>& aProtocolArray);
michael@0 150
michael@0 151 void Send(nsIInputStream* aMsgStream,
michael@0 152 const nsACString& aMsgString,
michael@0 153 uint32_t aMsgLength,
michael@0 154 bool aIsBinary,
michael@0 155 ErrorResult& aRv);
michael@0 156
michael@0 157 nsresult ParseURL(const nsString& aURL);
michael@0 158 nsresult EstablishConnection();
michael@0 159
michael@0 160 // These methods when called can release the WebSocket object
michael@0 161 void FailConnection(uint16_t reasonCode,
michael@0 162 const nsACString& aReasonString = EmptyCString());
michael@0 163 nsresult CloseConnection(uint16_t reasonCode,
michael@0 164 const nsACString& aReasonString = EmptyCString());
michael@0 165 nsresult Disconnect();
michael@0 166
michael@0 167 nsresult ConsoleError();
michael@0 168 nsresult PrintErrorOnConsole(const char* aBundleURI,
michael@0 169 const char16_t* aError,
michael@0 170 const char16_t** aFormatStrings,
michael@0 171 uint32_t aFormatStringsLen);
michael@0 172
michael@0 173 nsresult DoOnMessageAvailable(const nsACString& aMsg,
michael@0 174 bool isBinary);
michael@0 175
michael@0 176 // ConnectionCloseEvents: 'error' event if needed, then 'close' event.
michael@0 177 // - These must not be dispatched while we are still within an incoming call
michael@0 178 // from JS (ex: close()). Set 'sync' to false in that case to dispatch in a
michael@0 179 // separate new event.
michael@0 180 nsresult ScheduleConnectionCloseEvents(nsISupports* aContext,
michael@0 181 nsresult aStatusCode,
michael@0 182 bool sync);
michael@0 183 // 2nd half of ScheduleConnectionCloseEvents, sometimes run in its own event.
michael@0 184 void DispatchConnectionCloseEvents();
michael@0 185
michael@0 186 // These methods actually do the dispatch for various events.
michael@0 187 nsresult CreateAndDispatchSimpleEvent(const nsString& aName);
michael@0 188 nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
michael@0 189 bool isBinary);
michael@0 190 nsresult CreateAndDispatchCloseEvent(bool aWasClean,
michael@0 191 uint16_t aCode,
michael@0 192 const nsString& aReason);
michael@0 193
michael@0 194 // if there are "strong event listeners" (see comment in WebSocket.cpp) or
michael@0 195 // outgoing not sent messages then this method keeps the object alive
michael@0 196 // when js doesn't have strong references to it.
michael@0 197 void UpdateMustKeepAlive();
michael@0 198 // ATTENTION, when calling this method the object can be released
michael@0 199 // (and possibly collected).
michael@0 200 void DontKeepAliveAnyMore();
michael@0 201
michael@0 202 nsresult UpdateURI();
michael@0 203
michael@0 204 protected: //data
michael@0 205
michael@0 206 nsCOMPtr<nsIWebSocketChannel> mChannel;
michael@0 207
michael@0 208 // related to the WebSocket constructor steps
michael@0 209 nsString mOriginalURL;
michael@0 210 nsString mEffectiveURL; // after redirects
michael@0 211 bool mSecure; // if true it is using SSL and the wss scheme,
michael@0 212 // otherwise it is using the ws scheme with no SSL
michael@0 213
michael@0 214 bool mKeepingAlive;
michael@0 215 bool mCheckMustKeepAlive;
michael@0 216 bool mOnCloseScheduled;
michael@0 217 bool mFailed;
michael@0 218 bool mDisconnected;
michael@0 219
michael@0 220 // Set attributes of DOM 'onclose' message
michael@0 221 bool mCloseEventWasClean;
michael@0 222 nsString mCloseEventReason;
michael@0 223 uint16_t mCloseEventCode;
michael@0 224
michael@0 225 nsCString mAsciiHost; // hostname
michael@0 226 uint32_t mPort;
michael@0 227 nsCString mResource; // [filepath[?query]]
michael@0 228 nsString mUTF16Origin;
michael@0 229
michael@0 230 nsCOMPtr<nsIURI> mURI;
michael@0 231 nsCString mRequestedProtocolList;
michael@0 232 nsCString mEstablishedProtocol;
michael@0 233 nsCString mEstablishedExtensions;
michael@0 234
michael@0 235 uint16_t mReadyState;
michael@0 236
michael@0 237 nsCOMPtr<nsIPrincipal> mPrincipal;
michael@0 238
michael@0 239 uint32_t mOutgoingBufferedAmount;
michael@0 240
michael@0 241 dom::BinaryType mBinaryType;
michael@0 242
michael@0 243 // Web Socket owner information:
michael@0 244 // - the script file name, UTF8 encoded.
michael@0 245 // - source code line number where the Web Socket object was constructed.
michael@0 246 // - the ID of the inner window where the script lives. Note that this may not
michael@0 247 // be the same as the Web Socket owner window.
michael@0 248 // These attributes are used for error reporting.
michael@0 249 nsCString mScriptFile;
michael@0 250 uint32_t mScriptLine;
michael@0 251 uint64_t mInnerWindowID;
michael@0 252
michael@0 253 private:
michael@0 254 WebSocket(const WebSocket& x) MOZ_DELETE; // prevent bad usage
michael@0 255 WebSocket& operator=(const WebSocket& x) MOZ_DELETE;
michael@0 256 };
michael@0 257
michael@0 258 } //namespace dom
michael@0 259 } //namespace mozilla
michael@0 260
michael@0 261 #endif

mercurial