1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_RefMessageBodyService_h
8 #define mozilla_dom_RefMessageBodyService_h
11 #include "js/TypeDecls.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Mutex.h"
14 #include "mozilla/StaticMutex.h"
15 #include "mozilla/UniquePtr.h"
16 #include "nsHashKeys.h"
18 #include "nsISupports.h"
19 #include "nsRefPtrHashtable.h"
22 class CloneDataPolicy
;
38 class StructuredCloneData
;
42 * At the time a BroadcastChannel or MessagePort sends messages, we don't know
43 * which process is going to receive it. Because of this, we need to check if
44 * the message is able to cross the process boundary.
45 * If the message contains objects such as SharedArrayBuffers, WASM modules or
46 * ImageBitmaps, it can be delivered on the current process only.
47 * Instead of sending the whole message via IPC, we send a unique ID, while the
48 * message is kept alive by RefMessageBodyService, on the current process using
49 * a ref-counted RefMessageBody object.
50 * When the receiver obtains the message ID, it checks if the local
51 * RefMessageBodyService knows that ID. If yes, the sender and the receiver are
52 * on the same process and the delivery can be completed; if not, a
53 * messageerror event has to be dispatched instead.
55 * For MessagePort communication is 1-to-1 and because of this, the
56 * receiver takes ownership of the message (RefMessageBodyService::Steal()).
57 * If the receiver port is on a different process, RefMessageBodyService will
58 * return a nullptr and a messageerror event will be dispatched.
60 * For BroadcastChannel, the life-time of a message is a bit different than for
61 * MessagePort. It has a 1-to-many communication and we could have multiple
62 * receivers. Each one needs to deliver the message without taking full
64 * In order to support this feature, BroadcastChannel needs to call
65 * RefMessageBodyService::SetMaxCount() to inform how many ports are allowed to
66 * retrieve the current message, on the current process. Receivers on other
67 * processes are not kept in consideration because they will not be able to
68 * retrieve the message from RefMessageBodyService. When the last allowed
69 * port has called RefMessageBodyService::GetAndCount(), the message is
72 class RefMessageBody final
{
73 friend class RefMessageBodyService
;
76 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefMessageBody
)
78 RefMessageBody(const nsID
& aPortID
,
79 UniquePtr
<ipc::StructuredCloneData
>&& aCloneData
);
81 const nsID
& PortID() const { return mPortID
; }
83 void Read(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aValue
,
84 const JS::CloneDataPolicy
& aCloneDataPolicy
, ErrorResult
& aRv
);
86 // This method can be called only if the RefMessageBody is not supposed to be
87 // ref-counted (see mMaxCount).
88 bool TakeTransferredPortsAsSequence(
89 Sequence
<OwningNonNull
<mozilla::dom::MessagePort
>>& aPorts
);
96 // In case the RefMessageBody is shared and refcounted (see mCount/mMaxCount),
97 // we must enforce that the reading does not happen simultaneously on
99 Mutex mMutex MOZ_UNANNOTATED
;
101 UniquePtr
<ipc::StructuredCloneData
> mCloneData
;
103 // When mCount reaches mMaxCount, this object is released by the service.
104 Maybe
<uint32_t> mMaxCount
;
108 class RefMessageBodyService final
{
110 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefMessageBodyService
)
112 static already_AddRefed
<RefMessageBodyService
> GetOrCreate();
114 void ForgetPort(const nsID
& aPortID
);
116 const nsID
Register(already_AddRefed
<RefMessageBody
> aBody
, ErrorResult
& aRv
);
118 already_AddRefed
<RefMessageBody
> Steal(const nsID
& aID
);
120 already_AddRefed
<RefMessageBody
> GetAndCount(const nsID
& aID
);
122 void SetMaxCount(const nsID
& aID
, uint32_t aMaxCount
);
125 explicit RefMessageBodyService(const StaticMutexAutoLock
& aProofOfLock
);
126 ~RefMessageBodyService();
128 static RefMessageBodyService
* GetOrCreateInternal(
129 const StaticMutexAutoLock
& aProofOfLock
);
131 nsRefPtrHashtable
<nsIDHashKey
, RefMessageBody
> mMessages
;
135 } // namespace mozilla
137 #endif // mozilla_dom_RefMessageBodyService_h