1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=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/. */
7 #ifndef mozilla_dom_FileSystemTaskBase_h
8 #define mozilla_dom_FileSystemTaskBase_h
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/dom/FileSystemRequestParent.h"
12 #include "mozilla/dom/PFileSystemRequestChild.h"
21 class FileSystemParams
;
25 * The base class to implement a Task class.
26 * The task is used to handle the OOP (out of process) operations.
27 * The file system operations can only be performed in the parent process. When
28 * performing such a parent-process-only operation, a task will delivered the
29 * operation to the parent process if needed.
31 * The following diagram illustrates the how a API call from the content page
32 * starts a task and gets call back results.
34 * The left block is the call sequence inside the child process, while the
35 * right block is the call sequence inside the parent process.
37 * There are two types of API call. One is from the content page of the child
38 * process and we mark the steps as (1) to (8). The other is from the content
39 * page of the parent process and we mark the steps as (1') to (4').
44 * ______|________________ | _____________________|_____________
46 * | | Task in | | | Task in | |
47 * | | Child Process | | | Parent Process | |
49 * [new FileSystemTaskBase()] | | [new FileSystemTaskBase()] |
51 * | | (2) | | | (2') |
53 * | [GetRequestParams]------------->[new FileSystemTaskBase(...)] |
57 * | | | | -----------> [Work] |
59 * | | | | (5) | (3') |
61 * | | | | --------[HandleResult] |
65 * | [SetRequestResult]<-------------[GetRequestResult] | |
69 * |[HandlerCallback] | IPC | [HandlerCallback] |
70 * |_______|_______________| | |_________________________|_________|
75 * 1. From child process page
77 * (1) Call FileSystem API from content page with JS. Create a task and run.
78 * The base constructor [FileSystemTaskBase()] of the task should be called.
79 * (2) Forward the task to the parent process through the IPC and call
80 * [GetRequestParams] to prepare the parameters of the IPC.
82 * (3) The parent process receives IPC and handle it in
83 * FileystemRequestParent.
84 * Get the IPC parameters and create a task to run the IPC task. The base
85 * constructor [FileSystemTaskBase(aParam, aParent)] of the task should be
86 * called to set the task as an IPC task.
87 * (4) The task operation will be performed in the member function of [Work].
88 * A worker thread will be created to run that function. If error occurs
89 * during the operation, call [SetError] to record the error and then abort.
90 * (5) After finishing the task operation, call [HandleResult] to send the
91 * result back to the child process though the IPC.
92 * (6) Call [GetRequestResult] request result to prepare the parameters of the
93 * IPC. Because the formats of the error result for different task are the
94 * same, FileSystemTaskBase can handle the error message without interfering.
95 * Each task only needs to implement its specific success result preparation
96 * function -[GetSuccessRequestResult].
98 * (7) The child process receives IPC and calls [SetRequestResult] to get the
99 * task result. Each task needs to implement its specific success result
100 * parsing function [SetSuccessRequestResult] to get the success result.
101 * (8) Call [HandlerCallback] to send the task result to the content page.
102 * 2. From parent process page
103 * We don't need to send the task parameters and result to other process. So
104 * there are less steps, but their functions are the same. The correspondence
105 * between the two types of steps is:
111 class FileSystemTaskBase
113 , public PFileSystemRequestChild
117 * Start the task. If the task is running the child process, it will be
118 * forwarded to parent process by IPC, or else, creates a worker thread to
125 * The error codes are defined in xpcom/base/ErrorList.h and their
126 * corresponding error name and message are defined in dom/base/domerr.msg.
129 SetError(const nsresult
& aErrorCode
);
132 GetFileSystem() const;
135 * Get the type of permission access required to perform this task.
138 GetPermissionAccessType(nsCString
& aAccess
) const = 0;
143 * To create a task to handle the page content request.
145 explicit FileSystemTaskBase(FileSystemBase
* aFileSystem
);
148 * To create a parent process task delivered from the child process through
151 FileSystemTaskBase(FileSystemBase
* aFileSystem
,
152 const FileSystemParams
& aParam
,
153 FileSystemRequestParent
* aParent
);
156 ~FileSystemTaskBase();
159 * The function to perform task operation. It will be run on the worker
160 * thread of the parent process.
161 * Overrides this function to define the task operation for individual task.
167 * After the task is completed, this function will be called to pass the task
168 * result to the content page.
169 * Override this function to handle the call back to the content page.
172 HandlerCallback() = 0;
175 * Wrap the task parameter to FileSystemParams for sending it through IPC.
176 * It will be called when we need to forward a task from the child process to
177 * the prarent process.
178 * @param filesystem The string representation of the file system.
180 virtual FileSystemParams
181 GetRequestParams(const nsString
& aFileSystem
) const = 0;
184 * Wrap the task success result to FileSystemResponseValue for sending it
186 * It will be called when the task is completed successfully and we need to
187 * send the task success result back to the child process.
189 virtual FileSystemResponseValue
190 GetSuccessRequestResult() const = 0;
193 * Unwrap the IPC message to get the task success result.
194 * It will be called when the task is completed successfully and an IPC
195 * message is received in the child process and we want to get the task
199 SetSuccessRequestResult(const FileSystemResponseValue
& aValue
) = 0;
202 HasError() const { return mErrorValue
!= NS_OK
; }
204 // Overrides PFileSystemRequestChild
206 Recv__delete__(const FileSystemResponseValue
& value
) MOZ_OVERRIDE
;
209 GetBlobParent(nsIDOMFile
* aFile
) const;
211 nsresult mErrorValue
;
213 nsRefPtr
<FileSystemBase
> mFileSystem
;
214 nsRefPtr
<FileSystemRequestParent
> mRequestParent
;
217 * After finishing the task operation, handle the task result.
218 * If it is an IPC task, send back the IPC result. Or else, send the result
219 * to the content page.
225 * Wrap the task result to FileSystemResponseValue for sending it through IPC.
226 * It will be called when the task is completed and we need to
227 * send the task result back to the child process.
229 FileSystemResponseValue
230 GetRequestResult() const;
233 * Unwrap the IPC message to get the task result.
234 * It will be called when the task is completed and an IPC message is received
235 * in the child process and we want to get the task result.
238 SetRequestResult(const FileSystemResponseValue
& aValue
);
242 } // namespace mozilla
244 #endif // mozilla_dom_FileSystemTaskBase_h