tor-browser

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

StreamControl.h (2545B)


      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 mozilla_dom_cache_StreamControl_h
      8 #define mozilla_dom_cache_StreamControl_h
      9 
     10 #include "mozilla/dom/cache/ReadStream.h"
     11 #include "mozilla/dom/cache/Types.h"
     12 #include "nsTObserverArray.h"
     13 
     14 struct nsID;
     15 
     16 namespace mozilla::dom::cache {
     17 
     18 class CacheReadStream;
     19 
     20 // Abstract class to help implement the stream control Child and Parent actors.
     21 // This provides an interface to partly help with serialization of IPC types,
     22 // but also an implementation for tracking ReadStream objects.
     23 class StreamControl {
     24 public:
     25  // abstract interface that must be implemented by child class
     26  virtual void SerializeControl(CacheReadStream* aReadStreamOut) = 0;
     27 
     28  virtual void SerializeStream(CacheReadStream* aReadStreamOut,
     29                               nsIInputStream* aStream) = 0;
     30 
     31  virtual void OpenStream(const nsID& aId, InputStreamResolver&& aResolver) = 0;
     32 
     33  // inherited implementation of the ReadStream::Controllable list
     34 
     35  // Begin controlling the given ReadStream.  This causes a strong ref to
     36  // be held by the control.  The ReadStream must call NoteClosed() or
     37  // ForgetReadStream() to release this ref.
     38  void AddReadStream(SafeRefPtr<ReadStream::Controllable> aReadStream);
     39 
     40  // Forget the ReadStream without notifying the actor.
     41  void ForgetReadStream(SafeRefPtr<ReadStream::Controllable> aReadStream);
     42 
     43  // Forget the ReadStream and then notify the actor the stream is closed.
     44  void NoteClosed(SafeRefPtr<ReadStream::Controllable> aReadStream,
     45                  const nsID& aId);
     46 
     47 protected:
     48  ~StreamControl();
     49 
     50  void CloseAllReadStreams();
     51 
     52  void CloseAllReadStreamsWithoutReporting();
     53 
     54  bool HasEverBeenRead() const;
     55 
     56  // protected parts of the abstract interface
     57  virtual void NoteClosedAfterForget(const nsID& aId) = 0;
     58 
     59 #ifdef DEBUG
     60  virtual void AssertOwningThread() = 0;
     61 #else
     62  void AssertOwningThread() {}
     63 #endif
     64 
     65 private:
     66  // Hold strong references to ReadStream object.  When the stream is closed
     67  // it should call NoteClosed() or ForgetReadStream() to release this ref.
     68  using ReadStreamList = nsTObserverArray<SafeRefPtr<ReadStream::Controllable>>;
     69  ReadStreamList mReadStreamList;
     70 };
     71 
     72 }  // namespace mozilla::dom::cache
     73 
     74 #endif  // mozilla_dom_cache_StreamControl_h