2015-03-30 09:27:27 +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-03-30 10:46:11 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2015-03-30 10:48:11 +03:00
|
|
|
#include "mozilla/dom/HTMLIFrameElementBinding.h"
|
2015-03-30 10:46:11 +03:00
|
|
|
#include "mozilla/dom/TabParent.h"
|
2015-10-21 08:53:00 +03:00
|
|
|
#include "mozilla/Function.h"
|
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
#include "mozilla/Move.h"
|
2015-03-30 10:46:11 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2016-04-11 06:20:55 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
2015-03-30 10:46:11 +03:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIFrameLoader.h"
|
2015-04-22 11:01:38 +03:00
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
#include "nsINetAddr.h"
|
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsNetCID.h"
|
2015-03-30 09:27:27 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2016-03-14 00:05:00 +03:00
|
|
|
#include "PresentationLog.h"
|
2015-03-30 09:27:27 +03:00
|
|
|
#include "PresentationService.h"
|
|
|
|
#include "PresentationSessionInfo.h"
|
|
|
|
|
2015-10-21 08:53:00 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "nsIPresentationNetworkHelper.h"
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "nsINetworkInterface.h"
|
|
|
|
#include "nsINetworkManager.h"
|
|
|
|
#endif
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2015-03-30 10:46:11 +03:00
|
|
|
using namespace mozilla::services;
|
2015-03-30 09:27:27 +03:00
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
/*
|
|
|
|
* Implementation of PresentationChannelDescription
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-10-21 08:53:00 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PresentationNetworkHelper final : public nsIPresentationNetworkHelperListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIPRESENTATIONNETWORKHELPERLISTENER
|
|
|
|
|
|
|
|
using Function = nsresult(PresentationControllingInfo::*)(const nsACString&);
|
|
|
|
|
|
|
|
explicit PresentationNetworkHelper(PresentationControllingInfo* aInfo,
|
|
|
|
const Function& aFunc);
|
|
|
|
|
|
|
|
nsresult GetWifiIPAddress();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~PresentationNetworkHelper() = default;
|
|
|
|
|
|
|
|
RefPtr<PresentationControllingInfo> mInfo;
|
|
|
|
Function mFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PresentationNetworkHelper,
|
|
|
|
nsIPresentationNetworkHelperListener)
|
|
|
|
|
|
|
|
PresentationNetworkHelper::PresentationNetworkHelper(PresentationControllingInfo* aInfo,
|
|
|
|
const Function& aFunc)
|
|
|
|
: mInfo(aInfo)
|
|
|
|
, mFunc(aFunc)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInfo);
|
|
|
|
MOZ_ASSERT(aFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationNetworkHelper::GetWifiIPAddress()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationNetworkHelper> networkHelper =
|
|
|
|
do_GetService(PRESENTATION_NETWORK_HELPER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return networkHelper->GetWifiIPAddress(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationNetworkHelper::OnError(const nsACString & aReason)
|
|
|
|
{
|
2016-03-14 00:05:00 +03:00
|
|
|
PRES_ERROR("PresentationNetworkHelper::OnError: %s",
|
2015-10-21 08:53:00 +03:00
|
|
|
nsPromiseFlatCString(aReason).get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationNetworkHelper::OnGetWifiIPAddress(const nsACString& aIPAddress)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInfo);
|
|
|
|
MOZ_ASSERT(mFunc);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod<nsCString>(mInfo,
|
|
|
|
mFunc,
|
|
|
|
aIPAddress));
|
2015-10-21 08:53:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
#endif // MOZ_WIDGET_ANDROID
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
class TCPPresentationChannelDescription final : public nsIPresentationChannelDescription
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIPRESENTATIONCHANNELDESCRIPTION
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
TCPPresentationChannelDescription(const nsACString& aAddress,
|
|
|
|
uint16_t aPort)
|
2015-04-22 11:01:38 +03:00
|
|
|
: mAddress(aAddress)
|
|
|
|
, mPort(aPort)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-24 11:16:47 +03:00
|
|
|
~TCPPresentationChannelDescription() {}
|
2015-04-22 11:01:38 +03:00
|
|
|
|
|
|
|
nsCString mAddress;
|
|
|
|
uint16_t mPort;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
NS_IMPL_ISUPPORTS(TCPPresentationChannelDescription, nsIPresentationChannelDescription)
|
2015-04-22 11:01:38 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-24 11:16:47 +03:00
|
|
|
TCPPresentationChannelDescription::GetType(uint8_t* aRetVal)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aRetVal)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aRetVal = nsIPresentationChannelDescription::TYPE_TCP;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-24 11:16:47 +03:00
|
|
|
TCPPresentationChannelDescription::GetTcpAddress(nsIArray** aRetVal)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aRetVal)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMutableArray> array = do_CreateInstance(NS_ARRAY_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!array)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
// TODO bug 1228504 Take all IP addresses in PresentationChannelDescription
|
|
|
|
// into account. And at the first stage Presentation API is only exposed on
|
|
|
|
// Firefox OS where the first IP appears enough for most scenarios.
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> address = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!address)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
address->SetData(mAddress);
|
|
|
|
|
|
|
|
array->AppendElement(address, false);
|
|
|
|
array.forget(aRetVal);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-24 11:16:47 +03:00
|
|
|
TCPPresentationChannelDescription::GetTcpPort(uint16_t* aRetVal)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aRetVal)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aRetVal = mPort;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-24 11:16:47 +03:00
|
|
|
TCPPresentationChannelDescription::GetDataChannelSDP(nsAString& aDataChannelSDP)
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
|
|
|
aDataChannelSDP.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
/*
|
|
|
|
* Implementation of PresentationSessionInfo
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(PresentationSessionInfo,
|
|
|
|
nsIPresentationSessionTransportCallback,
|
2015-09-24 11:16:47 +03:00
|
|
|
nsIPresentationControlChannelListener,
|
|
|
|
nsIPresentationSessionTransportBuilderListener);
|
2015-03-30 09:27:27 +03:00
|
|
|
|
|
|
|
/* virtual */ nsresult
|
|
|
|
PresentationSessionInfo::Init(nsIPresentationControlChannel* aControlChannel)
|
|
|
|
{
|
|
|
|
SetControlChannel(aControlChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
PresentationSessionInfo::Shutdown(nsresult aReason)
|
|
|
|
{
|
2015-09-09 11:38:26 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(aReason));
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// Close the control channel if any.
|
|
|
|
if (mControlChannel) {
|
2016-07-04 13:12:04 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mControlChannel->Disconnect(aReason)));
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the data transport channel if any.
|
|
|
|
if (mTransport) {
|
2015-04-22 11:01:38 +03:00
|
|
|
// |mIsTransportReady| will be unset once |NotifyTransportClosed| is called.
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(mTransport->Close(aReason)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsResponderReady = false;
|
2016-06-14 10:15:07 +03:00
|
|
|
mIsOnTerminating = false;
|
2016-04-11 06:20:55 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
SetBuilder(nullptr);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::SetListener(nsIPresentationSessionListener* aListener)
|
|
|
|
{
|
2016-08-02 20:11:00 +03:00
|
|
|
if (mListener && aListener) {
|
|
|
|
NS_WARN_IF(NS_FAILED(mListener->NotifyReplaced()));
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
if (mListener) {
|
2015-09-08 11:22:22 +03:00
|
|
|
// Enable data notification for the transport channel if it's available.
|
|
|
|
if (mTransport) {
|
|
|
|
nsresult rv = mTransport->EnableDataNotification();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// The transport might become ready, or might become un-ready again, before
|
|
|
|
// the listener has registered. So notify the listener of the state change.
|
2016-05-30 09:48:00 +03:00
|
|
|
return mListener->NotifyStateChange(mSessionId, mState, mReason);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-04-11 06:20:55 +03:00
|
|
|
PresentationSessionInfo::Send(const nsAString& aData)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
2015-04-22 11:01:38 +03:00
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mTransport)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mTransport->Send(aData);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-10-08 13:11:10 +03:00
|
|
|
PresentationSessionInfo::Close(nsresult aReason,
|
|
|
|
uint32_t aState)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:15:07 +03:00
|
|
|
// Do nothing if session is already terminated.
|
|
|
|
if (nsIPresentationSessionListener::STATE_TERMINATED == mState) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-30 09:48:00 +03:00
|
|
|
SetStateWithReason(aState, aReason);
|
2015-10-08 13:11:10 +03:00
|
|
|
|
2016-06-14 10:15:07 +03:00
|
|
|
switch (aState) {
|
|
|
|
case nsIPresentationSessionListener::STATE_CLOSED: {
|
|
|
|
Shutdown(aReason);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIPresentationSessionListener::STATE_TERMINATED: {
|
|
|
|
if (!mControlChannel) {
|
|
|
|
nsCOMPtr<nsIPresentationControlChannel> ctrlChannel;
|
|
|
|
nsresult rv = mDevice->EstablishControlChannel(getter_AddRefs(ctrlChannel));
|
2016-07-28 01:53:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Shutdown(rv);
|
|
|
|
return rv;
|
2016-06-14 10:15:07 +03:00
|
|
|
}
|
2016-07-28 01:53:00 +03:00
|
|
|
|
|
|
|
SetControlChannel(ctrlChannel);
|
2016-06-14 10:15:07 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-28 01:53:00 +03:00
|
|
|
ContinueTermination();
|
|
|
|
return NS_OK;
|
2016-06-14 10:15:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::OnTerminate(nsIPresentationControlChannel* aControlChannel)
|
|
|
|
{
|
|
|
|
mIsOnTerminating = true; // Mark for terminating transport channel
|
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_TERMINATED, NS_OK);
|
|
|
|
SetControlChannel(aControlChannel);
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::ReplySuccess()
|
|
|
|
{
|
2016-05-30 09:48:00 +03:00
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CONNECTED, NS_OK);
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::ReplyError(nsresult aError)
|
|
|
|
{
|
|
|
|
Shutdown(aError);
|
|
|
|
|
|
|
|
// Remove itself since it never succeeds.
|
2015-08-31 08:24:35 +03:00
|
|
|
return UntrackFromService();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ nsresult
|
|
|
|
PresentationSessionInfo::UntrackFromService()
|
|
|
|
{
|
2015-03-30 09:27:27 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-04-18 03:19:00 +03:00
|
|
|
static_cast<PresentationService*>(service.get())->UntrackSessionInfo(mSessionId, mRole);
|
2015-03-30 09:27:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
nsPIDOMWindowInner*
|
|
|
|
PresentationSessionInfo::GetWindow()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint64_t windowId = 0;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(service->GetWindowIdBySessionId(mSessionId, &windowId)))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGlobalWindow::GetInnerWindowWithId(windowId)->AsInner();
|
|
|
|
}
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
/* virtual */ bool
|
|
|
|
PresentationSessionInfo::IsAccessible(base::ProcessId aProcessId)
|
|
|
|
{
|
|
|
|
// No restriction by default.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:15:07 +03:00
|
|
|
void
|
|
|
|
PresentationSessionInfo::ContinueTermination()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mControlChannel);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(mControlChannel->Terminate(mSessionId)))
|
|
|
|
|| mIsOnTerminating) {
|
|
|
|
Shutdown(NS_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// nsIPresentationSessionTransportCallback
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::NotifyTransportReady()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mIsTransportReady = true;
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
// Established RTCDataChannel implies responder is ready.
|
|
|
|
if (mTransportType == nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
|
|
|
mIsResponderReady = true;
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// At sender side, session might not be ready at this point (waiting for
|
|
|
|
// receiver's answer). Yet at receiver side, session must be ready at this
|
|
|
|
// point since the data transport channel is created after the receiver page
|
|
|
|
// is ready for presentation use.
|
|
|
|
if (IsSessionReady()) {
|
|
|
|
return ReplySuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::NotifyTransportClosed(nsresult aReason)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
// Nullify |mTransport| here so it won't try to re-close |mTransport| in
|
|
|
|
// potential subsequent |Shutdown| calls.
|
2015-03-30 09:27:27 +03:00
|
|
|
mTransport = nullptr;
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
if (NS_WARN_IF(!IsSessionReady() &&
|
|
|
|
mState == nsIPresentationSessionListener::STATE_CONNECTING)) {
|
2015-03-30 09:27:27 +03:00
|
|
|
// It happens before the session is ready. Reply the callback.
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
// Unset |mIsTransportReady| here so it won't affect |IsSessionReady()| above.
|
|
|
|
mIsTransportReady = false;
|
|
|
|
|
2015-10-08 13:11:10 +03:00
|
|
|
if (mState == nsIPresentationSessionListener::STATE_CONNECTED) {
|
|
|
|
// The transport channel is closed unexpectedly (not caused by a |Close| call).
|
2016-05-30 09:48:00 +03:00
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CLOSED, aReason);
|
2015-10-08 13:11:10 +03:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
Shutdown(aReason);
|
|
|
|
|
2015-10-08 13:11:10 +03:00
|
|
|
if (mState == nsIPresentationSessionListener::STATE_TERMINATED) {
|
2015-08-31 08:24:35 +03:00
|
|
|
// Directly untrack the session info from the service.
|
|
|
|
return UntrackFromService();
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::NotifyData(const nsACString& aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
2015-03-30 09:27:27 +03:00
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
if (NS_WARN_IF(!mListener)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mListener->NotifyMessage(mSessionId, aData);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// nsIPresentationSessionTransportBuilderListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::OnSessionTransport(nsIPresentationSessionTransport* transport)
|
|
|
|
{
|
2016-06-03 06:03:27 +03:00
|
|
|
SetBuilder(nullptr);
|
|
|
|
|
|
|
|
// The session transport is managed by content process
|
|
|
|
if (!transport) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
mTransport = transport;
|
|
|
|
|
|
|
|
nsresult rv = mTransport->SetCallback(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mTransport->EnableDataNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::OnError(nsresult reason)
|
|
|
|
{
|
2016-06-03 06:03:27 +03:00
|
|
|
SetBuilder(nullptr);
|
2016-04-11 06:20:55 +03:00
|
|
|
return ReplyError(reason);
|
2015-09-24 11:16:47 +03:00
|
|
|
}
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::SendOffer(nsIPresentationChannelDescription* aOffer)
|
|
|
|
{
|
|
|
|
return mControlChannel->SendOffer(aOffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::SendAnswer(nsIPresentationChannelDescription* aAnswer)
|
|
|
|
{
|
|
|
|
return mControlChannel->SendAnswer(aAnswer);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::SendIceCandidate(const nsAString& candidate)
|
|
|
|
{
|
|
|
|
return mControlChannel->SendIceCandidate(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationSessionInfo::Close(nsresult reason)
|
|
|
|
{
|
2016-07-04 13:12:04 +03:00
|
|
|
return mControlChannel->Disconnect(reason);
|
2016-06-03 06:03:15 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
/**
|
2015-09-09 12:41:55 +03:00
|
|
|
* Implementation of PresentationControllingInfo
|
2015-03-30 09:27:27 +03:00
|
|
|
*
|
|
|
|
* During presentation session establishment, the sender expects the following
|
2015-09-04 10:54:34 +03:00
|
|
|
* after trying to establish the control channel: (The order between step 3 and
|
|
|
|
* 4 is not guaranteed.)
|
2015-03-30 09:27:27 +03:00
|
|
|
* 1. |Init| is called to open a socket |mServerSocket| for data transport
|
2015-09-04 10:54:34 +03:00
|
|
|
* channel.
|
2016-07-12 05:16:46 +03:00
|
|
|
* 2. |NotifyConnected| of |nsIPresentationControlChannelListener| is called to
|
2015-09-04 10:54:34 +03:00
|
|
|
* indicate the control channel is ready to use. Then send the offer to the
|
|
|
|
* receiver via the control channel.
|
|
|
|
* 3.1 |OnSocketAccepted| of |nsIServerSocketListener| is called to indicate the
|
2015-03-30 09:27:27 +03:00
|
|
|
* data transport channel is connected. Then initialize |mTransport|.
|
2015-09-04 10:54:34 +03:00
|
|
|
* 3.2 |NotifyTransportReady| of |nsIPresentationSessionTransportCallback| is
|
2015-03-30 09:27:27 +03:00
|
|
|
* called.
|
2015-09-04 10:54:34 +03:00
|
|
|
* 4. |OnAnswer| of |nsIPresentationControlChannelListener| is called to
|
2015-03-30 09:27:27 +03:00
|
|
|
* indicate the receiver is ready. Close the control channel since it's no
|
|
|
|
* longer needed.
|
2015-09-04 10:54:34 +03:00
|
|
|
* 5. Once both step 3 and 4 are done, the presentation session is ready to use.
|
2015-03-30 09:27:27 +03:00
|
|
|
* So notify the listener of CONNECTED state.
|
|
|
|
*/
|
|
|
|
|
2015-09-09 12:41:55 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(PresentationControllingInfo,
|
2015-03-30 09:27:27 +03:00
|
|
|
PresentationSessionInfo,
|
|
|
|
nsIServerSocketListener)
|
|
|
|
|
|
|
|
nsresult
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::Init(nsIPresentationControlChannel* aControlChannel)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
PresentationSessionInfo::Init(aControlChannel);
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
// Initialize |mServerSocket| for bootstrapping the data transport channel and
|
|
|
|
// use |this| as the listener.
|
|
|
|
mServerSocket = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!mServerSocket)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-04-22 11:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mServerSocket->Init(-1, false, -1);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mServerSocket->AsyncListen(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-03-14 00:05:00 +03:00
|
|
|
int32_t port;
|
|
|
|
rv = mServerSocket->GetPort(&port);
|
|
|
|
if (!NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
PRES_DEBUG("%s:ServerSocket created.port[%d]\n",__func__, port);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::Shutdown(nsresult aReason)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
PresentationSessionInfo::Shutdown(aReason);
|
|
|
|
|
|
|
|
// Close the server socket if any.
|
|
|
|
if (mServerSocket) {
|
|
|
|
NS_WARN_IF(NS_FAILED(mServerSocket->Close()));
|
|
|
|
mServerSocket = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsresult
|
2015-10-21 08:53:00 +03:00
|
|
|
PresentationControllingInfo::GetAddress()
|
2015-04-22 11:01:38 +03:00
|
|
|
{
|
2015-10-21 08:53:00 +03:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
2015-04-22 11:01:38 +03:00
|
|
|
nsCOMPtr<nsINetworkManager> networkManager =
|
|
|
|
do_GetService("@mozilla.org/network/manager;1");
|
|
|
|
if (NS_WARN_IF(!networkManager)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINetworkInfo> activeNetworkInfo;
|
|
|
|
networkManager->GetActiveNetworkInfo(getter_AddRefs(activeNetworkInfo));
|
|
|
|
if (NS_WARN_IF(!activeNetworkInfo)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char16_t** ips = nullptr;
|
|
|
|
uint32_t* prefixes = nullptr;
|
|
|
|
uint32_t count = 0;
|
|
|
|
activeNetworkInfo->GetAddresses(&ips, &prefixes, &count);
|
|
|
|
if (NS_WARN_IF(!count)) {
|
|
|
|
NS_Free(prefixes);
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, ips);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
// TODO bug 1228504 Take all IP addresses in PresentationChannelDescription
|
|
|
|
// into account. And at the first stage Presentation API is only exposed on
|
|
|
|
// Firefox OS where the first IP appears enough for most scenarios.
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
nsAutoString ip;
|
|
|
|
ip.Assign(ips[0]);
|
2015-10-21 08:53:00 +03:00
|
|
|
|
|
|
|
// On Android platform, the IP address is retrieved from a callback function.
|
|
|
|
// To make consistent code sequence, following function call is dispatched
|
|
|
|
// into main thread instead of calling it directly.
|
|
|
|
NS_DispatchToMainThread(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod<nsCString>(
|
2015-10-21 08:53:00 +03:00
|
|
|
this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress,
|
|
|
|
NS_ConvertUTF16toUTF8(ip)));
|
2015-04-22 11:01:38 +03:00
|
|
|
|
|
|
|
NS_Free(prefixes);
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, ips);
|
2015-10-21 08:53:00 +03:00
|
|
|
|
|
|
|
#elif defined(MOZ_WIDGET_ANDROID)
|
|
|
|
RefPtr<PresentationNetworkHelper> networkHelper =
|
|
|
|
new PresentationNetworkHelper(this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress);
|
|
|
|
nsresult rv = networkHelper->GetWifiIPAddress();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-03-14 00:05:00 +03:00
|
|
|
#elif defined(MOZ_MULET)
|
|
|
|
// In simulator,we need to use the "127.0.0.1" as target address.
|
|
|
|
NS_DispatchToMainThread(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod<nsCString>(
|
2016-03-14 00:05:00 +03:00
|
|
|
this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress,
|
|
|
|
"127.0.0.1"));
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
#else
|
|
|
|
// TODO Get host IP via other platforms.
|
2015-10-21 08:53:00 +03:00
|
|
|
|
|
|
|
NS_DispatchToMainThread(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod<nsCString>(
|
2015-10-21 08:53:00 +03:00
|
|
|
this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress,
|
|
|
|
EmptyCString()));
|
2015-04-22 11:01:38 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-21 08:53:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::OnGetAddress(const nsACString& aAddress)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// Prepare and send the offer.
|
|
|
|
int32_t port;
|
|
|
|
nsresult rv = mServerSocket->GetPort(&port);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
RefPtr<TCPPresentationChannelDescription> description =
|
|
|
|
new TCPPresentationChannelDescription(aAddress, static_cast<uint16_t>(port));
|
2015-10-21 08:53:00 +03:00
|
|
|
return mControlChannel->SendOffer(description);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// nsIPresentationControlChannelListener
|
2016-06-03 06:03:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::OnIceCandidate(const nsAString& aCandidate)
|
|
|
|
{
|
|
|
|
if (mTransportType != nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder->OnIceCandidate(aCandidate);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::OnOffer(nsIPresentationChannelDescription* aDescription)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "Sender side should not receive offer.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::OnAnswer(nsIPresentationChannelDescription* aDescription)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
2016-06-03 06:03:15 +03:00
|
|
|
if (mTransportType == nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
2016-06-03 06:03:27 +03:00
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
2016-06-03 06:03:15 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
if (NS_WARN_IF(!builder)) {
|
2016-06-03 06:03:15 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-06-03 06:03:27 +03:00
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
return builder->OnAnswer(aDescription);
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
mIsResponderReady = true;
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// Close the control channel since it's no longer needed.
|
2016-07-04 13:12:04 +03:00
|
|
|
nsresult rv = mControlChannel->Disconnect(NS_OK);
|
2015-03-30 09:27:27 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Session might not be ready at this moment (waiting for the establishment of
|
|
|
|
// the data transport channel).
|
|
|
|
if (IsSessionReady()){
|
|
|
|
return ReplySuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-12 05:16:46 +03:00
|
|
|
PresentationControllingInfo::NotifyConnected()
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
2015-10-21 08:53:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-04-11 06:20:55 +03:00
|
|
|
|
2016-06-14 10:15:07 +03:00
|
|
|
switch (mState) {
|
|
|
|
case nsIPresentationSessionListener::STATE_CONNECTING: {
|
2016-08-02 20:11:00 +03:00
|
|
|
nsresult rv = mControlChannel->Launch(GetSessionId(), GetUrl());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-06-14 10:15:07 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(BuildTransport()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIPresentationSessionListener::STATE_TERMINATED: {
|
|
|
|
ContinueTermination();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::NotifyReconnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mReconnectCallback);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(mState == nsIPresentationSessionListener::STATE_TERMINATED)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CONNECTING, NS_OK);
|
|
|
|
return mReconnectCallback->NotifySuccess();
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:15:07 +03:00
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::BuildTransport()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
if (mState != nsIPresentationSessionListener::STATE_CONNECTING) {
|
|
|
|
return NS_OK;
|
2016-07-04 13:12:04 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
if (!Preferences::GetBool("dom.presentation.session_transport.data_channel.enable")) {
|
|
|
|
// Build TCP session transport
|
|
|
|
return GetAddress();
|
|
|
|
}
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
nsPIDOMWindowInner* window = nullptr;
|
|
|
|
/**
|
|
|
|
* Generally transport is maintained by the chrome process. However, data
|
|
|
|
* channel should be live with the DOM , which implies RTCDataChannel in an OOP
|
|
|
|
* page should be establish in the content process.
|
|
|
|
*
|
|
|
|
* In OOP data channel transport case, |mBuilder| is hooked when the content
|
|
|
|
* process is ready to build a data channel transport, trigger by:
|
|
|
|
* 1. PresentationIPCService::StartSession (sender)
|
|
|
|
* 2. PresentationIPCService::NotifyReceiverReady (receiver).
|
|
|
|
*
|
|
|
|
* In this case, |mBuilder| would be an object of |PresentationBuilderParent|
|
|
|
|
* and set previously. Therefore, |BuildDataChannelTransport| triggers an IPC
|
|
|
|
* call to make content process establish a RTCDataChannel transport.
|
|
|
|
*/
|
|
|
|
// in-process case
|
|
|
|
if (!mBuilder) {
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder> builder =
|
|
|
|
do_CreateInstance("@mozilla.org/presentation/datachanneltransportbuilder;1");
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
SetBuilder(builder);
|
|
|
|
// OOP window would be set from content process
|
|
|
|
window = GetWindow();
|
2016-04-11 06:20:55 +03:00
|
|
|
}
|
2016-06-03 06:03:27 +03:00
|
|
|
// OOP case
|
2016-04-11 06:20:55 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_DATACHANNEL;
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
dataChannelBuilder(do_QueryInterface(mBuilder));
|
|
|
|
if (NS_WARN_IF(!dataChannelBuilder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-08-02 20:11:00 +03:00
|
|
|
nsresult rv = dataChannelBuilder->
|
2016-07-04 13:12:04 +03:00
|
|
|
BuildDataChannelTransport(nsIPresentationService::ROLE_CONTROLLER,
|
|
|
|
window,
|
|
|
|
this);
|
2016-06-03 06:03:27 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-07-12 05:16:46 +03:00
|
|
|
PresentationControllingInfo::NotifyDisconnected(nsresult aReason)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
if (mTransportType == nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
2016-06-03 06:03:27 +03:00
|
|
|
if (builder) {
|
2016-07-12 05:16:46 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(builder->NotifyDisconnected(aReason)));
|
2016-06-03 06:03:27 +03:00
|
|
|
}
|
2016-06-03 06:03:15 +03:00
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
// Unset control channel here so it won't try to re-close it in potential
|
|
|
|
// subsequent |Shutdown| calls.
|
2015-03-30 09:27:27 +03:00
|
|
|
SetControlChannel(nullptr);
|
|
|
|
|
2015-10-23 01:16:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(aReason) || !mIsResponderReady)) {
|
2015-10-08 13:11:10 +03:00
|
|
|
// The presentation session instance may already exist.
|
2016-08-02 19:40:00 +03:00
|
|
|
// Change the state to CLOSED if it is not terminated.
|
|
|
|
if (nsIPresentationSessionListener::STATE_TERMINATED != mState) {
|
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CLOSED, aReason);
|
|
|
|
}
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// Reply error for an abnormal close.
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIServerSocketListener
|
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::OnSocketAccepted(nsIServerSocket* aServerSocket,
|
2015-03-30 09:27:27 +03:00
|
|
|
nsISocketTransport* aTransport)
|
|
|
|
{
|
2016-03-14 00:05:00 +03:00
|
|
|
int32_t port;
|
|
|
|
nsresult rv = aTransport->GetPort(&port);
|
|
|
|
if (!NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
PRES_DEBUG("%s:receive from port[%d]\n",__func__, port);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// Initialize session transport builder and use |this| as the callback.
|
|
|
|
nsCOMPtr<nsIPresentationTCPSessionTransportBuilder> builder =
|
|
|
|
do_CreateInstance(PRESENTATION_TCP_SESSION_TRANSPORT_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_TCP;
|
2015-09-24 11:16:47 +03:00
|
|
|
return builder->BuildTCPSenderTransport(aTransport, this);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationControllingInfo::OnStopListening(nsIServerSocket* aServerSocket,
|
2015-03-30 09:27:27 +03:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-04-23 06:44:01 +03:00
|
|
|
if (aStatus == NS_BINDING_ABORTED) { // The server socket was manually closed.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
Shutdown(aStatus);
|
|
|
|
|
2015-09-09 11:38:26 +03:00
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
2015-03-30 09:27:27 +03:00
|
|
|
// It happens before the session is ready. Reply the callback.
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
2015-10-08 13:11:10 +03:00
|
|
|
// It happens after the session is ready. Change the state to CLOSED.
|
2016-05-30 09:48:00 +03:00
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CLOSED, aStatus);
|
2015-03-30 09:27:27 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::Reconnect(nsIPresentationServiceCallback* aCallback)
|
|
|
|
{
|
|
|
|
if (!aCallback) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReconnectCallback = aCallback;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(mState == nsIPresentationSessionListener::STATE_TERMINATED)) {
|
|
|
|
return mReconnectCallback->NotifyError(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mControlChannel) {
|
|
|
|
nsCOMPtr<nsIPresentationControlChannel> ctrlChannel;
|
|
|
|
rv = mDevice->EstablishControlChannel(getter_AddRefs(ctrlChannel));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return mReconnectCallback->NotifyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = Init(ctrlChannel);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return mReconnectCallback->NotifyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mControlChannel->Reconnect(mSessionId, GetUrl());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return mReconnectCallback->NotifyError(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
/**
|
2015-09-09 12:41:55 +03:00
|
|
|
* Implementation of PresentationPresentingInfo
|
2015-03-30 10:46:11 +03:00
|
|
|
*
|
|
|
|
* During presentation session establishment, the receiver expects the following
|
|
|
|
* after trying to launch the app by notifying "presentation-launch-receiver":
|
|
|
|
* (The order between step 2 and 3 is not guaranteed.)
|
|
|
|
* 1. |Observe| of |nsIObserver| is called with "presentation-receiver-launched".
|
|
|
|
* Then start listen to document |STATE_TRANSFERRING| event.
|
|
|
|
* 2. |NotifyResponderReady| is called to indicate the receiver page is ready
|
|
|
|
* for presentation use.
|
|
|
|
* 3. |OnOffer| of |nsIPresentationControlChannelListener| is called.
|
|
|
|
* 4. Once both step 2 and 3 are done, establish the data transport channel and
|
|
|
|
* send the answer. (The control channel will be closed by the sender once it
|
|
|
|
* receives the answer.)
|
|
|
|
* 5. |NotifyTransportReady| of |nsIPresentationSessionTransportCallback| is
|
|
|
|
* called. The presentation session is ready to use, so notify the listener
|
|
|
|
* of CONNECTED state.
|
2015-03-30 09:27:27 +03:00
|
|
|
*/
|
|
|
|
|
2015-09-09 12:41:55 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(PresentationPresentingInfo,
|
2015-03-30 10:46:11 +03:00
|
|
|
PresentationSessionInfo,
|
|
|
|
nsITimerCallback)
|
|
|
|
|
|
|
|
nsresult
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::Init(nsIPresentationControlChannel* aControlChannel)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
|
|
|
PresentationSessionInfo::Init(aControlChannel);
|
|
|
|
|
|
|
|
// Add a timer to prevent waiting indefinitely in case the receiver page fails
|
|
|
|
// to become ready.
|
2015-03-30 10:48:11 +03:00
|
|
|
nsresult rv;
|
2015-03-30 10:46:11 +03:00
|
|
|
int32_t timeout =
|
|
|
|
Preferences::GetInt("presentation.receiver.loading.timeout", 10000);
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = mTimer->InitWithCallback(this, timeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::Shutdown(nsresult aReason)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
|
|
|
PresentationSessionInfo::Shutdown(aReason);
|
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
2016-04-28 10:05:16 +03:00
|
|
|
if (mDevice) {
|
|
|
|
mDevice->Disconnect();
|
|
|
|
}
|
|
|
|
mDevice = nullptr;
|
2015-03-30 10:46:11 +03:00
|
|
|
mLoadingCallback = nullptr;
|
|
|
|
mRequesterDescription = nullptr;
|
2016-06-03 06:03:27 +03:00
|
|
|
mPendingCandidates.Clear();
|
2015-03-30 10:48:11 +03:00
|
|
|
mPromise = nullptr;
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// nsIPresentationSessionTransportBuilderListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationPresentingInfo::OnSessionTransport(nsIPresentationSessionTransport* transport)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
2016-04-11 06:20:55 +03:00
|
|
|
nsresult rv = PresentationSessionInfo::OnSessionTransport(transport);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-08 11:22:22 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
// The session transport is managed by content process
|
|
|
|
if (!transport) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
// send answer for TCP session transport
|
|
|
|
if (mTransportType == nsIPresentationChannelDescription::TYPE_TCP) {
|
|
|
|
// Prepare and send the answer.
|
|
|
|
// In the current implementation of |PresentationSessionTransport|,
|
|
|
|
// |GetSelfAddress| cannot return the real info when it's initialized via
|
|
|
|
// |buildTCPReceiverTransport|. Yet this deficiency only affects the channel
|
|
|
|
// description for the answer, which is not actually checked at requester side.
|
|
|
|
nsCOMPtr<nsINetAddr> selfAddr;
|
|
|
|
rv = mTransport->GetSelfAddress(getter_AddRefs(selfAddr));
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
nsCString address;
|
|
|
|
uint16_t port = 0;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
selfAddr->GetAddress(address);
|
|
|
|
selfAddr->GetPort(&port);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPresentationChannelDescription> description =
|
|
|
|
new TCPPresentationChannelDescription(address, port);
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
return mControlChannel->SendAnswer(description);
|
2015-09-04 10:54:34 +03:00
|
|
|
}
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
return NS_OK;
|
2015-09-04 10:54:34 +03:00
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
// Delegate the pending offer and ICE candidates to builder.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationPresentingInfo::FlushPendingEvents(nsIPresentationDataChannelSessionTransportBuilder* builder)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mHasFlushPendingEvents = true;
|
|
|
|
|
|
|
|
if (mRequesterDescription) {
|
|
|
|
builder->OnOffer(mRequesterDescription);
|
|
|
|
}
|
|
|
|
mRequesterDescription = nullptr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mPendingCandidates.Length(); ++i) {
|
|
|
|
builder->OnIceCandidate(mPendingCandidates[i]);
|
|
|
|
}
|
|
|
|
mPendingCandidates.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
nsresult
|
|
|
|
PresentationPresentingInfo::InitTransportAndSendAnswer()
|
|
|
|
{
|
2016-04-11 06:28:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
uint8_t type = 0;
|
|
|
|
nsresult rv = mRequesterDescription->GetType(&type);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == nsIPresentationChannelDescription::TYPE_TCP) {
|
|
|
|
// Establish a data transport channel |mTransport| to the sender and use
|
|
|
|
// |this| as the callback.
|
|
|
|
nsCOMPtr<nsIPresentationTCPSessionTransportBuilder> builder =
|
|
|
|
do_CreateInstance(PRESENTATION_TCP_SESSION_TRANSPORT_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
SetBuilder(builder);
|
2016-04-11 06:20:55 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_TCP;
|
|
|
|
return builder->BuildTCPReceiverTransport(mRequesterDescription, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
2016-04-11 06:28:36 +03:00
|
|
|
if (!Preferences::GetBool("dom.presentation.session_transport.data_channel.enable")) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2016-06-03 06:03:27 +03:00
|
|
|
nsPIDOMWindowInner* window = nullptr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generally transport is maintained by the chrome process. However, data
|
|
|
|
* channel should be live with the DOM , which implies RTCDataChannel in an OOP
|
|
|
|
* page should be establish in the content process.
|
|
|
|
*
|
|
|
|
* In OOP data channel transport case, |mBuilder| is hooked when the content
|
|
|
|
* process is ready to build a data channel transport, trigger by:
|
|
|
|
* 1. PresentationIPCService::StartSession (sender)
|
|
|
|
* 2. PresentationIPCService::NotifyReceiverReady (receiver).
|
|
|
|
*
|
|
|
|
* In this case, |mBuilder| would be an object of |PresentationBuilderParent|
|
|
|
|
* and set previously. Therefore, |BuildDataChannelTransport| triggers an IPC
|
|
|
|
* call to make content process establish a RTCDataChannel transport.
|
|
|
|
*/
|
|
|
|
// in-process case
|
|
|
|
if (!mBuilder) {
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder> builder =
|
|
|
|
do_CreateInstance("@mozilla.org/presentation/datachanneltransportbuilder;1");
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-04-11 06:20:55 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
SetBuilder(builder);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
// OOP window would be set from content process
|
|
|
|
window = GetWindow();
|
|
|
|
}
|
2016-04-11 06:20:55 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_DATACHANNEL;
|
2016-06-03 06:03:27 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
dataChannelBuilder(do_QueryInterface(mBuilder));
|
|
|
|
if (NS_WARN_IF(!dataChannelBuilder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
rv = dataChannelBuilder->
|
|
|
|
BuildDataChannelTransport(nsIPresentationService::ROLE_RECEIVER,
|
|
|
|
window,
|
|
|
|
this);
|
2016-04-11 06:20:55 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
|
|
|
|
rv = this->FlushPendingEvents(dataChannelBuilder);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2015-09-24 11:16:47 +03:00
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
MOZ_ASSERT(false, "Unknown nsIPresentationChannelDescription type!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2015-09-24 11:16:47 +03:00
|
|
|
}
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
nsresult
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::UntrackFromService()
|
2015-08-31 08:24:35 +03:00
|
|
|
{
|
|
|
|
// Remove the OOP responding info (if it has never been used).
|
|
|
|
if (mContentParent) {
|
|
|
|
NS_WARN_IF(!static_cast<ContentParent*>(mContentParent.get())->SendNotifyPresentationReceiverCleanUp(mSessionId));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the session info (and the in-process responding info if there's any).
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-04-18 03:19:00 +03:00
|
|
|
static_cast<PresentationService*>(service.get())->UntrackSessionInfo(mSessionId, mRole);
|
2015-08-31 08:24:35 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::IsAccessible(base::ProcessId aProcessId)
|
2015-08-31 08:24:35 +03:00
|
|
|
{
|
|
|
|
// Only the specific content process should access the responder info.
|
|
|
|
return (mContentParent) ?
|
|
|
|
aProcessId == static_cast<ContentParent*>(mContentParent.get())->OtherPid() :
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
nsresult
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::NotifyResponderReady()
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsResponderReady = true;
|
|
|
|
|
|
|
|
// Initialize |mTransport| and send the answer to the sender if sender's
|
|
|
|
// description is already offered.
|
|
|
|
if (mRequesterDescription) {
|
|
|
|
nsresult rv = InitTransportAndSendAnswer();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-30 09:27:27 +03:00
|
|
|
|
2016-08-04 04:46:14 +03:00
|
|
|
nsresult
|
|
|
|
PresentationPresentingInfo::NotifyResponderFailure()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// nsIPresentationControlChannelListener
|
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::OnOffer(nsIPresentationChannelDescription* aDescription)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
2016-06-03 06:03:27 +03:00
|
|
|
if (NS_WARN_IF(mHasFlushPendingEvents)) {
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
if (NS_WARN_IF(!aDescription)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mRequesterDescription = aDescription;
|
|
|
|
|
|
|
|
// Initialize |mTransport| and send the answer to the sender if the receiver
|
|
|
|
// page is ready for presentation use.
|
|
|
|
if (mIsResponderReady) {
|
|
|
|
nsresult rv = InitTransportAndSendAnswer();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::OnAnswer(nsIPresentationChannelDescription* aDescription)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "Receiver side should not receive answer.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-05-25 10:23:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationPresentingInfo::OnIceCandidate(const nsAString& aCandidate)
|
|
|
|
{
|
2016-06-03 06:03:27 +03:00
|
|
|
if (!mBuilder && !mHasFlushPendingEvents) {
|
|
|
|
mPendingCandidates.AppendElement(nsString(aCandidate));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mBuilder && mHasFlushPendingEvents)) {
|
2016-06-03 06:03:15 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
|
|
|
|
|
|
|
return builder->OnIceCandidate(aCandidate);
|
2015-05-25 10:23:26 +03:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_IMETHODIMP
|
2016-07-12 05:16:46 +03:00
|
|
|
PresentationPresentingInfo::NotifyConnected()
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
2016-06-14 10:15:07 +03:00
|
|
|
if (nsIPresentationSessionListener::STATE_TERMINATED == mState) {
|
|
|
|
ContinueTermination();
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationPresentingInfo::NotifyReconnected()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "NotifyReconnected should not be called at receiver side.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_IMETHODIMP
|
2016-07-12 05:16:46 +03:00
|
|
|
PresentationPresentingInfo::NotifyDisconnected(nsresult aReason)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
if (mTransportType == nsIPresentationChannelDescription::TYPE_DATACHANNEL) {
|
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder>
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
2016-06-03 06:03:27 +03:00
|
|
|
if (builder) {
|
2016-07-12 05:16:46 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(builder->NotifyDisconnected(aReason)));
|
2016-06-03 06:03:27 +03:00
|
|
|
}
|
2016-06-03 06:03:15 +03:00
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
// Unset control channel here so it won't try to re-close it in potential
|
|
|
|
// subsequent |Shutdown| calls.
|
2015-03-30 09:27:27 +03:00
|
|
|
SetControlChannel(nullptr);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aReason))) {
|
2015-10-08 13:11:10 +03:00
|
|
|
// The presentation session instance may already exist.
|
|
|
|
// Change the state to TERMINATED since it never succeeds.
|
2016-05-30 09:48:00 +03:00
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_TERMINATED, aReason);
|
2015-04-22 11:01:38 +03:00
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
// Reply error for an abnormal close.
|
2015-09-09 11:38:26 +03:00
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
// nsITimerCallback
|
2015-03-30 10:46:11 +03:00
|
|
|
NS_IMETHODIMP
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::Notify(nsITimer* aTimer)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-30 10:48:11 +03:00
|
|
|
NS_WARNING("The receiver page fails to become ready before timeout.");
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
mTimer = nullptr;
|
|
|
|
return ReplyError(NS_ERROR_DOM_TIMEOUT_ERR);
|
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
// PromiseNativeHandler
|
|
|
|
void
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::ResolvedCallback(JSContext* aCx,
|
2016-04-11 06:20:55 +03:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2015-03-30 10:48:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
if (NS_WARN_IF(!aValue.isObject())) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
|
|
|
|
if (NS_WARN_IF(!obj)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start to listen to document state change event |STATE_TRANSFERRING|.
|
|
|
|
HTMLIFrameElement* frame = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(HTMLIFrameElement, obj, frame);
|
|
|
|
if (NS_WARN_IF(!frame)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFrameLoaderOwner> owner = do_QueryInterface((nsIFrameLoaderOwner*) frame);
|
|
|
|
if (NS_WARN_IF(!owner)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFrameLoader> frameLoader;
|
|
|
|
rv = owner->GetFrameLoader(getter_AddRefs(frameLoader));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TabParent> tabParent = TabParent::GetFrom(frameLoader);
|
2015-03-30 10:48:11 +03:00
|
|
|
if (tabParent) {
|
|
|
|
// OOP frame
|
2015-08-31 08:24:35 +03:00
|
|
|
// Notify the content process that a receiver page has launched, so it can
|
|
|
|
// start monitoring the loading progress.
|
|
|
|
mContentParent = tabParent->Manager();
|
|
|
|
NS_WARN_IF(!static_cast<ContentParent*>(mContentParent.get())->SendNotifyPresentationReceiverLaunched(tabParent, mSessionId));
|
2015-03-30 10:48:11 +03:00
|
|
|
} else {
|
|
|
|
// In-process frame
|
|
|
|
nsCOMPtr<nsIDocShell> docShell;
|
|
|
|
rv = frameLoader->GetDocShell(getter_AddRefs(docShell));
|
2015-03-30 10:46:11 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
// Keep an eye on the loading progress of the receiver page.
|
2015-03-30 10:48:11 +03:00
|
|
|
mLoadingCallback = new PresentationResponderLoadingCallback(mSessionId);
|
|
|
|
rv = mLoadingCallback->Init(docShell);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:48:11 +03:00
|
|
|
return;
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
void
|
2015-09-09 12:41:55 +03:00
|
|
|
PresentationPresentingInfo::RejectedCallback(JSContext* aCx,
|
2016-04-11 06:20:55 +03:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-04-23 06:44:01 +03:00
|
|
|
NS_WARNING("Launching the receiver page has been rejected.");
|
2015-03-30 10:46:11 +03:00
|
|
|
|
2015-03-30 10:48:11 +03:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-09 11:38:26 +03:00
|
|
|
ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|