2012-09-10 17:48:14 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-10 17:48:14 +04:00
|
|
|
/* 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__
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-06 10:43:07 +04:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
|
|
|
#include "mozilla/dom/WebSocketBinding.h" // for BinaryType
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2013-09-06 10:43:07 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-09-10 17:48:14 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-09-06 10:43:07 +04:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWrapperCache.h"
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
#define DEFAULT_WS_SCHEME_PORT 80
|
|
|
|
#define DEFAULT_WSS_SCHEME_PORT 443
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
class nsIInputStream;
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
class Blob;
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
class WebSocketImpl;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WebSocket final : public DOMEventTargetHelper
|
2012-09-10 17:48:14 +04:00
|
|
|
{
|
2014-10-10 20:56:43 +04:00
|
|
|
friend class WebSocketImpl;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
CONNECTING = 0,
|
|
|
|
OPEN = 1,
|
|
|
|
CLOSING = 2,
|
|
|
|
CLOSED = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
|
|
|
|
WebSocket, DOMEventTargetHelper)
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2013-05-23 15:41:32 +04:00
|
|
|
// EventTarget
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void EventListenerAdded(nsIAtom* aType) override;
|
|
|
|
virtual void EventListenerRemoved(nsIAtom* aType) override;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void DisconnectFromOwner() override;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
// nsWrapperCache
|
|
|
|
nsPIDOMWindow* GetParentObject() { return GetOwner(); }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
public: // static helpers:
|
|
|
|
|
|
|
|
// Determine if preferences allow WebSocket
|
2014-02-06 22:28:14 +04:00
|
|
|
static bool PrefEnabled(JSContext* aCx = nullptr, JSObject* aGlobal = nullptr);
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
public: // WebIDL interface:
|
|
|
|
|
|
|
|
// Constructor:
|
2012-12-03 20:07:49 +04:00
|
|
|
static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
ErrorResult& rv);
|
|
|
|
|
2012-12-03 20:07:49 +04:00
|
|
|
static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const nsAString& aProtocol,
|
|
|
|
ErrorResult& rv);
|
|
|
|
|
2012-12-03 20:07:49 +04:00
|
|
|
static already_AddRefed<WebSocket> Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
ErrorResult& rv);
|
|
|
|
|
|
|
|
// webIDL: readonly attribute DOMString url
|
|
|
|
void GetUrl(nsAString& aResult);
|
|
|
|
|
|
|
|
// webIDL: readonly attribute unsigned short readyState;
|
2014-10-14 13:28:07 +04:00
|
|
|
uint16_t ReadyState();
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
// webIDL: readonly attribute unsigned long bufferedAmount;
|
2014-10-10 20:56:43 +04:00
|
|
|
uint32_t BufferedAmount() const;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
// 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;
|
2014-10-10 20:56:43 +04:00
|
|
|
dom::BinaryType BinaryType() const;
|
|
|
|
void SetBinaryType(dom::BinaryType aData);
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
// webIDL: void send(DOMString|Blob|ArrayBufferView data);
|
|
|
|
void Send(const nsAString& aData,
|
|
|
|
ErrorResult& aRv);
|
2015-05-12 15:09:51 +03:00
|
|
|
void Send(Blob& aData,
|
2012-09-10 17:48:14 +04:00
|
|
|
ErrorResult& aRv);
|
2013-08-05 21:40:01 +04:00
|
|
|
void Send(const ArrayBuffer& aData,
|
2012-09-10 17:48:14 +04:00
|
|
|
ErrorResult& aRv);
|
2013-08-05 21:40:01 +04:00
|
|
|
void Send(const ArrayBufferView& aData,
|
2012-09-10 17:48:14 +04:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
private: // constructor && distructor
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit WebSocket(nsPIDOMWindow* aOwnerWindow);
|
2012-09-10 17:48:14 +04:00
|
|
|
virtual ~WebSocket();
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
void SetReadyState(uint16_t aReadyState);
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// These methods actually do the dispatch for various events.
|
2014-10-10 20:56:43 +04:00
|
|
|
nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
|
2014-10-10 20:58:05 +04:00
|
|
|
bool aIsBinary);
|
|
|
|
nsresult CreateAndDispatchMessageEvent(JSContext* aCx,
|
|
|
|
const nsACString& aData,
|
|
|
|
bool aIsBinary);
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult CreateAndDispatchCloseEvent(bool aWasClean,
|
2012-09-10 22:11:19 +04:00
|
|
|
uint16_t aCode,
|
2014-10-10 20:56:43 +04:00
|
|
|
const nsAString& aReason);
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
WebSocket(const WebSocket& x) = delete; // prevent bad usage
|
|
|
|
WebSocket& operator=(const WebSocket& x) = delete;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
void Send(nsIInputStream* aMsgStream,
|
|
|
|
const nsACString& aMsgString,
|
|
|
|
uint32_t aMsgLength,
|
|
|
|
bool aIsBinary,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
void AssertIsOnTargetThread() const;
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
// Raw pointer because this WebSocketImpl is created, managed and destroyed by
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocket.
|
|
|
|
WebSocketImpl* mImpl;
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
bool mIsMainThread;
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
bool mKeepingAlive;
|
|
|
|
bool mCheckMustKeepAlive;
|
2014-10-14 13:28:07 +04:00
|
|
|
|
|
|
|
uint32_t mOutgoingBufferedAmount;
|
|
|
|
|
|
|
|
// related to the WebSocket constructor steps
|
2015-03-31 13:39:35 +03:00
|
|
|
nsString mURI;
|
2014-10-14 13:28:07 +04:00
|
|
|
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;
|
2012-09-10 17:48:14 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} //namespace dom
|
|
|
|
} //namespace mozilla
|
|
|
|
|
|
|
|
#endif
|