2015-03-26 06:16:21 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et ft=cpp : */
|
|
|
|
/* 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/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/PPresentation.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2015-09-10 11:29:08 +03:00
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2015-03-26 06:16:21 +03:00
|
|
|
#include "nsIPresentationListener.h"
|
2015-03-30 10:46:11 +03:00
|
|
|
#include "PresentationCallbacks.h"
|
2015-03-26 06:16:21 +03:00
|
|
|
#include "PresentationChild.h"
|
|
|
|
#include "PresentationIPCService.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2015-09-10 11:29:08 +03:00
|
|
|
using namespace mozilla::ipc;
|
2015-03-26 06:16:21 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
PresentationChild* sPresentationChild;
|
|
|
|
|
|
|
|
} // anonymous
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PresentationIPCService, nsIPresentationService)
|
|
|
|
|
|
|
|
PresentationIPCService::PresentationIPCService()
|
|
|
|
{
|
|
|
|
ContentChild* contentChild = ContentChild::GetSingleton();
|
|
|
|
if (NS_WARN_IF(!contentChild)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sPresentationChild = new PresentationChild(this);
|
|
|
|
NS_WARN_IF(!contentChild->SendPPresentationConstructor(sPresentationChild));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */
|
|
|
|
PresentationIPCService::~PresentationIPCService()
|
|
|
|
{
|
2015-09-10 11:29:08 +03:00
|
|
|
mAvailabilityListeners.Clear();
|
2015-03-26 06:16:21 +03:00
|
|
|
mSessionListeners.Clear();
|
2015-08-31 08:24:35 +03:00
|
|
|
mRespondingSessionIds.Clear();
|
|
|
|
mRespondingWindowIds.Clear();
|
2015-03-26 06:16:21 +03:00
|
|
|
sPresentationChild = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::StartSession(const nsAString& aUrl,
|
|
|
|
const nsAString& aSessionId,
|
|
|
|
const nsAString& aOrigin,
|
2016-01-13 14:21:25 +03:00
|
|
|
const nsAString& aDeviceId,
|
2015-03-26 06:16:21 +03:00
|
|
|
nsIPresentationServiceCallback* aCallback)
|
|
|
|
{
|
2016-01-13 14:21:25 +03:00
|
|
|
return SendRequest(aCallback, StartSessionRequest(nsAutoString(aUrl),
|
|
|
|
nsAutoString(aSessionId),
|
|
|
|
nsAutoString(aOrigin),
|
|
|
|
nsAutoString(aDeviceId)));
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::SendSessionMessage(const nsAString& aSessionId,
|
|
|
|
nsIInputStream* aStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aSessionId.IsEmpty());
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
|
|
|
|
mozilla::ipc::OptionalInputStreamParams stream;
|
|
|
|
nsTArray<mozilla::ipc::FileDescriptor> fds;
|
|
|
|
SerializeInputStream(aStream, stream, fds);
|
|
|
|
MOZ_ASSERT(fds.IsEmpty());
|
|
|
|
|
|
|
|
return SendRequest(nullptr, SendSessionMessageRequest(nsAutoString(aSessionId), stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-10-08 13:11:10 +03:00
|
|
|
PresentationIPCService::CloseSession(const nsAString& aSessionId)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aSessionId.IsEmpty());
|
|
|
|
|
2015-10-08 13:11:10 +03:00
|
|
|
return SendRequest(nullptr, CloseSessionRequest(nsAutoString(aSessionId)));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::TerminateSession(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aSessionId.IsEmpty());
|
|
|
|
|
|
|
|
return SendRequest(nullptr, TerminateSessionRequest(nsAutoString(aSessionId)));
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationIPCService::SendRequest(nsIPresentationServiceCallback* aCallback,
|
2015-09-14 05:39:57 +03:00
|
|
|
const PresentationIPCRequest& aRequest)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
if (sPresentationChild) {
|
|
|
|
PresentationRequestChild* actor = new PresentationRequestChild(aCallback);
|
|
|
|
NS_WARN_IF(!sPresentationChild->SendPPresentationRequestConstructor(actor, aRequest));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-10 11:29:08 +03:00
|
|
|
PresentationIPCService::RegisterAvailabilityListener(nsIPresentationAvailabilityListener* aListener)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
mAvailabilityListeners.AppendElement(aListener);
|
2015-03-26 06:16:21 +03:00
|
|
|
if (sPresentationChild) {
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(!sPresentationChild->SendRegisterAvailabilityHandler());
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-10 11:29:08 +03:00
|
|
|
PresentationIPCService::UnregisterAvailabilityListener(nsIPresentationAvailabilityListener* aListener)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
mAvailabilityListeners.RemoveElement(aListener);
|
2015-03-26 06:16:21 +03:00
|
|
|
if (sPresentationChild) {
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_WARN_IF(!sPresentationChild->SendUnregisterAvailabilityHandler());
|
2015-03-26 06:16:21 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::RegisterSessionListener(const nsAString& aSessionId,
|
|
|
|
nsIPresentationSessionListener* aListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
|
|
|
|
mSessionListeners.Put(aSessionId, aListener);
|
|
|
|
if (sPresentationChild) {
|
|
|
|
NS_WARN_IF(!sPresentationChild->SendRegisterSessionHandler(nsAutoString(aSessionId)));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::UnregisterSessionListener(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
UntrackSessionInfo(aSessionId);
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
mSessionListeners.Remove(aSessionId);
|
|
|
|
if (sPresentationChild) {
|
|
|
|
NS_WARN_IF(!sPresentationChild->SendUnregisterSessionHandler(nsAutoString(aSessionId)));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::RegisterRespondingListener(uint64_t aWindowId,
|
|
|
|
nsIPresentationRespondingListener* aListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mRespondingListeners.Put(aWindowId, aListener);
|
|
|
|
if (sPresentationChild) {
|
|
|
|
NS_WARN_IF(!sPresentationChild->SendRegisterRespondingHandler(aWindowId));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::UnregisterRespondingListener(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mRespondingListeners.Remove(aWindowId);
|
|
|
|
if (sPresentationChild) {
|
|
|
|
NS_WARN_IF(!sPresentationChild->SendUnregisterRespondingHandler(aWindowId));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
nsresult
|
|
|
|
PresentationIPCService::NotifySessionStateChange(const nsAString& aSessionId,
|
|
|
|
uint16_t aState)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresentationSessionListener> listener;
|
|
|
|
if (NS_WARN_IF(!mSessionListeners.Get(aSessionId, getter_AddRefs(listener)))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener->NotifyStateChange(aSessionId, aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationIPCService::NotifyMessage(const nsAString& aSessionId,
|
|
|
|
const nsACString& aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresentationSessionListener> listener;
|
|
|
|
if (NS_WARN_IF(!mSessionListeners.Get(aSessionId, getter_AddRefs(listener)))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener->NotifyMessage(aSessionId, aData);
|
|
|
|
}
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
nsresult
|
|
|
|
PresentationIPCService::NotifySessionConnect(uint64_t aWindowId,
|
|
|
|
const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresentationRespondingListener> listener;
|
|
|
|
if (NS_WARN_IF(!mRespondingListeners.Get(aWindowId, getter_AddRefs(listener)))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return listener->NotifySessionConnect(aWindowId, aSessionId);
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
nsresult
|
|
|
|
PresentationIPCService::NotifyAvailableChange(bool aAvailable)
|
|
|
|
{
|
2015-09-10 11:29:08 +03:00
|
|
|
nsTObserverArray<nsCOMPtr<nsIPresentationAvailabilityListener>>::ForwardIterator iter(mAvailabilityListeners);
|
2015-03-26 06:16:21 +03:00
|
|
|
while (iter.HasMore()) {
|
2015-09-10 11:29:08 +03:00
|
|
|
nsIPresentationAvailabilityListener* listener = iter.GetNext();
|
2015-03-26 06:16:21 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(listener->NotifyAvailableChange(aAvailable)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-08-31 08:24:35 +03:00
|
|
|
PresentationIPCService::GetExistentSessionIdAtLaunch(uint64_t aWindowId,
|
|
|
|
nsAString& aSessionId)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
nsString* sessionId = mRespondingSessionIds.Get(aWindowId);
|
|
|
|
if (sessionId) {
|
|
|
|
aSessionId.Assign(*sessionId);
|
|
|
|
} else {
|
|
|
|
aSessionId.Truncate();
|
|
|
|
}
|
2015-03-26 06:16:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::NotifyReceiverReady(const nsAString& aSessionId,
|
|
|
|
uint64_t aWindowId)
|
2015-03-26 06:16:21 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
// No actual window uses 0 as its ID.
|
|
|
|
if (NS_WARN_IF(aWindowId == 0)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the responding info for an OOP receiver page.
|
|
|
|
mRespondingSessionIds.Put(aWindowId, new nsAutoString(aSessionId));
|
|
|
|
mRespondingWindowIds.Put(aSessionId, aWindowId);
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
NS_WARN_IF(!sPresentationChild->SendNotifyReceiverReady(nsAutoString(aSessionId)));
|
2015-11-26 04:39:00 +03:00
|
|
|
|
|
|
|
// Release mCallback after using aSessionId
|
|
|
|
// because aSessionId is held by mCallback.
|
|
|
|
mCallback = nullptr;
|
2015-03-26 06:16:21 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationIPCService::UntrackSessionInfo(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
// Remove the OOP responding info (if it has never been used).
|
|
|
|
uint64_t windowId = 0;
|
|
|
|
if(mRespondingWindowIds.Get(aSessionId, &windowId)) {
|
|
|
|
mRespondingWindowIds.Remove(aSessionId);
|
|
|
|
mRespondingSessionIds.Remove(windowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-26 06:16:21 +03:00
|
|
|
void
|
|
|
|
PresentationIPCService::NotifyPresentationChildDestroyed()
|
|
|
|
{
|
|
|
|
sPresentationChild = nullptr;
|
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationIPCService::MonitorResponderLoading(const nsAString& aSessionId,
|
|
|
|
nsIDocShell* aDocShell)
|
|
|
|
{
|
2015-08-31 08:24:35 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
mCallback = new PresentationResponderLoadingCallback(aSessionId);
|
|
|
|
return mCallback->Init(aDocShell);
|
|
|
|
}
|