Chromium Code Reviews| Index: ppapi/proxy/file_io_resource.cc |
| diff --git a/ppapi/proxy/file_io_resource.cc b/ppapi/proxy/file_io_resource.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d6890f21fe9b3c9948c973469e21f5cde3152bb9 |
| --- /dev/null |
| +++ b/ppapi/proxy/file_io_resource.cc |
| @@ -0,0 +1,289 @@ |
| +// Copyright (c) 2012 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/file_io_resource.h" |
| + |
| +#include "base/bind.h" |
| +#include "ipc/ipc_message.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/shared_impl/array_writer.h" |
| +#include "ppapi/shared_impl/ppapi_globals.h" |
| +#include "ppapi/shared_impl/resource_tracker.h" |
| +#include "ppapi/thunk/ppb_file_ref_api.h" |
| + |
| +using ppapi::thunk::PPB_FileRef_API; |
| + |
| +namespace { |
| + |
| +// An adapter to let Read() share the same implementation with ReadToArray(). |
| +void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
| + return user_data; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ppapi { |
| +namespace proxy { |
| + |
| +FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
| + : PluginResource(connection, instance), |
| + PPB_FileIO_Shared(), |
| + num_pending_ops_(0), |
| + pending_op_(OPERATION_NONE) { |
| + SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
| +} |
| + |
| +FileIOResource::~FileIOResource() { |
| +} |
| + |
| +thunk::PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
| + return this; |
| +} |
| + |
| +int32_t FileIOResource::Open(PP_Resource file_ref, |
| + int32_t open_flags, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + return PPB_FileIO_Shared::DoOpen(file_ref, open_flags); |
| +} |
| + |
| +int32_t FileIOResource::Query(PP_FileInfo* info, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + temp_info_ = info; |
| + return DoQuery(); |
| +} |
| + |
| +int32_t FileIOResource::Touch(PP_Time last_access_time, |
| + PP_Time last_modified_time, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + return DoTouch(last_access_time, last_modified_time); |
| +} |
| + |
| +int32_t FileIOResource::Read(int64_t offset, |
| + char* buffer, |
| + int32_t bytes_to_read, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + temp_array_output_.GetDataBuffer = &DummyGetDataBuffer; |
| + temp_array_output_.user_data = buffer; |
| + return PPB_FileIO_Shared::DoRead(offset, bytes_to_read); |
| +} |
| + |
| +int32_t FileIOResource::ReadToArray(int64_t offset, |
| + int32_t max_read_length, |
| + PP_ArrayOutput* array_output, |
| + scoped_refptr<TrackedCallback> callback) { |
| + DCHECK(array_output); |
| + temp_callback_ = callback; |
| + temp_array_output_ = *array_output; |
| + return PPB_FileIO_Shared::DoRead(offset, max_read_length); |
| +} |
| + |
| +int32_t FileIOResource::Write(int64_t offset, |
| + const char* buffer, |
| + int32_t bytes_to_write, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + return PPB_FileIO_Shared::DoWrite(offset, buffer, bytes_to_write); |
| +} |
| + |
| +int32_t FileIOResource::SetLength(int64_t length, |
| + scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + return PPB_FileIO_Shared::DoSetLength(length); |
| +} |
| + |
| +int32_t FileIOResource::Flush(scoped_refptr<TrackedCallback> callback) { |
| + temp_callback_ = callback; |
| + return PPB_FileIO_Shared::DoFlush(); |
| +} |
| + |
| +void FileIOResource::Close() { |
| + Post(RENDERER, PpapiHostMsg_FileIO_Close()); |
| +} |
| + |
| +int32_t FileIOResource::GetOSFileDescriptor() { |
| + int32_t file_descriptor; |
| + // Only available when runs in process. |
| + SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( |
| + RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); |
| + return file_descriptor; |
| +} |
| + |
| +int32_t FileIOResource::WillWrite(int64_t offset, |
| + int32_t bytes_to_write, |
| + scoped_refptr<TrackedCallback> callback) { |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + callback)); |
| + SetOpInProgress(OPERATION_EXCLUSIVE); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::WillSetLength(int64_t length, |
| + scoped_refptr<TrackedCallback> callback) { |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_WillSetLength(length), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + callback)); |
| + SetOpInProgress(OPERATION_EXCLUSIVE); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::CommonPreCondition(bool should_be_open, |
| + OperationType new_op) { |
| + if (!CheckOpenState(should_be_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 FileIOResource::CommonPostCondition(OperationType new_op) { |
| + SetOpInProgress(new_op); |
| +} |
| + |
| +int32_t FileIOResource::OpenValidated(PP_Resource file_ref_resource, |
| + PPB_FileRef_API* file_ref_api, |
| + int32_t open_flags) { |
| + Resource* file_ref_object = |
| + PpapiGlobals::Get()->GetResourceTracker()->GetResource(file_ref_resource); |
|
raymes
2012/11/27 16:41:30
You can use EnterResourceNoLock here.
victorhsieh
2012/11/28 04:11:55
Done.
|
| + |
| + Call<PpapiPluginMsg_FileIO_OpenFileComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Open( |
| + file_ref_object->host_resource().host_resource(), |
| + open_flags), |
| + base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, |
| + temp_callback_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::QueryValidated() { |
| + Call<PpapiPluginMsg_FileIO_QueryComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Query(), |
| + base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, |
| + temp_callback_, temp_info_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::TouchValidated(PP_Time last_access_time, |
| + PP_Time last_modified_time) { |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Touch(last_access_time, last_modified_time), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + temp_callback_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::ReadValidated(int64_t offset, |
| + int32_t bytes_to_read) { |
| + Call<PpapiPluginMsg_FileIO_ReadComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Read(offset, bytes_to_read), |
| + base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, |
| + temp_callback_, temp_array_output_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::WriteValidated(int64_t offset, |
| + const char* buffer, |
| + int32_t bytes_to_write) { |
| + // 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). |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Write(offset, std::string(buffer, bytes_to_write)), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + temp_callback_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::SetLengthValidated(int64_t length) { |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_SetLength(length), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + temp_callback_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +int32_t FileIOResource::FlushValidated() { |
| + Call<PpapiPluginMsg_FileIO_GeneralComplete>(RENDERER, |
| + PpapiHostMsg_FileIO_Flush(), |
| + base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| + temp_callback_)); |
| + return PP_OK_COMPLETIONPENDING; |
| +} |
| + |
| +void FileIOResource::OnPluginMsgGeneralComplete( |
| + scoped_refptr<TrackedCallback> callback, |
| + const ResourceMessageReplyParams& params) { |
| + DCHECK(pending_op_ == OPERATION_EXCLUSIVE || |
| + pending_op_ == OPERATION_WRITE); |
| + callback->Run(params.result()); |
| + SetOpFinished(); |
| +} |
| + |
| +void FileIOResource::OnPluginMsgOpenFileComplete( |
| + scoped_refptr<TrackedCallback> callback, |
| + const ResourceMessageReplyParams& params) { |
| + DCHECK(pending_op_ == OPERATION_EXCLUSIVE); |
| + if (params.result() == PP_OK) |
| + SetOpenSucceed(); |
| + callback->Run(params.result()); |
| + SetOpFinished(); |
| +} |
| + |
| +void FileIOResource::OnPluginMsgQueryComplete( |
| + scoped_refptr<TrackedCallback> callback, |
| + PP_FileInfo* output_info, |
| + const ResourceMessageReplyParams& params, |
| + const PP_FileInfo& info) { |
| + DCHECK(pending_op_ == OPERATION_EXCLUSIVE); |
| + *output_info = info; |
| + callback->Run(params.result()); |
| + SetOpFinished(); |
| +} |
| + |
| +void FileIOResource::OnPluginMsgReadComplete( |
| + scoped_refptr<TrackedCallback> callback, |
| + PP_ArrayOutput array_output, |
| + const ResourceMessageReplyParams& params, |
| + const std::string& data) { |
| + DCHECK(pending_op_ == OPERATION_READ); |
| + |
| + // The result code should contain the data size if it's positive. |
| + int32_t result = params.result(); |
| + DCHECK((result < 0 && data.size() == 0) || |
| + result == static_cast<int32_t>(data.size())); |
| + |
| + ArrayWriter output; |
| + output.set_pp_array_output(array_output); |
| + if (output.is_valid()) |
|
raymes
2012/11/27 16:41:30
If this fails, maybe we should run the callback wi
victorhsieh
2012/11/28 04:11:55
Done.
|
| + output.StoreArray(data.data(), std::max(0, result)); |
| + |
| + callback->Run(result); |
| + SetOpFinished(); |
| +} |
| + |
| +void FileIOResource::SetOpInProgress(OperationType new_op) { |
| + DCHECK(pending_op_ == OPERATION_NONE || |
| + (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == new_op)); |
| + pending_op_ = new_op; |
| + num_pending_ops_++; |
| +} |
| + |
| +void FileIOResource::SetOpFinished() { |
| + DCHECK(num_pending_ops_ > 0); |
| + if (--num_pending_ops_ == 0) |
| + pending_op_ = OPERATION_NONE; |
| +} |
| + |
| +} // namespace proxy |
| +} // namespace ppapi |