ReadStream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_cache_ReadStream_h
  6. #define mozilla_dom_cache_ReadStream_h
  7. #include "mozilla/ipc/FileDescriptor.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsID.h"
  10. #include "nsIInputStream.h"
  11. #include "nsISupportsImpl.h"
  12. #include "mozilla/ErrorResult.h"
  13. #include "mozilla/RefPtr.h"
  14. #include "nsTArrayForwardDeclare.h"
  15. using namespace mozilla;
  16. namespace mozilla {
  17. namespace ipc {
  18. class AutoIPCStream;
  19. } // namespace ipc
  20. namespace dom {
  21. namespace cache {
  22. class CacheReadStream;
  23. class CacheReadStreamOrVoid;
  24. class PCacheStreamControlParent;
  25. // IID for the dom::cache::ReadStream interface
  26. #define NS_DOM_CACHE_READSTREAM_IID \
  27. {0x8e5da7c9, 0x0940, 0x4f1d, \
  28. {0x97, 0x25, 0x5c, 0x59, 0x38, 0xdd, 0xb9, 0x9f}}
  29. // Custom stream class for Request and Response bodies being read from
  30. // a Cache. The main purpose of this class is to report back to the
  31. // Cache's Manager when the stream is closed. This allows the Cache to
  32. // accurately determine when the underlying body file can be deleted,
  33. // etc.
  34. //
  35. // The ReadStream class also provides us with a convenient QI'able
  36. // interface that we can use to pass additional meta-data with the
  37. // stream channel. For example, Cache.put() can detect that the content
  38. // script is passing a Cache-originated-stream back into the Cache
  39. // again. This enables certain optimizations.
  40. class ReadStream final : public nsIInputStream
  41. {
  42. public:
  43. // Interface that lets the StreamControl classes interact with
  44. // our private inner stream.
  45. class Controllable
  46. {
  47. public:
  48. // Closes the stream, notifies the stream control, and then forgets
  49. // the stream control.
  50. virtual void
  51. CloseStream() = 0;
  52. // Closes the stream and then forgets the stream control. Does not
  53. // notify.
  54. virtual void
  55. CloseStreamWithoutReporting() = 0;
  56. virtual bool
  57. MatchId(const nsID& aId) const = 0;
  58. virtual bool
  59. HasEverBeenRead() const = 0;
  60. NS_IMETHOD_(MozExternalRefCountType)
  61. AddRef(void) = 0;
  62. NS_IMETHOD_(MozExternalRefCountType)
  63. Release(void) = 0;
  64. };
  65. static already_AddRefed<ReadStream>
  66. Create(const CacheReadStreamOrVoid& aReadStreamOrVoid);
  67. static already_AddRefed<ReadStream>
  68. Create(const CacheReadStream& aReadStream);
  69. static already_AddRefed<ReadStream>
  70. Create(PCacheStreamControlParent* aControl, const nsID& aId,
  71. nsIInputStream* aStream);
  72. void Serialize(CacheReadStreamOrVoid* aReadStreamOut,
  73. nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
  74. ErrorResult& aRv);
  75. void Serialize(CacheReadStream* aReadStreamOut,
  76. nsTArray<UniquePtr<mozilla::ipc::AutoIPCStream>>& aStreamCleanupList,
  77. ErrorResult& aRv);
  78. private:
  79. class Inner;
  80. explicit ReadStream(Inner* aInner);
  81. ~ReadStream();
  82. // Hold a strong ref to an inner class that actually implements the
  83. // majority of the stream logic. Before releasing this ref the outer
  84. // ReadStream guarantees it will call Close() on the inner stream.
  85. // This is essential for the inner stream to avoid dealing with the
  86. // implicit close that can happen when a stream is destroyed.
  87. RefPtr<Inner> mInner;
  88. public:
  89. NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_CACHE_READSTREAM_IID);
  90. NS_DECL_THREADSAFE_ISUPPORTS
  91. NS_DECL_NSIINPUTSTREAM
  92. };
  93. NS_DEFINE_STATIC_IID_ACCESSOR(ReadStream, NS_DOM_CACHE_READSTREAM_IID);
  94. } // namespace cache
  95. } // namespace dom
  96. } // namespace mozilla
  97. #endif // mozilla_dom_cache_ReadStream_h