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