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/. */
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.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"
|
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"
|
2015-10-12 05:36:31 +03:00
|
|
|
#include "PresentationConnection.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)
|
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();
|
|
|
|
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)
|
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,
|
|
|
|
PresentationConnectionState aState)
|
2015-03-19 10:48:28 +03:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
, mId(aId)
|
|
|
|
, mState(aState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
PresentationConnectionState aState)
|
2015-03-19 10:48:28 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationConnection> connection =
|
2015-10-12 05:36:31 +03:00
|
|
|
new PresentationConnection(aWindow, aId, aState);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = service->RegisterSessionListener(mId, this);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = service->UnregisterSessionListener(mId);
|
|
|
|
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
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
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-11 06:20:55 +03:00
|
|
|
nsresult rv = service->SendSessionMessage(mId, 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
|
|
|
{
|
2015-10-08 13:11:10 +03:00
|
|
|
// It only works when the state is CONNECTED.
|
2015-10-12 05:36:31 +03:00
|
|
|
if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) {
|
2015-10-08 13:11:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Bug 1210340 - Support close semantics.
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::Terminate(ErrorResult& aRv)
|
2015-10-08 13:11:10 +03:00
|
|
|
{
|
|
|
|
// It only works when the state is CONNECTED.
|
2015-10-12 05:36:31 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-08 13:11:10 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(service->TerminateSession(mId)));
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::NotifyStateChange(const nsAString& aSessionId,
|
2016-04-11 06:20:55 +03:00
|
|
|
uint16_t aState)
|
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) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Unregister session listener if the session is no longer connected.
|
2015-10-12 05:36:31 +03:00
|
|
|
if (mState == PresentationConnectionState::Terminated) {
|
2015-04-22 11:01:38 +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);
|
|
|
|
if(NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DispatchStateChangeEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-10-12 05:36:31 +03:00
|
|
|
PresentationConnection::DispatchStateChangeEvent()
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2015-04-22 11:01:38 +03:00
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("statechange"), false);
|
|
|
|
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
|
|
|
}
|