2015-03-25 17:35:59 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "TCPSocket.h"
|
2015-03-25 17:36:24 +03:00
|
|
|
#include "TCPServerSocket.h"
|
2015-03-25 17:36:56 +03:00
|
|
|
#include "TCPSocketChild.h"
|
2015-03-25 17:35:59 +03:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/TCPSocketBinding.h"
|
|
|
|
#include "mozilla/dom/TCPSocketErrorEvent.h"
|
|
|
|
#include "mozilla/dom/TCPSocketErrorEventBinding.h"
|
|
|
|
#include "mozilla/dom/TCPSocketEvent.h"
|
|
|
|
#include "mozilla/dom/TCPSocketEventBinding.h"
|
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
|
|
|
#include "nsIArrayBufferInputStream.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsIAsyncStreamCopier.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIBinaryInputStream.h"
|
|
|
|
#include "nsIScriptableInputStream.h"
|
|
|
|
#include "nsIInputStreamPump.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsINSSErrorsService.h"
|
|
|
|
#include "nsISSLSocketControl.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
#include "sslerr.h"
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "nsINetworkStatsServiceProxy.h"
|
|
|
|
#include "nsINetworkManager.h"
|
|
|
|
#include "nsINetworkInterface.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 65536
|
|
|
|
#define NETWORK_STATS_THRESHOLD 65536
|
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(LegacyMozTCPSocket, mGlobal)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(LegacyMozTCPSocket)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(LegacyMozTCPSocket)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LegacyMozTCPSocket)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
LegacyMozTCPSocket::LegacyMozTCPSocket(nsPIDOMWindow* aWindow)
|
|
|
|
: mGlobal(do_QueryInterface(aWindow))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LegacyMozTCPSocket::~LegacyMozTCPSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TCPSocket>
|
|
|
|
LegacyMozTCPSocket::Open(const nsAString& aHost,
|
|
|
|
uint16_t aPort,
|
|
|
|
const SocketOptions& aOptions,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoJSAPI api;
|
|
|
|
if (NS_WARN_IF(!api.Init(mGlobal))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
GlobalObject globalObj(api.cx(), mGlobal->GetGlobalJSObject());
|
|
|
|
return TCPSocket::Constructor(globalObj, aHost, aPort, aOptions, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
already_AddRefed<TCPServerSocket>
|
|
|
|
LegacyMozTCPSocket::Listen(uint16_t aPort,
|
|
|
|
const ServerSocketOptions& aOptions,
|
|
|
|
uint16_t aBacklog,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoJSAPI api;
|
|
|
|
if (NS_WARN_IF(!api.Init(mGlobal))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
GlobalObject globalObj(api.cx(), mGlobal->GetGlobalJSObject());
|
|
|
|
return TCPServerSocket::Constructor(globalObj, aPort, aOptions, aBacklog, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
bool
|
|
|
|
LegacyMozTCPSocket::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
|
|
|
{
|
|
|
|
return LegacyMozTCPSocketBinding::Wrap(aCx, this, aGivenProto, aReflector);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(TCPSocket)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(TCPSocket,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(TCPSocket,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTransport)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocketInputStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocketOutputStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputStreamPump)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputStreamScriptable)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputStreamBinary)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMultiplexStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMultiplexStreamCopier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPendingDataAfterStartTLS)
|
2015-03-25 17:36:56 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocketBridgeChild)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSocketBridgeParent)
|
2015-03-25 17:35:59 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(TCPSocket,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTransport)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocketInputStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocketOutputStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputStreamPump)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputStreamScriptable)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputStreamBinary)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMultiplexStream)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMultiplexStreamCopier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPendingDataAfterStartTLS)
|
2015-03-25 17:36:56 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocketBridgeChild)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSocketBridgeParent)
|
2015-03-25 17:35:59 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(TCPSocket, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TCPSocket, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TCPSocket)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITransportEventSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITCPSocketCallback)
|
2015-03-25 17:35:59 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
TCPSocket::TCPSocket(nsIGlobalObject* aGlobal, const nsAString& aHost, uint16_t aPort,
|
|
|
|
bool aSsl, bool aUseArrayBuffers)
|
|
|
|
: DOMEventTargetHelper(aGlobal)
|
|
|
|
, mReadyState(TCPReadyState::Closed)
|
|
|
|
, mUseArrayBuffers(aUseArrayBuffers)
|
|
|
|
, mHost(aHost)
|
|
|
|
, mPort(aPort)
|
|
|
|
, mSsl(aSsl)
|
|
|
|
, mAsyncCopierActive(false)
|
|
|
|
, mWaitingForDrain(false)
|
|
|
|
, mInnerWindowID(0)
|
2015-03-25 17:36:56 +03:00
|
|
|
, mBufferedAmount(0)
|
2015-03-25 17:35:59 +03:00
|
|
|
, mSuspendCount(0)
|
2015-03-25 17:36:56 +03:00
|
|
|
, mTrackingNumber(0)
|
2015-03-25 17:35:59 +03:00
|
|
|
, mWaitingForStartTLS(false)
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
, mTxBytes(0)
|
|
|
|
, mRxBytes(0)
|
|
|
|
, mAppId(nsIScriptSecurityManager::UNKNOWN_APP_ID)
|
|
|
|
, mInBrowser(false)
|
|
|
|
#endif
|
|
|
|
{
|
2015-09-21 06:05:20 +03:00
|
|
|
if (aGlobal) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal);
|
|
|
|
if (window && window->IsOuterWindow()) {
|
|
|
|
window = window->GetCurrentInnerWindow();
|
|
|
|
}
|
|
|
|
if (window) {
|
|
|
|
mInnerWindowID = window->WindowID();
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocket::~TCPSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-03-25 17:36:24 +03:00
|
|
|
TCPSocket::CreateStream()
|
2015-03-25 17:35:59 +03:00
|
|
|
{
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult rv = mTransport->OpenInputStream(0, 0, 0, getter_AddRefs(mSocketInputStream));
|
2015-03-25 17:35:59 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mTransport->OpenOutputStream(nsITransport::OPEN_UNBUFFERED, 0, 0, getter_AddRefs(mSocketOutputStream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the other side is not listening, we will
|
|
|
|
// get an onInputStreamReady callback where available
|
|
|
|
// raises to indicate the connection was refused.
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mSocketInputStream);
|
|
|
|
NS_ENSURE_TRUE(asyncStream, NS_ERROR_NOT_AVAILABLE);
|
2015-03-25 17:36:24 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
rv = asyncStream->AsyncWait(this, nsIAsyncInputStream::WAIT_CLOSURE_ONLY, 0, mainThread);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mUseArrayBuffers) {
|
|
|
|
mInputStreamBinary = do_CreateInstance("@mozilla.org/binaryinputstream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mInputStreamBinary->SetInputStream(mSocketInputStream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
mInputStreamScriptable = do_CreateInstance("@mozilla.org/scriptableinputstream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mInputStreamScriptable->Init(mSocketInputStream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mMultiplexStream = do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mMultiplexStreamCopier = do_CreateInstance("@mozilla.org/network/async-stream-copier;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
nsCOMPtr<nsISocketTransportService> sts =
|
|
|
|
do_GetService("@mozilla.org/network/socket-transport-service;1");
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(sts);
|
|
|
|
rv = mMultiplexStreamCopier->Init(mMultiplexStream,
|
|
|
|
mSocketOutputStream,
|
|
|
|
target,
|
|
|
|
true, /* source buffered */
|
|
|
|
false, /* sink buffered */
|
|
|
|
BUFFER_SIZE,
|
|
|
|
false, /* close source */
|
|
|
|
false); /* close sink */
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-03-25 17:36:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
nsresult
|
|
|
|
TCPSocket::InitWithUnconnectedTransport(nsISocketTransport* aTransport)
|
|
|
|
{
|
|
|
|
mReadyState = TCPReadyState::Connecting;
|
|
|
|
mTransport = aTransport;
|
|
|
|
|
|
|
|
MOZ_ASSERT(XRE_GetProcessType() != GeckoProcessType_Content);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
mTransport->SetEventSink(this, mainThread);
|
|
|
|
|
|
|
|
nsresult rv = CreateStream();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult
|
|
|
|
TCPSocket::Init()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "inner-window-destroyed", true);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
2015-09-21 06:05:20 +03:00
|
|
|
mReadyState = TCPReadyState::Connecting;
|
2015-03-25 17:36:56 +03:00
|
|
|
mSocketBridgeChild = new TCPSocketChild(mHost, mPort);
|
|
|
|
mSocketBridgeChild->SendOpen(this, mSsl, mUseArrayBuffers);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
nsCOMPtr<nsISocketTransportService> sts =
|
|
|
|
do_GetService("@mozilla.org/network/socket-transport-service;1");
|
|
|
|
|
|
|
|
const char* socketTypes[1];
|
|
|
|
if (mSsl) {
|
|
|
|
socketTypes[0] = "ssl";
|
|
|
|
} else {
|
|
|
|
socketTypes[0] = "starttls";
|
|
|
|
}
|
2015-09-21 06:05:20 +03:00
|
|
|
nsCOMPtr<nsISocketTransport> transport;
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult rv = sts->CreateTransport(socketTypes, 1, NS_ConvertUTF16toUTF8(mHost), mPort,
|
2015-09-21 06:05:20 +03:00
|
|
|
nullptr, getter_AddRefs(transport));
|
2015-03-25 17:36:24 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
return InitWithUnconnectedTransport(transport);
|
2015-03-25 17:36:24 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPSocket::InitWithSocketChild(TCPSocketChild* aSocketBridge)
|
|
|
|
{
|
|
|
|
mSocketBridgeChild = aSocketBridge;
|
|
|
|
mReadyState = TCPReadyState::Open;
|
|
|
|
mSocketBridgeChild->SetSocket(this);
|
|
|
|
mSocketBridgeChild->GetHost(mHost);
|
|
|
|
mSocketBridgeChild->GetPort(&mPort);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult
|
|
|
|
TCPSocket::InitWithTransport(nsISocketTransport* aTransport)
|
|
|
|
{
|
|
|
|
mTransport = aTransport;
|
|
|
|
nsresult rv = CreateStream();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mReadyState = TCPReadyState::Open;
|
|
|
|
rv = CreateInputStreamPump();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
mTransport->GetHost(host);
|
|
|
|
mHost = NS_ConvertUTF8toUTF16(host);
|
|
|
|
int32_t port;
|
|
|
|
mTransport->GetPort(&port);
|
|
|
|
mPort = port;
|
2015-03-25 17:35:59 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
nsCOMPtr<nsINetworkManager> networkManager = do_GetService("@mozilla.org/network/manager;1");
|
|
|
|
if (networkManager) {
|
|
|
|
networkManager->GetActiveNetworkInfo(getter_AddRefs(mActiveNetworkInfo));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::UpgradeToSecure(mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mReadyState != TCPReadyState::Open) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSsl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSsl = true;
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
mSocketBridgeChild->SendStartTLS();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
uint32_t count = 0;
|
|
|
|
mMultiplexStream->GetCount(&count);
|
|
|
|
if (!count) {
|
|
|
|
ActivateTLS();
|
|
|
|
} else {
|
|
|
|
mWaitingForStartTLS = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class CopierCallbacks final : public nsIRequestObserver
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocket> mOwner;
|
2015-03-25 17:35:59 +03:00
|
|
|
public:
|
|
|
|
explicit CopierCallbacks(TCPSocket* aSocket) : mOwner(aSocket) {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
private:
|
|
|
|
~CopierCallbacks() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CopierCallbacks, nsIRequestObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CopierCallbacks::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CopierCallbacks::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
|
|
|
|
{
|
|
|
|
mOwner->NotifyCopyComplete(aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TCPSocket::EnsureCopying()
|
|
|
|
{
|
|
|
|
if (mAsyncCopierActive) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncCopierActive = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CopierCallbacks> callbacks = new CopierCallbacks(this);
|
2015-03-25 17:35:59 +03:00
|
|
|
return mMultiplexStreamCopier->AsyncCopy(callbacks, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::NotifyCopyComplete(nsresult aStatus)
|
|
|
|
{
|
|
|
|
mAsyncCopierActive = false;
|
|
|
|
mMultiplexStream->RemoveStream(0);
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeParent) {
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << mSocketBridgeParent->SendUpdateBufferedAmount(BufferedAmount(),
|
2015-03-25 17:36:56 +03:00
|
|
|
mTrackingNumber);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
MaybeReportErrorAndCloseIfOpen(aStatus);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t count;
|
|
|
|
nsresult rv = mMultiplexStream->GetCount(&count);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
EnsureCopying();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are waiting for initiating starttls, we can begin to
|
|
|
|
// activate tls now.
|
|
|
|
if (mWaitingForStartTLS && mReadyState == TCPReadyState::Open) {
|
|
|
|
ActivateTLS();
|
|
|
|
mWaitingForStartTLS = false;
|
|
|
|
// If we have pending data, we should send them, or fire
|
|
|
|
// a drain event if we are waiting for it.
|
|
|
|
if (!mPendingDataAfterStartTLS.IsEmpty()) {
|
|
|
|
while (!mPendingDataAfterStartTLS.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIInputStream> stream = mPendingDataAfterStartTLS[0];
|
|
|
|
mMultiplexStream->AppendStream(stream);
|
|
|
|
mPendingDataAfterStartTLS.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
EnsureCopying();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
// If we have a connected child, we let the child decide whether
|
|
|
|
// ondrain should be dispatched.
|
|
|
|
if (mWaitingForDrain && !mSocketBridgeParent) {
|
2015-03-25 17:35:59 +03:00
|
|
|
mWaitingForDrain = false;
|
|
|
|
FireEvent(NS_LITERAL_STRING("drain"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadyState == TCPReadyState::Closing) {
|
|
|
|
mSocketOutputStream->Close();
|
|
|
|
mReadyState = TCPReadyState::Closed;
|
|
|
|
FireEvent(NS_LITERAL_STRING("close"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::ActivateTLS()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> securityInfo;
|
|
|
|
mTransport->GetSecurityInfo(getter_AddRefs(securityInfo));
|
|
|
|
nsCOMPtr<nsISSLSocketControl> socketControl = do_QueryInterface(securityInfo);
|
|
|
|
if (socketControl) {
|
|
|
|
socketControl->StartTLS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocket::FireErrorEvent(const nsAString& aName, const nsAString& aType)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeParent) {
|
|
|
|
mSocketBridgeParent->FireErrorEvent(aName, aType, mReadyState);
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocketErrorEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mName = aName;
|
|
|
|
init.mMessage = aType;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocketErrorEvent> event =
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocketErrorEvent::Constructor(this, NS_LITERAL_STRING("error"), init);
|
2015-09-21 06:05:20 +03:00
|
|
|
MOZ_ASSERT(event);
|
2015-03-25 17:35:59 +03:00
|
|
|
event->SetTrusted(true);
|
|
|
|
bool dummy;
|
|
|
|
DispatchEvent(event, &dummy);
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocket::FireEvent(const nsAString& aType)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeParent) {
|
|
|
|
mSocketBridgeParent->FireEvent(aType, mReadyState);
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
AutoJSAPI api;
|
2015-09-22 15:45:00 +03:00
|
|
|
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> val(api.cx());
|
2015-09-21 06:05:20 +03:00
|
|
|
return FireDataEvent(api.cx(), aType, val);
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::FireDataArrayEvent(const nsAString& aType,
|
|
|
|
const InfallibleTArray<uint8_t>& buffer)
|
2015-09-21 06:05:20 +03:00
|
|
|
{
|
|
|
|
AutoJSAPI api;
|
2015-09-22 15:45:00 +03:00
|
|
|
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = api.cx();
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
|
|
|
|
bool ok = IPC::DeserializeArrayBuffer(cx, buffer, &val);
|
|
|
|
if (ok) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return FireDataEvent(cx, aType, val);
|
2015-09-21 06:05:20 +03:00
|
|
|
}
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-21 06:05:20 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::FireDataStringEvent(const nsAString& aType,
|
|
|
|
const nsACString& aString)
|
2015-09-21 06:05:20 +03:00
|
|
|
{
|
|
|
|
AutoJSAPI api;
|
2015-09-22 15:45:00 +03:00
|
|
|
if (NS_WARN_IF(!api.Init(GetOwnerGlobal()))) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = api.cx();
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
|
|
|
|
bool ok = ToJSValue(cx, NS_ConvertASCIItoUTF16(aString), &val);
|
|
|
|
if (ok) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return FireDataEvent(cx, aType, val);
|
2015-09-21 06:05:20 +03:00
|
|
|
}
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-21 06:05:20 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocket::FireDataEvent(JSContext* aCx, const nsAString& aType, JS::Handle<JS::Value> aData)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
MOZ_ASSERT(!mSocketBridgeParent);
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
RootedDictionary<TCPSocketEventInit> init(aCx);
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mData = aData;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocketEvent> event =
|
2015-03-25 17:35:59 +03:00
|
|
|
TCPSocketEvent::Constructor(this, aType, init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
bool dummy;
|
|
|
|
DispatchEvent(event, &dummy);
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
TCPSocket::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return TCPSocketBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::GetHost(nsAString& aHost)
|
|
|
|
{
|
|
|
|
aHost.Assign(mHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
TCPSocket::Port()
|
|
|
|
{
|
|
|
|
return mPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPSocket::Ssl()
|
|
|
|
{
|
|
|
|
return mSsl;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
TCPSocket::BufferedAmount()
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
return mBufferedAmount;
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
if (mMultiplexStream) {
|
|
|
|
uint64_t available = 0;
|
|
|
|
mMultiplexStream->Available(&available);
|
|
|
|
return available;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::Suspend()
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
mSocketBridgeChild->SendSuspend();
|
|
|
|
return;
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
if (mInputStreamPump) {
|
|
|
|
mInputStreamPump->Suspend();
|
|
|
|
}
|
|
|
|
mSuspendCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::Resume(mozilla::ErrorResult& aRv)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
mSocketBridgeChild->SendResume();
|
|
|
|
return;
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
if (!mSuspendCount) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInputStreamPump) {
|
|
|
|
mInputStreamPump->Resume();
|
|
|
|
}
|
|
|
|
mSuspendCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TCPSocket::MaybeReportErrorAndCloseIfOpen(nsresult status) {
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// Save network statistics once the connection is closed.
|
|
|
|
// For now this function is Gonk-specific.
|
|
|
|
SaveNetworkStats(true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If we're closed, we've already reported the error or just don't need to
|
|
|
|
// report the error.
|
|
|
|
if (mReadyState == TCPReadyState::Closed) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mReadyState = TCPReadyState::Closed;
|
|
|
|
|
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
// Convert the status code to an appropriate error message.
|
|
|
|
|
|
|
|
nsString errorType, errName;
|
|
|
|
|
|
|
|
// security module? (and this is an error)
|
|
|
|
if ((static_cast<uint32_t>(status) & 0xFF0000) == 0x5a0000) {
|
|
|
|
nsCOMPtr<nsINSSErrorsService> errSvc = do_GetService("@mozilla.org/nss_errors_service;1");
|
|
|
|
// getErrorClass will throw a generic NS_ERROR_FAILURE if the error code is
|
|
|
|
// somehow not in the set of covered errors.
|
|
|
|
uint32_t errorClass;
|
|
|
|
nsresult rv = errSvc->GetErrorClass(status, &errorClass);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
errorType.AssignLiteral("SecurityProtocol");
|
|
|
|
} else {
|
|
|
|
switch (errorClass) {
|
|
|
|
case nsINSSErrorsService::ERROR_CLASS_BAD_CERT:
|
|
|
|
errorType.AssignLiteral("SecurityCertificate");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorType.AssignLiteral("SecurityProtocol");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NSS_SEC errors (happen below the base value because of negative vals)
|
|
|
|
if ((static_cast<int32_t>(status) & 0xFFFF) < abs(nsINSSErrorsService::NSS_SEC_ERROR_BASE)) {
|
|
|
|
switch (static_cast<SECErrorCodes>(status)) {
|
|
|
|
case SEC_ERROR_EXPIRED_CERTIFICATE:
|
|
|
|
errName.AssignLiteral("SecurityExpiredCertificateError");
|
|
|
|
break;
|
|
|
|
case SEC_ERROR_REVOKED_CERTIFICATE:
|
|
|
|
errName.AssignLiteral("SecurityRevokedCertificateError");
|
|
|
|
break;
|
|
|
|
// per bsmith, we will be unable to tell these errors apart very soon,
|
|
|
|
// so it makes sense to just folder them all together already.
|
|
|
|
case SEC_ERROR_UNKNOWN_ISSUER:
|
|
|
|
case SEC_ERROR_UNTRUSTED_ISSUER:
|
|
|
|
case SEC_ERROR_UNTRUSTED_CERT:
|
|
|
|
case SEC_ERROR_CA_CERT_INVALID:
|
|
|
|
errName.AssignLiteral("SecurityUntrustedCertificateIssuerError");
|
|
|
|
break;
|
|
|
|
case SEC_ERROR_INADEQUATE_KEY_USAGE:
|
|
|
|
errName.AssignLiteral("SecurityInadequateKeyUsageError");
|
|
|
|
break;
|
|
|
|
case SEC_ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED:
|
|
|
|
errName.AssignLiteral("SecurityCertificateSignatureAlgorithmDisabledError");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errName.AssignLiteral("SecurityError");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// NSS_SSL errors
|
|
|
|
switch (static_cast<SSLErrorCodes>(status)) {
|
|
|
|
case SSL_ERROR_NO_CERTIFICATE:
|
|
|
|
errName.AssignLiteral("SecurityNoCertificateError");
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_BAD_CERTIFICATE:
|
|
|
|
errName.AssignLiteral("SecurityBadCertificateError");
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_UNSUPPORTED_CERTIFICATE_TYPE:
|
|
|
|
errName.AssignLiteral("SecurityUnsupportedCertificateTypeError");
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_UNSUPPORTED_VERSION:
|
|
|
|
errName.AssignLiteral("SecurityUnsupportedTLSVersionError");
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_BAD_CERT_DOMAIN:
|
|
|
|
errName.AssignLiteral("SecurityCertificateDomainMismatchError");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errName.AssignLiteral("SecurityError");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// must be network
|
|
|
|
errorType.AssignLiteral("Network");
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
// connect to host:port failed
|
|
|
|
case NS_ERROR_CONNECTION_REFUSED:
|
|
|
|
errName.AssignLiteral("ConnectionRefusedError");
|
|
|
|
break;
|
|
|
|
// network timeout error
|
|
|
|
case NS_ERROR_NET_TIMEOUT:
|
|
|
|
errName.AssignLiteral("NetworkTimeoutError");
|
|
|
|
break;
|
|
|
|
// hostname lookup failed
|
|
|
|
case NS_ERROR_UNKNOWN_HOST:
|
|
|
|
errName.AssignLiteral("DomainNotFoundError");
|
|
|
|
break;
|
|
|
|
case NS_ERROR_NET_INTERRUPT:
|
|
|
|
errName.AssignLiteral("NetworkInterruptError");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errName.AssignLiteral("NetworkError");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(FireErrorEvent(errName, errorType)));
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
return FireEvent(NS_LITERAL_STRING("close"));
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::Close()
|
|
|
|
{
|
|
|
|
if (mReadyState == TCPReadyState::Closed || mReadyState == TCPReadyState::Closing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadyState = TCPReadyState::Closing;
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
mSocketBridgeChild->SendClose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
uint32_t count = 0;
|
|
|
|
mMultiplexStream->GetCount(&count);
|
|
|
|
if (!count) {
|
|
|
|
mSocketOutputStream->Close();
|
|
|
|
}
|
|
|
|
mSocketInputStream->Close();
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPSocket::SendWithTrackingNumber(const nsACString& aData,
|
|
|
|
const uint32_t& aTrackingNumber,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSocketBridgeParent);
|
|
|
|
mTrackingNumber = aTrackingNumber;
|
|
|
|
// The JSContext isn't necessary for string values; it's a codegen limitation.
|
|
|
|
Send(nullptr, aData, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
bool
|
2015-03-25 17:36:56 +03:00
|
|
|
TCPSocket::Send(JSContext* aCx, const nsACString& aData, mozilla::ErrorResult& aRv)
|
2015-03-25 17:35:59 +03:00
|
|
|
{
|
|
|
|
if (mReadyState != TCPReadyState::Open) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t byteLength;
|
2015-03-25 17:36:56 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
mSocketBridgeChild->SendSend(aData, ++mTrackingNumber);
|
|
|
|
byteLength = aData.Length();
|
|
|
|
} else {
|
|
|
|
nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), aData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rv = stream->Available(&byteLength);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
return Send(stream, byteLength);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPSocket::SendWithTrackingNumber(JSContext* aCx,
|
|
|
|
const ArrayBuffer& aData,
|
|
|
|
uint32_t aByteOffset,
|
|
|
|
const Optional<uint32_t>& aByteLength,
|
|
|
|
const uint32_t& aTrackingNumber,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSocketBridgeParent);
|
|
|
|
mTrackingNumber = aTrackingNumber;
|
|
|
|
Send(aCx, aData, aByteOffset, aByteLength, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
bool
|
2015-03-25 17:36:56 +03:00
|
|
|
TCPSocket::Send(JSContext* aCx,
|
|
|
|
const ArrayBuffer& aData,
|
2015-03-25 17:35:59 +03:00
|
|
|
uint32_t aByteOffset,
|
|
|
|
const Optional<uint32_t>& aByteLength,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mReadyState != TCPReadyState::Open) {
|
2015-03-25 17:35:59 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
nsCOMPtr<nsIArrayBufferInputStream> stream;
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
uint32_t byteLength = aByteLength.WasPassed() ? aByteLength.Value() : aData.Length();
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
nsresult rv = mSocketBridgeChild->SendSend(aData, aByteOffset, byteLength, ++mTrackingNumber);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, aData.Obj());
|
|
|
|
JSAutoCompartment ac(aCx, obj);
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*obj));
|
|
|
|
|
|
|
|
stream = do_CreateInstance("@mozilla.org/io/arraybuffer-input-stream;1");
|
|
|
|
nsresult rv = stream->SetData(value, aByteOffset, byteLength, aCx);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
return Send(stream, byteLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TCPSocket::Send(nsIInputStream* aStream, uint32_t aByteLength)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
uint64_t newBufferedAmount = BufferedAmount() + aByteLength;
|
|
|
|
bool bufferFull = newBufferedAmount > BUFFER_SIZE;
|
2015-03-25 17:35:59 +03:00
|
|
|
if (bufferFull) {
|
|
|
|
// If we buffered more than some arbitrary amount of data,
|
|
|
|
// (65535 right now) we should tell the caller so they can
|
|
|
|
// wait until ondrain is called if they so desire. Once all the
|
|
|
|
// buffered data has been written to the socket, ondrain is
|
|
|
|
// called.
|
|
|
|
mWaitingForDrain = true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeChild) {
|
|
|
|
// In the child, we just add the buffer length to our bufferedAmount and let
|
|
|
|
// the parent update our bufferedAmount when the data have been sent.
|
|
|
|
mBufferedAmount = newBufferedAmount;
|
|
|
|
return !bufferFull;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
if (mWaitingForStartTLS) {
|
|
|
|
// When we are waiting for starttls, newStream is added to pendingData
|
|
|
|
// and will be appended to multiplexStream after tls had been set up.
|
|
|
|
mPendingDataAfterStartTLS.AppendElement(aStream);
|
|
|
|
} else {
|
|
|
|
mMultiplexStream->AppendStream(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureCopying();
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// Collect transmitted amount for network statistics.
|
|
|
|
mTxBytes += aByteLength;
|
|
|
|
SaveNetworkStats(false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return !bufferFull;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPReadyState
|
|
|
|
TCPSocket::ReadyState()
|
|
|
|
{
|
|
|
|
return mReadyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketBinaryType
|
|
|
|
TCPSocket::BinaryType()
|
|
|
|
{
|
|
|
|
if (mUseArrayBuffers) {
|
|
|
|
return TCPSocketBinaryType::Arraybuffer;
|
|
|
|
} else {
|
|
|
|
return TCPSocketBinaryType::String;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
already_AddRefed<TCPSocket>
|
|
|
|
TCPSocket::CreateAcceptedSocket(nsIGlobalObject* aGlobal,
|
|
|
|
nsISocketTransport* aTransport,
|
|
|
|
bool aUseArrayBuffers)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocket> socket = new TCPSocket(aGlobal, EmptyString(), 0, false, aUseArrayBuffers);
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult rv = socket->InitWithTransport(aTransport);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return socket.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
already_AddRefed<TCPSocket>
|
|
|
|
TCPSocket::CreateAcceptedSocket(nsIGlobalObject* aGlobal,
|
|
|
|
TCPSocketChild* aBridge,
|
|
|
|
bool aUseArrayBuffers)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocket> socket = new TCPSocket(aGlobal, EmptyString(), 0, false, aUseArrayBuffers);
|
2015-03-25 17:36:56 +03:00
|
|
|
socket->InitWithSocketChild(aBridge);
|
|
|
|
return socket.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
already_AddRefed<TCPSocket>
|
|
|
|
TCPSocket::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aHost,
|
|
|
|
uint16_t aPort,
|
|
|
|
const SocketOptions& aOptions,
|
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TCPSocket> socket =
|
2015-03-25 17:35:59 +03:00
|
|
|
new TCPSocket(global, aHost, aPort, aOptions.mUseSecureTransport,
|
|
|
|
aOptions.mBinaryType == TCPSocketBinaryType::Arraybuffer);
|
|
|
|
nsresult rv = socket->Init();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
nsresult
|
|
|
|
TCPSocket::CreateInputStreamPump()
|
2015-03-25 17:35:59 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
mInputStreamPump = do_CreateInstance("@mozilla.org/network/input-stream-pump;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mInputStreamPump->Init(mSocketInputStream, -1, -1, 0, 0, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uint64_t suspendCount = mSuspendCount;
|
|
|
|
while (suspendCount--) {
|
|
|
|
mInputStreamPump->Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mInputStreamPump->AsyncRead(this, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:24 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::OnTransportStatus(nsITransport* aTransport, nsresult aStatus,
|
|
|
|
int64_t aProgress, int64_t aProgressMax)
|
|
|
|
{
|
|
|
|
if (static_cast<uint32_t>(aStatus) != nsISocketTransport::STATUS_CONNECTED_TO) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReadyState = TCPReadyState::Open;
|
|
|
|
FireEvent(NS_LITERAL_STRING("open"));
|
|
|
|
|
|
|
|
nsresult rv = CreateInputStreamPump();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::OnInputStreamReady(nsIAsyncInputStream* aStream)
|
|
|
|
{
|
|
|
|
// Only used for detecting if the connection was refused.
|
|
|
|
|
|
|
|
uint64_t dummy;
|
|
|
|
nsresult rv = aStream->Available(&dummy);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MaybeReportErrorAndCloseIfOpen(NS_ERROR_CONNECTION_REFUSED);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext, nsIInputStream* aStream,
|
|
|
|
uint64_t aOffset, uint32_t aCount)
|
|
|
|
{
|
2015-03-25 17:36:56 +03:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// Collect received amount for network statistics.
|
|
|
|
mRxBytes += aCount;
|
|
|
|
SaveNetworkStats(false);
|
|
|
|
#endif
|
2015-03-25 17:35:59 +03:00
|
|
|
|
|
|
|
if (mUseArrayBuffers) {
|
|
|
|
nsTArray<uint8_t> buffer;
|
|
|
|
buffer.SetCapacity(aCount);
|
|
|
|
uint32_t actual;
|
|
|
|
nsresult rv = aStream->Read(reinterpret_cast<char*>(buffer.Elements()), aCount, &actual);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(actual == aCount);
|
|
|
|
buffer.SetLength(actual);
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
if (mSocketBridgeParent) {
|
|
|
|
mSocketBridgeParent->FireArrayBufferDataEvent(buffer, mReadyState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI api;
|
2015-09-22 15:45:00 +03:00
|
|
|
if (!api.Init(GetOwnerGlobal())) {
|
2015-03-25 17:35:59 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-03-25 17:36:56 +03:00
|
|
|
JSContext* cx = api.cx();
|
2015-03-25 17:35:59 +03:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(cx);
|
2015-10-14 21:05:36 +03:00
|
|
|
if (!ToJSValue(cx, TypedArrayCreator<ArrayBuffer>(buffer), &value)) {
|
2015-03-25 17:35:59 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
FireDataEvent(cx, NS_LITERAL_STRING("data"), value);
|
2015-03-25 17:36:56 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:35:59 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
nsCString data;
|
|
|
|
nsresult rv = mInputStreamScriptable->ReadBytes(aCount, data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mSocketBridgeParent) {
|
|
|
|
mSocketBridgeParent->FireStringDataEvent(data, mReadyState);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI api;
|
2015-09-22 15:45:00 +03:00
|
|
|
if (!api.Init(GetOwnerGlobal())) {
|
2015-03-25 17:36:56 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = api.cx();
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
if (!ToJSValue(cx, NS_ConvertASCIItoUTF16(data), &value)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
FireDataEvent(cx, NS_LITERAL_STRING("data"), value);
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
|
|
|
|
{
|
|
|
|
uint32_t count;
|
|
|
|
nsresult rv = mMultiplexStream->GetCount(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
bool bufferedOutput = count != 0;
|
|
|
|
|
|
|
|
mInputStreamPump = nullptr;
|
|
|
|
|
|
|
|
if (bufferedOutput && NS_SUCCEEDED(aStatus)) {
|
|
|
|
// If we have some buffered output still, and status is not an
|
|
|
|
// error, the other side has done a half-close, but we don't
|
|
|
|
// want to be in the close state until we are done sending
|
|
|
|
// everything that was buffered. We also don't want to call onclose
|
|
|
|
// yet.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We call this even if there is no error.
|
|
|
|
MaybeReportErrorAndCloseIfOpen(aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:36:56 +03:00
|
|
|
void
|
|
|
|
TCPSocket::SetSocketBridgeParent(TCPSocketParent* aBridgeParent)
|
|
|
|
{
|
|
|
|
mSocketBridgeParent = aBridgeParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TCPSocket::SetAppIdAndBrowser(uint32_t aAppId, bool aInBrowser)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
mAppId = aAppId;
|
|
|
|
mInBrowser = aInBrowser;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
2015-03-25 17:36:56 +03:00
|
|
|
TCPSocket::UpdateReadyState(uint32_t aReadyState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSocketBridgeChild);
|
|
|
|
mReadyState = static_cast<TCPReadyState>(aReadyState);
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 06:05:20 +03:00
|
|
|
NS_IMETHODIMP
|
2015-03-25 17:36:56 +03:00
|
|
|
TCPSocket::UpdateBufferedAmount(uint32_t aBufferedAmount, uint32_t aTrackingNumber)
|
|
|
|
{
|
|
|
|
if (aTrackingNumber != mTrackingNumber) {
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
mBufferedAmount = aBufferedAmount;
|
|
|
|
if (!mBufferedAmount) {
|
|
|
|
if (mWaitingForDrain) {
|
|
|
|
mWaitingForDrain = false;
|
2015-09-21 06:05:20 +03:00
|
|
|
return FireEvent(NS_LITERAL_STRING("drain"));
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-21 06:05:20 +03:00
|
|
|
return NS_OK;
|
2015-03-25 17:36:56 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 17:35:59 +03:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
void
|
|
|
|
TCPSocket::SaveNetworkStats(bool aEnforce)
|
|
|
|
{
|
|
|
|
if (!mTxBytes && !mRxBytes) {
|
|
|
|
// There is no traffic at all. No need to save statistics.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If "enforce" is false, the traffic amount is saved to NetworkStatsServiceProxy
|
|
|
|
// only when the total amount exceeds the predefined threshold value.
|
|
|
|
// The purpose is to avoid too much overhead for collecting statistics.
|
|
|
|
uint32_t totalBytes = mTxBytes + mRxBytes;
|
|
|
|
if (!aEnforce && totalBytes < NETWORK_STATS_THRESHOLD) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINetworkStatsServiceProxy> nssProxy =
|
|
|
|
do_GetService("@mozilla.org/networkstatsServiceProxy;1");
|
|
|
|
if (!nssProxy) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nssProxy->SaveAppStats(mAppId, mInBrowser, mActiveNetworkInfo, PR_Now(),
|
|
|
|
mRxBytes, mTxBytes, false, nullptr);
|
|
|
|
|
|
|
|
// Reset the counters once the statistics is saved to NetworkStatsServiceProxy.
|
|
|
|
mTxBytes = mRxBytes = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TCPSocket::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, "inner-window-destroyed")) {
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
|
|
|
uint64_t innerID;
|
|
|
|
nsresult rv = wrapper->GetData(&innerID);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (innerID == mInnerWindowID) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "inner-window-destroyed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-22 15:45:00 +03:00
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
TCPSocket::ShouldTCPSocketExist(JSContext* aCx, JSObject* aGlobal)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> global(aCx, aGlobal);
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(nsContentUtils::ObjectPrincipal(global))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const perms[] = { "tcp-socket", nullptr };
|
|
|
|
return Preferences::GetBool("dom.mozTCPSocket.enabled") &&
|
|
|
|
CheckAnyPermissions(aCx, global, perms);
|
|
|
|
}
|