1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/* -*- 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 "RefMessageBodyService.h"
#include "StructuredCloneData.h"
#include "nsContentUtils.h"
namespace mozilla {
namespace dom {
StaticMutex sRefMessageBodyServiceMutex;
// Raw pointer because the service is kept alive by other objects.
// See the CTOR and the DTOR of this object.
RefMessageBodyService* sService;
// static
already_AddRefed<RefMessageBodyService> RefMessageBodyService::GetOrCreate() {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
RefPtr<RefMessageBodyService> service = GetOrCreateInternal(lock);
return service.forget();
}
// static
RefMessageBodyService* RefMessageBodyService::GetOrCreateInternal(
const StaticMutexAutoLock& aProofOfLock) {
if (!sService) {
sService = new RefMessageBodyService();
}
return sService;
}
RefMessageBodyService::RefMessageBodyService() {
MOZ_DIAGNOSTIC_ASSERT(sService == nullptr);
}
RefMessageBodyService::~RefMessageBodyService() {
MOZ_DIAGNOSTIC_ASSERT(sService == this);
sService = nullptr;
}
const nsID RefMessageBodyService::Register(
already_AddRefed<RefMessageBody> aBody, ErrorResult& aRv) {
RefPtr<RefMessageBody> body = aBody;
MOZ_ASSERT(body);
nsID uuid = {};
aRv = nsContentUtils::GenerateUUIDInPlace(uuid);
if (NS_WARN_IF(aRv.Failed())) {
return nsID();
}
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
GetOrCreateInternal(lock)->mMessages.Put(uuid, std::move(body));
return uuid;
}
already_AddRefed<RefMessageBody> RefMessageBodyService::Steal(const nsID& aID) {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
if (!sService) {
return nullptr;
}
RefPtr<RefMessageBody> body;
sService->mMessages.Remove(aID, getter_AddRefs(body));
return body.forget();
}
already_AddRefed<RefMessageBody> RefMessageBodyService::GetAndCount(
const nsID& aID) {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
if (!sService) {
return nullptr;
}
RefPtr<RefMessageBody> body = sService->mMessages.Get(aID);
if (!body) {
return nullptr;
}
++body->mCount;
MOZ_ASSERT_IF(body->mMaxCount.isSome(),
body->mCount <= body->mMaxCount.value());
if (body->mMaxCount.isSome() && body->mCount >= body->mMaxCount.value()) {
sService->mMessages.Remove(aID);
}
return body.forget();
}
void RefMessageBodyService::SetMaxCount(const nsID& aID, uint32_t aMaxCount) {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
if (!sService) {
return;
}
RefPtr<RefMessageBody> body = sService->mMessages.Get(aID);
if (!body) {
return;
}
MOZ_ASSERT(body->mMaxCount.isNothing());
body->mMaxCount.emplace(aMaxCount);
MOZ_ASSERT(body->mCount <= body->mMaxCount.value());
if (body->mCount >= body->mMaxCount.value()) {
sService->mMessages.Remove(aID);
}
}
void RefMessageBodyService::ForgetPort(const nsID& aPortID) {
StaticMutexAutoLock lock(sRefMessageBodyServiceMutex);
if (!sService) {
return;
}
for (auto iter = sService->mMessages.ConstIter(); !iter.Done(); iter.Next()) {
if (iter.UserData()->PortID() == aPortID) {
iter.Remove();
}
}
}
RefMessageBody::RefMessageBody(const nsID& aPortID,
UniquePtr<ipc::StructuredCloneData>&& aCloneData)
: mPortID(aPortID),
mMutex("RefMessageBody::mMutex"),
mCloneData(std::move(aCloneData)),
mMaxCount(Nothing()),
mCount(0) {}
void RefMessageBody::Read(JSContext* aCx, JS::MutableHandle<JS::Value> aValue,
const JS::CloneDataPolicy& aCloneDataPolicy,
ErrorResult& aRv) {
MutexAutoLock lock(mMutex);
mCloneData->Read(aCx, aValue, aCloneDataPolicy, aRv);
}
bool RefMessageBody::TakeTransferredPortsAsSequence(
Sequence<OwningNonNull<mozilla::dom::MessagePort>>& aPorts) {
MOZ_ASSERT(mMaxCount.isNothing());
return mCloneData->TakeTransferredPortsAsSequence(aPorts);
}
} // namespace dom
} // namespace mozilla
|