2010-06-17 22:34:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
#include "WebSocket.h"
|
|
|
|
#include "mozilla/dom/WebSocketBinding.h"
|
2014-04-17 22:53:58 +04:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2011-05-23 20:46:36 +04:00
|
|
|
|
2013-08-27 06:05:20 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-10-10 20:56:43 +04:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-08-12 14:39:31 +03:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2015-07-10 19:15:30 +03:00
|
|
|
#include "mozilla/dom/nsCSPContext.h"
|
|
|
|
#include "mozilla/dom/nsCSPUtils.h"
|
2014-05-23 11:47:46 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-10-10 20:58:05 +04:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2014-10-10 20:58:19 +04:00
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2010-06-17 22:34:24 +04:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-06-17 22:34:24 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIPromptFactory.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIPrompt.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2014-07-01 03:00:31 +04:00
|
|
|
#include "mozilla/dom/CloseEvent.h"
|
2010-06-17 22:36:01 +04:00
|
|
|
#include "nsICryptoHash.h"
|
2010-12-21 20:05:34 +03:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIScriptError.h"
|
2011-05-22 05:27:52 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
2011-05-22 05:27:52 +04:00
|
|
|
#include "nsILoadGroup.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 20:06:36 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-12-08 03:19:43 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2012-01-26 19:55:30 +04:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-02-22 02:57:10 +04:00
|
|
|
#include "nsIObserverService.h"
|
2014-10-10 20:56:43 +04: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 20:58:05 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2014-10-10 20:56:43 +04:00
|
|
|
#include "nsWeakReference.h"
|
2014-04-17 22:53:58 +04:00
|
|
|
|
|
|
|
using namespace mozilla::net;
|
2014-10-10 20:58:05 +04:00
|
|
|
using namespace mozilla::dom::workers;
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WebSocketImpl final : public nsIInterfaceRequestor
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsIWebSocketListener
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
, public nsIRequest
|
|
|
|
, public nsIEventTarget
|
2014-10-10 20:56:43 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIWEBSOCKETLISTENER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSIREQUEST
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-10-10 20:58:05 +04:00
|
|
|
NS_DECL_NSIEVENTTARGET
|
2015-07-10 06:21:46 +03:00
|
|
|
// missing from NS_DECL_NSIEVENTTARGET because MSVC
|
|
|
|
nsresult Dispatch(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
return Dispatch(nsCOMPtr<nsIRunnable>(aEvent).forget(), aFlags);
|
|
|
|
}
|
2014-10-10 20:56:43 +04:00
|
|
|
|
2014-10-11 01:28:35 +04:00
|
|
|
explicit WebSocketImpl(WebSocket* aWebSocket)
|
2014-10-10 20:58:05 +04:00
|
|
|
: mWebSocket(aWebSocket)
|
2014-10-10 20:56:43 +04:00
|
|
|
, mOnCloseScheduled(false)
|
|
|
|
, mFailed(false)
|
2014-12-12 22:37:07 +03:00
|
|
|
, mDisconnectingOrDisconnected(false)
|
2014-10-10 20:56:43 +04:00
|
|
|
, mCloseEventWasClean(false)
|
|
|
|
, mCloseEventCode(nsIWebSocketChannel::CLOSE_ABNORMAL)
|
|
|
|
, mScriptLine(0)
|
|
|
|
, mInnerWindowID(0)
|
2014-10-10 20:58:05 +04:00
|
|
|
, mWorkerPrivate(nullptr)
|
2014-11-12 01:47:13 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mHasFeatureRegistered(false)
|
|
|
|
#endif
|
2014-12-12 22:37:07 +03:00
|
|
|
, mIsMainThread(true)
|
2015-02-17 20:40:09 +03:00
|
|
|
, mMutex("WebSocketImpl::mMutex")
|
2014-12-12 22:37:07 +03:00
|
|
|
, mWorkerShuttingDown(false)
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mWorkerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2014-12-12 22:37:07 +03:00
|
|
|
mIsMainThread = false;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssertIsOnTargetThread() const
|
2014-10-10 20:56:43 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(IsTargetThread());
|
2014-10-10 20:56:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04: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);
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
void AsyncOpen(nsIPrincipal* aPrincipal, ErrorResult& aRv);
|
2014-10-10 20:56:43 +04:00
|
|
|
|
|
|
|
nsresult ParseURL(const nsAString& aURL);
|
2015-05-15 01:43:06 +03:00
|
|
|
nsresult InitializeConnection(nsIPrincipal* aPrincipal);
|
2014-10-10 20:56:43 +04:00
|
|
|
|
|
|
|
// These methods when called can release the WebSocket object
|
|
|
|
void FailConnection(uint16_t reasonCode,
|
|
|
|
const nsACString& aReasonString = EmptyCString());
|
|
|
|
nsresult CloseConnection(uint16_t reasonCode,
|
2015-04-07 20:31:51 +03:00
|
|
|
const nsACString& aReasonString = EmptyCString());
|
2014-10-10 20:56:43 +04:00
|
|
|
nsresult Disconnect();
|
2014-10-10 20:58:05 +04:00
|
|
|
void DisconnectInternal();
|
2014-10-10 20:56:43 +04: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 20:58:05 +04:00
|
|
|
void AddRefObject();
|
|
|
|
void ReleaseObject();
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
bool RegisterFeature();
|
2014-10-10 20:58:05 +04:00
|
|
|
void UnregisterFeature();
|
|
|
|
|
|
|
|
nsresult CancelInternal();
|
|
|
|
|
|
|
|
nsRefPtr<WebSocket> mWebSocket;
|
2014-10-10 20:56:43 +04: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 22:37:07 +03:00
|
|
|
bool mDisconnectingOrDisconnected;
|
2014-10-10 20:56:43 +04: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 20:58:05 +04:00
|
|
|
nsCString mURI;
|
2014-10-10 20:56:43 +04:00
|
|
|
nsCString mRequestedProtocolList;
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
nsWeakPtr mOriginDocument;
|
2014-10-10 20:56:43 +04:00
|
|
|
|
|
|
|
// 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 20:58:05 +04:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsAutoPtr<WorkerFeature> mWorkerFeature;
|
|
|
|
|
2014-11-12 01:47:13 +03: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 11:12:26 +04:00
|
|
|
nsWeakPtr mWeakLoadGroup;
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
bool mIsMainThread;
|
2015-02-17 20:40:09 +03:00
|
|
|
|
|
|
|
// This mutex protects mWorkerShuttingDown.
|
|
|
|
mozilla::Mutex mMutex;
|
2014-12-12 22:37:07 +03:00
|
|
|
bool mWorkerShuttingDown;
|
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
private:
|
|
|
|
~WebSocketImpl()
|
|
|
|
{
|
|
|
|
// If we threw during Init we never called disconnect
|
2014-12-12 22:37:07 +03:00
|
|
|
if (!mDisconnectingOrDisconnected) {
|
2014-10-10 20:56:43 +04:00
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WebSocketImpl,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIWebSocketListener,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference,
|
2014-10-10 20:58:05 +04:00
|
|
|
nsIRequest,
|
|
|
|
nsIEventTarget)
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CallDispatchConnectionCloseEvents final : public nsCancelableRunnable
|
2012-06-05 03:38:32 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 20:56:43 +04:00
|
|
|
explicit CallDispatchConnectionCloseEvents(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
aWebSocketImpl->AssertIsOnTargetThread();
|
|
|
|
}
|
2012-06-05 03:38:32 +04:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
mWebSocketImpl->AssertIsOnTargetThread();
|
2014-10-10 20:56:43 +04:00
|
|
|
mWebSocketImpl->DispatchConnectionCloseEvents();
|
2012-06-05 03:38:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-10 20:56:43 +04:00
|
|
|
nsRefPtr<WebSocketImpl> mWebSocketImpl;
|
2012-06-05 03:38:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl
|
2012-06-05 03:38:32 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class PrintErrorOnConsoleRunnable final : public WorkerMainThreadRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::PrintErrorOnConsole(const char *aBundleURI,
|
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
// This method must run on the main thread.
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
nsRefPtr<PrintErrorOnConsoleRunnable> runnable =
|
|
|
|
new PrintErrorOnConsoleRunnable(this, aBundleURI, aError, aFormatStrings,
|
|
|
|
aFormatStringsLen);
|
|
|
|
runnable->Dispatch(mWorkerPrivate->GetJSContext());
|
|
|
|
return runnable->ErrorCode();
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv;
|
2011-05-22 05:27:52 +04:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsCOMPtr<nsIStringBundle> strBundle;
|
|
|
|
rv = bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-12-22 01:51:29 +04:00
|
|
|
nsCOMPtr<nsIScriptError> errorObject(
|
2011-05-22 05:27:52 +04:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
|
|
|
aFormatStringsLen,
|
|
|
|
getter_Copies(message));
|
2010-06-17 22:34:24 +04:00
|
|
|
} else {
|
2011-05-22 05:27:52 +04:00
|
|
|
rv = strBundle->GetStringFromName(aError, getter_Copies(message));
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-29 19:54:35 +03: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-22 01:51:29 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
// print the error message directly to the JS console
|
2011-12-22 01:51:29 +04:00
|
|
|
rv = console->LogMessage(errorObject);
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-24 21:12:17 +03:00
|
|
|
class CancelWebSocketRunnable final : public nsRunnable
|
2015-03-19 01:38:02 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CancelWebSocketRunnable(nsIWebSocketChannel* aChannel, uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
|
|
|
: mChannel(aChannel)
|
|
|
|
, mReasonCode(aReasonCode)
|
|
|
|
, mReasonString(aReasonString)
|
|
|
|
{}
|
|
|
|
|
2015-03-24 21:12:17 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-03-19 01:38:02 +03:00
|
|
|
{
|
|
|
|
mChannel->Close(mReasonCode, mReasonString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIWebSocketChannel> mChannel;
|
|
|
|
uint16_t mReasonCode;
|
|
|
|
nsCString mReasonString;
|
|
|
|
};
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
class MOZ_STACK_CLASS MaybeDisconnect
|
|
|
|
{
|
|
|
|
public:
|
2014-12-13 22:17:04 +03:00
|
|
|
explicit MaybeDisconnect(WebSocketImpl* aImpl)
|
2014-12-12 22:37:07 +03:00
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~MaybeDisconnect()
|
|
|
|
{
|
2015-02-17 20:40:09 +03:00
|
|
|
bool toDisconnect = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mImpl->mMutex);
|
|
|
|
toDisconnect = mImpl->mWorkerShuttingDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toDisconnect) {
|
2014-12-12 22:37:07 +03:00
|
|
|
mImpl->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2015-04-08 21:09:26 +03:00
|
|
|
class CloseConnectionRunnable final : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CloseConnectionRunnable(WebSocketImpl* aImpl,
|
|
|
|
uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
, mReasonCode(aReasonCode)
|
|
|
|
, mReasonString(aReasonString)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
return mImpl->CloseConnection(mReasonCode, mReasonString);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<WebSocketImpl> mImpl;
|
|
|
|
uint16_t mReasonCode;
|
|
|
|
const nsCString mReasonString;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::CloseConnection(uint16_t aReasonCode,
|
2015-04-07 20:31:51 +03:00
|
|
|
const nsACString& aReasonString)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2015-04-08 21:09:26 +03:00
|
|
|
if (!IsTargetThread()) {
|
|
|
|
nsRefPtr<nsRunnable> runnable =
|
|
|
|
new CloseConnectionRunnable(this, aReasonCode, aReasonString);
|
|
|
|
return Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 22:37:07 +03: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 13:28:07 +04:00
|
|
|
uint16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState == WebSocket::CLOSING ||
|
|
|
|
readyState == WebSocket::CLOSED) {
|
2011-06-08 16:27:11 +04:00
|
|
|
return NS_OK;
|
2012-06-05 03:38:32 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// The common case...
|
|
|
|
if (mChannel) {
|
2014-10-14 13:28:07 +04:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2014-10-10 20:58:05 +04:00
|
|
|
|
|
|
|
// The channel has to be closed on the main-thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return mChannel->Close(aReasonCode, aReasonString);
|
|
|
|
}
|
|
|
|
|
2015-04-07 20:31:51 +03:00
|
|
|
nsRefPtr<CancelWebSocketRunnable> runnable =
|
|
|
|
new CancelWebSocketRunnable(mChannel, aReasonCode, aReasonString);
|
|
|
|
return NS_DispatchToMainThread(runnable);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// No channel, but not disconnected: canceled or failed early
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CONNECTING,
|
2012-06-05 03:38:32 +04:00
|
|
|
"Should only get here for early websocket cancel/error");
|
2011-05-22 05:27:52 +04:00
|
|
|
|
2012-06-05 03:38:32 +04: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 13:28:07 +04:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// Can be called from Cancel() or Init() codepaths, so need to dispatch
|
|
|
|
// onerror/onclose asynchronously
|
|
|
|
ScheduleConnectionCloseEvents(
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2012-06-05 03:38:32 +04:00
|
|
|
(aReasonCode == nsIWebSocketChannel::CLOSE_NORMAL ||
|
|
|
|
aReasonCode == nsIWebSocketChannel::CLOSE_GOING_AWAY) ?
|
|
|
|
NS_OK : NS_ERROR_FAILURE,
|
|
|
|
false);
|
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::ConsoleError()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2015-03-19 01:38:02 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mWorkerShuttingDown) {
|
|
|
|
// Too late to report anything, bail out.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(mURI);
|
|
|
|
const char16_t* formatStrings[] = { specUTF16.get() };
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
if (mWebSocket->ReadyState() < WebSocket::OPEN) {
|
2014-10-10 20:58:05 +04: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 22:34:24 +04:00
|
|
|
}
|
2011-08-03 03:55:17 +04:00
|
|
|
/// todo some specific errors - like for message too large
|
2014-10-10 20:58:05 +04:00
|
|
|
return NS_OK;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-22 12:04:08 +04:00
|
|
|
void
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::FailConnection(uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
2012-02-22 02:57:10 +04:00
|
|
|
CloseConnection(aReasonCode, aReasonString);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class DisconnectInternalRunnable final : public WorkerMainThreadRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-11 01:28:35 +04:00
|
|
|
explicit DisconnectInternalRunnable(WebSocketImpl* aImpl)
|
2014-10-10 20:58:05 +04:00
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
mImpl->DisconnectInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// A raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Disconnect()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-12-12 22:37:07 +03: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 20:58:05 +04: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-22 02:57:10 +04:00
|
|
|
}
|
|
|
|
|
2011-10-20 12:27:13 +04:00
|
|
|
// DontKeepAliveAnyMore() can release the object. So hold a reference to this
|
|
|
|
// until the end of the method.
|
2014-10-10 20:56:43 +04:00
|
|
|
nsRefPtr<WebSocketImpl> kungfuDeathGrip = this;
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-10 20:58:05 +04: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 22:37:07 +03:00
|
|
|
if (mWorkerPrivate && mWorkerFeature) {
|
|
|
|
UnregisterFeature();
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
// We want to release the WebSocket in the correct thread.
|
|
|
|
mWebSocket = nullptr;
|
2011-06-08 16:27:11 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_OK;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
void
|
|
|
|
WebSocketImpl::DisconnectInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-17 11:12:26 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_QueryReferent(mWeakLoadGroup);
|
2014-10-10 20:58:05 +04:00
|
|
|
if (loadGroup) {
|
|
|
|
loadGroup->RemoveRequest(this, nullptr, NS_OK);
|
2014-11-12 01:47:13 +03:00
|
|
|
// mWeakLoadGroup has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mWeakLoadGroup = nullptr;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 21:09:26 +03:00
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
|
|
|
|
os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
|
|
|
|
}
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl::nsIWebSocketListener methods:
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-12-20 12:20:12 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::DoOnMessageAvailable(const nsACString& aMsg, bool isBinary)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2012-06-05 03:38:32 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState == WebSocket::CLOSED) {
|
2012-06-05 03:38:32 +04:00
|
|
|
NS_ERROR("Received message after CLOSED");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2011-12-20 12:20:12 +04:00
|
|
|
// Dispatch New Message
|
2014-10-10 20:58:05 +04:00
|
|
|
nsresult rv = mWebSocket->CreateAndDispatchMessageEvent(aMsg, isBinary);
|
2011-12-20 12:20:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event");
|
|
|
|
}
|
2015-01-13 22:03:56 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 22:36:01 +04:00
|
|
|
}
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
// CLOSING should be the only other state where it's possible to get msgs
|
|
|
|
// from channel: Spec says to drop them.
|
|
|
|
MOZ_ASSERT(readyState == WebSocket::CLOSING,
|
|
|
|
"Received message while CONNECTING or CLOSED");
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2011-12-20 12:20:12 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-12-20 12:20:12 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 22:37:07 +03:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 12:20:12 +04:00
|
|
|
return DoOnMessageAvailable(aMsg, false);
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnBinaryMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-05-22 05:27:52 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 22:37:07 +03:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 12:20:12 +04:00
|
|
|
return DoOnMessageAvailable(aMsg, true);
|
2011-05-22 05:27:52 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnStart(nsISupports* aContext)
|
2011-05-22 05:27:52 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2012-06-05 03:38:32 +04:00
|
|
|
|
|
|
|
// This is the only function that sets OPEN, and should be called only once
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::OPEN,
|
2012-06-05 03:38:32 +04:00
|
|
|
"readyState already OPEN! OnStart called twice?");
|
|
|
|
|
|
|
|
// Nothing to do if we've already closed/closing
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState != WebSocket::CONNECTING) {
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_OK;
|
2012-06-05 03:38:32 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-02-22 02:57:10 +04:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 20:58:05 +04:00
|
|
|
nsresult rv = mWebSocket->CheckInnerWindowCorrectness();
|
2012-02-22 02:57:10 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-05 03:38:32 +04:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-22 02:57:10 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-12-16 03:19:01 +04:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-10-14 13:28:07 +04:00
|
|
|
mChannel->GetProtocol(mWebSocket->mEstablishedProtocol);
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
mChannel->GetExtensions(mWebSocket->mEstablishedExtensions);
|
2011-12-16 03:23:00 +04:00
|
|
|
UpdateURI();
|
2011-08-04 07:46:13 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
mWebSocket->SetReadyState(WebSocket::OPEN);
|
2012-06-05 03:38:32 +04:00
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
// Let's keep the object alive because the webSocket can be CCed in the
|
|
|
|
// onopen callback.
|
|
|
|
nsRefPtr<WebSocket> webSocket = mWebSocket;
|
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// Call 'onopen'
|
2015-01-13 22:03:56 +03:00
|
|
|
rv = webSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("open"));
|
2012-06-05 03:38:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event");
|
|
|
|
}
|
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
webSocket->UpdateMustKeepAlive();
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnStop(nsISupports* aContext, nsresult aStatusCode)
|
2012-06-05 03:38:32 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-05 03:38:32 +04: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 13:28:07 +04:00
|
|
|
MOZ_ASSERT(mWebSocket->ReadyState() != WebSocket::CLOSED,
|
2012-06-05 03:38:32 +04: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 20:56:43 +04:00
|
|
|
WebSocketImpl::ScheduleConnectionCloseEvents(nsISupports* aContext,
|
|
|
|
nsresult aStatusCode,
|
|
|
|
bool sync)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// no-op if some other code has already initiated close event
|
|
|
|
if (!mOnCloseScheduled) {
|
|
|
|
mCloseEventWasClean = NS_SUCCEEDED(aStatusCode);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04: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 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
if (NS_FAILED(aStatusCode)) {
|
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
mOnCloseScheduled = true;
|
|
|
|
|
|
|
|
if (sync) {
|
|
|
|
DispatchConnectionCloseEvents();
|
|
|
|
} else {
|
2014-05-23 23:53:17 +04:00
|
|
|
NS_DispatchToCurrentThread(new CallDispatchConnectionCloseEvents(this));
|
2012-06-05 03:38:32 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnAcknowledge(nsISupports *aContext, uint32_t aSize)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
if (aSize > mWebSocket->mOutgoingBufferedAmount) {
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
mWebSocket->mOutgoingBufferedAmount -= aSize;
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::OnServerClose(nsISupports *aContext, uint16_t aCode,
|
|
|
|
const nsACString &aReason)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(readyState != WebSocket::CONNECTING,
|
2012-06-05 03:38:32 +04:00
|
|
|
"Received server close before connected?");
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::CLOSED,
|
2012-06-05 03:38:32 +04:00
|
|
|
"Received server close after already closed!");
|
2012-02-22 02:57:13 +04:00
|
|
|
|
2012-02-01 08:41:43 +04:00
|
|
|
// store code/string for onclose DOM event
|
|
|
|
mCloseEventCode = aCode;
|
|
|
|
CopyUTF8toUTF16(aReason, mCloseEventReason);
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2012-06-05 03:38:32 +04:00
|
|
|
// Server initiating close.
|
2012-05-17 04:09:42 +04: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-22 02:57:13 +04:00
|
|
|
} else {
|
2012-06-05 03:38:32 +04:00
|
|
|
// We initiated close, and server has replied: OnStop does rest of the work.
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CLOSING, "unknown state");
|
2012-02-22 02:57:13 +04:00
|
|
|
}
|
2012-02-01 08:41:43 +04:00
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl::nsIInterfaceRequestor
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::GetInterface(const nsIID& aIID, void** aResult)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
if (!mWebSocket || mWebSocket->ReadyState() == WebSocket::CLOSED) {
|
2011-06-08 16:27:11 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
2011-06-08 16:27:11 +04:00
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) {
|
|
|
|
nsresult rv;
|
2014-10-10 20:58:05 +04:00
|
|
|
nsIScriptContext* sc = mWebSocket->GetContextForEventHandlers(&rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc =
|
2012-03-13 04:56:07 +04:00
|
|
|
nsContentUtils::GetDocumentFromScriptContext(sc);
|
2014-10-10 20:58:05 +04:00
|
|
|
if (!doc) {
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04: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-22 02:57:10 +04:00
|
|
|
return QueryInterface(aIID, aResult);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-09-10 17:48:14 +04:00
|
|
|
// WebSocket
|
2010-06-17 22:34:24 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-07 06:53:23 +04:00
|
|
|
WebSocket::WebSocket(nsPIDOMWindow* aOwnerWindow)
|
2014-10-10 20:58:05 +04:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow)
|
2014-12-12 22:37:07 +03:00
|
|
|
, mIsMainThread(true)
|
2014-10-10 20:58:05 +04:00
|
|
|
, mKeepingAlive(false)
|
|
|
|
, mCheckMustKeepAlive(true)
|
2014-10-14 13:28:07 +04:00
|
|
|
, mOutgoingBufferedAmount(0)
|
|
|
|
, mBinaryType(dom::BinaryType::Blob)
|
2015-02-17 20:40:09 +03:00
|
|
|
, mMutex("WebSocket::mMutex")
|
2014-10-14 13:28:07 +04:00
|
|
|
, mReadyState(CONNECTING)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:56:43 +04:00
|
|
|
mImpl = new WebSocketImpl(this);
|
2014-12-12 22:37:07 +03:00
|
|
|
mIsMainThread = mImpl->mIsMainThread;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::~WebSocket()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
WebSocket::WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto)
|
2012-09-10 17:48:14 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return WebSocketBinding::Wrap(cx, this, aGivenProto);
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// WebIDL
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Constructor:
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 20:07:49 +04:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
2013-08-30 21:14:19 +04:00
|
|
|
return WebSocket::Constructor(aGlobal, aUrl, protocols, aRv);
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 20:07:49 +04:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const nsAString& aProtocol,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!protocols.AppendElement(aProtocol, fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-30 21:14:19 +04:00
|
|
|
return WebSocket::Constructor(aGlobal, aUrl, protocols, aRv);
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04: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;
|
|
|
|
};
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
class WebSocketMainThreadRunnable : public WorkerMainThreadRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-05-15 01:43:06 +03:00
|
|
|
WebSocketMainThreadRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
2015-05-15 01:43:06 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* window = wp->GetWindow();
|
2014-10-29 19:54:35 +03:00
|
|
|
if (window) {
|
|
|
|
return InitWithWindow(window);
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
return InitWindowless(wp);
|
2014-10-29 19:54:35 +03:00
|
|
|
}
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
protected:
|
|
|
|
virtual bool InitWithWindow(nsPIDOMWindow* aWindow) = 0;
|
|
|
|
|
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InitRunnable final : public WebSocketMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InitRunnable(WebSocketImpl* aImpl, const nsAString& aURL,
|
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile, uint32_t aScriptLine,
|
|
|
|
ErrorResult& aRv, bool* aConnectionFailed)
|
|
|
|
: WebSocketMainThreadRunnable(aImpl->mWorkerPrivate)
|
|
|
|
, mImpl(aImpl)
|
|
|
|
, mURL(aURL)
|
|
|
|
, mProtocolArray(aProtocolArray)
|
|
|
|
, mScriptFile(aScriptFile)
|
|
|
|
, mScriptLine(aScriptLine)
|
|
|
|
, mRv(aRv)
|
|
|
|
, mConnectionFailed(aConnectionFailed)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool InitWithWindow(nsPIDOMWindow* aWindow) override
|
2014-10-29 19:54:35 +03:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AutoJSAPI jsapi;
|
2014-10-29 19:54:35 +03:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(aWindow))) {
|
2014-10-10 20:58:05 +04:00
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearException ce(jsapi.cx());
|
|
|
|
|
2014-10-29 19:54:35 +03:00
|
|
|
nsIDocument* doc = aWindow->GetExtantDoc();
|
2014-10-10 20:58:05 +04: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;
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) override
|
2014-10-29 19:54:35 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-15 01:43:06 +03:00
|
|
|
MOZ_ASSERT(aTopLevelWorkerPrivate && !aTopLevelWorkerPrivate->GetWindow());
|
2014-10-29 19:54:35 +03:00
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
mImpl->Init(nullptr, aTopLevelWorkerPrivate->GetPrincipal(), mURL,
|
|
|
|
mProtocolArray, mScriptFile, mScriptLine, mRv,
|
|
|
|
mConnectionFailed);
|
2014-10-29 19:54:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
const nsAString& mURL;
|
|
|
|
nsTArray<nsString>& mProtocolArray;
|
|
|
|
nsCString mScriptFile;
|
|
|
|
uint32_t mScriptLine;
|
|
|
|
ErrorResult& mRv;
|
|
|
|
bool* mConnectionFailed;
|
|
|
|
};
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
class AsyncOpenRunnable final : public WebSocketMainThreadRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncOpenRunnable(WebSocketImpl* aImpl, ErrorResult& aRv)
|
2015-05-15 01:43:06 +03:00
|
|
|
: WebSocketMainThreadRunnable(aImpl->mWorkerPrivate)
|
2014-10-10 20:58:05 +04:00
|
|
|
, mImpl(aImpl)
|
|
|
|
, mRv(aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:43:06 +03:00
|
|
|
protected:
|
|
|
|
virtual bool InitWithWindow(nsPIDOMWindow* aWindow) override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-05-15 01:43:06 +03:00
|
|
|
|
|
|
|
nsIDocument* doc = aWindow->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->AsyncOpen(principal, mRv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aTopLevelWorkerPrivate && !aTopLevelWorkerPrivate->GetWindow());
|
|
|
|
|
|
|
|
mImpl->AsyncOpen(aTopLevelWorkerPrivate->GetPrincipal(), mRv);
|
2014-10-10 20:58:05 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
ErrorResult& mRv;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 20:07:49 +04:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 17:48:14 +04:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-10 20:56:43 +04: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 17:48:14 +04:00
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
principal = scriptPrincipal->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!sgo) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
ownerWindow = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 17:48:14 +04: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 06:53:23 +04:00
|
|
|
nsRefPtr<WebSocket> webSocket = new WebSocket(ownerWindow);
|
2014-10-10 20:58:05 +04: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 22:37:07 +03: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 20:58:05 +04: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 17:48:14 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:37:07 +03: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 20:58:05 +04: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-11 01:28:35 +04:00
|
|
|
explicit ClearWebSocket(WebSocketImpl* aWebSocketImpl)
|
2014-10-10 20:58:05 +04: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()) {
|
2015-05-15 01:43:06 +03:00
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
webSocket->mImpl->AsyncOpen(principal, aRv);
|
2014-10-10 20:58:05 +04:00
|
|
|
} else {
|
|
|
|
nsRefPtr<AsyncOpenRunnable> runnable =
|
|
|
|
new AsyncOpenRunnable(webSocket->mImpl, aRv);
|
|
|
|
runnable->Dispatch(aGlobal.Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:37:07 +03: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 20:58:05 +04:00
|
|
|
cws.Done();
|
2012-09-10 17:48:14 +04:00
|
|
|
return webSocket.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WebSocket)
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(WebSocket)
|
2012-02-16 20:45:25 +04:00
|
|
|
bool isBlack = tmp->IsBlack();
|
2014-10-10 20:56:43 +04:00
|
|
|
if (isBlack || tmp->mKeepingAlive) {
|
2012-01-26 19:55:30 +04:00
|
|
|
if (tmp->mListenerManager) {
|
2012-10-17 05:22:02 +04:00
|
|
|
tmp->mListenerManager->MarkForCC();
|
2012-01-26 19:55:30 +04:00
|
|
|
}
|
2012-03-14 18:22:10 +04:00
|
|
|
if (!isBlack && tmp->PreservingWrapper()) {
|
2013-09-09 07:28:48 +04:00
|
|
|
// This marks the wrapper black.
|
|
|
|
tmp->GetWrapper();
|
2012-02-16 20:45:25 +04:00
|
|
|
}
|
2012-01-26 19:55:30 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(WebSocket)
|
2012-01-26 19:55:30 +04:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(WebSocket)
|
2012-01-26 19:55:30 +04:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-01-26 19:55:30 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 20:56:43 +04:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImpl->mChannel)
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WebSocket,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 20:56:43 +04:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mImpl->mChannel)
|
|
|
|
tmp->mImpl->Disconnect();
|
|
|
|
MOZ_ASSERT(!tmp->mImpl);
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WebSocket)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WebSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WebSocket, DOMEventTargetHelper)
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-03-13 04:56:07 +04:00
|
|
|
void
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::DisconnectFromOwner()
|
2012-03-13 04:56:07 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-10-10 20:56:43 +04:00
|
|
|
|
|
|
|
if (mImpl) {
|
|
|
|
mImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
}
|
|
|
|
|
2012-03-13 04:56:07 +04:00
|
|
|
DontKeepAliveAnyMore();
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl:: initialization
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
void
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Init(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aURL,
|
2014-10-10 20:58:05 +04:00
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile,
|
|
|
|
uint32_t aScriptLine,
|
|
|
|
ErrorResult& aRv,
|
|
|
|
bool* aConnectionFailed)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2012-09-10 17:48:14 +04:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-10 20:56:43 +04: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 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 20:58:05 +04:00
|
|
|
aRv = mWebSocket->CheckInnerWindowCorrectness();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// Shut down websocket if window is frozen or destroyed (only needed for
|
|
|
|
// "ghost" websockets--see bug 696085)
|
2014-10-10 20:58:05 +04:00
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!os)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-03 11:24:17 +03:00
|
|
|
|
2014-10-10 20:58:05 +04: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 19:54:35 +03:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
unsigned lineno;
|
|
|
|
JS::AutoFilename file;
|
|
|
|
if (JS::DescribeScriptedCaller(aCx, &file, &lineno)) {
|
|
|
|
mScriptFile = file.get();
|
|
|
|
mScriptLine = lineno;
|
|
|
|
}
|
2010-12-03 11:24:17 +03:00
|
|
|
}
|
|
|
|
|
2014-10-29 19:54:35 +03: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 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// parses the url
|
2014-10-10 20:58:05 +04:00
|
|
|
aRv = ParseURL(PromiseFlatString(aURL));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
nsIScriptContext* sc = nullptr;
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
sc = mWebSocket->GetContextForEventHandlers(&rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2015-07-10 19:15:30 +03: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_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check content policy.
|
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
nsCOMPtr<nsIDocument> originDoc = nsContentUtils::GetDocumentFromScriptContext(sc);
|
|
|
|
mOriginDocument = do_GetWeakReference(originDoc);
|
|
|
|
aRv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_WEBSOCKET,
|
|
|
|
uri,
|
|
|
|
aPrincipal,
|
|
|
|
originDoc,
|
|
|
|
EmptyCString(),
|
|
|
|
nullptr,
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
// Disallowed by content policy.
|
|
|
|
aRv.Throw(NS_ERROR_CONTENT_BLOCKED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Potentially the page uses the CSP directive 'upgrade-insecure-requests'.
|
|
|
|
// In such a case we have to upgrade ws: to wss: and also update mSecure
|
|
|
|
// to reflect that upgrade. Please note that we can not upgrade from ws:
|
|
|
|
// to wss: before performing content policy checks because CSP needs to
|
|
|
|
// send reports in case the scheme is about to be upgraded.
|
|
|
|
if (!mSecure && originDoc && originDoc->GetUpgradeInsecureRequests()) {
|
|
|
|
// let's use the old specification before the upgrade for logging
|
|
|
|
NS_ConvertUTF8toUTF16 reportSpec(mURI);
|
|
|
|
|
|
|
|
// upgrade the request from ws:// to wss:// and mark as secure
|
|
|
|
mURI.ReplaceSubstring("ws://", "wss://");
|
|
|
|
if (NS_WARN_IF(mURI.Find("wss://") != 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSecure = true;
|
|
|
|
|
|
|
|
const char16_t* params[] = { reportSpec.get(), NS_LITERAL_STRING("wss").get() };
|
|
|
|
CSP_LogLocalizedStr(NS_LITERAL_STRING("upgradeInsecureRequest").get(),
|
|
|
|
params, ArrayLength(params),
|
|
|
|
EmptyString(), // aSourceFile
|
|
|
|
EmptyString(), // aScriptSample
|
|
|
|
0, // aLineNumber
|
|
|
|
0, // aColumnNumber
|
|
|
|
nsIScriptError::warningFlag, "CSP",
|
|
|
|
mInnerWindowID);
|
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04: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 23:04:28 +04:00
|
|
|
// secure context (e.g. https). Check the principal's uri to determine if
|
|
|
|
// we were loaded from https.
|
2014-08-19 23:02:05 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject(GetEntryGlobal());
|
2014-07-18 23:04:28 +04: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 20:58:05 +04:00
|
|
|
aRv = uri->SchemeIs("https", &originIsHttps);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-18 23:04:28 +04:00
|
|
|
if (originIsHttps) {
|
2014-10-10 20:58:05 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2014-07-18 23:04:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 20:42:02 +04:00
|
|
|
|
2012-09-10 17:48:14 +04: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 19:02:17 +04:00
|
|
|
if (aProtocolArray[index][i] < static_cast<char16_t>(0x0021) ||
|
2014-10-10 20:58:05 +04:00
|
|
|
aProtocolArray[index][i] > static_cast<char16_t>(0x007E)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-03 20:42:02 +04:00
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
|
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-05-22 07:48:50 +04:00
|
|
|
mRequestedProtocolList.AppendLiteral(", ");
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF16toUTF8(aProtocolArray[index], mRequestedProtocolList);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the constructor should throw a SYNTAX_ERROR only if it fails to parse the
|
2014-10-10 20:58:05 +04:00
|
|
|
// url parameter, so don't throw if InitializeConnection fails, and call
|
2012-09-10 17:48:14 +04:00
|
|
|
// onerror/onclose asynchronously
|
2015-05-15 01:43:06 +03:00
|
|
|
if (NS_FAILED(InitializeConnection(aPrincipal))) {
|
2014-10-10 20:58:05 +04:00
|
|
|
*aConnectionFailed = true;
|
|
|
|
} else {
|
|
|
|
*aConnectionFailed = false;
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
void
|
2015-05-15 01:43:06 +03:00
|
|
|
WebSocketImpl::AsyncOpen(nsIPrincipal* aPrincipal, ErrorResult& aRv)
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2014-10-10 20:58:05 +04:00
|
|
|
|
|
|
|
nsCString asciiOrigin;
|
2015-05-15 01:43:06 +03:00
|
|
|
aRv = nsContentUtils::GetASCIIOrigin(aPrincipal, asciiOrigin);
|
2014-10-10 20:58:05 +04:00
|
|
|
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 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl methods:
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsAutoCloseWS final
|
2011-10-20 12:27:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 20:56:43 +04:00
|
|
|
explicit nsAutoCloseWS(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2011-10-20 12:27:13 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
~nsAutoCloseWS()
|
|
|
|
{
|
2014-10-10 20:56:43 +04:00
|
|
|
if (!mWebSocketImpl->mChannel) {
|
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_INTERNAL_ERROR);
|
2011-10-20 12:27:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2014-10-10 20:56:43 +04:00
|
|
|
nsRefPtr<WebSocketImpl> mWebSocketImpl;
|
2011-10-20 12:27:13 +04:00
|
|
|
};
|
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
nsresult
|
2015-05-15 01:43:06 +03:00
|
|
|
WebSocketImpl::InitializeConnection(nsIPrincipal* aPrincipal)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mChannel, "mChannel should be null");
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-10-20 12:27:13 +04:00
|
|
|
nsCOMPtr<nsIWebSocketChannel> wsChannel;
|
|
|
|
nsAutoCloseWS autoClose(this);
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv;
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-10-20 12:27:13 +04: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 18:20:58 +04:00
|
|
|
rv = loadGroup->AddRequest(this, nullptr);
|
2011-10-20 12:27:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-17 11:12:26 +04:00
|
|
|
|
|
|
|
mWeakLoadGroup = do_GetWeakReference(loadGroup);
|
2011-10-20 12:27:13 +04:00
|
|
|
}
|
|
|
|
|
2014-08-14 00:26:17 +04:00
|
|
|
// manually adding loadinfo to the channel since it
|
|
|
|
// was not set during channel creation.
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mOriginDocument);
|
2014-11-12 01:47:13 +03:00
|
|
|
|
|
|
|
// mOriginDocument has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mOriginDocument = nullptr;
|
|
|
|
|
2015-02-17 21:09:58 +03:00
|
|
|
wsChannel->InitLoadInfo(doc ? doc->AsDOMNode() : nullptr,
|
2015-05-15 01:43:06 +03:00
|
|
|
doc ? doc->NodePrincipal() : aPrincipal,
|
|
|
|
aPrincipal,
|
2015-02-17 21:09:58 +03:00
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_WEBSOCKET);
|
2014-08-14 00:26:17 +04:00
|
|
|
|
2011-10-20 12:27:13 +04:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
|
|
|
rv = wsChannel->SetProtocol(mRequestedProtocolList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-03 03:55:17 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(wsChannel);
|
|
|
|
NS_ENSURE_TRUE(rr, NS_ERROR_FAILURE);
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
rv = rr->RetargetDeliveryTo(this);
|
2011-10-20 12:27:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-16 03:19:01 +04:00
|
|
|
mChannel = wsChannel;
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
void
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::DispatchConnectionCloseEvents()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 22:37:07 +03:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSED);
|
2012-06-05 03:38:32 +04:00
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
// Let's keep the object alive because the webSocket can be CCed in the
|
|
|
|
// onerror or in the onclose callback.
|
|
|
|
nsRefPtr<WebSocket> webSocket = mWebSocket;
|
|
|
|
|
2012-06-05 03:38:32 +04:00
|
|
|
// Call 'onerror' if needed
|
|
|
|
if (mFailed) {
|
2014-10-10 20:56:43 +04:00
|
|
|
nsresult rv =
|
2015-01-13 22:03:56 +03:00
|
|
|
webSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
2012-06-05 03:38:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event");
|
|
|
|
}
|
2010-06-17 22:36:01 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
nsresult rv = webSocket->CreateAndDispatchCloseEvent(mCloseEventWasClean,
|
|
|
|
mCloseEventCode,
|
|
|
|
mCloseEventReason);
|
2012-06-05 03:38:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the close event");
|
|
|
|
}
|
|
|
|
|
2015-01-13 22:03:56 +03:00
|
|
|
webSocket->UpdateMustKeepAlive();
|
2012-06-05 03:38:32 +04:00
|
|
|
Disconnect();
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2010-06-17 22:36:01 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocket::CreateAndDispatchSimpleEvent(const nsAString& aName)
|
2010-06-17 22:36:01 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 22:36:01 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
nsRefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = event->InitEvent(aName, false, false);
|
2010-06-17 22:36:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-22 12:18:08 +04:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 22:36:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::CreateAndDispatchMessageEvent(const nsACString& aData,
|
2014-10-10 20:58:05 +04:00
|
|
|
bool aIsBinary)
|
2010-06-17 22:36:01 +04:00
|
|
|
{
|
2014-10-10 20:56:43 +04:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2014-10-10 20:58:19 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 20:58:19 +04:00
|
|
|
} else {
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(!mIsMainThread);
|
|
|
|
MOZ_ASSERT(mImpl->mWorkerPrivate);
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mImpl->mWorkerPrivate->GlobalScope()))) {
|
2014-10-10 20:58:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:19 +04:00
|
|
|
return CreateAndDispatchMessageEvent(jsapi.cx(), aData, aIsBinary);
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WebSocket::CreateAndDispatchMessageEvent(JSContext* aCx,
|
|
|
|
const nsACString& aData,
|
|
|
|
bool aIsBinary)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 12:27:13 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2014-10-10 20:58:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 22:36:01 +04:00
|
|
|
return NS_OK;
|
2014-05-23 11:47:46 +04:00
|
|
|
}
|
2011-05-23 22:53:31 +04:00
|
|
|
|
2011-12-16 03:19:01 +04:00
|
|
|
// Create appropriate JS object for message
|
2014-10-10 20:58:05 +04:00
|
|
|
JS::Rooted<JS::Value> jsData(aCx);
|
|
|
|
if (aIsBinary) {
|
2014-10-14 13:28:07 +04:00
|
|
|
if (mBinaryType == dom::BinaryType::Blob) {
|
2014-10-10 20:58:05 +04:00
|
|
|
nsresult rv = nsContentUtils::CreateBlobBuffer(aCx, GetOwner(), aData,
|
|
|
|
&jsData);
|
2013-05-22 20:05:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-14 13:28:07 +04:00
|
|
|
} else if (mBinaryType == dom::BinaryType::Arraybuffer) {
|
2014-10-10 20:58:05 +04:00
|
|
|
JS::Rooted<JSObject*> arrayBuf(aCx);
|
|
|
|
nsresult rv = nsContentUtils::CreateArrayBuffer(aCx, aData,
|
|
|
|
arrayBuf.address());
|
2013-05-22 20:05:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-07-01 07:09:46 +03:00
|
|
|
jsData.setObject(*arrayBuf);
|
2011-12-16 03:19:01 +04:00
|
|
|
} else {
|
2013-05-22 20:05:28 +04:00
|
|
|
NS_RUNTIMEABORT("Unknown binary type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
2013-05-22 20:05:28 +04:00
|
|
|
} else {
|
|
|
|
// JS string
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
JSString* jsString;
|
2014-10-10 20:58:05 +04:00
|
|
|
jsString = JS_NewUCStringCopyN(aCx, utf16Data.get(), utf16Data.Length());
|
2013-05-22 20:05:28 +04:00
|
|
|
NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
|
|
|
|
|
2015-06-30 21:20:56 +03:00
|
|
|
jsData.setString(jsString);
|
2011-05-23 22:53:31 +04:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:36:01 +04:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
nsRefPtr<MessageEvent> event = NS_NewDOMMessageEvent(this, nullptr, nullptr);
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
rv = event->InitMessageEvent(NS_LITERAL_STRING("message"), false, false,
|
|
|
|
jsData, mImpl->mUTF16Origin, EmptyString(),
|
|
|
|
nullptr);
|
2010-06-17 22:36:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-12-22 12:18:08 +04:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
return DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr,
|
|
|
|
nullptr);
|
2010-06-17 22:36:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::CreateAndDispatchCloseEvent(bool aWasClean,
|
|
|
|
uint16_t aCode,
|
2014-10-10 20:56:43 +04:00
|
|
|
const nsAString &aReason)
|
2010-06-17 22:36:01 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 22:36:01 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-01 03:00:31 +04:00
|
|
|
CloseEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mWasClean = aWasClean;
|
|
|
|
init.mCode = aCode;
|
|
|
|
init.mReason = aReason;
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2014-07-01 03:00:31 +04:00
|
|
|
nsRefPtr<CloseEvent> event =
|
|
|
|
CloseEvent::Constructor(this, NS_LITERAL_STRING("close"), init);
|
2012-12-22 12:18:08 +04:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 22:36:01 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 22:36:01 +04:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::ParseURL(const nsAString& aURL)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_ENSURE_TRUE(!aURL.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsCOMPtr<nsIURL> parsedURL = do_QueryInterface(uri, &rv);
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2015-03-31 13:39:03 +03:00
|
|
|
bool hasRef;
|
|
|
|
rv = parsedURL->GetHasRef(&hasRef);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !hasRef,
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2010-06-17 22:34:24 +04:00
|
|
|
rv = parsedURL->GetScheme(scheme);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !scheme.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString host;
|
2010-06-17 22:34:24 +04:00
|
|
|
rv = parsedURL->GetAsciiHost(host);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !host.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2010-06-17 22:34:24 +04:00
|
|
|
rv = parsedURL->GetPort(&port);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
rv = NS_CheckPortSafety(port, scheme.get());
|
2015-03-31 13:39:43 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString filePath;
|
2010-06-17 22:34:24 +04:00
|
|
|
rv = parsedURL->GetFilePath(filePath);
|
|
|
|
if (filePath.IsEmpty()) {
|
2014-05-26 22:55:08 +04:00
|
|
|
filePath.Assign('/');
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString query;
|
2010-06-17 22:34:24 +04:00
|
|
|
rv = parsedURL->GetQuery(query);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("ws")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSecure = false;
|
2010-06-17 22:34:24 +04:00
|
|
|
mPort = (port == -1) ? DEFAULT_WS_SCHEME_PORT : port;
|
|
|
|
} else if (scheme.LowerCaseEqualsLiteral("wss")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSecure = true;
|
2010-06-17 22:34:24 +04:00
|
|
|
mPort = (port == -1) ? DEFAULT_WSS_SCHEME_PORT : port;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2011-08-04 04:32:23 +04:00
|
|
|
rv = nsContentUtils::GetUTFOrigin(parsedURL, mUTF16Origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
mAsciiHost = host;
|
|
|
|
ToLowerCase(mAsciiHost);
|
|
|
|
|
|
|
|
mResource = filePath;
|
|
|
|
if (!query.IsEmpty()) {
|
2014-05-22 07:48:51 +04:00
|
|
|
mResource.Append('?');
|
2010-06-17 22:34:24 +04:00
|
|
|
mResource.Append(query);
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length = mResource.Length();
|
|
|
|
uint32_t i;
|
2010-06-17 22:34:24 +04:00
|
|
|
for (i = 0; i < length; ++i) {
|
2014-01-04 19:02:17 +04:00
|
|
|
if (mResource[i] < static_cast<char16_t>(0x0021) ||
|
|
|
|
mResource[i] > static_cast<char16_t>(0x007E)) {
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
rv = parsedURL->GetSpec(mURI);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2015-03-31 13:39:35 +03:00
|
|
|
CopyUTF8toUTF16(mURI, mWebSocket->mURI);
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-28 01:09:29 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-07-21 20:05:56 +04: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-28 01:09:29 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::UpdateMustKeepAlive()
|
2010-06-28 01:09:29 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
if (!mCheckMustKeepAlive || !mImpl) {
|
2010-06-28 01:09:29 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool shouldKeepAlive = false;
|
2014-11-12 01:47:13 +03:00
|
|
|
uint16_t readyState = ReadyState();
|
2010-07-21 20:05:56 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mListenerManager) {
|
2014-11-12 01:47:13 +03:00
|
|
|
switch (readyState)
|
2010-07-21 20:05:56 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
case CONNECTING:
|
2010-07-21 20:05:56 +04:00
|
|
|
{
|
2012-10-29 08:33:51 +04:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onopen) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 20:05:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
case OPEN:
|
|
|
|
case CLOSING:
|
2010-07-21 20:05:56 +04:00
|
|
|
{
|
2012-10-29 08:33:51 +04:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose) ||
|
2014-10-14 13:28:07 +04:00
|
|
|
mOutgoingBufferedAmount != 0) {
|
2011-10-17 18:59:28 +04:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 20:05:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
case CLOSED:
|
2010-07-21 20:05:56 +04:00
|
|
|
{
|
2012-06-05 03:38:32 +04:00
|
|
|
shouldKeepAlive = false;
|
2010-07-21 20:05:56 +04:00
|
|
|
}
|
2010-06-28 01:09:29 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 20:05:56 +04:00
|
|
|
|
|
|
|
if (mKeepingAlive && !shouldKeepAlive) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 20:58:05 +04:00
|
|
|
mImpl->ReleaseObject();
|
2010-07-21 20:05:56 +04:00
|
|
|
} else if (!mKeepingAlive && shouldKeepAlive) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepingAlive = true;
|
2014-10-10 20:58:05 +04:00
|
|
|
mImpl->AddRefObject();
|
2010-07-21 20:05:56 +04:00
|
|
|
}
|
2010-06-28 01:09:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-10 17:48:14 +04:00
|
|
|
WebSocket::DontKeepAliveAnyMore()
|
2010-06-28 01:09:29 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2010-07-21 20:05:56 +04:00
|
|
|
if (mKeepingAlive) {
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 20:58:05 +04:00
|
|
|
mImpl->ReleaseObject();
|
2010-06-28 01:09:29 +04:00
|
|
|
}
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mCheckMustKeepAlive = false;
|
2010-06-28 01:09:29 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WebSocketWorkerFeature final : public WorkerFeature
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit WebSocketWorkerFeature(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool Notify(JSContext* aCx, Status aStatus) override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
|
|
|
|
|
|
|
if (aStatus >= Canceling) {
|
2015-02-17 20:40:09 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mWebSocketImpl->mMutex);
|
|
|
|
mWebSocketImpl->mWorkerShuttingDown = true;
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:38:02 +03:00
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY,
|
2015-04-07 20:31:51 +03:00
|
|
|
EmptyCString());
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool Suspend(JSContext* aCx) override
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
2015-02-17 20:40:09 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mWebSocketImpl->mMutex);
|
|
|
|
mWebSocketImpl->mWorkerShuttingDown = true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mWebSocketImpl;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::AddRefObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::ReleaseObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
bool
|
2014-11-12 01:47:13 +03: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 22:37:07 +03:00
|
|
|
return false;
|
2014-11-12 01:47:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
SetHasFeatureRegistered(true);
|
|
|
|
#endif
|
2014-12-12 22:37:07 +03:00
|
|
|
|
|
|
|
return true;
|
2014-11-12 01:47:13 +03:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
void
|
|
|
|
WebSocketImpl::UnregisterFeature()
|
|
|
|
{
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(mDisconnectingOrDisconnected);
|
2014-10-10 20:58:05 +04:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerFeature);
|
|
|
|
|
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
mWorkerPrivate->RemoveFeature(cx, mWorkerFeature);
|
|
|
|
mWorkerFeature = nullptr;
|
2014-12-12 22:37:07 +03:00
|
|
|
mWorkerPrivate = nullptr;
|
2014-11-12 01:47:13 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
SetHasFeatureRegistered(false);
|
|
|
|
#endif
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2011-12-16 03:23:00 +04:00
|
|
|
nsresult
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::UpdateURI()
|
2011-12-16 03:23:00 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-12-16 03:23:00 +04:00
|
|
|
// Check for Redirections
|
2014-04-17 22:53:58 +04:00
|
|
|
nsRefPtr<BaseWebSocketChannel> channel;
|
|
|
|
channel = static_cast<BaseWebSocketChannel*>(mChannel.get());
|
|
|
|
MOZ_ASSERT(channel);
|
2011-12-16 03:23:00 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
channel->GetEffectiveURL(mWebSocket->mEffectiveURL);
|
2014-04-17 22:53:58 +04:00
|
|
|
mSecure = channel->IsEncrypted();
|
2011-12-16 03:23:00 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:16:08 +04:00
|
|
|
void
|
2013-05-23 15:41:32 +04:00
|
|
|
WebSocket::EventListenerAdded(nsIAtom* aType)
|
2013-04-17 01:16:08 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 15:41:32 +04:00
|
|
|
UpdateMustKeepAlive();
|
2010-06-28 01:09:29 +04:00
|
|
|
}
|
|
|
|
|
2013-04-17 01:16:08 +04:00
|
|
|
void
|
2013-05-23 15:41:32 +04:00
|
|
|
WebSocket::EventListenerRemoved(nsIAtom* aType)
|
2013-04-17 01:16:08 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 15:41:32 +04:00
|
|
|
UpdateMustKeepAlive();
|
2013-04-17 01:16:08 +04:00
|
|
|
}
|
2013-05-23 15:41:32 +04:00
|
|
|
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-09-10 17:48:14 +04:00
|
|
|
// WebSocket - methods
|
2010-06-17 22:34:24 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 20:56:43 +04:00
|
|
|
// webIDL: readonly attribute unsigned short readyState;
|
|
|
|
uint16_t
|
2014-10-14 13:28:07 +04:00
|
|
|
WebSocket::ReadyState()
|
2014-10-10 20:56:43 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocket::SetReadyState(uint16_t aReadyState)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mReadyState = aReadyState;
|
2014-10-10 20:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: readonly attribute unsigned long bufferedAmount;
|
|
|
|
uint32_t
|
|
|
|
WebSocket::BufferedAmount() const
|
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mOutgoingBufferedAmount;
|
2014-10-10 20:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
dom::BinaryType
|
|
|
|
WebSocket::BinaryType() const
|
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mBinaryType;
|
2014-10-10 20:56:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
void
|
|
|
|
WebSocket::SetBinaryType(dom::BinaryType aData)
|
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
mBinaryType = aData;
|
2014-10-10 20:56:43 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// webIDL: readonly attribute DOMString url
|
|
|
|
void
|
|
|
|
WebSocket::GetUrl(nsAString& aURL)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
if (mEffectiveURL.IsEmpty()) {
|
2015-03-31 13:39:35 +03:00
|
|
|
aURL = mURI;
|
2014-10-14 13:28:07 +04:00
|
|
|
} else {
|
|
|
|
aURL = mEffectiveURL;
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// webIDL: readonly attribute DOMString extensions;
|
|
|
|
void
|
|
|
|
WebSocket::GetExtensions(nsAString& aExtensions)
|
2011-08-04 07:46:13 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedExtensions, aExtensions);
|
2011-08-04 07:46:13 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// webIDL: readonly attribute DOMString protocol;
|
|
|
|
void
|
|
|
|
WebSocket::GetProtocol(nsAString& aProtocol)
|
2011-05-22 05:27:52 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedProtocol, aProtocol);
|
2011-05-22 05:27:52 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// webIDL: void send(DOMString data);
|
|
|
|
void
|
|
|
|
WebSocket::Send(const nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
NS_ConvertUTF16toUTF8 msgString(aData);
|
2014-10-14 13:28:07 +04:00
|
|
|
Send(nullptr, msgString, msgString.Length(), false, aRv);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
void
|
2015-05-12 15:09:51 +03:00
|
|
|
WebSocket::Send(Blob& aData, ErrorResult& aRv)
|
2011-12-16 03:19:01 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsCOMPtr<nsIInputStream> msgStream;
|
2015-05-19 17:36:37 +03:00
|
|
|
aData.GetInternalStream(getter_AddRefs(msgStream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())){
|
2012-09-10 17:48:14 +04:00
|
|
|
return;
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
uint64_t msgLength = aData.GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())){
|
2012-09-10 17:48:14 +04:00
|
|
|
return;
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (msgLength > UINT32_MAX) {
|
2012-09-10 17:48:14 +04:00
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
Send(msgStream, EmptyCString(), msgLength, true, aRv);
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
WebSocket::Send(const ArrayBuffer& aData,
|
2012-09-10 17:48:14 +04:00
|
|
|
ErrorResult& aRv)
|
2011-08-03 23:15:25 +04:00
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 23:15:25 +04: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-28 01:32:41 +04:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 13:28:07 +04:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2012-09-10 17:48:14 +04:00
|
|
|
}
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
WebSocket::Send(const ArrayBufferView& aData,
|
2012-09-10 17:48:14 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-14 13:28:07 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-12-16 03:19:01 +04: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-28 01:32:41 +04:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 13:28:07 +04:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
void
|
2014-10-14 13:28:07 +04:00
|
|
|
WebSocket::Send(nsIInputStream* aMsgStream,
|
|
|
|
const nsACString& aMsgString,
|
|
|
|
uint32_t aMsgLength,
|
|
|
|
bool aIsBinary,
|
|
|
|
ErrorResult& aRv)
|
2011-12-16 03:19:01 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 13:28:07 +04:00
|
|
|
if (readyState == CONNECTING) {
|
2012-09-10 17:48:14 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
// Always increment outgoing buffer len, even if closed
|
|
|
|
mOutgoingBufferedAmount += aMsgLength;
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 17:48:14 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
// We must have mImpl when connected.
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
MOZ_ASSERT(readyState == OPEN, "Unknown state in WebSocket::Send");
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (aMsgStream) {
|
2014-10-14 13:28:07 +04:00
|
|
|
rv = mImpl->mChannel->SendBinaryStream(aMsgStream, aMsgLength);
|
2012-09-10 17:48:14 +04:00
|
|
|
} else {
|
|
|
|
if (aIsBinary) {
|
2014-10-14 13:28:07 +04:00
|
|
|
rv = mImpl->mChannel->SendBinaryMsg(aMsgString);
|
2012-09-10 17:48:14 +04:00
|
|
|
} else {
|
2014-10-14 13:28:07 +04:00
|
|
|
rv = mImpl->mChannel->SendMsg(aMsgString);
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-16 03:19:01 +04:00
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
UpdateMustKeepAlive();
|
2011-12-16 03:19:01 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04: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 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 23:15:25 +04:00
|
|
|
|
|
|
|
// the reason code is optional, but if provided it must be in a specific range
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t closeCode = 0;
|
2012-09-10 17:48:14 +04: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 08:41:43 +04:00
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
closeCode = aCode.Value();
|
2011-08-03 23:15:25 +04:00
|
|
|
}
|
|
|
|
|
2012-09-10 17:48:14 +04:00
|
|
|
nsCString closeReason;
|
|
|
|
if (aReason.WasPassed()) {
|
|
|
|
CopyUTF16toUTF8(aReason.Value(), closeReason);
|
2011-08-03 23:15:25 +04:00
|
|
|
|
|
|
|
// The API requires the UTF-8 string to be 123 or less bytes
|
2012-02-01 08:41:43 +04:00
|
|
|
if (closeReason.Length() > 123) {
|
2012-09-10 17:48:14 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2012-02-01 08:41:43 +04:00
|
|
|
}
|
2011-08-03 23:15:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 13:28:07 +04:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 17:48:14 +04:00
|
|
|
return;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04: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 17:48:14 +04:00
|
|
|
return;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
MOZ_ASSERT(readyState == OPEN);
|
|
|
|
mImpl->CloseConnection(closeCode, closeReason);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
2012-02-22 02:57:10 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl::nsIObserver
|
2012-02-22 02:57:10 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2012-02-22 02:57:10 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
if ((readyState == WebSocket::CLOSING) ||
|
|
|
|
(readyState == WebSocket::CLOSED)) {
|
2012-02-22 02:57:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aSubject);
|
2014-10-10 20:58:05 +04:00
|
|
|
if (!mWebSocket->GetOwner() || window != mWebSocket->GetOwner()) {
|
2012-02-22 02:57:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) ||
|
|
|
|
(strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0))
|
|
|
|
{
|
2012-06-05 03:38:32 +04:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-22 02:57:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 20:56:43 +04:00
|
|
|
// WebSocketImpl::nsIRequest
|
2011-05-22 05:27:52 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-06-17 22:34:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::GetName(nsACString& aName)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-31 13:39:35 +03:00
|
|
|
CopyUTF16toUTF8(mWebSocket->mURI, aName);
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::IsPending(bool* aValue)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
*aValue = (readyState != WebSocket::CLOSED);
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::GetStatus(nsresult* aStatus)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
*aStatus = NS_OK;
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CancelRunnable final : public WorkerRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
2011-08-07 17:44:19 +04:00
|
|
|
// Window closed, stop/reload button pressed, user navigated away from page, etc.
|
2010-06-17 22:34:24 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Cancel(nsresult aStatus)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (!mIsMainThread) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2014-10-10 20:58:05 +04: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 22:34:24 +04:00
|
|
|
|
2014-11-07 17:35:54 +03:00
|
|
|
// If CancelInternal is called by a runnable, we may already be disconnected
|
|
|
|
// by the time it runs.
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2014-11-07 17:35:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 20:58:05 +04:00
|
|
|
if (readyState == WebSocket::CLOSING || readyState == WebSocket::CLOSED) {
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_OK;
|
2012-06-05 03:38:32 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
ConsoleError();
|
2012-06-05 03:38:32 +04:00
|
|
|
|
2012-02-01 08:41:43 +04:00
|
|
|
return CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Suspend()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::Resume()
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 22:34:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::GetLoadGroup(nsILoadGroup** aLoadGroup)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aLoadGroup = nullptr;
|
2011-05-22 05:27:52 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mIsMainThread) {
|
2014-10-10 20:58:05 +04: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 05:27:52 +04:00
|
|
|
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-10-10 20:58:05 +04: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 05:27:52 +04:00
|
|
|
if (doc) {
|
2014-03-15 23:00:15 +04:00
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
2011-05-22 05:27:52 +04:00
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
2011-05-22 05:27:52 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 05:27:52 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-06-17 22:34:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::GetLoadFlags(nsLoadFlags* aLoadFlags)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
*aLoadFlags = nsIRequest::LOAD_BACKGROUND;
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 20:56:43 +04:00
|
|
|
WebSocketImpl::SetLoadFlags(nsLoadFlags aLoadFlags)
|
2010-06-17 22:34:24 +04:00
|
|
|
{
|
2014-10-10 20:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 05:27:52 +04:00
|
|
|
// we won't change the load flags at all.
|
2010-06-17 22:34:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-10 17:48:14 +04:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerRunnableDispatcher final : public WorkerRunnable
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
2015-02-17 20:40:09 +03:00
|
|
|
nsRefPtr<WebSocketImpl> mWebSocketImpl;
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
public:
|
2015-02-17 20:40:09 +03:00
|
|
|
WorkerRunnableDispatcher(WebSocketImpl* aImpl, WorkerPrivate* aWorkerPrivate,
|
2015-07-10 06:21:46 +03:00
|
|
|
already_AddRefed<nsIRunnable>&& aEvent)
|
2014-10-10 20:58:05 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2015-02-17 20:40:09 +03:00
|
|
|
, mWebSocketImpl(aImpl)
|
2014-10-10 20:58:05 +04:00
|
|
|
, mEvent(aEvent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2015-03-19 01:38:02 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, true);
|
2015-02-17 20:40:09 +03:00
|
|
|
|
|
|
|
// No messages when disconnected.
|
|
|
|
if (mWebSocketImpl->mDisconnectingOrDisconnected) {
|
|
|
|
NS_WARNING("Dispatching a WebSocket event after the disconnection!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-10-10 20:58:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-10 06:21:46 +03:00
|
|
|
WebSocketImpl::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketImpl::Dispatch(already_AddRefed<nsIRunnable>&& aEvent, uint32_t aFlags)
|
2014-10-10 20:58:05 +04:00
|
|
|
{
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event_ref(aEvent);
|
2014-10-10 20:58:05 +04:00
|
|
|
// If the target is the main-thread we can just dispatch the runnable.
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mIsMainThread) {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_DispatchToMainThread(event_ref.forget());
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 20:40:09 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2014-12-12 22:37:07 +03:00
|
|
|
if (mWorkerShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-11-12 01:47:13 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(HasFeatureRegistered());
|
|
|
|
#endif
|
|
|
|
|
2015-03-19 01:38:02 +03:00
|
|
|
// If the target is a worker, we have to use a custom WorkerRunnableDispatcher
|
|
|
|
// runnable.
|
|
|
|
nsRefPtr<WorkerRunnableDispatcher> event =
|
2015-07-10 06:21:46 +03:00
|
|
|
new WorkerRunnableDispatcher(this, mWorkerPrivate, event_ref.forget());
|
2015-03-19 01:38:02 +03:00
|
|
|
|
2014-10-10 20:58:05 +04:00
|
|
|
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 22:37:07 +03:00
|
|
|
return NS_IsMainThread() == mIsMainThread;
|
2014-10-10 20:58:05 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 13:28:07 +04:00
|
|
|
void
|
|
|
|
WebSocket::AssertIsOnTargetThread() const
|
|
|
|
{
|
2014-12-12 22:37:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-14 13:28:07 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|