Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / cache / CacheStreamControlChild.cpp
blob26b11e7f46024fc32579488819a3e5e7075eb53e
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 #include "CacheStreamControlChild.h"
9 #include "mozilla/Unused.h"
10 #include "mozilla/dom/cache/ActorUtils.h"
11 #include "mozilla/dom/cache/CacheTypes.h"
12 #include "mozilla/dom/cache/CacheWorkerRef.h"
13 #include "mozilla/ipc/IPCStreamUtils.h"
14 #include "mozilla/ipc/PBackgroundChild.h"
15 #include "nsISupportsImpl.h"
17 namespace mozilla::dom::cache {
19 using mozilla::ipc::FileDescriptor;
21 // declared in ActorUtils.h
22 already_AddRefed<PCacheStreamControlChild> AllocPCacheStreamControlChild() {
23 return MakeAndAddRef<CacheStreamControlChild>();
26 CacheStreamControlChild::CacheStreamControlChild()
27 : mDestroyStarted(false), mDestroyDelayed(false) {
28 MOZ_COUNT_CTOR(cache::CacheStreamControlChild);
31 CacheStreamControlChild::~CacheStreamControlChild() {
32 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
33 MOZ_COUNT_DTOR(cache::CacheStreamControlChild);
36 void CacheStreamControlChild::StartDestroy() {
37 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
38 // This can get called twice under some circumstances. For example, if the
39 // actor is added to a CacheWorkerRef that has already been notified and
40 // the Cache actor has no mListener.
41 if (mDestroyStarted) {
42 return;
44 mDestroyStarted = true;
46 // If any of the streams have started to be read, then wait for them to close
47 // naturally.
48 if (HasEverBeenRead()) {
49 // Note that we are delaying so that we can re-check for active streams
50 // in NoteClosedAfterForget().
51 mDestroyDelayed = true;
52 return;
55 // Otherwise, if the streams have not been touched then just pre-emptively
56 // close them now. This handles the case where someone retrieves a Response
57 // from the Cache, but never accesses the body. We should not keep the
58 // Worker alive until that Response is GC'd just because of its ignored
59 // body stream.
61 // Begin shutting down all streams. This is the same as if the parent had
62 // asked us to shutdown. So simulate the CloseAll IPC message.
63 RecvCloseAll();
66 void CacheStreamControlChild::SerializeControl(
67 CacheReadStream* aReadStreamOut) {
68 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
69 MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
70 aReadStreamOut->control() = this;
73 void CacheStreamControlChild::SerializeStream(CacheReadStream* aReadStreamOut,
74 nsIInputStream* aStream) {
75 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
76 MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
77 MOZ_ALWAYS_TRUE(mozilla::ipc::SerializeIPCStream(
78 do_AddRef(aStream), aReadStreamOut->stream(), /* aAllowLazy */ false));
81 void CacheStreamControlChild::OpenStream(const nsID& aId,
82 InputStreamResolver&& aResolver) {
83 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
85 if (mDestroyStarted) {
86 aResolver(nullptr);
87 return;
90 // If we are on a worker, then we need to hold it alive until the async
91 // IPC operation below completes. While the IPC layer will trigger a
92 // rejection here in many cases, we must handle the case where the
93 // MozPromise resolve runnable is already in the event queue when the
94 // worker wants to shut down.
95 const SafeRefPtr<CacheWorkerRef> holder = GetWorkerRefPtr().clonePtr();
97 SendOpenStream(aId)->Then(
98 GetCurrentSerialEventTarget(), __func__,
99 [aResolver,
100 holder = holder.clonePtr()](const Maybe<IPCStream>& aOptionalStream) {
101 nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aOptionalStream);
102 aResolver(std::move(stream));
104 [aResolver, holder = holder.clonePtr()](ResponseRejectReason&& aReason) {
105 aResolver(nullptr);
109 void CacheStreamControlChild::NoteClosedAfterForget(const nsID& aId) {
110 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
112 QM_WARNONLY_TRY(OkIf(SendNoteClosed(aId)));
114 // A stream has closed. If we delayed StartDestry() due to this stream
115 // being read, then we should check to see if any of the remaining streams
116 // are active. If none of our other streams have been read, then we can
117 // proceed with the shutdown now.
118 if (mDestroyDelayed && !HasEverBeenRead()) {
119 mDestroyDelayed = false;
120 RecvCloseAll();
124 #ifdef DEBUG
125 void CacheStreamControlChild::AssertOwningThread() {
126 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
128 #endif
130 void CacheStreamControlChild::ActorDestroy(ActorDestroyReason aReason) {
131 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
132 CloseAllReadStreamsWithoutReporting();
133 RemoveWorkerRef();
136 mozilla::ipc::IPCResult CacheStreamControlChild::RecvCloseAll() {
137 NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
138 CloseAllReadStreams();
139 return IPC_OK();
142 } // namespace mozilla::dom::cache