/work/obj-fuzz/dist/include/mozilla/dom/FileSystemTaskBase.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_FileSystemTaskBase_h |
8 | | #define mozilla_dom_FileSystemTaskBase_h |
9 | | |
10 | | #include "mozilla/ErrorResult.h" |
11 | | #include "mozilla/dom/FileSystemRequestParent.h" |
12 | | #include "mozilla/dom/PFileSystemRequestChild.h" |
13 | | #include "nsIGlobalObject.h" |
14 | | #include "nsThreadUtils.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | class BlobImpl; |
20 | | class FileSystemBase; |
21 | | class FileSystemParams; |
22 | | |
23 | | /* |
24 | | * The base class to implement a Task class. |
25 | | * The file system operations can only be performed in the parent process. In |
26 | | * order to avoid duplicated code, we used PBackground for child-parent and |
27 | | * parent-parent communications. |
28 | | * |
29 | | * The following diagram illustrates the how a API call from the content page |
30 | | * starts a task and gets call back results. |
31 | | * |
32 | | * The left block is the call sequence inside any process loading content, while |
33 | | * the right block is the call sequence only inside the parent process. |
34 | | * |
35 | | * Page |
36 | | * | |
37 | | * | (1) |
38 | | * ______|_________________________ | _________________________________ |
39 | | * | | | | | | |
40 | | * | | | | | | |
41 | | * | V | IPC | PBackground thread on | |
42 | | * | [new FileSystemTaskChildBase()] | | | the parent process | |
43 | | * | | | | | | |
44 | | * | | (2) | | | |
45 | | * | V | (3) | | |
46 | | * | [GetRequestParams]------------------->[new FileSystemTaskParentBase()] | |
47 | | * | | | | | |
48 | | * | | | | | (4) _____________ | |
49 | | * | | | | | | | | |
50 | | * | | | | | | I/O Thread | | |
51 | | * | | | | | | | | |
52 | | * | | | | ---------> [IOWork] | | |
53 | | * | | IPC | | | | | |
54 | | * | | | | | | (5) | | |
55 | | * | | | | -------------- | | |
56 | | * | | | | | |_____________| | |
57 | | * | | | | | | |
58 | | * | | | | V | |
59 | | * | | | | [HandleResult] | |
60 | | * | | | | | | |
61 | | * | | | | (6) | |
62 | | * | | (7) | V | |
63 | | * | [SetRequestResult]<---------------------[GetRequestResult] | |
64 | | * | | | | | |
65 | | * | | (8) | | | | |
66 | | * | V | | | | |
67 | | * |[HandlerCallback] | IPC | | |
68 | | * |_______|_________________________| | |_________________________________| |
69 | | * | | |
70 | | * V |
71 | | * Page |
72 | | * |
73 | | * 1. From the process that is handling the request |
74 | | * Child/Parent (it can be in any process): |
75 | | * (1) Call FileSystem API from content page with JS. Create a task and run. |
76 | | * The base constructor [FileSystemTaskChildBase()] of the task should be |
77 | | * called. |
78 | | * (2) Forward the task to the parent process through the IPC and call |
79 | | * [GetRequestParams] to prepare the parameters of the IPC. |
80 | | * Parent: |
81 | | * (3) The parent process receives IPC and handle it in |
82 | | * FileystemRequestParent. Get the IPC parameters and create a task to run the |
83 | | * IPC task. |
84 | | * (4) The task operation will be performed in the member function of [IOWork]. |
85 | | * A I/O thread will be created to run that function. If error occurs |
86 | | * during the operation, call [SetError] to record the error and then abort. |
87 | | * (5) After finishing the task operation, call [HandleResult] to send the |
88 | | * result back to the child process though the IPC. |
89 | | * (6) Call [GetRequestResult] request result to prepare the parameters of the |
90 | | * IPC. Because the formats of the error result for different task are the |
91 | | * same, FileSystemTaskChildBase can handle the error message without |
92 | | * interfering. |
93 | | * Each task only needs to implement its specific success result preparation |
94 | | * function -[GetSuccessRequestResult]. |
95 | | * Child/Parent: |
96 | | * (7) The process receives IPC and calls [SetRequestResult] to get the |
97 | | * task result. Each task needs to implement its specific success result |
98 | | * parsing function [SetSuccessRequestResult] to get the success result. |
99 | | * (8) Call [HandlerCallback] to send the task result to the content page. |
100 | | */ |
101 | | class FileSystemTaskChildBase : public PFileSystemRequestChild |
102 | | { |
103 | | public: |
104 | | NS_INLINE_DECL_REFCOUNTING(FileSystemTaskChildBase) |
105 | | |
106 | | /* |
107 | | * Start the task. It will dispatch all the information to the parent process, |
108 | | * PBackground thread. This method must be called from the owning thread. |
109 | | */ |
110 | | void |
111 | | Start(); |
112 | | |
113 | | /* |
114 | | * The error codes are defined in xpcom/base/ErrorList.h and their |
115 | | * corresponding error name and message are defined in dom/base/domerr.msg. |
116 | | */ |
117 | | void |
118 | | SetError(const nsresult& aErrorCode); |
119 | | |
120 | | FileSystemBase* |
121 | | GetFileSystem() const; |
122 | | |
123 | | /* |
124 | | * After the task is completed, this function will be called to pass the task |
125 | | * result to the content page. This method is called in the owning thread. |
126 | | * Override this function to handle the call back to the content page. |
127 | | */ |
128 | | virtual void |
129 | | HandlerCallback() = 0; |
130 | | |
131 | | bool |
132 | 0 | HasError() const { return NS_FAILED(mErrorValue); } |
133 | | |
134 | | protected: |
135 | | /* |
136 | | * To create a task to handle the page content request. |
137 | | */ |
138 | | FileSystemTaskChildBase(nsIGlobalObject* aGlobalObject, |
139 | | FileSystemBase* aFileSystem); |
140 | | |
141 | | virtual |
142 | | ~FileSystemTaskChildBase(); |
143 | | |
144 | | /* |
145 | | * Wrap the task parameter to FileSystemParams for sending it through IPC. |
146 | | * It will be called when we need to forward a task from the child process to |
147 | | * the parent process. This method runs in the owning thread. |
148 | | * @param filesystem The string representation of the file system. |
149 | | */ |
150 | | virtual FileSystemParams |
151 | | GetRequestParams(const nsString& aSerializedDOMPath, |
152 | | ErrorResult& aRv) const = 0; |
153 | | |
154 | | /* |
155 | | * Unwrap the IPC message to get the task success result. |
156 | | * It will be called when the task is completed successfully and an IPC |
157 | | * message is received in the child process and we want to get the task |
158 | | * success result. This method runs in the owning thread. |
159 | | */ |
160 | | virtual void |
161 | | SetSuccessRequestResult(const FileSystemResponseValue& aValue, |
162 | | ErrorResult& aRv) = 0; |
163 | | |
164 | | // Overrides PFileSystemRequestChild |
165 | | virtual mozilla::ipc::IPCResult |
166 | | Recv__delete__(const FileSystemResponseValue& value) override; |
167 | | |
168 | | nsresult mErrorValue; |
169 | | RefPtr<FileSystemBase> mFileSystem; |
170 | | nsCOMPtr<nsIGlobalObject> mGlobalObject; |
171 | | |
172 | | private: |
173 | | |
174 | | /* |
175 | | * Unwrap the IPC message to get the task result. |
176 | | * It will be called when the task is completed and an IPC message is received |
177 | | * in the content process and we want to get the task result. This runs on the |
178 | | * owning thread. |
179 | | */ |
180 | | void |
181 | | SetRequestResult(const FileSystemResponseValue& aValue); |
182 | | }; |
183 | | |
184 | | // This class is the 'alter ego' of FileSystemTaskChildBase in the PBackground |
185 | | // world. |
186 | | class FileSystemTaskParentBase : public Runnable |
187 | | { |
188 | | public: |
189 | | FileSystemTaskParentBase() |
190 | | : Runnable("FileSystemTaskParentBase") |
191 | | , mErrorValue(NS_ERROR_NOT_INITIALIZED) |
192 | 0 | {} |
193 | | |
194 | | /* |
195 | | * Start the task. This must be called from the PBackground thread only. |
196 | | */ |
197 | | void |
198 | | Start(); |
199 | | |
200 | | /* |
201 | | * The error codes are defined in xpcom/base/ErrorList.h and their |
202 | | * corresponding error name and message are defined in dom/base/domerr.msg. |
203 | | */ |
204 | | void |
205 | | SetError(const nsresult& aErrorCode); |
206 | | |
207 | | /* |
208 | | * The function to perform task operation. It will be run on the I/O |
209 | | * thread of the parent process. |
210 | | * Overrides this function to define the task operation for individual task. |
211 | | */ |
212 | | virtual nsresult |
213 | | IOWork() = 0; |
214 | | |
215 | | /* |
216 | | * Wrap the task success result to FileSystemResponseValue for sending it |
217 | | * through IPC. This method runs in the PBackground thread. |
218 | | * It will be called when the task is completed successfully and we need to |
219 | | * send the task success result back to the child process. |
220 | | */ |
221 | | virtual FileSystemResponseValue |
222 | | GetSuccessRequestResult(ErrorResult& aRv) const = 0; |
223 | | |
224 | | /* |
225 | | * After finishing the task operation, handle the task result. |
226 | | * If it is an IPC task, send back the IPC result. It runs on the PBackground |
227 | | * thread. |
228 | | */ |
229 | | void |
230 | | HandleResult(); |
231 | | |
232 | | bool |
233 | 0 | HasError() const { return NS_FAILED(mErrorValue); } |
234 | | |
235 | | NS_IMETHOD |
236 | | Run() override; |
237 | | |
238 | | virtual nsresult |
239 | | GetTargetPath(nsAString& aPath) const = 0; |
240 | | |
241 | | private: |
242 | | /* |
243 | | * Wrap the task result to FileSystemResponseValue for sending it through IPC. |
244 | | * It will be called when the task is completed and we need to |
245 | | * send the task result back to the content. This runs on the PBackground |
246 | | * thread. |
247 | | */ |
248 | | FileSystemResponseValue |
249 | | GetRequestResult() const; |
250 | | |
251 | | protected: |
252 | | /* |
253 | | * To create a parent process task delivered from the child process through |
254 | | * IPC. |
255 | | */ |
256 | | FileSystemTaskParentBase(FileSystemBase* aFileSystem, |
257 | | const FileSystemParams& aParam, |
258 | | FileSystemRequestParent* aParent); |
259 | | |
260 | | virtual |
261 | | ~FileSystemTaskParentBase(); |
262 | | |
263 | | nsresult mErrorValue; |
264 | | RefPtr<FileSystemBase> mFileSystem; |
265 | | RefPtr<FileSystemRequestParent> mRequestParent; |
266 | | nsCOMPtr<nsIEventTarget> mBackgroundEventTarget; |
267 | | }; |
268 | | |
269 | | } // namespace dom |
270 | | } // namespace mozilla |
271 | | |
272 | | #endif // mozilla_dom_FileSystemTaskBase_h |