File: ReadStream.h

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (99 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_cache_ReadStream_h
#define mozilla_dom_cache_ReadStream_h

#include "mozilla/RefPtr.h"
#include "mozilla/dom/SafeRefPtr.h"
#include "mozilla/ipc/FileDescriptor.h"
#include "nsCOMPtr.h"
#include "nsID.h"
#include "nsIInputStream.h"
#include "nsISupportsImpl.h"
#include "nsTArrayForwardDeclare.h"

namespace mozilla {
class ErrorResult;

namespace dom::cache {

class CacheReadStream;
class PCacheStreamControlParent;

// IID for the dom::cache::ReadStream interface
#define NS_DOM_CACHE_READSTREAM_IID \
  {0x8e5da7c9, 0x0940, 0x4f1d, {0x97, 0x25, 0x5c, 0x59, 0x38, 0xdd, 0xb9, 0x9f}}

// Custom stream class for Request and Response bodies being read from
// a Cache.  The main purpose of this class is to report back to the
// Cache's Manager when the stream is closed.  This allows the Cache to
// accurately determine when the underlying body file can be deleted,
// etc.
//
// The ReadStream class also provides us with a convenient QI'able
// interface that we can use to pass additional meta-data with the
// stream channel.  For example, Cache.put() can detect that the content
// script is passing a Cache-originated-stream back into the Cache
// again.  This enables certain optimizations.
class ReadStream final : public nsIInputStream {
 public:
  // Interface that lets the StreamControl classes interact with
  // our private inner stream.
  class Controllable : public AtomicSafeRefCounted<Controllable> {
   public:
    virtual ~Controllable() = default;

    // Closes the stream, notifies the stream control, and then forgets
    // the stream control.
    virtual void CloseStream() = 0;

    // Closes the stream and then forgets the stream control.  Does not
    // notify.
    virtual void CloseStreamWithoutReporting() = 0;

    virtual bool HasEverBeenRead() const = 0;

    MOZ_DECLARE_REFCOUNTED_TYPENAME(ReadStream::Controllable);
  };

  static already_AddRefed<ReadStream> Create(
      const Maybe<CacheReadStream>& aMaybeReadStream);

  static already_AddRefed<ReadStream> Create(
      const CacheReadStream& aReadStream);

  static already_AddRefed<ReadStream> Create(
      PCacheStreamControlParent* aControl, const nsID& aId,
      nsIInputStream* aStream);

  void Serialize(Maybe<CacheReadStream>* aReadStreamOut, ErrorResult& aRv);
  void Serialize(CacheReadStream* aReadStreamOut, ErrorResult& aRv);

 private:
  class Inner;

  ~ReadStream();

  // Hold a strong ref to an inner class that actually implements the
  // majority of the stream logic.  Before releasing this ref the outer
  // ReadStream guarantees it will call Close() on the inner stream.
  // This is essential for the inner stream to avoid dealing with the
  // implicit close that can happen when a stream is destroyed.
  SafeRefPtr<ReadStream::Inner> mInner;

 public:
  explicit ReadStream(SafeRefPtr<ReadStream::Inner> aInner);

  NS_INLINE_DECL_STATIC_IID(NS_DOM_CACHE_READSTREAM_IID);
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIINPUTSTREAM
};

}  // namespace dom::cache
}  // namespace mozilla

#endif  // mozilla_dom_cache_ReadStream_h