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;
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-30 11:48:40 +03:00
|
|
|
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
|
|
|
nsTArray<BroadcastChannelParent*>* 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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-30 11:48:40 +03:00
|
|
|
BroadcastChannelService::UnregisterActor(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
|
|
|
DebugOnly<bool> found = false;
|
|
|
|
mAgents.LookupRemoveIf(aOriginChannelKey,
|
|
|
|
[&found, aParent] (nsTArray<BroadcastChannelParent*>* aValue) {
|
|
|
|
found = true;
|
|
|
|
aValue->RemoveElement(aParent);
|
|
|
|
return aValue->IsEmpty(); // remove the entry if the array is now empty
|
|
|
|
});
|
|
|
|
MOZ_ASSERT(found, "Invalid state");
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelService::PostMessage(BroadcastChannelParent* aParent,
|
2015-01-15 19:58:41 +03:00
|
|
|
const ClonedMessageData& 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;
|
|
|
|
if (!aData.blobs().IsEmpty()) {
|
|
|
|
blobImpls.SetCapacity(aData.blobs().Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0, len = aData.blobs().Length(); i < len; ++i) {
|
|
|
|
RefPtr<BlobImpl> impl = IPCBlobUtils::Deserialize(aData.blobs()[i]);
|
|
|
|
|
|
|
|
MOZ_ASSERT(impl);
|
|
|
|
blobImpls.AppendElement(impl);
|
2015-07-16 05:38:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// We need to have a copy of the data for this parent.
|
|
|
|
ClonedMessageData newData(aData);
|
|
|
|
MOZ_ASSERT(blobImpls.Length() == newData.blobs().Length());
|
|
|
|
|
|
|
|
if (!blobImpls.IsEmpty()) {
|
|
|
|
// 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(),
|
|
|
|
newData.blobs()[i]);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << parent->SendNotify(newData);
|
2015-07-16 05:38:48 +03:00
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|