2015-03-26 06:16:21 +03:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
#include "nsIPresentationDeviceManager.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "PresentationParent.h"
|
|
|
|
#include "PresentationService.h"
|
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of PresentationParent
|
|
|
|
*/
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_IMPL_ISUPPORTS(PresentationParent,
|
|
|
|
nsIPresentationAvailabilityListener,
|
|
|
|
nsIPresentationSessionListener,
|
|
|
|
nsIPresentationRespondingListener)
|
2015-03-26 06:16:21 +03:00
|
|
|
|
|
|
|
PresentationParent::PresentationParent()
|
|
|
|
: mActorDestroyed(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PresentationParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ PresentationParent::~PresentationParent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PresentationParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PresentationParent::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mService);
|
|
|
|
mService = do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
return NS_WARN_IF(!mService) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationParent::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2016-04-25 01:50:55 +03:00
|
|
|
for (uint32_t i = 0; i < mSessionIds.Length(); i++) {
|
|
|
|
NS_WARN_IF(NS_FAILED(mService->UnregisterSessionListener(mSessionIds[i])));
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
2016-04-25 01:50:55 +03:00
|
|
|
mSessionIds.Clear();
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2015-09-22 13:36:47 +03:00
|
|
|
for (uint32_t i = 0; i < mWindowIds.Length(); i++) {
|
|
|
|
NS_WARN_IF(NS_FAILED(mService->UnregisterRespondingListener(mWindowIds[i])));
|
|
|
|
}
|
|
|
|
mWindowIds.Clear();
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
mService->UnregisterAvailabilityListener(this);
|
2015-03-26 06:16:21 +03:00
|
|
|
mService = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PresentationParent::RecvPPresentationRequestConstructor(
|
|
|
|
PPresentationRequestParent* aActor,
|
2015-09-14 05:39:57 +03:00
|
|
|
const PresentationIPCRequest& aRequest)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
PresentationRequestParent* actor = static_cast<PresentationRequestParent*>(aActor);
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
switch (aRequest.type()) {
|
2015-09-14 05:39:57 +03:00
|
|
|
case PresentationIPCRequest::TStartSessionRequest:
|
2015-03-26 06:16:21 +03:00
|
|
|
rv = actor->DoRequest(aRequest.get_StartSessionRequest());
|
|
|
|
break;
|
2015-09-14 05:39:57 +03:00
|
|
|
case PresentationIPCRequest::TSendSessionMessageRequest:
|
2015-03-26 06:16:21 +03:00
|
|
|
rv = actor->DoRequest(aRequest.get_SendSessionMessageRequest());
|
|
|
|
break;
|
2015-10-08 13:11:10 +03:00
|
|
|
case PresentationIPCRequest::TCloseSessionRequest:
|
|
|
|
rv = actor->DoRequest(aRequest.get_CloseSessionRequest());
|
|
|
|
break;
|
|
|
|
case PresentationIPCRequest::TTerminateSessionRequest:
|
|
|
|
rv = actor->DoRequest(aRequest.get_TerminateSessionRequest());
|
2015-03-26 06:16:21 +03:00
|
|
|
break;
|
|
|
|
default:
|
2015-09-14 05:39:57 +03:00
|
|
|
MOZ_CRASH("Unknown PresentationIPCRequest type");
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_WARN_IF(NS_FAILED(rv)) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PPresentationRequestParent*
|
|
|
|
PresentationParent::AllocPPresentationRequestParent(
|
2015-09-14 05:39:57 +03:00
|
|
|
const PresentationIPCRequest& aRequest)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationRequestParent> actor = new PresentationRequestParent(mService);
|
2015-03-26 06:16:21 +03:00
|
|
|
return actor.forget().take();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PresentationParent::DeallocPPresentationRequestParent(
|
|
|
|
PPresentationRequestParent* aActor)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationRequestParent> actor =
|
2015-03-26 06:16:21 +03:00
|
|
|
dont_AddRef(static_cast<PresentationRequestParent*>(aActor));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PresentationParent::Recv__delete__()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-10 11:29:08 +03:00
|
|
|
PresentationParent::RecvRegisterAvailabilityHandler()
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mService->RegisterAvailabilityListener(this)));
|
2015-03-26 06:16:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-10 11:29:08 +03:00
|
|
|
PresentationParent::RecvUnregisterAvailabilityHandler()
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mService->UnregisterAvailabilityListener(this)));
|
2015-03-26 06:16:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ bool
|
2016-04-25 01:50:55 +03:00
|
|
|
PresentationParent::RecvRegisterSessionHandler(const nsString& aSessionId)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-08-31 08:24:35 +03:00
|
|
|
|
|
|
|
// Validate the accessibility (primarily for receiver side) so that a
|
|
|
|
// compromised child process can't fake the ID.
|
|
|
|
if (NS_WARN_IF(!static_cast<PresentationService*>(mService.get())->
|
2016-04-25 01:50:55 +03:00
|
|
|
IsSessionAccessible(aSessionId, OtherPid()))) {
|
2015-08-31 08:24:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-25 01:50:55 +03:00
|
|
|
mSessionIds.AppendElement(aSessionId);
|
|
|
|
NS_WARN_IF(NS_FAILED(mService->RegisterSessionListener(aSessionId, this)));
|
2015-03-26 06:16:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ bool
|
2016-04-25 01:50:55 +03:00
|
|
|
PresentationParent::RecvUnregisterSessionHandler(const nsString& aSessionId)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2016-04-25 01:50:55 +03:00
|
|
|
mSessionIds.RemoveElement(aSessionId);
|
|
|
|
NS_WARN_IF(NS_FAILED(mService->UnregisterSessionListener(aSessionId)));
|
2015-03-26 06:16:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
/* virtual */ bool
|
|
|
|
PresentationParent::RecvRegisterRespondingHandler(const uint64_t& aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-09-22 13:36:47 +03:00
|
|
|
|
|
|
|
mWindowIds.AppendElement(aWindowId);
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mService->RegisterRespondingListener(aWindowId, this)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ bool
|
|
|
|
PresentationParent::RecvUnregisterRespondingHandler(const uint64_t& aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-09-22 13:36:47 +03:00
|
|
|
mWindowIds.RemoveElement(aWindowId);
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mService->UnregisterRespondingListener(aWindowId)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationParent::NotifyAvailableChange(bool aAvailable)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mActorDestroyed || !SendNotifyAvailableChange(aAvailable))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationParent::NotifyStateChange(const nsAString& aSessionId,
|
|
|
|
uint16_t aState)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mActorDestroyed ||
|
2016-04-11 06:20:55 +03:00
|
|
|
!SendNotifySessionStateChange(nsString(aSessionId), aState))) {
|
2015-03-26 06:16:21 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationParent::NotifyMessage(const nsAString& aSessionId,
|
|
|
|
const nsACString& aData)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mActorDestroyed ||
|
2016-04-11 06:20:55 +03:00
|
|
|
!SendNotifyMessage(nsString(aSessionId), nsCString(aData)))) {
|
2015-03-26 06:16:21 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationParent::NotifySessionConnect(uint64_t aWindowId,
|
|
|
|
const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mActorDestroyed ||
|
2016-04-11 06:20:55 +03:00
|
|
|
!SendNotifySessionConnect(aWindowId, nsString(aSessionId)))) {
|
2015-09-10 11:29:08 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
bool
|
|
|
|
PresentationParent::RecvNotifyReceiverReady(const nsString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
// Set window ID to 0 since the window is from content process.
|
2015-08-31 08:24:35 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mService->NotifyReceiverReady(aSessionId, 0)));
|
2015-03-26 06:16:21 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of PresentationRequestParent
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PresentationRequestParent, nsIPresentationServiceCallback)
|
|
|
|
|
|
|
|
PresentationRequestParent::PresentationRequestParent(nsIPresentationService* aService)
|
|
|
|
: mActorDestroyed(false)
|
|
|
|
, mService(aService)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PresentationRequestParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationRequestParent::~PresentationRequestParent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PresentationRequestParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PresentationRequestParent::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
mService = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationRequestParent::DoRequest(const StartSessionRequest& aRequest)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
// Set window ID to 0 since the window is from content process.
|
2015-03-26 06:16:21 +03:00
|
|
|
return mService->StartSession(aRequest.url(), aRequest.sessionId(),
|
2016-04-11 06:20:55 +03:00
|
|
|
aRequest.origin(), aRequest.deviceId(), 0, this);
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationRequestParent::DoRequest(const SendSessionMessageRequest& aRequest)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-08-31 08:24:35 +03:00
|
|
|
|
|
|
|
// Validate the accessibility (primarily for receiver side) so that a
|
|
|
|
// compromised child process can't fake the ID.
|
|
|
|
if (NS_WARN_IF(!static_cast<PresentationService*>(mService.get())->
|
2016-04-25 01:50:55 +03:00
|
|
|
IsSessionAccessible(aRequest.sessionId(), OtherPid()))) {
|
2015-08-31 08:24:35 +03:00
|
|
|
return NotifyError(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
nsresult rv = mService->SendSessionMessage(aRequest.sessionId(),
|
|
|
|
aRequest.data());
|
2015-03-26 06:16:21 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NotifyError(rv);
|
|
|
|
}
|
|
|
|
return NotifySuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-10-08 13:11:10 +03:00
|
|
|
PresentationRequestParent::DoRequest(const CloseSessionRequest& aRequest)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
|
|
|
|
|
|
|
// Validate the accessibility (primarily for receiver side) so that a
|
|
|
|
// compromised child process can't fake the ID.
|
|
|
|
if (NS_WARN_IF(!static_cast<PresentationService*>(mService.get())->
|
2016-04-25 01:50:55 +03:00
|
|
|
IsSessionAccessible(aRequest.sessionId(), OtherPid()))) {
|
2015-10-08 13:11:10 +03:00
|
|
|
return NotifyError(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
|
2016-04-25 01:50:55 +03:00
|
|
|
nsresult rv = mService->CloseSession(aRequest.sessionId());
|
2015-10-08 13:11:10 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NotifyError(rv);
|
|
|
|
}
|
|
|
|
return NotifySuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationRequestParent::DoRequest(const TerminateSessionRequest& aRequest)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mService);
|
2015-08-31 08:24:35 +03:00
|
|
|
|
|
|
|
// Validate the accessibility (primarily for receiver side) so that a
|
|
|
|
// compromised child process can't fake the ID.
|
|
|
|
if (NS_WARN_IF(!static_cast<PresentationService*>(mService.get())->
|
2016-04-25 01:50:55 +03:00
|
|
|
IsSessionAccessible(aRequest.sessionId(), OtherPid()))) {
|
2015-08-31 08:24:35 +03:00
|
|
|
return NotifyError(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
|
|
|
|
2016-04-25 01:50:55 +03:00
|
|
|
nsresult rv = mService->TerminateSession(aRequest.sessionId());
|
2015-03-26 06:16:21 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NotifyError(rv);
|
|
|
|
}
|
|
|
|
return NotifySuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationRequestParent::NotifySuccess()
|
|
|
|
{
|
|
|
|
return SendResponse(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationRequestParent::NotifyError(nsresult aError)
|
|
|
|
{
|
|
|
|
return SendResponse(aError);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationRequestParent::SendResponse(nsresult aResult)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(mActorDestroyed || !Send__delete__(this, aResult))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|