2015-05-03 22:32:37 +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-01-15 19:58:40 +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/. */
|
|
|
|
|
|
|
|
#include "BroadcastChannelService.h"
|
|
|
|
#include "BroadcastChannelParent.h"
|
2015-01-17 10:29:15 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2017-04-24 13:09:40 +03:00
|
|
|
#include "mozilla/dom/IPCBlobUtils.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
|
2015-01-15 19:58:42 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
# undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
BroadcastChannelService* sInstance = nullptr;
|
|
|
|
|
2020-02-01 18:16:43 +03:00
|
|
|
ClonedMessageData CloneClonedMessageData(const ClonedMessageData& aOther) {
|
|
|
|
auto cloneData = SerializedStructuredCloneBuffer{};
|
|
|
|
cloneData.data.initScope(aOther.data().data.scope());
|
|
|
|
const bool res = cloneData.data.Append(aOther.data().data);
|
|
|
|
MOZ_RELEASE_ASSERT(res, "out of memory");
|
|
|
|
return {std::move(cloneData), aOther.blobs(), aOther.inputStreams(),
|
|
|
|
aOther.identifiers()};
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageData CloneMessageData(const MessageData& aOther) {
|
|
|
|
switch (aOther.data().type()) {
|
|
|
|
case MessageDataType::TClonedMessageData:
|
|
|
|
return {aOther.agentClusterId(),
|
|
|
|
CloneClonedMessageData(aOther.data().get_ClonedMessageData())};
|
|
|
|
case MessageDataType::TRefMessageData:
|
|
|
|
return {aOther.agentClusterId(), aOther.data().get_RefMessageData()};
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unexpected MessageDataType type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
BroadcastChannelService::BroadcastChannelService() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// sInstance is a raw BroadcastChannelService*.
|
|
|
|
MOZ_ASSERT(!sInstance);
|
|
|
|
sInstance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadcastChannelService::~BroadcastChannelService() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(sInstance == this);
|
|
|
|
MOZ_ASSERT(mAgents.Count() == 0);
|
|
|
|
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<BroadcastChannelService>
|
|
|
|
BroadcastChannelService::GetOrCreate() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BroadcastChannelService> instance = sInstance;
|
2015-01-15 19:58:40 +03:00
|
|
|
if (!instance) {
|
|
|
|
instance = new BroadcastChannelService();
|
|
|
|
}
|
|
|
|
return instance.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-30 11:48:40 +03:00
|
|
|
void BroadcastChannelService::RegisterActor(
|
|
|
|
BroadcastChannelParent* aParent, const nsAString& aOriginChannelKey) {
|
2015-01-15 19:58:40 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
2017-06-14 17:11:21 +03:00
|
|
|
const auto& parents = mAgents.LookupForAdd(aOriginChannelKey).OrInsert([]() {
|
|
|
|
return new nsTArray<BroadcastChannelParent*>();
|
|
|
|
});
|
2016-05-30 11:48:40 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!parents->Contains(aParent));
|
|
|
|
parents->AppendElement(aParent);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2016-05-30 11:48:40 +03:00
|
|
|
void BroadcastChannelService::UnregisterActor(
|
|
|
|
BroadcastChannelParent* aParent, const nsAString& aOriginChannelKey) {
|
2015-01-15 19:58:40 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = mAgents.Lookup(aOriginChannelKey)) {
|
|
|
|
entry.Data()->RemoveElement(aParent);
|
|
|
|
// remove the entry if the array is now empty
|
|
|
|
if (entry.Data()->IsEmpty()) {
|
|
|
|
entry.Remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Invalid state");
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BroadcastChannelService::PostMessage(BroadcastChannelParent* aParent,
|
2020-01-24 11:00:08 +03:00
|
|
|
const MessageData& aData,
|
2016-05-30 11:48:40 +03:00
|
|
|
const nsAString& aOriginChannelKey) {
|
2015-01-15 19:58:40 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_ASSERT(aParent);
|
2016-05-30 11:48:40 +03:00
|
|
|
|
|
|
|
nsTArray<BroadcastChannelParent*>* parents;
|
|
|
|
if (!mAgents.Get(aOriginChannelKey, &parents)) {
|
|
|
|
MOZ_CRASH("Invalid state");
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
2015-07-16 05:38:48 +03:00
|
|
|
// We need to keep the array alive for the life-time of this operation.
|
2017-04-24 13:09:40 +03:00
|
|
|
nsTArray<RefPtr<BlobImpl>> blobImpls;
|
2020-01-24 19:19:59 +03:00
|
|
|
if (aData.data().type() == MessageDataType::TClonedMessageData) {
|
|
|
|
const nsTArray<IPCBlob>& blobs =
|
|
|
|
aData.data().get_ClonedMessageData().blobs();
|
2020-01-24 11:00:08 +03:00
|
|
|
if (!blobs.IsEmpty()) {
|
|
|
|
blobImpls.SetCapacity(blobs.Length());
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
for (uint32_t i = 0, len = blobs.Length(); i < len; ++i) {
|
|
|
|
RefPtr<BlobImpl> impl = IPCBlobUtils::Deserialize(blobs[i]);
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
MOZ_ASSERT(impl);
|
|
|
|
blobImpls.AppendElement(impl);
|
|
|
|
}
|
2015-07-16 05:38:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
uint32_t selectedActorsOnSamePid = 0;
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
// For each parent actor, we notify the message.
|
2016-05-30 11:48:40 +03:00
|
|
|
for (uint32_t i = 0; i < parents->Length(); ++i) {
|
|
|
|
BroadcastChannelParent* parent = parents->ElementAt(i);
|
2015-07-16 05:38:48 +03:00
|
|
|
MOZ_ASSERT(parent);
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
if (parent == aParent) {
|
|
|
|
continue;
|
2015-07-16 05:38:48 +03:00
|
|
|
}
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
if (parent->OtherPid() == aParent->OtherPid()) {
|
|
|
|
++selectedActorsOnSamePid;
|
|
|
|
}
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
// We need to have a copy of the data for this parent.
|
2020-02-01 18:16:43 +03:00
|
|
|
MessageData newData = CloneMessageData(aData);
|
2020-01-24 19:19:59 +03:00
|
|
|
MOZ_ASSERT(newData.data().type() == aData.data().type());
|
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
if (!blobImpls.IsEmpty()) {
|
2020-01-24 19:19:59 +03:00
|
|
|
nsTArray<IPCBlob>& newBlobImpls =
|
|
|
|
newData.data().get_ClonedMessageData().blobs();
|
2020-01-24 11:00:08 +03:00
|
|
|
MOZ_ASSERT(blobImpls.Length() == newBlobImpls.Length());
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
// Serialize Blob objects for this message.
|
|
|
|
for (uint32_t i = 0, len = blobImpls.Length(); i < len; ++i) {
|
|
|
|
nsresult rv = IPCBlobUtils::Serialize(blobImpls[i], parent->Manager(),
|
2020-01-24 11:00:08 +03:00
|
|
|
newBlobImpls[i]);
|
2017-04-24 13:09:40 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << parent->SendNotify(newData);
|
2015-07-16 05:38:48 +03:00
|
|
|
}
|
2020-01-24 11:00:08 +03:00
|
|
|
|
|
|
|
// If this is a refMessageData, we need to know when it can be released.
|
2020-01-24 19:19:59 +03:00
|
|
|
if (aData.data().type() == MessageDataType::TRefMessageData) {
|
2020-01-24 11:00:08 +03:00
|
|
|
Unused << aParent->SendRefMessageDelivered(
|
2020-01-24 19:19:59 +03:00
|
|
|
aData.data().get_RefMessageData().uuid(), selectedActorsOnSamePid);
|
2020-01-24 11:00:08 +03:00
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|