forked from mirrors/gecko-dev
		
	 3794888bc4
			
		
	
	
		3794888bc4
		
	
	
	
	
		
			
			--HG-- rename : dom/base/WebSocket.cpp => dom/websocket/WebSocket.cpp rename : dom/base/WebSocket.h => dom/websocket/WebSocket.h rename : dom/base/test/file_bug1384658.html => dom/websocket/tests/file_bug1384658.html rename : dom/base/test/file_websocket_basic_wsh.py => dom/websocket/tests/file_websocket_basic_wsh.py rename : dom/base/test/file_websocket_bigBlob_wsh.py => dom/websocket/tests/file_websocket_bigBlob_wsh.py rename : dom/base/test/file_websocket_hello_wsh.py => dom/websocket/tests/file_websocket_hello_wsh.py rename : dom/base/test/file_websocket_http_resource.txt => dom/websocket/tests/file_websocket_http_resource.txt rename : dom/base/test/file_websocket_permessage_deflate_disabled_wsh.py => dom/websocket/tests/file_websocket_permessage_deflate_disabled_wsh.py rename : dom/base/test/file_websocket_permessage_deflate_params_wsh.py => dom/websocket/tests/file_websocket_permessage_deflate_params_wsh.py rename : dom/base/test/file_websocket_permessage_deflate_rejected_wsh.py => dom/websocket/tests/file_websocket_permessage_deflate_rejected_wsh.py rename : dom/base/test/file_websocket_permessage_deflate_wsh.py => dom/websocket/tests/file_websocket_permessage_deflate_wsh.py rename : dom/base/test/file_websocket_wsh.py => dom/websocket/tests/file_websocket_wsh.py rename : dom/base/test/frame_bug1384658.html => dom/websocket/tests/frame_bug1384658.html rename : dom/base/test/iframe_webSocket_sandbox.html => dom/websocket/tests/iframe_webSocket_sandbox.html rename : dom/base/test/test_bug1081686.html => dom/websocket/tests/test_bug1081686.html rename : dom/base/test/test_bug1384658.html => dom/websocket/tests/test_bug1384658.html rename : dom/base/test/test_webSocket_sandbox.html => dom/websocket/tests/test_webSocket_sandbox.html rename : dom/workers/test/test_webSocket_sharedWorker.html => dom/websocket/tests/test_webSocket_sharedWorker.html rename : dom/base/test/test_websocket1.html => dom/websocket/tests/test_websocket1.html rename : dom/base/test/test_websocket2.html => dom/websocket/tests/test_websocket2.html rename : dom/base/test/test_websocket3.html => dom/websocket/tests/test_websocket3.html rename : dom/base/test/test_websocket4.html => dom/websocket/tests/test_websocket4.html rename : dom/base/test/test_websocket5.html => dom/websocket/tests/test_websocket5.html rename : dom/base/test/test_websocket_basic.html => dom/websocket/tests/test_websocket_basic.html rename : dom/base/test/test_websocket_bigBlob.html => dom/websocket/tests/test_websocket_bigBlob.html rename : dom/base/test/test_websocket_frame.html => dom/websocket/tests/test_websocket_frame.html rename : dom/base/test/test_websocket_hello.html => dom/websocket/tests/test_websocket_hello.html rename : dom/base/test/test_websocket_permessage_deflate.html => dom/websocket/tests/test_websocket_permessage_deflate.html rename : dom/workers/test/test_websocket1.html => dom/websocket/tests/test_worker_websocket1.html rename : dom/workers/test/test_websocket2.html => dom/websocket/tests/test_worker_websocket2.html rename : dom/workers/test/test_websocket3.html => dom/websocket/tests/test_worker_websocket3.html rename : dom/workers/test/test_websocket4.html => dom/websocket/tests/test_worker_websocket4.html rename : dom/workers/test/test_websocket5.html => dom/websocket/tests/test_worker_websocket5.html rename : dom/workers/test/test_websocket_basic.html => dom/websocket/tests/test_worker_websocket_basic.html rename : dom/workers/test/test_websocket_https.html => dom/websocket/tests/test_worker_websocket_https.html rename : dom/workers/test/test_websocket_loadgroup.html => dom/websocket/tests/test_worker_websocket_loadgroup.html rename : dom/workers/test/webSocket_sharedWorker.js => dom/websocket/tests/webSocket_sharedWorker.js rename : dom/workers/test/websocket_basic_worker.js => dom/websocket/tests/websocket_basic_worker.js rename : dom/base/test/websocket_helpers.js => dom/websocket/tests/websocket_helpers.js rename : dom/workers/test/websocket_https_worker.js => dom/websocket/tests/websocket_https_worker.js rename : dom/base/test/websocket_hybi/file_binary-frames_wsh.py => dom/websocket/tests/websocket_hybi/file_binary-frames_wsh.py rename : dom/base/test/websocket_hybi/file_check-binary-messages_wsh.py => dom/websocket/tests/websocket_hybi/file_check-binary-messages_wsh.py rename : dom/base/test/websocket_hybi/mochitest.ini => dom/websocket/tests/websocket_hybi/mochitest.ini rename : dom/base/test/websocket_hybi/test_receive-arraybuffer.html => dom/websocket/tests/websocket_hybi/test_receive-arraybuffer.html rename : dom/base/test/websocket_hybi/test_receive-blob.html => dom/websocket/tests/websocket_hybi/test_receive-blob.html rename : dom/base/test/websocket_hybi/test_send-arraybuffer.html => dom/websocket/tests/websocket_hybi/test_send-arraybuffer.html rename : dom/base/test/websocket_hybi/test_send-blob.html => dom/websocket/tests/websocket_hybi/test_send-blob.html rename : dom/workers/test/websocket_loadgroup_worker.js => dom/websocket/tests/websocket_loadgroup_worker.js rename : dom/base/test/websocket_tests.js => dom/websocket/tests/websocket_tests.js rename : dom/workers/test/websocket_worker1.js => dom/websocket/tests/websocket_worker1.js rename : dom/workers/test/websocket_worker2.js => dom/websocket/tests/websocket_worker2.js rename : dom/workers/test/websocket_worker3.js => dom/websocket/tests/websocket_worker3.js rename : dom/workers/test/websocket_worker4.js => dom/websocket/tests/websocket_worker4.js rename : dom/workers/test/websocket_worker5.js => dom/websocket/tests/websocket_worker5.js rename : dom/workers/test/websocket_https.html => dom/websocket/tests/websocket_worker_https.html rename : dom/base/test/window_bug1384658.html => dom/websocket/tests/window_bug1384658.html
		
			
				
	
	
		
			214 lines
		
	
	
	
		
			7.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			214 lines
		
	
	
	
		
			7.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 | |
| /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 | |
| /* This Source Code Form is subject to the terms of the Mozilla Public
 | |
|  * License, v. 2.0. If a copy of the MPL was not distributed with this
 | |
|  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | |
| 
 | |
| #ifndef WebSocket_h__
 | |
| #define WebSocket_h__
 | |
| 
 | |
| #include "mozilla/Attributes.h"
 | |
| #include "mozilla/CheckedInt.h"
 | |
| #include "mozilla/dom/TypedArray.h"
 | |
| #include "mozilla/dom/WebSocketBinding.h" // for BinaryType
 | |
| #include "mozilla/DOMEventTargetHelper.h"
 | |
| #include "mozilla/ErrorResult.h"
 | |
| #include "mozilla/Mutex.h"
 | |
| #include "nsCOMPtr.h"
 | |
| #include "nsCycleCollectionParticipant.h"
 | |
| #include "nsISupports.h"
 | |
| #include "nsISupportsUtils.h"
 | |
| #include "nsString.h"
 | |
| #include "nsWrapperCache.h"
 | |
| 
 | |
| #define DEFAULT_WS_SCHEME_PORT  80
 | |
| #define DEFAULT_WSS_SCHEME_PORT 443
 | |
| 
 | |
| class nsIInputStream;
 | |
| class nsITransportProvider;
 | |
| 
 | |
| namespace mozilla {
 | |
| namespace dom {
 | |
| 
 | |
| class Blob;
 | |
| 
 | |
| class WebSocketImpl;
 | |
| 
 | |
| class WebSocket final : public DOMEventTargetHelper
 | |
| {
 | |
|   friend class WebSocketImpl;
 | |
| 
 | |
| public:
 | |
|   enum {
 | |
|     CONNECTING = 0,
 | |
|     OPEN       = 1,
 | |
|     CLOSING    = 2,
 | |
|     CLOSED     = 3
 | |
|   };
 | |
| 
 | |
| public:
 | |
|   NS_DECL_ISUPPORTS_INHERITED
 | |
|   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WebSocket, DOMEventTargetHelper)
 | |
|   virtual bool IsCertainlyAliveForCC() const override;
 | |
| 
 | |
|   // EventTarget
 | |
|   using EventTarget::EventListenerAdded;
 | |
|   virtual void EventListenerAdded(nsAtom* aType) override;
 | |
| 
 | |
|   using EventTarget::EventListenerRemoved;
 | |
|   virtual void EventListenerRemoved(nsAtom* aType) override;
 | |
| 
 | |
|   virtual void DisconnectFromOwner() override;
 | |
| 
 | |
|   // nsWrapperCache
 | |
|   nsPIDOMWindowInner* GetParentObject() { return GetOwner(); }
 | |
| 
 | |
|   virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto) override;
 | |
| 
 | |
| public: // static helpers:
 | |
| 
 | |
|   // Determine if preferences allow WebSocket
 | |
|   static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
 | |
| 
 | |
| public: // WebIDL interface:
 | |
| 
 | |
|   // Constructor:
 | |
|   static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
 | |
|                                                  const nsAString& aUrl,
 | |
|                                                  ErrorResult& rv);
 | |
| 
 | |
|   static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
 | |
|                                                  const nsAString& aUrl,
 | |
|                                                  const nsAString& aProtocol,
 | |
|                                                  ErrorResult& rv);
 | |
| 
 | |
|   static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
 | |
|                                                  const nsAString& aUrl,
 | |
|                                                  const Sequence<nsString>& aProtocols,
 | |
|                                                  ErrorResult& rv);
 | |
| 
 | |
|   static already_AddRefed<WebSocket> CreateServerWebSocket(const GlobalObject& aGlobal,
 | |
|                                                            const nsAString& aUrl,
 | |
|                                                            const Sequence<nsString>& aProtocols,
 | |
|                                                            nsITransportProvider* aTransportProvider,
 | |
|                                                            const nsAString& aNegotiatedExtensions,
 | |
|                                                            ErrorResult& rv);
 | |
| 
 | |
|   static already_AddRefed<WebSocket> ConstructorCommon(const GlobalObject& aGlobal,
 | |
|                                                        const nsAString& aUrl,
 | |
|                                                        const Sequence<nsString>& aProtocols,
 | |
|                                                        nsITransportProvider* aTransportProvider,
 | |
|                                                        const nsACString& aNegotiatedExtensions,
 | |
|                                                        ErrorResult& rv);
 | |
| 
 | |
|   // webIDL: readonly attribute DOMString url
 | |
|   void GetUrl(nsAString& aResult);
 | |
| 
 | |
|   // webIDL: readonly attribute unsigned short readyState;
 | |
|   uint16_t ReadyState();
 | |
| 
 | |
|   // webIDL: readonly attribute unsigned long bufferedAmount;
 | |
|   uint32_t BufferedAmount() const;
 | |
| 
 | |
|   // webIDL: attribute Function? onopen;
 | |
|   IMPL_EVENT_HANDLER(open)
 | |
| 
 | |
|   // webIDL: attribute Function? onerror;
 | |
|   IMPL_EVENT_HANDLER(error)
 | |
| 
 | |
|   // webIDL: attribute Function? onclose;
 | |
|   IMPL_EVENT_HANDLER(close)
 | |
| 
 | |
|   // webIDL: readonly attribute DOMString extensions;
 | |
|   void GetExtensions(nsAString& aResult);
 | |
| 
 | |
|   // webIDL: readonly attribute DOMString protocol;
 | |
|   void GetProtocol(nsAString& aResult);
 | |
| 
 | |
|   // webIDL: void close(optional unsigned short code, optional DOMString reason):
 | |
|   void Close(const Optional<uint16_t>& aCode,
 | |
|              const Optional<nsAString>& aReason,
 | |
|              ErrorResult& aRv);
 | |
| 
 | |
|   // webIDL: attribute Function? onmessage;
 | |
|   IMPL_EVENT_HANDLER(message)
 | |
| 
 | |
|   // webIDL: attribute DOMString binaryType;
 | |
|   dom::BinaryType BinaryType() const;
 | |
|   void SetBinaryType(dom::BinaryType aData);
 | |
| 
 | |
|   // webIDL: void send(DOMString|Blob|ArrayBufferView data);
 | |
|   void Send(const nsAString& aData,
 | |
|             ErrorResult& aRv);
 | |
|   void Send(Blob& aData,
 | |
|             ErrorResult& aRv);
 | |
|   void Send(const ArrayBuffer& aData,
 | |
|             ErrorResult& aRv);
 | |
|   void Send(const ArrayBufferView& aData,
 | |
|             ErrorResult& aRv);
 | |
| 
 | |
| private: // constructor && destructor
 | |
|   explicit WebSocket(nsPIDOMWindowInner* aOwnerWindow);
 | |
|   virtual ~WebSocket();
 | |
| 
 | |
|   void SetReadyState(uint16_t aReadyState);
 | |
| 
 | |
|   // These methods actually do the dispatch for various events.
 | |
|   nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
 | |
|   nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
 | |
|                                          bool aIsBinary);
 | |
|   nsresult CreateAndDispatchCloseEvent(bool aWasClean,
 | |
|                                        uint16_t aCode,
 | |
|                                        const nsAString& aReason);
 | |
| 
 | |
|   // if there are "strong event listeners" (see comment in WebSocket.cpp) or
 | |
|   // outgoing not sent messages then this method keeps the object alive
 | |
|   // when js doesn't have strong references to it.
 | |
|   void UpdateMustKeepAlive();
 | |
|   // ATTENTION, when calling this method the object can be released
 | |
|   // (and possibly collected).
 | |
|   void DontKeepAliveAnyMore();
 | |
| 
 | |
| private:
 | |
|   WebSocket(const WebSocket& x) = delete;   // prevent bad usage
 | |
|   WebSocket& operator=(const WebSocket& x) = delete;
 | |
| 
 | |
|   void Send(nsIInputStream* aMsgStream,
 | |
|             const nsACString& aMsgString,
 | |
|             uint32_t aMsgLength,
 | |
|             bool aIsBinary,
 | |
|             ErrorResult& aRv);
 | |
| 
 | |
|   void AssertIsOnTargetThread() const;
 | |
| 
 | |
|   // Raw pointer because this WebSocketImpl is created, managed and destroyed by
 | |
|   // WebSocket.
 | |
|   WebSocketImpl* mImpl;
 | |
| 
 | |
|   bool mIsMainThread;
 | |
| 
 | |
|   bool mKeepingAlive;
 | |
|   bool mCheckMustKeepAlive;
 | |
| 
 | |
|   CheckedUint32 mOutgoingBufferedAmount;
 | |
| 
 | |
|   // related to the WebSocket constructor steps
 | |
|   nsString mURI;
 | |
|   nsString mEffectiveURL;   // after redirects
 | |
|   nsCString mEstablishedExtensions;
 | |
|   nsCString mEstablishedProtocol;
 | |
| 
 | |
|   dom::BinaryType mBinaryType;
 | |
| 
 | |
|   // This mutex protects mReadyState that is the only variable that is used in
 | |
|   // different threads.
 | |
|   mozilla::Mutex mMutex;
 | |
| 
 | |
|   // This value should not be used directly but use ReadyState() instead.
 | |
|   uint16_t mReadyState;
 | |
| };
 | |
| 
 | |
| } //namespace dom
 | |
| } //namespace mozilla
 | |
| 
 | |
| #endif
 |