Bug 504553 - patch 2 - WebSockes in Workers: WebSocketImpl, thread-safe class, r=smaug

This commit is contained in:
Andrea Marchesini 2014-10-10 17:56:43 +01:00
parent 83de2be098
commit 99566c64c1
5 changed files with 398 additions and 302 deletions

File diff suppressed because it is too large Load Diff

View File

@ -15,15 +15,9 @@
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIInterfaceRequestor.h"
#include "nsIObserver.h"
#include "nsIRequest.h"
#include "nsISupports.h"
#include "nsISupportsUtils.h"
#include "nsIWebSocketChannel.h"
#include "nsIWebSocketListener.h"
#include "nsString.h"
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#define DEFAULT_WS_SCHEME_PORT 80
@ -34,15 +28,11 @@ namespace dom {
class File;
class WebSocket MOZ_FINAL : public DOMEventTargetHelper,
public nsIInterfaceRequestor,
public nsIWebSocketListener,
public nsIObserver,
public nsSupportsWeakReference,
public nsIRequest
class WebSocketImpl;
class WebSocket MOZ_FINAL : public DOMEventTargetHelper
{
friend class CallDispatchConnectionCloseEvents;
friend class nsAutoCloseWS;
friend class WebSocketImpl;
public:
enum {
@ -56,10 +46,6 @@ public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
WebSocket, DOMEventTargetHelper)
NS_DECL_NSIINTERFACEREQUESTOR
NS_DECL_NSIWEBSOCKETLISTENER
NS_DECL_NSIOBSERVER
NS_DECL_NSIREQUEST
// EventTarget
virtual void EventListenerAdded(nsIAtom* aType) MOZ_OVERRIDE;
@ -98,10 +84,10 @@ public: // WebIDL interface:
void GetUrl(nsAString& aResult);
// webIDL: readonly attribute unsigned short readyState;
uint16_t ReadyState() const { return mReadyState; }
uint16_t ReadyState() const;
// webIDL: readonly attribute unsigned long bufferedAmount;
uint32_t BufferedAmount() const { return mOutgoingBufferedAmount; }
uint32_t BufferedAmount() const;
// webIDL: attribute Function? onopen;
IMPL_EVENT_HANDLER(open)
@ -127,8 +113,8 @@ public: // WebIDL interface:
IMPL_EVENT_HANDLER(message)
// webIDL: attribute DOMString binaryType;
dom::BinaryType BinaryType() const { return mBinaryType; }
void SetBinaryType(dom::BinaryType aData) { mBinaryType = aData; }
dom::BinaryType BinaryType() const;
void SetBinaryType(dom::BinaryType aData);
// webIDL: void send(DOMString|Blob|ArrayBufferView data);
void Send(const nsAString& aData,
@ -144,54 +130,13 @@ private: // constructor && distructor
explicit WebSocket(nsPIDOMWindow* aOwnerWindow);
virtual ~WebSocket();
protected:
nsresult Init(JSContext* aCx,
nsIPrincipal* aPrincipal,
const nsAString& aURL,
nsTArray<nsString>& aProtocolArray);
void Send(nsIInputStream* aMsgStream,
const nsACString& aMsgString,
uint32_t aMsgLength,
bool aIsBinary,
ErrorResult& aRv);
nsresult ParseURL(const nsString& aURL);
nsresult EstablishConnection();
// These methods when called can release the WebSocket object
void FailConnection(uint16_t reasonCode,
const nsACString& aReasonString = EmptyCString());
nsresult CloseConnection(uint16_t reasonCode,
const nsACString& aReasonString = EmptyCString());
nsresult Disconnect();
nsresult ConsoleError();
nsresult PrintErrorOnConsole(const char* aBundleURI,
const char16_t* aError,
const char16_t** aFormatStrings,
uint32_t aFormatStringsLen);
nsresult DoOnMessageAvailable(const nsACString& aMsg,
bool isBinary);
// ConnectionCloseEvents: 'error' event if needed, then 'close' event.
// - These must not be dispatched while we are still within an incoming call
// from JS (ex: close()). Set 'sync' to false in that case to dispatch in a
// separate new event.
nsresult ScheduleConnectionCloseEvents(nsISupports* aContext,
nsresult aStatusCode,
bool sync);
// 2nd half of ScheduleConnectionCloseEvents, sometimes run in its own event.
void DispatchConnectionCloseEvents();
// These methods actually do the dispatch for various events.
nsresult CreateAndDispatchSimpleEvent(const nsString& aName);
nsresult CreateAndDispatchSimpleEvent(const nsAString& aName);
nsresult CreateAndDispatchMessageEvent(const nsACString& aData,
bool isBinary);
nsresult CreateAndDispatchCloseEvent(bool aWasClean,
uint16_t aCode,
const nsString& aReason);
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
@ -201,61 +146,16 @@ protected:
// (and possibly collected).
void DontKeepAliveAnyMore();
nsresult UpdateURI();
protected: //data
nsCOMPtr<nsIWebSocketChannel> mChannel;
// related to the WebSocket constructor steps
nsString mOriginalURL;
nsString mEffectiveURL; // after redirects
bool mSecure; // if true it is using SSL and the wss scheme,
// otherwise it is using the ws scheme with no SSL
bool mKeepingAlive;
bool mCheckMustKeepAlive;
bool mOnCloseScheduled;
bool mFailed;
bool mDisconnected;
// Set attributes of DOM 'onclose' message
bool mCloseEventWasClean;
nsString mCloseEventReason;
uint16_t mCloseEventCode;
nsCString mAsciiHost; // hostname
uint32_t mPort;
nsCString mResource; // [filepath[?query]]
nsString mUTF16Origin;
nsCOMPtr<nsIURI> mURI;
nsCString mRequestedProtocolList;
nsCString mEstablishedProtocol;
nsCString mEstablishedExtensions;
uint16_t mReadyState;
nsCOMPtr<nsIPrincipal> mPrincipal;
nsWeakPtr mOriginDocument;
uint32_t mOutgoingBufferedAmount;
dom::BinaryType mBinaryType;
// Web Socket owner information:
// - the script file name, UTF8 encoded.
// - source code line number where the Web Socket object was constructed.
// - the ID of the inner window where the script lives. Note that this may not
// be the same as the Web Socket owner window.
// These attributes are used for error reporting.
nsCString mScriptFile;
uint32_t mScriptLine;
uint64_t mInnerWindowID;
private:
WebSocket(const WebSocket& x) MOZ_DELETE; // prevent bad usage
WebSocket& operator=(const WebSocket& x) MOZ_DELETE;
// Raw pointer because this WebSocketImpl is created, managed an destroyed by
// WebSocket.
WebSocketImpl* mImpl;
bool mKeepingAlive;
bool mCheckMustKeepAlive;
};
} //namespace dom

View File

@ -94,8 +94,8 @@ class ChannelEventQueue MOZ_FINAL
// Keep ptr to avoid refcount cycle: only grab ref during flushing.
nsISupports *mOwner;
// Target thread for delivery of events.
nsCOMPtr<nsIThread> mTargetThread;
// EventTarget for delivery of events to the correct thread.
nsCOMPtr<nsIEventTarget> mTargetThread;
friend class AutoEventEnqueuer;
};

View File

@ -65,7 +65,7 @@ class BaseWebSocketChannel : public nsIWebSocketChannel,
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
nsCOMPtr<nsILoadGroup> mLoadGroup;
nsCOMPtr<nsILoadInfo> mLoadInfo;
nsCOMPtr<nsIThread> mTargetThread;
nsCOMPtr<nsIEventTarget> mTargetThread;
nsCString mProtocol;
nsCString mOrigin;

View File

@ -654,11 +654,9 @@ NS_IMPL_ISUPPORTS(CallOnServerClose, nsIRunnable)
// CallAcknowledge
//-----------------------------------------------------------------------------
class CallAcknowledge MOZ_FINAL : public nsIRunnable
class CallAcknowledge MOZ_FINAL : public nsCancelableRunnable
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
CallAcknowledge(WebSocketChannel *aChannel,
uint32_t aSize)
: mChannel(aChannel),
@ -679,7 +677,6 @@ private:
nsRefPtr<WebSocketChannel> mChannel;
uint32_t mSize;
};
NS_IMPL_ISUPPORTS(CallAcknowledge, nsIRunnable)
//-----------------------------------------------------------------------------
// CallOnTransportAvailable