Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / ipc / RefMessageBodyService.h
blob6cca2a836efa0f7173e162020ad1737ad1757978
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
10 #include <cstdint>
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"
17 #include "nsID.h"
18 #include "nsISupports.h"
19 #include "nsRefPtrHashtable.h"
21 namespace JS {
22 class CloneDataPolicy;
23 } // namespace JS
25 namespace mozilla {
27 class ErrorResult;
28 template <class T>
29 class OwningNonNull;
31 namespace dom {
33 class MessagePort;
34 template <typename T>
35 class Sequence;
37 namespace ipc {
38 class StructuredCloneData;
41 /**
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
63 * ownership of it.
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
70 * released.
72 class RefMessageBody final {
73 friend class RefMessageBodyService;
75 public:
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);
91 private:
92 ~RefMessageBody();
94 const nsID mPortID;
96 // In case the RefMessageBody is shared and refcounted (see mCount/mMaxCount),
97 // we must enforce that the reading does not happen simultaneously on
98 // different threads.
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;
105 uint32_t mCount;
108 class RefMessageBodyService final {
109 public:
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);
124 private:
125 explicit RefMessageBodyService(const StaticMutexAutoLock& aProofOfLock);
126 ~RefMessageBodyService();
128 static RefMessageBodyService* GetOrCreateInternal(
129 const StaticMutexAutoLock& aProofOfLock);
131 nsRefPtrHashtable<nsIDHashKey, RefMessageBody> mMessages;
134 } // namespace dom
135 } // namespace mozilla
137 #endif // mozilla_dom_RefMessageBodyService_h