2015-03-19 10:48:28 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-05-30 09:48:00 +03:00
|
|
|
#include "PresentationConnection.h"
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
#include "ControllerConnectionCollection.h"
|
2015-04-22 11:01:38 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2016-05-30 09:48:00 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2016-09-18 02:42:00 +03: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"
|
2016-10-04 01:22:34 +03:00
|
|
|
#include "mozilla/dom/PresentationConnectionCloseEvent.h"
|
2016-05-30 09:48:00 +03:00
|
|
|
#include "mozilla/ErrorNames.h"
|
2016-09-02 10:12:24 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2015-04-22 11:01:38 +03:00
|
|
|
#include "nsIPresentationService.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringStream.h"
|
2016-05-29 20:59:00 +03:00
|
|
|
#include "PresentationConnectionList.h"
|
2016-08-15 13:26:13 +03:00
|
|
|
#include "PresentationLog.h"
|
2015-03-19 10:48:28 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PresentationConnection)
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PresentationConnection, DOMEventTargetHelper)
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwningConnectionList)
|
2015-03-19 10:48:28 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PresentationConnection, DOMEventTargetHelper)
|
2015-03-19 10:48:28 +03:00
|
|
|
tmp->Shutdown();
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwningConnectionList)
|
2015-03-19 10:48:28 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(PresentationConnection, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PresentationConnection, DOMEventTargetHelper)
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(PresentationConnection)
|
2015-04-22 11:01:38 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPresentationSessionListener)
|
2016-05-30 09:48:00 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequest)
|
2015-03-19 10:48:28 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
PresentationConnection::PresentationConnection(nsPIDOMWindowInner* aWindow,
|
2015-10-12 05:36:31 +03:00
|
|
|
const nsAString& aId,
|
2016-08-02 20:11:00 +03:00
|
|
|
const nsAString& aUrl,
|
2016-04-18 03:19:00 +03:00
|
|
|
const uint8_t aRole,
|
2016-05-29 20:59:00 +03:00
|
|
|
PresentationConnectionList* aList)
|
2015-03-19 10:48:28 +03:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
, mId(aId)
|
2016-08-02 20:11:00 +03:00
|
|
|
, mUrl(aUrl)
|
2016-05-29 20:59:00 +03:00
|
|
|
, mState(PresentationConnectionState::Connecting)
|
|
|
|
, mOwningConnectionList(aList)
|
2016-09-18 02:42:00 +03:00
|
|
|
, mBinaryType(PresentationConnectionBinaryType::Arraybuffer)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2016-04-18 03:19:00 +03:00
|
|
|
MOZ_ASSERT(aRole == nsIPresentationService::ROLE_CONTROLLER ||
|
|
|
|
aRole == nsIPresentationService::ROLE_RECEIVER);
|
|
|
|
mRole = aRole;
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
/* virtual */ PresentationConnection::~PresentationConnection()
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
/* static */ already_AddRefed<PresentationConnection>
|
2016-01-30 20:05:36 +03:00
|
|
|
PresentationConnection::Create(nsPIDOMWindowInner* aWindow,
|
2015-10-12 05:36:31 +03:00
|
|
|
const nsAString& aId,
|
2016-08-02 20:11:00 +03:00
|
|
|
const nsAString& aUrl,
|
2016-04-18 03:19:00 +03:00
|
|
|
const uint8_t aRole,
|
2016-05-29 20:59:00 +03:00
|
|
|
PresentationConnectionList* aList)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2016-04-18 03:19:00 +03:00
|
|
|
MOZ_ASSERT(aRole == nsIPresentationService::ROLE_CONTROLLER ||
|
|
|
|
aRole == nsIPresentationService::ROLE_RECEIVER);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationConnection> connection =
|
2016-08-02 20:11:00 +03:00
|
|
|
new PresentationConnection(aWindow, aId, aUrl, aRole, aList);
|
|
|
|
if (NS_WARN_IF(!connection->Init())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRole == nsIPresentationService::ROLE_CONTROLLER) {
|
|
|
|
ControllerConnectionCollection::GetSingleton()->AddConnection(connection,
|
|
|
|
aRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection.forget();
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Init()
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mId.IsEmpty())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:19:00 +03:00
|
|
|
nsresult rv = service->RegisterSessionListener(mId, mRole, this);
|
2015-04-22 11:01:38 +03:00
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-19 10:48:28 +03:00
|
|
|
|
2016-07-13 01:18:00 +03:00
|
|
|
rv = AddIntoLoadGroup();
|
2016-05-30 09:48:00 +03:00
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 10:48:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Shutdown()
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("connection shutdown:id[%s], role[%d]\n",
|
|
|
|
NS_ConvertUTF16toUTF8(mId).get(), mRole);
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
DebugOnly<nsresult> rv = service->UnregisterSessionListener(mId, mRole);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "UnregisterSessionListener failed");
|
2016-05-30 09:48:00 +03:00
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
DebugOnly<nsresult> rv2 = RemoveFromLoadGroup();
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv2), "RemoveFromLoadGroup failed");
|
2016-08-02 20:11:00 +03:00
|
|
|
|
|
|
|
if (mRole == nsIPresentationService::ROLE_CONTROLLER) {
|
|
|
|
ControllerConnectionCollection::GetSingleton()->RemoveConnection(this,
|
|
|
|
mRole);
|
|
|
|
}
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
/* virtual */ void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::DisconnectFromOwner()
|
2015-09-10 11:29:08 +03:00
|
|
|
{
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(ProcessConnectionWentAway()));
|
2015-09-10 11:29:08 +03:00
|
|
|
DOMEventTargetHelper::DisconnectFromOwner();
|
|
|
|
}
|
|
|
|
|
2015-03-19 10:48:28 +03:00
|
|
|
/* virtual */ JSObject*
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2015-10-12 05:36:31 +03:00
|
|
|
return PresentationConnectionBinding::Wrap(aCx, this, aGivenProto);
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::GetId(nsAString& aId) const
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
aId = mId;
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
void
|
|
|
|
PresentationConnection::GetUrl(nsAString& aUrl) const
|
|
|
|
{
|
|
|
|
aUrl = mUrl;
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnectionState
|
|
|
|
PresentationConnection::State() const
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
return mState;
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
PresentationConnectionBinaryType
|
|
|
|
PresentationConnection::BinaryType() const
|
|
|
|
{
|
|
|
|
return mBinaryType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationConnection::SetBinaryType(PresentationConnectionBinaryType aType)
|
|
|
|
{
|
|
|
|
mBinaryType = aType;
|
|
|
|
}
|
|
|
|
|
2015-03-19 10:48:28 +03:00
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Send(const nsAString& aData,
|
2016-04-11 06:20:55 +03:00
|
|
|
ErrorResult& aRv)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
// Sending is not allowed if the session is not connected.
|
2015-10-12 05:36:31 +03:00
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
2015-03-19 10:48:28 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
2016-09-18 02:42:00 +03:00
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send message due to an internal error."));
|
2015-03-19 10:48:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:19:00 +03:00
|
|
|
nsresult rv = service->SendSessionMessage(mId, mRole, aData);
|
2015-04-22 11:01:38 +03:00
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
2016-09-18 02:42:00 +03:00
|
|
|
const uint32_t kMaxMessageLength = 256;
|
|
|
|
nsAutoString data(Substring(aData, 0, kMaxMessageLength));
|
|
|
|
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send message: \"") + data +
|
|
|
|
NS_LITERAL_STRING("\""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationConnection::Send(Blob& aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send message due to an internal error."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = service->SendSessionBlob(mId, mRole, &aData);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send binary message for Blob message."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationConnection::Send(const ArrayBuffer& aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send message due to an internal error."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
|
|
|
uint32_t length = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
|
|
|
nsDependentCSubstring msgString(data, length);
|
|
|
|
|
|
|
|
nsresult rv = service->SendSessionBinaryMsg(mId, mRole, msgString);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send binary message for ArrayBuffer message."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationConnection::Send(const ArrayBufferView& aData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send message due to an internal error."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aData.ComputeLengthAndData();
|
|
|
|
|
|
|
|
static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required");
|
|
|
|
|
|
|
|
uint32_t length = aData.Length();
|
|
|
|
char* data = reinterpret_cast<char*>(aData.Data());
|
|
|
|
nsDependentCSubstring msgString(data, length);
|
|
|
|
|
|
|
|
nsresult rv = service->SendSessionBinaryMsg(mId, mRole, msgString);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to send binary message for ArrayBufferView message."));
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Close(ErrorResult& aRv)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2016-07-13 01:18:00 +03:00
|
|
|
// It only works when the state is CONNECTED or CONNECTING.
|
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected &&
|
|
|
|
mState != PresentationConnectionState::Connecting)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
2015-10-08 13:11:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
2016-07-13 01:18:00 +03:00
|
|
|
service->CloseSession(mId,
|
|
|
|
mRole,
|
|
|
|
nsIPresentationService::CLOSED_REASON_CLOSED)));
|
2015-10-08 13:11:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Terminate(ErrorResult& aRv)
|
2015-10-08 13:11:10 +03:00
|
|
|
{
|
2016-06-14 10:15:07 +03:00
|
|
|
// It only works when the state is CONNECTED.
|
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
2015-03-19 10:48:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
2015-10-08 13:11:10 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
2015-04-22 11:01:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(service->TerminateSession(mId, mRole)));
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
bool
|
|
|
|
PresentationConnection::Equals(uint64_t aWindowId,
|
|
|
|
const nsAString& aId)
|
|
|
|
{
|
|
|
|
return GetOwner() &&
|
|
|
|
aWindowId == GetOwner()->WindowID() &&
|
|
|
|
mId.Equals(aId);
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
NS_IMETHODIMP
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::NotifyStateChange(const nsAString& aSessionId,
|
2016-05-30 09:48:00 +03:00
|
|
|
uint16_t aState,
|
|
|
|
nsresult aReason)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("connection state change:id[%s], state[%x], reason[%x], role[%d]\n",
|
|
|
|
NS_ConvertUTF16toUTF8(aSessionId).get(), aState,
|
|
|
|
aReason, mRole);
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
if (!aSessionId.Equals(mId)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
// A terminated connection should always remain in terminated.
|
|
|
|
if (mState == PresentationConnectionState::Terminated) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnectionState state;
|
2015-04-22 11:01:38 +03:00
|
|
|
switch (aState) {
|
2016-05-29 20:59:00 +03:00
|
|
|
case nsIPresentationSessionListener::STATE_CONNECTING:
|
|
|
|
state = PresentationConnectionState::Connecting;
|
|
|
|
break;
|
2015-04-22 11:01:38 +03:00
|
|
|
case nsIPresentationSessionListener::STATE_CONNECTED:
|
2015-10-12 05:36:31 +03:00
|
|
|
state = PresentationConnectionState::Connected;
|
2015-04-22 11:01:38 +03:00
|
|
|
break;
|
2015-10-08 13:11:10 +03:00
|
|
|
case nsIPresentationSessionListener::STATE_CLOSED:
|
2015-10-12 05:36:31 +03:00
|
|
|
state = PresentationConnectionState::Closed;
|
2015-04-22 11:01:38 +03:00
|
|
|
break;
|
|
|
|
case nsIPresentationSessionListener::STATE_TERMINATED:
|
2015-10-12 05:36:31 +03:00
|
|
|
state = PresentationConnectionState::Terminated;
|
2015-04-22 11:01:38 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unknown presentation session state.");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == state) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mState = state;
|
|
|
|
|
2016-05-30 09:48:00 +03:00
|
|
|
nsresult rv = ProcessStateChanged(aReason);
|
2016-05-29 20:59:00 +03:00
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningConnectionList) {
|
|
|
|
mOwningConnectionList->NotifyStateChange(aSessionId, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
2016-05-30 09:48:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationConnection::ProcessStateChanged(nsresult aReason)
|
|
|
|
{
|
|
|
|
switch (mState) {
|
2016-08-02 20:11:00 +03:00
|
|
|
case PresentationConnectionState::Connecting:
|
|
|
|
return NS_OK;
|
2016-05-30 09:48:00 +03:00
|
|
|
case PresentationConnectionState::Connected: {
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("connect"), false);
|
|
|
|
return asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
case PresentationConnectionState::Closed: {
|
|
|
|
PresentationConnectionClosedReason reason =
|
|
|
|
PresentationConnectionClosedReason::Closed;
|
|
|
|
|
|
|
|
nsString errorMsg;
|
|
|
|
if (NS_FAILED(aReason)) {
|
|
|
|
reason = PresentationConnectionClosedReason::Error;
|
|
|
|
nsCString name, message;
|
|
|
|
|
|
|
|
// If aReason is not a DOM error, use error name as message.
|
|
|
|
if (NS_FAILED(NS_GetNameAndMessageForDOMNSResult(aReason,
|
|
|
|
name,
|
|
|
|
message))) {
|
|
|
|
mozilla::GetErrorName(aReason, message);
|
|
|
|
message.InsertLiteral("Internal error: ", 0);
|
|
|
|
}
|
|
|
|
CopyUTF8toUTF16(message, errorMsg);
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused <<
|
2016-10-04 01:22:34 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(DispatchConnectionCloseEvent(reason, errorMsg)));
|
2016-07-13 01:18:00 +03:00
|
|
|
|
|
|
|
return RemoveFromLoadGroup();
|
2016-05-30 09:48:00 +03:00
|
|
|
}
|
|
|
|
case PresentationConnectionState::Terminated: {
|
2016-06-14 10:15:07 +03:00
|
|
|
// Ensure onterminate event is fired.
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("terminate"), false);
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(asyncDispatcher->PostDOMEvent()));
|
2016-06-14 10:15:07 +03:00
|
|
|
|
2016-05-30 09:48:00 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = service->UnregisterSessionListener(mId, mRole);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-13 01:18:00 +03:00
|
|
|
return RemoveFromLoadGroup();
|
2016-05-30 09:48:00 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown presentation session state.");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
NS_IMETHODIMP
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::NotifyMessage(const nsAString& aSessionId,
|
2016-09-18 02:42:00 +03:00
|
|
|
const nsACString& aData,
|
|
|
|
bool aIsBinary)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("connection %s:id[%s], data[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(aSessionId).get(),
|
|
|
|
nsPromiseFlatCString(aData).get(), mRole);
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
if (!aSessionId.Equals(mId)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No message should be expected when the session is not connected.
|
2015-10-12 05:36:31 +03:00
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
2015-04-22 11:01:38 +03:00
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(DoReceiveMessage(aData, aIsBinary)))) {
|
|
|
|
AsyncCloseConnectionWithErrorMsg(
|
|
|
|
NS_LITERAL_STRING("Unable to receive a message."));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationConnection::DoReceiveMessage(const nsACString& aData, bool aIsBinary)
|
|
|
|
{
|
2015-04-22 11:01:38 +03:00
|
|
|
// Transform the data.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(GetOwner())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
2016-09-18 02:42:00 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (aIsBinary) {
|
|
|
|
if (mBinaryType == PresentationConnectionBinaryType::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;
|
2016-09-18 02:42:00 +03:00
|
|
|
}
|
|
|
|
} else if (mBinaryType == PresentationConnectionBinaryType::Arraybuffer) {
|
|
|
|
JS::Rooted<JSObject*> arrayBuf(cx);
|
|
|
|
rv = nsContentUtils::CreateArrayBuffer(cx, aData, arrayBuf.address());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
jsData.setObject(*arrayBuf);
|
|
|
|
} else {
|
|
|
|
NS_RUNTIMEABORT("Unknown binary type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
if(NS_WARN_IF(!ToJSValue(cx, utf16Data, &jsData))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return DispatchMessageEvent(jsData);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-10-04 01:22:34 +03:00
|
|
|
PresentationConnection::DispatchConnectionCloseEvent(
|
2016-05-30 09:48:00 +03:00
|
|
|
PresentationConnectionClosedReason aReason,
|
2016-09-18 02:42:00 +03:00
|
|
|
const nsAString& aMessage,
|
|
|
|
bool aDispatchNow)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
2016-05-30 09:48:00 +03:00
|
|
|
if (mState != PresentationConnectionState::Closed) {
|
|
|
|
MOZ_ASSERT(false, "The connection state should be closed.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
PresentationConnectionCloseEventInit init;
|
2016-05-30 09:48:00 +03:00
|
|
|
init.mReason = aReason;
|
|
|
|
init.mMessage = aMessage;
|
|
|
|
|
2016-10-04 01:22:34 +03:00
|
|
|
RefPtr<PresentationConnectionCloseEvent> closedEvent =
|
|
|
|
PresentationConnectionCloseEvent::Constructor(this,
|
2016-05-30 09:48:00 +03:00
|
|
|
NS_LITERAL_STRING("close"),
|
|
|
|
init);
|
|
|
|
closedEvent->SetTrusted(true);
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
if (aDispatchNow) {
|
|
|
|
bool ignore;
|
|
|
|
return DOMEventTargetHelper::DispatchEvent(closedEvent, &ignore);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2016-05-30 09:48:00 +03:00
|
|
|
new AsyncEventDispatcher(this, static_cast<Event*>(closedEvent));
|
2015-04-22 11:01:38 +03:00
|
|
|
return asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::DispatchMessageEvent(JS::Handle<JS::Value> aData)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the origin.
|
|
|
|
nsAutoString origin;
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(global->PrincipalOrNull(), origin);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-12 16:17:10 +03:00
|
|
|
RefPtr<MessageEvent> messageEvent = new MessageEvent(this, nullptr, nullptr);
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
messageEvent->InitMessageEvent(nullptr,
|
|
|
|
NS_LITERAL_STRING("message"),
|
|
|
|
false, false, aData, origin,
|
2016-10-25 08:48:05 +03:00
|
|
|
EmptyString(), nullptr,
|
|
|
|
Sequence<OwningNonNull<MessagePort>>());
|
2015-08-12 14:39:31 +03:00
|
|
|
messageEvent->SetTrusted(true);
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2015-08-12 14:39:31 +03:00
|
|
|
new AsyncEventDispatcher(this, static_cast<Event*>(messageEvent));
|
2015-04-22 11:01:38 +03:00
|
|
|
return asyncDispatcher->PostDOMEvent();
|
2015-03-19 10:48:28 +03:00
|
|
|
}
|
2016-05-30 09:48:00 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationConnection::ProcessConnectionWentAway()
|
|
|
|
{
|
|
|
|
if (mState != PresentationConnectionState::Connected &&
|
|
|
|
mState != PresentationConnectionState::Connecting) {
|
|
|
|
// If the state is not connected or connecting, do not need to
|
|
|
|
// close the session.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = PresentationConnectionState::Terminated;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return service->CloseSession(
|
|
|
|
mId, mRole, nsIPresentationService::CLOSED_REASON_WENTAWAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::GetName(nsACString &aResult)
|
|
|
|
{
|
|
|
|
aResult.AssignLiteral("about:presentation-connection");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::IsPending(bool* aRetval)
|
|
|
|
{
|
|
|
|
*aRetval = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::GetStatus(nsresult* aStatus)
|
|
|
|
{
|
|
|
|
*aStatus = NS_OK;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::Cancel(nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NewRunnableMethod(this, &PresentationConnection::ProcessConnectionWentAway);
|
|
|
|
return NS_DispatchToCurrentThread(event);
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::Suspend(void)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::Resume(void)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::GetLoadGroup(nsILoadGroup** aLoadGroup)
|
|
|
|
{
|
|
|
|
*aLoadGroup = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = GetOwner() ? GetOwner()->GetExtantDoc() : nullptr;
|
|
|
|
if (!doc) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aLoadGroup = doc->GetDocumentLoadGroup().take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::SetLoadGroup(nsILoadGroup * aLoadGroup)
|
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::GetLoadFlags(nsLoadFlags* aLoadFlags)
|
|
|
|
{
|
|
|
|
*aLoadFlags = nsIRequest::LOAD_BACKGROUND;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationConnection::SetLoadFlags(nsLoadFlags aLoadFlags)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-07-13 01:18:00 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationConnection::AddIntoLoadGroup()
|
|
|
|
{
|
|
|
|
// Avoid adding to loadgroup multiple times
|
|
|
|
if (mWeakLoadGroup) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
nsresult rv = GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = loadGroup->AddRequest(this, nullptr);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWeakLoadGroup = do_GetWeakReference(loadGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationConnection::RemoveFromLoadGroup()
|
|
|
|
{
|
|
|
|
if (!mWeakLoadGroup) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_QueryReferent(mWeakLoadGroup);
|
|
|
|
if (loadGroup) {
|
|
|
|
mWeakLoadGroup = nullptr;
|
|
|
|
return loadGroup->RemoveRequest(this, nullptr, NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-09-18 02:42:00 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
PresentationConnection::AsyncCloseConnectionWithErrorMsg(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
if (mState == PresentationConnectionState::Terminated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString message = nsString(aMessage);
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
|
|
|
NS_NewRunnableFunction([this, message]() -> void {
|
|
|
|
// Set |mState| to |PresentationConnectionState::Closed| here to avoid
|
|
|
|
// calling |ProcessStateChanged|.
|
|
|
|
mState = PresentationConnectionState::Closed;
|
|
|
|
|
|
|
|
// Make sure dispatching the event and closing the connection are invoked
|
|
|
|
// at the same time by setting |aDispatchNow| to true.
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
2016-10-04 01:22:34 +03:00
|
|
|
DispatchConnectionCloseEvent(PresentationConnectionClosedReason::Error,
|
2016-09-18 02:42:00 +03:00
|
|
|
message,
|
|
|
|
true)));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if(NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
|
|
|
service->CloseSession(mId,
|
|
|
|
mRole,
|
|
|
|
nsIPresentationService::CLOSED_REASON_ERROR)));
|
|
|
|
});
|
|
|
|
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(r)));
|
|
|
|
}
|