Chromium Code Reviews| Index: webkit/plugins/ppapi/ppb_file_io_impl.cc |
| diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.cc b/webkit/plugins/ppapi/ppb_file_io_impl.cc |
| index 309067cd1b5cbf51783770aa46f8c63a8acff00a..8c585c9794440b81d939d2f6a1eb0ee36bfcadd7 100644 |
| --- a/webkit/plugins/ppapi/ppb_file_io_impl.cc |
| +++ b/webkit/plugins/ppapi/ppb_file_io_impl.cc |
| @@ -4,7 +4,6 @@ |
| #include "webkit/plugins/ppapi/ppb_file_io_impl.h" |
|
michaeln
2011/07/20 21:28:33
Are any plugins using the file descriptor base API
kinuko
2011/07/21 14:34:08
I guessed you meant this but also wished there mig
|
| -#include "base/callback.h" |
| #include "base/file_util.h" |
| #include "base/file_util_proxy.h" |
| #include "base/message_loop_proxy.h" |
| @@ -18,12 +17,14 @@ |
| #include "ppapi/shared_impl/time_conversion.h" |
| #include "ppapi/thunk/enter.h" |
| #include "ppapi/thunk/ppb_file_ref_api.h" |
| +#include "webkit/fileapi/file_system_util.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" |
| #include "webkit/plugins/ppapi/resource_tracker.h" |
| +#include "webkit/quota/quota_client.h" |
| using ppapi::PPTimeToTime; |
| using ppapi::TimeToPPTime; |
| @@ -34,6 +35,23 @@ using ppapi::thunk::PPB_FileRef_API; |
| namespace webkit { |
| namespace ppapi { |
| +namespace { |
| +quota::StorageType PPFileSystemTypeToQuotaStorageType(PP_FileSystemType type) { |
| + switch (type) { |
| + case PP_FILESYSTEMTYPE_LOCALPERSISTENT: |
| + return quota::kStorageTypePersistent; |
| + case PP_FILESYSTEMTYPE_LOCALTEMPORARY: |
| + return quota::kStorageTypeTemporary; |
| + default: |
| + return quota::kStorageTypeUnknown; |
| + } |
| + NOTREACHED(); |
| +} |
| +} // namespace |
| + |
| +PPB_FileIO_Impl::PendingOperation::PendingOperation() |
| + : type(UNKNOWN), offset(0), buffer(NULL), bytes_to_write(0) {} |
| + |
| PPB_FileIO_Impl::CallbackEntry::CallbackEntry() |
| : read_buffer(NULL) { |
| } |
| @@ -50,8 +68,11 @@ PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) |
| : Resource(instance), |
| ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), |
| file_(base::kInvalidPlatformFileValue), |
| + file_size_(-1), |
| + cached_available_space_(0), |
| pending_op_(OPERATION_NONE), |
| - info_(NULL) { |
| + info_(NULL), |
| + pending_setlength_length_(-1) { |
| } |
| PPB_FileIO_Impl::~PPB_FileIO_Impl() { |
| @@ -88,18 +109,21 @@ int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, |
| return PP_ERROR_FAILED; |
| break; |
| case PP_FILESYSTEMTYPE_LOCALPERSISTENT: |
| - case PP_FILESYSTEMTYPE_LOCALTEMPORARY: |
| + case PP_FILESYSTEMTYPE_LOCALTEMPORARY: { |
| + GURL root_url = file_ref->GetFileSystemURL(); |
| if (!instance()->delegate()->AsyncOpenFileSystemURL( |
| - file_ref->GetFileSystemURL(), flags, |
| + root_url, flags, |
| callback_factory_.NewCallback( |
| &PPB_FileIO_Impl::AsyncOpenFileCallback))) |
| return PP_ERROR_FAILED; |
| + origin_url_ = GURL(root_url.path()).GetOrigin(); |
| break; |
| + } |
| default: |
| return PP_ERROR_FAILED; |
| } |
| - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| + RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -120,7 +144,7 @@ int32_t PPB_FileIO_Impl::Query(PP_FileInfo* info, |
| callback_factory_.NewCallback(&PPB_FileIO_Impl::QueryInfoCallback))) |
| return PP_ERROR_FAILED; |
| - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| + RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -138,7 +162,7 @@ int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, |
| callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) |
| return PP_ERROR_FAILED; |
| - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| + RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -156,7 +180,7 @@ int32_t PPB_FileIO_Impl::Read(int64_t offset, |
| callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadCallback))) |
| return PP_ERROR_FAILED; |
| - RegisterCallback(OPERATION_READ, callback, buffer); |
| + RegisterCallback(&callbacks_, OPERATION_READ, callback, buffer); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -168,13 +192,32 @@ int32_t PPB_FileIO_Impl::Write(int64_t offset, |
| if (rv != PP_OK) |
| return rv; |
| - if (!base::FileUtilProxy::Write( |
| - instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| - file_, offset, buffer, bytes_to_write, |
| - callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) |
| - return PP_ERROR_FAILED; |
| + int64_t expected_growth = offset + bytes_to_write - file_size_; |
| - RegisterCallback(OPERATION_WRITE, callback, NULL); |
| + if (!DoesRequireQuotaCheck() || |
| + expected_growth <= cached_available_space_) { |
| + // we're ok to go. |
| + if (!base::FileUtilProxy::Write( |
| + instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| + file_, offset, buffer, bytes_to_write, |
| + callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) { |
| + return PP_ERROR_FAILED; |
| + } |
| + pending_write_offset_.push(offset); |
| + RegisterCallback(&callbacks_, OPERATION_WRITE, callback, NULL); |
| + return PP_OK_COMPLETIONPENDING; |
| + } |
| + |
| + // Enqueue this request and query the quota. |
| + PendingOperation op; |
| + op.type = PendingOperation::WRITE; |
| + op.offset = offset; |
| + op.buffer = buffer; |
| + op.bytes_to_write = bytes_to_write; |
| + QueryAvailableSpace(op); |
| + |
| + RegisterCallback(&pending_quota_check_callbacks_, |
|
yzshen1
2011/07/20 20:50:36
The problem is that write operations may be execut
kinuko
2011/07/21 14:34:08
I changed the code to either always or never queue
|
| + OPERATION_WRITE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -184,13 +227,31 @@ int32_t PPB_FileIO_Impl::SetLength(int64_t length, |
| if (rv != PP_OK) |
| return rv; |
| - if (!base::FileUtilProxy::Truncate( |
| - instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| - file_, length, |
| - callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) |
| - return PP_ERROR_FAILED; |
| + DCHECK_EQ(-1, pending_setlength_length_); |
| + int64_t expected_growth = length - file_size_; |
| + |
| + if (!DoesRequireQuotaCheck() || |
| + expected_growth <= cached_available_space_) { |
| + if (!base::FileUtilProxy::Truncate( |
| + instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| + file_, length, |
| + callback_factory_.NewCallback( |
| + &PPB_FileIO_Impl::SetLengthCallback))) { |
| + return PP_ERROR_FAILED; |
| + } |
| + pending_setlength_length_ = length; |
| + RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL); |
| + return PP_OK_COMPLETIONPENDING; |
| + } |
| + |
| + // Enqueue this request and query the quota. |
| + PendingOperation op; |
| + op.type = PendingOperation::SETLENGTH; |
| + op.length = length; |
| + QueryAvailableSpace(op); |
| - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| + RegisterCallback(&pending_quota_check_callbacks_, |
| + OPERATION_EXCLUSIVE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -204,7 +265,7 @@ int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { |
| callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) |
| return PP_ERROR_FAILED; |
| - RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL); |
| + RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL); |
| return PP_OK_COMPLETIONPENDING; |
| } |
| @@ -230,12 +291,14 @@ int32_t PPB_FileIO_Impl::WillWrite(int64_t offset, |
| int32_t bytes_to_write, |
| PP_CompletionCallback callback) { |
| // TODO(dumi): implement me |
| + // Request quota. |
|
michaeln
2011/07/20 21:28:33
Given the shape of the trusted APIs (there's a Wil
kinuko
2011/07/21 14:34:08
Hmm the successive write or truncate may fail, so
|
| return PP_OK; |
| } |
| int32_t PPB_FileIO_Impl::WillSetLength(int64_t length, |
| PP_CompletionCallback callback) { |
| // TODO(dumi): implement me |
| + // Request quota. |
| return PP_OK; |
| } |
| @@ -264,7 +327,8 @@ int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open, |
| return PP_OK; |
| } |
| -void PPB_FileIO_Impl::RegisterCallback(OperationType op, |
| +void PPB_FileIO_Impl::RegisterCallback(std::queue<CallbackEntry>* callbacks, |
| + OperationType op, |
| PP_CompletionCallback callback, |
| char* read_buffer) { |
| DCHECK(callback.func); |
| @@ -278,16 +342,17 @@ void PPB_FileIO_Impl::RegisterCallback(OperationType op, |
| instance()->module()->GetCallbackTracker(), resource_id, callback); |
| entry.read_buffer = read_buffer; |
| - callbacks_.push(entry); |
| + callbacks->push(entry); |
| pending_op_ = op; |
| } |
| -void PPB_FileIO_Impl::RunAndRemoveFirstPendingCallback(int32_t result) { |
| - DCHECK(!callbacks_.empty()); |
| +void PPB_FileIO_Impl::RunAndRemoveFirstPendingCallback( |
| + std::queue<CallbackEntry>* callbacks, int32_t result) { |
| + DCHECK(!callbacks->empty()); |
| - CallbackEntry front = callbacks_.front(); |
| - callbacks_.pop(); |
| - if (callbacks_.empty()) |
| + CallbackEntry front = callbacks->front(); |
| + callbacks->pop(); |
| + if (callbacks->empty()) |
| pending_op_ = OPERATION_NONE; |
| front.callback->Run(result); // Will complete abortively if necessary. |
| @@ -299,12 +364,14 @@ void PPB_FileIO_Impl::StatusCallback(base::PlatformFileError error_code) { |
| return; |
| } |
| - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| + RunAndRemoveFirstPendingCallback( |
| + &callbacks_, PlatformFileErrorToPepperError(error_code)); |
| } |
| void PPB_FileIO_Impl::AsyncOpenFileCallback( |
| base::PlatformFileError error_code, |
| - base::PassPlatformFile file) { |
| + base::PassPlatformFile file, |
| + int64_t file_size) { |
| if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) { |
| NOTREACHED(); |
| return; |
| @@ -312,7 +379,12 @@ void PPB_FileIO_Impl::AsyncOpenFileCallback( |
| DCHECK(file_ == base::kInvalidPlatformFileValue); |
| file_ = file.ReleaseValue(); |
| - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| + file_size_ = file_size; |
| + DCHECK(!DoesRequireQuotaCheck() || |
| + error_code != base::PLATFORM_FILE_OK || |
| + file_size_ >= 0); |
| + RunAndRemoveFirstPendingCallback( |
| + &callbacks_, PlatformFileErrorToPepperError(error_code)); |
| } |
| void PPB_FileIO_Impl::QueryInfoCallback( |
| @@ -336,7 +408,8 @@ void PPB_FileIO_Impl::QueryInfoCallback( |
| info_->type = PP_FILETYPE_REGULAR; |
| } |
| info_ = NULL; |
| - RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code)); |
| + RunAndRemoveFirstPendingCallback( |
| + &callbacks_, PlatformFileErrorToPepperError(error_code)); |
| } |
| void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code, |
| @@ -359,7 +432,7 @@ void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code, |
| rv = PlatformFileErrorToPepperError(error_code); |
| } |
| - RunAndRemoveFirstPendingCallback(rv); |
| + RunAndRemoveFirstPendingCallback(&callbacks_, rv); |
| } |
| void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, |
| @@ -369,11 +442,143 @@ void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, |
| return; |
| } |
| + DCHECK(!pending_write_offset_.empty()); |
| + int64_t offset = pending_write_offset_.front(); |
| + pending_write_offset_.pop(); |
| + |
| if (error_code != base::PLATFORM_FILE_OK) { |
| RunAndRemoveFirstPendingCallback( |
| - PlatformFileErrorToPepperError(error_code)); |
| + &callbacks_, PlatformFileErrorToPepperError(error_code)); |
| } else { |
| - RunAndRemoveFirstPendingCallback(bytes_written); |
| + int64_t growth = offset + bytes_written - file_size_; |
| + if (growth >= 0) { |
| + if (DoesRequireQuotaCheck()) { |
| + instance()->delegate()->NotifyStorageModified( |
| + quota::QuotaClient::kFileSystem, |
| + origin_url_, |
| + PPFileSystemTypeToQuotaStorageType(file_system_type_), |
| + growth); |
| + } |
| + file_size_ += growth; |
| + } |
| + RunAndRemoveFirstPendingCallback(&callbacks_, bytes_written); |
| + } |
| + |
| + // Reset the available space. |
| + if (HasNoPendingWrites()) |
| + cached_available_space_ = 0; |
| +} |
| + |
| +void PPB_FileIO_Impl::SetLengthCallback( |
| + base::PlatformFileError error_code) { |
| + if (callbacks_.empty()) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + DCHECK_NE(-1, pending_setlength_length_); |
| + |
| + if (base::PLATFORM_FILE_OK == error_code) { |
| + if (DoesRequireQuotaCheck()) { |
| + int64_t delta = pending_setlength_length_ - file_size_; |
| + instance()->delegate()->NotifyStorageModified( |
| + quota::QuotaClient::kFileSystem, |
| + origin_url_, |
| + PPFileSystemTypeToQuotaStorageType(file_system_type_), |
| + delta); |
| + } |
| + file_size_ = pending_setlength_length_; |
| + } |
| + |
| + RunAndRemoveFirstPendingCallback( |
| + &callbacks_, PlatformFileErrorToPepperError(error_code)); |
| + |
| + pending_setlength_length_ = -1; |
|
michaeln
2011/07/20 21:28:33
If the caller's callback has invoked FileIO.SetLen
kinuko
2011/07/21 14:34:08
Good catch, fixed.
|
| + cached_available_space_ = 0; |
| +} |
| + |
| +bool PPB_FileIO_Impl::DoesRequireQuotaCheck() const { |
| + return (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || |
| + file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT); |
| +} |
| + |
| +bool PPB_FileIO_Impl::HasNoPendingWrites() const { |
| + return pending_quota_checks_.empty() && |
| + pending_write_offset_.empty() && |
| + pending_setlength_length_ == -1; |
| +} |
| + |
| +void PPB_FileIO_Impl::QueryAvailableSpace(const PendingOperation& op) { |
| + pending_quota_checks_.push(op); |
| + if (pending_quota_checks_.size() == 1) { |
| + // This is the first one. |
| + instance()->delegate()->QueryAvailableSpace( |
| + origin_url_, |
| + PPFileSystemTypeToQuotaStorageType(file_system_type_), |
| + callback_factory_.NewCallback( |
| + &PPB_FileIO_Impl::DidQueryAvailableSpace)); |
| + } |
| +} |
| + |
| +void PPB_FileIO_Impl::DidQueryAvailableSpace(int64_t avail_space) { |
| + cached_available_space_ = avail_space; |
| + DCHECK(!pending_quota_checks_.empty()); |
| + |
| + while (!pending_quota_checks_.empty()) { |
| + DCHECK(!pending_quota_check_callbacks_.empty()); |
| + PendingOperation op = pending_quota_checks_.front(); |
| + pending_quota_checks_.pop(); |
| + |
| + switch (op.type) { |
| + case PendingOperation::WRITE: { |
| + int64_t growth = op.offset + op.bytes_to_write - file_size_; |
| + if (growth > cached_available_space_) { |
| + RunAndRemoveFirstPendingCallback( |
| + &pending_quota_check_callbacks_, |
| + PP_ERROR_NOQUOTA); |
| + return; |
|
michaeln
2011/07/20 21:28:33
What about other pending checks in the queue? Is i
kinuko
2011/07/21 14:34:08
Done.
|
| + } |
| + if (!base::FileUtilProxy::Write( |
| + instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| + file_, op.offset, op.buffer, op.bytes_to_write, |
| + callback_factory_.NewCallback( |
| + &PPB_FileIO_Impl::WriteCallback))) { |
| + RunAndRemoveFirstPendingCallback( |
| + &pending_quota_check_callbacks_, PP_ERROR_FAILED); |
| + } |
| + pending_write_offset_.push(op.offset); |
| + break; |
| + } |
| + |
| + case PendingOperation::SETLENGTH: { |
| + int64_t growth = op.length - file_size_; |
| + if (growth > cached_available_space_) { |
| + RunAndRemoveFirstPendingCallback( |
| + &pending_quota_check_callbacks_, |
| + PP_ERROR_NOQUOTA); |
| + return; |
| + } |
| + if (!base::FileUtilProxy::Truncate( |
| + instance()->delegate()->GetFileThreadMessageLoopProxy(), |
| + file_, op.length, |
| + callback_factory_.NewCallback( |
| + &PPB_FileIO_Impl::SetLengthCallback))) { |
| + RunAndRemoveFirstPendingCallback( |
| + &pending_quota_check_callbacks_, PP_ERROR_FAILED); |
| + } |
| + DCHECK_EQ(-1, pending_setlength_length_); |
| + pending_setlength_length_ = op.length; |
| + break; |
| + } |
| + |
| + default: |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + CallbackEntry entry = pending_quota_check_callbacks_.front(); |
| + pending_quota_check_callbacks_.pop(); |
| + callbacks_.push(entry); |
| } |
| } |