2020-01-24 10:58:34 +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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_RefMessageBodyService_h
|
|
|
|
#define mozilla_dom_RefMessageBodyService_h
|
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
2020-01-24 11:00:08 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2020-01-24 10:58:34 +03:00
|
|
|
#include "mozilla/StaticMutex.h"
|
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
/**
|
|
|
|
* At the time a BroadcastChannel or MessagePort sends messages, we don't know
|
|
|
|
* which process is going to receive it. Because of this, we need to check if
|
|
|
|
* the message is able to cross the process boundary.
|
|
|
|
* If the message contains objects such as SharedArrayBuffers, WASM modules or
|
|
|
|
* ImageBitmaps, it can be delivered on the current process only.
|
|
|
|
* Instead of sending the whole message via IPC, we send a unique ID, while the
|
|
|
|
* message is kept alive by RefMessageBodyService, on the current process using
|
|
|
|
* a ref-counted RefMessageBody object.
|
|
|
|
* When the receiver obtains the message ID, it checks if the local
|
|
|
|
* RefMessageBodyService knows that ID. If yes, the sender and the receiver are
|
|
|
|
* on the same process and the delivery can be completed; if not, a
|
|
|
|
* messageerror event has to be dispatched instead.
|
|
|
|
*
|
|
|
|
* For MessagePort communication is 1-to-1 and because of this, the
|
|
|
|
* receiver takes ownership of the message (RefMessageBodyService::Steal()).
|
|
|
|
* If the receiver port is on a different process, RefMessageBodyService will
|
|
|
|
* return a nullptr and a messageerror event will be dispatched.
|
|
|
|
|
|
|
|
* For BroadcastChannel, the life-time of a message is a bit different than for
|
|
|
|
* MessagePort. It has a 1-to-many communication and we could have multiple
|
|
|
|
* receivers. Each one needs to deliver the message without taking full
|
|
|
|
* ownership of it.
|
|
|
|
* In order to support this feature, BroadcastChannel needs to call
|
|
|
|
* RefMessageBodyService::SetMaxCount() to inform how many ports are allowed to
|
|
|
|
* retrieve the current message, on the current process. Receivers on other
|
|
|
|
* processes are not kept in consideration because they will not be able to
|
|
|
|
* retrieve the message from RefMessageBodyService. When the last allowed
|
|
|
|
* port has called RefMessageBodyService::GetAndCount(), the message is
|
|
|
|
* released.
|
|
|
|
*/
|
2020-01-24 10:58:34 +03:00
|
|
|
class RefMessageBody final {
|
2020-01-24 11:00:08 +03:00
|
|
|
friend class RefMessageBodyService;
|
|
|
|
|
2020-01-24 10:58:34 +03:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefMessageBody)
|
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
RefMessageBody(const nsID& aPortID,
|
2020-01-24 10:58:34 +03:00
|
|
|
UniquePtr<ipc::StructuredCloneData>&& aCloneData)
|
2020-01-24 11:00:08 +03:00
|
|
|
: mPortID(aPortID),
|
|
|
|
mCloneData(std::move(aCloneData)),
|
|
|
|
mMaxCount(Nothing()),
|
|
|
|
mCount(0) {}
|
2020-01-24 10:58:34 +03:00
|
|
|
|
|
|
|
const nsID& PortID() const { return mPortID; }
|
|
|
|
|
|
|
|
ipc::StructuredCloneData* CloneData() { return mCloneData.get(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~RefMessageBody() = default;
|
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
const nsID mPortID;
|
2020-01-24 10:58:34 +03:00
|
|
|
UniquePtr<ipc::StructuredCloneData> mCloneData;
|
2020-01-24 11:00:08 +03:00
|
|
|
|
|
|
|
// When mCount reaches mMaxCount, this object is released by the service.
|
|
|
|
Maybe<uint32_t> mMaxCount;
|
|
|
|
uint32_t mCount;
|
2020-01-24 10:58:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class RefMessageBodyService final {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefMessageBodyService)
|
|
|
|
|
|
|
|
static already_AddRefed<RefMessageBodyService> GetOrCreate();
|
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
void ForgetPort(const nsID& aPortID);
|
|
|
|
|
|
|
|
const nsID Register(already_AddRefed<RefMessageBody> aBody, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<RefMessageBody> Steal(const nsID& aID);
|
2020-01-24 10:58:34 +03:00
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
already_AddRefed<RefMessageBody> GetAndCount(const nsID& aID);
|
2020-01-24 10:58:34 +03:00
|
|
|
|
2020-01-24 11:00:08 +03:00
|
|
|
void SetMaxCount(const nsID& aID, uint32_t aMaxCount);
|
2020-01-24 10:58:34 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
RefMessageBodyService();
|
|
|
|
~RefMessageBodyService();
|
|
|
|
|
|
|
|
static RefMessageBodyService* GetOrCreateInternal(
|
|
|
|
const StaticMutexAutoLock& aProofOfLock);
|
|
|
|
|
|
|
|
nsRefPtrHashtable<nsIDHashKey, RefMessageBody> mMessages;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_RefMessageBodyService_h
|