SharedMessageBody.h (4140B)
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/. */ 6 7 #ifndef mozilla_dom_SharedMessageBody_h 8 #define mozilla_dom_SharedMessageBody_h 9 10 #include "mozilla/Maybe.h" 11 #include "mozilla/dom/ipc/StructuredCloneData.h" 12 13 namespace mozilla { 14 15 namespace ipc { 16 class PBackgroundChild; 17 } 18 19 namespace dom { 20 21 class MessagePort; 22 class RefMessageBody; 23 class RefMessageBodyService; 24 25 class SharedMessageBody final { 26 public: 27 NS_INLINE_DECL_REFCOUNTING(SharedMessageBody) 28 29 SharedMessageBody( 30 StructuredCloneHolder::TransferringSupport aSupportsTransferring, 31 const Maybe<nsID>& aAgentClusterId); 32 33 // Note that the populated MessageData borrows the underlying 34 // JSStructuredCloneData from the SharedMessageBody, so the caller is 35 // required to ensure that the MessageData instances are destroyed prior to 36 // the SharedMessageBody instances. 37 static void FromSharedToMessageChild( 38 mozilla::ipc::PBackgroundChild* aBackgroundManager, 39 SharedMessageBody* aData, MessageData& aMessage); 40 static void FromSharedToMessagesChild( 41 mozilla::ipc::PBackgroundChild* aBackgroundManager, 42 const nsTArray<RefPtr<SharedMessageBody>>& aData, 43 nsTArray<MessageData>& aArray); 44 45 // Const MessageData. 46 static already_AddRefed<SharedMessageBody> FromMessageToSharedChild( 47 MessageData& aMessage, 48 StructuredCloneHolder::TransferringSupport aSupportsTransferring = 49 StructuredCloneHolder::TransferringSupported); 50 // Non-const MessageData. 51 static already_AddRefed<SharedMessageBody> FromMessageToSharedChild( 52 const MessageData& aMessage, 53 StructuredCloneHolder::TransferringSupport aSupportsTransferring = 54 StructuredCloneHolder::TransferringSupported); 55 // Array of MessageData objects 56 static bool FromMessagesToSharedChild( 57 nsTArray<MessageData>& aArray, 58 FallibleTArray<RefPtr<SharedMessageBody>>& aData, 59 StructuredCloneHolder::TransferringSupport aSupportsTransferring = 60 StructuredCloneHolder::TransferringSupported); 61 62 // Note that the populated MessageData borrows the underlying 63 // JSStructuredCloneData from the SharedMessageBody, so the caller is 64 // required to ensure that the MessageData instances are destroyed prior to 65 // the SharedMessageBody instances. 66 static bool FromSharedToMessagesParent( 67 mozilla::ipc::PBackgroundParent* aManager, 68 const nsTArray<RefPtr<SharedMessageBody>>& aData, 69 nsTArray<MessageData>& aArray); 70 71 static already_AddRefed<SharedMessageBody> FromMessageToSharedParent( 72 MessageData& aMessage, 73 StructuredCloneHolder::TransferringSupport aSupportsTransferring = 74 StructuredCloneHolder::TransferringSupported); 75 static bool FromMessagesToSharedParent( 76 nsTArray<MessageData>& aArray, 77 FallibleTArray<RefPtr<SharedMessageBody>>& aData, 78 StructuredCloneHolder::TransferringSupport aSupportsTransferring = 79 StructuredCloneHolder::TransferringSupported); 80 81 enum ReadMethod { 82 StealRefMessageBody, 83 KeepRefMessageBody, 84 }; 85 86 void Read(JSContext* aCx, JS::MutableHandle<JS::Value> aValue, 87 RefMessageBodyService* aRefMessageBodyService, 88 ReadMethod aReadMethod, ErrorResult& aRv); 89 90 void Write(JSContext* aCx, JS::Handle<JS::Value> aValue, 91 JS::Handle<JS::Value> aTransfers, nsID& aPortID, 92 RefMessageBodyService* aRefMessageBodyService, ErrorResult& aRv); 93 94 bool TakeTransferredPortsAsSequence( 95 Sequence<OwningNonNull<mozilla::dom::MessagePort>>& aPorts); 96 97 private: 98 ~SharedMessageBody() = default; 99 100 UniquePtr<ipc::StructuredCloneData> mCloneData; 101 102 RefPtr<RefMessageBody> mRefData; 103 Maybe<nsID> mRefDataId; 104 105 const StructuredCloneHolder::TransferringSupport mSupportsTransferring; 106 const Maybe<nsID> mAgentClusterId; 107 }; 108 109 } // namespace dom 110 } // namespace mozilla 111 112 #endif // mozilla_dom_SharedMessageBody_h