FetchConsumer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_FetchConsumer_h
  6. #define mozilla_dom_FetchConsumer_h
  7. #include "Fetch.h"
  8. #include "nsIInputStream.h"
  9. #include "nsIObserver.h"
  10. #include "nsWeakReference.h"
  11. #include "mozilla/dom/AbortSignal.h"
  12. #include "mozilla/dom/MutableBlobStorage.h"
  13. class nsIThread;
  14. class nsIInputStreamPump;
  15. namespace mozilla {
  16. namespace dom {
  17. class Promise;
  18. namespace workers {
  19. class WorkerPrivate;
  20. class WorkerHolder;
  21. }
  22. template <class Derived> class FetchBody;
  23. // FetchBody is not thread-safe but we need to move it around threads.
  24. // In order to keep it alive all the time, we use a WorkerHolder, if created on
  25. // workers, plus a this consumer.
  26. template <class Derived>
  27. class FetchBodyConsumer final : public nsIObserver
  28. , public nsSupportsWeakReference
  29. , public AbortSignal::Follower
  30. {
  31. public:
  32. NS_DECL_THREADSAFE_ISUPPORTS
  33. NS_DECL_NSIOBSERVER
  34. static already_AddRefed<Promise>
  35. Create(nsIGlobalObject* aGlobal,
  36. FetchBody<Derived>* aBody,
  37. AbortSignal* aSignal,
  38. FetchConsumeType aType,
  39. ErrorResult& aRv);
  40. void
  41. ReleaseObject();
  42. void
  43. BeginConsumeBodyMainThread();
  44. void
  45. ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult);
  46. void
  47. ContinueConsumeBlobBody(BlobImpl* aBlobImpl);
  48. void
  49. ShutDownMainThreadConsuming();
  50. workers::WorkerPrivate*
  51. GetWorkerPrivate() const
  52. {
  53. return mWorkerPrivate;
  54. }
  55. void
  56. NullifyConsumeBodyPump()
  57. {
  58. mShuttingDown = true;
  59. mConsumeBodyPump = nullptr;
  60. }
  61. // Override AbortSignal::Follower::Aborted
  62. void Aborted() override;
  63. private:
  64. FetchBodyConsumer(nsIGlobalObject* aGlobalObject,
  65. workers::WorkerPrivate* aWorkerPrivate,
  66. FetchBody<Derived>* aBody,
  67. nsIInputStream* aBodyStream,
  68. Promise* aPromise,
  69. FetchConsumeType aType);
  70. ~FetchBodyConsumer();
  71. void
  72. AssertIsOnTargetThread() const;
  73. bool
  74. RegisterWorkerHolder();
  75. nsCOMPtr<nsIThread> mTargetThread;
  76. #ifdef DEBUG
  77. // This is used only to check if the body has been correctly consumed.
  78. RefPtr<FetchBody<Derived>> mBody;
  79. #endif
  80. nsCOMPtr<nsIInputStream> mBodyStream;
  81. MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
  82. nsCString mBodyMimeType;
  83. // Set when consuming the body is attempted on a worker.
  84. // Unset when consumption is done/aborted.
  85. // This WorkerHolder keeps alive the consumer via a cycle.
  86. UniquePtr<workers::WorkerHolder> mWorkerHolder;
  87. nsCOMPtr<nsIGlobalObject> mGlobal;
  88. // Always set whenever the FetchBodyConsumer is created on the worker thread.
  89. workers::WorkerPrivate* mWorkerPrivate;
  90. // Touched on the main-thread only.
  91. nsCOMPtr<nsIInputStreamPump> mConsumeBodyPump;
  92. // Only ever set once, always on target thread.
  93. FetchConsumeType mConsumeType;
  94. RefPtr<Promise> mConsumePromise;
  95. // touched only on the target thread.
  96. bool mBodyConsumed;
  97. // touched only on the main-thread.
  98. bool mShuttingDown;
  99. };
  100. } // namespace dom
  101. } // namespace mozilla
  102. #endif // mozilla_dom_FetchConsumer_h