2010-06-17 18:34:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 19:32:37 +00:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
#include "WebSocket.h"
|
|
|
|
#include "mozilla/dom/WebSocketBinding.h"
|
2014-04-17 18:53:58 +00:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2011-05-23 16:46:36 +00:00
|
|
|
|
2013-08-27 02:05:20 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "mozilla/net/WebSocketChannel.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-08-12 11:39:31 +00:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2016-01-30 17:05:36 +00:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2016-05-17 10:04:11 +00:00
|
|
|
#include "mozilla/dom/nsCSPService.h"
|
2015-07-10 16:15:30 +00:00
|
|
|
#include "mozilla/dom/nsCSPContext.h"
|
|
|
|
#include "mozilla/dom/nsCSPUtils.h"
|
2014-05-23 07:47:46 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-10-10 16:58:05 +00:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2014-10-10 16:58:19 +00:00
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2016-06-07 20:10:18 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2016-03-11 11:56:05 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2010-06-17 18:34:24 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsContentUtils.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2010-06-17 18:34:24 +00:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIPromptFactory.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIPrompt.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2014-06-30 23:00:31 +00:00
|
|
|
#include "mozilla/dom/CloseEvent.h"
|
2015-10-28 19:11:05 +00:00
|
|
|
#include "mozilla/net/WebSocketEventService.h"
|
2010-06-17 18:36:01 +00:00
|
|
|
#include "nsICryptoHash.h"
|
2010-12-21 17:05:34 +00:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsIScriptError.h"
|
2011-05-22 01:27:52 +00:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
2011-05-22 01:27:52 +00:00
|
|
|
#include "nsILoadGroup.h"
|
2011-05-25 06:31:59 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 16:06:36 +00:00
|
|
|
#include "xpcpublic.h"
|
2011-12-07 23:19:43 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2012-01-26 15:55:30 +00:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-02-21 22:57:10 +00:00
|
|
|
#include "nsIObserverService.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
|
|
#include "nsIWebSocketChannel.h"
|
|
|
|
#include "nsIWebSocketListener.h"
|
2014-10-10 16:58:05 +00:00
|
|
|
#include "nsProxyRelease.h"
|
2014-10-10 16:56:43 +00:00
|
|
|
#include "nsWeakReference.h"
|
2014-04-17 18:53:58 +00:00
|
|
|
|
|
|
|
using namespace mozilla::net;
|
2014-10-10 16:58:05 +00:00
|
|
|
using namespace mozilla::dom::workers;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class WebSocketImpl final : public nsIInterfaceRequestor
|
2015-03-27 18:52:19 +00:00
|
|
|
, public nsIWebSocketListener
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
, public nsIRequest
|
|
|
|
, public nsIEventTarget
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIWEBSOCKETLISTENER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSIREQUEST
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_DECL_NSIEVENTTARGET
|
2016-01-12 04:18:47 +00:00
|
|
|
using nsIEventTarget::Dispatch;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit WebSocketImpl(WebSocket* aWebSocket)
|
2014-10-10 16:58:05 +00:00
|
|
|
: mWebSocket(aWebSocket)
|
2016-06-01 09:35:33 +00:00
|
|
|
, mIsServerSide(false)
|
2014-10-10 16:56:43 +00:00
|
|
|
, mOnCloseScheduled(false)
|
|
|
|
, mFailed(false)
|
2014-12-12 19:37:07 +00:00
|
|
|
, mDisconnectingOrDisconnected(false)
|
2014-10-10 16:56:43 +00:00
|
|
|
, mCloseEventWasClean(false)
|
|
|
|
, mCloseEventCode(nsIWebSocketChannel::CLOSE_ABNORMAL)
|
|
|
|
, mScriptLine(0)
|
2015-09-01 04:29:23 +00:00
|
|
|
, mScriptColumn(0)
|
2014-10-10 16:56:43 +00:00
|
|
|
, mInnerWindowID(0)
|
2014-10-10 16:58:05 +00:00
|
|
|
, mWorkerPrivate(nullptr)
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
2016-06-23 08:53:14 +00:00
|
|
|
, mHasWorkerHolderRegistered(false)
|
2014-11-11 22:47:13 +00:00
|
|
|
#endif
|
2014-12-12 19:37:07 +00:00
|
|
|
, mIsMainThread(true)
|
2015-02-17 17:40:09 +00:00
|
|
|
, mMutex("WebSocketImpl::mMutex")
|
2014-12-12 19:37:07 +00:00
|
|
|
, mWorkerShuttingDown(false)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mWorkerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2014-12-12 19:37:07 +00:00
|
|
|
mIsMainThread = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssertIsOnTargetThread() const
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(IsTargetThread());
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
bool IsTargetThread() const;
|
|
|
|
|
|
|
|
void Init(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2016-06-01 09:35:33 +00:00
|
|
|
bool aIsServerSide,
|
2014-10-10 16:58:05 +00:00
|
|
|
const nsAString& aURL,
|
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile,
|
|
|
|
uint32_t aScriptLine,
|
2015-09-01 04:29:23 +00:00
|
|
|
uint32_t aScriptColumn,
|
2014-10-10 16:58:05 +00:00
|
|
|
ErrorResult& aRv,
|
|
|
|
bool* aConnectionFailed);
|
|
|
|
|
2015-10-26 15:29:28 +00:00
|
|
|
void AsyncOpen(nsIPrincipal* aPrincipal, uint64_t aInnerWindowID,
|
2016-06-01 09:35:33 +00:00
|
|
|
nsITransportProvider* aTransportProvider,
|
|
|
|
const nsACString& aNegotiatedExtensions,
|
2015-10-26 15:29:28 +00:00
|
|
|
ErrorResult& aRv);
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsresult ParseURL(const nsAString& aURL);
|
2015-05-14 22:43:06 +00:00
|
|
|
nsresult InitializeConnection(nsIPrincipal* aPrincipal);
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
// These methods when called can release the WebSocket object
|
|
|
|
void FailConnection(uint16_t reasonCode,
|
|
|
|
const nsACString& aReasonString = EmptyCString());
|
|
|
|
nsresult CloseConnection(uint16_t reasonCode,
|
2015-04-07 17:31:51 +00:00
|
|
|
const nsACString& aReasonString = EmptyCString());
|
2015-11-24 05:04:20 +00:00
|
|
|
void Disconnect();
|
2014-10-10 16:58:05 +00:00
|
|
|
void DisconnectInternal();
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsresult ConsoleError();
|
2015-11-24 05:04:20 +00:00
|
|
|
void PrintErrorOnConsole(const char* aBundleURI,
|
|
|
|
const char16_t* aError,
|
|
|
|
const char16_t** aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen);
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsresult DoOnMessageAvailable(const nsACString& aMsg,
|
|
|
|
bool isBinary);
|
|
|
|
|
|
|
|
// ConnectionCloseEvents: 'error' event if needed, then 'close' event.
|
|
|
|
nsresult ScheduleConnectionCloseEvents(nsISupports* aContext,
|
2016-03-09 16:02:28 +00:00
|
|
|
nsresult aStatusCode);
|
|
|
|
// 2nd half of ScheduleConnectionCloseEvents, run in its own event.
|
2014-10-10 16:56:43 +00:00
|
|
|
void DispatchConnectionCloseEvents();
|
|
|
|
|
|
|
|
nsresult UpdateURI();
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void AddRefObject();
|
|
|
|
void ReleaseObject();
|
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
bool RegisterWorkerHolder();
|
|
|
|
void UnregisterWorkerHolder();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
nsresult CancelInternal();
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocket> mWebSocket;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWebSocketChannel> mChannel;
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
bool mIsServerSide; // True if we're implementing the server side of a
|
|
|
|
// websocket connection
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
bool mSecure; // if true it is using SSL and the wss scheme,
|
|
|
|
// otherwise it is using the ws scheme with no SSL
|
|
|
|
|
|
|
|
bool mOnCloseScheduled;
|
|
|
|
bool mFailed;
|
2014-12-12 19:37:07 +00:00
|
|
|
bool mDisconnectingOrDisconnected;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
// Set attributes of DOM 'onclose' message
|
|
|
|
bool mCloseEventWasClean;
|
|
|
|
nsString mCloseEventReason;
|
|
|
|
uint16_t mCloseEventCode;
|
|
|
|
|
|
|
|
nsCString mAsciiHost; // hostname
|
|
|
|
uint32_t mPort;
|
|
|
|
nsCString mResource; // [filepath[?query]]
|
|
|
|
nsString mUTF16Origin;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCString mURI;
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCString mRequestedProtocolList;
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
nsWeakPtr mOriginDocument;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
// Web Socket owner information:
|
|
|
|
// - the script file name, UTF8 encoded.
|
2015-09-01 04:29:23 +00:00
|
|
|
// - source code line number and column number where the Web Socket object
|
|
|
|
// was constructed.
|
2014-10-10 16:56:43 +00:00
|
|
|
// - 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;
|
2015-09-01 04:29:23 +00:00
|
|
|
uint32_t mScriptColumn;
|
2014-10-10 16:56:43 +00:00
|
|
|
uint64_t mInnerWindowID;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2016-06-23 08:53:14 +00:00
|
|
|
nsAutoPtr<WorkerHolder> mWorkerHolder;
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// This is protected by mutex.
|
2016-06-23 08:53:14 +00:00
|
|
|
bool mHasWorkerHolderRegistered;
|
2014-11-11 22:47:13 +00:00
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
bool HasWorkerHolderRegistered()
|
2014-11-11 22:47:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWebSocket);
|
|
|
|
MutexAutoLock lock(mWebSocket->mMutex);
|
2016-06-23 08:53:14 +00:00
|
|
|
return mHasWorkerHolderRegistered;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
void SetHasWorkerHolderRegistered(bool aValue)
|
2014-11-11 22:47:13 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWebSocket);
|
|
|
|
MutexAutoLock lock(mWebSocket->mMutex);
|
2016-06-23 08:53:14 +00:00
|
|
|
mHasWorkerHolderRegistered = aValue;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-17 07:12:26 +00:00
|
|
|
nsWeakPtr mWeakLoadGroup;
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
bool mIsMainThread;
|
2015-02-17 17:40:09 +00:00
|
|
|
|
|
|
|
// This mutex protects mWorkerShuttingDown.
|
|
|
|
mozilla::Mutex mMutex;
|
2014-12-12 19:37:07 +00:00
|
|
|
bool mWorkerShuttingDown;
|
|
|
|
|
2015-10-28 19:11:05 +00:00
|
|
|
RefPtr<WebSocketEventService> mService;
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
private:
|
|
|
|
~WebSocketImpl()
|
|
|
|
{
|
|
|
|
// If we threw during Init we never called disconnect
|
2014-12-12 19:37:07 +00:00
|
|
|
if (!mDisconnectingOrDisconnected) {
|
2014-10-10 16:56:43 +00:00
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WebSocketImpl,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIWebSocketListener,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference,
|
2014-10-10 16:58:05 +00:00
|
|
|
nsIRequest,
|
|
|
|
nsIEventTarget)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2016-04-11 18:40:06 +00:00
|
|
|
class CallDispatchConnectionCloseEvents final : public CancelableRunnable
|
2012-06-04 23:38:32 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 16:56:43 +00:00
|
|
|
explicit CallDispatchConnectionCloseEvents(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
aWebSocketImpl->AssertIsOnTargetThread();
|
|
|
|
}
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override
|
2012-06-04 23:38:32 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
mWebSocketImpl->AssertIsOnTargetThread();
|
2014-10-10 16:56:43 +00:00
|
|
|
mWebSocketImpl->DispatchConnectionCloseEvents();
|
2012-06-04 23:38:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> mWebSocketImpl;
|
2012-06-04 23:38:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl
|
2012-06-04 23:38:32 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class PrintErrorOnConsoleRunnable final : public WorkerMainThreadRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrintErrorOnConsoleRunnable(WebSocketImpl* aImpl,
|
|
|
|
const char* aBundleURI,
|
|
|
|
const char16_t* aError,
|
|
|
|
const char16_t** aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen)
|
2016-05-03 07:09:47 +00:00
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("WebSocket :: print error on console"))
|
2014-10-10 16:58:05 +00:00
|
|
|
, mImpl(aImpl)
|
|
|
|
, mBundleURI(aBundleURI)
|
|
|
|
, mError(aError)
|
|
|
|
, mFormatStrings(aFormatStrings)
|
|
|
|
, mFormatStringsLen(aFormatStringsLen)
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2015-11-24 05:04:20 +00:00
|
|
|
mImpl->PrintErrorOnConsole(mBundleURI, mError, mFormatStrings,
|
|
|
|
mFormatStringsLen);
|
2014-10-10 16:58:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
const char* mBundleURI;
|
|
|
|
const char16_t* mError;
|
|
|
|
const char16_t** mFormatStrings;
|
|
|
|
uint32_t mFormatStringsLen;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2015-11-24 05:04:20 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::PrintErrorOnConsole(const char *aBundleURI,
|
|
|
|
const char16_t *aError,
|
|
|
|
const char16_t **aFormatStrings,
|
|
|
|
uint32_t aFormatStringsLen)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// This method must run on the main thread.
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<PrintErrorOnConsoleRunnable> runnable =
|
2014-10-10 16:58:05 +00:00
|
|
|
new PrintErrorOnConsoleRunnable(this, aBundleURI, aError, aFormatStrings,
|
|
|
|
aFormatStringsLen);
|
2015-11-24 05:04:20 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
runnable->Dispatch(rv);
|
|
|
|
// XXXbz this seems totally broken. We should be propagating this out, but
|
|
|
|
// none of our callers really propagate anything usefully. Come to think of
|
|
|
|
// it, why is this a syncrunnable anyway? Can't this be a fire-and-forget
|
|
|
|
// runnable??
|
|
|
|
rv.SuppressException();
|
2015-11-24 05:04:20 +00:00
|
|
|
return;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> strBundle;
|
|
|
|
rv = bundleService->CreateBundle(aBundleURI, getter_AddRefs(strBundle));
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv));
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-12-21 21:51:29 +00:00
|
|
|
nsCOMPtr<nsIScriptError> errorObject(
|
2011-05-22 01:27:52 +00:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv));
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// Localize the error message
|
|
|
|
nsXPIDLString message;
|
|
|
|
if (aFormatStrings) {
|
|
|
|
rv = strBundle->FormatStringFromName(aError, aFormatStrings,
|
|
|
|
aFormatStringsLen,
|
|
|
|
getter_Copies(message));
|
2010-06-17 18:34:24 +00:00
|
|
|
} else {
|
2011-05-22 01:27:52 +00:00
|
|
|
rv = strBundle->GetStringFromName(aError, getter_Copies(message));
|
|
|
|
}
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
if (mInnerWindowID) {
|
|
|
|
rv = errorObject->InitWithWindowID(message,
|
|
|
|
NS_ConvertUTF8toUTF16(mScriptFile),
|
2015-09-01 04:29:23 +00:00
|
|
|
EmptyString(), mScriptLine,
|
|
|
|
mScriptColumn,
|
2014-10-29 16:54:35 +00:00
|
|
|
nsIScriptError::errorFlag, "Web Socket",
|
|
|
|
mInnerWindowID);
|
|
|
|
} else {
|
|
|
|
rv = errorObject->Init(message,
|
|
|
|
NS_ConvertUTF8toUTF16(mScriptFile),
|
2015-09-01 04:29:23 +00:00
|
|
|
EmptyString(), mScriptLine, mScriptColumn,
|
2014-10-29 16:54:35 +00:00
|
|
|
nsIScriptError::errorFlag, "Web Socket");
|
|
|
|
}
|
|
|
|
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// print the error message directly to the JS console
|
2011-12-21 21:51:29 +00:00
|
|
|
rv = console->LogMessage(errorObject);
|
2015-11-24 05:04:20 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class CancelWebSocketRunnable final : public Runnable
|
2015-03-18 22:38:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CancelWebSocketRunnable(nsIWebSocketChannel* aChannel, uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
|
|
|
: mChannel(aChannel)
|
|
|
|
, mReasonCode(aReasonCode)
|
|
|
|
, mReasonString(aReasonString)
|
|
|
|
{}
|
|
|
|
|
2015-03-24 18:12:17 +00:00
|
|
|
NS_IMETHOD Run() override
|
2015-03-18 22:38:02 +00:00
|
|
|
{
|
|
|
|
mChannel->Close(mReasonCode, mReasonString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIWebSocketChannel> mChannel;
|
|
|
|
uint16_t mReasonCode;
|
|
|
|
nsCString mReasonString;
|
|
|
|
};
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
class MOZ_STACK_CLASS MaybeDisconnect
|
|
|
|
{
|
|
|
|
public:
|
2014-12-13 19:17:04 +00:00
|
|
|
explicit MaybeDisconnect(WebSocketImpl* aImpl)
|
2014-12-12 19:37:07 +00:00
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~MaybeDisconnect()
|
|
|
|
{
|
2015-02-17 17:40:09 +00:00
|
|
|
bool toDisconnect = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mImpl->mMutex);
|
|
|
|
toDisconnect = mImpl->mWorkerShuttingDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toDisconnect) {
|
2014-12-12 19:37:07 +00:00
|
|
|
mImpl->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class CloseConnectionRunnable final : public Runnable
|
2015-04-08 18:09:26 +00:00
|
|
|
{
|
|
|
|
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:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> mImpl;
|
2015-04-08 18:09:26 +00:00
|
|
|
uint16_t mReasonCode;
|
|
|
|
const nsCString mReasonString;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::CloseConnection(uint16_t aReasonCode,
|
2015-04-07 17:31:51 +00:00
|
|
|
const nsACString& aReasonString)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2015-04-08 18:09:26 +00:00
|
|
|
if (!IsTargetThread()) {
|
2016-04-26 00:23:21 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2015-04-08 18:09:26 +00:00
|
|
|
new CloseConnectionRunnable(this, aReasonCode, aReasonString);
|
2016-04-26 00:23:21 +00:00
|
|
|
return Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2015-04-08 18:09:26 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this method is called because the worker is going away, we will not
|
|
|
|
// receive the OnStop() method and we have to disconnect the WebSocket and
|
2016-06-23 08:53:14 +00:00
|
|
|
// release the WorkerHolder.
|
2014-12-12 19:37:07 +00:00
|
|
|
MaybeDisconnect md(this);
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
uint16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSING ||
|
|
|
|
readyState == WebSocket::CLOSED) {
|
2011-06-08 12:27:11 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// The common case...
|
|
|
|
if (mChannel) {
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
// The channel has to be closed on the main-thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return mChannel->Close(aReasonCode, aReasonString);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CancelWebSocketRunnable> runnable =
|
2015-04-07 17:31:51 +00:00
|
|
|
new CancelWebSocketRunnable(mChannel, aReasonCode, aReasonString);
|
|
|
|
return NS_DispatchToMainThread(runnable);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// No channel, but not disconnected: canceled or failed early
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CONNECTING,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Should only get here for early websocket cancel/error");
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// Server won't be sending us a close code, so use what's passed in here.
|
|
|
|
mCloseEventCode = aReasonCode;
|
|
|
|
CopyUTF8toUTF16(aReasonString, mCloseEventReason);
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSING);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
ScheduleConnectionCloseEvents(
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr,
|
2012-06-04 23:38:32 +00:00
|
|
|
(aReasonCode == nsIWebSocketChannel::CLOSE_NORMAL ||
|
|
|
|
aReasonCode == nsIWebSocketChannel::CLOSE_GOING_AWAY) ?
|
2016-03-09 16:02:28 +00:00
|
|
|
NS_OK : NS_ERROR_FAILURE);
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ConsoleError()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2015-03-18 22:38:02 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mWorkerShuttingDown) {
|
|
|
|
// Too late to report anything, bail out.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_ConvertUTF8toUTF16 specUTF16(mURI);
|
|
|
|
const char16_t* formatStrings[] = { specUTF16.get() };
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (mWebSocket->ReadyState() < WebSocket::OPEN) {
|
2014-10-10 16:58:05 +00:00
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2016-07-21 05:03:25 +00:00
|
|
|
u"connectionFailure",
|
2014-10-10 16:58:05 +00:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
|
|
|
} else {
|
|
|
|
PrintErrorOnConsole("chrome://global/locale/appstrings.properties",
|
2016-07-21 05:03:25 +00:00
|
|
|
u"netInterrupt",
|
2014-10-10 16:58:05 +00:00
|
|
|
formatStrings, ArrayLength(formatStrings));
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
2011-08-02 23:55:17 +00:00
|
|
|
/// todo some specific errors - like for message too large
|
2014-10-10 16:58:05 +00:00
|
|
|
return NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 08:04:08 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::FailConnection(uint16_t aReasonCode,
|
|
|
|
const nsACString& aReasonString)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
2012-02-21 22:57:10 +00:00
|
|
|
CloseConnection(aReasonCode, aReasonString);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class DisconnectInternalRunnable final : public WorkerMainThreadRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit DisconnectInternalRunnable(WebSocketImpl* aImpl)
|
2016-05-03 07:09:47 +00:00
|
|
|
: WorkerMainThreadRunnable(aImpl->mWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("WebSocket :: disconnect"))
|
2014-10-10 16:58:05 +00:00
|
|
|
, mImpl(aImpl)
|
|
|
|
{ }
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
mImpl->DisconnectInternal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// A raw pointer because this runnable is sync.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2015-11-24 05:04:20 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Disconnect()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2015-11-24 05:04:20 +00:00
|
|
|
return;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// Disconnect can be called from some control event (such as Notify() of
|
2016-06-23 08:53:14 +00:00
|
|
|
// WorkerHolder). This will be schedulated before any other sync/async
|
2014-12-12 19:37:07 +00:00
|
|
|
// runnable. In order to prevent some double Disconnect() calls, we use this
|
|
|
|
// boolean.
|
|
|
|
mDisconnectingOrDisconnected = true;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// DisconnectInternal touches observers and nsILoadGroup and it must run on
|
|
|
|
// the main thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
DisconnectInternal();
|
|
|
|
} else {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DisconnectInternalRunnable> runnable =
|
2014-10-10 16:58:05 +00:00
|
|
|
new DisconnectInternalRunnable(this);
|
2015-11-24 05:04:20 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
runnable->Dispatch(rv);
|
|
|
|
// XXXbz this seems totally broken. We should be propagating this out, but
|
|
|
|
// where to, exactly?
|
|
|
|
rv.SuppressException();
|
2012-02-21 22:57:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
// DontKeepAliveAnyMore() can release the object. So hold a reference to this
|
|
|
|
// until the end of the method.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> kungfuDeathGrip = this;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2016-02-10 07:23:00 +00:00
|
|
|
NS_ReleaseOnMainThread(mChannel.forget());
|
|
|
|
NS_ReleaseOnMainThread(mService.forget());
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
mWebSocket->DontKeepAliveAnyMore();
|
|
|
|
mWebSocket->mImpl = nullptr;
|
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
if (mWorkerPrivate && mWorkerHolder) {
|
|
|
|
UnregisterWorkerHolder();
|
2014-12-12 19:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// We want to release the WebSocket in the correct thread.
|
|
|
|
mWebSocket = nullptr;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
|
|
|
WebSocketImpl::DisconnectInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-17 07:12:26 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_QueryReferent(mWeakLoadGroup);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (loadGroup) {
|
|
|
|
loadGroup->RemoveRequest(this, nullptr, NS_OK);
|
2014-11-11 22:47:13 +00:00
|
|
|
// mWeakLoadGroup has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mWeakLoadGroup = nullptr;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:09:26 +00: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 16:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIWebSocketListener methods:
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::DoOnMessageAvailable(const nsACString& aMsg, bool isBinary)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSED) {
|
2012-06-04 23:38:32 +00:00
|
|
|
NS_ERROR("Received message after CLOSED");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2011-12-20 08:20:12 +00:00
|
|
|
// Dispatch New Message
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = mWebSocket->CreateAndDispatchMessageEvent(aMsg, isBinary);
|
2011-12-20 08:20:12 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event");
|
|
|
|
}
|
2015-01-13 19:03:56 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2015-01-13 19:03:56 +00: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 01:27:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-12-20 08:20:12 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
return DoOnMessageAvailable(aMsg, false);
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnBinaryMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMsg)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:20:12 +00:00
|
|
|
return DoOnMessageAvailable(aMsg, true);
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnStart(nsISupports* aContext)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2012-06-04 23:38:32 +00:00
|
|
|
|
|
|
|
// This is the only function that sets OPEN, and should be called only once
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::OPEN,
|
2012-06-04 23:38:32 +00:00
|
|
|
"readyState already OPEN! OnStart called twice?");
|
|
|
|
|
|
|
|
// Nothing to do if we've already closed/closing
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState != WebSocket::CONNECTING) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 16:58:05 +00:00
|
|
|
nsresult rv = mWebSocket->CheckInnerWindowCorrectness();
|
2012-02-21 22:57:10 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-04 23:38:32 +00:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-21 22:57:10 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-10-14 09:28:07 +00:00
|
|
|
mChannel->GetProtocol(mWebSocket->mEstablishedProtocol);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mChannel->GetExtensions(mWebSocket->mEstablishedExtensions);
|
2011-12-15 23:23:00 +00:00
|
|
|
UpdateURI();
|
2011-08-04 03:46:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::OPEN);
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2015-10-28 19:11:05 +00:00
|
|
|
mService->WebSocketOpened(mChannel->Serial(),mInnerWindowID,
|
|
|
|
mWebSocket->mEffectiveURL,
|
|
|
|
mWebSocket->mEstablishedProtocol,
|
|
|
|
mWebSocket->mEstablishedExtensions);
|
|
|
|
|
2015-01-13 19:03:56 +00:00
|
|
|
// Let's keep the object alive because the webSocket can be CCed in the
|
|
|
|
// onopen callback.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocket> webSocket = mWebSocket;
|
2015-01-13 19:03:56 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// Call 'onopen'
|
2015-01-13 19:03:56 +00:00
|
|
|
rv = webSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("open"));
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the open event");
|
|
|
|
}
|
|
|
|
|
2015-01-13 19:03:56 +00:00
|
|
|
webSocket->UpdateMustKeepAlive();
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnStop(nsISupports* aContext, nsresult aStatusCode)
|
2012-06-04 23:38:32 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// We can be CONNECTING here if connection failed.
|
|
|
|
// We can be OPEN if we have encountered a fatal protocol error
|
|
|
|
// We can be CLOSING if close() was called and/or server initiated close.
|
2014-10-14 09:28:07 +00:00
|
|
|
MOZ_ASSERT(mWebSocket->ReadyState() != WebSocket::CLOSED,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Shouldn't already be CLOSED when OnStop called");
|
|
|
|
|
2016-03-09 16:02:28 +00:00
|
|
|
return ScheduleConnectionCloseEvents(aContext, aStatusCode);
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ScheduleConnectionCloseEvents(nsISupports* aContext,
|
2016-03-09 16:02:28 +00:00
|
|
|
nsresult aStatusCode)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// no-op if some other code has already initiated close event
|
|
|
|
if (!mOnCloseScheduled) {
|
|
|
|
mCloseEventWasClean = NS_SUCCEEDED(aStatusCode);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
if (aStatusCode == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// don't generate an error event just because of an unclean close
|
|
|
|
aStatusCode = NS_OK;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(aStatusCode)) {
|
|
|
|
ConsoleError();
|
|
|
|
mFailed = true;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
mOnCloseScheduled = true;
|
|
|
|
|
2016-03-09 16:02:28 +00:00
|
|
|
NS_DispatchToCurrentThread(new CallDispatchConnectionCloseEvents(this));
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnAcknowledge(nsISupports *aContext, uint32_t aSize)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (aSize > mWebSocket->mOutgoingBufferedAmount) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->mOutgoingBufferedAmount -= aSize;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::OnServerClose(nsISupports *aContext, uint16_t aCode,
|
|
|
|
const nsACString &aReason)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int16_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(readyState != WebSocket::CONNECTING,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Received server close before connected?");
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState != WebSocket::CLOSED,
|
2012-06-04 23:38:32 +00:00
|
|
|
"Received server close after already closed!");
|
2012-02-21 22:57:13 +00:00
|
|
|
|
2012-02-01 04:41:43 +00:00
|
|
|
// store code/string for onclose DOM event
|
|
|
|
mCloseEventCode = aCode;
|
|
|
|
CopyUTF8toUTF16(aReason, mCloseEventReason);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::OPEN) {
|
2012-06-04 23:38:32 +00:00
|
|
|
// Server initiating close.
|
2012-05-17 00:09:42 +00:00
|
|
|
// RFC 6455, 5.5.1: "When sending a Close frame in response, the endpoint
|
|
|
|
// typically echos the status code it received".
|
|
|
|
// But never send certain codes, per section 7.4.1
|
|
|
|
if (aCode == 1005 || aCode == 1006 || aCode == 1015) {
|
|
|
|
CloseConnection(0, EmptyCString());
|
|
|
|
} else {
|
|
|
|
CloseConnection(aCode, aReason);
|
|
|
|
}
|
2012-02-21 22:57:13 +00:00
|
|
|
} else {
|
2012-06-04 23:38:32 +00:00
|
|
|
// We initiated close, and server has replied: OnStop does rest of the work.
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(readyState == WebSocket::CLOSING, "unknown state");
|
2012-02-21 22:57:13 +00:00
|
|
|
}
|
2012-02-01 04:41:43 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIInterfaceRequestor
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetInterface(const nsIID& aIID, void** aResult)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (!mWebSocket || mWebSocket->ReadyState() == WebSocket::CLOSED) {
|
2011-06-08 12:27:11 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2011-06-08 12:27:11 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIAuthPrompt)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsIAuthPrompt2))) {
|
2016-04-20 22:04:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = mWebSocket->GetWindowIfCurrent();
|
|
|
|
if (!win) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2016-04-20 22:04:36 +00:00
|
|
|
nsresult rv;
|
2010-06-17 18:34:24 +00:00
|
|
|
nsCOMPtr<nsIPromptFactory> wwatch =
|
|
|
|
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-04-20 22:04:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outerWindow = win->GetOuterWindow();
|
2010-06-17 18:34:24 +00:00
|
|
|
return wwatch->GetPrompt(outerWindow, aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
return QueryInterface(aIID, aResult);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-09-10 13:48:14 +00:00
|
|
|
// WebSocket
|
2010-06-17 18:34:24 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
WebSocket::WebSocket(nsPIDOMWindowInner* aOwnerWindow)
|
2014-10-10 16:58:05 +00:00
|
|
|
: DOMEventTargetHelper(aOwnerWindow)
|
2014-12-12 19:37:07 +00:00
|
|
|
, mIsMainThread(true)
|
2014-10-10 16:58:05 +00:00
|
|
|
, mKeepingAlive(false)
|
|
|
|
, mCheckMustKeepAlive(true)
|
2014-10-14 09:28:07 +00:00
|
|
|
, mOutgoingBufferedAmount(0)
|
|
|
|
, mBinaryType(dom::BinaryType::Blob)
|
2015-02-17 17:40:09 +00:00
|
|
|
, mMutex("WebSocket::mMutex")
|
2014-10-14 09:28:07 +00:00
|
|
|
, mReadyState(CONNECTING)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
mImpl = new WebSocketImpl(this);
|
2014-12-12 19:37:07 +00:00
|
|
|
mIsMainThread = mImpl->mIsMainThread;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::~WebSocket()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
JSObject*
|
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 14:13:33 +00:00
|
|
|
WebSocket::WrapObject(JSContext* cx, JS::Handle<JSObject*> aGivenProto)
|
2012-09-10 13:48:14 +00: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 14:13:33 +00:00
|
|
|
return WebSocketBinding::Wrap(cx, this, aGivenProto);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// WebIDL
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Constructor:
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 16:07:49 +00:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 13:48:14 +00:00
|
|
|
const nsAString& aUrl,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
2016-06-01 09:35:33 +00:00
|
|
|
return WebSocket::ConstructorCommon(aGlobal, aUrl, protocols, nullptr,
|
|
|
|
EmptyCString(), aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WebSocket>
|
2012-12-03 16:07:49 +00:00
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
2012-09-10 13:48:14 +00:00
|
|
|
const nsAString& aUrl,
|
|
|
|
const nsAString& aProtocol,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
Sequence<nsString> protocols;
|
2015-05-28 18:07:44 +00:00
|
|
|
if (!protocols.AppendElement(aProtocol, fallible)) {
|
2015-05-25 11:50:15 +00:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
return WebSocket::ConstructorCommon(aGlobal, aUrl, protocols, nullptr,
|
|
|
|
EmptyCString(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<WebSocket>
|
|
|
|
WebSocket::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return WebSocket::ConstructorCommon(aGlobal, aUrl, aProtocols, nullptr,
|
|
|
|
EmptyCString(), aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 14:01:00 +00:00
|
|
|
already_AddRefed<WebSocket>
|
|
|
|
WebSocket::CreateServerWebSocket(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
nsITransportProvider* aTransportProvider,
|
|
|
|
const nsAString& aNegotiatedExtensions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return WebSocket::ConstructorCommon(aGlobal, aUrl, aProtocols, aTransportProvider,
|
|
|
|
NS_ConvertUTF16toUTF8(aNegotiatedExtensions), aRv);
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This class is used to clear any exception.
|
|
|
|
class MOZ_STACK_CLASS ClearException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ClearException(JSContext* aCx)
|
|
|
|
: mCx(aCx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClearException()
|
|
|
|
{
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mCx;
|
|
|
|
};
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
class WebSocketMainThreadRunnable : public WorkerMainThreadRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-05-03 07:09:47 +00:00
|
|
|
WebSocketMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aTelemetryKey)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate, aTelemetryKey)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2015-05-14 22:43:06 +00:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
bool MainThreadRun() override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner* window = wp->GetWindow();
|
2014-10-29 16:54:35 +00:00
|
|
|
if (window) {
|
|
|
|
return InitWithWindow(window);
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
return InitWindowless(wp);
|
2014-10-29 16:54:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
protected:
|
2016-01-30 17:05:36 +00:00
|
|
|
virtual bool InitWithWindow(nsPIDOMWindowInner* aWindow) = 0;
|
2015-05-14 22:43:06 +00:00
|
|
|
|
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InitRunnable final : public WebSocketMainThreadRunnable
|
|
|
|
{
|
|
|
|
public:
|
2016-06-01 09:35:33 +00:00
|
|
|
InitRunnable(WebSocketImpl* aImpl, bool aIsServerSide,
|
|
|
|
const nsAString& aURL,
|
2015-05-14 22:43:06 +00:00
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile, uint32_t aScriptLine,
|
2015-09-01 04:29:23 +00:00
|
|
|
uint32_t aScriptColumn,
|
2015-05-14 22:43:06 +00:00
|
|
|
ErrorResult& aRv, bool* aConnectionFailed)
|
2016-05-03 07:09:47 +00:00
|
|
|
: WebSocketMainThreadRunnable(aImpl->mWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("WebSocket :: init"))
|
2015-05-14 22:43:06 +00:00
|
|
|
, mImpl(aImpl)
|
2016-06-01 09:35:33 +00:00
|
|
|
, mIsServerSide(aIsServerSide)
|
2015-05-14 22:43:06 +00:00
|
|
|
, mURL(aURL)
|
|
|
|
, mProtocolArray(aProtocolArray)
|
|
|
|
, mScriptFile(aScriptFile)
|
|
|
|
, mScriptLine(aScriptLine)
|
2015-09-01 04:29:23 +00:00
|
|
|
, mScriptColumn(aScriptColumn)
|
2015-05-14 22:43:06 +00:00
|
|
|
, mRv(aRv)
|
|
|
|
, mConnectionFailed(aConnectionFailed)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-01-30 17:05:36 +00:00
|
|
|
virtual bool InitWithWindow(nsPIDOMWindowInner* aWindow) override
|
2014-10-29 16:54:35 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AutoJSAPI jsapi;
|
2014-10-29 16:54:35 +00:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(aWindow))) {
|
2014-10-10 16:58:05 +00:00
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearException ce(jsapi.cx());
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
nsIDocument* doc = aWindow->GetExtantDoc();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!doc) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
mImpl->Init(jsapi.cx(), principal, mIsServerSide, mURL, mProtocolArray,
|
|
|
|
mScriptFile, mScriptLine, mScriptColumn, mRv,
|
|
|
|
mConnectionFailed);
|
2014-10-10 16:58:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) override
|
2014-10-29 16:54:35 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-14 22:43:06 +00:00
|
|
|
MOZ_ASSERT(aTopLevelWorkerPrivate && !aTopLevelWorkerPrivate->GetWindow());
|
2014-10-29 16:54:35 +00:00
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
mImpl->Init(nullptr, aTopLevelWorkerPrivate->GetPrincipal(), mIsServerSide,
|
|
|
|
mURL, mProtocolArray, mScriptFile, mScriptLine, mScriptColumn,
|
|
|
|
mRv, mConnectionFailed);
|
2014-10-29 16:54:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
bool mIsServerSide;
|
2014-10-10 16:58:05 +00:00
|
|
|
const nsAString& mURL;
|
|
|
|
nsTArray<nsString>& mProtocolArray;
|
|
|
|
nsCString mScriptFile;
|
|
|
|
uint32_t mScriptLine;
|
2015-09-01 04:29:23 +00:00
|
|
|
uint32_t mScriptColumn;
|
2014-10-10 16:58:05 +00:00
|
|
|
ErrorResult& mRv;
|
|
|
|
bool* mConnectionFailed;
|
|
|
|
};
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
class AsyncOpenRunnable final : public WebSocketMainThreadRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncOpenRunnable(WebSocketImpl* aImpl, ErrorResult& aRv)
|
2016-05-03 07:09:47 +00:00
|
|
|
: WebSocketMainThreadRunnable(aImpl->mWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("WebSocket :: AsyncOpen"))
|
2014-10-10 16:58:05 +00:00
|
|
|
, mImpl(aImpl)
|
|
|
|
, mRv(aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:43:06 +00:00
|
|
|
protected:
|
2016-01-30 17:05:36 +00:00
|
|
|
virtual bool InitWithWindow(nsPIDOMWindowInner* aWindow) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-10-26 15:29:28 +00:00
|
|
|
MOZ_ASSERT(aWindow);
|
2015-05-14 22:43:06 +00: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;
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:29:28 +00:00
|
|
|
uint64_t windowID = 0;
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> topWindow = aWindow->GetScriptableTop();
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> topInner;
|
2015-10-26 21:37:32 +00:00
|
|
|
if (topWindow) {
|
2016-01-30 17:05:36 +00:00
|
|
|
topInner = topWindow->GetCurrentInnerWindow();
|
2015-10-26 15:30:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
if (topInner) {
|
|
|
|
windowID = topInner->WindowID();
|
2015-10-26 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
mImpl->AsyncOpen(principal, windowID, nullptr, EmptyCString(), mRv);
|
2015-05-14 22:43:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InitWindowless(WorkerPrivate* aTopLevelWorkerPrivate) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aTopLevelWorkerPrivate && !aTopLevelWorkerPrivate->GetWindow());
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
mImpl->AsyncOpen(aTopLevelWorkerPrivate->GetPrincipal(), 0, nullptr,
|
|
|
|
EmptyCString(), mRv);
|
2014-10-10 16:58:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Raw pointer. This worker runs synchronously.
|
|
|
|
WebSocketImpl* mImpl;
|
|
|
|
|
|
|
|
ErrorResult& mRv;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
already_AddRefed<WebSocket>
|
2016-06-01 09:35:33 +00:00
|
|
|
WebSocket::ConstructorCommon(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aUrl,
|
|
|
|
const Sequence<nsString>& aProtocols,
|
|
|
|
nsITransportProvider* aTransportProvider,
|
|
|
|
const nsACString& aNegotiatedExtensions,
|
|
|
|
ErrorResult& aRv)
|
2012-09-10 13:48:14 +00:00
|
|
|
{
|
2016-06-01 09:35:33 +00:00
|
|
|
MOZ_ASSERT_IF(!aTransportProvider, aNegotiatedExtensions.IsEmpty());
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> ownerWindow;
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!scriptPrincipal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
principal = scriptPrincipal->GetPrincipal();
|
|
|
|
if (!principal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!sgo) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
ownerWindow = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!ownerWindow) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 21:37:32 +00:00
|
|
|
MOZ_ASSERT_IF(ownerWindow, ownerWindow->IsInnerWindow());
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsTArray<nsString> protocolArray;
|
|
|
|
|
|
|
|
for (uint32_t index = 0, len = aProtocols.Length(); index < len; ++index) {
|
|
|
|
|
|
|
|
const nsString& protocolElement = aProtocols[index];
|
|
|
|
|
|
|
|
if (protocolElement.IsEmpty()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (protocolArray.Contains(protocolElement)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (protocolElement.FindChar(',') != -1) /* interferes w/list */ {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolArray.AppendElement(protocolElement);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocket> webSocket = new WebSocket(ownerWindow);
|
|
|
|
RefPtr<WebSocketImpl> kungfuDeathGrip = webSocket->mImpl;
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
bool connectionFailed = true;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2016-06-01 09:35:33 +00:00
|
|
|
webSocket->mImpl->Init(aGlobal.Context(), principal, !!aTransportProvider,
|
|
|
|
aUrl, protocolArray, EmptyCString(),
|
|
|
|
0, 0, aRv, &connectionFailed);
|
2014-10-10 16:58:05 +00:00
|
|
|
} else {
|
2016-06-23 08:53:14 +00:00
|
|
|
// In workers we have to keep the worker alive using a workerHolder in order
|
|
|
|
// to dispatch messages correctly.
|
|
|
|
if (!webSocket->mImpl->RegisterWorkerHolder()) {
|
2014-12-12 19:37:07 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-01 04:29:23 +00:00
|
|
|
unsigned lineno, column;
|
2016-03-09 10:20:11 +00:00
|
|
|
JS::AutoFilename file;
|
2015-09-01 04:29:23 +00:00
|
|
|
if (!JS::DescribeScriptedCaller(aGlobal.Context(), &file, &lineno,
|
|
|
|
&column)) {
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_WARNING("Failed to get line number and filename in workers.");
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<InitRunnable> runnable =
|
2016-06-01 09:35:33 +00:00
|
|
|
new InitRunnable(webSocket->mImpl, !!aTransportProvider, aUrl,
|
|
|
|
protocolArray, nsDependentCString(file.get()), lineno,
|
|
|
|
column, aRv, &connectionFailed);
|
2015-11-24 05:04:20 +00:00
|
|
|
runnable->Dispatch(aRv);
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// It can be that we have been already disconnected because the WebSocket is
|
|
|
|
// gone away while we where initializing the webSocket.
|
|
|
|
if (!webSocket->mImpl) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// We don't return an error if the connection just failed. Instead we dispatch
|
|
|
|
// an event.
|
|
|
|
if (connectionFailed) {
|
|
|
|
webSocket->mImpl->FailConnection(nsIWebSocketChannel::CLOSE_ABNORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a channel, the connection is failed and onerror() will be
|
|
|
|
// called asynchrounsly.
|
|
|
|
if (!webSocket->mImpl->mChannel) {
|
|
|
|
return webSocket.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS ClearWebSocket
|
|
|
|
{
|
|
|
|
public:
|
2014-10-10 21:28:35 +00:00
|
|
|
explicit ClearWebSocket(WebSocketImpl* aWebSocketImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
|
|
|
, mDone(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Done()
|
|
|
|
{
|
|
|
|
mDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClearWebSocket()
|
|
|
|
{
|
|
|
|
if (!mDone) {
|
|
|
|
mWebSocketImpl->mChannel = nullptr;
|
|
|
|
mWebSocketImpl->FailConnection(nsIWebSocketChannel::CLOSE_ABNORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketImpl* mWebSocketImpl;
|
|
|
|
bool mDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClearWebSocket cws(webSocket->mImpl);
|
|
|
|
|
|
|
|
// This operation must be done on the correct thread. The rest must run on the
|
|
|
|
// main-thread.
|
|
|
|
aRv = webSocket->mImpl->mChannel->SetNotificationCallbacks(webSocket->mImpl);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2015-05-14 22:43:06 +00:00
|
|
|
MOZ_ASSERT(principal);
|
2015-10-26 15:29:28 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowOuter* outerWindow = ownerWindow->GetOuterWindow();
|
2015-10-26 21:37:32 +00:00
|
|
|
|
2015-10-26 15:29:28 +00:00
|
|
|
uint64_t windowID = 0;
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> topWindow = outerWindow->GetScriptableTop();
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> topInner;
|
2015-10-26 21:37:32 +00:00
|
|
|
if (topWindow) {
|
2016-01-30 17:05:36 +00:00
|
|
|
topInner = topWindow->GetCurrentInnerWindow();
|
2015-10-26 15:30:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
if (topInner) {
|
|
|
|
windowID = topInner->WindowID();
|
2015-10-26 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
webSocket->mImpl->AsyncOpen(principal, windowID, aTransportProvider,
|
|
|
|
aNegotiatedExtensions, aRv);
|
2014-10-10 16:58:05 +00:00
|
|
|
} else {
|
2016-06-01 09:35:33 +00:00
|
|
|
MOZ_ASSERT(!aTransportProvider && aNegotiatedExtensions.IsEmpty(),
|
|
|
|
"not yet implemented");
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AsyncOpenRunnable> runnable =
|
2014-10-10 16:58:05 +00:00
|
|
|
new AsyncOpenRunnable(webSocket->mImpl, aRv);
|
2015-11-24 05:04:20 +00:00
|
|
|
runnable->Dispatch(aRv);
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
// It can be that we have been already disconnected because the WebSocket is
|
|
|
|
// gone away while we where initializing the webSocket.
|
|
|
|
if (!webSocket->mImpl) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-28 19:11:05 +00:00
|
|
|
// Let's inform devtools about this new active WebSocket.
|
|
|
|
webSocket->mImpl->mService->WebSocketCreated(webSocket->mImpl->mChannel->Serial(),
|
|
|
|
webSocket->mImpl->mInnerWindowID,
|
|
|
|
webSocket->mURI,
|
|
|
|
webSocket->mImpl->mRequestedProtocolList);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
cws.Done();
|
2012-09-10 13:48:14 +00:00
|
|
|
return webSocket.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WebSocket)
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(WebSocket)
|
2012-02-16 16:45:25 +00:00
|
|
|
bool isBlack = tmp->IsBlack();
|
2014-10-10 16:56:43 +00:00
|
|
|
if (isBlack || tmp->mKeepingAlive) {
|
2012-01-26 15:55:30 +00:00
|
|
|
if (tmp->mListenerManager) {
|
2012-10-17 01:22:02 +00:00
|
|
|
tmp->mListenerManager->MarkForCC();
|
2012-01-26 15:55:30 +00:00
|
|
|
}
|
2012-03-14 14:22:10 +00:00
|
|
|
if (!isBlack && tmp->PreservingWrapper()) {
|
2013-09-09 03:28:48 +00:00
|
|
|
// This marks the wrapper black.
|
|
|
|
tmp->GetWrapper();
|
2012-02-16 16:45:25 +00:00
|
|
|
}
|
2012-01-26 15:55:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(WebSocket)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(WebSocket)
|
2012-01-26 15:55:30 +00:00
|
|
|
return tmp->IsBlack();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-01-26 15:55:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 16:56:43 +00:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mImpl->mChannel)
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WebSocket,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2014-10-10 16:56:43 +00:00
|
|
|
if (tmp->mImpl) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mImpl->mChannel)
|
|
|
|
tmp->mImpl->Disconnect();
|
|
|
|
MOZ_ASSERT(!tmp->mImpl);
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WebSocket)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WebSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WebSocket, DOMEventTargetHelper)
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::DisconnectFromOwner()
|
2012-03-13 00:56:07 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
2014-10-10 16:56:43 +00:00
|
|
|
|
|
|
|
if (mImpl) {
|
|
|
|
mImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
|
|
|
}
|
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
DontKeepAliveAnyMore();
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl:: initialization
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Init(JSContext* aCx,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2016-06-01 09:35:33 +00:00
|
|
|
bool aIsServerSide,
|
2014-10-10 16:56:43 +00:00
|
|
|
const nsAString& aURL,
|
2014-10-10 16:58:05 +00:00
|
|
|
nsTArray<nsString>& aProtocolArray,
|
|
|
|
const nsACString& aScriptFile,
|
|
|
|
uint32_t aScriptLine,
|
2015-09-01 04:29:23 +00:00
|
|
|
uint32_t aScriptColumn,
|
2014-10-10 16:58:05 +00:00
|
|
|
ErrorResult& aRv,
|
|
|
|
bool* aConnectionFailed)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2012-09-10 13:48:14 +00:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2015-10-28 19:11:05 +00:00
|
|
|
mService = WebSocketEventService::GetOrCreate();
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
// We need to keep the implementation alive in case the init disconnects it
|
|
|
|
// because of some error.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> kungfuDeathGrip = this;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Attempt to kill "ghost" websocket: but usually too early for check to fail
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = mWebSocket->CheckInnerWindowCorrectness();
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Shut down websocket if window is frozen or destroyed (only needed for
|
|
|
|
// "ghost" websockets--see bug 696085)
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!os)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-03 08:24:17 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = os->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = os->AddObserver(this, DOM_WINDOW_FROZEN_TOPIC, true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
mScriptFile = aScriptFile;
|
|
|
|
mScriptLine = aScriptLine;
|
2015-09-01 04:29:23 +00:00
|
|
|
mScriptColumn = aScriptColumn;
|
2014-10-10 16:58:05 +00:00
|
|
|
} else {
|
2014-10-29 16:54:35 +00:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2015-09-01 04:29:23 +00:00
|
|
|
unsigned lineno, column;
|
2016-03-09 10:20:11 +00:00
|
|
|
JS::AutoFilename file;
|
2015-09-01 04:29:23 +00:00
|
|
|
if (JS::DescribeScriptedCaller(aCx, &file, &lineno, &column)) {
|
2014-10-10 16:58:05 +00:00
|
|
|
mScriptFile = file.get();
|
|
|
|
mScriptLine = lineno;
|
2015-09-01 04:29:23 +00:00
|
|
|
mScriptColumn = column;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2010-12-03 08:24:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
mIsServerSide = aIsServerSide;
|
|
|
|
|
2014-10-29 16:54:35 +00:00
|
|
|
// If we don't have aCx, we are window-less, so we don't have a
|
|
|
|
// inner-windowID. This can happen in sharedWorkers and ServiceWorkers or in
|
|
|
|
// DedicateWorkers created by JSM.
|
|
|
|
if (aCx) {
|
|
|
|
mInnerWindowID = nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(aCx);
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// parses the url
|
2014-10-10 16:58:05 +00:00
|
|
|
aRv = ParseURL(PromiseFlatString(aURL));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2016-04-20 22:04:36 +00:00
|
|
|
nsCOMPtr<nsIDocument> originDoc = mWebSocket->GetDocumentIfCurrent();
|
|
|
|
if (!originDoc) {
|
|
|
|
nsresult rv = mWebSocket->CheckInnerWindowCorrectness();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 16:15:30 +00:00
|
|
|
mOriginDocument = do_GetWeakReference(originDoc);
|
2016-05-19 15:06:04 +00:00
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
if (!mIsServerSide) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 10:04:11 +00:00
|
|
|
// The 'real' nsHttpChannel of the websocket gets opened in the parent.
|
|
|
|
// Since we don't serialize the CSP within child and parent we have to
|
|
|
|
// perform the CSP check here instead of AsyncOpen2().
|
|
|
|
// Please note that websockets can't follow redirects, hence there is no
|
|
|
|
// need to perform a CSP check after redirects.
|
|
|
|
nsCOMPtr<nsIContentPolicy> cspService = do_GetService(CSPSERVICE_CONTRACTID);
|
|
|
|
int16_t shouldLoad = nsIContentPolicy::REJECT_REQUEST;
|
|
|
|
aRv = cspService->ShouldLoad(nsIContentPolicy::TYPE_WEBSOCKET,
|
|
|
|
uri,
|
|
|
|
nullptr, // aRequestOrigin not used within CSP
|
|
|
|
originDoc,
|
|
|
|
EmptyCString(), // aMimeTypeGuess
|
|
|
|
nullptr, // aExtra
|
|
|
|
aPrincipal,
|
|
|
|
&shouldLoad);
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_CP_REJECTED(shouldLoad)) {
|
2016-05-17 10:04:11 +00:00
|
|
|
// Disallowed by CSP
|
2016-06-01 09:35:33 +00:00
|
|
|
aRv.Throw(NS_ERROR_CONTENT_BLOCKED);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-19 15:06:04 +00:00
|
|
|
}
|
2015-07-10 16:15:30 +00:00
|
|
|
|
|
|
|
// 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.
|
2016-06-01 09:35:33 +00:00
|
|
|
if (!mIsServerSide && !mSecure && originDoc &&
|
|
|
|
originDoc->GetUpgradeInsecureRequests(false)) {
|
2015-07-10 16:15:30 +00:00
|
|
|
// 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;
|
|
|
|
|
2016-07-21 05:03:25 +00:00
|
|
|
const char16_t* params[] = { reportSpec.get(), u"wss" };
|
|
|
|
CSP_LogLocalizedStr(u"upgradeInsecureRequest",
|
2015-07-10 16:15:30 +00:00
|
|
|
params, ArrayLength(params),
|
|
|
|
EmptyString(), // aSourceFile
|
|
|
|
EmptyString(), // aScriptSample
|
|
|
|
0, // aLineNumber
|
|
|
|
0, // aColumnNumber
|
|
|
|
nsIScriptError::warningFlag, "CSP",
|
|
|
|
mInnerWindowID);
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Don't allow https:// to open ws://
|
2016-06-01 09:35:33 +00:00
|
|
|
if (!mIsServerSide && !mSecure &&
|
2012-09-10 13:48:14 +00:00
|
|
|
!Preferences::GetBool("network.websocket.allowInsecureFromHTTPS",
|
|
|
|
false)) {
|
|
|
|
// Confirmed we are opening plain ws:// and want to prevent this from a
|
2015-09-12 21:38:51 +00:00
|
|
|
// secure context (e.g. https).
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsCOMPtr<nsIURI> originURI;
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
// For workers, retrieve the URI from the WorkerPrivate
|
|
|
|
principal = mWorkerPrivate->GetPrincipal();
|
|
|
|
} else {
|
|
|
|
// Check the principal's uri to determine if we were loaded from https.
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject(GetEntryGlobal());
|
|
|
|
if (globalObject) {
|
|
|
|
principal = globalObject->PrincipalOrNull();
|
|
|
|
}
|
2016-03-11 11:56:05 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> innerWindow;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
bool isNullPrincipal = true;
|
|
|
|
if (principal) {
|
|
|
|
nsresult rv = principal->GetIsNullPrincipal(&isNullPrincipal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNullPrincipal) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!innerWindow) {
|
|
|
|
innerWindow = do_QueryInterface(globalObject);
|
|
|
|
if (NS_WARN_IF(!innerWindow)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> parentWindow =
|
|
|
|
innerWindow->GetScriptableParent();
|
|
|
|
if (NS_WARN_IF(!parentWindow)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> currentInnerWindow =
|
|
|
|
parentWindow->GetCurrentInnerWindow();
|
|
|
|
if (NS_WARN_IF(!currentInnerWindow)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are at the top. Let's see if we have an opener window.
|
|
|
|
if (innerWindow == currentInnerWindow) {
|
|
|
|
ErrorResult error;
|
|
|
|
parentWindow =
|
|
|
|
nsGlobalWindow::Cast(innerWindow)->GetOpenerWindow(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
error.SuppressException();
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentWindow) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentInnerWindow = parentWindow->GetCurrentInnerWindow();
|
|
|
|
if (NS_WARN_IF(!currentInnerWindow)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(currentInnerWindow != innerWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
innerWindow = currentInnerWindow;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document = innerWindow->GetExtantDoc();
|
|
|
|
if (NS_WARN_IF(!document)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
principal = document->NodePrincipal();
|
|
|
|
}
|
2015-09-12 21:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (principal) {
|
|
|
|
principal->GetURI(getter_AddRefs(originURI));
|
|
|
|
}
|
2016-03-11 11:56:05 +00:00
|
|
|
|
2015-09-12 21:38:51 +00:00
|
|
|
if (originURI) {
|
|
|
|
bool originIsHttps = false;
|
|
|
|
aRv = originURI->SchemeIs("https", &originIsHttps);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (originIsHttps) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2014-07-18 19:04:28 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-03 16:42:02 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Assign the sub protocol list and scan it for illegal values
|
|
|
|
for (uint32_t index = 0; index < aProtocolArray.Length(); ++index) {
|
|
|
|
for (uint32_t i = 0; i < aProtocolArray[index].Length(); ++i) {
|
2014-01-04 15:02:17 +00:00
|
|
|
if (aProtocolArray[index][i] < static_cast<char16_t>(0x0021) ||
|
2014-10-10 16:58:05 +00:00
|
|
|
aProtocolArray[index][i] > static_cast<char16_t>(0x007E)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-03 16:42:02 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
2014-05-22 03:48:50 +00:00
|
|
|
mRequestedProtocolList.AppendLiteral(", ");
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF16toUTF8(aProtocolArray[index], mRequestedProtocolList);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the constructor should throw a SYNTAX_ERROR only if it fails to parse the
|
2014-10-10 16:58:05 +00:00
|
|
|
// url parameter, so don't throw if InitializeConnection fails, and call
|
2012-09-10 13:48:14 +00:00
|
|
|
// onerror/onclose asynchronously
|
2015-05-14 22:43:06 +00:00
|
|
|
if (NS_FAILED(InitializeConnection(aPrincipal))) {
|
2014-10-10 16:58:05 +00:00
|
|
|
*aConnectionFailed = true;
|
|
|
|
} else {
|
|
|
|
*aConnectionFailed = false;
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
2015-10-26 15:29:28 +00:00
|
|
|
WebSocketImpl::AsyncOpen(nsIPrincipal* aPrincipal, uint64_t aInnerWindowID,
|
2016-06-01 09:35:33 +00:00
|
|
|
nsITransportProvider* aTransportProvider,
|
|
|
|
const nsACString& aNegotiatedExtensions,
|
2015-10-26 15:29:28 +00:00
|
|
|
ErrorResult& aRv)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2016-06-01 09:35:33 +00:00
|
|
|
MOZ_ASSERT_IF(!aTransportProvider, aNegotiatedExtensions.IsEmpty());
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
nsCString asciiOrigin;
|
2015-05-14 22:43:06 +00:00
|
|
|
aRv = nsContentUtils::GetASCIIOrigin(aPrincipal, asciiOrigin);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
if (aTransportProvider) {
|
|
|
|
aRv = mChannel->SetServerParameters(aTransportProvider, aNegotiatedExtensions);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
ToLowerCase(asciiOrigin);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-06-01 09:35:33 +00:00
|
|
|
if (!aTransportProvider) {
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), mURI);
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2015-10-26 15:29:28 +00:00
|
|
|
aRv = mChannel->AsyncOpen(uri, asciiOrigin, aInnerWindowID, this, nullptr);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2016-05-17 10:04:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_CONTENT_BLOCKED);
|
2014-10-10 16:58:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-10-28 19:11:05 +00:00
|
|
|
|
|
|
|
mInnerWindowID = aInnerWindowID;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl methods:
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsAutoCloseWS final
|
2011-10-20 08:27:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 16:56:43 +00:00
|
|
|
explicit nsAutoCloseWS(WebSocketImpl* aWebSocketImpl)
|
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
2011-10-20 08:27:13 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
~nsAutoCloseWS()
|
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
if (!mWebSocketImpl->mChannel) {
|
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_INTERNAL_ERROR);
|
2011-10-20 08:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> mWebSocketImpl;
|
2011-10-20 08:27:13 +00:00
|
|
|
};
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
nsresult
|
2015-05-14 22:43:06 +00:00
|
|
|
WebSocketImpl::InitializeConnection(nsIPrincipal* aPrincipal)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(!mChannel, "mChannel should be null");
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
nsCOMPtr<nsIWebSocketChannel> wsChannel;
|
|
|
|
nsAutoCloseWS autoClose(this);
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
if (mSecure) {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=wss", &rv);
|
|
|
|
} else {
|
|
|
|
wsChannel =
|
|
|
|
do_CreateInstance("@mozilla.org/network/protocol;1?name=ws", &rv);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// add ourselves to the document's load group and
|
|
|
|
// provide the http stack the loadgroup info too
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (loadGroup) {
|
|
|
|
rv = wsChannel->SetLoadGroup(loadGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = loadGroup->AddRequest(this, nullptr);
|
2011-10-20 08:27:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-17 07:12:26 +00:00
|
|
|
|
|
|
|
mWeakLoadGroup = do_GetWeakReference(loadGroup);
|
2011-10-20 08:27:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 20:26:17 +00:00
|
|
|
// manually adding loadinfo to the channel since it
|
|
|
|
// was not set during channel creation.
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mOriginDocument);
|
2014-11-11 22:47:13 +00:00
|
|
|
|
|
|
|
// mOriginDocument has to be release on main-thread because WeakReferences
|
|
|
|
// are not thread-safe.
|
|
|
|
mOriginDocument = nullptr;
|
|
|
|
|
2016-06-21 11:31:56 +00:00
|
|
|
|
|
|
|
// The TriggeringPrincipal for websockets must always be a script.
|
|
|
|
// Let's make sure that the doc's principal (if a doc exists)
|
|
|
|
// and aPrincipal are same origin.
|
|
|
|
MOZ_ASSERT(!doc || doc->NodePrincipal()->Equals(aPrincipal));
|
|
|
|
|
2015-02-17 18:09:58 +00:00
|
|
|
wsChannel->InitLoadInfo(doc ? doc->AsDOMNode() : nullptr,
|
2015-05-14 22:43:06 +00:00
|
|
|
doc ? doc->NodePrincipal() : aPrincipal,
|
|
|
|
aPrincipal,
|
2016-05-17 10:04:11 +00:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-02-17 18:09:58 +00:00
|
|
|
nsIContentPolicy::TYPE_WEBSOCKET);
|
2014-08-13 20:26:17 +00:00
|
|
|
|
2011-10-20 08:27:13 +00:00
|
|
|
if (!mRequestedProtocolList.IsEmpty()) {
|
|
|
|
rv = wsChannel->SetProtocol(mRequestedProtocolList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-08-02 23:55:17 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> rr = do_QueryInterface(wsChannel);
|
|
|
|
NS_ENSURE_TRUE(rr, NS_ERROR_FAILURE);
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
rv = rr->RetargetDeliveryTo(this);
|
2011-10-20 08:27:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
mChannel = wsChannel;
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
void
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::DispatchConnectionCloseEvents()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
if (mDisconnectingOrDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
mWebSocket->SetReadyState(WebSocket::CLOSED);
|
2012-06-04 23:38:32 +00:00
|
|
|
|
2015-01-13 19:03:56 +00:00
|
|
|
// Let's keep the object alive because the webSocket can be CCed in the
|
|
|
|
// onerror or in the onclose callback.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocket> webSocket = mWebSocket;
|
2015-01-13 19:03:56 +00:00
|
|
|
|
2012-06-04 23:38:32 +00:00
|
|
|
// Call 'onerror' if needed
|
|
|
|
if (mFailed) {
|
2014-10-10 16:56:43 +00:00
|
|
|
nsresult rv =
|
2015-01-13 19:03:56 +00:00
|
|
|
webSocket->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the error event");
|
|
|
|
}
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2015-01-13 19:03:56 +00:00
|
|
|
nsresult rv = webSocket->CreateAndDispatchCloseEvent(mCloseEventWasClean,
|
|
|
|
mCloseEventCode,
|
|
|
|
mCloseEventReason);
|
2012-06-04 23:38:32 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the close event");
|
|
|
|
}
|
|
|
|
|
2015-01-13 19:03:56 +00:00
|
|
|
webSocket->UpdateMustKeepAlive();
|
2012-06-04 23:38:32 +00:00
|
|
|
Disconnect();
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2010-06-17 18:36:01 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocket::CreateAndDispatchSimpleEvent(const nsAString& aName)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 18:36:01 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
// it doesn't bubble, and it isn't cancelable
|
2015-11-13 00:09:42 +00:00
|
|
|
event->InitEvent(aName, false, false);
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::CreateAndDispatchMessageEvent(const nsACString& aData,
|
2014-10-10 16:58:05 +00:00
|
|
|
bool aIsBinary)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:56:43 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-10 16:58:19 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 16:58:19 +00:00
|
|
|
} else {
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(!mIsMainThread);
|
|
|
|
MOZ_ASSERT(mImpl->mWorkerPrivate);
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mImpl->mWorkerPrivate->GlobalScope()))) {
|
2014-10-10 16:58:19 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 20:02:28 +00:00
|
|
|
JSContext* cx = jsapi.cx();
|
2011-10-20 08:27:13 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 18:36:01 +00:00
|
|
|
return NS_OK;
|
2014-05-23 07:47:46 +00:00
|
|
|
}
|
2011-05-23 18:53:31 +00:00
|
|
|
|
2015-10-28 19:11:25 +00:00
|
|
|
uint16_t messageType = nsIWebSocketEventListener::TYPE_STRING;
|
|
|
|
|
2011-12-15 23:19:01 +00:00
|
|
|
// Create appropriate JS object for message
|
2016-02-23 20:02:28 +00:00
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (aIsBinary) {
|
2014-10-14 09:28:07 +00:00
|
|
|
if (mBinaryType == dom::BinaryType::Blob) {
|
2015-10-28 19:11:25 +00:00
|
|
|
messageType = nsIWebSocketEventListener::TYPE_BLOB;
|
|
|
|
|
2016-02-23 20:02:28 +00:00
|
|
|
nsresult rv = nsContentUtils::CreateBlobBuffer(cx, GetOwner(), aData,
|
2014-10-10 16:58:05 +00:00
|
|
|
&jsData);
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-10-14 09:28:07 +00:00
|
|
|
} else if (mBinaryType == dom::BinaryType::Arraybuffer) {
|
2015-10-28 19:11:25 +00:00
|
|
|
messageType = nsIWebSocketEventListener::TYPE_ARRAYBUFFER;
|
|
|
|
|
2016-02-23 20:02:28 +00:00
|
|
|
JS::Rooted<JSObject*> arrayBuf(cx);
|
|
|
|
nsresult rv = nsContentUtils::CreateArrayBuffer(cx, aData,
|
2014-10-10 16:58:05 +00:00
|
|
|
arrayBuf.address());
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-07-01 04:09:46 +00:00
|
|
|
jsData.setObject(*arrayBuf);
|
2011-12-15 23:19:01 +00:00
|
|
|
} else {
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_RUNTIMEABORT("Unknown binary type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
2013-05-22 16:05:28 +00:00
|
|
|
} else {
|
|
|
|
// JS string
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
JSString* jsString;
|
2016-02-23 20:02:28 +00:00
|
|
|
jsString = JS_NewUCStringCopyN(cx, utf16Data.get(), utf16Data.Length());
|
2013-05-22 16:05:28 +00:00
|
|
|
NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
|
|
|
|
|
2015-06-30 18:20:56 +00:00
|
|
|
jsData.setString(jsString);
|
2011-05-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 19:11:25 +00:00
|
|
|
mImpl->mService->WebSocketMessageAvailable(mImpl->mChannel->Serial(),
|
|
|
|
mImpl->mInnerWindowID,
|
|
|
|
aData, messageType);
|
|
|
|
|
2010-06-17 18:36:01 +00:00
|
|
|
// create an event that uses the MessageEvent interface,
|
|
|
|
// which does not bubble, is not cancelable, and has no default action
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessageEvent> event = NS_NewDOMMessageEvent(this, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
event->InitMessageEvent(nullptr, NS_LITERAL_STRING("message"), false, false,
|
|
|
|
jsData, mImpl->mUTF16Origin, EmptyString(), nullptr,
|
|
|
|
nullptr);
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2015-08-12 11:39:31 +00:00
|
|
|
return DispatchDOMEvent(nullptr, static_cast<Event*>(event), nullptr,
|
|
|
|
nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::CreateAndDispatchCloseEvent(bool aWasClean,
|
|
|
|
uint16_t aCode,
|
2015-10-28 19:11:05 +00:00
|
|
|
const nsAString& aReason)
|
2010-06-17 18:36:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2015-12-22 20:05:07 +00:00
|
|
|
if (mImpl->mChannel) {
|
|
|
|
mImpl->mService->WebSocketClosed(mImpl->mChannel->Serial(),
|
|
|
|
mImpl->mInnerWindowID,
|
|
|
|
aWasClean, aCode, aReason);
|
|
|
|
}
|
2015-10-28 19:11:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
2010-06-17 18:36:01 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-30 23:00:31 +00:00
|
|
|
CloseEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mWasClean = aWasClean;
|
|
|
|
init.mCode = aCode;
|
|
|
|
init.mReason = aReason;
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CloseEvent> event =
|
2014-06-30 23:00:31 +00:00
|
|
|
CloseEvent::Constructor(this, NS_LITERAL_STRING("close"), init);
|
2012-12-22 08:18:08 +00:00
|
|
|
event->SetTrusted(true);
|
2010-06-17 18:36:01 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2010-06-17 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::ParseURL(const nsAString& aURL)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_TRUE(!aURL.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2016-06-01 09:35:33 +00:00
|
|
|
if (mIsServerSide) {
|
|
|
|
mWebSocket->mURI = aURL;
|
|
|
|
CopyUTF16toUTF8(mWebSocket->mURI, mURI);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCOMPtr<nsIURL> parsedURL = do_QueryInterface(uri, &rv);
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2015-03-31 10:39:03 +00:00
|
|
|
bool hasRef;
|
|
|
|
rv = parsedURL->GetHasRef(&hasRef);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !hasRef,
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString scheme;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetScheme(scheme);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !scheme.IsEmpty(),
|
|
|
|
NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString host;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetAsciiHost(host);
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && !host.IsEmpty(), NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetPort(&port);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
rv = NS_CheckPortSafety(port, scheme.get());
|
2015-03-31 10:39:43 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SECURITY_ERR);
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString filePath;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetFilePath(filePath);
|
|
|
|
if (filePath.IsEmpty()) {
|
2014-05-26 18:55:08 +00:00
|
|
|
filePath.Assign('/');
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString query;
|
2010-06-17 18:34:24 +00:00
|
|
|
rv = parsedURL->GetQuery(query);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("ws")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mSecure = false;
|
2010-06-17 18:34:24 +00:00
|
|
|
mPort = (port == -1) ? DEFAULT_WS_SCHEME_PORT : port;
|
|
|
|
} else if (scheme.LowerCaseEqualsLiteral("wss")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mSecure = true;
|
2010-06-17 18:34:24 +00:00
|
|
|
mPort = (port == -1) ? DEFAULT_WSS_SCHEME_PORT : port;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2011-08-04 00:32:23 +00:00
|
|
|
rv = nsContentUtils::GetUTFOrigin(parsedURL, mUTF16Origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
mAsciiHost = host;
|
|
|
|
ToLowerCase(mAsciiHost);
|
|
|
|
|
|
|
|
mResource = filePath;
|
|
|
|
if (!query.IsEmpty()) {
|
2014-05-22 03:48:51 +00:00
|
|
|
mResource.Append('?');
|
2010-06-17 18:34:24 +00:00
|
|
|
mResource.Append(query);
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = mResource.Length();
|
|
|
|
uint32_t i;
|
2010-06-17 18:34:24 +00:00
|
|
|
for (i = 0; i < length; ++i) {
|
2014-01-04 15:02:17 +00:00
|
|
|
if (mResource[i] < static_cast<char16_t>(0x0021) ||
|
|
|
|
mResource[i] > static_cast<char16_t>(0x007E)) {
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
rv = parsedURL->GetSpec(mURI);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
2015-03-31 10:39:35 +00:00
|
|
|
CopyUTF8toUTF16(mURI, mWebSocket->mURI);
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-27 21:09:29 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-07-21 16:05:56 +00:00
|
|
|
// Methods that keep alive the WebSocket object when:
|
|
|
|
// 1. the object has registered event listeners that can be triggered
|
|
|
|
// ("strong event listeners");
|
|
|
|
// 2. there are outgoing not sent messages.
|
2010-06-27 21:09:29 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::UpdateMustKeepAlive()
|
2010-06-27 21:09:29 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
if (!mCheckMustKeepAlive || !mImpl) {
|
2010-06-27 21:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool shouldKeepAlive = false;
|
2014-11-11 22:47:13 +00:00
|
|
|
uint16_t readyState = ReadyState();
|
2010-07-21 16:05:56 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mListenerManager) {
|
2014-11-11 22:47:13 +00:00
|
|
|
switch (readyState)
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
case CONNECTING:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-10-29 04:33:51 +00:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onopen) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
case OPEN:
|
|
|
|
case CLOSING:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-10-29 04:33:51 +00:00
|
|
|
if (mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose) ||
|
2014-10-14 09:28:07 +00:00
|
|
|
mOutgoingBufferedAmount != 0) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldKeepAlive = true;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
case CLOSED:
|
2010-07-21 16:05:56 +00:00
|
|
|
{
|
2012-06-04 23:38:32 +00:00
|
|
|
shouldKeepAlive = false;
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-21 16:05:56 +00:00
|
|
|
|
|
|
|
if (mKeepingAlive && !shouldKeepAlive) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->ReleaseObject();
|
2010-07-21 16:05:56 +00:00
|
|
|
} else if (!mKeepingAlive && shouldKeepAlive) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = true;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->AddRefObject();
|
2010-07-21 16:05:56 +00:00
|
|
|
}
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-10 13:48:14 +00:00
|
|
|
WebSocket::DontKeepAliveAnyMore()
|
2010-06-27 21:09:29 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
// Here we could not have mImpl.
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2010-07-21 16:05:56 +00:00
|
|
|
if (mKeepingAlive) {
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mKeepingAlive = false;
|
2014-10-10 16:58:05 +00:00
|
|
|
mImpl->ReleaseObject();
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mCheckMustKeepAlive = false;
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
class WebSocketWorkerHolder final : public WorkerHolder
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-23 08:53:14 +00:00
|
|
|
explicit WebSocketWorkerHolder(WebSocketImpl* aWebSocketImpl)
|
2014-10-10 16:58:05 +00:00
|
|
|
: mWebSocketImpl(aWebSocketImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:28:14 +00:00
|
|
|
bool Notify(Status aStatus) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
|
|
|
|
|
|
|
if (aStatus >= Canceling) {
|
2015-02-17 17:40:09 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mWebSocketImpl->mMutex);
|
|
|
|
mWebSocketImpl->mWorkerShuttingDown = true;
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:38:02 +00:00
|
|
|
mWebSocketImpl->CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY,
|
2015-04-07 17:31:51 +00:00
|
|
|
EmptyCString());
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WebSocketImpl* mWebSocketImpl;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::AddRefObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocketImpl::ReleaseObject()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
bool
|
2016-06-23 08:53:14 +00:00
|
|
|
WebSocketImpl::RegisterWorkerHolder()
|
2014-11-11 22:47:13 +00:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-06-23 08:53:14 +00:00
|
|
|
MOZ_ASSERT(!mWorkerHolder);
|
|
|
|
mWorkerHolder = new WebSocketWorkerHolder(this);
|
2014-11-11 22:47:13 +00:00
|
|
|
|
2016-06-23 08:53:14 +00:00
|
|
|
if (NS_WARN_IF(!mWorkerHolder->HoldWorker(mWorkerPrivate))) {
|
|
|
|
mWorkerHolder = nullptr;
|
2014-12-12 19:37:07 +00:00
|
|
|
return false;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-06-23 08:53:14 +00:00
|
|
|
SetHasWorkerHolderRegistered(true);
|
2014-11-11 22:47:13 +00:00
|
|
|
#endif
|
2014-12-12 19:37:07 +00:00
|
|
|
|
|
|
|
return true;
|
2014-11-11 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
void
|
2016-06-23 08:53:14 +00:00
|
|
|
WebSocketImpl::UnregisterWorkerHolder()
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mDisconnectingOrDisconnected);
|
2014-10-10 16:58:05 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-06-23 08:53:14 +00:00
|
|
|
MOZ_ASSERT(mWorkerHolder);
|
|
|
|
|
|
|
|
// The DTOR of this WorkerHolder will release the worker for us.
|
|
|
|
mWorkerHolder = nullptr;
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
mWorkerPrivate = nullptr;
|
2014-11-11 22:47:13 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-06-23 08:53:14 +00:00
|
|
|
SetHasWorkerHolderRegistered(false);
|
2014-11-11 22:47:13 +00:00
|
|
|
#endif
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-15 23:23:00 +00:00
|
|
|
nsresult
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::UpdateURI()
|
2011-12-15 23:23:00 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-12-15 23:23:00 +00:00
|
|
|
// Check for Redirections
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BaseWebSocketChannel> channel;
|
2014-04-17 18:53:58 +00:00
|
|
|
channel = static_cast<BaseWebSocketChannel*>(mChannel.get());
|
|
|
|
MOZ_ASSERT(channel);
|
2011-12-15 23:23:00 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
channel->GetEffectiveURL(mWebSocket->mEffectiveURL);
|
2014-04-17 18:53:58 +00:00
|
|
|
mSecure = channel->IsEncrypted();
|
2011-12-15 23:23:00 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-16 21:16:08 +00:00
|
|
|
void
|
2013-05-23 11:41:32 +00:00
|
|
|
WebSocket::EventListenerAdded(nsIAtom* aType)
|
2013-04-16 21:16:08 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 11:41:32 +00:00
|
|
|
UpdateMustKeepAlive();
|
2010-06-27 21:09:29 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 21:16:08 +00:00
|
|
|
void
|
2013-05-23 11:41:32 +00:00
|
|
|
WebSocket::EventListenerRemoved(nsIAtom* aType)
|
2013-04-16 21:16:08 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2013-05-23 11:41:32 +00:00
|
|
|
UpdateMustKeepAlive();
|
2013-04-16 21:16:08 +00:00
|
|
|
}
|
2013-05-23 11:41:32 +00:00
|
|
|
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-09-10 13:48:14 +00:00
|
|
|
// WebSocket - methods
|
2010-06-17 18:34:24 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-10 16:56:43 +00:00
|
|
|
// webIDL: readonly attribute unsigned short readyState;
|
|
|
|
uint16_t
|
2014-10-14 09:28:07 +00:00
|
|
|
WebSocket::ReadyState()
|
2014-10-10 16:56:43 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WebSocket::SetReadyState(uint16_t aReadyState)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mReadyState = aReadyState;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: readonly attribute unsigned long bufferedAmount;
|
|
|
|
uint32_t
|
|
|
|
WebSocket::BufferedAmount() const
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mOutgoingBufferedAmount;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
dom::BinaryType
|
|
|
|
WebSocket::BinaryType() const
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
return mBinaryType;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// webIDL: attribute BinaryType binaryType;
|
|
|
|
void
|
|
|
|
WebSocket::SetBinaryType(dom::BinaryType aData)
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
mBinaryType = aData;
|
2014-10-10 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString url
|
|
|
|
void
|
|
|
|
WebSocket::GetUrl(nsAString& aURL)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
if (mEffectiveURL.IsEmpty()) {
|
2015-03-31 10:39:35 +00:00
|
|
|
aURL = mURI;
|
2014-10-14 09:28:07 +00:00
|
|
|
} else {
|
|
|
|
aURL = mEffectiveURL;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString extensions;
|
|
|
|
void
|
|
|
|
WebSocket::GetExtensions(nsAString& aExtensions)
|
2011-08-04 03:46:13 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedExtensions, aExtensions);
|
2011-08-04 03:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: readonly attribute DOMString protocol;
|
|
|
|
void
|
|
|
|
WebSocket::GetProtocol(nsAString& aProtocol)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
CopyUTF8toUTF16(mEstablishedProtocol, aProtocol);
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: void send(DOMString data);
|
|
|
|
void
|
|
|
|
WebSocket::Send(const nsAString& aData,
|
|
|
|
ErrorResult& aRv)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
NS_ConvertUTF16toUTF8 msgString(aData);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, msgString.Length(), false, aRv);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2015-05-12 12:09:51 +00:00
|
|
|
WebSocket::Send(Blob& aData, ErrorResult& aRv)
|
2011-12-15 23:19:01 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCOMPtr<nsIInputStream> msgStream;
|
2015-05-19 14:36:37 +00:00
|
|
|
aData.GetInternalStream(getter_AddRefs(msgStream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())){
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
uint64_t msgLength = aData.GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())){
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
if (msgLength > UINT32_MAX) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(msgStream, EmptyCString(), msgLength, true, aRv);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2013-08-05 17:40:01 +00:00
|
|
|
WebSocket::Send(const ArrayBuffer& aData,
|
2012-09-10 13:48:14 +00:00
|
|
|
ErrorResult& aRv)
|
2011-08-03 19:15:25 +00:00
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 19:15:25 +00:00
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2012-09-10 13:48:14 +00:00
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2013-08-05 17:40:01 +00:00
|
|
|
WebSocket::Send(const ArrayBufferView& aData,
|
2012-09-10 13:48:14 +00:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-14 09:28:07 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-12-15 23:19:01 +00:00
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsDependentCSubstring msgString(data, len);
|
2014-10-14 09:28:07 +00:00
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
void
|
2014-10-14 09:28:07 +00:00
|
|
|
WebSocket::Send(nsIInputStream* aMsgStream,
|
|
|
|
const nsACString& aMsgString,
|
|
|
|
uint32_t aMsgLength,
|
|
|
|
bool aIsBinary,
|
|
|
|
ErrorResult& aRv)
|
2011-12-15 23:19:01 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CONNECTING) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// Always increment outgoing buffer len, even if closed
|
|
|
|
mOutgoingBufferedAmount += aMsgLength;
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
// We must have mImpl when connected.
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
MOZ_ASSERT(readyState == OPEN, "Unknown state in WebSocket::Send");
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsresult rv;
|
|
|
|
if (aMsgStream) {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendBinaryStream(aMsgStream, aMsgLength);
|
2012-09-10 13:48:14 +00:00
|
|
|
} else {
|
|
|
|
if (aIsBinary) {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendBinaryMsg(aMsgString);
|
2012-09-10 13:48:14 +00:00
|
|
|
} else {
|
2014-10-14 09:28:07 +00:00
|
|
|
rv = mImpl->mChannel->SendMsg(aMsgString);
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-15 23:19:01 +00:00
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
UpdateMustKeepAlive();
|
2011-12-15 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
// webIDL: void close(optional unsigned short code, optional DOMString reason):
|
|
|
|
void
|
|
|
|
WebSocket::Close(const Optional<uint16_t>& aCode,
|
|
|
|
const Optional<nsAString>& aReason,
|
|
|
|
ErrorResult& aRv)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
2011-08-03 19:15:25 +00:00
|
|
|
|
|
|
|
// the reason code is optional, but if provided it must be in a specific range
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t closeCode = 0;
|
2012-09-10 13:48:14 +00:00
|
|
|
if (aCode.WasPassed()) {
|
|
|
|
if (aCode.Value() != 1000 && (aCode.Value() < 3000 || aCode.Value() > 4999)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
2012-02-01 04:41:43 +00:00
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
closeCode = aCode.Value();
|
2011-08-03 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:48:14 +00:00
|
|
|
nsCString closeReason;
|
|
|
|
if (aReason.WasPassed()) {
|
|
|
|
CopyUTF16toUTF8(aReason.Value(), closeReason);
|
2011-08-03 19:15:25 +00:00
|
|
|
|
|
|
|
// The API requires the UTF-8 string to be 123 or less bytes
|
2012-02-01 04:41:43 +00:00
|
|
|
if (closeReason.Length() > 123) {
|
2012-09-10 13:48:14 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2012-02-01 04:41:43 +00:00
|
|
|
}
|
2011-08-03 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
int64_t readyState = ReadyState();
|
2014-10-14 09:28:07 +00:00
|
|
|
if (readyState == CLOSING ||
|
|
|
|
readyState == CLOSED) {
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
// If the webSocket is not closed we MUST have a mImpl.
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
|
|
|
if (readyState == CONNECTING) {
|
|
|
|
mImpl->FailConnection(closeCode, closeReason);
|
2012-09-10 13:48:14 +00:00
|
|
|
return;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
MOZ_ASSERT(readyState == OPEN);
|
|
|
|
mImpl->CloseConnection(closeCode, closeReason);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 22:57:10 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIObserver
|
2012-02-21 22:57:10 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2012-02-21 22:57:10 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if ((readyState == WebSocket::CLOSING) ||
|
|
|
|
(readyState == WebSocket::CLOSED)) {
|
2012-02-21 22:57:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aSubject);
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!mWebSocket->GetOwner() || window != mWebSocket->GetOwner()) {
|
2012-02-21 22:57:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((strcmp(aTopic, DOM_WINDOW_FROZEN_TOPIC) == 0) ||
|
|
|
|
(strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC) == 0))
|
|
|
|
{
|
2012-06-04 23:38:32 +00:00
|
|
|
CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2012-02-21 22:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-10 16:56:43 +00:00
|
|
|
// WebSocketImpl::nsIRequest
|
2011-05-22 01:27:52 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetName(nsACString& aName)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-03-31 10:39:35 +00:00
|
|
|
CopyUTF16toUTF8(mWebSocket->mURI, aName);
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::IsPending(bool* aValue)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
*aValue = (readyState != WebSocket::CLOSED);
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetStatus(nsresult* aStatus)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnTargetThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
*aStatus = NS_OK;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-06-08 05:12:05 +00:00
|
|
|
class CancelRunnable final : public MainThreadWorkerRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CancelRunnable(WorkerPrivate* aWorkerPrivate, WebSocketImpl* aImpl)
|
2016-06-08 05:12:05 +00:00
|
|
|
: MainThreadWorkerRunnable(aWorkerPrivate)
|
2014-10-10 16:58:05 +00:00
|
|
|
, mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:05:39 +00:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return !NS_FAILED(mImpl->CancelInternal());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> mImpl;
|
2014-10-10 16:58:05 +00:00
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
2011-08-07 13:44:19 +00:00
|
|
|
// Window closed, stop/reload button pressed, user navigated away from page, etc.
|
2010-06-17 18:34:24 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Cancel(nsresult aStatus)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (!mIsMainThread) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<CancelRunnable> runnable =
|
2014-10-10 16:58:05 +00:00
|
|
|
new CancelRunnable(mWorkerPrivate, this);
|
2016-02-26 20:23:12 +00:00
|
|
|
if (!runnable->Dispatch()) {
|
2014-10-10 16:58:05 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CancelInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WebSocketImpl::CancelInternal()
|
|
|
|
{
|
|
|
|
AssertIsOnTargetThread();
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2014-11-07 14:35:54 +00:00
|
|
|
// If CancelInternal is called by a runnable, we may already be disconnected
|
|
|
|
// by the time it runs.
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mDisconnectingOrDisconnected) {
|
2014-11-07 14:35:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
int64_t readyState = mWebSocket->ReadyState();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (readyState == WebSocket::CLOSING || readyState == WebSocket::CLOSED) {
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_OK;
|
2012-06-04 23:38:32 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
2012-02-01 04:41:43 +00:00
|
|
|
return CloseConnection(nsIWebSocketChannel::CLOSE_GOING_AWAY);
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Suspend()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::Resume()
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2010-06-17 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetLoadGroup(nsILoadGroup** aLoadGroup)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aLoadGroup = nullptr;
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mIsMainThread) {
|
2016-04-20 22:04:36 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = mWebSocket->GetDocumentIfCurrent();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (doc) {
|
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-22 01:27:52 +00:00
|
|
|
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsPIDOMWindowInner* window = wp->GetWindow();
|
2014-10-10 16:58:05 +00:00
|
|
|
if (!window) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
2011-05-22 01:27:52 +00:00
|
|
|
if (doc) {
|
2014-03-15 19:00:15 +00:00
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
2011-05-22 01:27:52 +00:00
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::SetLoadGroup(nsILoadGroup* aLoadGroup)
|
2011-05-22 01:27:52 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
2011-05-22 01:27:52 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-06-17 18:34:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::GetLoadFlags(nsLoadFlags* aLoadFlags)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
*aLoadFlags = nsIRequest::LOAD_BACKGROUND;
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-10 16:56:43 +00:00
|
|
|
WebSocketImpl::SetLoadFlags(nsLoadFlags aLoadFlags)
|
2010-06-17 18:34:24 +00:00
|
|
|
{
|
2014-10-10 16:58:05 +00:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-05-22 01:27:52 +00:00
|
|
|
// we won't change the load flags at all.
|
2010-06-17 18:34:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-10 13:48:14 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class WorkerRunnableDispatcher final : public WorkerRunnable
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WebSocketImpl> mWebSocketImpl;
|
2015-02-17 17:40:09 +00:00
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
public:
|
2015-02-17 17:40:09 +00:00
|
|
|
WorkerRunnableDispatcher(WebSocketImpl* aImpl, WorkerPrivate* aWorkerPrivate,
|
2016-06-01 00:04:54 +00:00
|
|
|
already_AddRefed<nsIRunnable> aEvent)
|
2014-10-10 16:58:05 +00:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2015-02-17 17:40:09 +00:00
|
|
|
, mWebSocketImpl(aImpl)
|
2016-06-01 00:04:54 +00:00
|
|
|
, mEvent(Move(aEvent))
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:05:39 +00:00
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2015-02-17 17:40:09 +00:00
|
|
|
|
|
|
|
// No messages when disconnected.
|
|
|
|
if (mWebSocketImpl->mDisconnectingOrDisconnected) {
|
|
|
|
NS_WARNING("Dispatching a WebSocket event after the disconnection!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
return !NS_FAILED(mEvent->Run());
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:05:39 +00:00
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-02-25 21:05:39 +00:00
|
|
|
PreDispatch(WorkerPrivate* aWorkerPrivate) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2016-02-18 23:02:51 +00:00
|
|
|
// We don't call WorkerRunnable::PreDispatch because it would assert the
|
|
|
|
// wrong thing about which thread we're on. We're on whichever thread the
|
|
|
|
// channel implementation is running on (probably the main thread or socket
|
|
|
|
// transport thread).
|
2014-10-10 16:58:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-02-26 20:23:12 +00:00
|
|
|
PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2016-02-18 23:02:51 +00:00
|
|
|
// We don't call WorkerRunnable::PreDispatch because it would assert the
|
|
|
|
// wrong thing about which thread we're on. We're on whichever thread the
|
|
|
|
// channel implementation is running on (probably the main thread or socket
|
|
|
|
// transport thread).
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIRunnable> mEvent;
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2014-10-10 16:58:05 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-10 03:21:46 +00:00
|
|
|
WebSocketImpl::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event(aEvent);
|
|
|
|
return Dispatch(event.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-06-01 00:04:54 +00:00
|
|
|
WebSocketImpl::Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags)
|
2014-10-10 16:58:05 +00:00
|
|
|
{
|
2015-07-10 03:21:46 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event_ref(aEvent);
|
2014-10-10 16:58:05 +00:00
|
|
|
// If the target is the main-thread we can just dispatch the runnable.
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mIsMainThread) {
|
2015-07-10 03:21:46 +00:00
|
|
|
return NS_DispatchToMainThread(event_ref.forget());
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:40:09 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
2014-12-12 19:37:07 +00:00
|
|
|
if (mWorkerShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
2014-11-11 22:47:13 +00:00
|
|
|
#ifdef DEBUG
|
2016-06-23 08:53:14 +00:00
|
|
|
MOZ_ASSERT(HasWorkerHolderRegistered());
|
2014-11-11 22:47:13 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-18 22:38:02 +00:00
|
|
|
// If the target is a worker, we have to use a custom WorkerRunnableDispatcher
|
|
|
|
// runnable.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<WorkerRunnableDispatcher> event =
|
2015-07-10 03:21:46 +00:00
|
|
|
new WorkerRunnableDispatcher(this, mWorkerPrivate, event_ref.forget());
|
2015-03-18 22:38:02 +00:00
|
|
|
|
2016-02-26 20:23:12 +00:00
|
|
|
if (!event->Dispatch()) {
|
2014-10-10 16:58:05 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:15:43 +00:00
|
|
|
NS_IMETHODIMP
|
2016-06-01 00:04:54 +00:00
|
|
|
WebSocketImpl::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t)
|
2016-05-12 22:15:43 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-10-10 16:58:05 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WebSocketImpl::IsOnCurrentThread(bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = IsTargetThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WebSocketImpl::IsTargetThread() const
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
return NS_IsMainThread() == mIsMainThread;
|
2014-10-10 16:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:28:07 +00:00
|
|
|
void
|
|
|
|
WebSocket::AssertIsOnTargetThread() const
|
|
|
|
{
|
2014-12-12 19:37:07 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
|
2014-10-14 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|