gecko-dev/dom/presentation/PresentationReceiver.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 строки
4.9 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "PresentationReceiver.h"
#include "mozilla/dom/PresentationReceiverBinding.h"
#include "mozilla/dom/Promise.h"
#include "nsContentUtils.h"
#include "nsIPresentationService.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "PresentationConnection.h"
#include "PresentationConnectionList.h"
#include "PresentationLog.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PresentationReceiver, mOwner,
mGetConnectionListPromise,
mConnectionList)
NS_IMPL_CYCLE_COLLECTING_ADDREF(PresentationReceiver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(PresentationReceiver)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PresentationReceiver)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIPresentationRespondingListener)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
/* static */ already_AddRefed<PresentationReceiver>
PresentationReceiver::Create(nsPIDOMWindowInner* aWindow) {
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<PresentationReceiver> receiver = new PresentationReceiver(aWindow);
return NS_WARN_IF(!receiver->Init()) ? nullptr : receiver.forget();
}
PresentationReceiver::PresentationReceiver(nsPIDOMWindowInner* aWindow)
: mOwner(aWindow) {
MOZ_ASSERT(aWindow);
}
PresentationReceiver::~PresentationReceiver() { Shutdown(); }
bool PresentationReceiver::Init() {
if (NS_WARN_IF(!mOwner)) {
return false;
}
mWindowId = mOwner->WindowID();
nsCOMPtr<nsIDocShell> docShell = mOwner->GetDocShell();
MOZ_ASSERT(docShell);
nsContentUtils::GetPresentationURL(docShell, mUrl);
return !mUrl.IsEmpty();
}
void PresentationReceiver::Shutdown() {
PRES_DEBUG("receiver shutdown:windowId[%" PRId64 "]\n", mWindowId);
// Unregister listener for incoming sessions.
nsCOMPtr<nsIPresentationService> service =
do_GetService(PRESENTATION_SERVICE_CONTRACTID);
if (NS_WARN_IF(!service)) {
return;
}
Unused << NS_WARN_IF(
NS_FAILED(service->UnregisterRespondingListener(mWindowId)));
}
/* virtual */ JSObject* PresentationReceiver::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return PresentationReceiver_Binding::Wrap(aCx, this, aGivenProto);
}
NS_IMETHODIMP
PresentationReceiver::NotifySessionConnect(uint64_t aWindowId,
const nsAString& aSessionId) {
PRES_DEBUG("receiver session connect:id[%s], windowId[%" PRIx64 "]\n",
NS_ConvertUTF16toUTF8(aSessionId).get(), aWindowId);
if (NS_WARN_IF(!mOwner)) {
return NS_ERROR_FAILURE;
}
if (NS_WARN_IF(aWindowId != mWindowId)) {
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!mConnectionList)) {
return NS_ERROR_FAILURE;
}
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;
}
already_AddRefed<Promise> PresentationReceiver::GetConnectionList(
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mOwner);
if (NS_WARN_IF(!global)) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
if (!mGetConnectionListPromise) {
mGetConnectionListPromise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
RefPtr<PresentationReceiver> self = this;
nsresult rv = NS_DispatchToMainThread(NS_NewRunnableFunction(
"dom::PresentationReceiver::GetConnectionList",
[self]() -> void { self->CreateConnectionList(); }));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
}
RefPtr<Promise> promise = mGetConnectionListPromise;
if (nsContentUtils::ShouldResistFingerprinting()) {
promise->MaybeReject(NS_ERROR_DOM_SECURITY_ERR);
}
return promise.forget();
}
void PresentationReceiver::CreateConnectionList() {
MOZ_ASSERT(mGetConnectionListPromise);
if (mConnectionList) {
return;
}
mConnectionList =
new PresentationConnectionList(mOwner, mGetConnectionListPromise);
// 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;
}
nsresult rv = service->RegisterRespondingListener(mWindowId, this);
if (NS_WARN_IF(NS_FAILED(rv))) {
mGetConnectionListPromise->MaybeReject(rv);
}
}
} // namespace dom
} // namespace mozilla