| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | 5 #ifndef PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ |
| 6 #define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | 6 #define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include "base/basictypes.h" |
| 9 | |
| 10 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "ppapi/c/pp_file_info.h" |
| 11 #include "ppapi/c/pp_resource.h" |
| 11 #include "ppapi/shared_impl/ppapi_shared_export.h" | 12 #include "ppapi/shared_impl/ppapi_shared_export.h" |
| 12 #include "ppapi/shared_impl/resource.h" | |
| 13 #include "ppapi/shared_impl/tracked_callback.h" | |
| 14 #include "ppapi/thunk/ppb_file_io_api.h" | |
| 15 | 13 |
| 16 namespace ppapi { | 14 namespace ppapi { |
| 17 | 15 |
| 18 namespace thunk { | 16 namespace thunk { |
| 19 class PPB_FileRef_API; | 17 class PPB_FileRef_API; |
| 20 } | 18 } |
| 21 | 19 |
| 22 class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, | 20 class PPAPI_SHARED_EXPORT PPB_FileIO_Shared { |
| 23 public thunk::PPB_FileIO_API { | 21 protected: |
| 24 public: | 22 PPB_FileIO_Shared(); |
| 25 PPB_FileIO_Shared(PP_Instance instance); | |
| 26 PPB_FileIO_Shared(const HostResource& host_resource); | |
| 27 ~PPB_FileIO_Shared(); | 23 ~PPB_FileIO_Shared(); |
| 28 | 24 |
| 29 // Resource overrides. | 25 // Only FileIOResource needs this, but leave here as shared code to make the |
| 30 virtual thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; | 26 // resource code succinct. |
| 31 | |
| 32 // PPB_FileIO_API implementation. | |
| 33 virtual int32_t Open(PP_Resource file_ref, | |
| 34 int32_t open_flags, | |
| 35 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 36 virtual int32_t Query(PP_FileInfo* info, | |
| 37 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 38 virtual int32_t Touch(PP_Time last_access_time, | |
| 39 PP_Time last_modified_time, | |
| 40 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 41 virtual int32_t Read(int64_t offset, | |
| 42 char* buffer, | |
| 43 int32_t bytes_to_read, | |
| 44 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 45 virtual int32_t ReadToArray(int64_t offset, | |
| 46 int32_t max_read_length, | |
| 47 PP_ArrayOutput* output_array_buffer, | |
| 48 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 49 virtual int32_t Write(int64_t offset, | |
| 50 const char* buffer, | |
| 51 int32_t bytes_to_write, | |
| 52 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 53 virtual int32_t SetLength(int64_t length, | |
| 54 scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 55 virtual int32_t Flush(scoped_refptr<TrackedCallback> callback) OVERRIDE; | |
| 56 | |
| 57 // Callback handler for different types of operations. | |
| 58 void ExecuteGeneralCallback(int32_t pp_error); | |
| 59 void ExecuteOpenFileCallback(int32_t pp_error); | |
| 60 void ExecuteQueryCallback(int32_t pp_error, const PP_FileInfo& info); | |
| 61 void ExecuteReadCallback(int32_t pp_error_or_bytes, const char* data); | |
| 62 | |
| 63 protected: | |
| 64 struct CallbackEntry { | |
| 65 CallbackEntry(); | |
| 66 CallbackEntry(const CallbackEntry& entry); | |
| 67 ~CallbackEntry(); | |
| 68 | |
| 69 scoped_refptr<TrackedCallback> callback; | |
| 70 | |
| 71 // Output buffer used only by |Read()|. | |
| 72 PP_ArrayOutput read_buffer; | |
| 73 | |
| 74 // Pointer back to the caller's PP_FileInfo structure for Query operations. | |
| 75 // NULL for non-query operations. Not owned. | |
| 76 PP_FileInfo* info; | |
| 77 }; | |
| 78 | |
| 79 enum OperationType { | 27 enum OperationType { |
| 80 // There is no pending operation right now. | 28 // There is no pending operation right now. |
| 81 OPERATION_NONE, | 29 OPERATION_NONE, |
| 82 | 30 |
| 83 // If there are pending reads, any other kind of async operation is not | 31 // If there are pending reads, any other kind of async operation is not |
| 84 // allowed. | 32 // allowed. |
| 85 OPERATION_READ, | 33 OPERATION_READ, |
| 86 | 34 |
| 87 // If there are pending writes, any other kind of async operation is not | 35 // If there are pending writes, any other kind of async operation is not |
| 88 // allowed. | 36 // allowed. |
| 89 OPERATION_WRITE, | 37 OPERATION_WRITE, |
| 90 | 38 |
| 91 // If there is a pending operation that is neither read nor write, no | 39 // If there is a pending operation that is neither read nor write, no |
| 92 // further async operation is allowed. | 40 // further async operation is allowed. |
| 93 OPERATION_EXCLUSIVE | 41 OPERATION_EXCLUSIVE |
| 94 }; | 42 }; |
| 95 | 43 |
| 44 // FileIO operations templates to perform real operation between common |
| 45 // pre/post-condition. |
| 46 int32_t DoOpen(PP_Resource file_ref, int32_t open_flags); |
| 47 int32_t DoQuery(); |
| 48 int32_t DoTouch(PP_Time last_access_time, PP_Time last_modified_time); |
| 49 int32_t DoRead(int64_t offset, int32_t bytes_to_read); |
| 50 int32_t DoWrite(int64_t offset, const char* buffer, int32_t bytes_to_write); |
| 51 int32_t DoSetLength(int64_t length); |
| 52 int32_t DoFlush(); |
| 53 |
| 54 void SetOpenSucceed(); |
| 55 bool CheckOpenState(bool should_be_open) const; |
| 56 |
| 96 // Validated versions of the FileIO API. Subclasses in the proxy and impl | 57 // Validated versions of the FileIO API. Subclasses in the proxy and impl |
| 97 // implement these so the common error checking stays here. | 58 // implement these so the common error checking stays here. |
| 98 virtual int32_t OpenValidated(PP_Resource file_ref_resource, | 59 virtual int32_t OpenValidated(PP_Resource file_ref_resource, |
| 99 thunk::PPB_FileRef_API* file_ref_api, | 60 thunk::PPB_FileRef_API* file_ref_api, |
| 100 int32_t open_flags, | 61 int32_t open_flags) = 0; |
| 101 scoped_refptr<TrackedCallback> callback) = 0; | 62 virtual int32_t QueryValidated() = 0; |
| 102 virtual int32_t QueryValidated(PP_FileInfo* info, | |
| 103 scoped_refptr<TrackedCallback> callback) = 0; | |
| 104 virtual int32_t TouchValidated(PP_Time last_access_time, | 63 virtual int32_t TouchValidated(PP_Time last_access_time, |
| 105 PP_Time last_modified_time, | 64 PP_Time last_modified_time) = 0; |
| 106 scoped_refptr<TrackedCallback> callback) = 0; | |
| 107 virtual int32_t ReadValidated(int64_t offset, | 65 virtual int32_t ReadValidated(int64_t offset, |
| 108 const PP_ArrayOutput& buffer, | 66 int32_t max_read_length) = 0; |
| 109 int32_t max_read_length, | |
| 110 scoped_refptr<TrackedCallback> callback) = 0; | |
| 111 virtual int32_t WriteValidated(int64_t offset, | 67 virtual int32_t WriteValidated(int64_t offset, |
| 112 const char* buffer, | 68 const char* buffer, |
| 113 int32_t bytes_to_write, | 69 int32_t bytes_to_write) = 0; |
| 114 scoped_refptr<TrackedCallback> callback) = 0; | 70 virtual int32_t SetLengthValidated(int64_t length) = 0; |
| 115 virtual int32_t SetLengthValidated( | 71 virtual int32_t FlushValidated() = 0; |
| 116 int64_t length, | |
| 117 scoped_refptr<TrackedCallback> callback) = 0; | |
| 118 virtual int32_t FlushValidated(scoped_refptr<TrackedCallback> callback) = 0; | |
| 119 | 72 |
| 120 // Called for every "Validated" function. | 73 // Called before every "Validated" function. It is responsible to make sure |
| 74 // that "state" is correct. For example, some operations are only valid after |
| 75 // the file is opened, or operations might need to run excludively. See |
| 76 // subclasses implementation for detail. |
| 121 // | 77 // |
| 122 // This verifies that the callback is valid and that no callback is already | 78 // It returns |PP_OK| on success, or |PP_ERROR_...| for various reasons. |
| 123 // pending, or it is a read(write) request and currently the pending | 79 virtual int32_t CommonPreCondition(bool should_be_open, |
| 124 // operations are reads(writes). | 80 OperationType new_op) = 0; |
| 125 // | |
| 126 // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if | |
| 127 // the call should be aborted and that code returned to the plugin. | |
| 128 int32_t CommonCallValidation(bool should_be_open, OperationType new_op); | |
| 129 | 81 |
| 130 // Sets up a pending callback. This should only be called once it is certain | 82 // Called after every "Validated" function. |
| 131 // that |PP_OK_COMPLETIONPENDING| will be returned. | 83 virtual void CommonPostCondition(OperationType new_op) = 0; |
| 132 // | |
| 133 // |read_buffer| is only used by read operations, |info| is used only by | |
| 134 // query operations. | |
| 135 void RegisterCallback(OperationType op, | |
| 136 scoped_refptr<TrackedCallback> callback, | |
| 137 const PP_ArrayOutput* read_buffer, | |
| 138 PP_FileInfo* info); | |
| 139 | |
| 140 // Pops the oldest callback from the queue and runs it. | |
| 141 void RunAndRemoveFirstPendingCallback(int32_t result); | |
| 142 | |
| 143 // The file system type specified in the Open() call. This will be | |
| 144 // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not | |
| 145 // indicate that the open command actually succeeded. | |
| 146 PP_FileSystemType file_system_type_; | |
| 147 | 84 |
| 148 // Set to true when the file has been successfully opened. | 85 // Set to true when the file has been successfully opened. |
| 149 bool file_open_; | 86 bool file_open_; |
| 150 | 87 |
| 151 std::deque<CallbackEntry> callbacks_; | |
| 152 OperationType pending_op_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared); | 88 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared); |
| 155 }; | 89 }; |
| 156 | 90 |
| 157 } // namespace ppapi | 91 } // namespace ppapi |
| 158 | 92 |
| 159 #endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ | 93 #endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ |
| OLD | NEW |