2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-03-05 03:25:40 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FileSystemTaskBase_h
|
|
|
|
#define mozilla_dom_FileSystemTaskBase_h
|
|
|
|
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/FileSystemRequestParent.h"
|
|
|
|
#include "mozilla/dom/PFileSystemRequestChild.h"
|
2016-04-09 18:17:02 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImpl;
|
2014-03-05 03:25:40 +00:00
|
|
|
class FileSystemBase;
|
|
|
|
class FileSystemParams;
|
2016-04-09 18:17:02 +00:00
|
|
|
class PBlobParent;
|
2014-03-05 03:25:40 +00:00
|
|
|
|
2016-04-13 11:15:56 +00:00
|
|
|
#define DIRECTORY_READ_PERMISSION "read"
|
|
|
|
#define DIRECTORY_WRITE_PERMISSION "write"
|
|
|
|
#define DIRECTORY_CREATE_PERMISSION "create"
|
|
|
|
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
|
|
|
* The base class to implement a Task class.
|
2016-04-09 18:17:02 +00:00
|
|
|
* The file system operations can only be performed in the parent process. In
|
|
|
|
* order to avoid duplicated code, we used PBackground for child-parent and
|
|
|
|
* parent-parent communications.
|
2014-03-05 03:25:40 +00:00
|
|
|
*
|
|
|
|
* The following diagram illustrates the how a API call from the content page
|
|
|
|
* starts a task and gets call back results.
|
|
|
|
*
|
2016-04-09 18:17:02 +00:00
|
|
|
* The left block is the call sequence inside any process loading content, while
|
|
|
|
* the right block is the call sequence only inside the parent process.
|
2014-03-05 03:25:40 +00:00
|
|
|
*
|
2016-04-09 18:17:02 +00:00
|
|
|
* Page
|
|
|
|
* |
|
|
|
|
* | (1)
|
|
|
|
* ______|_______________________ | __________________________________
|
|
|
|
* | | | | | |
|
2016-04-09 18:17:28 +00:00
|
|
|
* | | | | | |
|
|
|
|
* | V | IPC | PBackground thread on |
|
|
|
|
* | [new FileSystemTaskChildBase()] | | | the parent process |
|
|
|
|
* | | | | | |
|
|
|
|
* | | (2) | | | |
|
|
|
|
* | V | | | |
|
2016-04-09 18:17:02 +00:00
|
|
|
* | [FileSystemPermissionRequest------------------\ |
|
|
|
|
* | ::RequestForTask()] <------------------------/ |
|
2016-04-09 18:17:28 +00:00
|
|
|
* | | | | | |
|
|
|
|
* | | (3) | | |
|
|
|
|
* | V | (4) | |
|
2016-04-09 18:17:02 +00:00
|
|
|
* | [GetRequestParams]------------------->[new FileSystemTaskParentBase()] |
|
2016-04-09 18:17:28 +00:00
|
|
|
* | | | | |
|
|
|
|
* | | | | | (5) _____________ |
|
|
|
|
* | | | | | | | |
|
|
|
|
* | | | | | | I/O Thread | |
|
|
|
|
* | | | | | | | |
|
|
|
|
* | | | | ---------> [IOWork] | |
|
|
|
|
* | | IPC | | | | |
|
|
|
|
* | | | | | | (6) | |
|
|
|
|
* | | | | -------------- | |
|
|
|
|
* | | | | | |_____________| |
|
|
|
|
* | | | | | |
|
|
|
|
* | | | | V |
|
|
|
|
* | | | | [HandleResult] |
|
|
|
|
* | | | | | |
|
|
|
|
* | | | | (7) |
|
|
|
|
* | | (8) | V |
|
|
|
|
* | [SetRequestResult]<---------------------[GetRequestResult] |
|
|
|
|
* | | | | |
|
|
|
|
* | | (9) | | | |
|
|
|
|
* | V | | | |
|
|
|
|
* |[HandlerCallback] | IPC | |
|
|
|
|
* |_______|_________________________| | |_________________________________|
|
|
|
|
* | |
|
2016-04-09 18:17:02 +00:00
|
|
|
* V
|
|
|
|
* Page
|
2014-03-05 03:25:40 +00:00
|
|
|
*
|
2016-04-09 18:17:02 +00:00
|
|
|
* 1. From the process that is handling the request
|
|
|
|
* Child/Parent (it can be in any process):
|
2014-03-05 03:25:40 +00:00
|
|
|
* (1) Call FileSystem API from content page with JS. Create a task and run.
|
2016-04-09 18:17:28 +00:00
|
|
|
* The base constructor [FileSystemTaskChildBase()] of the task should be
|
|
|
|
* called.
|
|
|
|
* (2) The FileSystemTaskChildBase object is given to
|
2016-04-09 18:17:02 +00:00
|
|
|
* [FileSystemPermissionRequest::RequestForTask()] that will perform a
|
|
|
|
* permission check step if needed (See ePermissionCheckType enum). The real
|
|
|
|
* operation is done on the parent process but it's hidden by
|
|
|
|
* [nsContentPermissionUtils::AskPermission()]. If the permission check is not
|
|
|
|
* needed or if the page has the right permission, the
|
|
|
|
* FileSystemPermissionRequest will start the task (only once PBackground
|
|
|
|
* actor is fully initialized).
|
|
|
|
* (3) Forward the task to the parent process through the IPC and call
|
2014-03-05 03:25:40 +00:00
|
|
|
* [GetRequestParams] to prepare the parameters of the IPC.
|
|
|
|
* Parent:
|
2016-04-09 18:17:02 +00:00
|
|
|
* (4) The parent process receives IPC and handle it in
|
|
|
|
* FileystemRequestParent. Get the IPC parameters and create a task to run the
|
|
|
|
* IPC task. The base constructor [FileSystemTaskParentBase(aParam, aParent)]
|
|
|
|
* For security reasons, we do an additional permission check if needed. In
|
|
|
|
* the check fails, the child process will be killed.
|
|
|
|
* of the task should be called to set the task as an IPC task.
|
|
|
|
* (5) The task operation will be performed in the member function of [IOWork].
|
|
|
|
* A I/O thread will be created to run that function. If error occurs
|
2014-03-05 03:25:40 +00:00
|
|
|
* during the operation, call [SetError] to record the error and then abort.
|
2016-04-09 18:17:02 +00:00
|
|
|
* (6) After finishing the task operation, call [HandleResult] to send the
|
2014-03-05 03:25:40 +00:00
|
|
|
* result back to the child process though the IPC.
|
2016-04-09 18:17:02 +00:00
|
|
|
* (7) Call [GetRequestResult] request result to prepare the parameters of the
|
2014-03-05 03:25:40 +00:00
|
|
|
* IPC. Because the formats of the error result for different task are the
|
2016-04-09 18:17:28 +00:00
|
|
|
* same, FileSystemTaskChildBase can handle the error message without
|
|
|
|
* interfering.
|
2014-03-05 03:25:40 +00:00
|
|
|
* Each task only needs to implement its specific success result preparation
|
|
|
|
* function -[GetSuccessRequestResult].
|
2016-04-09 18:17:02 +00:00
|
|
|
* Child/Parent:
|
|
|
|
* (8) The process receives IPC and calls [SetRequestResult] to get the
|
2014-03-05 03:25:40 +00:00
|
|
|
* task result. Each task needs to implement its specific success result
|
|
|
|
* parsing function [SetSuccessRequestResult] to get the success result.
|
2016-04-09 18:17:02 +00:00
|
|
|
* (9) Call [HandlerCallback] to send the task result to the content page.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
2016-04-09 18:17:28 +00:00
|
|
|
class FileSystemTaskChildBase : public PFileSystemRequestChild
|
2014-03-05 03:25:40 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-04-09 18:17:28 +00:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(FileSystemTaskChildBase)
|
2016-04-09 18:17:02 +00:00
|
|
|
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
2016-04-09 18:17:02 +00:00
|
|
|
* Start the task. It will dispatch all the information to the parent process,
|
|
|
|
* PBackground thread. This method must be called from the owning thread.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Start();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The error codes are defined in xpcom/base/ErrorList.h and their
|
|
|
|
* corresponding error name and message are defined in dom/base/domerr.msg.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SetError(const nsresult& aErrorCode);
|
|
|
|
|
2014-03-05 03:24:19 +00:00
|
|
|
FileSystemBase*
|
|
|
|
GetFileSystem() const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the type of permission access required to perform this task.
|
|
|
|
*/
|
|
|
|
virtual void
|
|
|
|
GetPermissionAccessType(nsCString& aAccess) const = 0;
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
/*
|
2016-04-09 18:17:02 +00:00
|
|
|
* After the task is completed, this function will be called to pass the task
|
|
|
|
* result to the content page. This method is called in the owning thread.
|
|
|
|
* Override this function to handle the call back to the content page.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
2016-04-09 18:17:02 +00:00
|
|
|
virtual void
|
|
|
|
HandlerCallback() = 0;
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasError() const { return NS_FAILED(mErrorValue); }
|
2014-03-05 03:25:40 +00:00
|
|
|
|
2016-04-09 18:17:02 +00:00
|
|
|
protected:
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
2016-04-09 18:17:02 +00:00
|
|
|
* To create a task to handle the page content request.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
2016-04-09 18:17:28 +00:00
|
|
|
explicit FileSystemTaskChildBase(FileSystemBase* aFileSystem);
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
virtual
|
2016-04-09 18:17:28 +00:00
|
|
|
~FileSystemTaskChildBase();
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrap the task parameter to FileSystemParams for sending it through IPC.
|
|
|
|
* It will be called when we need to forward a task from the child process to
|
2016-04-09 18:17:02 +00:00
|
|
|
* the parent process. This method runs in the owning thread.
|
2014-03-05 03:25:40 +00:00
|
|
|
* @param filesystem The string representation of the file system.
|
|
|
|
*/
|
|
|
|
virtual FileSystemParams
|
2016-03-20 10:56:10 +00:00
|
|
|
GetRequestParams(const nsString& aSerializedDOMPath,
|
|
|
|
ErrorResult& aRv) const = 0;
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unwrap the IPC message to get the task success result.
|
|
|
|
* It will be called when the task is completed successfully and an IPC
|
|
|
|
* message is received in the child process and we want to get the task
|
2016-04-09 18:17:02 +00:00
|
|
|
* success result. This method runs in the owning thread.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
|
|
|
virtual void
|
2016-03-20 10:56:10 +00:00
|
|
|
SetSuccessRequestResult(const FileSystemResponseValue& aValue,
|
|
|
|
ErrorResult& aRv) = 0;
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
// Overrides PFileSystemRequestChild
|
|
|
|
virtual bool
|
2015-03-21 16:28:04 +00:00
|
|
|
Recv__delete__(const FileSystemResponseValue& value) override;
|
2014-03-05 03:25:40 +00:00
|
|
|
|
|
|
|
nsresult mErrorValue;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<FileSystemBase> mFileSystem;
|
2016-04-09 18:17:02 +00:00
|
|
|
|
2014-03-05 03:25:40 +00:00
|
|
|
private:
|
2016-04-09 18:17:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unwrap the IPC message to get the task result.
|
|
|
|
* It will be called when the task is completed and an IPC message is received
|
|
|
|
* in the content process and we want to get the task result. This runs on the
|
|
|
|
* owning thread.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SetRequestResult(const FileSystemResponseValue& aValue);
|
|
|
|
};
|
|
|
|
|
2016-04-09 18:17:28 +00:00
|
|
|
// This class is the 'alter ego' of FileSystemTaskChildBase in the PBackground
|
|
|
|
// world.
|
2016-04-26 00:23:21 +00:00
|
|
|
class FileSystemTaskParentBase : public Runnable
|
2016-04-09 18:17:02 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* Start the task. This must be called from the PBackground thread only.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Start();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The error codes are defined in xpcom/base/ErrorList.h and their
|
|
|
|
* corresponding error name and message are defined in dom/base/domerr.msg.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SetError(const nsresult& aErrorCode);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The function to perform task operation. It will be run on the I/O
|
|
|
|
* thread of the parent process.
|
|
|
|
* Overrides this function to define the task operation for individual task.
|
|
|
|
*/
|
|
|
|
virtual nsresult
|
|
|
|
IOWork() = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrap the task success result to FileSystemResponseValue for sending it
|
|
|
|
* through IPC. This method runs in the PBackground thread.
|
|
|
|
* It will be called when the task is completed successfully and we need to
|
|
|
|
* send the task success result back to the child process.
|
|
|
|
*/
|
|
|
|
virtual FileSystemResponseValue
|
|
|
|
GetSuccessRequestResult(ErrorResult& aRv) const = 0;
|
|
|
|
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
|
|
|
* After finishing the task operation, handle the task result.
|
2016-04-09 18:17:02 +00:00
|
|
|
* If it is an IPC task, send back the IPC result. It runs on the PBackground
|
|
|
|
* thread.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
HandleResult();
|
|
|
|
|
2016-04-09 18:17:02 +00:00
|
|
|
// If this task must do something on the main-thread before IOWork(), it must
|
|
|
|
// overwrite this method. Otherwise it returns true if the FileSystem must be
|
|
|
|
// initialized on the main-thread. It's called from the Background thread.
|
|
|
|
virtual bool
|
|
|
|
NeedToGoToMainThread() const;
|
|
|
|
|
|
|
|
// This method is called only if NeedToGoToMainThread() returns true.
|
|
|
|
// Of course, it runs on the main-thread.
|
|
|
|
virtual nsresult
|
|
|
|
MainThreadWork();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the type of permission access required to perform this task.
|
|
|
|
*/
|
|
|
|
virtual void
|
|
|
|
GetPermissionAccessType(nsCString& aAccess) const = 0;
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasError() const { return NS_FAILED(mErrorValue); }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override;
|
|
|
|
|
|
|
|
private:
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
|
|
|
* Wrap the task result to FileSystemResponseValue for sending it through IPC.
|
|
|
|
* It will be called when the task is completed and we need to
|
2016-04-09 18:17:02 +00:00
|
|
|
* send the task result back to the content. This runs on the PBackground
|
|
|
|
* thread.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
|
|
|
FileSystemResponseValue
|
|
|
|
GetRequestResult() const;
|
|
|
|
|
2016-04-09 18:17:02 +00:00
|
|
|
protected:
|
2014-03-05 03:25:40 +00:00
|
|
|
/*
|
2016-04-09 18:17:02 +00:00
|
|
|
* To create a parent process task delivered from the child process through
|
|
|
|
* IPC.
|
2014-03-05 03:25:40 +00:00
|
|
|
*/
|
2016-04-09 18:17:02 +00:00
|
|
|
FileSystemTaskParentBase(FileSystemBase* aFileSystem,
|
|
|
|
const FileSystemParams& aParam,
|
|
|
|
FileSystemRequestParent* aParent);
|
|
|
|
|
|
|
|
virtual
|
|
|
|
~FileSystemTaskParentBase();
|
|
|
|
|
|
|
|
nsresult mErrorValue;
|
|
|
|
RefPtr<FileSystemBase> mFileSystem;
|
|
|
|
RefPtr<FileSystemRequestParent> mRequestParent;
|
|
|
|
nsCOMPtr<nsIEventTarget> mBackgroundEventTarget;
|
2014-03-05 03:25:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FileSystemTaskBase_h
|