Bug 1824243: Ensure WebTransport object remains alive while any dependent streams are alive r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D173526
This commit is contained in:
Randell Jesup 2023-03-24 22:19:08 +00:00
parent 23660aba47
commit ea365b49c1
4 changed files with 51 additions and 13 deletions

View File

@ -17,8 +17,20 @@ using namespace mozilla::ipc;
namespace mozilla::dom {
WebTransportReceiveStream::WebTransportReceiveStream(nsIGlobalObject* aGlobal)
: ReadableStream(aGlobal, HoldDropJSObjectsCaller::Implicit) {}
NS_IMPL_CYCLE_COLLECTION_INHERITED(WebTransportReceiveStream, ReadableStream,
mTransport)
NS_IMPL_ADDREF_INHERITED(WebTransportReceiveStream, ReadableStream)
NS_IMPL_RELEASE_INHERITED(WebTransportReceiveStream, ReadableStream)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebTransportReceiveStream)
NS_INTERFACE_MAP_END_INHERITING(ReadableStream)
WebTransportReceiveStream::WebTransportReceiveStream(nsIGlobalObject* aGlobal,
WebTransport* aTransport)
: ReadableStream(aGlobal,
ReadableStream::HoldDropJSObjectsCaller::Explicit),
mTransport(aTransport) {
mozilla::HoldJSObjects(this);
}
// WebIDL Boilerplate
@ -37,7 +49,7 @@ already_AddRefed<WebTransportReceiveStream> WebTransportReceiveStream::Create(
}
JSContext* cx = jsapi.cx();
auto stream = MakeRefPtr<WebTransportReceiveStream>(aGlobal);
auto stream = MakeRefPtr<WebTransportReceiveStream>(aGlobal, aWebTransport);
nsCOMPtr<nsIAsyncInputStream> inputStream = receiver;
auto algorithms = MakeRefPtr<InputToReadableStreamAlgorithms>(

View File

@ -19,10 +19,11 @@ class WebTransport;
class WebTransportReceiveStream final : public ReadableStream {
public:
NS_INLINE_DECL_REFCOUNTING_INHERITED(WebTransportReceiveStream,
ReadableStream)
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WebTransportReceiveStream,
ReadableStream)
explicit WebTransportReceiveStream(nsIGlobalObject* aGlobal);
WebTransportReceiveStream(nsIGlobalObject* aGlobal, WebTransport* aTransport);
MOZ_CAN_RUN_SCRIPT_BOUNDARY static already_AddRefed<WebTransportReceiveStream>
Create(WebTransport* aWebTransport, nsIGlobalObject* aGlobal,
@ -36,7 +37,13 @@ class WebTransportReceiveStream final : public ReadableStream {
already_AddRefed<Promise> GetStats();
private:
~WebTransportReceiveStream() override = default;
~WebTransportReceiveStream() override { mozilla::DropJSObjects(this); }
// We must hold a reference to the WebTransport so it can't go away on
// us. This forms a cycle with WebTransport that will be broken when the
// CC runs. WebTransport::CleanUp() will destroy all the send and receive
// streams, breaking the cycle.
RefPtr<WebTransport> mTransport;
};
} // namespace mozilla::dom

View File

@ -16,9 +16,20 @@ using namespace mozilla::ipc;
namespace mozilla::dom {
WebTransportSendStream::WebTransportSendStream(nsIGlobalObject* aGlobal)
NS_IMPL_CYCLE_COLLECTION_INHERITED(WebTransportSendStream, WritableStream,
mTransport)
NS_IMPL_ADDREF_INHERITED(WebTransportSendStream, WritableStream)
NS_IMPL_RELEASE_INHERITED(WebTransportSendStream, WritableStream)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebTransportSendStream)
NS_INTERFACE_MAP_END_INHERITING(WritableStream)
WebTransportSendStream::WebTransportSendStream(nsIGlobalObject* aGlobal,
WebTransport* aTransport)
: WritableStream(aGlobal,
WritableStream::HoldDropJSObjectsCaller::Implicit) {}
WritableStream::HoldDropJSObjectsCaller::Explicit),
mTransport(aTransport) {
mozilla::HoldJSObjects(this);
}
JSObject* WebTransportSendStream::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
@ -37,7 +48,7 @@ already_AddRefed<WebTransportSendStream> WebTransportSendStream::Create(
}
JSContext* cx = jsapi.cx();
auto stream = MakeRefPtr<WebTransportSendStream>(aGlobal);
auto stream = MakeRefPtr<WebTransportSendStream>(aGlobal, aWebTransport);
nsCOMPtr<nsIAsyncOutputStream> outputStream = sender;
auto algorithms = MakeRefPtr<WritableStreamToOutput>(

View File

@ -19,9 +19,11 @@ class WebTransport;
class WebTransportSendStream final : public WritableStream {
public:
NS_INLINE_DECL_REFCOUNTING_INHERITED(WebTransportSendStream, WritableStream)
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WebTransportSendStream,
WritableStream)
explicit WebTransportSendStream(nsIGlobalObject* aGlobal);
WebTransportSendStream(nsIGlobalObject* aGlobal, WebTransport* aTransport);
MOZ_CAN_RUN_SCRIPT_BOUNDARY static already_AddRefed<WebTransportSendStream>
Create(WebTransport* aWebTransport, nsIGlobalObject* aGlobal,
@ -35,7 +37,13 @@ class WebTransportSendStream final : public WritableStream {
already_AddRefed<Promise> GetStats();
private:
~WebTransportSendStream() override = default;
~WebTransportSendStream() override { mozilla::DropJSObjects(this); };
// We must hold a reference to the WebTransport so it can't go away on
// us. This forms a cycle with WebTransport that will be broken when the
// CC runs. WebTransport::CleanUp() will destroy all the send and receive
// streams, breaking the cycle.
RefPtr<WebTransport> mTransport;
};
} // namespace mozilla::dom