Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 5 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
| 6 #define WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 6 #define WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
| 7 | 7 |
| 8 #include <queue> | |
| 9 | |
| 10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" | 9 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 15 #include "base/platform_file.h" | 13 //#include "ppapi/c/pp_file_info.h" |
|
viettrungluu
2011/12/01 22:59:43
Nit: Eh?
| |
| 16 #include "ppapi/c/pp_file_info.h" | 14 //#include "ppapi/c/pp_time.h" |
| 17 #include "ppapi/c/pp_time.h" | 15 //#include "ppapi/shared_impl/resource.h" |
| 18 #include "ppapi/shared_impl/resource.h" | 16 #include "ppapi/shared_impl/file_io_impl.h" |
| 19 #include "ppapi/thunk/ppb_file_io_api.h" | |
| 20 #include "webkit/plugins/ppapi/callbacks.h" | |
| 21 #include "webkit/plugins/ppapi/plugin_delegate.h" | 17 #include "webkit/plugins/ppapi/plugin_delegate.h" |
| 22 | 18 |
| 23 struct PP_CompletionCallback; | 19 struct PP_CompletionCallback; |
| 24 struct PPB_FileIO; | 20 struct PPB_FileIO; |
| 25 | 21 |
| 26 namespace webkit { | 22 namespace webkit { |
| 27 namespace ppapi { | 23 namespace ppapi { |
| 28 | 24 |
| 29 class QuotaFileIO; | 25 class QuotaFileIO; |
| 30 | 26 |
| 31 class PPB_FileIO_Impl : public ::ppapi::Resource, | 27 class PPB_FileIO_Impl : public ::ppapi::FileIOImpl { |
| 32 public ::ppapi::thunk::PPB_FileIO_API { | |
| 33 public: | 28 public: |
| 34 explicit PPB_FileIO_Impl(PP_Instance instance); | 29 explicit PPB_FileIO_Impl(PP_Instance instance); |
| 35 virtual ~PPB_FileIO_Impl(); | 30 virtual ~PPB_FileIO_Impl(); |
| 36 | 31 |
| 37 // Resource overrides. | 32 // PPB_FileIO_API implementation (most of the operations are implemented |
| 38 virtual ::ppapi::thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; | 33 // as the "Validated" versions below). |
| 39 | |
| 40 // PPB_FileIO_API implementation. | |
| 41 virtual int32_t Open(PP_Resource file_ref, | |
| 42 int32_t open_flags, | |
| 43 PP_CompletionCallback callback) OVERRIDE; | |
| 44 virtual int32_t Query(PP_FileInfo* info, | |
| 45 PP_CompletionCallback callback) OVERRIDE; | |
| 46 virtual int32_t Touch(PP_Time last_access_time, | |
| 47 PP_Time last_modified_time, | |
| 48 PP_CompletionCallback callback) OVERRIDE; | |
| 49 virtual int32_t Read(int64_t offset, | |
| 50 char* buffer, | |
| 51 int32_t bytes_to_read, | |
| 52 PP_CompletionCallback callback) OVERRIDE; | |
| 53 virtual int32_t Write(int64_t offset, | |
| 54 const char* buffer, | |
| 55 int32_t bytes_to_write, | |
| 56 PP_CompletionCallback callback) OVERRIDE; | |
| 57 virtual int32_t SetLength(int64_t length, | |
| 58 PP_CompletionCallback callback) OVERRIDE; | |
| 59 virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE; | |
| 60 virtual void Close() OVERRIDE; | 34 virtual void Close() OVERRIDE; |
| 61 virtual int32_t GetOSFileDescriptor() OVERRIDE; | 35 virtual int32_t GetOSFileDescriptor() OVERRIDE; |
| 62 virtual int32_t WillWrite(int64_t offset, | 36 virtual int32_t WillWrite(int64_t offset, |
| 63 int32_t bytes_to_write, | 37 int32_t bytes_to_write, |
| 64 PP_CompletionCallback callback) OVERRIDE; | 38 PP_CompletionCallback callback) OVERRIDE; |
| 65 virtual int32_t WillSetLength(int64_t length, | 39 virtual int32_t WillSetLength(int64_t length, |
| 66 PP_CompletionCallback callback) OVERRIDE; | 40 PP_CompletionCallback callback) OVERRIDE; |
| 67 | 41 |
| 68 private: | 42 private: |
| 69 struct CallbackEntry { | 43 // FileIOImpl overrides. |
| 70 CallbackEntry(); | 44 virtual int32_t OpenValidated(PP_Resource file_ref_resource, |
| 71 CallbackEntry(const CallbackEntry& entry); | 45 ::ppapi::thunk::PPB_FileRef_API* file_ref_api, |
| 72 ~CallbackEntry(); | 46 int32_t open_flags, |
| 47 PP_CompletionCallback callback) OVERRIDE; | |
| 48 virtual int32_t QueryValidated(PP_FileInfo* info, | |
| 49 PP_CompletionCallback callback) OVERRIDE; | |
| 50 virtual int32_t TouchValidated(PP_Time last_access_time, | |
| 51 PP_Time last_modified_time, | |
| 52 PP_CompletionCallback callback) OVERRIDE; | |
| 53 virtual int32_t ReadValidated(int64_t offset, | |
| 54 char* buffer, | |
| 55 int32_t bytes_to_read, | |
| 56 PP_CompletionCallback callback) OVERRIDE; | |
| 57 virtual int32_t WriteValidated(int64_t offset, | |
| 58 const char* buffer, | |
| 59 int32_t bytes_to_write, | |
| 60 PP_CompletionCallback callback) OVERRIDE; | |
| 61 virtual int32_t SetLengthValidated(int64_t length, | |
| 62 PP_CompletionCallback callback) OVERRIDE; | |
| 63 virtual int32_t FlushValidated(PP_CompletionCallback callback) OVERRIDE; | |
| 73 | 64 |
| 74 scoped_refptr<TrackedCompletionCallback> callback; | 65 // Returns the plugin delegate for this resource if it exists, or NULL if it |
| 66 // doesn't. Calling code should always check for NULL. | |
| 67 PluginDelegate* GetPluginDelegate(); | |
| 75 | 68 |
| 76 // Pointer back to the caller's read buffer; only used by |Read()|. | 69 // Callback handlers. These mostly convert the PlatformFileError to the |
| 77 // Not owned. | 70 // PP_Error code and call the shared (non-"Platform") version. |
| 78 char* read_buffer; | 71 void ExecutePlatformGeneralCallback(base::PlatformFileError error_code); |
| 79 }; | 72 void ExecutePlatformOpenFileCallback(base::PlatformFileError error_code, |
| 80 | 73 base::PassPlatformFile file); |
| 81 enum OperationType { | 74 void ExecutePlatformQueryCallback(base::PlatformFileError error_code, |
| 82 // If there are pending reads, any other kind of async operation is not | 75 const base::PlatformFileInfo& file_info); |
| 83 // allowed. | 76 void ExecutePlatformReadCallback(base::PlatformFileError error_code, |
| 84 OPERATION_READ, | 77 const char* data, int bytes_read); |
| 85 // If there are pending writes, any other kind of async operation is not | 78 void ExecutePlatformWriteCallback(base::PlatformFileError error_code, |
| 86 // allowed. | 79 int bytes_written); |
| 87 OPERATION_WRITE, | 80 void ExecutePlatformWillWriteCallback(base::PlatformFileError error_code, |
| 88 // If there is a pending operation that is neither read nor write, no | 81 int bytes_written); |
| 89 // further async operation is allowed. | |
| 90 OPERATION_EXCLUSIVE, | |
| 91 // There is no pending operation right now. | |
| 92 OPERATION_NONE, | |
| 93 }; | |
| 94 | |
| 95 // Verifies: | |
| 96 // - that |callback| is valid (only nonblocking operation supported); | |
| 97 // - that the file is already open or not, depending on |should_be_open|; and | |
| 98 // - that no callback is already pending, or it is a read(write) request | |
| 99 // and currently the pending operations are reads(writes). | |
| 100 // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if | |
| 101 // the call should be aborted and that code returned to the plugin. | |
| 102 int32_t CommonCallValidation(bool should_be_open, | |
| 103 OperationType new_op, | |
| 104 PP_CompletionCallback callback); | |
| 105 | |
| 106 // Sets up a pending callback. This should only be called once it is certain | |
| 107 // that |PP_OK_COMPLETIONPENDING| will be returned. | |
| 108 // |read_buffer| is only used by read operations. | |
| 109 void RegisterCallback(OperationType op, | |
| 110 PP_CompletionCallback callback, | |
| 111 char* read_buffer); | |
| 112 void RunAndRemoveFirstPendingCallback(int32_t result); | |
| 113 | |
| 114 void StatusCallback(base::PlatformFileError error_code); | |
| 115 void AsyncOpenFileCallback(base::PlatformFileError error_code, | |
| 116 base::PassPlatformFile file); | |
| 117 void QueryInfoCallback(base::PlatformFileError error_code, | |
| 118 const base::PlatformFileInfo& file_info); | |
| 119 void ReadCallback(base::PlatformFileError error_code, | |
| 120 const char* data, int bytes_read); | |
| 121 void WriteCallback(base::PlatformFileError error_code, int bytes_written); | |
| 122 void WillWriteCallback(base::PlatformFileError error_code, int bytes_written); | |
| 123 | 82 |
| 124 base::PlatformFile file_; | 83 base::PlatformFile file_; |
| 125 PP_FileSystemType file_system_type_; | |
| 126 | 84 |
| 127 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. | 85 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. |
| 128 GURL file_system_url_; | 86 GURL file_system_url_; |
| 129 | 87 |
| 130 std::queue<CallbackEntry> callbacks_; | |
| 131 OperationType pending_op_; | |
| 132 | |
| 133 // Output buffer pointer for |Query()|; only non-null when a callback is | |
| 134 // pending for it. | |
| 135 PP_FileInfo* info_; | |
| 136 | |
| 137 // Pointer to a QuotaFileIO instance, which is valid only while a file | 88 // Pointer to a QuotaFileIO instance, which is valid only while a file |
| 138 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. | 89 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. |
| 139 scoped_ptr<QuotaFileIO> quota_file_io_; | 90 scoped_ptr<QuotaFileIO> quota_file_io_; |
| 140 | 91 |
| 141 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; | 92 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; |
| 142 | 93 |
| 143 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); | 94 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); |
| 144 }; | 95 }; |
| 145 | 96 |
| 146 } // namespace ppapi | 97 } // namespace ppapi |
| 147 } // namespace webkit | 98 } // namespace webkit |
| 148 | 99 |
| 149 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 100 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
| OLD | NEW |