2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-09-22 13:36:47 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
#include "PresentationReceiver.h"
|
|
|
|
|
2015-09-22 13:36:47 +03:00
|
|
|
#include "mozilla/dom/PresentationReceiverBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2016-08-02 20:11:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-09-22 13:36:47 +03:00
|
|
|
#include "nsIPresentationService.h"
|
2016-10-04 01:22:34 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-09-22 13:36:47 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-05-29 20:59:00 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-10-12 05:36:31 +03:00
|
|
|
#include "PresentationConnection.h"
|
2016-05-29 20:59:00 +03:00
|
|
|
#include "PresentationConnectionList.h"
|
2016-08-15 13:26:13 +03:00
|
|
|
#include "PresentationLog.h"
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PresentationReceiver, mOwner,
|
|
|
|
mGetConnectionListPromise,
|
|
|
|
mConnectionList)
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PresentationReceiver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PresentationReceiver)
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PresentationReceiver)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2015-09-22 13:36:47 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPresentationRespondingListener)
|
2016-10-20 06:27:55 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
2015-09-22 13:36:47 +03:00
|
|
|
|
|
|
|
/* static */ already_AddRefed<PresentationReceiver>
|
2016-05-17 06:18:16 +03:00
|
|
|
PresentationReceiver::Create(nsPIDOMWindowInner* aWindow) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PresentationReceiver> receiver = new PresentationReceiver(aWindow);
|
2016-05-17 06:18:16 +03:00
|
|
|
return NS_WARN_IF(!receiver->Init()) ? nullptr : receiver.forget();
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
PresentationReceiver::PresentationReceiver(nsPIDOMWindowInner* aWindow)
|
2018-04-13 16:01:28 +03:00
|
|
|
: mOwner(aWindow) {
|
2016-05-29 20:59:00 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PresentationReceiver::~PresentationReceiver() { Shutdown(); }
|
|
|
|
|
2016-05-17 06:18:16 +03:00
|
|
|
bool PresentationReceiver::Init() {
|
2016-05-29 20:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mOwner)) {
|
2015-09-22 13:36:47 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-29 20:59:00 +03:00
|
|
|
mWindowId = mOwner->WindowID();
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-08-02 20:11:00 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = mOwner->GetDocShell();
|
|
|
|
MOZ_ASSERT(docShell);
|
|
|
|
|
|
|
|
nsContentUtils::GetPresentationURL(docShell, mUrl);
|
|
|
|
return !mUrl.IsEmpty();
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PresentationReceiver::Shutdown() {
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("receiver shutdown:windowId[%" PRId64 "]\n", mWindowId);
|
2016-08-15 13:26:13 +03:00
|
|
|
|
2015-09-22 13:36:47 +03:00
|
|
|
// Unregister listener for incoming sessions.
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
NS_FAILED(service->UnregisterRespondingListener(mWindowId)));
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ JSObject* PresentationReceiver::WrapObject(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PresentationReceiver_Binding::Wrap(aCx, this, aGivenProto);
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PresentationReceiver::NotifySessionConnect(uint64_t aWindowId,
|
|
|
|
const nsAString& aSessionId) {
|
2016-12-16 06:16:31 +03:00
|
|
|
PRES_DEBUG("receiver session connect:id[%s], windowId[%" PRIx64 "]\n",
|
2016-08-15 13:26:13 +03:00
|
|
|
NS_ConvertUTF16toUTF8(aSessionId).get(), aWindowId);
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mOwner)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
if (NS_WARN_IF(aWindowId != mWindowId)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
if (NS_WARN_IF(!mConnectionList)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
RefPtr<PresentationConnection> connection = PresentationConnection::Create(
|
|
|
|
mOwner, aSessionId, mUrl, nsIPresentationService::ROLE_RECEIVER,
|
|
|
|
mConnectionList);
|
|
|
|
if (NS_WARN_IF(!connection)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
already_AddRefed<Promise> PresentationReceiver::GetConnectionList(
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mOwner);
|
2015-09-22 13:36:47 +03:00
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
if (!mGetConnectionListPromise) {
|
|
|
|
mGetConnectionListPromise = Promise::Create(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PresentationReceiver> self = this;
|
2017-06-12 22:34:10 +03:00
|
|
|
nsresult rv = NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"dom::PresentationReceiver::GetConnectionList",
|
|
|
|
[self]() -> void { self->CreateConnectionList(); }));
|
2016-05-29 20:59:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
RefPtr<Promise> promise = mGetConnectionListPromise;
|
2017-08-09 12:38:30 +03:00
|
|
|
if (nsContentUtils::ShouldResistFingerprinting()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
}
|
2015-09-22 13:36:47 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
void PresentationReceiver::CreateConnectionList() {
|
|
|
|
MOZ_ASSERT(mGetConnectionListPromise);
|
2016-03-05 00:37:50 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
if (mConnectionList) {
|
|
|
|
return;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
mConnectionList =
|
|
|
|
new PresentationConnectionList(mOwner, mGetConnectionListPromise);
|
2015-09-22 13:36:47 +03:00
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
// Register listener for incoming sessions.
|
|
|
|
nsCOMPtr<nsIPresentationService> service =
|
|
|
|
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
mGetConnectionListPromise->MaybeReject(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
return;
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
nsresult rv = service->RegisterRespondingListener(mWindowId, this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mGetConnectionListPromise->MaybeReject(rv);
|
|
|
|
}
|
2015-09-22 13:36:47 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 20:59:00 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|