2010-06-17 18:34:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 11:12:37 +00: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/. */
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
#include "WebSocket.h"
|
|
|
|
#include "mozilla/dom/WebSocketBinding.h"
|
2014-04-17 18:53:58 +00:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2011-05-23 16:46:36 +00:00
|
|
|
|
2013-08-27 02:05:20 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-05-23 07:47:46 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-10-10 16:58:05 +00:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2014-10-10 16:58:19 +00:00
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2010-06-17 18:34:24 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2010-06-17 18:34:24 +00:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIDOMMessageEvent.h"
|
|
|
|
#include "nsIPromptFactory.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIPrompt.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2014-06-30 23:00:31 +00:00
|
|
|
#include "mozilla/dom/CloseEvent.h"
|
2010-06-17 18:36:01 +00:00
|
|
|
#include "nsICryptoHash.h"
|
2010-12-21 17:05:34 +00:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIScriptError.h"
|
2011-05-22 01:27:52 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsILoadGroup.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 16:06:36 +00:00
|
|
|
#include "xpcpublic.h"
|
2011-12-07 23:19:43 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2012-01-26 15:55:30 +00:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-02-21 22:57:10 +00:00
|
|
|
#include "nsIObserverService.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
|
|
#include "nsIWebSocketChannel.h"
|
|
|
|
#include "nsIWebSocketListener.h"
|
2014-10-10 16:58:05 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "nsWeakReference.h"
|
2014-04-17 18:53:58 +00:00
|
|
|
|
|
|
|
using namespace mozilla::net;
|
2014-10-10 16:58:05 +00:00
|
|
|
using namespace mozilla::dom::workers;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
class WebSocketImpl MOZ_FINAL : public nsIInterfaceRequestor
|
|
|
|
, public nsIWebSocketListener
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
, public nsIRequest
|
2014-10-10 16:58:05 +00:00
|
|
|
, public nsIEventTarget
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIWEBSOCKETLISTENER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSIREQUEST
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_DECL_NSIEVENTTARGET
|
2014-10-10 16:56:43 +00:00
|
|
|
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit WebSocketImpl(WebSocket* aWebSocket)
|
2014-10-10 16:58:05 +00:00
|
|
|
: mWebSocket(aWebSocket)
|
2014-10-10 16:56:43 +00:00
|
|
|
, mOnCloseScheduled(false)
|
|
|
|
, mFailed(false)
|
2014-12-12 19:37:07 +00:00
|
|
|
, mDisconnectingOrDisconnected(false)
|
2014-10-10 16:56:43 +00:00
|
|
|
, mCloseEventWasClean(false)
|
|
|
|
, mCloseEventCode(nsIWebSocketChannel::CLOSE_ABNORMAL)
|
|
|
|
, mScriptLine(0)
|
|
|
|
, mInnerWindowID(0)
|
2014-10-10 16:58:05 +00:00
|
|
|
, mWorkerPrivate(nullptr)
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mHasFeatureRegistered(false)
|
|
|
|
#endif
|
2014-12-12 19:37:07 +00:00
|
|
|
, mIsMainThread(true)
|
|
|
|
, mWorkerShuttingDown(false)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mWorkerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2014-12-12 19:37:07 +00:00
|
|
|
mIsMainThread = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssertIsOnTargetThread() const
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(IsTargetThread());
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
bool IsTargetThread() const;
|
|
|
|
|
|
|
|
void Init(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile,
|
|
|
|
uint32_t aScriptLine,
|
|
|
|
ErrorResult& aRv,
|
|
|
|
bool* aConnectionFailed);
|
|
|
|
|
|
|
|
void AsyncOpen(ErrorResult& aRv);
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsresult ParseURL(const nsAString& aURL);
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult InitializeConnection();
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
// 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();
|
2014-10-10 16:58:05 +00:00
|
|
|
void DisconnectInternal();
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Dispatch a runnable to the right thread.
|
|
|
|
nsresult DispatchRunnable(nsIRunnable* aRunnable);
|
|
|
|
|
|
|
|
nsresult UpdateURI();
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void AddRefObject();
|
|
|
|
void ReleaseObject();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
bool RegisterFeature();
|
2014-10-10 16:58:05 +00:00
|
|
|
void UnregisterFeature();
|
|
|
|
|
|
|
|
nsresult CancelInternal();
|
|
|
|
|
|
|
|
nsRefPtr<WebSocket> mWebSocket;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWebSocketChannel> mChannel;
|
|
|
|
|
|
|
|
bool mSecure; // if true it is using SSL and the wss scheme,
|
|
|
|
// otherwise it is using the ws scheme with no SSL
|
|
|
|
|
|
|
|
bool mOnCloseScheduled;
|
|
|
|
bool mFailed;
|
2014-12-12 19:37:07 +00:00
|
|
|
bool mDisconnectingOrDisconnected;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCString mURI;
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCString mRequestedProtocolList;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsWeakPtr mOriginDocument;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsAutoPtr<WorkerFeature> mWorkerFeature;
|
|
|
|
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// This is protected by mutex.
|
|
|
|
bool mHasFeatureRegistered;
|
|
|
|
|
|
|
|
bool HasFeatureRegistered()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWebSocket);
|
|
|
|
MutexAutoLock lock(mWebSocket->mMutex);
|
|
|
|
return mHasFeatureRegistered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHasFeatureRegistered(bool aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWebSocket);
|
|
|
|
MutexAutoLock lock(mWebSocket->mMutex);
|
|
|
|
mHasFeatureRegistered = aValue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-17 07:12:26 +00:00
|
|
|
nsWeakPtr mWeakLoadGroup;
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
bool mIsMainThread;
|
|
|
|
bool mWorkerShuttingDown;
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
private:
|
|
|
|
~WebSocketImpl()
|
|
|
|
{
|
|
|
|
// If we threw during Init we never called disconnect
|
2014-12-12 19:37:07 +00:00
|
|
|
if (!mDisconnectingOrDisconnected) {
|
2014-10-10 16:56:43 +00:00
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WebSocketImpl,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIWebSocketListener,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference,
|
2014-10-10 16:58:05 +00:00
|
|
|
nsIRequest,
|
|
|
|
nsIEventTarget)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-11-11 22:47:13 +00:00
|
|
|
class CallDispatchConnectionCloseEvents MOZ_FINAL : public nsCancelableRunnable
|
2012-06-04 23:38:32 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 16:56:43 +00:00
|
|
|
explicit CallDispatchConnectionCloseEvents(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
aWebSocketImpl->AssertIsOnTargetThread();
|
|
|
|
}
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocketImpl->AssertIsOnTargetThread();
|
2014-10-10 16:56:43 +00:00
|
|
|
mWebSocketImpl->DispatchConnectionCloseEvents();
|
2012-06-04 23:38:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-10 16:56:43 +00:00
|
|
|
nsRefPtr<WebSocketImpl> mWebSocketImpl;
|
2012-06-04 23:38:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl
|
2012-06-04 23:38:32 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrintErrorOnConsoleRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrintErrorOnConsoleRunnable(WebSocketImpl* aImpl,
|
|
|
|
const char* aBundleURI,
|
|
|
|
const char16_t* aError,
|
|
|
|
const char16_t** aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen)
|
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
, mBundleURI(aBundleURI)
|
|
|
|
, mError(aError)
|
|
|
|
, mFormatStrings(aFormatStrings)
|
|
|
|
, mFormatStringsLen(aFormatStringsLen)
|
|
|
|
, mRv(NS_ERROR_FAILURE)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mRv = mImpl->PrintErrorOnConsole(mBundleURI, mError, mFormatStrings,
|
|
|
|
mFormatStringsLen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ErrorCode() const
|
|
|
|
{
|
|
|
|
return mRv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
const char* mBundleURI;
|
|
|
|
const char16_t* mError;
|
|
|
|
const char16_t** mFormatStrings;
|
|
|
|
uint32_t mFormatStringsLen;
|
|
|
|
nsresult mRv;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::PrintErrorOnConsole(const char *aBundleURI,
|
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// This method must run on the main thread.
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsRefPtr<PrintErrorOnConsoleRunnable> runnable =
|
|
|
|
new PrintErrorOnConsoleRunnable(this, aBundleURI, aError, aFormatStrings,
|
|
|
|
aFormatStringsLen);
|
|
|
|
runnable->Dispatch(mWorkerPrivate->GetJSContext());
|
|
|
|
return runnable->ErrorCode();
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> strBundle;
|
|
|
|
rv = bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-12-21 21:51:29 +00:00
|
|
|
nsCOMPtr<nsIScriptError> errorObject(
|
2011-05-22 01:27:52 +00:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
|
|
|
aFormatStringsLen,
|
|
|
|
getter_Copies(message));
|
2010-06-17 18:34:24 +00:00
|
|
|
} else {
|
2011-05-22 01:27:52 +00:00
|
|
|
rv = strBundle->GetStringFromName(aError, getter_Copies(message));
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
if (mInnerWindowID) {
|
|
|
|
rv = errorObject->InitWithWindowID(message,
|
|
|
|
NS_ConvertUTF8toUTF16(mScriptFile),
|
|
|
|
EmptyString(), mScriptLine, 0,
|
|
|
|
nsIScriptError::errorFlag, "Web Socket",
|
|
|
|
mInnerWindowID);
|
|
|
|
} else {
|
|
|
|
rv = errorObject->Init(message,
|
|
|
|
NS_ConvertUTF8toUTF16(mScriptFile),
|
|
|
|
EmptyString(), mScriptLine, 0,
|
|
|
|
nsIScriptError::errorFlag, "Web Socket");
|
|
|
|
}
|
|
|
|
|
2011-12-21 21:51:29 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// print the error message directly to the JS console
|
2011-12-21 21:51:29 +00:00
|
|
|
rv = console->LogMessage(errorObject);
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CloseRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CloseRunnable(WebSocketImpl* aImpl, uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
, mReasonCode(aReasonCode)
|
|
|
|
, mReasonString(aReasonString)
|
|
|
|
, mRv(NS_ERROR_FAILURE)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mRv = mImpl->mChannel->Close(mReasonCode, mReasonString);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ErrorCode() const
|
|
|
|
{
|
|
|
|
return mRv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// A raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
uint16_t mReasonCode;
|
|
|
|
const nsACString& mReasonString;
|
|
|
|
nsresult mRv;
|
|
|
|
};
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
class MOZ_STACK_CLASS MaybeDisconnect
|
|
|
|
{
|
|
|
|
public:
|
2014-12-13 19:17:04 +00:00
|
|
|
explicit MaybeDisconnect(WebSocketImpl* aImpl)
|
2014-12-12 19:37:07 +00:00
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~MaybeDisconnect()
|
|
|
|
{
|
|
|
|
if (mImpl->mWorkerShuttingDown) {
|
|
|
|
mImpl->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::CloseConnection(uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this method is called because the worker is going away, we will not
|
|
|
|
// receive the OnStop() method and we have to disconnect the WebSocket and
|
|
|
|
// release the WorkerFeature.
|
|
|
|
MaybeDisconnect md(this);
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
uint16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSING ||
|
|
|
|
readyState == WebSocket::CLOSED) {
|
2011-06-08 12:27:11 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// The common case...
|
|
|
|
if (mChannel) {
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
// The channel has to be closed on the main-thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return mChannel->Close(aReasonCode, aReasonString);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CloseRunnable> runnable =
|
|
|
|
new CloseRunnable(this, aReasonCode, aReasonString);
|
|
|
|
runnable->Dispatch(mWorkerPrivate->GetJSContext());
|
|
|
|
return runnable->ErrorCode();
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// No channel, but not disconnected: canceled or failed early
|
|
|
|
//
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CONNECTING,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Should only get here for early websocket cancel/error");
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// Server won't be sending us a close code, so use what's passed in here.
|
|
|
|
mCloseEventCode = aReasonCode;
|
|
|
|
CopyUTF8toUTF16(aReasonString, mCloseEventReason);
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// Can be called from Cancel() or Init() codepaths, so need to dispatch
|
|
|
|
// onerror/onclose asynchronously
|
|
|
|
ScheduleConnectionCloseEvents(
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2012-06-04 23:38:32 +00:00
|
|
|
(aReasonCode == nsIWebSocketChannel::CLOSE_NORMAL ||
|
|
|
|
aReasonCode == nsIWebSocketChannel::CLOSE_GOING_AWAY) ?
|
|
|
|
NS_OK : NS_ERROR_FAILURE,
|
|
|
|
false);
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ConsoleError()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(mURI);
|
|
|
|
const char16_t* formatStrings[] = { specUTF16.get() };
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (mWebSocket->ReadyState() < WebSocket::OPEN) {
|
2014-10-10 16:58:05 +00:00
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
|
|
|
MOZ_UTF16("connectionFailure"),
|
|
|
|
formatStrings, ArrayLength(formatStrings));
|
|
|
|
} else {
|
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
|
|
|
MOZ_UTF16("netInterrupt"),
|
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
2011-08-02 23:55:17 +00:00
|
|
|
/// todo some specific errors - like for message too large
|
2014-10-10 16:58:05 +00:00
|
|
|
return NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 08:04:08 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::FailConnection(uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
2012-02-21 22:57:10 +00:00
|
|
|
CloseConnection(aReasonCode, aReasonString);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DisconnectInternalRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit DisconnectInternalRunnable(WebSocketImpl* aImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mImpl->DisconnectInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// A raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Disconnect()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// Disconnect can be called from some control event (such as Notify() of
|
|
|
|
// WorkerFeature). This will be schedulated before any other sync/async
|
|
|
|
// runnable. In order to prevent some double Disconnect() calls, we use this
|
|
|
|
// boolean.
|
|
|
|
mDisconnectingOrDisconnected = true;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// DisconnectInternal touches observers and nsILoadGroup and it must run on
|
|
|
|
// the main thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
DisconnectInternal();
|
|
|
|
} else {
|
|
|
|
nsRefPtr<DisconnectInternalRunnable> runnable =
|
|
|
|
new DisconnectInternalRunnable(this);
|
|
|
|
runnable->Dispatch(mWorkerPrivate->GetJSContext());
|
2012-02-21 22:57:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
// DontKeepAliveAnyMore() can release the object. So hold a reference to this
|
|
|
|
// until the end of the method.
|
2014-10-10 16:56:43 +00:00
|
|
|
nsRefPtr<WebSocketImpl> kungfuDeathGrip = this;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
if (NS_FAILED(NS_GetMainThread(getter_AddRefs(mainThread))) ||
|
|
|
|
NS_FAILED(NS_ProxyRelease(mainThread, mChannel))) {
|
|
|
|
NS_WARNING("Failed to proxy release of channel, leaking instead!");
|
|
|
|
}
|
|
|
|
|
|
|
|
mWebSocket->DontKeepAliveAnyMore();
|
|
|
|
mWebSocket->mImpl = nullptr;
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mWorkerPrivate && mWorkerFeature) {
|
|
|
|
UnregisterFeature();
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// We want to release the WebSocket in the correct thread.
|
|
|
|
mWebSocket = nullptr;
|
2011-06-08 12:27:11 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
|
|
|
WebSocketImpl::DisconnectInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-17 07:12:26 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_QueryReferent(mWeakLoadGroup);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (loadGroup) {
|
|
|
|
loadGroup->RemoveRequest(this, nullptr, NS_OK);
|
2014-11-11 22:47:13 +00:00
|
|
|
// mWeakLoadGroup has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mWeakLoadGroup = nullptr;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIWebSocketListener methods:
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::DoOnMessageAvailable(const nsACString& aMsg, bool isBinary)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSED) {
|
2012-06-04 23:38:32 +00:00
|
|
|
NS_ERROR("Received message after CLOSED");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2011-12-20 08:20:12 +00:00
|
|
|
// Dispatch New Message
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = mWebSocket->CreateAndDispatchMessageEvent(aMsg, isBinary);
|
2011-12-20 08:20:12 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// CLOSING should be the only other state where it's possible to get msgs
|
|
|
|
// from channel: Spec says to drop them.
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CLOSING,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Received message while CONNECTING or CLOSED");
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-12-20 08:20:12 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
return DoOnMessageAvailable(aMsg, false);
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnBinaryMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
return DoOnMessageAvailable(aMsg, true);
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnStart(nsISupports* aContext)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
// This is the only function that sets OPEN, and should be called only once
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::OPEN,
|
2012-06-04 23:38:32 +00:00
|
|
|
"readyState already OPEN! OnStart called twice?");
|
|
|
|
|
|
|
|
// Nothing to do if we've already closed/closing
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState != WebSocket::CONNECTING) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = mWebSocket->CheckInnerWindowCorrectness();
|
2012-02-21 22:57:10 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-04 23:38:32 +00:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-21 22:57:10 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-10-14 09:28:07 +00:00
|
|
|
mChannel->GetProtocol(mWebSocket->mEstablishedProtocol);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mChannel->GetExtensions(mWebSocket->mEstablishedExtensions);
|
2011-12-15 23:23:00 +00:00
|
|
|
UpdateURI();
|
2011-08-04 03:46:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::OPEN);
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
// Call 'onopen'
|
2014-10-10 16:58:05 +00:00
|
|
|
rv = mWebSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("open"));
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event");
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocket->UpdateMustKeepAlive();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnStop(nsISupports* aContext, nsresult aStatusCode)
|
2012-06-04 23:38:32 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// We can be CONNECTING here if connection failed.
|
|
|
|
// We can be OPEN if we have encountered a fatal protocol error
|
|
|
|
// We can be CLOSING if close() was called and/or server initiated close.
|
2014-10-14 09:28:07 +00:00
|
|
|
MOZ_ASSERT(mWebSocket->ReadyState() != WebSocket::CLOSED,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Shouldn't already be CLOSED when OnStop called");
|
|
|
|
|
|
|
|
// called by network stack, not JS, so can dispatch JS events synchronously
|
|
|
|
return ScheduleConnectionCloseEvents(aContext, aStatusCode, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ScheduleConnectionCloseEvents(nsISupports* aContext,
|
|
|
|
nsresult aStatusCode,
|
|
|
|
bool sync)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// no-op if some other code has already initiated close event
|
|
|
|
if (!mOnCloseScheduled) {
|
|
|
|
mCloseEventWasClean = NS_SUCCEEDED(aStatusCode);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
if (aStatusCode == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// don't generate an error event just because of an unclean close
|
|
|
|
aStatusCode = NS_OK;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(aStatusCode)) {
|
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
mOnCloseScheduled = true;
|
|
|
|
|
|
|
|
if (sync) {
|
|
|
|
DispatchConnectionCloseEvents();
|
|
|
|
} else {
|
2014-05-23 19:53:17 +00:00
|
|
|
NS_DispatchToCurrentThread(new CallDispatchConnectionCloseEvents(this));
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnAcknowledge(nsISupports *aContext, uint32_t aSize)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (aSize > mWebSocket->mOutgoingBufferedAmount) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->mOutgoingBufferedAmount -= aSize;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnServerClose(nsISupports *aContext, uint16_t aCode,
|
|
|
|
const nsACString &aReason)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(readyState != WebSocket::CONNECTING,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Received server close before connected?");
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::CLOSED,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Received server close after already closed!");
|
2012-02-21 22:57:13 +00:00
|
|
|
|
2012-02-01 04:41:43 +00:00
|
|
|
// store code/string for onclose DOM event
|
|
|
|
mCloseEventCode = aCode;
|
|
|
|
CopyUTF8toUTF16(aReason, mCloseEventReason);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2012-06-04 23:38:32 +00:00
|
|
|
// Server initiating close.
|
2012-05-17 00:09:42 +00:00
|
|
|
// RFC 6455, 5.5.1: "When sending a Close frame in response, the endpoint
|
|
|
|
// typically echos the status code it received".
|
|
|
|
// But never send certain codes, per section 7.4.1
|
|
|
|
if (aCode == 1005 || aCode == 1006 || aCode == 1015) {
|
|
|
|
CloseConnection(0, EmptyCString());
|
|
|
|
} else {
|
|
|
|
CloseConnection(aCode, aReason);
|
|
|
|
}
|
2012-02-21 22:57:13 +00:00
|
|
|
} else {
|
2012-06-04 23:38:32 +00:00
|
|
|
// We initiated close, and server has replied: OnStop does rest of the work.
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CLOSING, "unknown state");
|
2012-02-21 22:57:13 +00:00
|
|
|
}
|
2012-02-01 04:41:43 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIInterfaceRequestor
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetInterface(const nsIID& aIID, void** aResult)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (!mWebSocket || mWebSocket->ReadyState() == WebSocket::CLOSED) {
|
2011-06-08 12:27:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-06-08 12:27:11 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) {
|
|
|
|
nsresult rv;
|
2014-10-10 16:58:05 +00:00
|
|
|
nsIScriptContext* sc = mWebSocket->GetContextForEventHandlers(&rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2012-03-13 00:56:07 +00:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!doc) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPromptFactory> wwatch =
|
|
|
|
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> outerWindow = doc->GetWindow();
|
|
|
|
return wwatch->GetPrompt(outerWindow, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
return QueryInterface(aIID, aResult);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-09-10 13:48:14 +00:00
|
|
|
// WebSocket
|
2010-06-17 18:34:24 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-07 02:53:23 +00:00
|
|
|
WebSocket::WebSocket(nsPIDOMWindow* aOwnerWindow)
|
2014-10-10 16:58:05 +00:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow)
|
2014-12-12 19:37:07 +00:00
|
|
|
, mIsMainThread(true)
|
2014-10-10 16:58:05 +00:00
|
|
|
, mKeepingAlive(false)
|
|
|
|
, mCheckMustKeepAlive(true)
|
2014-10-14 09:28:07 +00:00
|
|
|
, mOutgoingBufferedAmount(0)
|
|
|
|
, mBinaryType(dom::BinaryType::Blob)
|
|
|
|
, mMutex("WebSocketImpl::mMutex")
|
|
|
|
, mReadyState(CONNECTING)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
mImpl = new WebSocketImpl(this);
|
2014-12-12 19:37:07 +00:00
|
|
|
mIsMainThread = mImpl->mIsMainThread;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::~WebSocket()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
WebSocket::WrapObject(JSContext* cx)
|
2012-09-10 13:48:14 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return WebSocketBinding::Wrap(cx, this);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// WebIDL
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Constructor:
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 16:07:49 +00:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 13:48:14 +00:00
|
|
|
const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
2013-08-30 17:14:19 +00:00
|
|
|
return WebSocket::Constructor(aGlobal, aUrl, protocols, aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 16:07:49 +00:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 13:48:14 +00:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const nsAString& aProtocol,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
|
|
|
protocols.AppendElement(aProtocol);
|
2013-08-30 17:14:19 +00:00
|
|
|
return WebSocket::Constructor(aGlobal, aUrl, protocols, aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This class is used to clear any exception.
|
|
|
|
class MOZ_STACK_CLASS ClearException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ClearException(JSContext* aCx)
|
|
|
|
: mCx(aCx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClearException()
|
|
|
|
{
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mCx;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InitRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InitRunnable(WebSocketImpl* aImpl, const nsAString& aURL,
|
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile, uint32_t aScriptLine,
|
|
|
|
ErrorResult& aRv, bool* aConnectionFailed)
|
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
, mURL(aURL)
|
|
|
|
, mProtocolArray(aProtocolArray)
|
|
|
|
, mScriptFile(aScriptFile)
|
|
|
|
, mScriptLine(aScriptLine)
|
|
|
|
, mRv(aRv)
|
|
|
|
, mConnectionFailed(aConnectionFailed)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* window = wp->GetWindow();
|
2014-10-29 16:54:35 +00:00
|
|
|
if (window) {
|
|
|
|
return InitWithWindow(window);
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
return InitWindowless();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool InitWithWindow(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AutoJSAPI jsapi;
|
2014-10-29 16:54:35 +00:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(aWindow))) {
|
2014-10-10 16:58:05 +00:00
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearException ce(jsapi.cx());
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
nsIDocument* doc = aWindow->GetExtantDoc();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!doc) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->Init(jsapi.cx(), principal, mURL, mProtocolArray, mScriptFile,
|
|
|
|
mScriptLine, mRv, mConnectionFailed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
bool InitWindowless()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!wp->GetWindow());
|
|
|
|
|
|
|
|
mImpl->Init(nullptr, wp->GetPrincipal(), mURL, mProtocolArray, mScriptFile,
|
|
|
|
mScriptLine, mRv, mConnectionFailed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
const nsAString& mURL;
|
|
|
|
nsTArray<nsString>& mProtocolArray;
|
|
|
|
nsCString mScriptFile;
|
|
|
|
uint32_t mScriptLine;
|
|
|
|
ErrorResult& mRv;
|
|
|
|
bool* mConnectionFailed;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AsyncOpenRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncOpenRunnable(WebSocketImpl* aImpl, ErrorResult& aRv)
|
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
, mRv(aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mImpl->AsyncOpen(mRv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
ErrorResult& mRv;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 16:07:49 +00:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 13:48:14 +00:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> ownerWindow;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!scriptPrincipal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
principal = scriptPrincipal->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!sgo) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
ownerWindow = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsString> protocolArray;
|
|
|
|
|
|
|
|
for (uint32_t index = 0, len = aProtocols.Length(); index < len; ++index) {
|
|
|
|
|
|
|
|
const nsString& protocolElement = aProtocols[index];
|
|
|
|
|
|
|
|
if (protocolElement.IsEmpty()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (protocolArray.Contains(protocolElement)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (protocolElement.FindChar(',') != -1) /* interferes w/list */ {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolArray.AppendElement(protocolElement);
|
|
|
|
}
|
|
|
|
|
2014-01-07 02:53:23 +00:00
|
|
|
nsRefPtr<WebSocket> webSocket = new WebSocket(ownerWindow);
|
2014-10-10 16:58:05 +00:00
|
|
|
nsRefPtr<WebSocketImpl> kungfuDeathGrip = webSocket->mImpl;
|
|
|
|
|
|
|
|
bool connectionFailed = true;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
webSocket->mImpl->Init(aGlobal.Context(), principal, aUrl, protocolArray,
|
|
|
|
EmptyCString(), 0, aRv, &connectionFailed);
|
|
|
|
} else {
|
2014-12-12 19:37:07 +00:00
|
|
|
// In workers we have to keep the worker alive using a feature in order to
|
|
|
|
// dispatch messages correctly.
|
|
|
|
if (!webSocket->mImpl->RegisterFeature()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
unsigned lineno;
|
|
|
|
JS::AutoFilename file;
|
|
|
|
if (!JS::DescribeScriptedCaller(aGlobal.Context(), &file, &lineno)) {
|
|
|
|
NS_WARNING("Failed to get line number and filename in workers.");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<InitRunnable> runnable =
|
|
|
|
new InitRunnable(webSocket->mImpl, aUrl, protocolArray,
|
|
|
|
nsAutoCString(file.get()), lineno, aRv,
|
|
|
|
&connectionFailed);
|
|
|
|
runnable->Dispatch(aGlobal.Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// It can be that we have been already disconnected because the WebSocket is
|
|
|
|
// gone away while we where initializing the webSocket.
|
|
|
|
if (!webSocket->mImpl) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// We don't return an error if the connection just failed. Instead we dispatch
|
|
|
|
// an event.
|
|
|
|
if (connectionFailed) {
|
|
|
|
webSocket->mImpl->FailConnection(nsIWebSocketChannel::CLOSE_ABNORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a channel, the connection is failed and onerror() will be
|
|
|
|
// called asynchrounsly.
|
|
|
|
if (!webSocket->mImpl->mChannel) {
|
|
|
|
return webSocket.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS ClearWebSocket
|
|
|
|
{
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit ClearWebSocket(WebSocketImpl* aWebSocketImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
|
|
|
, mDone(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Done()
|
|
|
|
{
|
|
|
|
mDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClearWebSocket()
|
|
|
|
{
|
|
|
|
if (!mDone) {
|
|
|
|
mWebSocketImpl->mChannel = nullptr;
|
|
|
|
mWebSocketImpl->FailConnection(nsIWebSocketChannel::CLOSE_ABNORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketImpl* mWebSocketImpl;
|
|
|
|
bool mDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClearWebSocket cws(webSocket->mImpl);
|
|
|
|
|
|
|
|
// This operation must be done on the correct thread. The rest must run on the
|
|
|
|
// main-thread.
|
|
|
|
aRv = webSocket->mImpl->mChannel->SetNotificationCallbacks(webSocket->mImpl);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
webSocket->mImpl->AsyncOpen(aRv);
|
|
|
|
} else {
|
|
|
|
nsRefPtr<AsyncOpenRunnable> runnable =
|
|
|
|
new AsyncOpenRunnable(webSocket->mImpl, aRv);
|
|
|
|
runnable->Dispatch(aGlobal.Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// It can be that we have been already disconnected because the WebSocket is
|
|
|
|
// gone away while we where initializing the webSocket.
|
|
|
|
if (!webSocket->mImpl) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
cws.Done();
|
2012-09-10 13:48:14 +00:00
|
|
|
return webSocket.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WebSocket)
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(WebSocket)
|
2012-02-16 16:45:25 +00:00
|
|
|
bool isBlack = tmp->IsBlack();
|
2014-10-10 16:56:43 +00:00
|
|
|
if (isBlack || tmp->mKeepingAlive) {
|
2012-01-26 15:55:30 +00:00
|
|
|
if (tmp->mListenerManager) {
|
2012-10-17 01:22:02 +00:00
|
|
|
tmp->mListenerManager->MarkForCC();
|
2012-01-26 15:55:30 +00:00
|
|
|
}
|
2012-03-14 14:22:10 +00:00
|
|
|
if (!isBlack && tmp->PreservingWrapper()) {
|
2013-09-09 03:28:48 +00:00
|
|
|
// This marks the wrapper black.
|
|
|
|
tmp->GetWrapper();
|
2012-02-16 16:45:25 +00:00
|
|
|
}
|
2012-01-26 15:55:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(WebSocket)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(WebSocket)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-01-26 15:55:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 16:56:43 +00:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImpl->mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImpl->mChannel)
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 16:56:43 +00:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mImpl->mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mImpl->mChannel)
|
|
|
|
tmp->mImpl->Disconnect();
|
|
|
|
MOZ_ASSERT(!tmp->mImpl);
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WebSocket)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WebSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WebSocket, DOMEventTargetHelper)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::DisconnectFromOwner()
|
2012-03-13 00:56:07 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
if (mImpl) {
|
|
|
|
mImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
}
|
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
DontKeepAliveAnyMore();
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl:: initialization
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Init(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
2014-10-10 16:58:05 +00:00
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile,
|
|
|
|
uint32_t aScriptLine,
|
|
|
|
ErrorResult& aRv,
|
|
|
|
bool* aConnectionFailed)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2012-09-10 13:48:14 +00:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
// We need to keep the implementation alive in case the init disconnects it
|
|
|
|
// because of some error.
|
|
|
|
nsRefPtr<WebSocketImpl> kungfuDeathGrip = this;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
mPrincipal = aPrincipal;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = mWebSocket->CheckInnerWindowCorrectness();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Shut down websocket if window is frozen or destroyed (only needed for
|
|
|
|
// "ghost" websockets--see bug 696085)
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!os)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-03 08:24:17 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = os->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = os->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
mScriptFile = aScriptFile;
|
|
|
|
mScriptLine = aScriptLine;
|
|
|
|
} else {
|
2014-10-29 16:54:35 +00:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
unsigned lineno;
|
|
|
|
JS::AutoFilename file;
|
|
|
|
if (JS::DescribeScriptedCaller(aCx, &file, &lineno)) {
|
|
|
|
mScriptFile = file.get();
|
|
|
|
mScriptLine = lineno;
|
|
|
|
}
|
2010-12-03 08:24:17 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
// If we don't have aCx, we are window-less, so we don't have a
|
|
|
|
// inner-windowID. This can happen in sharedWorkers and ServiceWorkers or in
|
|
|
|
// DedicateWorkers created by JSM.
|
|
|
|
if (aCx) {
|
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(aCx);
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// parses the url
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = ParseURL(PromiseFlatString(aURL));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsIScriptContext* sc = nullptr;
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
sc = mWebSocket->GetContextForEventHandlers(&rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Don't allow https:// to open ws://
|
|
|
|
if (!mSecure &&
|
|
|
|
!Preferences::GetBool("network.websocket.allowInsecureFromHTTPS",
|
|
|
|
false)) {
|
|
|
|
// Confirmed we are opening plain ws:// and want to prevent this from a
|
2014-07-18 19:04:28 +00:00
|
|
|
// secure context (e.g. https). Check the principal's uri to determine if
|
|
|
|
// we were loaded from https.
|
2014-08-19 19:02:05 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject(GetEntryGlobal());
|
2014-07-18 19:04:28 +00:00
|
|
|
if (globalObject) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal(globalObject->PrincipalOrNull());
|
|
|
|
if (principal) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
principal->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri) {
|
|
|
|
bool originIsHttps = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = uri->SchemeIs("https", &originIsHttps);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-18 19:04:28 +00:00
|
|
|
if (originIsHttps) {
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2014-07-18 19:04:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 16:42:02 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Assign the sub protocol list and scan it for illegal values
|
|
|
|
for (uint32_t index = 0; index < aProtocolArray.Length(); ++index) {
|
|
|
|
for (uint32_t i = 0; i < aProtocolArray[index].Length(); ++i) {
|
2014-01-04 15:02:17 +00:00
|
|
|
if (aProtocolArray[index][i] < static_cast<char16_t>(0x0021) ||
|
2014-10-10 16:58:05 +00:00
|
|
|
aProtocolArray[index][i] > static_cast<char16_t>(0x007E)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-03 16:42:02 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-05-22 03:48:50 +00:00
|
|
|
mRequestedProtocolList.AppendLiteral(", ");
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF16toUTF8(aProtocolArray[index], mRequestedProtocolList);
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
{
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), mURI);
|
|
|
|
|
|
|
|
// We crash here because we are sure that mURI is a valid URI, so either we
|
|
|
|
// are OOM'ing or something else bad is happening.
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Check content policy.
|
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2014-07-18 19:04:28 +00:00
|
|
|
nsCOMPtr<nsIDocument> originDoc = nsContentUtils::GetDocumentFromScriptContext(sc);
|
2014-08-13 20:26:17 +00:00
|
|
|
mOriginDocument = do_GetWeakReference(originDoc);
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_WEBSOCKET,
|
|
|
|
uri,
|
|
|
|
mPrincipal,
|
|
|
|
originDoc,
|
|
|
|
EmptyCString(),
|
|
|
|
nullptr,
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
if (NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
// Disallowed by content policy.
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv.Throw(NS_ERROR_CONTENT_BLOCKED);
|
|
|
|
return;
|
2011-08-03 16:42:02 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// the constructor should throw a SYNTAX_ERROR only if it fails to parse the
|
2014-10-10 16:58:05 +00:00
|
|
|
// url parameter, so don't throw if InitializeConnection fails, and call
|
2012-09-10 13:48:14 +00:00
|
|
|
// onerror/onclose asynchronously
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_FAILED(InitializeConnection())) {
|
|
|
|
*aConnectionFailed = true;
|
|
|
|
} else {
|
|
|
|
*aConnectionFailed = false;
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
|
|
|
WebSocketImpl::AsyncOpen(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread");
|
|
|
|
|
|
|
|
nsCString asciiOrigin;
|
|
|
|
aRv = nsContentUtils::GetASCIIOrigin(mPrincipal, asciiOrigin);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToLowerCase(asciiOrigin);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), mURI);
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
|
|
|
|
|
|
|
aRv = mChannel->AsyncOpen(uri, asciiOrigin, this, nullptr);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl methods:
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
class nsAutoCloseWS MOZ_FINAL
|
2011-10-20 08:27:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 16:56:43 +00:00
|
|
|
explicit nsAutoCloseWS(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2011-10-20 08:27:13 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
~nsAutoCloseWS()
|
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
if (!mWebSocketImpl->mChannel) {
|
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_INTERNAL_ERROR);
|
2011-10-20 08:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2014-10-10 16:56:43 +00:00
|
|
|
nsRefPtr<WebSocketImpl> mWebSocketImpl;
|
2011-10-20 08:27:13 +00:00
|
|
|
};
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
nsresult
|
2014-10-10 16:58:05 +00:00
|
|
|
WebSocketImpl::InitializeConnection()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-12-15 23:19:01 +00:00
|
|
|
NS_ABORT_IF_FALSE(!mChannel, "mChannel should be null");
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
nsCOMPtr<nsIWebSocketChannel> wsChannel;
|
|
|
|
nsAutoCloseWS autoClose(this);
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
if (mSecure) {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=wss", &rv);
|
|
|
|
} else {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=ws", &rv);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// add ourselves to the document's load group and
|
|
|
|
// provide the http stack the loadgroup info too
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup) {
|
|
|
|
rv = wsChannel->SetLoadGroup(loadGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = loadGroup->AddRequest(this, nullptr);
|
2011-10-20 08:27:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-17 07:12:26 +00:00
|
|
|
|
|
|
|
mWeakLoadGroup = do_GetWeakReference(loadGroup);
|
2011-10-20 08:27:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 20:26:17 +00:00
|
|
|
// manually adding loadinfo to the channel since it
|
|
|
|
// was not set during channel creation.
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mOriginDocument);
|
2014-11-11 22:47:13 +00:00
|
|
|
|
|
|
|
// mOriginDocument has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mOriginDocument = nullptr;
|
|
|
|
|
2014-08-13 20:26:17 +00:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo =
|
2014-11-14 16:56:55 +00:00
|
|
|
new LoadInfo(doc ?
|
|
|
|
doc->NodePrincipal() : mPrincipal.get(),
|
|
|
|
mPrincipal,
|
2014-08-13 20:26:17 +00:00
|
|
|
doc,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_WEBSOCKET);
|
|
|
|
rv = wsChannel->SetLoadInfo(loadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
|
|
|
rv = wsChannel->SetProtocol(mRequestedProtocolList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-02 23:55:17 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(wsChannel);
|
|
|
|
NS_ENSURE_TRUE(rr, NS_ERROR_FAILURE);
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
rv = rr->RetargetDeliveryTo(this);
|
2011-10-20 08:27:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
mChannel = wsChannel;
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::DispatchConnectionCloseEvents()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSED);
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
// Call 'onerror' if needed
|
|
|
|
if (mFailed) {
|
2014-10-10 16:56:43 +00:00
|
|
|
nsresult rv =
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event");
|
|
|
|
}
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = mWebSocket->CreateAndDispatchCloseEvent(mCloseEventWasClean,
|
|
|
|
mCloseEventCode,
|
|
|
|
mCloseEventReason);
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the close event");
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocket->UpdateMustKeepAlive();
|
2012-06-04 23:38:32 +00:00
|
|
|
Disconnect();
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2010-06-17 18:36:01 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocket::CreateAndDispatchSimpleEvent(const nsAString& aName)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 18:36:01 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 11:34:29 +00:00
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = event->InitEvent(aName, false, false);
|
2010-06-17 18:36:01 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::CreateAndDispatchMessageEvent(const nsACString& aData,
|
2014-10-10 16:58:05 +00:00
|
|
|
bool aIsBinary)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-10 16:58:19 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 16:58:19 +00:00
|
|
|
} else {
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(!mIsMainThread);
|
|
|
|
MOZ_ASSERT(mImpl->mWorkerPrivate);
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mImpl->mWorkerPrivate->GlobalScope()))) {
|
2014-10-10 16:58:19 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:19 +00:00
|
|
|
return CreateAndDispatchMessageEvent(jsapi.cx(), aData, aIsBinary);
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WebSocket::CreateAndDispatchMessageEvent(JSContext* aCx,
|
|
|
|
const nsACString& aData,
|
|
|
|
bool aIsBinary)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 18:36:01 +00:00
|
|
|
return NS_OK;
|
2014-05-23 07:47:46 +00:00
|
|
|
}
|
2011-05-23 18:53:31 +00:00
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
// Create appropriate JS object for message
|
2014-10-10 16:58:05 +00:00
|
|
|
JS::Rooted<JS::Value> jsData(aCx);
|
|
|
|
if (aIsBinary) {
|
2014-10-14 09:28:07 +00:00
|
|
|
if (mBinaryType == dom::BinaryType::Blob) {
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = nsContentUtils::CreateBlobBuffer(aCx, GetOwner(), aData,
|
|
|
|
&jsData);
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-14 09:28:07 +00:00
|
|
|
} else if (mBinaryType == dom::BinaryType::Arraybuffer) {
|
2014-10-10 16:58:05 +00:00
|
|
|
JS::Rooted<JSObject*> arrayBuf(aCx);
|
|
|
|
nsresult rv = nsContentUtils::CreateArrayBuffer(aCx, aData,
|
|
|
|
arrayBuf.address());
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
jsData = OBJECT_TO_JSVAL(arrayBuf);
|
2011-12-15 23:19:01 +00:00
|
|
|
} else {
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_RUNTIMEABORT("Unknown binary type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
2013-05-22 16:05:28 +00:00
|
|
|
} else {
|
|
|
|
// JS string
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
JSString* jsString;
|
2014-10-10 16:58:05 +00:00
|
|
|
jsString = JS_NewUCStringCopyN(aCx, utf16Data.get(), utf16Data.Length());
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
jsData = STRING_TO_JSVAL(jsString);
|
2011-05-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-06-17 18:36:01 +00:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 11:34:29 +00:00
|
|
|
rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
|
|
|
|
rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
|
2011-10-17 14:59:28 +00:00
|
|
|
false, false,
|
2011-05-23 18:53:31 +00:00
|
|
|
jsData,
|
2014-10-10 16:56:43 +00:00
|
|
|
mImpl->mUTF16Origin,
|
2012-07-30 14:20:58 +00:00
|
|
|
EmptyString(), nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::CreateAndDispatchCloseEvent(bool aWasClean,
|
|
|
|
uint16_t aCode,
|
2014-10-10 16:56:43 +00:00
|
|
|
const nsAString &aReason)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 18:36:01 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-30 23:00:31 +00:00
|
|
|
CloseEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mWasClean = aWasClean;
|
|
|
|
init.mCode = aCode;
|
|
|
|
init.mReason = aReason;
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2014-06-30 23:00:31 +00:00
|
|
|
nsRefPtr<CloseEvent> event =
|
|
|
|
CloseEvent::Constructor(this, NS_LITERAL_STRING("close"), init);
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrefEnabledRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit PrefEnabledRunnable(WorkerPrivate* aWorkerPrivate)
|
2014-10-10 16:58:05 +00:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mEnabled(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-10-30 15:22:31 +00:00
|
|
|
mEnabled = Preferences::GetBool("dom.workers.websocket.enabled", false);
|
2014-10-10 16:58:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEnabled() const
|
|
|
|
{
|
|
|
|
return mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mEnabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2014-10-10 16:58:05 +00:00
|
|
|
WebSocket::PrefEnabled(JSContext* /* aCx */, JSObject* /* aGlobal */)
|
2010-12-08 22:12:51 +00:00
|
|
|
{
|
2014-10-30 15:22:31 +00:00
|
|
|
// WebSockets are always enabled on main-thread.
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_IsMainThread()) {
|
2014-10-30 15:22:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-30 15:22:31 +00:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-30 15:22:31 +00:00
|
|
|
nsRefPtr<PrefEnabledRunnable> runnable =
|
|
|
|
new PrefEnabledRunnable(workerPrivate);
|
|
|
|
runnable->Dispatch(workerPrivate->GetJSContext());
|
|
|
|
|
|
|
|
return runnable->IsEnabled();
|
2010-12-08 22:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ParseURL(const nsAString& aURL)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_TRUE(!aURL.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCOMPtr<nsIURL> parsedURL = do_QueryInterface(uri, &rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString fragment;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetRef(fragment);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && fragment.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString scheme;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetScheme(scheme);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !scheme.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString host;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetAsciiHost(host);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !host.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetPort(&port);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
rv = NS_CheckPortSafety(port, scheme.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString filePath;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetFilePath(filePath);
|
|
|
|
if (filePath.IsEmpty()) {
|
2014-05-26 18:55:08 +00:00
|
|
|
filePath.Assign('/');
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString query;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetQuery(query);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("ws")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mSecure = false;
|
2010-06-17 18:34:24 +00:00
|
|
|
mPort = (port == -1) ? DEFAULT_WS_SCHEME_PORT : port;
|
|
|
|
} else if (scheme.LowerCaseEqualsLiteral("wss")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mSecure = true;
|
2010-06-17 18:34:24 +00:00
|
|
|
mPort = (port == -1) ? DEFAULT_WSS_SCHEME_PORT : port;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2011-08-04 00:32:23 +00:00
|
|
|
rv = nsContentUtils::GetUTFOrigin(parsedURL, mUTF16Origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
mAsciiHost = host;
|
|
|
|
ToLowerCase(mAsciiHost);
|
|
|
|
|
|
|
|
mResource = filePath;
|
|
|
|
if (!query.IsEmpty()) {
|
2014-05-22 03:48:51 +00:00
|
|
|
mResource.Append('?');
|
2010-06-17 18:34:24 +00:00
|
|
|
mResource.Append(query);
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = mResource.Length();
|
|
|
|
uint32_t i;
|
2010-06-17 18:34:24 +00:00
|
|
|
for (i = 0; i < length; ++i) {
|
2014-01-04 15:02:17 +00:00
|
|
|
if (mResource[i] < static_cast<char16_t>(0x0021) ||
|
|
|
|
mResource[i] > static_cast<char16_t>(0x007E)) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->mOriginalURL = aURL;
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
rv = parsedURL->GetSpec(mURI);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-27 21:09:29 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-07-21 16:05:56 +00:00
|
|
|
// Methods that keep alive the WebSocket object when:
|
|
|
|
// 1. the object has registered event listeners that can be triggered
|
|
|
|
// ("strong event listeners");
|
|
|
|
// 2. there are outgoing not sent messages.
|
2010-06-27 21:09:29 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::UpdateMustKeepAlive()
|
2010-06-27 21:09:29 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
if (!mCheckMustKeepAlive || !mImpl) {
|
2010-06-27 21:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool shouldKeepAlive = false;
|
2014-11-11 22:47:13 +00:00
|
|
|
uint16_t readyState = ReadyState();
|
2010-07-21 16:05:56 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mListenerManager) {
|
2014-11-11 22:47:13 +00:00
|
|
|
switch (readyState)
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
case CONNECTING:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-10-29 04:33:51 +00:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onopen) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
case OPEN:
|
|
|
|
case CLOSING:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-10-29 04:33:51 +00:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose) ||
|
2014-10-14 09:28:07 +00:00
|
|
|
mOutgoingBufferedAmount != 0) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
case CLOSED:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-06-04 23:38:32 +00:00
|
|
|
shouldKeepAlive = false;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 16:05:56 +00:00
|
|
|
|
|
|
|
if (mKeepingAlive && !shouldKeepAlive) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->ReleaseObject();
|
2010-07-21 16:05:56 +00:00
|
|
|
} else if (!mKeepingAlive && shouldKeepAlive) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = true;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->AddRefObject();
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::DontKeepAliveAnyMore()
|
2010-06-27 21:09:29 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2010-07-21 16:05:56 +00:00
|
|
|
if (mKeepingAlive) {
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->ReleaseObject();
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mCheckMustKeepAlive = false;
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WebSocketWorkerFeature MOZ_FINAL : public WorkerFeature
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit WebSocketWorkerFeature(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Notify(JSContext* aCx, Status aStatus) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
|
|
|
|
|
|
|
if (aStatus >= Canceling) {
|
2014-12-12 19:37:07 +00:00
|
|
|
mWebSocketImpl->mWorkerShuttingDown = true;
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suspend(JSContext* aCx)
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
mWebSocketImpl->mWorkerShuttingDown = true;
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mWebSocketImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::AddRefObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::ReleaseObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
bool
|
2014-11-11 22:47:13 +00:00
|
|
|
WebSocketImpl::RegisterFeature()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!mWorkerFeature);
|
|
|
|
mWorkerFeature = new WebSocketWorkerFeature(this);
|
|
|
|
|
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
if (!mWorkerPrivate->AddFeature(cx, mWorkerFeature)) {
|
|
|
|
NS_WARNING("Failed to register a feature.");
|
|
|
|
mWorkerFeature = nullptr;
|
2014-12-12 19:37:07 +00:00
|
|
|
return false;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
SetHasFeatureRegistered(true);
|
|
|
|
#endif
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
return true;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
|
|
|
WebSocketImpl::UnregisterFeature()
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mDisconnectingOrDisconnected);
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerFeature);
|
|
|
|
|
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
mWorkerPrivate->RemoveFeature(cx, mWorkerFeature);
|
|
|
|
mWorkerFeature = nullptr;
|
2014-12-12 19:37:07 +00:00
|
|
|
mWorkerPrivate = nullptr;
|
2014-11-11 22:47:13 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
SetHasFeatureRegistered(false);
|
|
|
|
#endif
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-15 23:23:00 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::UpdateURI()
|
2011-12-15 23:23:00 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-12-15 23:23:00 +00:00
|
|
|
// Check for Redirections
|
2014-04-17 18:53:58 +00:00
|
|
|
nsRefPtr<BaseWebSocketChannel> channel;
|
|
|
|
channel = static_cast<BaseWebSocketChannel*>(mChannel.get());
|
|
|
|
MOZ_ASSERT(channel);
|
2011-12-15 23:23:00 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
channel->GetEffectiveURL(mWebSocket->mEffectiveURL);
|
2014-04-17 18:53:58 +00:00
|
|
|
mSecure = channel->IsEncrypted();
|
2011-12-15 23:23:00 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-16 21:16:08 +00:00
|
|
|
void
|
2013-05-23 11:41:32 +00:00
|
|
|
WebSocket::EventListenerAdded(nsIAtom* aType)
|
2013-04-16 21:16:08 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 11:41:32 +00:00
|
|
|
UpdateMustKeepAlive();
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 21:16:08 +00:00
|
|
|
void
|
2013-05-23 11:41:32 +00:00
|
|
|
WebSocket::EventListenerRemoved(nsIAtom* aType)
|
2013-04-16 21:16:08 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 11:41:32 +00:00
|
|
|
UpdateMustKeepAlive();
|
2013-04-16 21:16:08 +00:00
|
|
|
}
|
2013-05-23 11:41:32 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-09-10 13:48:14 +00:00
|
|
|
// WebSocket - methods
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
// webIDL: readonly attribute unsigned short readyState;
|
|
|
|
uint16_t
|
2014-10-14 09:28:07 +00:00
|
|
|
WebSocket::ReadyState()
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocket::SetReadyState(uint16_t aReadyState)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mReadyState = aReadyState;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: readonly attribute unsigned long bufferedAmount;
|
|
|
|
uint32_t
|
|
|
|
WebSocket::BufferedAmount() const
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mOutgoingBufferedAmount;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
dom::BinaryType
|
|
|
|
WebSocket::BinaryType() const
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mBinaryType;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
void
|
|
|
|
WebSocket::SetBinaryType(dom::BinaryType aData)
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
mBinaryType = aData;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString url
|
|
|
|
void
|
|
|
|
WebSocket::GetUrl(nsAString& aURL)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
if (mEffectiveURL.IsEmpty()) {
|
|
|
|
aURL = mOriginalURL;
|
|
|
|
} else {
|
|
|
|
aURL = mEffectiveURL;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString extensions;
|
|
|
|
void
|
|
|
|
WebSocket::GetExtensions(nsAString& aExtensions)
|
2011-08-04 03:46:13 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedExtensions, aExtensions);
|
2011-08-04 03:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString protocol;
|
|
|
|
void
|
|
|
|
WebSocket::GetProtocol(nsAString& aProtocol)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedProtocol, aProtocol);
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: void send(DOMString data);
|
|
|
|
void
|
|
|
|
WebSocket::Send(const nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_ConvertUTF16toUTF8 msgString(aData);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, msgString.Length(), false, aRv);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2014-10-08 16:15:23 +00:00
|
|
|
WebSocket::Send(File& aData, ErrorResult& aRv)
|
2011-12-15 23:19:01 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCOMPtr<nsIInputStream> msgStream;
|
2014-10-08 16:15:22 +00:00
|
|
|
nsresult rv = aData.GetInternalStream(getter_AddRefs(msgStream));
|
2012-09-10 13:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
uint64_t msgLength;
|
2014-10-08 16:15:22 +00:00
|
|
|
rv = aData.GetSize(&msgLength);
|
2012-09-10 13:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
if (msgLength > UINT32_MAX) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(msgStream, EmptyCString(), msgLength, true, aRv);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2013-08-05 17:40:01 +00:00
|
|
|
WebSocket::Send(const ArrayBuffer& aData,
|
2012-09-10 13:48:14 +00:00
|
|
|
ErrorResult& aRv)
|
2011-08-03 19:15:25 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 19:15:25 +00:00
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2013-08-05 17:40:01 +00:00
|
|
|
WebSocket::Send(const ArrayBufferView& aData,
|
2012-09-10 13:48:14 +00:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-12-15 23:19:01 +00:00
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2014-10-14 09:28:07 +00:00
|
|
|
WebSocket::Send(nsIInputStream* aMsgStream,
|
|
|
|
const nsACString& aMsgString,
|
|
|
|
uint32_t aMsgLength,
|
|
|
|
bool aIsBinary,
|
|
|
|
ErrorResult& aRv)
|
2011-12-15 23:19:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CONNECTING) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Always increment outgoing buffer len, even if closed
|
|
|
|
mOutgoingBufferedAmount += aMsgLength;
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
// We must have mImpl when connected.
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
MOZ_ASSERT(readyState == OPEN, "Unknown state in WebSocket::Send");
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
|
|
|
if (aMsgStream) {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendBinaryStream(aMsgStream, aMsgLength);
|
2012-09-10 13:48:14 +00:00
|
|
|
} else {
|
|
|
|
if (aIsBinary) {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendBinaryMsg(aMsgString);
|
2012-09-10 13:48:14 +00:00
|
|
|
} else {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendMsg(aMsgString);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
UpdateMustKeepAlive();
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: void close(optional unsigned short code, optional DOMString reason):
|
|
|
|
void
|
|
|
|
WebSocket::Close(const Optional<uint16_t>& aCode,
|
|
|
|
const Optional<nsAString>& aReason,
|
|
|
|
ErrorResult& aRv)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 19:15:25 +00:00
|
|
|
|
|
|
|
// the reason code is optional, but if provided it must be in a specific range
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t closeCode = 0;
|
2012-09-10 13:48:14 +00:00
|
|
|
if (aCode.WasPassed()) {
|
|
|
|
if (aCode.Value() != 1000 && (aCode.Value() < 3000 || aCode.Value() > 4999)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
2012-02-01 04:41:43 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
closeCode = aCode.Value();
|
2011-08-03 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCString closeReason;
|
|
|
|
if (aReason.WasPassed()) {
|
|
|
|
CopyUTF16toUTF8(aReason.Value(), closeReason);
|
2011-08-03 19:15:25 +00:00
|
|
|
|
|
|
|
// The API requires the UTF-8 string to be 123 or less bytes
|
2012-02-01 04:41:43 +00:00
|
|
|
if (closeReason.Length() > 123) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2012-02-01 04:41:43 +00:00
|
|
|
}
|
2011-08-03 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
// If the webSocket is not closed we MUST have a mImpl.
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
|
|
|
if (readyState == CONNECTING) {
|
|
|
|
mImpl->FailConnection(closeCode, closeReason);
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
MOZ_ASSERT(readyState == OPEN);
|
|
|
|
mImpl->CloseConnection(closeCode, closeReason);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIObserver
|
2012-02-21 22:57:10 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2012-02-21 22:57:10 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if ((readyState == WebSocket::CLOSING) ||
|
|
|
|
(readyState == WebSocket::CLOSED)) {
|
2012-02-21 22:57:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aSubject);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!mWebSocket->GetOwner() || window != mWebSocket->GetOwner()) {
|
2012-02-21 22:57:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) ||
|
|
|
|
(strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0))
|
|
|
|
{
|
2012-06-04 23:38:32 +00:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-21 22:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIRequest
|
2011-05-22 01:27:52 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetName(nsACString& aName)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
CopyUTF16toUTF8(mWebSocket->mOriginalURL, aName);
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::IsPending(bool* aValue)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
*aValue = (readyState != WebSocket::CLOSED);
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetStatus(nsresult* aStatus)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
*aStatus = NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CancelRunnable MOZ_FINAL : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CancelRunnable(WorkerPrivate* aWorkerPrivate, WebSocketImpl* aImpl)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, true);
|
|
|
|
return !NS_FAILED(mImpl->CancelInternal());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<WebSocketImpl> mImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-08-07 13:44:19 +00:00
|
|
|
// Window closed, stop/reload button pressed, user navigated away from page, etc.
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Cancel(nsresult aStatus)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (!mIsMainThread) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2014-10-10 16:58:05 +00:00
|
|
|
nsRefPtr<CancelRunnable> runnable =
|
|
|
|
new CancelRunnable(mWorkerPrivate, this);
|
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CancelInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WebSocketImpl::CancelInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-11-07 14:35:54 +00:00
|
|
|
// If CancelInternal is called by a runnable, we may already be disconnected
|
|
|
|
// by the time it runs.
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2014-11-07 14:35:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSING || readyState == WebSocket::CLOSED) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
ConsoleError();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2012-02-01 04:41:43 +00:00
|
|
|
return CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Suspend()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Resume()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetLoadGroup(nsILoadGroup** aLoadGroup)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aLoadGroup = nullptr;
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mIsMainThread) {
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsIScriptContext* sc = mWebSocket->GetContextForEventHandlers(&rv);
|
|
|
|
nsCOMPtr<nsIDocument> doc =
|
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* window = wp->GetWindow();
|
|
|
|
if (!window) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
2011-05-22 01:27:52 +00:00
|
|
|
if (doc) {
|
2014-03-15 19:00:15 +00:00
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetLoadFlags(nsLoadFlags* aLoadFlags)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
*aLoadFlags = nsIRequest::LOAD_BACKGROUND;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::SetLoadFlags(nsLoadFlags aLoadFlags)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// we won't change the load flags at all.
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WorkerRunnableDispatcher MOZ_FINAL : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WorkerRunnableDispatcher(WorkerPrivate* aWorkerPrivate, nsIRunnable* aEvent)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
|
|
|
, mEvent(aEvent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, true);
|
|
|
|
return !NS_FAILED(mEvent->Run());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketImpl::Dispatch(nsIRunnable* aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
// If the target is the main-thread we can just dispatch the runnable.
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mIsMainThread) {
|
2014-10-10 16:58:05 +00:00
|
|
|
return NS_DispatchToMainThread(aEvent);
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// No messages when disconnected.
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
NS_WARNING("Dispatching a WebSocket event after the disconnection!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorkerShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(HasFeatureRegistered());
|
|
|
|
#endif
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// If the target is a worker, we have to use a custom WorkerRunnableDispatcher
|
|
|
|
// runnable.
|
|
|
|
nsRefPtr<WorkerRunnableDispatcher> event =
|
|
|
|
new WorkerRunnableDispatcher(mWorkerPrivate, aEvent);
|
|
|
|
if (!event->Dispatch(nullptr)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketImpl::IsOnCurrentThread(bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = IsTargetThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebSocketImpl::IsTargetThread() const
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
return NS_IsMainThread() == mIsMainThread;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
void
|
|
|
|
WebSocket::AssertIsOnTargetThread() const
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-14 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|