diff options
29 files changed, 1280 insertions, 431 deletions
diff --git a/chrome/test/ui/ppapi_uitest.cc b/chrome/test/ui/ppapi_uitest.cc index f065a93..04c957d 100644 --- a/chrome/test/ui/ppapi_uitest.cc +++ b/chrome/test/ui/ppapi_uitest.cc @@ -490,11 +490,9 @@ TEST_PPAPI_NACL_VIA_HTTP(MAYBE_FileIO_ReadWriteSetLength) TEST_PPAPI_NACL_VIA_HTTP(DISABLED_FileIO_WillWriteWillSetLength) TEST_PPAPI_IN_PROCESS_VIA_HTTP(FileRef) -// Disabled because it times out: http://crbug.com/89961 -// TEST_PPAPI_OUT_OF_PROCESS_VIA_HTTP(FileRef) +TEST_PPAPI_OUT_OF_PROCESS_VIA_HTTP(FileRef) TEST_PPAPI_NACL_VIA_HTTP(FileRef) - TEST_PPAPI_IN_PROCESS_VIA_HTTP(FileSystem) TEST_PPAPI_OUT_OF_PROCESS_VIA_HTTP(FileSystem) TEST_PPAPI_NACL_VIA_HTTP(FileSystem) diff --git a/ppapi/api/ppb_file_io.idl b/ppapi/api/ppb_file_io.idl index 0d2c845..d595bdc 100644 --- a/ppapi/api/ppb_file_io.idl +++ b/ppapi/api/ppb_file_io.idl @@ -99,8 +99,9 @@ interface PPB_FileIO { [in] PP_CompletionCallback callback); /** - * Query() queries info about the file opened by this FileIO object. This - * function will fail if the FileIO object has not been opened. + * Query() queries info about the file opened by this FileIO object. The + * FileIO object must be opened, and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a * FileIO. @@ -110,6 +111,8 @@ interface PPB_FileIO { * completion of Query(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t Query([in] PP_Resource file_io, [out] PP_FileInfo info, @@ -117,7 +120,9 @@ interface PPB_FileIO { /** * Touch() Updates time stamps for the file opened by this FileIO object. - * This function will fail if the FileIO object has not been opened. + * This function will fail if the FileIO object has not been opened. The + * FileIO object must be opened, and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -127,6 +132,8 @@ interface PPB_FileIO { * completion of Touch(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t Touch([in] PP_Resource file_io, [in] PP_Time last_access_time, @@ -150,7 +157,8 @@ interface PPB_FileIO { * @return An The number of bytes read an error code from * <code>pp_errors.h</code>. If the return value is 0, then end-of-file was * reached. It is valid to call Read() multiple times with a completion - * callback to queue up parallel reads from the file at different offsets. + * callback to queue up parallel reads from the file, but pending reads + * cannot be interleaved with other operations. */ int32_t Read([in] PP_Resource file_io, [in] int64_t offset, @@ -174,7 +182,8 @@ interface PPB_FileIO { * @return An The number of bytes written or an error code from * <code>pp_errors.h</code>. If the return value is 0, then end-of-file was * reached. It is valid to call Write() multiple times with a completion - * callback to queue up parallel writes to the file at different offsets. + * callback to queue up parallel writes to the file, but pending writes + * cannot be interleaved with other operations. */ int32_t Write([in] PP_Resource file_io, [in] int64_t offset, @@ -183,8 +192,9 @@ interface PPB_FileIO { [in] PP_CompletionCallback callback); /** * SetLength() sets the length of the file. If the file size is extended, - * then the extended area of the file is zero-filled. The FileIO object must - * have been opened with write access. + * then the extended area of the file is zero-filled. The FileIO object must + * have been opened with write access and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -193,13 +203,17 @@ interface PPB_FileIO { * completion of SetLength(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t SetLength([in] PP_Resource file_io, [in] int64_t length, [in] PP_CompletionCallback callback); /** - * Flush() flushes changes to disk. This call can be very expensive! + * Flush() flushes changes to disk. This call can be very expensive! The + * FileIO object must have been opened with write access and there must be no + * other operations pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -207,6 +221,8 @@ interface PPB_FileIO { * completion of Flush(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t Flush([in] PP_Resource file_io, [in] PP_CompletionCallback callback); diff --git a/ppapi/c/ppb_file_io.h b/ppapi/c/ppb_file_io.h index 333984b..aa028b1 100644 --- a/ppapi/c/ppb_file_io.h +++ b/ppapi/c/ppb_file_io.h @@ -3,7 +3,7 @@ * found in the LICENSE file. */ -/* From ppb_file_io.idl modified Mon Aug 29 10:11:34 2011. */ +/* From ppb_file_io.idl modified Thu Dec 1 10:47:02 2011. */ #ifndef PPAPI_C_PPB_FILE_IO_H_ #define PPAPI_C_PPB_FILE_IO_H_ @@ -117,8 +117,9 @@ struct PPB_FileIO { int32_t open_flags, struct PP_CompletionCallback callback); /** - * Query() queries info about the file opened by this FileIO object. This - * function will fail if the FileIO object has not been opened. + * Query() queries info about the file opened by this FileIO object. The + * FileIO object must be opened, and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a * FileIO. @@ -128,13 +129,17 @@ struct PPB_FileIO { * completion of Query(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t (*Query)(PP_Resource file_io, struct PP_FileInfo* info, struct PP_CompletionCallback callback); /** * Touch() Updates time stamps for the file opened by this FileIO object. - * This function will fail if the FileIO object has not been opened. + * This function will fail if the FileIO object has not been opened. The + * FileIO object must be opened, and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -144,6 +149,8 @@ struct PPB_FileIO { * completion of Touch(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t (*Touch)(PP_Resource file_io, PP_Time last_access_time, @@ -166,7 +173,8 @@ struct PPB_FileIO { * @return An The number of bytes read an error code from * <code>pp_errors.h</code>. If the return value is 0, then end-of-file was * reached. It is valid to call Read() multiple times with a completion - * callback to queue up parallel reads from the file at different offsets. + * callback to queue up parallel reads from the file, but pending reads + * cannot be interleaved with other operations. */ int32_t (*Read)(PP_Resource file_io, int64_t offset, @@ -189,7 +197,8 @@ struct PPB_FileIO { * @return An The number of bytes written or an error code from * <code>pp_errors.h</code>. If the return value is 0, then end-of-file was * reached. It is valid to call Write() multiple times with a completion - * callback to queue up parallel writes to the file at different offsets. + * callback to queue up parallel writes to the file, but pending writes + * cannot be interleaved with other operations. */ int32_t (*Write)(PP_Resource file_io, int64_t offset, @@ -198,8 +207,9 @@ struct PPB_FileIO { struct PP_CompletionCallback callback); /** * SetLength() sets the length of the file. If the file size is extended, - * then the extended area of the file is zero-filled. The FileIO object must - * have been opened with write access. + * then the extended area of the file is zero-filled. The FileIO object must + * have been opened with write access and there must be no other operations + * pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -208,12 +218,16 @@ struct PPB_FileIO { * completion of SetLength(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t (*SetLength)(PP_Resource file_io, int64_t length, struct PP_CompletionCallback callback); /** - * Flush() flushes changes to disk. This call can be very expensive! + * Flush() flushes changes to disk. This call can be very expensive! The + * FileIO object must have been opened with write access and there must be no + * other operations pending. * * @param[in] file_io A <code>PP_Resource</code> corresponding to a file * FileIO. @@ -221,6 +235,8 @@ struct PPB_FileIO { * completion of Flush(). * * @return An int32_t containing an error code from <code>pp_errors.h</code>. + * PP_ERROR_FAILED will be returned if the file isn't opened, and + * PP_ERROR_INPROGRESS will be returned if there is another operation pending. */ int32_t (*Flush)(PP_Resource file_io, struct PP_CompletionCallback callback); /** diff --git a/ppapi/ppapi_proxy.gypi b/ppapi/ppapi_proxy.gypi index 8765d44..20157f2 100644 --- a/ppapi/ppapi_proxy.gypi +++ b/ppapi/ppapi_proxy.gypi @@ -70,6 +70,8 @@ 'proxy/ppb_cursor_control_proxy.h', 'proxy/ppb_file_chooser_proxy.cc', 'proxy/ppb_file_chooser_proxy.h', + 'proxy/ppb_file_io_proxy.cc', + 'proxy/ppb_file_io_proxy.h', 'proxy/ppb_file_ref_proxy.cc', 'proxy/ppb_file_ref_proxy.h', 'proxy/ppb_file_system_proxy.cc', diff --git a/ppapi/ppapi_shared.gypi b/ppapi/ppapi_shared.gypi index 51b52df..f7a01d9 100644 --- a/ppapi/ppapi_shared.gypi +++ b/ppapi/ppapi_shared.gypi @@ -30,6 +30,8 @@ '../base/base.gyp:base', ], 'sources': [ + 'shared_impl/file_type_conversion.cc', + 'shared_impl/file_type_conversion.h', 'shared_impl/function_group_base.cc', 'shared_impl/function_group_base.h', 'shared_impl/host_resource.h', @@ -50,6 +52,8 @@ 'shared_impl/ppb_char_set_shared.cc', 'shared_impl/ppb_char_set_shared.h', 'shared_impl/ppb_crypto_shared.cc', + 'shared_impl/ppb_file_io_shared.cc', + 'shared_impl/ppb_file_io_shared.h', 'shared_impl/ppb_file_ref_shared.cc', 'shared_impl/ppb_file_ref_shared.h', 'shared_impl/ppb_font_shared.cc', diff --git a/ppapi/proxy/interface_list.cc b/ppapi/proxy/interface_list.cc index da978e1..2d5b90b 100644 --- a/ppapi/proxy/interface_list.cc +++ b/ppapi/proxy/interface_list.cc @@ -24,6 +24,7 @@ #include "ppapi/c/ppb_audio_config.h" #include "ppapi/c/ppb_audio.h" #include "ppapi/c/ppb_core.h" +#include "ppapi/c/ppb_file_io.h" #include "ppapi/c/ppb_file_ref.h" #include "ppapi/c/ppb_file_system.h" #include "ppapi/c/ppb_fullscreen.h" @@ -53,6 +54,7 @@ #include "ppapi/c/private/ppb_tcp_socket_private.h" #include "ppapi/c/private/ppb_udp_socket_private.h" #include "ppapi/c/trusted/ppb_broker_trusted.h" +#include "ppapi/c/trusted/ppb_file_io_trusted.h" #include "ppapi/c/trusted/ppb_url_loader_trusted.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/ppb_audio_input_proxy.h" @@ -62,6 +64,7 @@ #include "ppapi/proxy/ppb_core_proxy.h" #include "ppapi/proxy/ppb_cursor_control_proxy.h" #include "ppapi/proxy/ppb_file_chooser_proxy.h" +#include "ppapi/proxy/ppb_file_io_proxy.h" #include "ppapi/proxy/ppb_file_ref_proxy.h" #include "ppapi/proxy/ppb_file_system_proxy.h" #include "ppapi/proxy/ppb_flash_clipboard_proxy.h" diff --git a/ppapi/proxy/ppapi_messages.h b/ppapi/proxy/ppapi_messages.h index b894db9..32ac229 100644 --- a/ppapi/proxy/ppapi_messages.h +++ b/ppapi/proxy/ppapi_messages.h @@ -238,6 +238,22 @@ IPC_MESSAGE_ROUTED3( int32_t /* result_code (will be != PP_OK on failure */, std::vector<ppapi::PPB_FileRef_CreateInfo> /* chosen_files */) +// PPB_FileIO. +IPC_MESSAGE_ROUTED2(PpapiMsg_PPBFileIO_GeneralComplete, + ppapi::HostResource /* file_io */, + int32_t /* result */) +IPC_MESSAGE_ROUTED2(PpapiMsg_PPBFileIO_OpenFileComplete, + ppapi::HostResource /* file_io */, + int32_t /* result */) +IPC_MESSAGE_ROUTED3(PpapiMsg_PPBFileIO_QueryComplete, + ppapi::HostResource /* file_io */, + int32_t /* result */, + PP_FileInfo /* info */) +IPC_MESSAGE_ROUTED3(PpapiMsg_PPBFileIO_ReadComplete, + ppapi::HostResource /* file_io */, + int32_t /* result */, + std::string /* data */) + // PPB_FileRef. IPC_MESSAGE_ROUTED3( PpapiMsg_PPBFileRef_CallbackComplete, @@ -555,6 +571,43 @@ IPC_MESSAGE_ROUTED4(PpapiHostMsg_PPBFileChooser_Show, std::string /* suggested_file_name */, bool /* require_user_gesture */) +// PPB_FileIO. +IPC_SYNC_MESSAGE_ROUTED1_1(PpapiHostMsg_PPBFileIO_Create, + PP_Instance /* instance */, + ppapi::HostResource /* result */) +IPC_MESSAGE_ROUTED3(PpapiHostMsg_PPBFileIO_Open, + ppapi::HostResource /* host_resource */, + ppapi::HostResource /* file_ref_resource */, + int32_t /* open_flags */) +IPC_MESSAGE_ROUTED1(PpapiHostMsg_PPBFileIO_Close, + ppapi::HostResource /* host_resource */) +IPC_MESSAGE_ROUTED1(PpapiHostMsg_PPBFileIO_Query, + ppapi::HostResource /* host_resource */) +IPC_MESSAGE_ROUTED3(PpapiHostMsg_PPBFileIO_Touch, + ppapi::HostResource /* host_resource */, + PP_Time /* last_access_time */, + PP_Time /* last_modified_time */) +IPC_MESSAGE_ROUTED3(PpapiHostMsg_PPBFileIO_Read, + ppapi::HostResource /* host_resource */, + int64_t /* offset */, + int32_t /* bytes_to_read */) +IPC_MESSAGE_ROUTED3(PpapiHostMsg_PPBFileIO_Write, + ppapi::HostResource /* host_resource */, + int64_t /* offset */, + std::string /* data */) +IPC_MESSAGE_ROUTED2(PpapiHostMsg_PPBFileIO_SetLength, + ppapi::HostResource /* host_resource */, + int64_t /* length */) +IPC_MESSAGE_ROUTED1(PpapiHostMsg_PPBFileIO_Flush, + ppapi::HostResource /* host_resource */) +IPC_MESSAGE_ROUTED3(PpapiHostMsg_PPBFileIO_WillWrite, + ppapi::HostResource /* host_resource */, + int64_t /* offset */, + int32_t /* bytes_to_write */) +IPC_MESSAGE_ROUTED2(PpapiHostMsg_PPBFileIO_WillSetLength, + ppapi::HostResource /* host_resource */, + int64_t /* length */) + // PPB_FileRef. IPC_SYNC_MESSAGE_ROUTED2_1(PpapiHostMsg_PPBFileRef_Create, ppapi::HostResource /* file_system */, diff --git a/ppapi/proxy/ppapi_param_traits.cc b/ppapi/proxy/ppapi_param_traits.cc index ac66648..ca03910 100644 --- a/ppapi/proxy/ppapi_param_traits.cc +++ b/ppapi/proxy/ppapi_param_traits.cc @@ -124,7 +124,8 @@ bool ParamTraits<PP_FileInfo>::Read(const Message* m, void** iter, type != PP_FILETYPE_OTHER) return false; r->type = static_cast<PP_FileType>(type); - if (system_type != PP_FILESYSTEMTYPE_EXTERNAL && + if (system_type != PP_FILESYSTEMTYPE_INVALID && + system_type != PP_FILESYSTEMTYPE_EXTERNAL && system_type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && system_type != PP_FILESYSTEMTYPE_LOCALTEMPORARY) return false; diff --git a/ppapi/proxy/ppb_file_io_proxy.cc b/ppapi/proxy/ppb_file_io_proxy.cc new file mode 100644 index 0000000..0acd3ac --- /dev/null +++ b/ppapi/proxy/ppb_file_io_proxy.cc @@ -0,0 +1,442 @@ +// Copyright (c) 2011 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. + +#include "ppapi/proxy/ppb_file_io_proxy.h" + +#include "ppapi/c/pp_errors.h" +#include "ppapi/proxy/enter_proxy.h" +#include "ppapi/proxy/ppapi_messages.h" +#include "ppapi/proxy/ppb_file_ref_proxy.h" +#include "ppapi/shared_impl/ppapi_globals.h" +#include "ppapi/shared_impl/ppb_file_io_shared.h" +#include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/resource_tracker.h" + +using ppapi::thunk::PPB_FileIO_API; +using ppapi::thunk::PPB_FileRef_API; + +namespace ppapi { +namespace proxy { + +namespace { + +// The maximum size we'll support reading in one chunk. The renderer process +// must allocate a buffer sized according to the request of the plugin. To +// keep things from getting out of control, we cap the read size to this value. +// This should generally be OK since the API specifies that it may perform a +// partial read. +static const int32_t kMaxReadSize = 33554432; // 32MB + +typedef EnterHostFromHostResourceForceCallback<PPB_FileIO_API> EnterHostFileIO; +typedef EnterPluginFromHostResource<PPB_FileIO_API> EnterPluginFileIO; + +class FileIO : public PPB_FileIO_Shared { + public: + explicit FileIO(const HostResource& host_resource); + virtual ~FileIO(); + + // PPB_FileIO_API implementation (not provided by FileIOImpl). + virtual void Close() OVERRIDE; + virtual int32_t GetOSFileDescriptor() OVERRIDE; + virtual int32_t WillWrite(int64_t offset, + int32_t bytes_to_write, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t WillSetLength(int64_t length, + PP_CompletionCallback callback) OVERRIDE; + + private: + // FileIOImpl overrides. + virtual int32_t OpenValidated(PP_Resource file_ref_resource, + PPB_FileRef_API* file_ref_api, + int32_t open_flags, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t QueryValidated(PP_FileInfo* info, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t TouchValidated(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t ReadValidated(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t WriteValidated(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t SetLengthValidated(int64_t length, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t FlushValidated(PP_CompletionCallback callback) OVERRIDE; + + PluginDispatcher* GetDispatcher() const { + return PluginDispatcher::GetForResource(this); + } + + static const ApiID kApiID = API_ID_PPB_FILE_IO; + + DISALLOW_IMPLICIT_CONSTRUCTORS(FileIO); +}; + +FileIO::FileIO(const HostResource& host_resource) + : PPB_FileIO_Shared(host_resource) { +} + +FileIO::~FileIO() { + Close(); +} + +void FileIO::Close() { + if (file_open_) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Close(kApiID, + host_resource())); + } +} + +int32_t FileIO::GetOSFileDescriptor() { + return -1; +} + +int32_t FileIO::WillWrite(int64_t offset, + int32_t bytes_to_write, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_WillWrite( + kApiID, host_resource(), offset, bytes_to_write)); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::WillSetLength(int64_t length, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_WillSetLength( + kApiID, host_resource(), length)); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::OpenValidated(PP_Resource file_ref_resource, + PPB_FileRef_API* file_ref_api, + int32_t open_flags, + PP_CompletionCallback callback) { + Resource* file_ref_object = + PpapiGlobals::Get()->GetResourceTracker()->GetResource(file_ref_resource); + + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Open( + kApiID, host_resource(), file_ref_object->host_resource(), open_flags)); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::QueryValidated(PP_FileInfo* info, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Query( + kApiID, host_resource())); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::TouchValidated(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Touch( + kApiID, host_resource(), last_access_time, last_modified_time)); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::ReadValidated(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Read( + kApiID, host_resource(), offset, bytes_to_read)); + RegisterCallback(OPERATION_READ, callback, buffer, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::WriteValidated(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) { + // TODO(brettw) it would be nice to use a shared memory buffer for large + // writes rather than having to copy to a string (which will involve a number + // of extra copies to serialize over IPC). + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Write( + kApiID, host_resource(), offset, std::string(buffer, bytes_to_write))); + RegisterCallback(OPERATION_WRITE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::SetLengthValidated(int64_t length, + PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_SetLength( + kApiID, host_resource(), length)); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +int32_t FileIO::FlushValidated(PP_CompletionCallback callback) { + GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Flush( + kApiID, host_resource())); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); + return PP_OK_COMPLETIONPENDING; +} + +} // namespace + +// ----------------------------------------------------------------------------- + +PPB_FileIO_Proxy::PPB_FileIO_Proxy(Dispatcher* dispatcher) + : InterfaceProxy(dispatcher), + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { +} + +PPB_FileIO_Proxy::~PPB_FileIO_Proxy() { +} + +// static +PP_Resource PPB_FileIO_Proxy::CreateProxyResource(PP_Instance instance) { + PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); + if (!dispatcher) + return 0; + + HostResource result; + dispatcher->Send(new PpapiHostMsg_PPBFileIO_Create(kApiID, instance, + &result)); + if (result.is_null()) + return 0; + return (new FileIO(result))->GetReference(); +} + +bool PPB_FileIO_Proxy::OnMessageReceived(const IPC::Message& msg) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(PPB_FileIO_Proxy, msg) + // Plugin -> host message. + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Create, OnHostMsgCreate) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Open, OnHostMsgOpen) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Close, OnHostMsgClose) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Query, OnHostMsgQuery) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Touch, OnHostMsgTouch) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Read, OnHostMsgRead) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Write, OnHostMsgWrite) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_SetLength, OnHostMsgSetLength) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Flush, OnHostMsgFlush) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_WillWrite, OnHostMsgWillWrite) + IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_WillSetLength, + OnHostMsgWillSetLength) + + // Host -> plugin messages. + IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_GeneralComplete, + OnPluginMsgGeneralComplete) + IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_OpenFileComplete, + OnPluginMsgOpenFileComplete) + IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_QueryComplete, + OnPluginMsgQueryComplete) + IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_ReadComplete, + OnPluginMsgReadComplete) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void PPB_FileIO_Proxy::OnHostMsgCreate(PP_Instance instance, + HostResource* result) { + thunk::EnterResourceCreation enter(instance); + if (enter.succeeded()) { + result->SetHostResource(instance, + enter.functions()->CreateFileIO(instance)); + } +} + +void PPB_FileIO_Proxy::OnHostMsgOpen(const HostResource& host_resource, + const HostResource& file_ref_resource, + int32_t open_flags) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::OpenFileCallbackCompleteInHost, host_resource); + if (enter.succeeded()) { + enter.SetResult(enter.object()->Open( + file_ref_resource.host_resource(), open_flags, enter.callback())); + } +} + +void PPB_FileIO_Proxy::OnHostMsgClose(const HostResource& host_resource) { + EnterHostFromHostResource<PPB_FileIO_API> enter(host_resource); + if (enter.succeeded()) + enter.object()->Close(); +} + +void PPB_FileIO_Proxy::OnHostMsgQuery(const HostResource& host_resource) { + // The callback will take charge of deleting the FileInfo. The contents must + // be defined so we don't send garbage to the plugin in the failure case. + PP_FileInfo* info = new PP_FileInfo; + memset(info, 0, sizeof(PP_FileInfo)); + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::QueryCallbackCompleteInHost, + host_resource, info); + if (enter.succeeded()) + enter.SetResult(enter.object()->Query(info, enter.callback())); +} + +void PPB_FileIO_Proxy::OnHostMsgTouch(const HostResource& host_resource, + PP_Time last_access_time, + PP_Time last_modified_time) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) { + enter.SetResult(enter.object()->Touch(last_access_time, last_modified_time, + enter.callback())); + } +} + +void PPB_FileIO_Proxy::OnHostMsgRead(const HostResource& host_resource, + int64_t offset, + int32_t bytes_to_read) { + // Validate bytes_to_read before allocating below. This value is coming from + // the untrusted plugin. + bytes_to_read = std::min(bytes_to_read, kMaxReadSize); + if (bytes_to_read < 0) { + ReadCallbackCompleteInHost(PP_ERROR_FAILED, host_resource, + new std::string()); + return; + } + + // The callback will take charge of deleting the string. + std::string* dest = new std::string; + dest->resize(bytes_to_read); + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::ReadCallbackCompleteInHost, + host_resource, dest); + if (enter.succeeded()) { + enter.SetResult(enter.object()->Read(offset, + bytes_to_read > 0 ? &(*dest)[0] : NULL, + bytes_to_read, enter.callback())); + } +} + +void PPB_FileIO_Proxy::OnHostMsgWrite(const HostResource& host_resource, + int64_t offset, + const std::string& data) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) { + enter.SetResult(enter.object()->Write(offset, data.data(), data.size(), + enter.callback())); + } +} + +void PPB_FileIO_Proxy::OnHostMsgSetLength(const HostResource& host_resource, + int64_t length) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) + enter.SetResult(enter.object()->SetLength(length, enter.callback())); +} + +void PPB_FileIO_Proxy::OnHostMsgFlush(const HostResource& host_resource) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) + enter.SetResult(enter.object()->Flush(enter.callback())); +} + +void PPB_FileIO_Proxy::OnHostMsgWillWrite(const HostResource& host_resource, + int64_t offset, + int32_t bytes_to_write) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) { + enter.SetResult(enter.object()->WillWrite(offset, bytes_to_write, + enter.callback())); + } +} + +void PPB_FileIO_Proxy::OnHostMsgWillSetLength(const HostResource& host_resource, + int64_t length) { + EnterHostFileIO enter(host_resource, callback_factory_, + &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost, + host_resource); + if (enter.succeeded()) + enter.SetResult(enter.object()->WillSetLength(length, enter.callback())); +} + +void PPB_FileIO_Proxy::OnPluginMsgGeneralComplete( + const HostResource& host_resource, + int32_t result) { + EnterPluginFileIO enter(host_resource); + if (enter.succeeded()) + static_cast<FileIO*>(enter.object())->ExecuteGeneralCallback(result); +} + +void PPB_FileIO_Proxy::OnPluginMsgOpenFileComplete( + const HostResource& host_resource, + int32_t result) { + EnterPluginFileIO enter(host_resource); + if (enter.succeeded()) + static_cast<FileIO*>(enter.object())->ExecuteOpenFileCallback(result); +} + +void PPB_FileIO_Proxy::OnPluginMsgQueryComplete( + const HostResource& host_resource, + int32_t result, + const PP_FileInfo& info) { + EnterPluginFileIO enter(host_resource); + if (enter.succeeded()) + static_cast<FileIO*>(enter.object())->ExecuteQueryCallback(result, info); +} + +void PPB_FileIO_Proxy::OnPluginMsgReadComplete( + const HostResource& host_resource, + int32_t result, + const std::string& data) { + EnterPluginFileIO enter(host_resource); + if (enter.succeeded()) { + // The result code should contain the data size if it's positive. + DCHECK((result < 0 && data.size() == 0) || + result == static_cast<int32_t>(data.size())); + static_cast<FileIO*>(enter.object())->ExecuteReadCallback(result, + data.data()); + } +} + +void PPB_FileIO_Proxy::GeneralCallbackCompleteInHost( + int32_t pp_error, + const HostResource& host_resource) { + Send(new PpapiMsg_PPBFileIO_GeneralComplete(kApiID, host_resource, pp_error)); +} + +void PPB_FileIO_Proxy::OpenFileCallbackCompleteInHost( + int32_t pp_error, + const HostResource& host_resource) { + Send(new PpapiMsg_PPBFileIO_OpenFileComplete(kApiID, host_resource, + pp_error)); +} + +void PPB_FileIO_Proxy::QueryCallbackCompleteInHost( + int32_t pp_error, + const HostResource& host_resource, + PP_FileInfo* info) { + Send(new PpapiMsg_PPBFileIO_QueryComplete(kApiID, host_resource, pp_error, + *info)); + delete info; +} + +void PPB_FileIO_Proxy::ReadCallbackCompleteInHost( + int32_t pp_error, + const HostResource& host_resource, + std::string* data) { + // Only send the amount of data in the string that was actually read. + if (pp_error >= 0) { + DCHECK(pp_error <= static_cast<int32_t>(data->size())); + data->resize(pp_error); + } + Send(new PpapiMsg_PPBFileIO_ReadComplete(kApiID, host_resource, pp_error, + *data)); + delete data; +} + +} // namespace proxy +} // namespace ppapi diff --git a/ppapi/proxy/ppb_file_io_proxy.h b/ppapi/proxy/ppb_file_io_proxy.h new file mode 100644 index 0000000..fbc8a16 --- /dev/null +++ b/ppapi/proxy/ppb_file_io_proxy.h @@ -0,0 +1,93 @@ +// Copyright (c) 2011 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 PPAPI_PROXY_PPB_FILE_IO_PROXY_H_ +#define PPAPI_PROXY_PPB_FILE_IO_PROXY_H_ + +#include <string> + +#include "base/basictypes.h" +#include "ppapi/c/pp_file_info.h" +#include "ppapi/proxy/interface_proxy.h" +#include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" + +namespace ppapi { + +class HostResource; +namespace proxy { + +class PPB_FileIO_Proxy : public InterfaceProxy { + public: + explicit PPB_FileIO_Proxy(Dispatcher* dispatcher); + virtual ~PPB_FileIO_Proxy(); + + static PP_Resource CreateProxyResource(PP_Instance instance); + + // InterfaceProxy implementation. + virtual bool OnMessageReceived(const IPC::Message& msg); + + static const ApiID kApiID = API_ID_PPB_FILE_IO; + + private: + // Plugin -> Host message handlers. + void OnHostMsgCreate(PP_Instance instance, HostResource* result); + void OnHostMsgOpen(const HostResource& host_resource, + const HostResource& file_ref_resource, + int32_t open_flags); + void OnHostMsgClose(const HostResource& host_resource); + void OnHostMsgQuery(const HostResource& host_resource); + void OnHostMsgTouch(const HostResource& host_resource, + PP_Time last_access_time, + PP_Time last_modified_time); + void OnHostMsgRead(const HostResource& host_resource, + int64_t offset, + int32_t bytes_to_read); + void OnHostMsgWrite(const HostResource& host_resource, + int64_t offset, + const std::string& data); + void OnHostMsgSetLength(const HostResource& host_resource, + int64_t length); + void OnHostMsgFlush(const HostResource& host_resource); + void OnHostMsgWillWrite(const HostResource& host_resource, + int64_t offset, + int32_t bytes_to_write); + void OnHostMsgWillSetLength(const HostResource& host_resource, + int64_t length); + + // Host -> Plugin message handlers. + void OnPluginMsgGeneralComplete(const HostResource& host_resource, + int32_t result); + void OnPluginMsgOpenFileComplete(const HostResource& host_resource, + int32_t result); + void OnPluginMsgQueryComplete(const HostResource& host_resource, + int32_t result, + const PP_FileInfo& info); + void OnPluginMsgReadComplete(const HostResource& host_resource, + int32_t result, + const std::string& data); + void OnPluginMsgWriteComplete(const HostResource& host_resource, + int32_t result); + + // Host-side callback handlers. These convert the callbacks to an IPC message + // to the plugin. + void GeneralCallbackCompleteInHost(int32_t pp_error, + const HostResource& host_resource); + void OpenFileCallbackCompleteInHost(int32_t pp_error, + const HostResource& host_resource); + void QueryCallbackCompleteInHost(int32_t pp_error, + const HostResource& host_resource, + PP_FileInfo* info); + void ReadCallbackCompleteInHost(int32_t pp_error, + const HostResource& host_resource, + std::string* data); + pp::CompletionCallbackFactory<PPB_FileIO_Proxy, + ProxyNonThreadSafeRefCount> callback_factory_; + + DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Proxy); +}; + +} // namespace proxy +} // namespace ppapi + +#endif // PPAPI_PROXY_PPB_FILE_IO_PROXY_H_ diff --git a/ppapi/proxy/ppb_file_ref_proxy.cc b/ppapi/proxy/ppb_file_ref_proxy.cc index 20b522c..e481f86 100644 --- a/ppapi/proxy/ppb_file_ref_proxy.cc +++ b/ppapi/proxy/ppb_file_ref_proxy.cc @@ -33,6 +33,9 @@ class FileRef : public PPB_FileRef_Shared { explicit FileRef(const PPB_FileRef_CreateInfo& info); virtual ~FileRef(); + // Resource overrides. + virtual void LastPluginRefWasDeleted() OVERRIDE; + // PPB_FileRef_API implementation (not provided by PPB_FileRef_Shared). virtual PP_Resource GetParent() OVERRIDE; virtual int32_t MakeDirectory(PP_Bool make_ancestors, @@ -77,6 +80,11 @@ FileRef::FileRef(const PPB_FileRef_CreateInfo& info) } FileRef::~FileRef() { + // The callbacks map should have been cleared by LastPluginRefWasDeleted. + DCHECK(pending_callbacks_.empty()); +} + +void FileRef::LastPluginRefWasDeleted() { // Abort all pending callbacks. Do this by posting a task to avoid reentering // the plugin's Release() call that probably deleted this object. for (PendingCallbackMap::iterator i = pending_callbacks_.begin(); @@ -85,6 +93,7 @@ FileRef::~FileRef() { i->second.func, i->second.user_data, static_cast<int32_t>(PP_ERROR_ABORTED))); } + pending_callbacks_.clear(); } PP_Resource FileRef::GetParent() { diff --git a/ppapi/proxy/ppb_file_ref_proxy.h b/ppapi/proxy/ppb_file_ref_proxy.h index 5f0f947..c0fa23c 100644 --- a/ppapi/proxy/ppb_file_ref_proxy.h +++ b/ppapi/proxy/ppb_file_ref_proxy.h @@ -31,6 +31,8 @@ class PPB_FileRef_Proxy : public InterfaceProxy { static PP_Resource CreateProxyResource(PP_Resource file_system, const char* path); + static PP_Resource CreateProxyResource( + const PPB_FileRef_CreateInfo& serialized); // InterfaceProxy implementation. virtual bool OnMessageReceived(const IPC::Message& msg); @@ -60,7 +62,7 @@ class PPB_FileRef_Proxy : public InterfaceProxy { static const ApiID kApiID = API_ID_PPB_FILE_REF; private: - // Message handlers. + // Plugin -> host message handlers. void OnMsgCreate(const HostResource& file_system, const std::string& path, PPB_FileRef_CreateInfo* result); diff --git a/ppapi/proxy/resource_creation_proxy.cc b/ppapi/proxy/resource_creation_proxy.cc index 52cce18..e758035 100644 --- a/ppapi/proxy/resource_creation_proxy.cc +++ b/ppapi/proxy/resource_creation_proxy.cc @@ -15,6 +15,7 @@ #include "ppapi/proxy/ppb_buffer_proxy.h" #include "ppapi/proxy/ppb_broker_proxy.h" #include "ppapi/proxy/ppb_file_chooser_proxy.h" +#include "ppapi/proxy/ppb_file_io_proxy.h" #include "ppapi/proxy/ppb_file_ref_proxy.h" #include "ppapi/proxy/ppb_file_system_proxy.h" #include "ppapi/proxy/ppb_flash_menu_proxy.h" @@ -122,8 +123,7 @@ PP_Resource ResourceCreationProxy::CreateFileChooser( } PP_Resource ResourceCreationProxy::CreateFileIO(PP_Instance instance) { - NOTIMPLEMENTED(); // Not proxied yet. - return 0; + return PPB_FileIO_Proxy::CreateProxyResource(instance); } PP_Resource ResourceCreationProxy::CreateFileRef(PP_Resource file_system, diff --git a/ppapi/shared_impl/api_id.h b/ppapi/shared_impl/api_id.h index c8c9362..73654b0 100644 --- a/ppapi/shared_impl/api_id.h +++ b/ppapi/shared_impl/api_id.h @@ -21,6 +21,7 @@ enum ApiID { API_ID_PPB_CORE, API_ID_PPB_CURSORCONTROL, API_ID_PPB_FILE_CHOOSER, + API_ID_PPB_FILE_IO, API_ID_PPB_FILE_REF, API_ID_PPB_FILE_SYSTEM, API_ID_PPB_FLASH, diff --git a/webkit/plugins/ppapi/file_type_conversions.cc b/ppapi/shared_impl/file_type_conversion.cc index 42f1aa1..a9bd435 100644 --- a/webkit/plugins/ppapi/file_type_conversions.cc +++ b/ppapi/shared_impl/file_type_conversion.cc @@ -2,12 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "webkit/plugins/ppapi/file_type_conversions.h" +#include "ppapi/shared_impl/file_type_conversion.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/ppb_file_io.h" -namespace webkit { namespace ppapi { int PlatformFileErrorToPepperError(base::PlatformFileError error_code) { @@ -73,4 +72,3 @@ bool PepperFileOpenFlagsToPlatformFileFlags(int32_t pp_open_flags, } } // namespace ppapi -} // namespace webkit diff --git a/webkit/plugins/ppapi/file_type_conversions.h b/ppapi/shared_impl/file_type_conversion.h index 708a94b..eb6cb56 100644 --- a/webkit/plugins/ppapi/file_type_conversions.h +++ b/ppapi/shared_impl/file_type_conversion.h @@ -2,24 +2,25 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef WEBKIT_PLUGINS_PPAPI_FILE_TYPE_CONVERSIONS_H_ -#define WEBKIT_PLUGINS_PPAPI_FILE_TYPE_CONVERSIONS_H_ +#ifndef PPAPI_SHARED_IMPL_FILE_TYPE_CONVERSION_H_ +#define PPAPI_SHARED_IMPL_FILE_TYPE_CONVERSION_H_ #include "base/platform_file.h" #include "ppapi/c/pp_stdint.h" +#include "ppapi/shared_impl/ppapi_shared_export.h" -namespace webkit { namespace ppapi { -int PlatformFileErrorToPepperError(base::PlatformFileError error_code); +PPAPI_SHARED_EXPORT int PlatformFileErrorToPepperError( + base::PlatformFileError error_code); // Converts a PP_FileOpenFlags_Dev flag combination into a corresponding // PlatformFileFlags flag combination. // Returns |true| if okay. -bool PepperFileOpenFlagsToPlatformFileFlags(int32_t pp_open_flags, - int* flags_out); +PPAPI_SHARED_EXPORT bool PepperFileOpenFlagsToPlatformFileFlags( + int32_t pp_open_flags, + int* flags_out); } // namespace ppapi -} // namespace webkit -#endif // WEBKIT_PLUGINS_PPAPI_FILE_TYPE_CONVERSIONS_H_ +#endif // PPAPI_SHARED_IMPL_FILE_TYPE_CONVERSION_H_ diff --git a/ppapi/shared_impl/ppb_file_io_shared.cc b/ppapi/shared_impl/ppb_file_io_shared.cc new file mode 100644 index 0000000..a51c33f --- /dev/null +++ b/ppapi/shared_impl/ppb_file_io_shared.cc @@ -0,0 +1,237 @@ +// Copyright (c) 2011 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. + +#include "ppapi/shared_impl/ppb_file_io_shared.h" + +#include <string.h> + +#include "base/bind.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/shared_impl/file_type_conversion.h" +#include "ppapi/shared_impl/time_conversion.h" +#include "ppapi/thunk/enter.h" +#include "ppapi/thunk/ppb_file_ref_api.h" + +namespace ppapi { + +using thunk::EnterResourceNoLock; +using thunk::PPB_FileIO_API; +using thunk::PPB_FileRef_API; + +PPB_FileIO_Shared::CallbackEntry::CallbackEntry() + : read_buffer(NULL), + info(NULL) { +} + +PPB_FileIO_Shared::CallbackEntry::CallbackEntry(const CallbackEntry& entry) + : callback(entry.callback), + read_buffer(entry.read_buffer), + info(entry.info) { +} + +PPB_FileIO_Shared::CallbackEntry::~CallbackEntry() { +} + +PPB_FileIO_Shared::PPB_FileIO_Shared(PP_Instance instance) + : Resource(instance), + file_system_type_(PP_FILESYSTEMTYPE_INVALID), + file_open_(false), + pending_op_(OPERATION_NONE) { +} + +PPB_FileIO_Shared::PPB_FileIO_Shared(const HostResource& host_resource) + : Resource(host_resource), + file_system_type_(PP_FILESYSTEMTYPE_INVALID), + file_open_(false), + pending_op_(OPERATION_NONE) { +} + +PPB_FileIO_Shared::~PPB_FileIO_Shared() { + // The callbacks list should have been cleared by LastPluginRefWasDeleted. + DCHECK(callbacks_.empty()); +} + +void PPB_FileIO_Shared::LastPluginRefWasDeleted() { + // Abort all pending callbacks. Do this by posting a task to avoid reentering + // the plugin's Release() call that probably deleted this object. + for (size_t i = 0; i < callbacks_.size(); i++) { + MessageLoop::current()->PostTask(FROM_HERE, base::Bind( + callbacks_[i].callback.func, callbacks_[i].callback.user_data, + static_cast<int32_t>(PP_ERROR_ABORTED))); + } + callbacks_.erase(callbacks_.begin(), callbacks_.end()); +} + +thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { + return this; +} + +int32_t PPB_FileIO_Shared::Open(PP_Resource file_ref, + int32_t open_flags, + PP_CompletionCallback callback) { + EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); + if (enter.failed()) + return PP_ERROR_BADRESOURCE; + + int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE, callback); + if (rv != PP_OK) + return rv; + + PP_FileSystemType type = enter.object()->GetFileSystemType(); + if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && + type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && + type != PP_FILESYSTEMTYPE_EXTERNAL) + return PP_ERROR_FAILED; + file_system_type_ = type; + + return OpenValidated(file_ref, enter.object(), open_flags, callback); +} + +int32_t PPB_FileIO_Shared::Query(PP_FileInfo* info, + PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); + if (rv != PP_OK) + return rv; + if (!info) + return PP_ERROR_BADARGUMENT; + return QueryValidated(info, callback); +} + +int32_t PPB_FileIO_Shared::Touch(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); + if (rv != PP_OK) + return rv; + return TouchValidated(last_access_time, last_modified_time, callback); +} + +int32_t PPB_FileIO_Shared::Read(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); + if (rv != PP_OK) + return rv; + return ReadValidated(offset, buffer, bytes_to_read, callback); +} + +int32_t PPB_FileIO_Shared::Write(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); + if (rv != PP_OK) + return rv; + return WriteValidated(offset, buffer, bytes_to_write, callback); +} + +int32_t PPB_FileIO_Shared::SetLength(int64_t length, + PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); + if (rv != PP_OK) + return rv; + return SetLengthValidated(length, callback); +} + +int32_t PPB_FileIO_Shared::Flush(PP_CompletionCallback callback) { + int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); + if (rv != PP_OK) + return rv; + return FlushValidated(callback); +} + +void PPB_FileIO_Shared::ExecuteGeneralCallback(int32_t pp_error) { + RunAndRemoveFirstPendingCallback(pp_error); +} + +void PPB_FileIO_Shared::ExecuteOpenFileCallback(int32_t pp_error) { + if (pp_error == PP_OK) + file_open_ = true; + ExecuteGeneralCallback(pp_error); +} + +void PPB_FileIO_Shared::ExecuteQueryCallback(int32_t pp_error, + const PP_FileInfo& info) { + if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty() || + !callbacks_.front().info) { + NOTREACHED(); + return; + } + *callbacks_.front().info = info; + RunAndRemoveFirstPendingCallback(pp_error); +} + +void PPB_FileIO_Shared::ExecuteReadCallback(int32_t pp_error, + const char* data) { + if (pending_op_ != OPERATION_READ || callbacks_.empty()) { + NOTREACHED(); + return; + } + + char* read_buffer = callbacks_.front().read_buffer; + DCHECK(data); + DCHECK(read_buffer); + + // The result code contains the number of bytes if it's positive. + if (pp_error > 0) + memcpy(read_buffer, data, pp_error); + RunAndRemoveFirstPendingCallback(pp_error); +} + +int32_t PPB_FileIO_Shared::CommonCallValidation( + bool should_be_open, + OperationType new_op, + PP_CompletionCallback callback) { + // Only asynchronous operation is supported. + if (!callback.func) + return PP_ERROR_BLOCKS_MAIN_THREAD; + + if (should_be_open) { + if (!file_open_) + return PP_ERROR_FAILED; + } else { + if (file_open_) + return PP_ERROR_FAILED; + } + + if (pending_op_ != OPERATION_NONE && + (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { + return PP_ERROR_INPROGRESS; + } + + return PP_OK; +} + +void PPB_FileIO_Shared::RegisterCallback(OperationType op, + PP_CompletionCallback callback, + char* read_buffer, + PP_FileInfo* info) { + DCHECK(callback.func); + DCHECK(pending_op_ == OPERATION_NONE || + (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); + + CallbackEntry entry; + entry.callback = callback; + entry.read_buffer = read_buffer; + entry.info = info; + callbacks_.push_back(entry); + + pending_op_ = op; +} + +void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { + DCHECK(!callbacks_.empty()); + + CallbackEntry front = callbacks_.front(); + callbacks_.pop_front(); + if (callbacks_.empty()) + pending_op_ = OPERATION_NONE; + + PP_RunCompletionCallback(&front.callback, result); +} + +} // namespace ppapi diff --git a/ppapi/shared_impl/ppb_file_io_shared.h b/ppapi/shared_impl/ppb_file_io_shared.h new file mode 100644 index 0000000..341db46 --- /dev/null +++ b/ppapi/shared_impl/ppb_file_io_shared.h @@ -0,0 +1,157 @@ +// Copyright (c) 2011 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 PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ +#define PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ + +#include <deque> + +#include "base/compiler_specific.h" +#include "ppapi/shared_impl/ppapi_shared_export.h" +#include "ppapi/shared_impl/resource.h" +#include "ppapi/thunk/ppb_file_io_api.h" + +namespace ppapi { + +namespace thunk { +class PPB_FileRef_API; +} + +class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, + public thunk::PPB_FileIO_API { + public: + PPB_FileIO_Shared(PP_Instance instance); + PPB_FileIO_Shared(const HostResource& host_resource); + ~PPB_FileIO_Shared(); + + // Resource overrides. + virtual void LastPluginRefWasDeleted() OVERRIDE; + virtual thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; + + // PPB_FileIO_API implementation. + virtual int32_t Open(PP_Resource file_ref, + int32_t open_flags, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t Query(PP_FileInfo* info, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t Touch(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t Read(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t Write(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t SetLength(int64_t length, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE; + + // Callback handler for different types of operations. + void ExecuteGeneralCallback(int32_t pp_error); + void ExecuteOpenFileCallback(int32_t pp_error); + void ExecuteQueryCallback(int32_t pp_error, const PP_FileInfo& info); + void ExecuteReadCallback(int32_t pp_error, const char* data); + + protected: + struct CallbackEntry { + CallbackEntry(); + CallbackEntry(const CallbackEntry& entry); + ~CallbackEntry(); + + PP_CompletionCallback callback; + + // Pointer back to the caller's read buffer; only used by |Read()|, NULL + // for non-read operations. Not owned. + char* read_buffer; + + // Pointer back to the caller's PP_FileInfo structure for Query operations. + // NULL for non-query operations. Not owned. + PP_FileInfo* info; + }; + + enum OperationType { + // There is no pending operation right now. + OPERATION_NONE, + + // If there are pending reads, any other kind of async operation is not + // allowed. + OPERATION_READ, + + // If there are pending writes, any other kind of async operation is not + // allowed. + OPERATION_WRITE, + + // If there is a pending operation that is neither read nor write, no + // further async operation is allowed. + OPERATION_EXCLUSIVE + }; + + // Validated versions of the FileIO API. Subclasses in the proxy and impl + // implement these so the common error checking stays here. + virtual int32_t OpenValidated(PP_Resource file_ref_resource, + thunk::PPB_FileRef_API* file_ref_api, + int32_t open_flags, + PP_CompletionCallback callback) = 0; + virtual int32_t QueryValidated(PP_FileInfo* info, + PP_CompletionCallback callback) = 0; + virtual int32_t TouchValidated(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) = 0; + virtual int32_t ReadValidated(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) = 0; + virtual int32_t WriteValidated(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) = 0; + virtual int32_t SetLengthValidated(int64_t length, + PP_CompletionCallback callback) = 0; + virtual int32_t FlushValidated(PP_CompletionCallback callback) = 0; + + // Called for every "Validated" function. + // + // This verifies that the callback is valid and that no callback is already + // pending, or it is a read(write) request and currently the pending + // operations are reads(writes). + // + // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if + // the call should be aborted and that code returned to the plugin. + int32_t CommonCallValidation(bool should_be_open, + OperationType new_op, + PP_CompletionCallback callback); + + // Sets up a pending callback. This should only be called once it is certain + // that |PP_OK_COMPLETIONPENDING| will be returned. + // + // |read_buffer| is only used by read operations, |info| is used only by + // query operations. + void RegisterCallback(OperationType op, + PP_CompletionCallback callback, + char* read_buffer, + PP_FileInfo* info); + + // Pops the oldest callback from the queue and runs it. + void RunAndRemoveFirstPendingCallback(int32_t result); + + // The file system type specified in the Open() call. This will be + // PP_FILESYSTEMTYPE_INVALID before open was called. This value does not + // indicate that the open command actually succeeded. + PP_FileSystemType file_system_type_; + + // Set to true when the file has been successfully opened. + bool file_open_; + + std::deque<CallbackEntry> callbacks_; + OperationType pending_op_; + + DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Shared); +}; + +} // namespace ppapi + +#endif // PPAPI_SHARED_IMPL_PPB_FILE_IO_SHARED_H_ diff --git a/ppapi/tests/test_file_io.cc b/ppapi/tests/test_file_io.cc index b04c7e9..057fdef 100644 --- a/ppapi/tests/test_file_io.cc +++ b/ppapi/tests/test_file_io.cc @@ -903,6 +903,31 @@ std::string TestFileIO::TestNotAllowMixedReadWrite() { if (rv_2 != PP_ERROR_INPROGRESS) return ReportError("FileIO::Read", rv_2); + // Cannot query while the write is pending. + TestCompletionCallback callback_3(instance_->pp_instance(), force_async_); + PP_FileInfo info; + int32_t rv_3 = file_io.Query(&info, callback_3); + if (rv_3 == PP_OK_COMPLETIONPENDING) + rv_3 = callback_3.WaitForResult(); + if (rv_3 != PP_ERROR_INPROGRESS) + return ReportError("FileIO::Query", rv_3); + + // Cannot touch while the write is pending. + TestCompletionCallback callback_4(instance_->pp_instance(), force_async_); + int32_t rv_4 = file_io.Touch(1234.0, 5678.0, callback_4); + if (rv_4 == PP_OK_COMPLETIONPENDING) + rv_4 = callback_4.WaitForResult(); + if (rv_4 != PP_ERROR_INPROGRESS) + return ReportError("FileIO::Touch", rv_4); + + // Cannot set length while the write is pending. + TestCompletionCallback callback_5(instance_->pp_instance(), force_async_); + int32_t rv_5 = file_io.SetLength(123, callback_5); + if (rv_5 == PP_OK_COMPLETIONPENDING) + rv_5 = callback_5.WaitForResult(); + if (rv_5 != PP_ERROR_INPROGRESS) + return ReportError("FileIO::SetLength", rv_5); + callback_1.WaitForResult(); PASS(); @@ -940,10 +965,14 @@ std::string TestFileIO::TestWillWriteWillSetLength() { if (!trusted) return ReportError("FileIOTrusted", PP_ERROR_FAILED); - // Get file descriptor. - int32_t fd = trusted->GetOSFileDescriptor(file_io.pp_resource()); - if (fd < 0) - return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; + // Get file descriptor. This is only supported in-process for now, so don't + // test out of process. + const PPB_Testing_Dev* testing_interface = GetTestingInterface(); + if (testing_interface && !testing_interface->IsOutOfProcess()) { + int32_t fd = trusted->GetOSFileDescriptor(file_io.pp_resource()); + if (fd < 0) + return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; + } // Calling WillWrite. rv = trusted->WillWrite( diff --git a/ppapi/tests/test_url_loader.cc b/ppapi/tests/test_url_loader.cc index f55694c..f379c31 100644 --- a/ppapi/tests/test_url_loader.cc +++ b/ppapi/tests/test_url_loader.cc @@ -70,22 +70,14 @@ bool TestURLLoader::Init() { const PPB_FileIO* file_io_interface = static_cast<const PPB_FileIO*>( pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_INTERFACE)); - if (testing_interface_->IsOutOfProcess() && file_io_interface) { - instance_->AppendError( - "FileIO interface is now supported by ppapi proxy: update this test!"); - } else if (!testing_interface_->IsOutOfProcess() && !file_io_interface) { + if (!file_io_interface) instance_->AppendError("FileIO interface not available"); - } file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted*>( pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>( pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); - if (testing_interface_->IsOutOfProcess() && file_io_trusted_interface_) { - instance_->AppendError( - "FileIOTrusted interface is now supported by ppapi proxy: " - "update this test!"); - } else if (!testing_interface_->IsOutOfProcess()) { + if (!testing_interface_->IsOutOfProcess()) { // Trusted interfaces are not supported under NaCl. #if !(defined __native_client__) if (!file_io_trusted_interface_) @@ -480,36 +472,34 @@ std::string TestURLLoader::TestStreamToFile() { return ReportError("URLLoader::FinishStreamingToFile", rv); // FileIO is not yet supported by ppapi/proxy. - if (!testing_interface_->IsOutOfProcess()) { - pp::FileIO reader(instance_); - rv = reader.Open(body, PP_FILEOPENFLAG_READ, callback); - if (force_async_ && rv != PP_OK_COMPLETIONPENDING) - return ReportError("FileIO::Open force_async", rv); - if (rv == PP_OK_COMPLETIONPENDING) - rv = callback.WaitForResult(); - if (rv != PP_OK) - return ReportError("FileIO::Open", rv); + pp::FileIO reader(instance_); + rv = reader.Open(body, PP_FILEOPENFLAG_READ, callback); + if (force_async_ && rv != PP_OK_COMPLETIONPENDING) + return ReportError("FileIO::Open force_async", rv); + if (rv == PP_OK_COMPLETIONPENDING) + rv = callback.WaitForResult(); + if (rv != PP_OK) + return ReportError("FileIO::Open", rv); - std::string data; - std::string error = ReadEntireFile(&reader, &data); - if (!error.empty()) - return error; + std::string data; + std::string error = ReadEntireFile(&reader, &data); + if (!error.empty()) + return error; - std::string expected_body = "hello\n"; - if (data.size() != expected_body.size()) - return "ReadEntireFile returned unexpected content length"; - if (data != expected_body) - return "ReadEntireFile returned unexpected content"; + std::string expected_body = "hello\n"; + if (data.size() != expected_body.size()) + return "ReadEntireFile returned unexpected content length"; + if (data != expected_body) + return "ReadEntireFile returned unexpected content"; - // FileIOTrusted is not supported by NaCl or ppapi/proxy. - if (!testing_interface_->IsOutOfProcess()) { + // FileIOTrusted is not supported by NaCl or ppapi/proxy. + if (!testing_interface_->IsOutOfProcess()) { #if !(defined __native_client__) - int32_t file_descriptor = file_io_trusted_interface_->GetOSFileDescriptor( - reader.pp_resource()); - if (file_descriptor < 0) - return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; + int32_t file_descriptor = file_io_trusted_interface_->GetOSFileDescriptor( + reader.pp_resource()); + if (file_descriptor < 0) + return "FileIO::GetOSFileDescriptor() returned a bad file descriptor."; #endif - } } PASS(); } diff --git a/ppapi/thunk/interfaces_ppb_private.h b/ppapi/thunk/interfaces_ppb_private.h index d7c8b69..c6770f8 100644 --- a/ppapi/thunk/interfaces_ppb_private.h +++ b/ppapi/thunk/interfaces_ppb_private.h @@ -13,6 +13,8 @@ PROXIED_API(PPB_UDPSocket_Private) PROXIED_IFACE(PPB_Broker, PPB_BROKER_TRUSTED_INTERFACE_0_2, PPB_BrokerTrusted) PROXIED_IFACE(PPB_FileRef, PPB_FILEREFPRIVATE_INTERFACE_0_1, PPB_FileRefPrivate) +// This uses the FileIO API which is declared in the public stable file. +PROXIED_IFACE(PPB_FileIO, PPB_FILEIOTRUSTED_INTERFACE_0_4, PPB_FileIOTrusted) PROXIED_IFACE(PPB_Instance, PPB_FLASHFULLSCREEN_INTERFACE, PPB_FlashFullscreen) PROXIED_IFACE(NoAPIName, PPB_NETADDRESS_PRIVATE_INTERFACE, PPB_NetAddress_Private) diff --git a/ppapi/thunk/interfaces_ppb_public_stable.h b/ppapi/thunk/interfaces_ppb_public_stable.h index b81f744..f4d5ec3 100644 --- a/ppapi/thunk/interfaces_ppb_public_stable.h +++ b/ppapi/thunk/interfaces_ppb_public_stable.h @@ -22,7 +22,7 @@ PROXIED_API(PPB_Audio) // serialize it to a struct when we need it on the host side. UNPROXIED_API(PPB_AudioConfig) PROXIED_API(PPB_Core) -UNPROXIED_API(PPB_FileIO) +PROXIED_API(PPB_FileIO) PROXIED_API(PPB_FileRef) PROXIED_API(PPB_FileSystem) PROXIED_API(PPB_Graphics2D) @@ -50,7 +50,7 @@ PROXIED_IFACE(PPB_Audio, PPB_AUDIO_INTERFACE_1_0, PPB_Audio) // This has no corresponding _Proxy object since it does no IPC. PROXIED_IFACE(NoAPIName, PPB_AUDIO_CONFIG_INTERFACE_1_0, PPB_AudioConfig) // Note: Core is special and is registered manually. -UNPROXIED_IFACE(PPB_FileIO, PPB_FILEIO_INTERFACE_1_0, PPB_FileIO) +PROXIED_IFACE(PPB_FileIO, PPB_FILEIO_INTERFACE_1_0, PPB_FileIO) PROXIED_IFACE(PPB_FileRef, PPB_FILEREF_INTERFACE_1_0, PPB_FileRef) PROXIED_IFACE(PPB_FileSystem, PPB_FILESYSTEM_INTERFACE_1_0, PPB_FileSystem) PROXIED_IFACE(PPB_Graphics2D, PPB_GRAPHICS_2D_INTERFACE_1_0, PPB_Graphics2D) diff --git a/ppapi/thunk/thunk.h b/ppapi/thunk/thunk.h index d59c907..00773f3 100644 --- a/ppapi/thunk/thunk.h +++ b/ppapi/thunk/thunk.h @@ -32,7 +32,6 @@ struct PPB_AudioTrusted; struct PPB_BrokerTrusted; struct PPB_BufferTrusted; struct PPB_FileChooserTrusted; -struct PPB_FileIOTrusted; struct PPB_Flash_Clipboard; struct PPB_Flash_Menu; struct PPB_Flash_NetConnector; @@ -59,7 +58,6 @@ PPAPI_THUNK_EXPORT const PPB_BrokerTrusted* GetPPB_Broker_Thunk(); PPAPI_THUNK_EXPORT const PPB_BufferTrusted* GetPPB_BufferTrusted_Thunk(); PPAPI_THUNK_EXPORT const PPB_FileChooserTrusted* GetPPB_FileChooser_Trusted_Thunk(); -PPAPI_THUNK_EXPORT const PPB_FileIOTrusted* GetPPB_FileIOTrusted_Thunk(); PPAPI_THUNK_EXPORT const PPB_Flash_Clipboard* GetPPB_Flash_Clipboard_Thunk(); PPAPI_THUNK_EXPORT const PPB_Flash_Menu* GetPPB_Flash_Menu_Thunk(); PPAPI_THUNK_EXPORT const PPB_Flash_NetConnector* diff --git a/webkit/glue/webkit_glue.gypi b/webkit/glue/webkit_glue.gypi index d2a97e0..2c049b8 100644 --- a/webkit/glue/webkit_glue.gypi +++ b/webkit/glue/webkit_glue.gypi @@ -216,8 +216,6 @@ '../plugins/ppapi/file_callbacks.h', '../plugins/ppapi/file_path.cc', '../plugins/ppapi/file_path.h', - '../plugins/ppapi/file_type_conversions.cc', - '../plugins/ppapi/file_type_conversions.h', '../plugins/ppapi/fullscreen_container.h', '../plugins/ppapi/host_globals.cc', '../plugins/ppapi/host_globals.h', diff --git a/webkit/plugins/ppapi/file_callbacks.cc b/webkit/plugins/ppapi/file_callbacks.cc index 3cb1c08..ce84c41 100644 --- a/webkit/plugins/ppapi/file_callbacks.cc +++ b/webkit/plugins/ppapi/file_callbacks.cc @@ -8,10 +8,10 @@ #include "ppapi/c/pp_file_info.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/ppb_file_system.h" +#include "ppapi/shared_impl/file_type_conversion.h" #include "ppapi/shared_impl/time_conversion.h" #include "webkit/fileapi/file_system_types.h" #include "webkit/plugins/ppapi/callbacks.h" -#include "webkit/plugins/ppapi/file_type_conversions.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" #include "webkit/plugins/ppapi/ppb_file_system_impl.h" @@ -101,7 +101,7 @@ void FileCallbacks::RunCallback(base::PlatformFileError error_code) { if (callback_->completed()) return; - callback_->Run(PlatformFileErrorToPepperError(error_code)); + callback_->Run(::ppapi::PlatformFileErrorToPepperError(error_code)); } } // namespace ppapi diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index aaee85f..1b723a6 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -284,8 +284,6 @@ const void* GetInterface(const char* name) { return ::ppapi::thunk::GetPPB_BufferTrusted_Thunk(); if (strcmp(name, PPB_CORE_INTERFACE) == 0) return &core_interface; - if (strcmp(name, PPB_FILEIOTRUSTED_INTERFACE) == 0) - return ::ppapi::thunk::GetPPB_FileIOTrusted_Thunk(); if (strcmp(name, PPB_FILECHOOSER_TRUSTED_INTERFACE) == 0) return ::ppapi::thunk::GetPPB_FileChooser_Trusted_Thunk(); if (strcmp(name, PPB_FLASH_INTERFACE) == 0) diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.cc b/webkit/plugins/ppapi/ppb_file_io_impl.cc index 351d232..7fa69bc 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_io_impl.cc @@ -16,11 +16,11 @@ #include "ppapi/c/trusted/ppb_file_io_trusted.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/pp_errors.h" +#include "ppapi/shared_impl/file_type_conversion.h" #include "ppapi/shared_impl/time_conversion.h" #include "ppapi/thunk/enter.h" #include "ppapi/thunk/ppb_file_ref_api.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/file_type_conversions.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" @@ -29,31 +29,14 @@ using ppapi::PPTimeToTime; using ppapi::TimeToPPTime; -using ppapi::thunk::EnterResourceNoLock; -using ppapi::thunk::PPB_FileIO_API; using ppapi::thunk::PPB_FileRef_API; namespace webkit { namespace ppapi { -PPB_FileIO_Impl::CallbackEntry::CallbackEntry() - : read_buffer(NULL) { -} - -PPB_FileIO_Impl::CallbackEntry::CallbackEntry(const CallbackEntry& entry) - : callback(entry.callback), - read_buffer(entry.read_buffer) { -} - -PPB_FileIO_Impl::CallbackEntry::~CallbackEntry() { -} - PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance) - : Resource(instance), + : ::ppapi::PPB_FileIO_Shared(instance), file_(base::kInvalidPlatformFileValue), - file_system_type_(PP_FILESYSTEMTYPE_INVALID), - pending_op_(OPERATION_NONE), - info_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { } @@ -61,41 +44,25 @@ PPB_FileIO_Impl::~PPB_FileIO_Impl() { Close(); } -PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { - return this; -} - -int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, - int32_t open_flags, - PP_CompletionCallback callback) { - EnterResourceNoLock<PPB_FileRef_API> enter(pp_file_ref, true); - if (enter.failed()) - return PP_ERROR_BADRESOURCE; - PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object()); - - int32_t rv = CommonCallValidation(false, OPERATION_EXCLUSIVE, callback); - if (rv != PP_OK) - return rv; +int32_t PPB_FileIO_Impl::OpenValidated(PP_Resource file_ref_resource, + PPB_FileRef_API* file_ref_api, + int32_t open_flags, + PP_CompletionCallback callback) { + PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api); int flags = 0; - if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) + if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) return PP_ERROR_BADARGUMENT; - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) - return false; - - file_system_type_ = file_ref->GetFileSystemType(); - if (file_system_type_ != PP_FILESYSTEMTYPE_LOCALPERSISTENT && - file_system_type_ != PP_FILESYSTEMTYPE_LOCALTEMPORARY && - file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) - return PP_ERROR_FAILED; + return PP_ERROR_BADARGUMENT; if (file_ref->HasValidFileSystem()) { file_system_url_ = file_ref->GetFileSystemURL(); if (!plugin_delegate->AsyncOpenFileSystemURL( file_system_url_, flags, - base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } else { @@ -103,49 +70,35 @@ int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, return PP_ERROR_FAILED; if (!plugin_delegate->AsyncOpenFile( file_ref->GetSystemPath(), flags, - base::Bind(&PPB_FileIO_Impl::AsyncOpenFileCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::Query(PP_FileInfo* info, - PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); - if (rv != PP_OK) - return rv; - - if (!info) - return PP_ERROR_BADARGUMENT; - - DCHECK(!info_); // If |info_|, a callback should be pending (caught above). - info_ = info; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::QueryValidated(PP_FileInfo* info, + PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, - base::Bind(&PPB_FileIO_Impl::QueryInfoCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformQueryCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, - PP_Time last_modified_time, - PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); - if (rv != PP_OK) - return rv; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::TouchValidated(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; @@ -153,117 +106,101 @@ int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, plugin_delegate->GetFileThreadMessageLoopProxy(), file_, PPTimeToTime(last_access_time), PPTimeToTime(last_modified_time), - base::Bind(&PPB_FileIO_Impl::StatusCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::Read(int64_t offset, - char* buffer, - int32_t bytes_to_read, - PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_READ, callback); - if (rv != PP_OK) - return rv; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::ReadValidated(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; if (!base::FileUtilProxy::Read( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, bytes_to_read, - base::Bind(&PPB_FileIO_Impl::ReadCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformReadCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_READ, callback, buffer); + RegisterCallback(OPERATION_READ, callback, buffer, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::Write(int64_t offset, - const char* buffer, - int32_t bytes_to_write, - PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback); - if (rv != PP_OK) - return rv; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::WriteValidated(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; if (quota_file_io_.get()) { if (!quota_file_io_->Write( offset, buffer, bytes_to_write, - base::Bind(&PPB_FileIO_Impl::WriteCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } else { if (!base::FileUtilProxy::Write( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset, buffer, bytes_to_write, - base::Bind(&PPB_FileIO_Impl::WriteCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } - RegisterCallback(OPERATION_WRITE, callback, NULL); + RegisterCallback(OPERATION_WRITE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::SetLength(int64_t length, - PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); - if (rv != PP_OK) - return rv; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::SetLengthValidated(int64_t length, + PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; if (quota_file_io_.get()) { if (!quota_file_io_->SetLength( length, - base::Bind(&PPB_FileIO_Impl::StatusCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } else { if (!base::FileUtilProxy::Truncate( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length, - base::Bind(&PPB_FileIO_Impl::StatusCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; } - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { - int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback); - if (rv != PP_OK) - return rv; - - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); +int32_t PPB_FileIO_Impl::FlushValidated(PP_CompletionCallback callback) { + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (!plugin_delegate) return PP_ERROR_FAILED; if (!base::FileUtilProxy::Flush( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, - base::Bind(&PPB_FileIO_Impl::StatusCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } void PPB_FileIO_Impl::Close() { - PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); + PluginDelegate* plugin_delegate = GetPluginDelegate(); if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) { base::FileUtilProxy::Close( plugin_delegate->GetFileThreadMessageLoopProxy(), file_, @@ -295,11 +232,11 @@ int32_t PPB_FileIO_Impl::WillWrite(int64_t offset, if (!quota_file_io_->WillWrite( offset, bytes_to_write, - base::Bind(&PPB_FileIO_Impl::WillWriteCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformWillWriteCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } @@ -314,85 +251,26 @@ int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, if (!quota_file_io_->WillSetLength( length, - base::Bind(&PPB_FileIO_Impl::StatusCallback, + base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback, weak_factory_.GetWeakPtr()))) return PP_ERROR_FAILED; - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); + RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL); return PP_OK_COMPLETIONPENDING; } -int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, - OperationType new_op, - PP_CompletionCallback callback) { - // Only asynchronous operation is supported. - if (!callback.func) - return PP_ERROR_BLOCKS_MAIN_THREAD; - - if (should_be_open) { - if (file_ == base::kInvalidPlatformFileValue) - return PP_ERROR_FAILED; - } else { - if (file_ != base::kInvalidPlatformFileValue) - return PP_ERROR_FAILED; - } - - if (pending_op_ != OPERATION_NONE && - (pending_op_ != new_op || pending_op_ == OPERATION_EXCLUSIVE)) { - return PP_ERROR_INPROGRESS; - } - - return PP_OK; -} - -void PPB_FileIO_Impl::RegisterCallback(OperationType op, - PP_CompletionCallback callback, - char* read_buffer) { - DCHECK(callback.func); - DCHECK(pending_op_ == OPERATION_NONE || - (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); - - PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); - if (!plugin_module) - return; - - CallbackEntry entry; - entry.callback = new TrackedCompletionCallback( - plugin_module->GetCallbackTracker(), pp_resource(), callback); - entry.read_buffer = read_buffer; - - callbacks_.push(entry); - pending_op_ = op; -} - -void PPB_FileIO_Impl::RunAndRemoveFirstPendingCallback(int32_t result) { - DCHECK(!callbacks_.empty()); - - CallbackEntry front = callbacks_.front(); - callbacks_.pop(); - if (callbacks_.empty()) - pending_op_ = OPERATION_NONE; - - front.callback->Run(result); // Will complete abortively if necessary. +PluginDelegate* PPB_FileIO_Impl::GetPluginDelegate() { + return ResourceHelper::GetPluginDelegate(this); } -void PPB_FileIO_Impl::StatusCallback(base::PlatformFileError error_code) { - if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { - NOTREACHED(); - return; - } - - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); +void PPB_FileIO_Impl::ExecutePlatformGeneralCallback( + base::PlatformFileError error_code) { + ExecuteGeneralCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); } -void PPB_FileIO_Impl::AsyncOpenFileCallback( +void PPB_FileIO_Impl::ExecutePlatformOpenFileCallback( base::PlatformFileError error_code, base::PassPlatformFile file) { - if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { - NOTREACHED(); - return; - } - DCHECK(file_ == base::kInvalidPlatformFileValue); file_ = file.ReleaseValue(); @@ -404,73 +282,46 @@ void PPB_FileIO_Impl::AsyncOpenFileCallback( pp_instance(), file_, file_system_url_, file_system_type_)); } - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); + ExecuteOpenFileCallback(::ppapi::PlatformFileErrorToPepperError(error_code)); } -void PPB_FileIO_Impl::QueryInfoCallback( +void PPB_FileIO_Impl::ExecutePlatformQueryCallback( base::PlatformFileError error_code, const base::PlatformFileInfo& file_info) { - if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { - NOTREACHED(); - return; - } - - DCHECK(info_); - if (error_code == base::PLATFORM_FILE_OK) { - info_->size = file_info.size; - info_->creation_time = TimeToPPTime(file_info.creation_time); - info_->last_access_time = TimeToPPTime(file_info.last_accessed); - info_->last_modified_time = TimeToPPTime(file_info.last_modified); - info_->system_type = file_system_type_; - if (file_info.is_directory) - info_->type = PP_FILETYPE_DIRECTORY; - else - info_->type = PP_FILETYPE_REGULAR; - } - info_ = NULL; - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); + PP_FileInfo pp_info; + pp_info.size = file_info.size; + pp_info.creation_time = TimeToPPTime(file_info.creation_time); + pp_info.last_access_time = TimeToPPTime(file_info.last_accessed); + pp_info.last_modified_time = TimeToPPTime(file_info.last_modified); + pp_info.system_type = file_system_type_; + if (file_info.is_directory) + pp_info.type = PP_FILETYPE_DIRECTORY; + else + pp_info.type = PP_FILETYPE_REGULAR; + + ExecuteQueryCallback(::ppapi::PlatformFileErrorToPepperError(error_code), + pp_info); } -void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code, - const char* data, int bytes_read) { - if (pending_op_ != OPERATION_READ || callbacks_.empty()) { - NOTREACHED(); - return; - } - - char* read_buffer = callbacks_.front().read_buffer; - DCHECK(data); - DCHECK(read_buffer); - - int rv; - if (error_code == base::PLATFORM_FILE_OK) { - rv = bytes_read; - if (file_ != base::kInvalidPlatformFileValue) - memcpy(read_buffer, data, bytes_read); - } else { - rv = PlatformFileErrorToPepperError(error_code); - } - - RunAndRemoveFirstPendingCallback(rv); +void PPB_FileIO_Impl::ExecutePlatformReadCallback( + base::PlatformFileError error_code, + const char* data, int bytes_read) { + // Map the error code, OK getting mapped to the # of bytes read. + int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code); + pp_result = pp_result == PP_OK ? bytes_read : pp_result; + ExecuteReadCallback(pp_result, data); } -void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, - int bytes_written) { - if (pending_op_ != OPERATION_WRITE || callbacks_.empty()) { - NOTREACHED(); - return; - } - - if (error_code != base::PLATFORM_FILE_OK) { - RunAndRemoveFirstPendingCallback( - PlatformFileErrorToPepperError(error_code)); - } else { - RunAndRemoveFirstPendingCallback(bytes_written); - } +void PPB_FileIO_Impl::ExecutePlatformWriteCallback( + base::PlatformFileError error_code, + int bytes_written) { + int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code); + ExecuteGeneralCallback(pp_result == PP_OK ? bytes_written : pp_result); } -void PPB_FileIO_Impl::WillWriteCallback(base::PlatformFileError error_code, - int bytes_written) { +void PPB_FileIO_Impl::ExecutePlatformWillWriteCallback( + base::PlatformFileError error_code, + int bytes_written) { if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { NOTREACHED(); return; @@ -478,7 +329,7 @@ void PPB_FileIO_Impl::WillWriteCallback(base::PlatformFileError error_code, if (error_code != base::PLATFORM_FILE_OK) { RunAndRemoveFirstPendingCallback( - PlatformFileErrorToPepperError(error_code)); + ::ppapi::PlatformFileErrorToPepperError(error_code)); } else { RunAndRemoveFirstPendingCallback(bytes_written); } diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.h b/webkit/plugins/ppapi/ppb_file_io_impl.h index c4a0b3a9..c57430b 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.h +++ b/webkit/plugins/ppapi/ppb_file_io_impl.h @@ -5,19 +5,12 @@ #ifndef WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ #define WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ -#include <queue> - #include "base/basictypes.h" -#include "base/file_path.h" +#include "base/compiler_specific.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" -#include "base/platform_file.h" -#include "ppapi/c/pp_file_info.h" -#include "ppapi/c/pp_time.h" -#include "ppapi/shared_impl/resource.h" -#include "ppapi/thunk/ppb_file_io_api.h" -#include "webkit/plugins/ppapi/callbacks.h" +#include "ppapi/shared_impl/ppb_file_io_shared.h" #include "webkit/plugins/ppapi/plugin_delegate.h" struct PP_CompletionCallback; @@ -28,35 +21,13 @@ namespace ppapi { class QuotaFileIO; -class PPB_FileIO_Impl : public ::ppapi::Resource, - public ::ppapi::thunk::PPB_FileIO_API { +class PPB_FileIO_Impl : public ::ppapi::PPB_FileIO_Shared { public: explicit PPB_FileIO_Impl(PP_Instance instance); virtual ~PPB_FileIO_Impl(); - // Resource overrides. - virtual ::ppapi::thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; - - // PPB_FileIO_API implementation. - virtual int32_t Open(PP_Resource file_ref, - int32_t open_flags, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t Query(PP_FileInfo* info, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t Touch(PP_Time last_access_time, - PP_Time last_modified_time, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t Read(int64_t offset, - char* buffer, - int32_t bytes_to_read, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t Write(int64_t offset, - const char* buffer, - int32_t bytes_to_write, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t SetLength(int64_t length, - PP_CompletionCallback callback) OVERRIDE; - virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE; + // PPB_FileIO_API implementation (most of the operations are implemented + // as the "Validated" versions below). virtual void Close() OVERRIDE; virtual int32_t GetOSFileDescriptor() OVERRIDE; virtual int32_t WillWrite(int64_t offset, @@ -66,74 +37,51 @@ class PPB_FileIO_Impl : public ::ppapi::Resource, PP_CompletionCallback callback) OVERRIDE; private: - struct CallbackEntry { - CallbackEntry(); - CallbackEntry(const CallbackEntry& entry); - ~CallbackEntry(); - - scoped_refptr<TrackedCompletionCallback> callback; - - // Pointer back to the caller's read buffer; only used by |Read()|. - // Not owned. - char* read_buffer; - }; - - enum OperationType { - // If there are pending reads, any other kind of async operation is not - // allowed. - OPERATION_READ, - // If there are pending writes, any other kind of async operation is not - // allowed. - OPERATION_WRITE, - // If there is a pending operation that is neither read nor write, no - // further async operation is allowed. - OPERATION_EXCLUSIVE, - // There is no pending operation right now. - OPERATION_NONE, - }; - - // Verifies: - // - that |callback| is valid (only nonblocking operation supported); - // - that the file is already open or not, depending on |should_be_open|; and - // - that no callback is already pending, or it is a read(write) request - // and currently the pending operations are reads(writes). - // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if - // the call should be aborted and that code returned to the plugin. - int32_t CommonCallValidation(bool should_be_open, - OperationType new_op, - PP_CompletionCallback callback); - - // Sets up a pending callback. This should only be called once it is certain - // that |PP_OK_COMPLETIONPENDING| will be returned. - // |read_buffer| is only used by read operations. - void RegisterCallback(OperationType op, - PP_CompletionCallback callback, - char* read_buffer); - void RunAndRemoveFirstPendingCallback(int32_t result); - - void StatusCallback(base::PlatformFileError error_code); - void AsyncOpenFileCallback(base::PlatformFileError error_code, - base::PassPlatformFile file); - void QueryInfoCallback(base::PlatformFileError error_code, - const base::PlatformFileInfo& file_info); - void ReadCallback(base::PlatformFileError error_code, - const char* data, int bytes_read); - void WriteCallback(base::PlatformFileError error_code, int bytes_written); - void WillWriteCallback(base::PlatformFileError error_code, int bytes_written); + // FileIOImpl overrides. + virtual int32_t OpenValidated(PP_Resource file_ref_resource, + ::ppapi::thunk::PPB_FileRef_API* file_ref_api, + int32_t open_flags, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t QueryValidated(PP_FileInfo* info, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t TouchValidated(PP_Time last_access_time, + PP_Time last_modified_time, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t ReadValidated(int64_t offset, + char* buffer, + int32_t bytes_to_read, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t WriteValidated(int64_t offset, + const char* buffer, + int32_t bytes_to_write, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t SetLengthValidated(int64_t length, + PP_CompletionCallback callback) OVERRIDE; + virtual int32_t FlushValidated(PP_CompletionCallback callback) OVERRIDE; + + // Returns the plugin delegate for this resource if it exists, or NULL if it + // doesn't. Calling code should always check for NULL. + PluginDelegate* GetPluginDelegate(); + + // Callback handlers. These mostly convert the PlatformFileError to the + // PP_Error code and call the shared (non-"Platform") version. + void ExecutePlatformGeneralCallback(base::PlatformFileError error_code); + void ExecutePlatformOpenFileCallback(base::PlatformFileError error_code, + base::PassPlatformFile file); + void ExecutePlatformQueryCallback(base::PlatformFileError error_code, + const base::PlatformFileInfo& file_info); + void ExecutePlatformReadCallback(base::PlatformFileError error_code, + const char* data, int bytes_read); + void ExecutePlatformWriteCallback(base::PlatformFileError error_code, + int bytes_written); + void ExecutePlatformWillWriteCallback(base::PlatformFileError error_code, + int bytes_written); base::PlatformFile file_; - PP_FileSystemType file_system_type_; // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. GURL file_system_url_; - std::queue<CallbackEntry> callbacks_; - OperationType pending_op_; - - // Output buffer pointer for |Query()|; only non-null when a callback is - // pending for it. - PP_FileInfo* info_; - // Pointer to a QuotaFileIO instance, which is valid only while a file // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. scoped_ptr<QuotaFileIO> quota_file_io_; diff --git a/webkit/plugins/ppapi/ppb_flash_file_impl.cc b/webkit/plugins/ppapi/ppb_flash_file_impl.cc index 97eb649..46d9786 100644 --- a/webkit/plugins/ppapi/ppb_flash_file_impl.cc +++ b/webkit/plugins/ppapi/ppb_flash_file_impl.cc @@ -11,11 +11,11 @@ #include "ppapi/c/pp_file_info.h" #include "ppapi/c/ppb_file_io.h" #include "ppapi/c/private/ppb_flash_file.h" +#include "ppapi/shared_impl/file_type_conversion.h" #include "ppapi/shared_impl/time_conversion.h" #include "ppapi/thunk/enter.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/file_path.h" -#include "webkit/plugins/ppapi/file_type_conversions.h" #include "webkit/plugins/ppapi/host_globals.h" #include "webkit/plugins/ppapi/plugin_delegate.h" #include "webkit/plugins/ppapi/plugin_module.h" @@ -63,7 +63,9 @@ int32_t OpenModuleLocalFile(PP_Instance pp_instance, int32_t mode, PP_FileHandle* file) { int flags = 0; - if (!path || !PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || !file) + if (!path || + !::ppapi::PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || + !file) return PP_ERROR_BADARGUMENT; PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); @@ -76,7 +78,7 @@ int32_t OpenModuleLocalFile(PP_Instance pp_instance, flags, &base_file); *file = base_file; - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t RenameModuleLocalFile(PP_Instance pp_instance, @@ -92,7 +94,7 @@ int32_t RenameModuleLocalFile(PP_Instance pp_instance, base::PlatformFileError result = instance->delegate()->RenameFile( PepperFilePath::MakeModuleLocal(instance->module(), from_path), PepperFilePath::MakeModuleLocal(instance->module(), to_path)); - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, @@ -108,7 +110,7 @@ int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, base::PlatformFileError result = instance->delegate()->DeleteFileOrDir( PepperFilePath::MakeModuleLocal(instance->module(), path), PPBoolToBool(recursive)); - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { @@ -121,7 +123,7 @@ int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { base::PlatformFileError result = instance->delegate()->CreateDir( PepperFilePath::MakeModuleLocal(instance->module(), path)); - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t QueryModuleLocalFile(PP_Instance pp_instance, @@ -149,7 +151,7 @@ int32_t QueryModuleLocalFile(PP_Instance pp_instance, else info->type = PP_FILETYPE_REGULAR; } - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t GetModuleLocalDirContents(PP_Instance pp_instance, @@ -168,7 +170,7 @@ int32_t GetModuleLocalDirContents(PP_Instance pp_instance, &pepper_contents); if (result != base::PLATFORM_FILE_OK) - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); *contents = new PP_DirContents_Dev; size_t count = pepper_contents.size(); @@ -218,7 +220,7 @@ int32_t OpenFileRefFile(PP_Resource file_ref_id, int32_t mode, PP_FileHandle* file) { int flags = 0; - if (!PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || !file) + if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || !file) return PP_ERROR_BADARGUMENT; EnterResource<PPB_FileRef_API> enter(file_ref_id, true); @@ -236,7 +238,7 @@ int32_t OpenFileRefFile(PP_Resource file_ref_id, flags, &base_file); *file = base_file; - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } int32_t QueryFileRefFile(PP_Resource file_ref_id, @@ -265,7 +267,7 @@ int32_t QueryFileRefFile(PP_Resource file_ref_id, else info->type = PP_FILETYPE_REGULAR; } - return PlatformFileErrorToPepperError(result); + return ::ppapi::PlatformFileErrorToPepperError(result); } const PPB_Flash_File_FileRef ppb_flash_file_fileref = { |