2012-10-04 03:51:47 +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-10-04 03:51:47 +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/. */
|
|
|
|
|
2013-05-20 11:10:59 +04:00
|
|
|
#include "nsDOMDataChannel.h"
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
#include "base/basictypes.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
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"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/EventListenerManager.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"
|
2014-05-23 13:18:37 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2019-05-09 17:48:13 +03:00
|
|
|
#include "mozilla/dom/Blob.h"
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsIScriptContext.h"
|
2012-10-04 03:51:47 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2016-04-26 07:28:53 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
#include "DataChannel.h"
|
2015-11-03 07:35:29 +03:00
|
|
|
#include "DataChannelLog.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)
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(("%p: Close()ing %p", this, mDataChannel.get()));
|
2013-05-20 11:10:59 +04:00
|
|
|
mDataChannel->SetListener(nullptr, nullptr);
|
|
|
|
mDataChannel->Close();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* nsDOMDataChannel::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return RTCDataChannel_Binding::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
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMDataChannel)
|
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,
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* aWindow)
|
|
|
|
: DOMEventTargetHelper(aWindow),
|
2014-01-07 06:53:23 +04:00
|
|
|
mDataChannel(aDataChannel),
|
2013-10-11 01:04:28 +04:00
|
|
|
mBinaryType(DC_BINARY_TYPE_BLOB),
|
2016-04-26 07:28:53 +03:00
|
|
|
mCheckMustKeepAlive(true),
|
|
|
|
mSentClose(false) {}
|
2013-10-11 01:04:28 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult nsDOMDataChannel::Init(nsPIDOMWindowInner* aDOMWindow) {
|
2012-10-04 03:51:47 +04:00
|
|
|
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
|
2019-04-01 19:46:46 +03:00
|
|
|
rv = CheckCurrentGlobalCorrectness();
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(principal, mOrigin);
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(("%s: origin = %s\n", __FUNCTION__,
|
|
|
|
NS_LossyConvertUTF16toASCII(mOrigin).get()));
|
2012-10-04 03:51:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-20 08:03:44 +03:00
|
|
|
// Most of the GetFoo()/SetFoo()s don't need to touch shared resources and
|
|
|
|
// are safe after Close()
|
2012-10-04 03:51:47 +04:00
|
|
|
void nsDOMDataChannel::GetLabel(nsAString& aLabel) {
|
|
|
|
mDataChannel->GetLabel(aLabel);
|
|
|
|
}
|
|
|
|
|
2013-04-01 05:09:25 +04:00
|
|
|
void nsDOMDataChannel::GetProtocol(nsAString& aProtocol) {
|
|
|
|
mDataChannel->GetProtocol(aProtocol);
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:38:23 +03:00
|
|
|
mozilla::dom::Nullable<uint16_t> nsDOMDataChannel::GetId() const {
|
|
|
|
mozilla::dom::Nullable<uint16_t> result = mDataChannel->GetStream();
|
|
|
|
if (result.Value() == 65535) {
|
|
|
|
result.SetNull();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-06-04 01:34:42 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-31 19:50:55 +03:00
|
|
|
mozilla::dom::Nullable<uint16_t> nsDOMDataChannel::GetMaxPacketLifeTime()
|
|
|
|
const {
|
2018-06-01 15:20:51 +03:00
|
|
|
return mDataChannel->GetMaxPacketLifeTime();
|
2018-05-31 19:50:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::dom::Nullable<uint16_t> nsDOMDataChannel::GetMaxRetransmits() const {
|
2018-06-01 15:20:51 +03:00
|
|
|
return mDataChannel->GetMaxRetransmits();
|
2018-05-31 19:50:55 +03:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:26:01 +03:00
|
|
|
bool nsDOMDataChannel::Negotiated() const {
|
|
|
|
return mDataChannel->GetNegotiated();
|
|
|
|
}
|
2019-05-17 00:11:46 +03:00
|
|
|
|
2013-05-20 11:19:45 +04:00
|
|
|
bool nsDOMDataChannel::Ordered() const { return mDataChannel->GetOrdered(); }
|
|
|
|
|
|
|
|
RTCDataChannelState nsDOMDataChannel::ReadyState() const {
|
|
|
|
return static_cast<RTCDataChannelState>(mDataChannel->GetReadyState());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t nsDOMDataChannel::BufferedAmount() const {
|
2016-10-20 08:03:44 +03:00
|
|
|
if (!mSentClose) {
|
|
|
|
return mDataChannel->GetBufferedAmount();
|
|
|
|
}
|
|
|
|
return 0;
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:02:23 +03:00
|
|
|
uint32_t nsDOMDataChannel::BufferedAmountLowThreshold() const {
|
|
|
|
return mDataChannel->GetBufferedAmountLowThreshold();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsDOMDataChannel::SetBufferedAmountLowThreshold(uint32_t aThreshold) {
|
|
|
|
mDataChannel->SetBufferedAmountLowThreshold(aThreshold);
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
void nsDOMDataChannel::Close() {
|
|
|
|
mDataChannel->Close();
|
2016-04-26 07:28:53 +03:00
|
|
|
UpdateMustKeepAlive();
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-10-02 15:41:54 +03:00
|
|
|
nsAutoCString msgString;
|
|
|
|
if (!AppendUTF16toUTF8(aData, msgString, mozilla::fallible_t())) {
|
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-09 17:48:13 +03:00
|
|
|
Send(nullptr, &msgString, false, aRv);
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
void nsDOMDataChannel::Send(Blob& aData, ErrorResult& aRv) {
|
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;
|
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())) {
|
2013-05-20 11:19:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
uint64_t msgLength = aData.GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2013-05-20 11:19:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msgLength > UINT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:48:13 +03:00
|
|
|
Send(&aData, nullptr, true, aRv);
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:40:01 +04:00
|
|
|
void nsDOMDataChannel::Send(const ArrayBuffer& aData, ErrorResult& aRv) {
|
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
|
|
|
|
2020-01-23 06:22:06 +03:00
|
|
|
aData.ComputeState();
|
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
|
|
|
|
|
|
|
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());
|
|
|
|
|
2020-10-02 15:41:54 +03:00
|
|
|
nsDependentCSubstring msgString;
|
|
|
|
if (!msgString.Assign(data, len, mozilla::fallible_t())) {
|
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:48:13 +03:00
|
|
|
Send(nullptr, &msgString, true, aRv);
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:40:01 +04:00
|
|
|
void nsDOMDataChannel::Send(const ArrayBufferView& aData, ErrorResult& aRv) {
|
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
|
|
|
|
2020-01-23 06:22:06 +03:00
|
|
|
aData.ComputeState();
|
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
|
|
|
|
|
|
|
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());
|
|
|
|
|
2020-10-02 15:41:54 +03:00
|
|
|
nsDependentCSubstring msgString;
|
|
|
|
if (!msgString.Assign(data, len, mozilla::fallible_t())) {
|
|
|
|
aRv.Throw(NS_ERROR_FILE_TOO_BIG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:48:13 +03:00
|
|
|
Send(nullptr, &msgString, true, aRv);
|
2013-05-20 11:19:45 +04:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:48:13 +03:00
|
|
|
void nsDOMDataChannel::Send(mozilla::dom::Blob* aMsgBlob,
|
|
|
|
const nsACString* aMsgString, bool aIsBinary,
|
|
|
|
mozilla::ErrorResult& aRv) {
|
2012-10-04 03:51:47 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-10-20 08:03:44 +03:00
|
|
|
uint16_t state = mozilla::DataChannel::CLOSED;
|
|
|
|
if (!mSentClose) {
|
|
|
|
state = mDataChannel->GetReadyState();
|
|
|
|
}
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-05-09 17:48:13 +03:00
|
|
|
if (aMsgBlob) {
|
|
|
|
mDataChannel->SendBinaryBlob(*aMsgBlob, aRv);
|
2012-10-04 03:51:47 +04:00
|
|
|
} else {
|
2013-05-20 11:19:45 +04:00
|
|
|
if (aIsBinary) {
|
2019-05-09 17:48:13 +03:00
|
|
|
mDataChannel->SendBinaryMsg(*aMsgString, aRv);
|
2012-10-04 03:51:47 +04:00
|
|
|
} else {
|
2019-05-09 17:48:13 +03:00
|
|
|
mDataChannel->SendMsg(*aMsgString, aRv);
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDOMDataChannel::DoOnMessageAvailable(const nsACString& aData,
|
|
|
|
bool aBinary) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_VERBOSE((
|
|
|
|
"DoOnMessageAvailable%s\n",
|
|
|
|
aBinary ? ((mBinaryType == DC_BINARY_TYPE_BLOB) ? " (blob)" : " (binary)")
|
|
|
|
: ""));
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2019-04-01 19:46:46 +03:00
|
|
|
nsresult rv = CheckCurrentGlobalCorrectness();
|
2012-10-04 03:51:47 +04:00
|
|
|
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) {
|
2016-10-25 08:53:54 +03:00
|
|
|
RefPtr<Blob> blob =
|
2020-09-23 18:17:15 +03:00
|
|
|
Blob::CreateStringBlob(GetOwnerGlobal(), aData, u""_ns);
|
2019-10-21 08:33:19 +03:00
|
|
|
if (NS_WARN_IF(!blob)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-10-25 08:53:54 +03:00
|
|
|
|
|
|
|
if (!ToJSValue(cx, blob, &jsData)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-10-04 03:51:47 +04:00
|
|
|
} 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);
|
2015-07-01 07:09:46 +03:00
|
|
|
jsData.setObject(*arrayBuf);
|
2012-10-04 03:51:47 +04:00
|
|
|
} else {
|
2016-12-03 00:46:53 +03:00
|
|
|
MOZ_CRASH("Unknown binary type!");
|
2012-10-04 03:51:47 +04:00
|
|
|
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);
|
|
|
|
|
2015-06-30 21:20:56 +03:00
|
|
|
jsData.setString(jsString);
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
2016-10-12 16:17:10 +03:00
|
|
|
RefPtr<MessageEvent> event = new MessageEvent(this, nullptr, nullptr);
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2018-06-25 19:23:50 +03:00
|
|
|
event->InitMessageEvent(nullptr, u"message"_ns, CanBubble::eNo,
|
2020-09-23 18:17:15 +03:00
|
|
|
Cancelable::eNo, jsData, mOrigin, u""_ns, nullptr,
|
|
|
|
Sequence<OwningNonNull<MessagePort>>());
|
2012-10-04 03:51:47 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(
|
|
|
|
("%p(%p): %s - Dispatching\n", this, (void*)mDataChannel, __FUNCTION__));
|
2018-04-05 20:42:41 +03:00
|
|
|
ErrorResult err;
|
|
|
|
DispatchEvent(*event, err);
|
|
|
|
if (err.Failed()) {
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_ERROR(("%p(%p): %s - Failed to dispatch message", this,
|
|
|
|
(void*)mDataChannel, __FUNCTION__));
|
2012-10-04 03:51:47 +04:00
|
|
|
NS_WARNING("Failed to dispatch the message event!!!");
|
|
|
|
}
|
2018-04-05 20:42:41 +03:00
|
|
|
return err.StealNSResult();
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2019-04-01 19:46:46 +03:00
|
|
|
nsresult rv = CheckCurrentGlobalCorrectness();
|
2012-10-04 03:51:47 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
|
2012-10-04 03:51:47 +04:00
|
|
|
|
2018-06-25 19:23:50 +03:00
|
|
|
event->InitEvent(aName, CanBubble::eNo, Cancelable::eNo);
|
2012-10-04 03:51:47 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
ErrorResult err;
|
|
|
|
DispatchEvent(*event, err);
|
|
|
|
return err.StealNSResult();
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDOMDataChannel::OnChannelConnected(nsISupports* aContext) {
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(
|
|
|
|
("%p(%p): %s - Dispatching\n", this, (void*)mDataChannel, __FUNCTION__));
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
return OnSimpleEvent(aContext, u"open"_ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDOMDataChannel::OnChannelClosed(nsISupports* aContext) {
|
2016-04-26 07:28:53 +03:00
|
|
|
nsresult rv;
|
|
|
|
// so we don't have to worry if we're notified from different paths in
|
|
|
|
// the underlying code
|
|
|
|
if (!mSentClose) {
|
2016-10-20 08:03:44 +03:00
|
|
|
// Ok, we're done with it.
|
|
|
|
mDataChannel->ReleaseConnection();
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(("%p(%p): %s - Dispatching\n", this, (void*)mDataChannel,
|
|
|
|
__FUNCTION__));
|
2016-04-26 07:28:53 +03:00
|
|
|
|
|
|
|
rv = OnSimpleEvent(aContext, u"close"_ns);
|
|
|
|
// no more events can happen
|
|
|
|
mSentClose = true;
|
|
|
|
} else {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
DontKeepAliveAnyMore();
|
|
|
|
return rv;
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:02:23 +03:00
|
|
|
nsresult nsDOMDataChannel::OnBufferLow(nsISupports* aContext) {
|
2019-09-19 17:50:57 +03:00
|
|
|
DC_DEBUG(
|
|
|
|
("%p(%p): %s - Dispatching\n", this, (void*)mDataChannel, __FUNCTION__));
|
2015-09-29 02:02:23 +03:00
|
|
|
|
|
|
|
return OnSimpleEvent(aContext, u"bufferedamountlow"_ns);
|
|
|
|
}
|
|
|
|
|
2016-04-26 07:28:53 +03:00
|
|
|
nsresult nsDOMDataChannel::NotBuffered(nsISupports* aContext) {
|
|
|
|
// In the rare case that we held off GC to let the buffer drain
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Methods that keep alive the DataChannel object when:
|
|
|
|
// 1. the object has registered event listeners that can be triggered
|
|
|
|
// ("strong event listeners");
|
|
|
|
// 2. there are outgoing not sent messages.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void nsDOMDataChannel::UpdateMustKeepAlive() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mCheckMustKeepAlive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldKeepAlive = false;
|
|
|
|
uint16_t readyState = mDataChannel->GetReadyState();
|
|
|
|
|
|
|
|
switch (readyState) {
|
2019-05-30 00:27:18 +03:00
|
|
|
case DataChannel::CONNECTING: {
|
2016-04-26 07:28:53 +03:00
|
|
|
if (mListenerManager &&
|
|
|
|
(mListenerManager->HasListenersFor(nsGkAtoms::onopen) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onbufferedamountlow) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose))) {
|
|
|
|
shouldKeepAlive = true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case DataChannel::OPEN:
|
|
|
|
case DataChannel::CLOSING: {
|
|
|
|
if (mDataChannel->GetBufferedAmount() != 0 ||
|
|
|
|
(mListenerManager &&
|
|
|
|
(mListenerManager->HasListenersFor(nsGkAtoms::onmessage) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onerror) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onbufferedamountlow) ||
|
|
|
|
mListenerManager->HasListenersFor(nsGkAtoms::onclose)))) {
|
|
|
|
shouldKeepAlive = true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case DataChannel::CLOSED: {
|
|
|
|
shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSelfRef && !shouldKeepAlive) {
|
2017-07-14 09:49:22 +03:00
|
|
|
ReleaseSelf();
|
2016-04-26 07:28:53 +03:00
|
|
|
} else if (!mSelfRef && shouldKeepAlive) {
|
|
|
|
mSelfRef = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsDOMDataChannel::DontKeepAliveAnyMore() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mSelfRef) {
|
2017-07-14 09:49:22 +03:00
|
|
|
// Since we're on MainThread, force an eventloop trip to avoid deleting
|
|
|
|
// ourselves.
|
|
|
|
ReleaseSelf();
|
2016-04-26 07:28:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mCheckMustKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
2017-07-14 09:49:22 +03:00
|
|
|
void nsDOMDataChannel::ReleaseSelf() {
|
|
|
|
// release our self-reference (safely) by putting it in an event (always)
|
2020-04-07 18:16:23 +03:00
|
|
|
NS_ReleaseOnMainThread("nsDOMDataChannel::mSelfRef", mSelfRef.forget(), true);
|
2017-07-14 09:49:22 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void nsDOMDataChannel::EventListenerAdded(nsAtom* aType) {
|
2016-04-26 07:28:53 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
void nsDOMDataChannel::EventListenerRemoved(nsAtom* aType) {
|
2016-04-26 07:28:53 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
UpdateMustKeepAlive();
|
|
|
|
}
|
|
|
|
|
2012-10-04 03:51:47 +04:00
|
|
|
/* static */
|
2014-03-15 23:00:17 +04:00
|
|
|
nsresult NS_NewDOMDataChannel(
|
|
|
|
already_AddRefed<mozilla::DataChannel>&& aDataChannel,
|
2018-04-04 22:32:19 +03:00
|
|
|
nsPIDOMWindowInner* aWindow, nsDOMDataChannel** aDomDataChannel) {
|
2014-01-07 06:53:23 +04:00
|
|
|
RefPtr<nsDOMDataChannel> domdc = new nsDOMDataChannel(aDataChannel, aWindow);
|
2012-10-04 03:51:47 +04:00
|
|
|
|
|
|
|
nsresult rv = domdc->Init(aWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-04-04 22:32:19 +03:00
|
|
|
domdc.forget(aDomDataChannel);
|
|
|
|
return NS_OK;
|
2012-10-04 03:51:47 +04:00
|
|
|
}
|