| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/plugins/ppapi/quota_file_io.h" | 5 #include "webkit/plugins/ppapi/quota_file_io.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_callback_factory.h" | 10 #include "base/memory/scoped_callback_factory.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "base/task.h" | 14 #include "base/task.h" |
| 14 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 15 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 15 #include "webkit/plugins/ppapi/resource_helper.h" | 16 #include "webkit/plugins/ppapi/resource_helper.h" |
| 16 #include "webkit/plugins/ppapi/resource_tracker.h" | 17 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 17 | 18 |
| 18 using base::PlatformFile; | 19 using base::PlatformFile; |
| 19 using base::PlatformFileError; | 20 using base::PlatformFileError; |
| 20 using quota::StorageType; | 21 using quota::StorageType; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 const bool is_will_operation_; | 58 const bool is_will_operation_; |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 class QuotaFileIO::WriteOperation : public PendingOperationBase { | 61 class QuotaFileIO::WriteOperation : public PendingOperationBase { |
| 61 public: | 62 public: |
| 62 WriteOperation(QuotaFileIO* quota_io, | 63 WriteOperation(QuotaFileIO* quota_io, |
| 63 bool is_will_operation, | 64 bool is_will_operation, |
| 64 int64_t offset, | 65 int64_t offset, |
| 65 const char* buffer, | 66 const char* buffer, |
| 66 int32_t bytes_to_write, | 67 int32_t bytes_to_write, |
| 67 WriteCallback* callback) | 68 const WriteCallback& callback) |
| 68 : PendingOperationBase(quota_io, is_will_operation), | 69 : PendingOperationBase(quota_io, is_will_operation), |
| 69 offset_(offset), | 70 offset_(offset), |
| 70 bytes_to_write_(bytes_to_write), | 71 bytes_to_write_(bytes_to_write), |
| 71 callback_(callback), | 72 callback_(callback), |
| 72 finished_(false), | 73 finished_(false), |
| 73 status_(base::PLATFORM_FILE_OK), | 74 status_(base::PLATFORM_FILE_OK), |
| 74 bytes_written_(0), | 75 bytes_written_(0), |
| 75 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 76 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 76 runnable_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 77 ALLOW_THIS_IN_INITIALIZER_LIST(runnable_factory_(this)) { |
| 77 if (!is_will_operation) { | 78 if (!is_will_operation) { |
| 78 // TODO(kinuko): check the API convention if we really need to keep a | 79 // TODO(kinuko): check the API convention if we really need to keep a |
| 79 // copy of the buffer during the async write operations. | 80 // copy of the buffer during the async write operations. |
| 80 buffer_.reset(new char[bytes_to_write]); | 81 buffer_.reset(new char[bytes_to_write]); |
| 81 memcpy(buffer_.get(), buffer, bytes_to_write); | 82 memcpy(buffer_.get(), buffer, bytes_to_write); |
| 82 } | 83 } |
| 83 } | 84 } |
| 84 virtual ~WriteOperation() {} | 85 virtual ~WriteOperation() {} |
| 85 virtual void Run() OVERRIDE { | 86 virtual void Run() OVERRIDE { |
| 86 DCHECK(quota_io_); | 87 DCHECK(quota_io_); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 | 98 |
| 98 PluginDelegate* plugin_delegate = quota_io_->GetPluginDelegate(); | 99 PluginDelegate* plugin_delegate = quota_io_->GetPluginDelegate(); |
| 99 if (!plugin_delegate) { | 100 if (!plugin_delegate) { |
| 100 DidFail(base::PLATFORM_FILE_ERROR_FAILED); | 101 DidFail(base::PLATFORM_FILE_ERROR_FAILED); |
| 101 return; | 102 return; |
| 102 } | 103 } |
| 103 | 104 |
| 104 if (!base::FileUtilProxy::Write( | 105 if (!base::FileUtilProxy::Write( |
| 105 plugin_delegate->GetFileThreadMessageLoopProxy(), | 106 plugin_delegate->GetFileThreadMessageLoopProxy(), |
| 106 quota_io_->file_, offset_, buffer_.get(), bytes_to_write_, | 107 quota_io_->file_, offset_, buffer_.get(), bytes_to_write_, |
| 107 callback_factory_.NewCallback(&WriteOperation::DidFinish))) { | 108 base::Bind(&WriteOperation::DidFinish, |
| 109 weak_factory_.GetWeakPtr()))) { |
| 108 DidFail(base::PLATFORM_FILE_ERROR_FAILED); | 110 DidFail(base::PLATFORM_FILE_ERROR_FAILED); |
| 109 return; | 111 return; |
| 110 } | 112 } |
| 111 } | 113 } |
| 112 | 114 |
| 113 virtual void DidFail(PlatformFileError error) OVERRIDE { | 115 virtual void DidFail(PlatformFileError error) OVERRIDE { |
| 114 DidFinish(error, 0); | 116 DidFinish(error, 0); |
| 115 } | 117 } |
| 116 | 118 |
| 117 bool finished() const { return finished_; } | 119 bool finished() const { return finished_; } |
| 118 | 120 |
| 119 virtual void WillRunCallback() { | 121 virtual void WillRunCallback() { |
| 120 base::MessageLoopProxy::current()->PostTask( | 122 base::MessageLoopProxy::current()->PostTask( |
| 121 FROM_HERE, runnable_factory_.NewRunnableMethod( | 123 FROM_HERE, runnable_factory_.NewRunnableMethod( |
| 122 &WriteOperation::RunCallback)); | 124 &WriteOperation::RunCallback)); |
| 123 } | 125 } |
| 124 | 126 |
| 125 private: | 127 private: |
| 126 void DidFinish(PlatformFileError status, int bytes_written) { | 128 void DidFinish(PlatformFileError status, int bytes_written) { |
| 127 finished_ = true; | 129 finished_ = true; |
| 128 status_ = status; | 130 status_ = status; |
| 129 bytes_written_ = bytes_written; | 131 bytes_written_ = bytes_written; |
| 130 int64_t max_offset = | 132 int64_t max_offset = |
| 131 (status != base::PLATFORM_FILE_OK) ? 0 : offset_ + bytes_written; | 133 (status != base::PLATFORM_FILE_OK) ? 0 : offset_ + bytes_written; |
| 132 // This may delete itself by calling RunCallback. | 134 // This may delete itself by calling RunCallback. |
| 133 quota_io_->DidWrite(this, max_offset); | 135 quota_io_->DidWrite(this, max_offset); |
| 134 } | 136 } |
| 135 | 137 |
| 136 virtual void RunCallback() { | 138 virtual void RunCallback() { |
| 137 DCHECK(callback_.get()); | 139 DCHECK_EQ(false, callback_.is_null()); |
| 138 callback_->Run(status_, bytes_written_); | 140 callback_.Run(status_, bytes_written_); |
| 139 callback_.reset(); | |
| 140 delete this; | 141 delete this; |
| 141 } | 142 } |
| 142 | 143 |
| 143 const int64_t offset_; | 144 const int64_t offset_; |
| 144 scoped_array<char> buffer_; | 145 scoped_array<char> buffer_; |
| 145 const int32_t bytes_to_write_; | 146 const int32_t bytes_to_write_; |
| 146 scoped_ptr<WriteCallback> callback_; | 147 WriteCallback callback_; |
| 147 bool finished_; | 148 bool finished_; |
| 148 PlatformFileError status_; | 149 PlatformFileError status_; |
| 149 int64_t bytes_written_; | 150 int64_t bytes_written_; |
| 150 base::ScopedCallbackFactory<WriteOperation> callback_factory_; | 151 base::WeakPtrFactory<WriteOperation> weak_factory_; |
| 151 ScopedRunnableMethodFactory<WriteOperation> runnable_factory_; | 152 ScopedRunnableMethodFactory<WriteOperation> runnable_factory_; |
| 152 }; | 153 }; |
| 153 | 154 |
| 154 class QuotaFileIO::SetLengthOperation : public PendingOperationBase { | 155 class QuotaFileIO::SetLengthOperation : public PendingOperationBase { |
| 155 public: | 156 public: |
| 156 SetLengthOperation(QuotaFileIO* quota_io, | 157 SetLengthOperation(QuotaFileIO* quota_io, |
| 157 bool is_will_operation, | 158 bool is_will_operation, |
| 158 int64_t length, | 159 int64_t length, |
| 159 StatusCallback* callback) | 160 StatusCallback* callback) |
| 160 : PendingOperationBase(quota_io, is_will_operation), | 161 : PendingOperationBase(quota_io, is_will_operation), |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 QuotaFileIO::~QuotaFileIO() { | 234 QuotaFileIO::~QuotaFileIO() { |
| 234 // Note that this doesn't dispatch pending callbacks. | 235 // Note that this doesn't dispatch pending callbacks. |
| 235 STLDeleteContainerPointers(pending_operations_.begin(), | 236 STLDeleteContainerPointers(pending_operations_.begin(), |
| 236 pending_operations_.end()); | 237 pending_operations_.end()); |
| 237 STLDeleteContainerPointers(pending_callbacks_.begin(), | 238 STLDeleteContainerPointers(pending_callbacks_.begin(), |
| 238 pending_callbacks_.end()); | 239 pending_callbacks_.end()); |
| 239 } | 240 } |
| 240 | 241 |
| 241 bool QuotaFileIO::Write( | 242 bool QuotaFileIO::Write( |
| 242 int64_t offset, const char* buffer, int32_t bytes_to_write, | 243 int64_t offset, const char* buffer, int32_t bytes_to_write, |
| 243 WriteCallback* callback) { | 244 const WriteCallback& callback) { |
| 244 if (bytes_to_write <= 0) { | 245 if (bytes_to_write <= 0) |
| 245 delete callback; | |
| 246 return false; | 246 return false; |
| 247 } | 247 |
| 248 WriteOperation* op = new WriteOperation( | 248 WriteOperation* op = new WriteOperation( |
| 249 this, false, offset, buffer, bytes_to_write, callback); | 249 this, false, offset, buffer, bytes_to_write, callback); |
| 250 return RegisterOperationForQuotaChecks(op); | 250 return RegisterOperationForQuotaChecks(op); |
| 251 } | 251 } |
| 252 | 252 |
| 253 bool QuotaFileIO::SetLength(int64_t length, StatusCallback* callback) { | 253 bool QuotaFileIO::SetLength(int64_t length, StatusCallback* callback) { |
| 254 DCHECK(pending_operations_.empty()); | 254 DCHECK(pending_operations_.empty()); |
| 255 SetLengthOperation* op = new SetLengthOperation( | 255 SetLengthOperation* op = new SetLengthOperation( |
| 256 this, false, length, callback); | 256 this, false, length, callback); |
| 257 return RegisterOperationForQuotaChecks(op); | 257 return RegisterOperationForQuotaChecks(op); |
| 258 } | 258 } |
| 259 | 259 |
| 260 bool QuotaFileIO::WillWrite( | 260 bool QuotaFileIO::WillWrite( |
| 261 int64_t offset, int32_t bytes_to_write, WriteCallback* callback) { | 261 int64_t offset, int32_t bytes_to_write, const WriteCallback& callback) { |
| 262 WriteOperation* op = new WriteOperation( | 262 WriteOperation* op = new WriteOperation( |
| 263 this, true, offset, NULL, bytes_to_write, callback); | 263 this, true, offset, NULL, bytes_to_write, callback); |
| 264 return RegisterOperationForQuotaChecks(op); | 264 return RegisterOperationForQuotaChecks(op); |
| 265 } | 265 } |
| 266 | 266 |
| 267 bool QuotaFileIO::WillSetLength(int64_t length, StatusCallback* callback) { | 267 bool QuotaFileIO::WillSetLength(int64_t length, StatusCallback* callback) { |
| 268 DCHECK(pending_operations_.empty()); | 268 DCHECK(pending_operations_.empty()); |
| 269 SetLengthOperation* op = new SetLengthOperation(this, true, length, callback); | 269 SetLengthOperation* op = new SetLengthOperation(this, true, length, callback); |
| 270 return RegisterOperationForQuotaChecks(op); | 270 return RegisterOperationForQuotaChecks(op); |
| 271 } | 271 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 397 |
| 398 | 398 |
| 399 PluginDelegate* plugin_delegate = GetPluginDelegate(); | 399 PluginDelegate* plugin_delegate = GetPluginDelegate(); |
| 400 if (plugin_delegate) | 400 if (plugin_delegate) |
| 401 plugin_delegate->DidUpdateFile(file_url_, delta); | 401 plugin_delegate->DidUpdateFile(file_url_, delta); |
| 402 inflight_operations_ = 0; | 402 inflight_operations_ = 0; |
| 403 } | 403 } |
| 404 | 404 |
| 405 } // namespace ppapi | 405 } // namespace ppapi |
| 406 } // namespace webkit | 406 } // namespace webkit |
| OLD | NEW |