2015-11-19 06:38:55 +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-06-17 13:44:27 +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 "MessagePortService.h"
|
|
|
|
#include "MessagePortParent.h"
|
|
|
|
#include "SharedMessagePortMessage.h"
|
2015-06-24 01:50:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
2015-06-24 01:50:00 +03:00
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
StaticRefPtr<MessagePortService> gInstance;
|
2015-06-24 01:50:00 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsInMainProcess()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
class MessagePortService::MessagePortServiceData final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit MessagePortServiceData(const nsID& aDestinationUUID)
|
|
|
|
: mDestinationUUID(aDestinationUUID)
|
|
|
|
, mSequenceID(1)
|
|
|
|
, mParent(nullptr)
|
2015-11-18 02:38:01 +03:00
|
|
|
// By default we don't know the next parent.
|
|
|
|
, mWaitingForNewParent(true)
|
|
|
|
, mNextStepCloseAll(false)
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MessagePortServiceData);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagePortServiceData(const MessagePortServiceData& aOther) = delete;
|
|
|
|
MessagePortServiceData& operator=(const MessagePortServiceData&) = delete;
|
|
|
|
|
|
|
|
~MessagePortServiceData()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MessagePortServiceData);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsID mDestinationUUID;
|
|
|
|
|
|
|
|
uint32_t mSequenceID;
|
|
|
|
MessagePortParent* mParent;
|
|
|
|
|
|
|
|
struct NextParent
|
|
|
|
{
|
|
|
|
uint32_t mSequenceID;
|
|
|
|
// MessagePortParent keeps the service alive, and we don't want a cycle.
|
|
|
|
MessagePortParent* mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
FallibleTArray<NextParent> mNextParents;
|
2015-10-18 08:24:48 +03:00
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>> mMessages;
|
2015-11-18 02:38:01 +03:00
|
|
|
|
|
|
|
bool mWaitingForNewParent;
|
|
|
|
bool mNextStepCloseAll;
|
2015-06-17 13:44:27 +03:00
|
|
|
};
|
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
/* static */ MessagePortService*
|
|
|
|
MessagePortService::Get()
|
|
|
|
{
|
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
/* static */ MessagePortService*
|
|
|
|
MessagePortService::GetOrCreate()
|
|
|
|
{
|
2015-06-24 01:50:00 +03:00
|
|
|
AssertIsInMainProcess();
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
if (!gInstance) {
|
|
|
|
gInstance = new MessagePortService();
|
|
|
|
}
|
|
|
|
|
|
|
|
return gInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePortService::RequestEntangling(MessagePortParent* aParent,
|
|
|
|
const nsID& aDestinationUUID,
|
|
|
|
const uint32_t& aSequenceID)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
|
|
|
|
// If we don't have a MessagePortServiceData, we must create 2 of them for
|
|
|
|
// both ports.
|
|
|
|
if (!mPorts.Get(aParent->ID(), &data)) {
|
|
|
|
// Create the MessagePortServiceData for the destination.
|
|
|
|
if (mPorts.Get(aDestinationUUID, nullptr)) {
|
|
|
|
MOZ_ASSERT(false, "The creation of the 2 ports should be in sync.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = new MessagePortServiceData(aParent->ID());
|
|
|
|
mPorts.Put(aDestinationUUID, data);
|
|
|
|
|
|
|
|
data = new MessagePortServiceData(aDestinationUUID);
|
|
|
|
mPorts.Put(aParent->ID(), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a security check.
|
|
|
|
if (!data->mDestinationUUID.Equals(aDestinationUUID)) {
|
|
|
|
MOZ_ASSERT(false, "DestinationUUIDs do not match!");
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseAll(aParent->ID());
|
2015-06-17 13:44:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSequenceID < data->mSequenceID) {
|
|
|
|
MOZ_ASSERT(false, "Invalid sequence ID!");
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseAll(aParent->ID());
|
2015-06-17 13:44:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSequenceID == data->mSequenceID) {
|
|
|
|
if (data->mParent) {
|
|
|
|
MOZ_ASSERT(false, "Two ports cannot have the same sequenceID.");
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseAll(aParent->ID());
|
2015-06-17 13:44:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We activate this port, sending all the messages.
|
|
|
|
data->mParent = aParent;
|
2015-11-18 02:38:01 +03:00
|
|
|
data->mWaitingForNewParent = false;
|
2016-11-08 12:50:51 +03:00
|
|
|
|
|
|
|
// We want to ensure we clear data->mMessages even if we early return, while
|
|
|
|
// also ensuring that its contents remain alive until after array's contents
|
|
|
|
// are destroyed because of JSStructuredCloneData borrowing. So we use
|
|
|
|
// Move to initialize things swapped and do it before we declare `array` so
|
|
|
|
// that reverse destruction order works for us.
|
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>>
|
2018-05-30 22:15:35 +03:00
|
|
|
messages(std::move(data->mMessages));
|
2016-11-08 12:50:51 +03:00
|
|
|
FallibleTArray<ClonedMessageData> array;
|
2015-06-17 13:44:27 +03:00
|
|
|
if (!SharedMessagePortMessage::FromSharedToMessagesParent(aParent,
|
2016-11-08 12:50:51 +03:00
|
|
|
messages,
|
2015-06-17 13:44:27 +03:00
|
|
|
array)) {
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseAll(aParent->ID());
|
2015-06-17 13:44:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:01 +03:00
|
|
|
// We can entangle the port.
|
|
|
|
if (!aParent->Entangled(array)) {
|
|
|
|
CloseAll(aParent->ID());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were waiting for this parent in order to close this channel, this
|
|
|
|
// is the time to do it.
|
|
|
|
if (data->mNextStepCloseAll) {
|
|
|
|
CloseAll(aParent->ID());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This new parent will be the next one when a Disentangle request is
|
|
|
|
// received from the current parent.
|
|
|
|
MessagePortServiceData::NextParent* nextParent =
|
|
|
|
data->mNextParents.AppendElement(mozilla::fallible);
|
|
|
|
if (!nextParent) {
|
2015-11-18 02:38:01 +03:00
|
|
|
CloseAll(aParent->ID());
|
2015-06-17 13:44:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextParent->mSequenceID = aSequenceID;
|
|
|
|
nextParent->mParent = aParent;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePortService::DisentanglePort(
|
|
|
|
MessagePortParent* aParent,
|
2015-10-18 08:24:48 +03:00
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>>& aMessages)
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aParent->ID(), &data)) {
|
|
|
|
MOZ_ASSERT(false, "Unknown MessagePortParent should not happen.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mParent != aParent) {
|
|
|
|
MOZ_ASSERT(false, "DisentanglePort() should be called just from the correct parent.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's put the messages in the correct order. |aMessages| contains the
|
|
|
|
// unsent messages so they have to go first.
|
|
|
|
if (!aMessages.AppendElements(data->mMessages, mozilla::fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->mMessages.Clear();
|
|
|
|
|
|
|
|
++data->mSequenceID;
|
|
|
|
|
|
|
|
// If we don't have a parent, we have to store the pending messages and wait.
|
|
|
|
uint32_t index = 0;
|
|
|
|
MessagePortParent* nextParent = nullptr;
|
|
|
|
for (; index < data->mNextParents.Length(); ++index) {
|
|
|
|
if (data->mNextParents[index].mSequenceID == data->mSequenceID) {
|
|
|
|
nextParent = data->mNextParents[index].mParent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find the parent.
|
|
|
|
if (!nextParent) {
|
|
|
|
data->mMessages.SwapElements(aMessages);
|
2015-11-18 02:38:01 +03:00
|
|
|
data->mWaitingForNewParent = true;
|
2015-06-17 13:44:27 +03:00
|
|
|
data->mParent = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->mParent = nextParent;
|
|
|
|
data->mNextParents.RemoveElementAt(index);
|
|
|
|
|
2016-11-08 12:50:51 +03:00
|
|
|
FallibleTArray<ClonedMessageData> array;
|
2015-06-17 13:44:27 +03:00
|
|
|
if (!SharedMessagePortMessage::FromSharedToMessagesParent(data->mParent,
|
|
|
|
aMessages,
|
|
|
|
array)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << data->mParent->Entangled(array);
|
2015-06-17 13:44:27 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePortService::ClosePort(MessagePortParent* aParent)
|
|
|
|
{
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aParent->ID(), &data)) {
|
|
|
|
MOZ_ASSERT(false, "Unknown MessagePortParent should not happend.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mParent != aParent) {
|
|
|
|
MOZ_ASSERT(false, "ClosePort() should be called just from the correct parent.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->mNextParents.IsEmpty()) {
|
|
|
|
MOZ_ASSERT(false, "ClosePort() should be called when there are not next parents.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to send a message to this parent.
|
|
|
|
data->mParent = nullptr;
|
|
|
|
|
|
|
|
CloseAll(aParent->ID());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-18 02:38:13 +03:00
|
|
|
MessagePortService::CloseAll(const nsID& aUUID, bool aForced)
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aUUID, &data)) {
|
|
|
|
MaybeShutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mParent) {
|
|
|
|
data->mParent->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const MessagePortServiceData::NextParent& parent : data->mNextParents) {
|
|
|
|
parent.mParent->CloseAndDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsID destinationUUID = data->mDestinationUUID;
|
2015-11-18 02:38:01 +03:00
|
|
|
|
|
|
|
// If we have informations about the other port and that port has some
|
|
|
|
// pending messages to deliver but the parent has not processed them yet,
|
|
|
|
// because its entangling request didn't arrive yet), we cannot close this
|
|
|
|
// channel.
|
|
|
|
MessagePortServiceData* destinationData;
|
2015-11-18 02:38:13 +03:00
|
|
|
if (!aForced &&
|
|
|
|
mPorts.Get(destinationUUID, &destinationData) &&
|
2015-11-18 02:38:01 +03:00
|
|
|
!destinationData->mMessages.IsEmpty() &&
|
|
|
|
destinationData->mWaitingForNewParent) {
|
|
|
|
MOZ_ASSERT(!destinationData->mNextStepCloseAll);
|
|
|
|
destinationData->mNextStepCloseAll = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
mPorts.Remove(aUUID);
|
|
|
|
|
2015-11-18 02:38:13 +03:00
|
|
|
CloseAll(destinationUUID, aForced);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
// CloseAll calls itself recursively and it can happen that it deletes
|
|
|
|
// itself. Before continuing we must check if we are still alive.
|
|
|
|
if (!gInstance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
#ifdef DEBUG
|
2015-11-19 06:38:55 +03:00
|
|
|
for (auto iter = mPorts.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
MOZ_ASSERT(!aUUID.Equals(iter.Key()));
|
|
|
|
}
|
2015-06-17 13:44:27 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MaybeShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This service can be dismissed when there are not active ports.
|
|
|
|
void
|
|
|
|
MessagePortService::MaybeShutdown()
|
|
|
|
{
|
|
|
|
if (mPorts.Count() == 0) {
|
|
|
|
gInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePortService::PostMessages(
|
|
|
|
MessagePortParent* aParent,
|
2015-10-18 08:24:48 +03:00
|
|
|
FallibleTArray<RefPtr<SharedMessagePortMessage>>& aMessages)
|
2015-06-17 13:44:27 +03:00
|
|
|
{
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aParent->ID(), &data)) {
|
|
|
|
MOZ_ASSERT(false, "Unknown MessagePortParent should not happend.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mParent != aParent) {
|
|
|
|
MOZ_ASSERT(false, "PostMessages() should be called just from the correct parent.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(mPorts.Get(data->mDestinationUUID, &data));
|
|
|
|
|
|
|
|
if (!data->mMessages.AppendElements(aMessages, mozilla::fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the parent can send data to the child, let's proceed.
|
|
|
|
if (data->mParent && data->mParent->CanSendData()) {
|
2016-11-08 12:50:51 +03:00
|
|
|
{
|
|
|
|
FallibleTArray<ClonedMessageData> messages;
|
|
|
|
if (!SharedMessagePortMessage::FromSharedToMessagesParent(data->mParent,
|
|
|
|
data->mMessages,
|
|
|
|
messages)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << data->mParent->SendReceiveData(messages);
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
2016-11-08 12:50:51 +03:00
|
|
|
// `messages` borrows the underlying JSStructuredCloneData so we need to
|
|
|
|
// avoid destroying the `mMessages` until after we've destroyed `messages`.
|
2015-06-17 13:44:27 +03:00
|
|
|
data->mMessages.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePortService::ParentDestroy(MessagePortParent* aParent)
|
|
|
|
{
|
|
|
|
// This port has already been destroyed.
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aParent->ID(), &data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mParent != aParent) {
|
|
|
|
// We don't want to send a message to this parent.
|
|
|
|
for (uint32_t i = 0; i < data->mNextParents.Length(); ++i) {
|
|
|
|
if (aParent == data->mNextParents[i].mParent) {
|
|
|
|
data->mNextParents.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAll(aParent->ID());
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:50:00 +03:00
|
|
|
bool
|
|
|
|
MessagePortService::ForceClose(const nsID& aUUID,
|
|
|
|
const nsID& aDestinationUUID,
|
|
|
|
const uint32_t& aSequenceID)
|
|
|
|
{
|
|
|
|
MessagePortServiceData* data;
|
|
|
|
if (!mPorts.Get(aUUID, &data)) {
|
|
|
|
NS_WARNING("Unknown MessagePort in ForceClose()");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->mDestinationUUID.Equals(aDestinationUUID) ||
|
|
|
|
data->mSequenceID != aSequenceID) {
|
|
|
|
NS_WARNING("DestinationUUID and/or sequenceID do not match.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-18 02:38:13 +03:00
|
|
|
CloseAll(aUUID, true);
|
2015-06-24 01:50:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|