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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-03-30 10:46:11 +03:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIPresentationService.h"
|
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-09-16 15:59:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-03-30 09:27:27 +03:00
|
|
|
#include "PresentationCallbacks.h"
|
2015-09-10 11:29:08 +03:00
|
|
|
#include "PresentationRequest.h"
|
2015-10-12 05:36:31 +03:00
|
|
|
#include "PresentationConnection.h"
|
2016-09-16 15:59:00 +03:00
|
|
|
#include "PresentationTransportBuilderConstructor.h"
|
2015-03-30 09:27:27 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
/*
|
|
|
|
* Implementation of PresentationRequesterCallback
|
|
|
|
*/
|
|
|
|
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_IMPL_ISUPPORTS(PresentationRequesterCallback, nsIPresentationServiceCallback)
|
|
|
|
|
2015-09-10 11:29:08 +03:00
|
|
|
PresentationRequesterCallback::PresentationRequesterCallback(PresentationRequest* aRequest,
|
2015-03-30 09:27:27 +03:00
|
|
|
const nsAString& aSessionId,
|
|
|
|
Promise* aPromise)
|
2015-09-10 11:29:08 +03:00
|
|
|
: mRequest(aRequest)
|
2015-03-30 09:27:27 +03:00
|
|
|
, mSessionId(aSessionId)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
{
|
2015-09-10 11:29:08 +03:00
|
|
|
MOZ_ASSERT(mRequest);
|
2015-03-30 09:27:27 +03:00
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
MOZ_ASSERT(!mSessionId.IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationRequesterCallback::~PresentationRequesterCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
// nsIPresentationServiceCallback
|
2015-03-30 09:27:27 +03:00
|
|
|
NS_IMETHODIMP
|
2016-09-05 02:17:00 +03:00
|
|
|
PresentationRequesterCallback::NotifySuccess(const nsAString& aUrl)
|
2015-03-30 09:27:27 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-09-05 02:17:00 +03:00
|
|
|
if (aUrl.IsEmpty()) {
|
|
|
|
return NotifyError(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationConnection> connection =
|
2016-09-05 02:17:00 +03:00
|
|
|
PresentationConnection::Create(mRequest->GetOwner(), mSessionId, aUrl,
|
2016-05-29 20:59:00 +03:00
|
|
|
nsIPresentationService::ROLE_CONTROLLER);
|
2015-10-12 05:36:31 +03:00
|
|
|
if (NS_WARN_IF(!connection)) {
|
2015-09-09 11:38:26 +03:00
|
|
|
mPromise->MaybeReject(NS_ERROR_DOM_OPERATION_ERR);
|
2015-03-30 09:27:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
mPromise->MaybeResolve(connection);
|
2015-09-10 11:29:08 +03:00
|
|
|
|
2015-10-12 05:36:31 +03:00
|
|
|
return mRequest->DispatchConnectionAvailableEvent(connection);
|
2015-03-30 09:27:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationRequesterCallback::NotifyError(nsresult aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mPromise->MaybeReject(aError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-30 10:46:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of PresentationRequesterCallback
|
|
|
|
*/
|
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(PresentationReconnectCallback,
|
|
|
|
PresentationRequesterCallback)
|
|
|
|
|
|
|
|
PresentationReconnectCallback::PresentationReconnectCallback(
|
|
|
|
PresentationRequest* aRequest,
|
|
|
|
const nsAString& aSessionId,
|
|
|
|
Promise* aPromise,
|
|
|
|
PresentationConnection* aConnection)
|
2016-09-05 02:17:00 +03:00
|
|
|
: PresentationRequesterCallback(aRequest, aSessionId, aPromise)
|
2016-08-02 20:11:00 +03:00
|
|
|
, mConnection(aConnection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationReconnectCallback::~PresentationReconnectCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-09-05 02:17:00 +03:00
|
|
|
PresentationReconnectCallback::NotifySuccess(const nsAString& aUrl)
|
2016-08-02 20:11:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
// We found a matched connection with the same window ID, URL, and
|
|
|
|
// the session ID. Resolve the promise with this connection and dispatch
|
|
|
|
// the event.
|
|
|
|
if (mConnection) {
|
2016-09-29 00:35:00 +03:00
|
|
|
mConnection->NotifyStateChange(
|
|
|
|
mSessionId,
|
|
|
|
nsIPresentationSessionListener::STATE_CONNECTING,
|
|
|
|
NS_OK);
|
2016-08-02 20:11:00 +03:00
|
|
|
mPromise->MaybeResolve(mConnection);
|
|
|
|
rv = mRequest->DispatchConnectionAvailableEvent(mConnection);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use |PresentationRequesterCallback::NotifySuccess| to create a new
|
|
|
|
// connection since we don't find one that can be reused.
|
2016-09-05 02:17:00 +03:00
|
|
|
rv = PresentationRequesterCallback::NotifySuccess(aUrl);
|
2016-08-02 20:11:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = service->UpdateWindowIdBySessionId(mSessionId,
|
2016-08-30 02:40:00 +03:00
|
|
|
nsIPresentationService::ROLE_CONTROLLER,
|
2016-08-02 20:11:00 +03:00
|
|
|
mRequest->GetOwner()->WindowID());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString sessionId = nsString(mSessionId);
|
|
|
|
return NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction([sessionId, service]() -> void {
|
|
|
|
service->BuildTransport(sessionId,
|
|
|
|
nsIPresentationService::ROLE_CONTROLLER);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationReconnectCallback::NotifyError(nsresult aError)
|
|
|
|
{
|
2016-09-29 00:35:00 +03:00
|
|
|
if (mConnection) {
|
|
|
|
mConnection->NotifyStateChange(
|
|
|
|
mSessionId,
|
|
|
|
nsIPresentationSessionListener::STATE_CLOSED,
|
|
|
|
aError);
|
|
|
|
}
|
2016-08-02 20:11:00 +03:00
|
|
|
return PresentationRequesterCallback::NotifyError(aError);
|
|
|
|
}
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
NS_IMPL_ISUPPORTS(PresentationResponderLoadingCallback,
|
|
|
|
nsIWebProgressListener,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
PresentationResponderLoadingCallback::PresentationResponderLoadingCallback(const nsAString& aSessionId)
|
|
|
|
: mSessionId(aSessionId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationResponderLoadingCallback::~PresentationResponderLoadingCallback()
|
|
|
|
{
|
|
|
|
if (mProgress) {
|
|
|
|
mProgress->RemoveProgressListener(this);
|
|
|
|
mProgress = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PresentationResponderLoadingCallback::Init(nsIDocShell* aDocShell)
|
|
|
|
{
|
|
|
|
mProgress = do_GetInterface(aDocShell);
|
|
|
|
if (NS_WARN_IF(!mProgress)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t busyFlags = nsIDocShell::BUSY_FLAGS_NONE;
|
|
|
|
nsresult rv = aDocShell->GetBusyFlags(&busyFlags);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-09-28 00:32:22 +03:00
|
|
|
if ((busyFlags == nsIDocShell::BUSY_FLAGS_NONE) ||
|
2015-03-30 10:46:11 +03:00
|
|
|
(busyFlags & nsIDocShell::BUSY_FLAGS_PAGE_LOADING)) {
|
|
|
|
// The docshell has finished loading or is receiving data (|STATE_TRANSFERRING|
|
|
|
|
// has already been fired), so the page is ready for presentation use.
|
2016-08-04 04:46:14 +03:00
|
|
|
return NotifyReceiverReady(/* isLoading = */ true);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start to listen to document state change event |STATE_TRANSFERRING|.
|
|
|
|
return mProgress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-08-04 04:46:14 +03:00
|
|
|
PresentationResponderLoadingCallback::NotifyReceiverReady(bool aIsLoading)
|
2015-03-30 10:46:11 +03:00
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(mProgress);
|
2015-08-31 08:24:35 +03:00
|
|
|
if (NS_WARN_IF(!window || !window->GetCurrentInnerWindow())) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
uint64_t windowId = window->GetCurrentInnerWindow()->WindowID();
|
|
|
|
|
2015-03-30 10:46:11 +03:00
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:59:00 +03:00
|
|
|
nsCOMPtr<nsIPresentationTransportBuilderConstructor> constructor =
|
|
|
|
PresentationTransportBuilderConstructor::Create();
|
|
|
|
return service->NotifyReceiverReady(mSessionId,
|
|
|
|
windowId,aIsLoading,
|
|
|
|
constructor);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIWebProgressListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationResponderLoadingCallback::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
uint32_t aStateFlags,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-08-04 04:46:14 +03:00
|
|
|
if (aStateFlags & (nsIWebProgressListener::STATE_TRANSFERRING |
|
|
|
|
nsIWebProgressListener::STATE_STOP)) {
|
2015-03-30 10:46:11 +03:00
|
|
|
mProgress->RemoveProgressListener(this);
|
|
|
|
|
2016-08-04 04:46:14 +03:00
|
|
|
bool isLoading = aStateFlags & nsIWebProgressListener::STATE_TRANSFERRING;
|
|
|
|
return NotifyReceiverReady(isLoading);
|
2015-03-30 10:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationResponderLoadingCallback::OnProgressChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
int32_t aCurSelfProgress,
|
|
|
|
int32_t aMaxSelfProgress,
|
|
|
|
int32_t aCurTotalProgress,
|
|
|
|
int32_t aMaxTotalProgress)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationResponderLoadingCallback::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsIURI* aURI,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationResponderLoadingCallback::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const char16_t* aMessage)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationResponderLoadingCallback::OnSecurityChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
uint32_t state)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
return NS_OK;
|
|
|
|
}
|