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
3 // found in the LICENSE file.
5 #include "webkit/browser/fileapi/file_writer_delegate.h"
8 #include "base/callback.h"
9 #include "base/files/file_util_proxy.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "net/base/net_errors.h"
15 #include "webkit/browser/fileapi/file_stream_writer.h"
16 #include "webkit/browser/fileapi/file_system_context.h"
17 #include "webkit/common/fileapi/file_system_util.h"
21 static const int kReadBufSize = 32768;
23 FileWriterDelegate::FileWriterDelegate(
24 scoped_ptr<FileStreamWriter> file_stream_writer)
25 : file_stream_writer_(file_stream_writer.Pass()),
26 writing_started_(false),
27 bytes_written_backlog_(0),
30 io_buffer_(new net::IOBufferWithSize(kReadBufSize)),
34 FileWriterDelegate::~FileWriterDelegate() {
37 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request,
38 const DelegateWriteCallback& write_callback) {
39 write_callback_ = write_callback;
40 request_ = request.Pass();
44 void FileWriterDelegate::Cancel() {
46 // This halts any callbacks on this delegate.
47 request_->set_delegate(NULL);
51 const int status = file_stream_writer_->Cancel(
52 base::Bind(&FileWriterDelegate::OnWriteCancelled,
53 weak_factory_.GetWeakPtr()));
54 // Return true to finish immediately if we have no pending writes.
55 // Otherwise we'll do the final cleanup in the Cancel callback.
56 if (status != net::ERR_IO_PENDING) {
57 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0,
58 GetCompletionStatusOnError());
62 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request,
64 bool* defer_redirect) {
66 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
69 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request,
70 net::AuthChallengeInfo* auth_info) {
72 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
75 void FileWriterDelegate::OnCertificateRequested(
76 net::URLRequest* request,
77 net::SSLCertRequestInfo* cert_request_info) {
79 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
82 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request,
83 const net::SSLInfo& ssl_info,
86 OnError(base::PLATFORM_FILE_ERROR_SECURITY);
89 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) {
90 DCHECK_EQ(request_.get(), request);
91 if (!request->status().is_success() || request->GetResponseCode() != 200) {
92 OnError(base::PLATFORM_FILE_ERROR_FAILED);
98 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request,
100 DCHECK_EQ(request_.get(), request);
101 if (!request->status().is_success()) {
102 OnError(base::PLATFORM_FILE_ERROR_FAILED);
105 OnDataReceived(bytes_read);
108 void FileWriterDelegate::Read() {
111 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) {
112 base::MessageLoop::current()->PostTask(
114 base::Bind(&FileWriterDelegate::OnDataReceived,
115 weak_factory_.GetWeakPtr(), bytes_read_));
116 } else if (!request_->status().is_io_pending()) {
117 OnError(base::PLATFORM_FILE_ERROR_FAILED);
121 void FileWriterDelegate::OnDataReceived(int bytes_read) {
122 bytes_read_ = bytes_read;
123 if (!bytes_read_) { // We're done.
126 // This could easily be optimized to rotate between a pool of buffers, so
127 // that we could read and write at the same time. It's not yet clear that
129 cursor_ = new net::DrainableIOBuffer(io_buffer_.get(), bytes_read_);
134 void FileWriterDelegate::Write() {
135 writing_started_ = true;
136 int64 bytes_to_write = bytes_read_ - bytes_written_;
138 file_stream_writer_->Write(cursor_.get(),
139 static_cast<int>(bytes_to_write),
140 base::Bind(&FileWriterDelegate::OnDataWritten,
141 weak_factory_.GetWeakPtr()));
142 if (write_response > 0) {
143 base::MessageLoop::current()->PostTask(
145 base::Bind(&FileWriterDelegate::OnDataWritten,
146 weak_factory_.GetWeakPtr(), write_response));
147 } else if (net::ERR_IO_PENDING != write_response) {
148 OnError(NetErrorToPlatformFileError(write_response));
152 void FileWriterDelegate::OnDataWritten(int write_response) {
153 if (write_response > 0) {
154 OnProgress(write_response, false);
155 cursor_->DidConsume(write_response);
156 bytes_written_ += write_response;
157 if (bytes_written_ == bytes_read_)
162 OnError(NetErrorToPlatformFileError(write_response));
166 FileWriterDelegate::WriteProgressStatus
167 FileWriterDelegate::GetCompletionStatusOnError() const {
168 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED;
171 void FileWriterDelegate::OnError(base::PlatformFileError error) {
173 request_->set_delegate(NULL);
177 if (writing_started_)
178 FlushForCompletion(error, 0, ERROR_WRITE_STARTED);
180 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED);
183 void FileWriterDelegate::OnProgress(int bytes_written, bool done) {
184 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_);
185 static const int kMinProgressDelayMS = 200;
186 base::Time currentTime = base::Time::Now();
187 if (done || last_progress_event_time_.is_null() ||
188 (currentTime - last_progress_event_time_).InMilliseconds() >
189 kMinProgressDelayMS) {
190 bytes_written += bytes_written_backlog_;
191 last_progress_event_time_ = currentTime;
192 bytes_written_backlog_ = 0;
195 FlushForCompletion(base::PLATFORM_FILE_OK, bytes_written,
198 write_callback_.Run(base::PLATFORM_FILE_OK, bytes_written,
203 bytes_written_backlog_ += bytes_written;
206 void FileWriterDelegate::OnWriteCancelled(int status) {
207 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0,
208 GetCompletionStatusOnError());
211 void FileWriterDelegate::FlushForCompletion(
212 base::PlatformFileError error,
214 WriteProgressStatus progress_status) {
215 int flush_error = file_stream_writer_->Flush(
216 base::Bind(&FileWriterDelegate::OnFlushed, weak_factory_.GetWeakPtr(),
217 error, bytes_written, progress_status));
218 if (flush_error != net::ERR_IO_PENDING)
219 OnFlushed(error, bytes_written, progress_status, flush_error);
222 void FileWriterDelegate::OnFlushed(base::PlatformFileError error,
224 WriteProgressStatus progress_status,
226 if (error == base::PLATFORM_FILE_OK && flush_error != net::OK) {
227 // If the Flush introduced an error, overwrite the status.
228 // Otherwise, keep the original error status.
229 error = NetErrorToPlatformFileError(flush_error);
230 progress_status = GetCompletionStatusOnError();
232 write_callback_.Run(error, bytes_written, progress_status);
235 } // namespace fileapi