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