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