nsInputStreamPump.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 nsInputStreamPump_h__
  6. #define nsInputStreamPump_h__
  7. #include "nsIInputStreamPump.h"
  8. #include "nsIAsyncInputStream.h"
  9. #include "nsIThreadRetargetableRequest.h"
  10. #include "nsCOMPtr.h"
  11. #include "mozilla/Attributes.h"
  12. #include "mozilla/ReentrantMonitor.h"
  13. class nsIInputStream;
  14. class nsILoadGroup;
  15. class nsIStreamListener;
  16. class nsInputStreamPump final : public nsIInputStreamPump
  17. , public nsIInputStreamCallback
  18. , public nsIThreadRetargetableRequest
  19. {
  20. ~nsInputStreamPump();
  21. public:
  22. typedef mozilla::ReentrantMonitorAutoEnter ReentrantMonitorAutoEnter;
  23. NS_DECL_THREADSAFE_ISUPPORTS
  24. NS_DECL_NSIREQUEST
  25. NS_DECL_NSIINPUTSTREAMPUMP
  26. NS_DECL_NSIINPUTSTREAMCALLBACK
  27. NS_DECL_NSITHREADRETARGETABLEREQUEST
  28. nsInputStreamPump();
  29. static nsresult
  30. Create(nsInputStreamPump **result,
  31. nsIInputStream *stream,
  32. int64_t streamPos = -1,
  33. int64_t streamLen = -1,
  34. uint32_t segsize = 0,
  35. uint32_t segcount = 0,
  36. bool closeWhenDone = false);
  37. typedef void (*PeekSegmentFun)(void *closure, const uint8_t *buf,
  38. uint32_t bufLen);
  39. /**
  40. * Peek into the first chunk of data that's in the stream. Note that this
  41. * method will not call the callback when there is no data in the stream.
  42. * The callback will be called at most once.
  43. *
  44. * The data from the stream will not be consumed, i.e. the pump's listener
  45. * can still read all the data.
  46. *
  47. * Do not call before asyncRead. Do not call after onStopRequest.
  48. */
  49. nsresult PeekStream(PeekSegmentFun callback, void *closure);
  50. /**
  51. * Dispatched (to the main thread) by OnStateStop if it's called off main
  52. * thread. Updates mState based on return value of OnStateStop.
  53. */
  54. nsresult CallOnStateStop();
  55. protected:
  56. enum {
  57. STATE_IDLE,
  58. STATE_START,
  59. STATE_TRANSFER,
  60. STATE_STOP
  61. };
  62. nsresult EnsureWaiting();
  63. uint32_t OnStateStart();
  64. uint32_t OnStateTransfer();
  65. uint32_t OnStateStop();
  66. uint32_t mState;
  67. nsCOMPtr<nsILoadGroup> mLoadGroup;
  68. nsCOMPtr<nsIStreamListener> mListener;
  69. nsCOMPtr<nsISupports> mListenerContext;
  70. nsCOMPtr<nsIEventTarget> mTargetThread;
  71. nsCOMPtr<nsIInputStream> mStream;
  72. nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
  73. uint64_t mStreamOffset;
  74. uint64_t mStreamLength;
  75. uint32_t mSegSize;
  76. uint32_t mSegCount;
  77. nsresult mStatus;
  78. uint32_t mSuspendCount;
  79. uint32_t mLoadFlags;
  80. bool mIsPending;
  81. // True while in OnInputStreamReady, calling OnStateStart, OnStateTransfer
  82. // and OnStateStop. Used to prevent calls to AsyncWait during callbacks.
  83. bool mProcessingCallbacks;
  84. // True if waiting on the "input stream ready" callback.
  85. bool mWaitingForInputStreamReady;
  86. bool mCloseWhenDone;
  87. bool mRetargeting;
  88. // Protects state/member var accesses across multiple threads.
  89. mozilla::ReentrantMonitor mMonitor;
  90. };
  91. #endif // !nsInputStreamChannel_h__