tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StreamControl.cpp (2296B)


      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 #include "mozilla/dom/cache/StreamControl.h"
      8 
      9 namespace mozilla::dom::cache {
     10 
     11 void StreamControl::AddReadStream(
     12    SafeRefPtr<ReadStream::Controllable> aReadStream) {
     13  AssertOwningThread();
     14  MOZ_DIAGNOSTIC_ASSERT(aReadStream);
     15  MOZ_ASSERT(!mReadStreamList.Contains(aReadStream));
     16  mReadStreamList.AppendElement(std::move(aReadStream));
     17 }
     18 
     19 void StreamControl::ForgetReadStream(
     20    SafeRefPtr<ReadStream::Controllable> aReadStream) {
     21  AssertOwningThread();
     22  MOZ_ALWAYS_TRUE(mReadStreamList.RemoveElement(aReadStream));
     23 }
     24 
     25 void StreamControl::NoteClosed(SafeRefPtr<ReadStream::Controllable> aReadStream,
     26                               const nsID& aId) {
     27  AssertOwningThread();
     28  ForgetReadStream(std::move(aReadStream));
     29  NoteClosedAfterForget(aId);
     30 }
     31 
     32 StreamControl::~StreamControl() {
     33  // owning thread only, but can't call virtual AssertOwningThread in destructor
     34  MOZ_DIAGNOSTIC_ASSERT(mReadStreamList.IsEmpty());
     35 }
     36 
     37 void StreamControl::CloseAllReadStreams() {
     38  AssertOwningThread();
     39 
     40  // A copy of mReadStreamList is necessary here for two reasons:
     41  // 1. mReadStreamList is modified in StreamControl::ForgetReadStream (called
     42  //    transitively)
     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();
     48  }
     49 }
     50 
     51 void StreamControl::CloseAllReadStreamsWithoutReporting() {
     52  AssertOwningThread();
     53 
     54  for (const auto& stream : mReadStreamList.ForwardRange()) {
     55    // Note, we cannot trigger IPC traffic here.  So use
     56    // CloseStreamWithoutReporting().
     57    stream->CloseStreamWithoutReporting();
     58  }
     59 }
     60 
     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();
     65  });
     66 }
     67 
     68 }  // namespace mozilla::dom::cache