Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #ifndef mozilla_dom_filehandle_ActorsParent_h
6 : #define mozilla_dom_filehandle_ActorsParent_h
7 :
8 : #include "mozilla/dom/FileHandleStorage.h"
9 : #include "mozilla/dom/PBackgroundMutableFileParent.h"
10 : #include "mozilla/ipc/BackgroundParent.h"
11 : #include "nsAutoPtr.h"
12 : #include "nsClassHashtable.h"
13 : #include "nsCOMPtr.h"
14 : #include "nsHashKeys.h"
15 : #include "nsString.h"
16 : #include "nsTArrayForwardDeclare.h"
17 : #include "nsTHashtable.h"
18 :
19 : template <class> struct already_AddRefed;
20 : class nsIFile;
21 : class nsIRunnable;
22 : class nsIThreadPool;
23 :
24 : namespace mozilla {
25 :
26 : namespace ipc {
27 :
28 : class PBackgroundParent;
29 :
30 : } // namespace ipc
31 :
32 : namespace dom {
33 :
34 : class BlobImpl;
35 : class FileHandle;
36 : class FileHandleOp;
37 :
38 : class FileHandleThreadPool final
39 : {
40 : class FileHandleQueue;
41 : struct DelayedEnqueueInfo;
42 : class DirectoryInfo;
43 : struct StoragesCompleteCallback;
44 :
45 : nsCOMPtr<nsIThreadPool> mThreadPool;
46 : nsCOMPtr<nsIEventTarget> mOwningEventTarget;
47 :
48 : nsClassHashtable<nsCStringHashKey, DirectoryInfo> mDirectoryInfos;
49 :
50 : nsTArray<nsAutoPtr<StoragesCompleteCallback>> mCompleteCallbacks;
51 :
52 : bool mShutdownRequested;
53 : bool mShutdownComplete;
54 :
55 : public:
56 : static already_AddRefed<FileHandleThreadPool>
57 : Create();
58 :
59 : #ifdef DEBUG
60 : void
61 : AssertIsOnOwningThread() const;
62 :
63 : nsIEventTarget*
64 : GetThreadPoolEventTarget() const;
65 : #else
66 : void
67 : AssertIsOnOwningThread() const
68 : { }
69 : #endif
70 :
71 : void
72 : Enqueue(FileHandle* aFileHandle,
73 : FileHandleOp* aFileHandleOp,
74 : bool aFinish);
75 :
76 0 : NS_INLINE_DECL_REFCOUNTING(FileHandleThreadPool)
77 :
78 : void
79 : WaitForDirectoriesToComplete(nsTArray<nsCString>&& aDirectoryIds,
80 : nsIRunnable* aCallback);
81 :
82 : void
83 : Shutdown();
84 :
85 : private:
86 : FileHandleThreadPool();
87 :
88 : // Reference counted.
89 : ~FileHandleThreadPool();
90 :
91 : nsresult
92 : Init();
93 :
94 : void
95 : Cleanup();
96 :
97 : void
98 : FinishFileHandle(FileHandle* aFileHandle);
99 :
100 : bool
101 : MaybeFireCallback(StoragesCompleteCallback* aCallback);
102 : };
103 :
104 : class BackgroundMutableFileParentBase
105 : : public PBackgroundMutableFileParent
106 : {
107 : nsTHashtable<nsPtrHashKey<FileHandle>> mFileHandles;
108 : nsCString mDirectoryId;
109 : nsString mFileName;
110 : FileHandleStorage mStorage;
111 : bool mInvalidated;
112 : bool mActorWasAlive;
113 : bool mActorDestroyed;
114 :
115 : protected:
116 : nsCOMPtr<nsIFile> mFile;
117 :
118 : public:
119 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BackgroundMutableFileParentBase)
120 :
121 : void
122 : Invalidate();
123 :
124 : FileHandleStorage
125 0 : Storage() const
126 : {
127 0 : return mStorage;
128 : }
129 :
130 : const nsCString&
131 0 : DirectoryId() const
132 : {
133 0 : return mDirectoryId;
134 : }
135 :
136 : const nsString&
137 0 : FileName() const
138 : {
139 0 : return mFileName;
140 : }
141 :
142 : bool
143 : RegisterFileHandle(FileHandle* aFileHandle);
144 :
145 : void
146 : UnregisterFileHandle(FileHandle* aFileHandle);
147 :
148 : void
149 : SetActorAlive();
150 :
151 : bool
152 0 : IsActorDestroyed() const
153 : {
154 0 : mozilla::ipc::AssertIsOnBackgroundThread();
155 :
156 0 : return mActorWasAlive && mActorDestroyed;
157 : }
158 :
159 : bool
160 0 : IsInvalidated() const
161 : {
162 0 : mozilla::ipc::AssertIsOnBackgroundThread();
163 :
164 0 : return mInvalidated;
165 : }
166 :
167 : virtual void
168 0 : NoteActiveState()
169 0 : { }
170 :
171 : virtual void
172 0 : NoteInactiveState()
173 0 : { }
174 :
175 : virtual mozilla::ipc::PBackgroundParent*
176 : GetBackgroundParent() const = 0;
177 :
178 : virtual already_AddRefed<nsISupports>
179 : CreateStream(bool aReadOnly);
180 :
181 : virtual already_AddRefed<BlobImpl>
182 0 : CreateBlobImpl()
183 : {
184 0 : return nullptr;
185 : }
186 :
187 : protected:
188 : BackgroundMutableFileParentBase(FileHandleStorage aStorage,
189 : const nsACString& aDirectoryId,
190 : const nsAString& aFileName,
191 : nsIFile* aFile);
192 :
193 : // Reference counted.
194 : ~BackgroundMutableFileParentBase();
195 :
196 : // IPDL methods are only called by IPDL.
197 : virtual void
198 : ActorDestroy(ActorDestroyReason aWhy) override;
199 :
200 : virtual PBackgroundFileHandleParent*
201 : AllocPBackgroundFileHandleParent(const FileMode& aMode) override;
202 :
203 : virtual mozilla::ipc::IPCResult
204 : RecvPBackgroundFileHandleConstructor(PBackgroundFileHandleParent* aActor,
205 : const FileMode& aMode) override;
206 :
207 : virtual bool
208 : DeallocPBackgroundFileHandleParent(PBackgroundFileHandleParent* aActor)
209 : override;
210 :
211 : virtual mozilla::ipc::IPCResult
212 : RecvDeleteMe() override;
213 :
214 : virtual mozilla::ipc::IPCResult
215 : RecvGetFileId(int64_t* aFileId) override;
216 : };
217 :
218 : } // namespace dom
219 : } // namespace mozilla
220 :
221 : #endif // mozilla_dom_filehandle_ActorsParent_h
|