Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Unified Diff: webkit/plugins/ppapi/ppb_file_io_impl.cc

Issue 7433006: Pepper quota support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f2c01ed5a915e0e0dc4e5c3801216f96790f26db 100644
--- a/webkit/plugins/ppapi/ppb_file_io_impl.cc
+++ b/webkit/plugins/ppapi/ppb_file_io_impl.cc
@@ -34,6 +34,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()
yzshen1 2011/07/21 22:23:34 Please use the same order as they are declared in
+ : type(UNKNOWN), offset(0), buffer(NULL), bytes_to_write(0) {}
yzshen1 2011/07/21 22:23:34 You miss |length|.
+
PPB_FileIO_Impl::CallbackEntry::CallbackEntry()
: read_buffer(NULL) {
}
@@ -51,7 +68,10 @@ PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance)
ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
file_(base::kInvalidPlatformFileValue),
pending_op_(OPERATION_NONE),
- info_(NULL) {
+ info_(NULL),
+ cached_available_space_(0),
yzshen1 2011/07/21 22:23:34 You miss quite a few members. Please initialize al
+ did_notify_will_update_(false),
+ pending_setlength_length_(-1) {
}
PPB_FileIO_Impl::~PPB_FileIO_Impl() {
@@ -89,8 +109,9 @@ int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref,
break;
case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
+ path_url_ = file_ref->GetFileSystemURL();
if (!instance()->delegate()->AsyncOpenFileSystemURL(
- file_ref->GetFileSystemURL(), flags,
+ path_url_, flags,
callback_factory_.NewCallback(
&PPB_FileIO_Impl::AsyncOpenFileCallback)))
return PP_ERROR_FAILED;
@@ -99,7 +120,7 @@ int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref,
return PP_ERROR_FAILED;
}
- RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL);
+ RegisterCallback(&callbacks_, OPERATION_EXCLUSIVE, callback, NULL);
return PP_OK_COMPLETIONPENDING;
}
@@ -120,7 +141,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 +159,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 +177,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 +189,28 @@ 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)))
+ if (!DoesRequireQuotaCheck()) {
+ if (!base::FileUtilProxy::Write(
+ instance()->delegate()->GetFileThreadMessageLoopProxy(),
+ file_, offset, buffer, bytes_to_write,
+ callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback))) {
+ return PP_ERROR_FAILED;
+ }
+ 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;
+ if (!QueryFileSizeAndQuota(op))
return PP_ERROR_FAILED;
- RegisterCallback(OPERATION_WRITE, callback, NULL);
+ RegisterCallback(&pending_quota_check_callbacks_,
+ OPERATION_WRITE, callback, NULL);
return PP_OK_COMPLETIONPENDING;
}
@@ -184,13 +220,27 @@ 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)))
+ if (!DoesRequireQuotaCheck()) {
+ if (!base::FileUtilProxy::Truncate(
+ instance()->delegate()->GetFileThreadMessageLoopProxy(),
+ file_, length,
+ callback_factory_.NewCallback(
+ &PPB_FileIO_Impl::SetLengthCallback))) {
+ return PP_ERROR_FAILED;
+ }
+ 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;
+ if (!QueryFileSizeAndQuota(op))
return PP_ERROR_FAILED;
- RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL);
+ RegisterCallback(&pending_quota_check_callbacks_,
+ OPERATION_EXCLUSIVE, callback, NULL);
return PP_OK_COMPLETIONPENDING;
}
@@ -204,7 +254,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;
}
@@ -229,14 +279,47 @@ int32_t PPB_FileIO_Impl::GetOSFileDescriptor() {
int32_t PPB_FileIO_Impl::WillWrite(int64_t offset,
int32_t bytes_to_write,
PP_CompletionCallback callback) {
- // TODO(dumi): implement me
- return PP_OK;
+ int32_t rv = CommonCallValidation(true, OPERATION_WRITE, callback);
+ if (rv != PP_OK)
+ return rv;
+
+ if (!DoesRequireQuotaCheck()) {
+ PP_RunCompletionCallback(&callback, PP_OK);
+ return PP_OK;
+ }
+
+ PendingOperation op;
+ op.type = PendingOperation::WILL_WRITE;
+ op.offset = offset;
+ op.bytes_to_write = bytes_to_write;
+ if (!QueryFileSizeAndQuota(op))
+ return PP_ERROR_FAILED;
+
+ RegisterCallback(&pending_quota_check_callbacks_,
+ OPERATION_WRITE, callback, NULL);
+ return PP_OK_COMPLETIONPENDING;
}
int32_t PPB_FileIO_Impl::WillSetLength(int64_t length,
PP_CompletionCallback callback) {
- // TODO(dumi): implement me
- return PP_OK;
+ int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback);
+ if (rv != PP_OK)
+ return rv;
+
+ if (!DoesRequireQuotaCheck()) {
+ PP_RunCompletionCallback(&callback, PP_OK);
+ return PP_OK;
+ }
+
+ PendingOperation op;
+ op.type = PendingOperation::WILL_SETLENGTH;
+ op.length = length;
+ if (!QueryFileSizeAndQuota(op))
+ return PP_ERROR_FAILED;
+
+ RegisterCallback(&pending_quota_check_callbacks_,
+ OPERATION_EXCLUSIVE, callback, NULL);
+ return PP_OK_COMPLETIONPENDING;
}
int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open,
@@ -264,7 +347,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 +362,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())
yzshen1 2011/07/21 22:23:34 pending_op_ can be set incorrectly. Consider the f
pending_op_ = OPERATION_NONE;
front.callback->Run(result); // Will complete abortively if necessary.
@@ -299,7 +384,8 @@ void PPB_FileIO_Impl::StatusCallback(base::PlatformFileError error_code) {
return;
}
- RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code));
+ RunAndRemoveFirstPendingCallback(
+ &callbacks_, PlatformFileErrorToPepperError(error_code));
}
void PPB_FileIO_Impl::AsyncOpenFileCallback(
@@ -312,7 +398,8 @@ void PPB_FileIO_Impl::AsyncOpenFileCallback(
DCHECK(file_ == base::kInvalidPlatformFileValue);
file_ = file.ReleaseValue();
- RunAndRemoveFirstPendingCallback(PlatformFileErrorToPepperError(error_code));
+ RunAndRemoveFirstPendingCallback(
+ &callbacks_, PlatformFileErrorToPepperError(error_code));
}
void PPB_FileIO_Impl::QueryInfoCallback(
@@ -336,7 +423,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 +447,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,12 +457,201 @@ void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code,
return;
}
+ if (DoesRequireQuotaCheck()) {
+ DCHECK(did_notify_will_update_);
+ DCHECK(!pending_write_offset_.empty());
+
+ // Update possible max file offset.
+ int64_t offset = pending_write_offset_.front();
+ pending_write_offset_.pop();
+ if (error_code == base::PLATFORM_FILE_OK)
+ max_written_offset_ = std::max(max_written_offset_,
+ offset + bytes_written);
+
+ // If we have no more pending writes, notify the browser that we did
+ // update the file.
+ if (pending_quota_checks_.empty() &&
+ pending_write_offset_.empty()) {
+ int64_t growth = max_written_offset_ - cached_file_size_;
+ if (growth > 0)
+ instance()->delegate()->DidUpdateFile(path_url_, growth);
+ did_notify_will_update_ = false;
+ }
+ }
+
if (error_code != base::PLATFORM_FILE_OK) {
RunAndRemoveFirstPendingCallback(
- PlatformFileErrorToPepperError(error_code));
+ &callbacks_, PlatformFileErrorToPepperError(error_code));
} else {
- RunAndRemoveFirstPendingCallback(bytes_written);
+ RunAndRemoveFirstPendingCallback(&callbacks_, bytes_written);
+ }
+}
+
+void PPB_FileIO_Impl::SetLengthCallback(
+ base::PlatformFileError error_code) {
+ if (callbacks_.empty()) {
+ NOTREACHED();
+ return;
+ }
+
+ if (DoesRequireQuotaCheck()) {
+ DCHECK_NE(-1, pending_setlength_length_);
+ DCHECK(did_notify_will_update_);
+ if (base::PLATFORM_FILE_OK == error_code) {
+ instance()->delegate()->DidUpdateFile(
+ path_url_, pending_setlength_length_ - cached_file_size_);
+ }
+ pending_setlength_length_ = -1;
+ did_notify_will_update_ = false;
+ }
+
+ RunAndRemoveFirstPendingCallback(
+ &callbacks_, PlatformFileErrorToPepperError(error_code));
+}
+
+bool PPB_FileIO_Impl::DoesRequireQuotaCheck() const {
+ return (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY ||
+ file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT);
+}
+
+bool PPB_FileIO_Impl::QueryFileSizeAndQuota(const PendingOperation& op) {
+ pending_quota_checks_.push(op);
+ if (pending_quota_checks_.size() == 1) {
+ // This is the first call.
+ DCHECK(!did_notify_will_update_);
+ outstanding_quota_queries_ = 0;
+ outstanding_errors_ = 0;
+
+ // Query the file size.
+ ++outstanding_quota_queries_;
+ if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(
+ instance()->delegate()->GetFileThreadMessageLoopProxy(), file_,
+ callback_factory_.NewCallback(
+ &PPB_FileIO_Impl::DidQueryInfoForQuota))) {
+ pending_quota_checks_.pop();
yzshen1 2011/07/21 22:23:34 Maybe you could push at the end of the method, so
kinuko 2011/07/26 16:27:46 Done (in quota_file_io.cc)
+ return false;
+ }
+
+ // Query the current available space.
+ ++outstanding_quota_queries_;
+ instance()->delegate()->QueryAvailableSpace(
+ GURL(path_url_.path()).GetOrigin(),
+ PPFileSystemTypeToQuotaStorageType(file_system_type_),
+ callback_factory_.NewCallback(
+ &PPB_FileIO_Impl::DidQueryAvailableSpace));
+ }
+ return true;
+}
+
+void PPB_FileIO_Impl::DidQueryInfoForQuota(
+ base::PlatformFileError error_code,
+ const base::PlatformFileInfo& file_info) {
+ if (error_code != base::PLATFORM_FILE_OK)
+ ++outstanding_errors_;
+ cached_file_size_ = max_written_offset_ = file_info.size;
+ DCHECK_GT(outstanding_quota_queries_, 0);
+ if (--outstanding_quota_queries_ == 0)
+ DidQueryFileSizeAndQuota();
+}
+
+void PPB_FileIO_Impl::DidQueryAvailableSpace(int64_t avail_space) {
+ cached_available_space_ = avail_space;
+ DCHECK_GT(outstanding_quota_queries_, 0);
+ if (--outstanding_quota_queries_ == 0)
+ DidQueryFileSizeAndQuota();
+}
+
+void PPB_FileIO_Impl::DidQueryFileSizeAndQuota() {
+ DCHECK(!pending_quota_checks_.empty());
+ while (!pending_quota_checks_.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 - cached_file_size_;
+ if (!CommonQuotaCheck(growth))
+ continue;
+ 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);
+ continue;
+ }
+ pending_write_offset_.push(op.offset);
+ break;
+ }
+
+ case PendingOperation::SETLENGTH: {
+ int64_t growth = op.length - cached_file_size_;
+ if (!CommonQuotaCheck(growth))
+ continue;
+ 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);
+ continue;
+ }
+ DCHECK_EQ(-1, pending_setlength_length_);
+ pending_setlength_length_ = op.length;
+ break;
+ }
+
+ case PendingOperation::WILL_WRITE: {
+ int64_t growth = op.offset + op.bytes_to_write - cached_file_size_;
+ if (!CommonQuotaCheck(growth))
+ continue;
+ // TODO(kinuko) we should have a way to handle failure cases;
+ // currently we always update the usage upfront even if the actual
+ // write (which will be called after we run the callback) may fail.
+ pending_write_offset_.push(op.offset);
+ WriteCallback(base::PLATFORM_FILE_OK, op.bytes_to_write);
yzshen1 2011/07/21 22:23:34 You shouldn't call WriteCallback before moving the
+ break;
+ }
+
+ case PendingOperation::WILL_SETLENGTH: {
+ int64_t growth = op.length - cached_file_size_;
+ if (!CommonQuotaCheck(growth))
+ continue;
+ // TODO(kinuko) we should have a way to handle failure cases;
+ // currently we always update the usage upfront even if the actual
+ // truncate (which will be called after we run the callback) may fail.
+ pending_setlength_length_ = op.length;
+ SetLengthCallback(base::PLATFORM_FILE_OK);
yzshen1 2011/07/21 22:23:34 You shouldn't call SetLengthCallback before moving
kinuko 2011/07/26 16:27:46 Added tests that call WillWrite/WillSetLength in p
+ break;
+ }
+
+ default:
+ NOTREACHED();
+ continue;
+ }
+
+ DCHECK(did_notify_will_update_);
+ CallbackEntry entry = pending_quota_check_callbacks_.front();
+ pending_quota_check_callbacks_.pop();
+ callbacks_.push(entry);
+ }
+}
+
+bool PPB_FileIO_Impl::CommonQuotaCheck(int64_t growth) {
+ DCHECK(!pending_quota_check_callbacks_.empty());
+ if (outstanding_errors_ || growth > cached_available_space_) {
+ RunAndRemoveFirstPendingCallback(
+ &pending_quota_check_callbacks_,
+ PP_ERROR_NOQUOTA);
+ return false;
+ }
+ if (!did_notify_will_update_) {
+ instance()->delegate()->WillUpdateFile(path_url_);
+ did_notify_will_update_ = true;
}
+ return true;
}
} // namespace ppapi
« webkit/plugins/ppapi/ppb_file_io_impl.h ('K') | « webkit/plugins/ppapi/ppb_file_io_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698