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/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"
|
2016-08-11 19:09:22 +03:00
|
|
|
#include "nsFrameLoader.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(
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod<nsCString>("dom::PresentationNetworkHelper::OnGetWifiIPAddress",
|
|
|
|
mInfo,
|
2016-05-05 11:45:00 +03:00
|
|
|
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)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], reason[%" PRIx32 "], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), static_cast<uint32_t>(aReason),
|
|
|
|
mRole);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(aReason), "bad reason");
|
2015-09-09 11:38:26 +03:00
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
// Close the control channel if any.
|
|
|
|
if (mControlChannel) {
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << 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.
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(mTransport->Close(aReason)));
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mIsResponderReady = false;
|
2016-06-14 10:15:07 +03:00
|
|
|
mIsOnTerminating = false;
|
2016-04-11 06:20:55 +03:00
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
ResetBuilder();
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::SetListener(nsIPresentationSessionListener* aListener)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::SendBinaryMsg(const nsACString& aData)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mTransport)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mTransport->SendBinaryMsg(aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationSessionInfo::SendBlob(nsIDOMBlob* aBlob)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!IsSessionReady())) {
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mTransport)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mTransport->SendBlob(aBlob);
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
nsresult
|
2015-10-08 13:11:10 +03:00
|
|
|
PresentationSessionInfo::Close(nsresult aReason,
|
|
|
|
uint32_t aState)
|
|
|
|
{
|
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;
|
2016-08-30 02:40:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(service->GetWindowIdBySessionId(mSessionId,
|
|
|
|
mRole,
|
|
|
|
&windowId)))) {
|
2016-04-11 06:20:55 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
auto window = nsGlobalWindow::GetInnerWindowWithId(windowId);
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window->AsInner();
|
2016-04-11 06:20:55 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2016-09-05 13:18:11 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d], state[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole, mState);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-09-05 13:18:11 +03:00
|
|
|
if (mState != nsIPresentationSessionListener::STATE_CONNECTING &&
|
|
|
|
mState != nsIPresentationSessionListener::STATE_CONNECTED) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
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)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], reason[%" PRIx32 "], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), static_cast<uint32_t>(aReason),
|
|
|
|
mRole);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
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
|
2016-09-18 02:42:00 +03:00
|
|
|
PresentationSessionInfo::NotifyData(const nsACString& aData, bool aIsBinary)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
return mListener->NotifyMessage(mSessionId, aData, aIsBinary);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// nsIPresentationSessionTransportBuilderListener
|
|
|
|
NS_IMETHODIMP
|
2016-09-29 00:35:00 +03:00
|
|
|
PresentationSessionInfo::OnSessionTransport(nsIPresentationSessionTransport* aTransport)
|
2015-09-24 11:16:47 +03:00
|
|
|
{
|
2016-09-05 13:18:11 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d], state[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole, mState);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
ResetBuilder();
|
2016-06-03 06:03:27 +03:00
|
|
|
|
2016-09-05 13:18:11 +03:00
|
|
|
if (mState != nsIPresentationSessionListener::STATE_CONNECTING) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
if (NS_WARN_IF(!aTransport)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2016-06-03 06:03:27 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
mTransport = aTransport;
|
2015-09-24 11:16:47 +03:00
|
|
|
|
|
|
|
nsresult rv = mTransport->SetCallback(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListener) {
|
|
|
|
mTransport->EnableDataNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-08-15 13:26:13 +03:00
|
|
|
PresentationSessionInfo::OnError(nsresult aReason)
|
2015-09-24 11:16:47 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], reason[%" PRIx32 "], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), static_cast<uint32_t>(aReason),
|
|
|
|
mRole);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
ResetBuilder();
|
2016-08-15 13:26:13 +03:00
|
|
|
return ReplyError(aReason);
|
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) {
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(mServerSocket->Close()));
|
2015-03-30 09:27:27 +03:00
|
|
|
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>(
|
2017-06-12 22:34:10 +03:00
|
|
|
"dom::PresentationControllingInfo::OnGetAddress",
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-22 11:01:38 +03:00
|
|
|
#else
|
2016-08-12 10:27:33 +03:00
|
|
|
nsCOMPtr<nsINetworkInfoService> networkInfo = do_GetService(NETWORKINFOSERVICE_CONTRACT_ID);
|
|
|
|
MOZ_ASSERT(networkInfo);
|
2015-10-21 08:53:00 +03:00
|
|
|
|
2016-08-12 10:27:33 +03:00
|
|
|
nsresult rv = networkInfo->ListNetworkAddresses(this);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
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());
|
|
|
|
|
2016-08-17 02:32:00 +03:00
|
|
|
if (NS_WARN_IF(!mServerSocket)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mControlChannel)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-21 08:53:00 +03:00
|
|
|
// 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
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole);
|
|
|
|
|
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-17 02:32:00 +03:00
|
|
|
if (mIsReconnecting) {
|
|
|
|
return ContinueReconnect();
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole);
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-08-17 02:32:00 +03:00
|
|
|
if (NS_WARN_IF(mState != nsIPresentationSessionListener::STATE_CONNECTING)) {
|
2016-08-02 20:11:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
return NotifyReconnectResult(NS_OK);
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
|
|
|
|
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-09-16 15:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mBuilderConstructor)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-09-16 15:59:00 +03:00
|
|
|
* |mBuilderConstructor| is responsible for creating a builder, which is for
|
|
|
|
* building a data channel transport.
|
2016-06-03 06:03:27 +03:00
|
|
|
*
|
2016-09-16 15:59:00 +03:00
|
|
|
* In the OOP case, |mBuilderConstructor| would create a builder which is
|
|
|
|
* an object of |PresentationBuilderParent|. So, |BuildDataChannelTransport|
|
|
|
|
* triggers an IPC call to make content process establish a RTCDataChannel
|
|
|
|
* transport.
|
2016-06-03 06:03:27 +03:00
|
|
|
*/
|
2016-09-16 15:59:00 +03:00
|
|
|
|
2016-09-16 05:31:32 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_DATACHANNEL;
|
2016-09-16 15:59:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
mBuilderConstructor->CreateTransportBuilder(mTransportType,
|
|
|
|
getter_AddRefs(mBuilder))))) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-04-11 06:20:55 +03:00
|
|
|
|
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-09-16 15:59:00 +03:00
|
|
|
|
|
|
|
// OOP window would be set from content process
|
|
|
|
nsPIDOMWindowInner* window = GetWindow();
|
|
|
|
|
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;
|
|
|
|
}
|
2016-09-16 15:59:00 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
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
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], reason[%" PRIx32 "], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), static_cast<uint32_t>(aReason),
|
|
|
|
mRole);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
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-09-02 10:12:24 +03:00
|
|
|
Unused << 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
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
// If |aReason| is NS_OK, it implies that the user closes the connection
|
|
|
|
// before becomming connected. No need to call |ReplyError| in this case.
|
|
|
|
if (NS_FAILED(aReason)) {
|
|
|
|
if (mIsReconnecting) {
|
|
|
|
NotifyReconnectResult(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
// Reply error for an abnormal close.
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
Shutdown(aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the case for reconnecting a connection which is in
|
|
|
|
// connecting state and |mTransport| is not ready.
|
|
|
|
if (mDoReconnectAfterClose && !mTransport) {
|
|
|
|
mDoReconnectAfterClose = false;
|
|
|
|
return Reconnect(mReconnectCallback);
|
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());
|
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mBuilderConstructor)) {
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// Initialize session transport builder and use |this| as the callback.
|
2016-09-16 15:59:00 +03:00
|
|
|
nsCOMPtr<nsIPresentationTCPSessionTransportBuilder> builder;
|
|
|
|
if (NS_SUCCEEDED(mBuilderConstructor->CreateTransportBuilder(
|
|
|
|
nsIPresentationChannelDescription::TYPE_TCP,
|
|
|
|
getter_AddRefs(mBuilder)))) {
|
|
|
|
builder = do_QueryInterface(mBuilder);
|
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
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)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("controller %s:status[%" PRIx32 "]\n",__func__,
|
|
|
|
static_cast<uint32_t>(aStatus));
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
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-17 02:32:00 +03:00
|
|
|
/**
|
|
|
|
* The steps to reconnect a session are summarized below:
|
|
|
|
* 1. Change |mState| to CONNECTING.
|
|
|
|
* 2. Check whether |mControlChannel| is existed or not. Usually we have to
|
|
|
|
* create a new control cahnnel.
|
|
|
|
* 3.1 |mControlChannel| is null, which means we have to create a new one.
|
|
|
|
* |EstablishControlChannel| is called to create a new control channel.
|
|
|
|
* At this point, |mControlChannel| is not able to use yet. Set
|
|
|
|
* |mIsReconnecting| to true and wait until |NotifyConnected|.
|
|
|
|
* 3.2 |mControlChannel| is not null and is avaliable.
|
|
|
|
* We can just call |ContinueReconnect| to send reconnect command.
|
|
|
|
* 4. |NotifyReconnected| of |nsIPresentationControlChannelListener| is called
|
|
|
|
* to indicate the receiver is ready for reconnecting.
|
|
|
|
* 5. Once both step 3 and 4 are done, the rest is to build a new data
|
|
|
|
* transport channel by following the same steps as starting a
|
|
|
|
* new session.
|
|
|
|
*/
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::Reconnect(nsIPresentationServiceCallback* aCallback)
|
|
|
|
{
|
2016-09-29 00:35:00 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d], state[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole, mState);
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
if (!aCallback) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mReconnectCallback = aCallback;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(mState == nsIPresentationSessionListener::STATE_TERMINATED)) {
|
2016-09-29 00:35:00 +03:00
|
|
|
return NotifyReconnectResult(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
// If |mState| is not CLOSED, we have to close the connection before
|
|
|
|
// reconnecting. The process to reconnect will be continued after
|
|
|
|
// |NotifyDisconnected| or |NotifyTransportClosed| is invoked.
|
|
|
|
if (mState == nsIPresentationSessionListener::STATE_CONNECTING ||
|
|
|
|
mState == nsIPresentationSessionListener::STATE_CONNECTED) {
|
|
|
|
mDoReconnectAfterClose = true;
|
|
|
|
return Close(NS_OK, nsIPresentationSessionListener::STATE_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure |mState| is closed at this point.
|
|
|
|
MOZ_ASSERT(mState == nsIPresentationSessionListener::STATE_CLOSED);
|
|
|
|
|
|
|
|
mState = nsIPresentationSessionListener::STATE_CONNECTING;
|
|
|
|
mIsReconnecting = true;
|
2016-08-17 02:32:00 +03:00
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mControlChannel) {
|
|
|
|
nsCOMPtr<nsIPresentationControlChannel> ctrlChannel;
|
|
|
|
rv = mDevice->EstablishControlChannel(getter_AddRefs(ctrlChannel));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-09-29 00:35:00 +03:00
|
|
|
return NotifyReconnectResult(NS_ERROR_DOM_OPERATION_ERR);
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = Init(ctrlChannel);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-09-29 00:35:00 +03:00
|
|
|
return NotifyReconnectResult(NS_ERROR_DOM_OPERATION_ERR);
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
2016-08-17 02:32:00 +03:00
|
|
|
} else {
|
|
|
|
return ContinueReconnect();
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 02:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::ContinueReconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mControlChannel);
|
|
|
|
|
|
|
|
mIsReconnecting = false;
|
2016-09-29 00:35:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mControlChannel->Reconnect(mSessionId, GetUrl())))) {
|
|
|
|
return NotifyReconnectResult(NS_ERROR_DOM_OPERATION_ERR);
|
2016-08-02 20:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-12 10:27:33 +03:00
|
|
|
// nsIListNetworkAddressesListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::OnListedNetworkAddresses(const char** aAddressArray,
|
|
|
|
uint32_t aAddressArraySize)
|
|
|
|
{
|
|
|
|
if (!aAddressArraySize) {
|
|
|
|
return OnListNetworkAddressesFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
nsAutoCString ip;
|
|
|
|
ip.Assign(aAddressArray[0]);
|
|
|
|
|
|
|
|
// On Firefox desktop, 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.
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod<nsCString>(
|
|
|
|
"dom::PresentationControllingInfo::OnGetAddress",
|
|
|
|
this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress,
|
|
|
|
ip));
|
2016-08-12 10:27:33 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::OnListNetworkAddressesFailed()
|
|
|
|
{
|
|
|
|
PRES_ERROR("PresentationControllingInfo:OnListNetworkAddressesFailed");
|
|
|
|
|
|
|
|
// In 1-UA case, transport channel can still be established
|
|
|
|
// on loopback interface even if no network address available.
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod<nsCString>(
|
|
|
|
"dom::PresentationControllingInfo::OnGetAddress",
|
|
|
|
this,
|
|
|
|
&PresentationControllingInfo::OnGetAddress,
|
|
|
|
"127.0.0.1"));
|
2016-08-12 10:27:33 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:35:00 +03:00
|
|
|
nsresult
|
|
|
|
PresentationControllingInfo::NotifyReconnectResult(nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (!mReconnectCallback) {
|
|
|
|
MOZ_ASSERT(false, "mReconnectCallback can not be null here.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsReconnecting = false;
|
|
|
|
nsCOMPtr<nsIPresentationServiceCallback> callback =
|
|
|
|
mReconnectCallback.forget();
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return callback->NotifyError(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback->NotifySuccess(GetUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIPresentationSessionTransportCallback
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::NotifyTransportReady()
|
|
|
|
{
|
|
|
|
return PresentationSessionInfo::NotifyTransportReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::NotifyTransportClosed(nsresult aReason)
|
|
|
|
{
|
|
|
|
if (!mDoReconnectAfterClose) {
|
|
|
|
return PresentationSessionInfo::NotifyTransportClosed(aReason);;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == nsIPresentationSessionListener::STATE_CLOSED);
|
|
|
|
|
|
|
|
mTransport = nullptr;
|
|
|
|
mIsTransportReady = false;
|
|
|
|
mDoReconnectAfterClose = false;
|
|
|
|
return Reconnect(mReconnectCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationControllingInfo::NotifyData(const nsACString& aData, bool aIsBinary)
|
|
|
|
{
|
|
|
|
return PresentationSessionInfo::NotifyData(aData, aIsBinary);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadingCallback = nullptr;
|
|
|
|
mRequesterDescription = nullptr;
|
2016-06-03 06:03:27 +03:00
|
|
|
mPendingCandidates.Clear();
|
2015-03-30 10:48:11 +03:00
|
|
|
mPromise = nullptr;
|
2016-09-16 15:59:00 +03:00
|
|
|
mHasFlushPendingEvents = false;
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 11:16:47 +03:00
|
|
|
// nsIPresentationSessionTransportBuilderListener
|
|
|
|
NS_IMETHODIMP
|
2016-09-29 00:35:00 +03:00
|
|
|
PresentationPresentingInfo::OnSessionTransport(nsIPresentationSessionTransport* aTransport)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
2016-09-29 00:35:00 +03:00
|
|
|
nsresult rv = PresentationSessionInfo::OnSessionTransport(aTransport);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
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
|
2016-09-29 00:35:00 +03:00
|
|
|
if (NS_WARN_IF(!aTransport)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2016-06-03 06:03:27 +03:00
|
|
|
}
|
|
|
|
|
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));
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "GetSelfAddress failed");
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
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-09-05 13:18:11 +03:00
|
|
|
MOZ_ASSERT(mState == nsIPresentationSessionListener::STATE_CONNECTING);
|
2016-04-11 06:28:36 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mBuilderConstructor)) {
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(
|
|
|
|
mBuilderConstructor->CreateTransportBuilder(type,
|
|
|
|
getter_AddRefs(mBuilder))))) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
if (type == nsIPresentationChannelDescription::TYPE_TCP) {
|
|
|
|
// Establish a data transport channel |mTransport| to the sender and use
|
|
|
|
// |this| as the callback.
|
|
|
|
nsCOMPtr<nsIPresentationTCPSessionTransportBuilder> builder =
|
2016-09-16 15:59:00 +03:00
|
|
|
do_QueryInterface(mBuilder);
|
2016-04-11 06:20:55 +03:00
|
|
|
if (NS_WARN_IF(!builder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2016-09-16 15:59:00 +03:00
|
|
|
* |mBuilderConstructor| is responsible for creating a builder, which is for
|
|
|
|
* building a data channel transport.
|
2016-06-03 06:03:27 +03:00
|
|
|
*
|
2016-09-16 15:59:00 +03:00
|
|
|
* In the OOP case, |mBuilderConstructor| would create a builder which is
|
|
|
|
* an object of |PresentationBuilderParent|. So, |BuildDataChannelTransport|
|
|
|
|
* triggers an IPC call to make content process establish a RTCDataChannel
|
|
|
|
* transport.
|
2016-06-03 06:03:27 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
mTransportType = nsIPresentationChannelDescription::TYPE_DATACHANNEL;
|
2016-06-03 06:03:27 +03:00
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
nsCOMPtr<nsIPresentationDataChannelSessionTransportBuilder> dataChannelBuilder =
|
|
|
|
do_QueryInterface(mBuilder);
|
2016-06-03 06:03:27 +03:00
|
|
|
if (NS_WARN_IF(!dataChannelBuilder)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-09-16 15:59:00 +03:00
|
|
|
|
|
|
|
nsPIDOMWindowInner* window = GetWindow();
|
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
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-09-16 15:59:00 +03:00
|
|
|
rv = FlushPendingEvents(dataChannelBuilder);
|
2016-06-03 06:03:27 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-09-16 15:59:00 +03:00
|
|
|
|
2016-06-03 06:03:27 +03:00
|
|
|
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) {
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(!static_cast<ContentParent*>(mContentParent.get())->SendNotifyPresentationReceiverCleanUp(mSessionId));
|
2015-08-31 08:24:35 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 14:11:26 +03:00
|
|
|
// Receiver device might need clean up after session termination.
|
|
|
|
if (mDevice) {
|
|
|
|
mDevice->Disconnect();
|
|
|
|
}
|
|
|
|
mDevice = nullptr;
|
|
|
|
|
2015-08-31 08:24:35 +03:00
|
|
|
// 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
|
|
|
{
|
2016-09-05 13:18:11 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d], state[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole, mState);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
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()
|
|
|
|
{
|
2016-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole);
|
|
|
|
|
2016-08-04 04:46:14 +03:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReplyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:18:11 +03:00
|
|
|
nsresult
|
|
|
|
PresentationPresentingInfo::DoReconnect()
|
|
|
|
{
|
|
|
|
PRES_DEBUG("%s:id[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == nsIPresentationSessionListener::STATE_CLOSED);
|
|
|
|
|
|
|
|
SetStateWithReason(nsIPresentationSessionListener::STATE_CONNECTING, NS_OK);
|
|
|
|
|
|
|
|
return NotifyResponderReady();
|
|
|
|
}
|
|
|
|
|
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-08-15 13:26:13 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), mRole);
|
|
|
|
|
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
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("%s:id[%s], reason[%" PRIx32 "], role[%d]\n", __func__,
|
|
|
|
NS_ConvertUTF16toUTF8(mSessionId).get(), static_cast<uint32_t>(aReason),
|
|
|
|
mRole);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
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-09-02 10:12:24 +03:00
|
|
|
Unused << 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|.
|
2016-10-27 06:27:41 +03:00
|
|
|
// Use Element to support both HTMLIFrameElement and nsXULElement.
|
|
|
|
Element* frame = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Element, obj, frame);
|
2015-03-30 10:48:11 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-11 19:09:22 +03:00
|
|
|
nsCOMPtr<nsIFrameLoader> frameLoader = owner->GetFrameLoader();
|
|
|
|
if (NS_WARN_IF(!frameLoader)) {
|
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();
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << 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
|
|
|
}
|