| 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" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "webkit/fileapi/file_system_context.h" | 15 #include "webkit/fileapi/file_system_context.h" |
| 16 #include "webkit/fileapi/file_system_operation.h" | 16 #include "webkit/fileapi/file_system_operation.h" |
| 17 #include "webkit/fileapi/file_system_operation_context.h" | 17 #include "webkit/fileapi/file_system_operation_context.h" |
| 18 #include "webkit/fileapi/file_system_quota_util.h" | 18 #include "webkit/fileapi/file_writer.h" |
| 19 | 19 |
| 20 namespace fileapi { | 20 namespace fileapi { |
| 21 | 21 |
| 22 static const int kReadBufSize = 32768; | 22 static const int kReadBufSize = 32768; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 typedef base::Callback<void(base::PlatformFileError /* error code */, | 26 base::PlatformFileError NetErrorToPlatformFileError(int error) { |
| 27 const base::PlatformFileInfo& /* file_info */)> | 27 // TODO(kinuko): Move this static method to more convenient place. |
| 28 InitializeTaskCallback; | 28 switch (error) { |
| 29 case net::ERR_FILE_NO_SPACE: |
| 30 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 31 case net::ERR_FILE_NOT_FOUND: |
| 32 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 33 case net::ERR_ACCESS_DENIED: |
| 34 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 35 default: |
| 36 return base::PLATFORM_FILE_ERROR_FAILED; |
| 37 } |
| 38 } |
| 29 | 39 |
| 30 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { | 40 } // namespace |
| 31 public: | |
| 32 InitializeTask( | |
| 33 base::PlatformFile file, | |
| 34 const InitializeTaskCallback& callback) | |
| 35 : original_loop_(base::MessageLoopProxy::current()), | |
| 36 error_code_(base::PLATFORM_FILE_OK), | |
| 37 file_(file), | |
| 38 callback_(callback) { | |
| 39 DCHECK_EQ(false, callback.is_null()); | |
| 40 } | |
| 41 | |
| 42 bool Start(base::SequencedTaskRunner* task_runner, | |
| 43 const tracked_objects::Location& from_here) { | |
| 44 return task_runner->PostTask( | |
| 45 from_here, | |
| 46 base::Bind(&InitializeTask::ProcessOnTargetThread, this)); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 friend class base::RefCountedThreadSafe<InitializeTask>; | |
| 51 ~InitializeTask() {} | |
| 52 | |
| 53 void RunCallback() { | |
| 54 callback_.Run(error_code_, file_info_); | |
| 55 } | |
| 56 | |
| 57 void ProcessOnTargetThread() { | |
| 58 if (!base::GetPlatformFileInfo(file_, &file_info_)) | |
| 59 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; | |
| 60 original_loop_->PostTask( | |
| 61 FROM_HERE, | |
| 62 base::Bind(&InitializeTask::RunCallback, this)); | |
| 63 } | |
| 64 | |
| 65 scoped_refptr<base::MessageLoopProxy> original_loop_; | |
| 66 base::PlatformFileError error_code_; | |
| 67 | |
| 68 base::PlatformFile file_; | |
| 69 InitializeTaskCallback callback_; | |
| 70 | |
| 71 base::PlatformFileInfo file_info_; | |
| 72 }; | |
| 73 | |
| 74 } // namespace (anonymous) | |
| 75 | 41 |
| 76 FileWriterDelegate::FileWriterDelegate( | 42 FileWriterDelegate::FileWriterDelegate( |
| 77 FileSystemOperation* file_system_operation, | 43 FileSystemOperation* file_system_operation, |
| 78 const FileSystemPath& path, | 44 scoped_ptr<FileWriter> file_writer) |
| 79 int64 offset) | |
| 80 : file_system_operation_(file_system_operation), | 45 : file_system_operation_(file_system_operation), |
| 81 file_(base::kInvalidPlatformFileValue), | 46 file_writer_(file_writer.Pass()), |
| 82 path_(path), | |
| 83 offset_(offset), | |
| 84 has_pending_write_(false), | |
| 85 bytes_written_backlog_(0), | 47 bytes_written_backlog_(0), |
| 86 bytes_written_(0), | 48 bytes_written_(0), |
| 87 bytes_read_(0), | 49 bytes_read_(0), |
| 88 total_bytes_written_(0), | |
| 89 allowed_bytes_to_write_(0), | |
| 90 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 50 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), |
| 91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 51 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 92 } | 52 } |
| 93 | 53 |
| 94 FileWriterDelegate::~FileWriterDelegate() { | 54 FileWriterDelegate::~FileWriterDelegate() { |
| 95 } | 55 } |
| 96 | 56 |
| 97 void FileWriterDelegate::OnGetFileInfoAndStartRequest( | 57 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request) { |
| 98 scoped_ptr<net::URLRequest> request, | |
| 99 base::PlatformFileError error, | |
| 100 const base::PlatformFileInfo& file_info) { | |
| 101 if (error != base::PLATFORM_FILE_OK) { | |
| 102 OnError(error); | |
| 103 return; | |
| 104 } | |
| 105 int64 allowed_bytes_growth = | |
| 106 file_system_operation_context()->allowed_bytes_growth(); | |
| 107 if (allowed_bytes_growth < 0) | |
| 108 allowed_bytes_growth = 0; | |
| 109 int64 overlap = file_info.size - offset_; | |
| 110 allowed_bytes_to_write_ = allowed_bytes_growth; | |
| 111 if (kint64max - overlap > allowed_bytes_growth) | |
| 112 allowed_bytes_to_write_ += overlap; | |
| 113 size_ = file_info.size; | |
| 114 file_stream_.reset(new net::FileStream( | |
| 115 file_, | |
| 116 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
| 117 base::PLATFORM_FILE_ASYNC, | |
| 118 NULL)); | |
| 119 DCHECK(!request_.get()); | |
| 120 request_ = request.Pass(); | 58 request_ = request.Pass(); |
| 121 request_->Start(); | 59 request_->Start(); |
| 122 } | 60 } |
| 123 | 61 |
| 124 void FileWriterDelegate::Start(base::PlatformFile file, | |
| 125 scoped_ptr<net::URLRequest> request) { | |
| 126 file_ = file; | |
| 127 | |
| 128 scoped_refptr<InitializeTask> relay = new InitializeTask( | |
| 129 file_, | |
| 130 base::Bind(&FileWriterDelegate::OnGetFileInfoAndStartRequest, | |
| 131 weak_factory_.GetWeakPtr(), base::Passed(&request))); | |
| 132 relay->Start(file_system_operation_context()->file_task_runner(), FROM_HERE); | |
| 133 } | |
| 134 | |
| 135 bool FileWriterDelegate::Cancel() { | 62 bool FileWriterDelegate::Cancel() { |
| 136 if (request_.get()) { | 63 if (request_.get()) { |
| 137 // This halts any callbacks on this delegate. | 64 // This halts any callbacks on this delegate. |
| 138 request_->set_delegate(NULL); | 65 request_->set_delegate(NULL); |
| 139 request_->Cancel(); | 66 request_->Cancel(); |
| 140 } | 67 } |
| 141 | 68 |
| 142 // Return true to finish immediately if we're not writing. | 69 const int status = file_writer_->Cancel( |
| 143 // Otherwise we'll do the final cleanup in the write callback. | 70 base::Bind(&FileWriterDelegate::OnWriteCancelled, |
| 144 return !has_pending_write_; | 71 weak_factory_.GetWeakPtr())); |
| 72 // Return true to finish immediately if we have no pending writes. |
| 73 // Otherwise we'll do the final cleanup in the Cancel callback. |
| 74 return (status != net::ERR_IO_PENDING); |
| 145 } | 75 } |
| 146 | 76 |
| 147 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, | 77 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, |
| 148 const GURL& new_url, | 78 const GURL& new_url, |
| 149 bool* defer_redirect) { | 79 bool* defer_redirect) { |
| 150 NOTREACHED(); | 80 NOTREACHED(); |
| 151 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 81 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| 152 } | 82 } |
| 153 | 83 |
| 154 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, | 84 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 166 | 96 |
| 167 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request, | 97 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request, |
| 168 const net::SSLInfo& ssl_info, | 98 const net::SSLInfo& ssl_info, |
| 169 bool fatal) { | 99 bool fatal) { |
| 170 NOTREACHED(); | 100 NOTREACHED(); |
| 171 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 101 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| 172 } | 102 } |
| 173 | 103 |
| 174 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) { | 104 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) { |
| 175 DCHECK_EQ(request_.get(), request); | 105 DCHECK_EQ(request_.get(), request); |
| 176 // file_stream_->Seek() blocks the IO thread. | |
| 177 // See http://crbug.com/75548. | |
| 178 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 179 if (!request->status().is_success() || request->GetResponseCode() != 200) { | 106 if (!request->status().is_success() || request->GetResponseCode() != 200) { |
| 180 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 107 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 181 return; | 108 return; |
| 182 } | 109 } |
| 183 int64 error = file_stream_->SeekSync(net::FROM_BEGIN, offset_); | |
| 184 if (error != offset_) { | |
| 185 OnError(base::PLATFORM_FILE_ERROR_FAILED); | |
| 186 return; | |
| 187 } | |
| 188 Read(); | 110 Read(); |
| 189 } | 111 } |
| 190 | 112 |
| 191 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request, | 113 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request, |
| 192 int bytes_read) { | 114 int bytes_read) { |
| 193 DCHECK_EQ(request_.get(), request); | 115 DCHECK_EQ(request_.get(), request); |
| 194 if (!request->status().is_success()) { | 116 if (!request->status().is_success()) { |
| 195 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 117 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 196 return; | 118 return; |
| 197 } | 119 } |
| 198 OnDataReceived(bytes_read); | 120 OnDataReceived(bytes_read); |
| 199 } | 121 } |
| 200 | 122 |
| 201 void FileWriterDelegate::Read() { | 123 void FileWriterDelegate::Read() { |
| 202 bytes_written_ = 0; | 124 bytes_written_ = 0; |
| 203 bytes_read_ = 0; | 125 bytes_read_ = 0; |
| 204 if (request_->Read(io_buffer_.get(), io_buffer_->size(), | 126 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { |
| 205 &bytes_read_)) { | |
| 206 MessageLoop::current()->PostTask( | 127 MessageLoop::current()->PostTask( |
| 207 FROM_HERE, | 128 FROM_HERE, |
| 208 base::Bind(&FileWriterDelegate::OnDataReceived, | 129 base::Bind(&FileWriterDelegate::OnDataReceived, |
| 209 weak_factory_.GetWeakPtr(), bytes_read_)); | 130 weak_factory_.GetWeakPtr(), bytes_read_)); |
| 210 } else if (!request_->status().is_io_pending()) { | 131 } else if (!request_->status().is_io_pending()) { |
| 211 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 132 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 212 } | 133 } |
| 213 } | 134 } |
| 214 | 135 |
| 215 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 136 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 216 bytes_read_ = bytes_read; | 137 bytes_read_ = bytes_read; |
| 217 if (!bytes_read_) { // We're done. | 138 if (!bytes_read_) { // We're done. |
| 218 OnProgress(0, true); | 139 OnProgress(0, true); |
| 219 } else { | 140 } else { |
| 220 // This could easily be optimized to rotate between a pool of buffers, so | 141 // This could easily be optimized to rotate between a pool of buffers, so |
| 221 // that we could read and write at the same time. It's not yet clear that | 142 // that we could read and write at the same time. It's not yet clear that |
| 222 // it's necessary. | 143 // it's necessary. |
| 223 cursor_ = new net::DrainableIOBuffer(io_buffer_, bytes_read_); | 144 cursor_ = new net::DrainableIOBuffer(io_buffer_, bytes_read_); |
| 224 Write(); | 145 Write(); |
| 225 } | 146 } |
| 226 } | 147 } |
| 227 | 148 |
| 228 void FileWriterDelegate::Write() { | 149 void FileWriterDelegate::Write() { |
| 229 // allowed_bytes_to_write could be negative if the file size is | |
| 230 // greater than the current (possibly new) quota. | |
| 231 // (The UI should clear the entire origin data if the smaller quota size | |
| 232 // is set in general, though the UI/deletion code is not there yet.) | |
| 233 DCHECK(total_bytes_written_ <= allowed_bytes_to_write_ || | |
| 234 allowed_bytes_to_write_ < 0); | |
| 235 if (total_bytes_written_ >= allowed_bytes_to_write_) { | |
| 236 OnError(base::PLATFORM_FILE_ERROR_NO_SPACE); | |
| 237 return; | |
| 238 } | |
| 239 | |
| 240 int64 bytes_to_write = bytes_read_ - bytes_written_; | 150 int64 bytes_to_write = bytes_read_ - bytes_written_; |
| 241 if (bytes_to_write > allowed_bytes_to_write_ - total_bytes_written_) | |
| 242 bytes_to_write = allowed_bytes_to_write_ - total_bytes_written_; | |
| 243 | |
| 244 has_pending_write_ = true; | |
| 245 int write_response = | 151 int write_response = |
| 246 file_stream_->Write(cursor_, | 152 file_writer_->Write(cursor_, |
| 247 static_cast<int>(bytes_to_write), | 153 static_cast<int>(bytes_to_write), |
| 248 base::Bind(&FileWriterDelegate::OnDataWritten, | 154 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 249 weak_factory_.GetWeakPtr())); | 155 weak_factory_.GetWeakPtr())); |
| 250 if (write_response > 0) | 156 if (write_response > 0) |
| 251 MessageLoop::current()->PostTask( | 157 MessageLoop::current()->PostTask( |
| 252 FROM_HERE, | 158 FROM_HERE, |
| 253 base::Bind(&FileWriterDelegate::OnDataWritten, | 159 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 254 weak_factory_.GetWeakPtr(), write_response)); | 160 weak_factory_.GetWeakPtr(), write_response)); |
| 255 else if (net::ERR_IO_PENDING != write_response) | 161 else if (net::ERR_IO_PENDING != write_response) |
| 256 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 162 OnError(NetErrorToPlatformFileError(write_response)); |
| 257 } | 163 } |
| 258 | 164 |
| 259 void FileWriterDelegate::OnDataWritten(int write_response) { | 165 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 260 has_pending_write_ = false; | |
| 261 if (write_response > 0) { | 166 if (write_response > 0) { |
| 262 if (request_->status().status() == net::URLRequestStatus::CANCELED) { | |
| 263 OnProgress(write_response, true); | |
| 264 return; | |
| 265 } | |
| 266 OnProgress(write_response, false); | 167 OnProgress(write_response, false); |
| 267 cursor_->DidConsume(write_response); | 168 cursor_->DidConsume(write_response); |
| 268 bytes_written_ += write_response; | 169 bytes_written_ += write_response; |
| 269 total_bytes_written_ += write_response; | |
| 270 if (bytes_written_ == bytes_read_) | 170 if (bytes_written_ == bytes_read_) |
| 271 Read(); | 171 Read(); |
| 272 else | 172 else |
| 273 Write(); | 173 Write(); |
| 274 } else { | 174 } else { |
| 275 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 175 OnError(NetErrorToPlatformFileError(write_response)); |
| 276 } | 176 } |
| 277 } | 177 } |
| 278 | 178 |
| 279 void FileWriterDelegate::OnError(base::PlatformFileError error) { | 179 void FileWriterDelegate::OnError(base::PlatformFileError error) { |
| 280 if (request_.get()) { | 180 if (request_.get()) { |
| 281 request_->set_delegate(NULL); | 181 request_->set_delegate(NULL); |
| 282 request_->Cancel(); | 182 request_->Cancel(); |
| 283 } | 183 } |
| 284 | 184 |
| 285 file_system_operation_->DidWrite(error, 0, true); | 185 file_system_operation_->DidWrite(error, 0, true); |
| 286 } | 186 } |
| 287 | 187 |
| 288 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { | 188 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { |
| 289 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); | 189 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); |
| 290 if (quota_util() && | |
| 291 bytes_written > 0 && | |
| 292 total_bytes_written_ + bytes_written + offset_ > size_) { | |
| 293 int overlapped = 0; | |
| 294 if (total_bytes_written_ + offset_ < size_) | |
| 295 overlapped = size_ - total_bytes_written_ - offset_; | |
| 296 quota_util()->proxy()->UpdateOriginUsage( | |
| 297 file_system_operation_->file_system_context()->quota_manager_proxy(), | |
| 298 path_.origin(), path_.type(), | |
| 299 bytes_written - overlapped); | |
| 300 } | |
| 301 static const int kMinProgressDelayMS = 200; | 190 static const int kMinProgressDelayMS = 200; |
| 302 base::Time currentTime = base::Time::Now(); | 191 base::Time currentTime = base::Time::Now(); |
| 303 if (done || last_progress_event_time_.is_null() || | 192 if (done || last_progress_event_time_.is_null() || |
| 304 (currentTime - last_progress_event_time_).InMilliseconds() > | 193 (currentTime - last_progress_event_time_).InMilliseconds() > |
| 305 kMinProgressDelayMS) { | 194 kMinProgressDelayMS) { |
| 306 bytes_written += bytes_written_backlog_; | 195 bytes_written += bytes_written_backlog_; |
| 307 last_progress_event_time_ = currentTime; | 196 last_progress_event_time_ = currentTime; |
| 308 bytes_written_backlog_ = 0; | 197 bytes_written_backlog_ = 0; |
| 309 if (done && quota_util()) | |
| 310 quota_util()->proxy()->EndUpdateOrigin(path_.origin(), path_.type()); | |
| 311 file_system_operation_->DidWrite( | 198 file_system_operation_->DidWrite( |
| 312 base::PLATFORM_FILE_OK, bytes_written, done); | 199 base::PLATFORM_FILE_OK, bytes_written, done); |
| 313 return; | 200 return; |
| 314 } | 201 } |
| 315 bytes_written_backlog_ += bytes_written; | 202 bytes_written_backlog_ += bytes_written; |
| 316 } | 203 } |
| 317 | 204 |
| 205 void FileWriterDelegate::OnWriteCancelled(int status) { |
| 206 file_system_operation_->DidWrite(base::PLATFORM_FILE_ERROR_ABORT, 0, true); |
| 207 } |
| 208 |
| 318 FileSystemOperationContext* | 209 FileSystemOperationContext* |
| 319 FileWriterDelegate::file_system_operation_context() const { | 210 FileWriterDelegate::file_system_operation_context() const { |
| 320 DCHECK(file_system_operation_); | 211 DCHECK(file_system_operation_); |
| 321 DCHECK(file_system_operation_->file_system_operation_context()); | 212 DCHECK(file_system_operation_->file_system_operation_context()); |
| 322 return file_system_operation_->file_system_operation_context(); | 213 return file_system_operation_->file_system_operation_context(); |
| 323 } | 214 } |
| 324 | 215 |
| 325 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { | |
| 326 DCHECK(file_system_operation_); | |
| 327 DCHECK(file_system_operation_->file_system_context()); | |
| 328 DCHECK(file_system_operation_->file_system_operation_context()); | |
| 329 return file_system_operation_->file_system_context()->GetQuotaUtil( | |
| 330 path_.type()); | |
| 331 } | |
| 332 | |
| 333 } // namespace fileapi | 216 } // namespace fileapi |
| OLD | NEW |