WebTransportParent.h (4340B)
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 DOM_WEBTRANSPORT_PARENT_WEBTRANSPORTPARENT_H_ 8 #define DOM_WEBTRANSPORT_PARENT_WEBTRANSPORTPARENT_H_ 9 10 #include "ErrorList.h" 11 #include "mozilla/dom/ClientIPCTypes.h" 12 #include "mozilla/dom/FlippedOnce.h" 13 #include "mozilla/dom/PWebTransportParent.h" 14 #include "mozilla/ipc/Endpoint.h" 15 #include "mozilla/ipc/PBackgroundSharedTypes.h" 16 #include "nsIPrincipal.h" 17 #include "nsISupports.h" 18 #include "nsIWebTransport.h" 19 #include "nsIWebTransportStream.h" 20 #include "nsTHashMap.h" 21 22 namespace mozilla::dom { 23 24 enum class WebTransportReliabilityMode : uint8_t; 25 26 class WebTransportParent : public PWebTransportParent, 27 public WebTransportSessionEventListener { 28 using IPCResult = mozilla::ipc::IPCResult; 29 30 public: 31 WebTransportParent() = default; 32 33 NS_DECL_THREADSAFE_ISUPPORTS 34 NS_DECL_WEBTRANSPORTSESSIONEVENTLISTENER 35 36 void Create(const nsAString& aURL, nsIPrincipal* aPrincipal, 37 const uint64_t& aBrowsingContextID, 38 const mozilla::Maybe<IPCClientInfo>& aClientInfo, 39 const bool& aDedicated, const bool& aRequireUnreliable, 40 const uint32_t& aCongestionControl, 41 nsTArray<WebTransportHash>&& aServerCertHashes, 42 Endpoint<PWebTransportParent>&& aParentEndpoint, 43 std::function<void(std::tuple<const nsresult&, const uint8_t&>)>&& 44 aResolver); 45 46 IPCResult RecvClose(const uint32_t& aCode, const nsACString& aReason); 47 48 IPCResult RecvSetSendOrder(uint64_t aStreamId, Maybe<int64_t> aSendOrder); 49 50 IPCResult RecvCreateUnidirectionalStream( 51 Maybe<int64_t> aSendOrder, 52 CreateUnidirectionalStreamResolver&& aResolver); 53 IPCResult RecvCreateBidirectionalStream( 54 Maybe<int64_t> aSendOrder, CreateBidirectionalStreamResolver&& aResolver); 55 56 ::mozilla::ipc::IPCResult RecvOutgoingDatagram( 57 nsTArray<uint8_t>&& aData, const TimeStamp& aExpirationTime, 58 OutgoingDatagramResolver&& aResolver); 59 60 ::mozilla::ipc::IPCResult RecvGetMaxDatagramSize( 61 GetMaxDatagramSizeResolver&& aResolver); 62 63 ::mozilla::ipc::IPCResult RecvGetHttpChannelID( 64 GetHttpChannelIDResolver&& aResolver); 65 66 void ActorDestroy(ActorDestroyReason aWhy) override; 67 68 class OnResetOrStopSendingCallback final { 69 public: 70 explicit OnResetOrStopSendingCallback( 71 std::function<void(nsresult)>&& aCallback) 72 : mCallback(std::move(aCallback)) {} 73 ~OnResetOrStopSendingCallback() = default; 74 75 void OnResetOrStopSending(nsresult aError) { mCallback(aError); } 76 77 private: 78 std::function<void(nsresult)> mCallback; 79 }; 80 81 protected: 82 virtual ~WebTransportParent(); 83 84 private: 85 void NotifyRemoteClosed(bool aCleanly, uint32_t aErrorCode, 86 const nsACString& aReason); 87 88 using ResolveType = std::tuple<const nsresult&, const uint8_t&>; 89 nsCOMPtr<nsISerialEventTarget> mSocketThread; 90 Atomic<bool> mSessionReady{false}; 91 92 mozilla::Mutex mMutex{"WebTransportParent::mMutex"}; 93 std::function<void(ResolveType)> mResolver MOZ_GUARDED_BY(mMutex); 94 // This is needed because mResolver is resolved on the background thread and 95 // OnSessionClosed is called on the socket thread. 96 std::function<void()> mExecuteAfterResolverCallback MOZ_GUARDED_BY(mMutex); 97 OutgoingDatagramResolver mOutgoingDatagramResolver; 98 GetMaxDatagramSizeResolver mMaxDatagramSizeResolver; 99 FlippedOnce<false> mClosed MOZ_GUARDED_BY(mMutex); 100 101 nsCOMPtr<nsIWebTransport> mWebTransport; 102 nsCOMPtr<nsIEventTarget> mOwningEventTarget; 103 104 // What we need to be able to lookup by streamId 105 template <typename T> 106 struct StreamHash { 107 OnResetOrStopSendingCallback mCallback; 108 nsCOMPtr<T> mStream; 109 }; 110 nsTHashMap<NoMemMoveKey<nsUint64HashKey>, 111 StreamHash<nsIWebTransportBidirectionalStream>> 112 mBidiStreamCallbackMap; 113 nsTHashMap<NoMemMoveKey<nsUint64HashKey>, 114 StreamHash<nsIWebTransportSendStream>> 115 mUniStreamCallbackMap; 116 }; 117 118 } // namespace mozilla::dom 119 120 #endif // DOM_WEBTRANSPORT_PARENT_WEBTRANSPORTPARENT_H_