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 "mozilla/dom/cache/StreamControl.h"
9 namespace mozilla::dom::cache
{
11 void StreamControl::AddReadStream(
12 SafeRefPtr
<ReadStream::Controllable
> aReadStream
) {
14 MOZ_DIAGNOSTIC_ASSERT(aReadStream
);
15 MOZ_ASSERT(!mReadStreamList
.Contains(aReadStream
));
16 mReadStreamList
.AppendElement(std::move(aReadStream
));
19 void StreamControl::ForgetReadStream(
20 SafeRefPtr
<ReadStream::Controllable
> aReadStream
) {
22 MOZ_ALWAYS_TRUE(mReadStreamList
.RemoveElement(aReadStream
));
25 void StreamControl::NoteClosed(SafeRefPtr
<ReadStream::Controllable
> aReadStream
,
28 ForgetReadStream(std::move(aReadStream
));
29 NoteClosedAfterForget(aId
);
32 StreamControl::~StreamControl() {
33 // owning thread only, but can't call virtual AssertOwningThread in destructor
34 MOZ_DIAGNOSTIC_ASSERT(mReadStreamList
.IsEmpty());
37 void StreamControl::CloseAllReadStreams() {
40 // A copy of mReadStreamList is necessary here for two reasons:
41 // 1. mReadStreamList is modified in StreamControl::ForgetReadStream (called
43 // 2. the this pointer is deleted by CacheStreamControlParent::Shutdown
44 // (called transitively)
45 auto readStreamList
= mReadStreamList
.Clone();
46 for (const auto& stream
: readStreamList
.ForwardRange()) {
47 stream
->CloseStream();
51 void StreamControl::CloseAllReadStreamsWithoutReporting() {
54 for (const auto& stream
: mReadStreamList
.ForwardRange()) {
55 // Note, we cannot trigger IPC traffic here. So use
56 // CloseStreamWithoutReporting().
57 stream
->CloseStreamWithoutReporting();
61 bool StreamControl::HasEverBeenRead() const {
62 const auto range
= mReadStreamList
.NonObservingRange();
63 return std::any_of(range
.begin(), range
.end(), [](const auto& stream
) {
64 return stream
->HasEverBeenRead();
68 } // namespace mozilla::dom::cache