2012-10-04 03:51:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
|
|
|
/* 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/. */
|
|
|
|
|
2013-05-20 11:10:59 +04:00
|
|
|
#include "nsDOMDataChannel.h"
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-10-30 03:32:10 +04:00
|
|
|
extern PRLogModuleInfo* GetDataChannelLog();
|
2012-10-04 03:51:47 +04:00
|
|
|
#endif
|
|
|
|
#undef LOG
|
2012-10-30 03:32:10 +04:00
|
|
|
#define LOG(args) PR_LOG(GetDataChannelLog(), PR_LOG_DEBUG, args)
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
|
2013-05-20 11:10:59 +04:00
|
|
|
#include "nsDOMDataChannelDeclarations.h"
|
2013-10-11 01:04:28 +04:00
|
|
|
#include "nsDOMDataChannel.h"
|
2012-10-04 03:51:47 +04:00
|
|
|
#include "nsIDOMDataChannel.h"
|
|
|
|
#include "nsIDOMMessageEvent.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-05-23 13:18:37 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "DataChannel.h"
|
|
|
|
|
2013-10-11 01:04:28 +04:00
|
|
|
// Since we've moved the windows.h include down here, we have to explicitly
|
|
|
|
// undef GetBinaryType, otherwise we'll get really odd conflicts
|
|
|
|
#ifdef GetBinaryType
|
|
|
|
#undef GetBinaryType
|
|
|
|
#endif
|
|
|
|
|
2013-02-26 23:04:13 +04:00
|
|
|
using namespace mozilla;
|
2013-05-20 11:19:45 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2013-05-20 11:10:59 +04:00
|
|
|
nsDOMDataChannel::~nsDOMDataChannel()
|
|
|
|
{
|
|
|
|
// Don't call us anymore! Likely isn't an issue (or maybe just less of
|
|
|
|
// one) once we block GC until all the (appropriate) onXxxx handlers
|
|
|
|
// are dropped. (See WebRTC spec)
|
|
|
|
LOG(("Close()ing %p", mDataChannel.get()));
|
|
|
|
mDataChannel->SetListener(nullptr, nullptr);
|
|
|
|
mDataChannel->Close();
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
/* virtual */ 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
|
|
|
nsDOMDataChannel::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-05-20 11:19:45 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return DataChannelBinding::Wrap(aCx, this, aGivenProto);
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMDataChannel)
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMDataChannel,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMDataChannel,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMDataChannel, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMDataChannel, DOMEventTargetHelper)
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMDataChannel)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDataChannel)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
nsDOMDataChannel::nsDOMDataChannel(already_AddRefed<mozilla::DataChannel>& aDataChannel,
|
2014-01-07 06:53:23 +04:00
|
|
|
nsPIDOMWindow* aWindow)
|
2014-04-01 10:13:50 +04:00
|
|
|
: DOMEventTargetHelper(aWindow && aWindow->IsOuterWindow() ?
|
|
|
|
aWindow->GetCurrentInnerWindow() : aWindow)
|
2014-01-07 06:53:23 +04:00
|
|
|
, mDataChannel(aDataChannel)
|
2013-10-11 01:04:28 +04:00
|
|
|
, mBinaryType(DC_BINARY_TYPE_BLOB)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::Init(nsPIDOMWindow* aDOMWindow)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString urlParam;
|
|
|
|
|
|
|
|
MOZ_ASSERT(mDataChannel);
|
|
|
|
mDataChannel->SetListener(this, nullptr);
|
|
|
|
|
|
|
|
// Now grovel through the objects to get a usable origin for onMessage
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aDOMWindow);
|
|
|
|
NS_ENSURE_STATE(sgo);
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_STATE(scriptContext);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal(do_QueryInterface(aDOMWindow));
|
|
|
|
NS_ENSURE_STATE(scriptPrincipal);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = scriptPrincipal->GetPrincipal();
|
|
|
|
NS_ENSURE_STATE(principal);
|
|
|
|
|
|
|
|
// Attempt to kill "ghost" DataChannel (if one can happen): but usually too early for check to fail
|
|
|
|
rv = CheckInnerWindowCorrectness();
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(principal,mOrigin);
|
|
|
|
LOG(("%s: origin = %s\n",__FUNCTION__,NS_LossyConvertUTF16toASCII(mOrigin).get()));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDataChannel, open)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDataChannel, error)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDataChannel, close)
|
|
|
|
NS_IMPL_EVENT_HANDLER(nsDOMDataChannel, message)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetLabel(nsAString& aLabel)
|
|
|
|
{
|
|
|
|
mDataChannel->GetLabel(aLabel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-01 05:09:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetProtocol(nsAString& aProtocol)
|
|
|
|
{
|
|
|
|
mDataChannel->GetProtocol(aProtocol);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-04 01:34:42 +04:00
|
|
|
uint16_t
|
|
|
|
nsDOMDataChannel::Id() const
|
|
|
|
{
|
|
|
|
return mDataChannel->GetStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetId(uint16_t *aId)
|
|
|
|
{
|
|
|
|
*aId = Id();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
uint16_t
|
|
|
|
nsDOMDataChannel::Stream() const
|
|
|
|
{
|
|
|
|
return mDataChannel->GetStream();
|
|
|
|
}
|
|
|
|
|
2013-04-01 05:09:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetStream(uint16_t *aStream)
|
|
|
|
{
|
2013-05-20 11:19:45 +04:00
|
|
|
*aStream = Stream();
|
2013-04-01 05:09:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
// XXX should be GetType()? Open question for the spec
|
2013-05-20 11:19:45 +04:00
|
|
|
bool
|
|
|
|
nsDOMDataChannel::Reliable() const
|
|
|
|
{
|
|
|
|
return mDataChannel->GetType() == mozilla::DataChannelConnection::RELIABLE;
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetReliable(bool* aReliable)
|
|
|
|
{
|
2013-05-20 11:19:45 +04:00
|
|
|
*aReliable = Reliable();
|
2012-10-04 03:51:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
bool
|
|
|
|
nsDOMDataChannel::Ordered() const
|
|
|
|
{
|
|
|
|
return mDataChannel->GetOrdered();
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetOrdered(bool* aOrdered)
|
|
|
|
{
|
2013-05-20 11:19:45 +04:00
|
|
|
*aOrdered = Ordered();
|
2012-10-04 03:51:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
RTCDataChannelState
|
|
|
|
nsDOMDataChannel::ReadyState() const
|
|
|
|
{
|
|
|
|
return static_cast<RTCDataChannelState>(mDataChannel->GetReadyState());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetReadyState(nsAString& aReadyState)
|
|
|
|
{
|
|
|
|
uint16_t readyState = mDataChannel->GetReadyState();
|
2013-04-12 18:08:47 +04:00
|
|
|
// From the WebRTC spec
|
2012-10-04 03:51:47 +04:00
|
|
|
const char * stateName[] = {
|
2013-04-12 18:08:47 +04:00
|
|
|
"connecting",
|
|
|
|
"open",
|
|
|
|
"closing",
|
|
|
|
"closed"
|
2012-10-04 03:51:47 +04:00
|
|
|
};
|
|
|
|
MOZ_ASSERT(/*readyState >= mozilla::DataChannel::CONNECTING && */ // Always true due to datatypes
|
|
|
|
readyState <= mozilla::DataChannel::CLOSED);
|
|
|
|
aReadyState.AssignASCII(stateName[readyState]);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
uint32_t
|
|
|
|
nsDOMDataChannel::BufferedAmount() const
|
|
|
|
{
|
|
|
|
return mDataChannel->GetBufferedAmount();
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::GetBufferedAmount(uint32_t* aBufferedAmount)
|
|
|
|
{
|
2013-05-20 11:19:45 +04:00
|
|
|
*aBufferedAmount = BufferedAmount();
|
2012-10-04 03:51:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsDOMDataChannel::GetBinaryType(nsAString & aBinaryType)
|
|
|
|
{
|
|
|
|
switch (mBinaryType) {
|
|
|
|
case DC_BINARY_TYPE_ARRAYBUFFER:
|
|
|
|
aBinaryType.AssignLiteral("arraybuffer");
|
|
|
|
break;
|
|
|
|
case DC_BINARY_TYPE_BLOB:
|
|
|
|
aBinaryType.AssignLiteral("blob");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Should not happen");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::SetBinaryType(const nsAString& aBinaryType)
|
|
|
|
{
|
|
|
|
if (aBinaryType.EqualsLiteral("arraybuffer")) {
|
|
|
|
mBinaryType = DC_BINARY_TYPE_ARRAYBUFFER;
|
|
|
|
} else if (aBinaryType.EqualsLiteral("blob")) {
|
|
|
|
mBinaryType = DC_BINARY_TYPE_BLOB;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMDataChannel::Close()
|
|
|
|
{
|
|
|
|
mDataChannel->Close();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
// All of the following is copy/pasted from WebSocket.cpp.
|
|
|
|
void
|
|
|
|
nsDOMDataChannel::Send(const nsAString& aData, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
NS_ConvertUTF16toUTF8 msgString(aData);
|
|
|
|
Send(nullptr, msgString, msgString.Length(), false, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-08 20:15:23 +04:00
|
|
|
nsDOMDataChannel::Send(File& aData, ErrorResult& aRv)
|
2013-05-20 11:19:45 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2013-05-20 11:19:45 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> msgStream;
|
2014-10-08 20:15:22 +04:00
|
|
|
nsresult rv = aData.GetInternalStream(getter_AddRefs(msgStream));
|
2013-05-20 11:19:45 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t msgLength;
|
2014-10-08 20:15:22 +04:00
|
|
|
rv = aData.GetSize(&msgLength);
|
2013-05-20 11:19:45 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msgLength > UINT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Send(msgStream, EmptyCString(), msgLength, true, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
nsDOMDataChannel::Send(const ArrayBuffer& aData, ErrorResult& aRv)
|
2013-05-20 11:19:45 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2013-05-20 11:19:45 +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");
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
|
|
|
|
|
|
|
nsDependentCSubstring msgString(data, len);
|
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-05 21:40:01 +04:00
|
|
|
nsDOMDataChannel::Send(const ArrayBufferView& aData, ErrorResult& aRv)
|
2013-05-20 11:19:45 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread");
|
2013-05-20 11:19:45 +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");
|
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
uint32_t len = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
|
|
|
|
|
|
|
nsDependentCSubstring msgString(data, len);
|
|
|
|
Send(nullptr, msgString, len, true, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMDataChannel::Send(nsIInputStream* aMsgStream,
|
|
|
|
const nsACString& aMsgString,
|
|
|
|
uint32_t aMsgLength,
|
|
|
|
bool aIsBinary,
|
|
|
|
ErrorResult& aRv)
|
2012-10-04 03:51:47 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
uint16_t state = mDataChannel->GetReadyState();
|
|
|
|
|
|
|
|
// In reality, the DataChannel protocol allows this, but we want it to
|
|
|
|
// look like WebSockets
|
|
|
|
if (state == mozilla::DataChannel::CONNECTING) {
|
2013-05-20 11:19:45 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == mozilla::DataChannel::CLOSING ||
|
|
|
|
state == mozilla::DataChannel::CLOSED) {
|
2013-05-20 11:19:45 +04:00
|
|
|
return;
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(state == mozilla::DataChannel::OPEN,
|
2013-05-20 11:19:45 +04:00
|
|
|
"Unknown state in nsDOMDataChannel::Send");
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
int32_t sent;
|
2013-05-20 11:19:45 +04:00
|
|
|
if (aMsgStream) {
|
|
|
|
sent = mDataChannel->SendBinaryStream(aMsgStream, aMsgLength);
|
2012-10-04 03:51:47 +04:00
|
|
|
} else {
|
2013-05-20 11:19:45 +04:00
|
|
|
if (aIsBinary) {
|
|
|
|
sent = mDataChannel->SendBinaryMsg(aMsgString);
|
2012-10-04 03:51:47 +04:00
|
|
|
} else {
|
2013-05-20 11:19:45 +04:00
|
|
|
sent = mDataChannel->SendMsg(aMsgString);
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-20 11:19:45 +04:00
|
|
|
if (sent < 0) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::DoOnMessageAvailable(const nsACString& aData,
|
|
|
|
bool aBinary)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
LOG(("DoOnMessageAvailable%s\n",aBinary ? ((mBinaryType == DC_BINARY_TYPE_BLOB) ? " (blob)" : " (binary)") : ""));
|
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-19 11:21:14 +04:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 14:17:17 +04:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
|
2014-05-23 13:18:37 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2013-05-08 06:34:56 +04:00
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
if (aBinary) {
|
|
|
|
if (mBinaryType == DC_BINARY_TYPE_BLOB) {
|
2014-10-08 20:15:22 +04:00
|
|
|
rv = nsContentUtils::CreateBlobBuffer(cx, GetOwner(), aData, &jsData);
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else if (mBinaryType == DC_BINARY_TYPE_ARRAYBUFFER) {
|
2013-05-08 06:34:56 +04:00
|
|
|
JS::Rooted<JSObject*> arrayBuf(cx);
|
|
|
|
rv = nsContentUtils::CreateArrayBuffer(cx, aData, arrayBuf.address());
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
jsData = OBJECT_TO_JSVAL(arrayBuf);
|
|
|
|
} else {
|
|
|
|
NS_RUNTIMEABORT("Unknown binary type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF8toUTF16 utf16data(aData);
|
|
|
|
JSString* jsString = JS_NewUCStringCopyN(cx, utf16data.get(), utf16data.Length());
|
|
|
|
NS_ENSURE_TRUE(jsString, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
jsData = STRING_TO_JSVAL(jsString);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMMessageEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMessageEvent> messageEvent = do_QueryInterface(event);
|
|
|
|
rv = messageEvent->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false, false,
|
|
|
|
jsData, mOrigin, EmptyString(),
|
|
|
|
nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
LOG(("%p(%p): %s - Dispatching\n",this,(void*)mDataChannel,__FUNCTION__));
|
|
|
|
rv = DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch the message event!!!");
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::OnMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMessage)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return DoOnMessageAvailable(aMessage, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::OnBinaryMessageAvailable(nsISupports* aContext,
|
|
|
|
const nsACString& aMessage)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return DoOnMessageAvailable(aMessage, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::OnSimpleEvent(nsISupports* aContext, const nsAString& aName)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsresult rv = CheckInnerWindowCorrectness();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2013-03-09 15:34:29 +04:00
|
|
|
rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
rv = event->InitEvent(aName, false, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::OnChannelConnected(nsISupports* aContext)
|
|
|
|
{
|
|
|
|
LOG(("%p(%p): %s - Dispatching\n",this,(void*)mDataChannel,__FUNCTION__));
|
|
|
|
|
|
|
|
return OnSimpleEvent(aContext, NS_LITERAL_STRING("open"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMDataChannel::OnChannelClosed(nsISupports* aContext)
|
|
|
|
{
|
|
|
|
LOG(("%p(%p): %s - Dispatching\n",this,(void*)mDataChannel,__FUNCTION__));
|
|
|
|
|
|
|
|
return OnSimpleEvent(aContext, NS_LITERAL_STRING("close"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMDataChannel::AppReady()
|
|
|
|
{
|
|
|
|
mDataChannel->AppReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2014-03-15 23:00:17 +04:00
|
|
|
NS_NewDOMDataChannel(already_AddRefed<mozilla::DataChannel>&& aDataChannel,
|
2012-10-04 03:51:47 +04:00
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
nsIDOMDataChannel** aDomDataChannel)
|
|
|
|
{
|
2014-01-07 06:53:23 +04:00
|
|
|
nsRefPtr<nsDOMDataChannel> domdc =
|
|
|
|
new nsDOMDataChannel(aDataChannel, aWindow);
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
nsresult rv = domdc->Init(aWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
return CallQueryInterface(domdc, aDomDataChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
NS_DataChannelAppReady(nsIDOMDataChannel* aDomDataChannel)
|
|
|
|
{
|
|
|
|
((nsDOMDataChannel *)aDomDataChannel)->AppReady();
|
|
|
|
}
|