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"
|
|
|
|
|
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"
|
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-05-30 09:48:00 +03:00
|
|
|
#include "mozilla/dom/PresentationConnectionClosedEvent.h"
|
|
|
|
#include "mozilla/ErrorNames.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"
|
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-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-05-29 20:59:00 +03:00
|
|
|
, mState(PresentationConnectionState::Connecting)
|
|
|
|
, mOwningConnectionList(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);
|
|
|
|
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-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-05-29 20:59:00 +03:00
|
|
|
new PresentationConnection(aWindow, aId, aRole, aList);
|
2015-10-12 05:36:31 +03:00
|
|
|
return NS_WARN_IF(!connection->Init()) ? nullptr : 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
|
|
|
{
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:19:00 +03:00
|
|
|
nsresult rv = service->UnregisterSessionListener(mId, mRole);
|
2015-04-22 11:01:38 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2016-05-30 09:48:00 +03:00
|
|
|
|
2016-07-13 01:18:00 +03:00
|
|
|
rv = RemoveFromLoadGroup();
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
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-05-30 09:48:00 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnectionState
|
|
|
|
PresentationConnection::State() const
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
|
|
|
return mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2015-10-08 13:11:10 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
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))) {
|
2015-10-08 13:11:10 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
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-07-13 01:18:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(
|
|
|
|
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-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)) {
|
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-04-18 03:19:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(service->TerminateSession(mId, mRole)));
|
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
|
|
|
{
|
|
|
|
if (!aSessionId.Equals(mId)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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-07-13 01:18:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(DispatchConnectionClosedEvent(reason, errorMsg)));
|
|
|
|
|
|
|
|
return RemoveFromLoadGroup();
|
2016-05-30 09:48:00 +03:00
|
|
|
}
|
|
|
|
case PresentationConnectionState::Terminated: {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("terminate"), false);
|
2016-07-13 01:18:00 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(asyncDispatcher->PostDOMEvent()));
|
|
|
|
|
|
|
|
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-04-11 06:20:55 +03:00
|
|
|
const nsACString& aData)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the data.
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(GetOwner())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JS::Value> jsData(cx);
|
|
|
|
NS_ConvertUTF8toUTF16 utf16Data(aData);
|
|
|
|
if(NS_WARN_IF(!ToJSValue(cx, utf16Data, &jsData))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DispatchMessageEvent(jsData);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-05-30 09:48:00 +03:00
|
|
|
PresentationConnection::DispatchConnectionClosedEvent(
|
|
|
|
PresentationConnectionClosedReason aReason,
|
|
|
|
const nsAString& aMessage)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationConnectionClosedEventInit init;
|
|
|
|
init.mReason = aReason;
|
|
|
|
init.mMessage = aMessage;
|
|
|
|
|
|
|
|
RefPtr<PresentationConnectionClosedEvent> closedEvent =
|
|
|
|
PresentationConnectionClosedEvent::Constructor(this,
|
|
|
|
NS_LITERAL_STRING("close"),
|
|
|
|
init);
|
|
|
|
closedEvent->SetTrusted(true);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEvent> messageEvent =
|
2015-08-12 14:39:31 +03:00
|
|
|
NS_NewDOMMessageEvent(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,
|
|
|
|
EmptyString(), nullptr, nullptr);
|
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;
|
|
|
|
}
|