Index: ppapi/proxy/filesystem_provider_resource.h |
diff --git a/ppapi/proxy/filesystem_provider_resource.h b/ppapi/proxy/filesystem_provider_resource.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..478e676e9dde4eb5a25ee3f24c54f20539f2f5ba |
--- /dev/null |
+++ b/ppapi/proxy/filesystem_provider_resource.h |
@@ -0,0 +1,497 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+#ifndef FILESYSTEM_PROVIDER_RESOURCE_H |
+#define FILESYSTEM_PROVIDER_RESOURCE_H |
+ |
+#include <chrono> |
+#include <map> |
+ |
+#include "ppapi/proxy/connection.h" |
+#include "ppapi/proxy/plugin_resource.h" |
+#include "ppapi/proxy/ppapi_proxy_export.h" |
+#include "ppapi/shared_impl/resource.h" |
+#include "ppapi/thunk/ppb_filesystem_provider_api.h" |
+ |
+namespace ppapi { |
+namespace proxy { |
+ |
+namespace filesystem_provider_internal{ |
+struct OperationTranslator; |
+} // namespace |
+class PPAPI_PROXY_EXPORT FilesystemProviderResource |
+ :public PluginResource, |
+ public NON_EXPORTED_BASE(thunk::PPB_FilesystemProvider_API) { |
+ private: |
+ // Waiting room for shared memory |
+ // This is used when sending read file responses to the browser |
+ class ShmChannelSynchronizer{ |
+ typedef base::Callback<bool(scoped_ptr<base::ListValue>, const char* data, |
+ uint32_t data_size)> |
+ FlushResponseCallback; |
+ public: |
+ ShmChannelSynchronizer( uint32_t size, FlushResponseCallback callback ); |
+ ~ShmChannelSynchronizer(); |
+ // This will flush send the re |
+ bool EnqueueResponse(scoped_ptr<base::ListValue> response, |
+ const char* data, uint32_t data_size); |
+ bool PushNextResponse(); |
+ bool AckLastPushedResponse(); |
+ private: |
+ uint32_t size(); |
+ uint32_t max_size; |
+ ScopedVector<base::ListValue> replies; |
+ bool ready; |
+ FlushResponseCallback callback; |
+ }; |
+ // All the information we need about the filesystem is stored here |
+ struct ProvidedFilesystemInfo{ |
+ ProvidedFilesystemInfo(); |
+ ~ProvidedFilesystemInfo(); |
+ std::string filesystem_id; |
+ std::string display_name; |
+ bool writable; |
+ int32_t opened_files_limit; |
+ }; |
+ // Shared memory container |
+ struct ShmBuffer { |
+ ShmBuffer(uint32_t read_size, |
+ uint32_t write_size, scoped_ptr<base::SharedMemory> shm); |
+ ~ShmBuffer(); |
+ uint32_t read_size; |
+ uint32_t write_size; |
+ scoped_ptr<base::SharedMemory> shm; |
+ }; |
+ // An interface used for statistics and ill intended |
+ // notifications from the plugin implementation.Basically this will filter out |
+ // operation responses that don't have an operation request from the browser. |
+ class RequestManager{ |
+ private: |
+ // Current Requests typedefs |
+ typedef std::pair<PP_OperationType_Dev, |
+ std::chrono::time_point<std::chrono::system_clock>> |
+ OperationStartTimePair; |
+ typedef std::map<int32_t, OperationStartTimePair > |
+ ListOfRequests; |
+ typedef ListOfRequests::iterator ListOfRequestsIterator; |
+ |
+ public: |
+ RequestManager(); |
+ ~RequestManager(); |
+ // Adds a request |
+ void AddRequest( int32_t request_id, PP_OperationType_Dev operation ); |
+ // Removes a request. This is successful only if the request previously |
+ // existed. The out_time_span contains the time in seconds of the operation |
+ bool RemoveRequest( int32_t request_id, double* out_time_span); |
+ |
+ private: |
+ ListOfRequests requests_; |
+ }; |
+ public: |
+ FilesystemProviderResource( |
+ Connection connection, |
+ PP_Instance instance ); |
+ virtual ~FilesystemProviderResource(); |
+ // PluginResource overrides: |
+ virtual thunk::PPB_FilesystemProvider_API* |
+ AsPPB_FilesystemProvider_API() override; |
+ void OnReplyReceived( |
+ const ResourceMessageReplyParams ¶ms, |
+ const IPC::Message &msg) override; |
+ // PPB_FilesystemProvider_API overrides |
+ // The below functions will be called from the instance module |
+ // to notify the browser or to request certain actions from the browser |
+ virtual int32_t Mount( |
+ struct PP_Var filesystem_id, |
+ struct PP_Var display_name, |
+ PP_Bool writable, |
+ int32_t opened_files_limit, |
+ PP_ProviderError_Dev* error, |
+ scoped_refptr<TrackedCallback> callback) override; |
+ virtual int32_t Unmount( |
+ struct PP_Var filesystemId, |
+ PP_ProviderError_Dev* error, |
+ scoped_refptr<TrackedCallback> callback) override; |
+ |
+ virtual int32_t SendSuccessResponse( |
+ PP_OperationType_Dev operation_type, |
+ int32_t request_id) override; |
+ virtual int32_t SendErrorResponse( |
+ PP_OperationType_Dev operation_type, |
+ PP_ProviderError_Dev error, |
+ int32_t request_id) override; |
+ |
+ virtual int32_t SendMetadataSuccessResponse( |
+ const struct PP_EntryMetadata_Dev* metadata, |
+ int32_t request_id) override; |
+ virtual int32_t SendReadDirectorySuccessResponse( |
+ uint32_t array_size, |
+ const struct PP_EntryMetadata_Dev entries[], |
+ PP_Bool has_more, |
+ int32_t request_id) override; |
+ virtual int32_t SendReadFileSuccessResponse( |
+ uint32_t data_size, |
+ const void* data, |
+ PP_Bool has_more, |
+ int32_t request_id) override; |
+ virtual int32_t GetNextRequest( |
+ PP_FilesystemRequest *request, |
+ scoped_refptr<TrackedCallback> callback)override; |
+ virtual int32_t FreeWriteRequestBuffer(const void *buffer)override; |
+ |
+ |
+ private: |
+ // Message Handlers for the browser messages |
+ void OnPluginMsgMountReply( |
+ const ResourceMessageReplyParams& params, |
+ PP_ProviderError_Dev error); |
+ void OnPluginMsgUnmountReply( |
+ const ResourceMessageReplyParams& params, |
+ PP_ProviderError_Dev error); |
+ // Handles the file system operations |
+ // requested by the browser |
+ void OnPluginMsgOperationRequest( |
+ const ResourceMessageParams& params, |
+ const PP_OperationType_Dev& operation, |
+ const base::ListValue& operationArgs); |
+ void OnPluginMsgBuffersReady(const ResourceMessageParams& params, |
+ uint32_t buffer_length, uint32_t write_buffer_size); |
+ void OnPluginMsgReadAck( |
+ const ResourceMessageReplyParams& params); |
+ |
+ // Remember a request in the provided container address |
+ bool WriteRequest(); |
+ // Send Read response to browser |
+ bool FlushReadResponse(scoped_ptr<base::ListValue> response, |
+ const char *data, |
+ uint32_t data_size); |
+ // The filesystem information |
+ ProvidedFilesystemInfo filesystem_info_; |
+ // Flag for the state of the filesystem |
+ bool mounted_; |
+ // User callbacks |
+ scoped_refptr<TrackedCallback> mount_callback_; |
+ scoped_refptr<TrackedCallback> unmount_callback_; |
+ scoped_refptr<TrackedCallback> get_next_request_callback_; |
+ // State for the above callbacks |
+ PP_ProviderError_Dev* mount_callback_var_; |
+ PP_ProviderError_Dev* unmount_callback_var_; |
+ struct PP_FilesystemRequest* get_next_request_callback_var_; |
+ // Keep received requests until consumed |
+ // by calling GetNextRequest |
+ ScopedVector<filesystem_provider_internal::OperationTranslator> |
+ received_requests_; |
+ // Received write requests that are buffered from when GetNextRequest |
+ // is called until FreeWriteRequestBuffer is called. |
+ ScopedVector<filesystem_provider_internal::OperationTranslator> |
+ received_write_requests_; |
+ // Shared memory buffer |
+ scoped_ptr<ShmBuffer> read_write_buffer_; |
+ // A RequestManager that controls access to the shared memory write |
+ scoped_ptr<FilesystemProviderResource::RequestManager> request_manager_; |
+ // Cacher for replys that content for the shared mem buffer |
+ scoped_ptr<ShmChannelSynchronizer> read_channel_controller_; |
+ DISALLOW_COPY_AND_ASSIGN( FilesystemProviderResource ); |
+}; |
+ |
+namespace filesystem_provider_internal{ |
+ |
+struct OperationTranslator{ |
+ virtual ~OperationTranslator(); |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()=0; |
+}; |
+struct AbortOperationTranslator : public OperationTranslator{ |
+ virtual ~AbortOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static |
+ scoped_ptr< AbortOperationTranslator > PopulateFromRequest( |
+ const base::ListValue& request ); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // An ID of the request to be aborted. |
+ int operation_request_id; |
+ private: |
+ AbortOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(AbortOperationTranslator); |
+}; |
+ |
+struct CloseFileOperationTranslator : public OperationTranslator{ |
+ ~CloseFileOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ // Creates a CloseFileRequestedOptions object from a base::Value, or NULL on |
+ // failure. |
+ static |
+ scoped_ptr<CloseFileOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // A request ID used to open the file. |
+ int open_request_id; |
+ private: |
+ CloseFileOperationTranslator(){} |
+ DISALLOW_COPY_AND_ASSIGN(CloseFileOperationTranslator); |
+}; |
+ |
+struct CopyEntryOperationTranslator : public OperationTranslator{ |
+ ~CopyEntryOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<CopyEntryOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The source path of the entry to be copied. |
+ std::string source_path; |
+ // The destination path for the copy operation. |
+ std::string target_path; |
+ private: |
+ CopyEntryOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(CopyEntryOperationTranslator); |
+}; |
+ |
+struct CreateDirectoryOperationTranslator : public OperationTranslator { |
+ ~CreateDirectoryOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<CreateDirectoryOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request ); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the directory to be created. |
+ std::string directory_path; |
+ // Whether the operation is recursive (for directories only). |
+ bool recursive; |
+ private: |
+ CreateDirectoryOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(CreateDirectoryOperationTranslator); |
+}; |
+ |
+struct CreateFileOperationTranslator : public OperationTranslator{ |
+ ~CreateFileOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<CreateFileOperationTranslator> PopulateFromRequest( |
+ const base::ListValue &request ); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the file to be created. |
+ std::string file_path; |
+ private: |
+ CreateFileOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(CreateFileOperationTranslator); |
+}; |
+ |
+struct DeleteEntryOperationTranslator : public OperationTranslator{ |
+ ~DeleteEntryOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<DeleteEntryOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request ); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the entry to be deleted. |
+ std::string entry_path; |
+ // Whether the operation is recursive (for directories only). |
+ bool recursive; |
+ private: |
+ DeleteEntryOperationTranslator(){} |
+ DISALLOW_COPY_AND_ASSIGN(DeleteEntryOperationTranslator); |
+}; |
+ |
+struct GetMetadataOperationTranslator:public OperationTranslator{ |
+ ~GetMetadataOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<GetMetadataOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request ); |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the entry to fetch metadata about. |
+ std::string entry_path; |
+ // Set to <code>true</code> if the thumbnail is requested. |
+ bool thumbnail; |
+private: |
+ GetMetadataOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN( GetMetadataOperationTranslator ); |
+}; |
+ |
+struct MoveOperationTranslator : public OperationTranslator { |
+ ~MoveOperationTranslator()override{} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<MoveOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request ); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The source path of the entry to be moved into a new place. |
+ std::string source_path; |
+ // The destination path for the copy operation. |
+ std::string target_path; |
+ private: |
+ MoveOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(MoveOperationTranslator); |
+}; |
+ |
+struct OpenFileOperationTranslator : public OperationTranslator { |
+ ~OpenFileOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest()override; |
+ static scoped_ptr<OpenFileOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // A request ID which will be used by consecutive read/write and close |
+ // requests. |
+ int request_id; |
+ // The path of the file to be opened. |
+ std::string file_path; |
+ // Whether the file will be used for reading or writing. |
+ PP_OpenFileMode_Dev mode; |
+ private: |
+ OpenFileOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(OpenFileOperationTranslator); |
+}; |
+ |
+struct ReadDirectoryOperationTranslator : public OperationTranslator { |
+ ~ReadDirectoryOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<ReadDirectoryOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& value); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the directory which contents are requested. |
+ std::string directory_path; |
+ private: |
+ ReadDirectoryOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(ReadDirectoryOperationTranslator); |
+}; |
+ |
+struct ReadFileOperationTranslator : public OperationTranslator { |
+ ~ReadFileOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<ReadFileOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& value); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // A request ID used to open the file. |
+ int open_request_id; |
+ // Position in the file (in bytes) to start reading from. |
+ double offset; |
+ // Number of bytes to be returned. |
+ double length; |
+ private: |
+ ReadFileOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(ReadFileOperationTranslator); |
+}; |
+ |
+struct TruncateOperationTranslator : public OperationTranslator { |
+ ~TruncateOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<TruncateOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // The path of the file to be truncated. |
+ std::string file_path; |
+ // Number of bytes to be retained after the operation completes. |
+ double length; |
+ private: |
+ TruncateOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(TruncateOperationTranslator); |
+}; |
+ |
+struct UnmountOperationTranslator : public OperationTranslator { |
+ ~UnmountOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<UnmountOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request); |
+ // The identifier of the file system to be unmounted. |
+ std::string file_system_id; |
+ int request_id; |
+ private: |
+ UnmountOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(UnmountOperationTranslator); |
+}; |
+ |
+struct WriteFileOperationTranslator : public OperationTranslator { |
+ ~WriteFileOperationTranslator() override {} |
+ virtual scoped_ptr<PP_FilesystemRequest> ToFilesystemRequest() override; |
+ static scoped_ptr<WriteFileOperationTranslator> PopulateFromRequest( |
+ const base::ListValue& request, |
+ char* memory_address); |
+ // The identifier of the file system related to this operation. |
+ std::string file_system_id; |
+ // The unique identifier of this request. |
+ int request_id; |
+ // A request ID used to open the file. |
+ int open_request_id; |
+ // Position in the file (in bytes) to start writing the bytes from. |
+ double offset; |
+ // The size of the buffer |
+ double size; |
+ // Buffer for the file chunk |
+ std::vector<char> data; |
+ private: |
+ WriteFileOperationTranslator() {} |
+ DISALLOW_COPY_AND_ASSIGN(WriteFileOperationTranslator); |
+}; |
+ |
+struct PluginToBrowserTranslator{ |
+ ~PluginToBrowserTranslator() {} |
+ static scoped_ptr<base::ListValue> GenerateMountRequest( |
+ std::string filesystem_id, |
+ std::string display_name, |
+ bool writable, |
+ int32_t opened_files_limit); |
+ |
+ static scoped_ptr<base::ListValue> GenerateSuccessResponse( |
+ PP_OperationType_Dev operation_type, |
+ const std::string& filesystem_id, |
+ int32_t request_id, |
+ double time_span); |
+ |
+ static scoped_ptr<base::ListValue> GenerateFailureResponse( |
+ PP_OperationType_Dev operation_type, |
+ PP_ProviderError_Dev error, |
+ const std::string& filesystem_id, |
+ int32_t request_id, |
+ double time_span); |
+ static scoped_ptr<base::ListValue> GenerateMetadataResponse( |
+ const PP_EntryMetadata_Dev *metadata, |
+ int32_t request_id, |
+ const std::string& filesystem_id, |
+ double time_span); |
+ static scoped_ptr<base::ListValue> GenerateReadDirectoryResponse( |
+ uint32_t array_size, |
+ const PP_EntryMetadata_Dev entries[], |
+ PP_Bool has_more, int32_t request_id, const std::string& filesystem_id, |
+ double time_span); |
+ static scoped_ptr<base::ListValue> GenerateReadFileSuccessResponse( |
+ uint32_t data_size, bool has_more,int32_t request_id, |
+ double time_span, const std::string& filesystem_id); |
+ private: |
+ PluginToBrowserTranslator(){} |
+ DISALLOW_COPY_AND_ASSIGN(PluginToBrowserTranslator); |
+ |
+}; |
+ |
+}// namespace |
+}// namespace proxy |
+}// namespace ppapi |
+ |
+#endif // FILESYSTEM_PROVIDER_RESOURCE_H |
+ |