| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/fileapi/file_writer_delegate.h" | 5 #include "webkit/fileapi/file_writer_delegate.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 if (!request->status().is_success()) { | 188 if (!request->status().is_success()) { |
| 189 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 189 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 190 return; | 190 return; |
| 191 } | 191 } |
| 192 OnDataReceived(bytes_read); | 192 OnDataReceived(bytes_read); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void FileWriterDelegate::Read() { | 195 void FileWriterDelegate::Read() { |
| 196 bytes_written_ = 0; | 196 bytes_written_ = 0; |
| 197 bytes_read_ = 0; | 197 bytes_read_ = 0; |
| 198 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { | 198 if (request_->Read(io_buffer_.get(), io_buffer_->size(), |
| 199 &bytes_read_)) { |
| 199 MessageLoop::current()->PostTask( | 200 MessageLoop::current()->PostTask( |
| 200 FROM_HERE, | 201 FROM_HERE, |
| 201 base::Bind(&FileWriterDelegate::OnDataReceived, | 202 base::Bind(&FileWriterDelegate::OnDataReceived, |
| 202 weak_factory_.GetWeakPtr(), bytes_read_)); | 203 weak_factory_.GetWeakPtr(), bytes_read_)); |
| 203 } else if (!request_->status().is_io_pending()) { | 204 } else if (!request_->status().is_io_pending()) { |
| 204 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 205 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 205 } | 206 } |
| 206 } | 207 } |
| 207 | 208 |
| 208 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 209 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 209 bytes_read_ = bytes_read; | 210 bytes_read_ = bytes_read; |
| 210 if (!bytes_read_) { // We're done. | 211 if (!bytes_read_) { // We're done. |
| 211 OnProgress(0, true); | 212 OnProgress(0, true); |
| 212 } else { | 213 } else { |
| 213 // This could easily be optimized to rotate between a pool of buffers, so | 214 // This could easily be optimized to rotate between a pool of buffers, so |
| 214 // that we could read and write at the same time. It's not yet clear that | 215 // that we could read and write at the same time. It's not yet clear that |
| 215 // it's necessary. | 216 // it's necessary. |
| 217 cursor_ = new net::DrainableIOBuffer(io_buffer_, bytes_read_); |
| 216 Write(); | 218 Write(); |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 | 221 |
| 220 void FileWriterDelegate::Write() { | 222 void FileWriterDelegate::Write() { |
| 221 // allowed_bytes_to_write could be negative if the file size is | 223 // allowed_bytes_to_write could be negative if the file size is |
| 222 // greater than the current (possibly new) quota. | 224 // greater than the current (possibly new) quota. |
| 223 // (The UI should clear the entire origin data if the smaller quota size | 225 // (The UI should clear the entire origin data if the smaller quota size |
| 224 // is set in general, though the UI/deletion code is not there yet.) | 226 // is set in general, though the UI/deletion code is not there yet.) |
| 225 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || | 227 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || |
| 226 allowed_bytes_to_write_ < 0); | 228 allowed_bytes_to_write_ < 0); |
| 227 if (total_bytes_written_ >= allowed_bytes_to_write_) { | 229 if (total_bytes_written_ >= allowed_bytes_to_write_) { |
| 228 OnError(base::PLATFORM_FILE_ERROR_NO_SPACE); | 230 OnError(base::PLATFORM_FILE_ERROR_NO_SPACE); |
| 229 return; | 231 return; |
| 230 } | 232 } |
| 231 | 233 |
| 232 int64 bytes_to_write = bytes_read_ - bytes_written_; | 234 int64 bytes_to_write = bytes_read_ - bytes_written_; |
| 233 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_) | 235 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_) |
| 234 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_; | 236 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_; |
| 235 | 237 |
| 236 int write_response = | 238 int write_response = |
| 237 file_stream_->Write(io_buffer_->data() + bytes_written_, | 239 file_stream_->Write(cursor_, |
| 238 static_cast<int>(bytes_to_write), | 240 static_cast<int>(bytes_to_write), |
| 239 base::Bind(&FileWriterDelegate::OnDataWritten, | 241 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 240 base::Unretained(this))); | 242 base::Unretained(this))); |
| 241 if (write_response > 0) | 243 if (write_response > 0) |
| 242 MessageLoop::current()->PostTask( | 244 MessageLoop::current()->PostTask( |
| 243 FROM_HERE, | 245 FROM_HERE, |
| 244 base::Bind(&FileWriterDelegate::OnDataWritten, | 246 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 245 weak_factory_.GetWeakPtr(), write_response)); | 247 weak_factory_.GetWeakPtr(), write_response)); |
| 246 else if (net::ERR_IO_PENDING != write_response) | 248 else if (net::ERR_IO_PENDING != write_response) |
| 247 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 249 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 248 } | 250 } |
| 249 | 251 |
| 250 void FileWriterDelegate::OnDataWritten(int write_response) { | 252 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 251 if (write_response > 0) { | 253 if (write_response > 0) { |
| 252 OnProgress(write_response, false); | 254 OnProgress(write_response, false); |
| 255 cursor_->DidConsume(write_response); |
| 253 bytes_written_ += write_response; | 256 bytes_written_ += write_response; |
| 254 total_bytes_written_ += write_response; | 257 total_bytes_written_ += write_response; |
| 255 if (bytes_written_ == bytes_read_) | 258 if (bytes_written_ == bytes_read_) |
| 256 Read(); | 259 Read(); |
| 257 else | 260 else |
| 258 Write(); | 261 Write(); |
| 259 } else { | 262 } else { |
| 260 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 263 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 261 } | 264 } |
| 262 } | 265 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 322 |
| 320 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { | 323 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { |
| 321 DCHECK(file_system_operation_); | 324 DCHECK(file_system_operation_); |
| 322 DCHECK(file_system_operation_->file_system_context()); | 325 DCHECK(file_system_operation_->file_system_context()); |
| 323 DCHECK(file_system_operation_->file_system_operation_context()); | 326 DCHECK(file_system_operation_->file_system_operation_context()); |
| 324 return file_system_operation_->file_system_context()->GetQuotaUtil( | 327 return file_system_operation_->file_system_context()->GetQuotaUtil( |
| 325 file_system_operation_context()->src_type()); | 328 file_system_operation_context()->src_type()); |
| 326 } | 329 } |
| 327 | 330 |
| 328 } // namespace fileapi | 331 } // namespace fileapi |
| OLD | NEW |