/src/mozilla-central/dom/fetch/FetchConsumer.h
Line | Count | Source (jump to first uncovered line) |
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_FetchConsumer_h |
8 | | #define mozilla_dom_FetchConsumer_h |
9 | | |
10 | | #include "Fetch.h" |
11 | | #include "mozilla/dom/AbortSignal.h" |
12 | | #include "mozilla/dom/MutableBlobStorage.h" |
13 | | #include "nsIObserver.h" |
14 | | #include "nsWeakReference.h" |
15 | | |
16 | | class nsIThread; |
17 | | |
18 | | namespace mozilla { |
19 | | namespace dom { |
20 | | |
21 | | class Promise; |
22 | | class ThreadSafeWorkerRef; |
23 | | |
24 | | template <class Derived> class FetchBody; |
25 | | |
26 | | // FetchBody is not thread-safe but we need to move it around threads. In order |
27 | | // to keep it alive all the time, we use a ThreadSafeWorkerRef, if created on |
28 | | // workers. |
29 | | template <class Derived> |
30 | | class FetchBodyConsumer final : public nsIObserver |
31 | | , public nsSupportsWeakReference |
32 | | , public AbortFollower |
33 | | { |
34 | | public: |
35 | | NS_DECL_THREADSAFE_ISUPPORTS |
36 | | NS_DECL_NSIOBSERVER |
37 | | |
38 | | static already_AddRefed<Promise> |
39 | | Create(nsIGlobalObject* aGlobal, |
40 | | nsIEventTarget* aMainThreadEventTarget, |
41 | | FetchBody<Derived>* aBody, |
42 | | AbortSignalImpl* aSignalImpl, |
43 | | FetchConsumeType aType, |
44 | | ErrorResult& aRv); |
45 | | |
46 | | void |
47 | | ReleaseObject(); |
48 | | |
49 | | void |
50 | | BeginConsumeBodyMainThread(ThreadSafeWorkerRef* aWorkerRef); |
51 | | |
52 | | void |
53 | | OnBlobResult(Blob* aBlob, ThreadSafeWorkerRef* aWorkerRef = nullptr); |
54 | | |
55 | | void |
56 | | ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult, |
57 | | bool aShuttingDown = false); |
58 | | |
59 | | void |
60 | | ContinueConsumeBlobBody(BlobImpl* aBlobImpl, bool aShuttingDown = false); |
61 | | |
62 | | void |
63 | | ShutDownMainThreadConsuming(); |
64 | | |
65 | | void |
66 | | NullifyConsumeBodyPump() |
67 | 0 | { |
68 | 0 | mShuttingDown = true; |
69 | 0 | mConsumeBodyPump = nullptr; |
70 | 0 | } Unexecuted instantiation: mozilla::dom::FetchBodyConsumer<mozilla::dom::Request>::NullifyConsumeBodyPump() Unexecuted instantiation: mozilla::dom::FetchBodyConsumer<mozilla::dom::Response>::NullifyConsumeBodyPump() |
71 | | |
72 | | // AbortFollower |
73 | | void Abort() override; |
74 | | |
75 | | private: |
76 | | FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget, |
77 | | nsIGlobalObject* aGlobalObject, |
78 | | FetchBody<Derived>* aBody, |
79 | | nsIInputStream* aBodyStream, |
80 | | Promise* aPromise, |
81 | | FetchConsumeType aType); |
82 | | |
83 | | ~FetchBodyConsumer(); |
84 | | |
85 | | nsresult |
86 | | GetBodyLocalFile(nsIFile** aFile) const; |
87 | | |
88 | | void |
89 | | AssertIsOnTargetThread() const; |
90 | | |
91 | | nsCOMPtr<nsIThread> mTargetThread; |
92 | | nsCOMPtr<nsIEventTarget> mMainThreadEventTarget; |
93 | | |
94 | | #ifdef DEBUG |
95 | | // This is used only to check if the body has been correctly consumed. |
96 | | RefPtr<FetchBody<Derived>> mBody; |
97 | | #endif |
98 | | |
99 | | // This is nullified when the consuming of the body starts. |
100 | | nsCOMPtr<nsIInputStream> mBodyStream; |
101 | | |
102 | | MutableBlobStorage::MutableBlobStorageType mBlobStorageType; |
103 | | nsCString mBodyMimeType; |
104 | | |
105 | | nsString mBodyLocalPath; |
106 | | |
107 | | nsCOMPtr<nsIGlobalObject> mGlobal; |
108 | | |
109 | | // Touched on the main-thread only. |
110 | | nsCOMPtr<nsIInputStreamPump> mConsumeBodyPump; |
111 | | |
112 | | // Only ever set once, always on target thread. |
113 | | FetchConsumeType mConsumeType; |
114 | | RefPtr<Promise> mConsumePromise; |
115 | | |
116 | | // touched only on the target thread. |
117 | | bool mBodyConsumed; |
118 | | |
119 | | // touched only on the main-thread. |
120 | | bool mShuttingDown; |
121 | | }; |
122 | | |
123 | | } // namespace dom |
124 | | } // namespace mozilla |
125 | | |
126 | | #endif // mozilla_dom_FetchConsumer_h |