Line data Source code
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 "nsIObserver.h"
12 : #include "nsWeakReference.h"
13 :
14 : class nsIThread;
15 :
16 : namespace mozilla {
17 : namespace dom {
18 :
19 : class Promise;
20 :
21 : namespace workers {
22 : class WorkerPrivate;
23 : class WorkerHolder;
24 : }
25 :
26 : template <class Derived> class FetchBody;
27 :
28 : // FetchBody is not thread-safe but we need to move it around threads.
29 : // In order to keep it alive all the time, we use a WorkerHolder, if created on
30 : // workers, plus a this consumer.
31 : template <class Derived>
32 : class FetchBodyConsumer final : public nsIObserver
33 : , public nsSupportsWeakReference
34 : {
35 : public:
36 : NS_DECL_THREADSAFE_ISUPPORTS
37 : NS_DECL_NSIOBSERVER
38 :
39 : static already_AddRefed<Promise>
40 : Create(nsIGlobalObject* aGlobal,
41 : nsIEventTarget* aMainThreadEventTarget,
42 : FetchBody<Derived>* aBody,
43 : FetchConsumeType aType,
44 : ErrorResult& aRv);
45 :
46 : void
47 : ReleaseObject();
48 :
49 : FetchBody<Derived>*
50 : Body() const
51 : {
52 : return mBody;
53 : }
54 :
55 : void
56 : BeginConsumeBodyMainThread();
57 :
58 : void
59 : ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult);
60 :
61 : void
62 : ContinueConsumeBlobBody(BlobImpl* aBlobImpl);
63 :
64 : void
65 : ShutDownMainThreadConsuming();
66 :
67 : workers::WorkerPrivate*
68 1 : GetWorkerPrivate() const
69 : {
70 1 : return mWorkerPrivate;
71 : }
72 :
73 : void
74 1 : NullifyConsumeBodyPump()
75 : {
76 1 : mConsumeBodyPump = nullptr;
77 1 : }
78 :
79 : private:
80 : FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
81 : nsIGlobalObject* aGlobalObject,
82 : workers::WorkerPrivate* aWorkerPrivate,
83 : FetchBody<Derived>* aBody,
84 : Promise* aPromise,
85 : FetchConsumeType aType);
86 :
87 : ~FetchBodyConsumer();
88 :
89 : void
90 : AssertIsOnTargetThread() const;
91 :
92 : bool
93 : RegisterWorkerHolder(workers::WorkerPrivate* aWorkerPrivate);
94 :
95 : nsCOMPtr<nsIThread> mTargetThread;
96 : nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
97 : RefPtr<FetchBody<Derived>> mBody;
98 :
99 : // Set when consuming the body is attempted on a worker.
100 : // Unset when consumption is done/aborted.
101 : // This WorkerHolder keeps alive the consumer via a cycle.
102 : UniquePtr<workers::WorkerHolder> mWorkerHolder;
103 :
104 : nsCOMPtr<nsIGlobalObject> mGlobal;
105 :
106 : // Always set whenever the FetchBodyConsumer is created on the worker thread.
107 : workers::WorkerPrivate* mWorkerPrivate;
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
|