Chromium Code Reviews| 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_proxy.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "webkit/fileapi/file_system_context.h" | 13 #include "webkit/fileapi/file_system_context.h" |
| 14 #include "webkit/fileapi/file_system_operation.h" | 14 #include "webkit/fileapi/file_system_operation.h" |
| 15 #include "webkit/fileapi/file_system_operation_context.h" | 15 #include "webkit/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/fileapi/file_system_quota_util.h" | 16 #include "webkit/fileapi/file_system_quota_util.h" |
| 17 #include "webkit/fileapi/quota_file_util.h" | 17 #include "webkit/fileapi/quota_file_util.h" |
| 18 | 18 |
| 19 namespace fileapi { | 19 namespace fileapi { |
| 20 | 20 |
| 21 static const int kReadBufSize = 32768; | 21 static const int kReadBufSize = 32768; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 typedef base::Callback<void(base::PlatformFileError /* error code */, | 25 typedef base::Callback<void(base::PlatformFileError /* error code */, |
| 26 const base::PlatformFileInfo& /* file_info */)> | 26 const base::PlatformFileInfo& /* file_info */)> |
| 27 InitializeTaskCallback; | 27 InitializeTaskCallback; |
| 28 | 28 |
| 29 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { | 29 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { |
| 30 public: | 30 public: |
| 31 InitializeTask( | 31 InitializeTask( |
| 32 base::PlatformFile file, | 32 base::PlatformFile file, |
| 33 const FileSystemPath& path, | 33 const FileSystemPath& path, |
| 34 FileSystemOperationContext* context, | 34 FileSystemOperationContext* context, |
| 35 const InitializeTaskCallback& callback) | 35 const InitializeTaskCallback& callback) |
| 36 : origin_message_loop_proxy_( | 36 : origin_task_runner_(base::MessageLoopProxy::current()), |
| 37 base::MessageLoopProxy::current()), | |
| 38 error_code_(base::PLATFORM_FILE_OK), | 37 error_code_(base::PLATFORM_FILE_OK), |
| 39 file_(file), | 38 file_(file), |
| 40 path_(path), | 39 path_(path), |
| 41 context_(*context), | 40 context_(*context), |
| 42 callback_(callback) { | 41 callback_(callback) { |
| 43 DCHECK_EQ(false, callback.is_null()); | 42 DCHECK_EQ(false, callback.is_null()); |
| 44 } | 43 } |
| 45 | 44 |
| 46 bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 45 bool Start(base::TaskRunner* task_runner, |
| 47 const tracked_objects::Location& from_here) { | 46 const tracked_objects::Location& from_here) { |
| 48 return message_loop_proxy->PostTask( | 47 return task_runner->PostTask( |
| 49 from_here, | 48 from_here, |
| 50 base::Bind(&InitializeTask::ProcessOnTargetThread, this)); | 49 base::Bind(&InitializeTask::ProcessOnTargetThread, this)); |
| 51 } | 50 } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 friend class base::RefCountedThreadSafe<InitializeTask>; | 53 friend class base::RefCountedThreadSafe<InitializeTask>; |
| 55 | 54 |
| 56 void RunCallback() { | 55 void RunCallback() { |
| 57 callback_.Run(error_code_, file_info_); | 56 callback_.Run(error_code_, file_info_); |
| 58 } | 57 } |
| 59 | 58 |
| 60 void ProcessOnTargetThread() { | 59 void ProcessOnTargetThread() { |
| 61 DCHECK(context_.file_system_context()); | 60 DCHECK(context_.file_system_context()); |
| 62 FileSystemQuotaUtil* quota_util = context_.file_system_context()-> | 61 FileSystemQuotaUtil* quota_util = context_.file_system_context()-> |
| 63 GetQuotaUtil(path_.type()); | 62 GetQuotaUtil(path_.type()); |
| 64 if (quota_util) { | 63 if (quota_util) { |
| 65 DCHECK(quota_util->proxy()); | 64 DCHECK(quota_util->proxy()); |
| 66 quota_util->proxy()->StartUpdateOrigin(path_.origin(), path_.type()); | 65 quota_util->proxy()->StartUpdateOrigin(path_.origin(), path_.type()); |
| 67 } | 66 } |
| 68 if (!base::GetPlatformFileInfo(file_, &file_info_)) | 67 if (!base::GetPlatformFileInfo(file_, &file_info_)) |
| 69 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; | 68 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; |
| 70 origin_message_loop_proxy_->PostTask( | 69 origin_task_runner_->PostTask( |
| 71 FROM_HERE, | 70 FROM_HERE, |
| 72 base::Bind(&InitializeTask::RunCallback, this)); | 71 base::Bind(&InitializeTask::RunCallback, this)); |
| 73 } | 72 } |
| 74 | 73 |
| 75 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | 74 scoped_refptr<base::TaskRunner> origin_task_runner_; |
|
michaeln
2012/04/27 01:04:03
My vote would be to keep this as a MLP for clarity
kinuko
2012/04/27 10:10:29
Yup, I reverted this change.
| |
| 76 base::PlatformFileError error_code_; | 75 base::PlatformFileError error_code_; |
| 77 | 76 |
| 78 base::PlatformFile file_; | 77 base::PlatformFile file_; |
| 79 FileSystemPath path_; | 78 FileSystemPath path_; |
| 80 FileSystemOperationContext context_; | 79 FileSystemOperationContext context_; |
| 81 InitializeTaskCallback callback_; | 80 InitializeTaskCallback callback_; |
| 82 | 81 |
| 83 base::PlatformFileInfo file_info_; | 82 base::PlatformFileInfo file_info_; |
| 84 }; | 83 }; |
| 85 | 84 |
| 86 } // namespace (anonymous) | 85 } // namespace (anonymous) |
| 87 | 86 |
| 88 FileWriterDelegate::FileWriterDelegate( | 87 FileWriterDelegate::FileWriterDelegate( |
| 89 FileSystemOperation* file_system_operation, | 88 FileSystemOperation* file_system_operation, |
| 90 const FileSystemPath& path, | 89 const FileSystemPath& path, |
| 91 int64 offset, | 90 int64 offset, |
| 92 scoped_refptr<base::MessageLoopProxy> proxy) | 91 base::TaskRunner* task_runner) |
| 93 : file_system_operation_(file_system_operation), | 92 : file_system_operation_(file_system_operation), |
| 94 file_(base::kInvalidPlatformFileValue), | 93 file_(base::kInvalidPlatformFileValue), |
| 95 path_(path), | 94 path_(path), |
| 96 offset_(offset), | 95 offset_(offset), |
| 97 proxy_(proxy), | 96 task_runner_(task_runner), |
| 98 bytes_written_backlog_(0), | 97 bytes_written_backlog_(0), |
| 99 bytes_written_(0), | 98 bytes_written_(0), |
| 100 bytes_read_(0), | 99 bytes_read_(0), |
| 101 total_bytes_written_(0), | 100 total_bytes_written_(0), |
| 102 allowed_bytes_to_write_(0), | 101 allowed_bytes_to_write_(0), |
| 103 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 102 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), |
| 104 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 103 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 105 } | 104 } |
| 106 | 105 |
| 107 FileWriterDelegate::~FileWriterDelegate() { | 106 FileWriterDelegate::~FileWriterDelegate() { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 134 void FileWriterDelegate::Start(base::PlatformFile file, | 133 void FileWriterDelegate::Start(base::PlatformFile file, |
| 135 net::URLRequest* request) { | 134 net::URLRequest* request) { |
| 136 file_ = file; | 135 file_ = file; |
| 137 request_ = request; | 136 request_ = request; |
| 138 | 137 |
| 139 scoped_refptr<InitializeTask> relay = new InitializeTask( | 138 scoped_refptr<InitializeTask> relay = new InitializeTask( |
| 140 file_, path_, | 139 file_, path_, |
| 141 file_system_operation_context(), | 140 file_system_operation_context(), |
| 142 base::Bind(&FileWriterDelegate::OnGetFileInfoAndCallStartUpdate, | 141 base::Bind(&FileWriterDelegate::OnGetFileInfoAndCallStartUpdate, |
| 143 weak_factory_.GetWeakPtr())); | 142 weak_factory_.GetWeakPtr())); |
| 144 relay->Start(proxy_, FROM_HERE); | 143 relay->Start(task_runner_, FROM_HERE); |
| 145 } | 144 } |
| 146 | 145 |
| 147 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, | 146 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, |
| 148 const GURL& new_url, | 147 const GURL& new_url, |
| 149 bool* defer_redirect) { | 148 bool* defer_redirect) { |
| 150 NOTREACHED(); | 149 NOTREACHED(); |
| 151 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 150 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, | 153 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 return; | 195 return; |
| 197 } | 196 } |
| 198 OnDataReceived(bytes_read); | 197 OnDataReceived(bytes_read); |
| 199 } | 198 } |
| 200 | 199 |
| 201 void FileWriterDelegate::Read() { | 200 void FileWriterDelegate::Read() { |
| 202 bytes_written_ = 0; | 201 bytes_written_ = 0; |
| 203 bytes_read_ = 0; | 202 bytes_read_ = 0; |
| 204 if (request_->Read(io_buffer_.get(), io_buffer_->size(), | 203 if (request_->Read(io_buffer_.get(), io_buffer_->size(), |
| 205 &bytes_read_)) { | 204 &bytes_read_)) { |
| 206 MessageLoop::current()->PostTask( | 205 base::MessageLoopProxy::current()->PostTask( |
|
michaeln
2012/04/27 01:04:03
nit: change not really needed (afaict)
kinuko
2012/04/27 10:10:29
This was another not-indended-fix... reverted.
| |
| 207 FROM_HERE, | 206 FROM_HERE, |
| 208 base::Bind(&FileWriterDelegate::OnDataReceived, | 207 base::Bind(&FileWriterDelegate::OnDataReceived, |
| 209 weak_factory_.GetWeakPtr(), bytes_read_)); | 208 weak_factory_.GetWeakPtr(), bytes_read_)); |
| 210 } else if (!request_->status().is_io_pending()) { | 209 } else if (!request_->status().is_io_pending()) { |
| 211 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 210 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 212 } | 211 } |
| 213 } | 212 } |
| 214 | 213 |
| 215 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 214 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 216 bytes_read_ = bytes_read; | 215 bytes_read_ = bytes_read; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 240 int64 bytes_to_write = bytes_read_ - bytes_written_; | 239 int64 bytes_to_write = bytes_read_ - bytes_written_; |
| 241 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_) | 240 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_) |
| 242 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_; | 241 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_; |
| 243 | 242 |
| 244 int write_response = | 243 int write_response = |
| 245 file_stream_->Write(cursor_, | 244 file_stream_->Write(cursor_, |
| 246 static_cast<int>(bytes_to_write), | 245 static_cast<int>(bytes_to_write), |
| 247 base::Bind(&FileWriterDelegate::OnDataWritten, | 246 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 248 weak_factory_.GetWeakPtr())); | 247 weak_factory_.GetWeakPtr())); |
| 249 if (write_response > 0) | 248 if (write_response > 0) |
| 250 MessageLoop::current()->PostTask( | 249 base::MessageLoopProxy::current()->PostTask( |
| 251 FROM_HERE, | 250 FROM_HERE, |
| 252 base::Bind(&FileWriterDelegate::OnDataWritten, | 251 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 253 weak_factory_.GetWeakPtr(), write_response)); | 252 weak_factory_.GetWeakPtr(), write_response)); |
| 254 else if (net::ERR_IO_PENDING != write_response) | 253 else if (net::ERR_IO_PENDING != write_response) |
| 255 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 254 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 256 } | 255 } |
| 257 | 256 |
| 258 void FileWriterDelegate::OnDataWritten(int write_response) { | 257 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 259 if (write_response > 0) { | 258 if (write_response > 0) { |
| 260 OnProgress(write_response, false); | 259 OnProgress(write_response, false); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 | 318 |
| 320 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { | 319 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { |
| 321 DCHECK(file_system_operation_); | 320 DCHECK(file_system_operation_); |
| 322 DCHECK(file_system_operation_->file_system_context()); | 321 DCHECK(file_system_operation_->file_system_context()); |
| 323 DCHECK(file_system_operation_->file_system_operation_context()); | 322 DCHECK(file_system_operation_->file_system_operation_context()); |
| 324 return file_system_operation_->file_system_context()->GetQuotaUtil( | 323 return file_system_operation_->file_system_context()->GetQuotaUtil( |
| 325 path_.type()); | 324 path_.type()); |
| 326 } | 325 } |
| 327 | 326 |
| 328 } // namespace fileapi | 327 } // namespace fileapi |
| OLD | NEW |