| 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 "storage/browser/fileapi/file_writer_delegate.h" | 5 #include "storage/browser/fileapi/file_writer_delegate.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 OnError(base::File::FILE_ERROR_SECURITY); | 85 OnError(base::File::FILE_ERROR_SECURITY); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request, | 88 void FileWriterDelegate::OnSSLCertificateError(net::URLRequest* request, |
| 89 const net::SSLInfo& ssl_info, | 89 const net::SSLInfo& ssl_info, |
| 90 bool fatal) { | 90 bool fatal) { |
| 91 NOTREACHED(); | 91 NOTREACHED(); |
| 92 OnError(base::File::FILE_ERROR_SECURITY); | 92 OnError(base::File::FILE_ERROR_SECURITY); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request) { | 95 void FileWriterDelegate::OnResponseStarted(net::URLRequest* request, |
| 96 int net_error) { |
| 97 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 96 DCHECK_EQ(request_.get(), request); | 98 DCHECK_EQ(request_.get(), request); |
| 97 if (!request->status().is_success() || request->GetResponseCode() != 200) { | 99 |
| 100 if (net_error != net::OK || request->GetResponseCode() != 200) { |
| 98 OnError(base::File::FILE_ERROR_FAILED); | 101 OnError(base::File::FILE_ERROR_FAILED); |
| 99 return; | 102 return; |
| 100 } | 103 } |
| 101 Read(); | 104 Read(); |
| 102 } | 105 } |
| 103 | 106 |
| 104 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request, | 107 void FileWriterDelegate::OnReadCompleted(net::URLRequest* request, |
| 105 int bytes_read) { | 108 int bytes_read) { |
| 109 DCHECK_NE(net::ERR_IO_PENDING, bytes_read); |
| 106 DCHECK_EQ(request_.get(), request); | 110 DCHECK_EQ(request_.get(), request); |
| 107 if (!request->status().is_success()) { | 111 |
| 112 if (bytes_read < 0) { |
| 108 OnError(base::File::FILE_ERROR_FAILED); | 113 OnError(base::File::FILE_ERROR_FAILED); |
| 109 return; | 114 return; |
| 110 } | 115 } |
| 111 OnDataReceived(bytes_read); | 116 OnDataReceived(bytes_read); |
| 112 } | 117 } |
| 113 | 118 |
| 114 void FileWriterDelegate::Read() { | 119 void FileWriterDelegate::Read() { |
| 115 bytes_written_ = 0; | 120 bytes_written_ = 0; |
| 116 bytes_read_ = 0; | 121 bytes_read_ = request_->Read(io_buffer_.get(), io_buffer_->size()); |
| 117 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { | 122 if (bytes_read_ == net::ERR_IO_PENDING) |
| 123 return; |
| 124 |
| 125 if (bytes_read_ >= 0) { |
| 118 base::ThreadTaskRunnerHandle::Get()->PostTask( | 126 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 119 FROM_HERE, base::Bind(&FileWriterDelegate::OnDataReceived, | 127 FROM_HERE, base::Bind(&FileWriterDelegate::OnDataReceived, |
| 120 weak_factory_.GetWeakPtr(), bytes_read_)); | 128 weak_factory_.GetWeakPtr(), bytes_read_)); |
| 121 } else if (!request_->status().is_io_pending()) { | 129 } else { |
| 122 OnError(base::File::FILE_ERROR_FAILED); | 130 OnError(base::File::FILE_ERROR_FAILED); |
| 123 } | 131 } |
| 124 } | 132 } |
| 125 | 133 |
| 126 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 134 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 127 bytes_read_ = bytes_read; | 135 bytes_read_ = bytes_read; |
| 128 if (!bytes_read_) { // We're done. | 136 if (bytes_read == 0) { // We're done. |
| 129 OnProgress(0, true); | 137 OnProgress(0, true); |
| 130 } else { | 138 } else { |
| 131 // This could easily be optimized to rotate between a pool of buffers, so | 139 // This could easily be optimized to rotate between a pool of buffers, so |
| 132 // that we could read and write at the same time. It's not yet clear that | 140 // that we could read and write at the same time. It's not yet clear that |
| 133 // it's necessary. | 141 // it's necessary. |
| 134 cursor_ = new net::DrainableIOBuffer(io_buffer_.get(), bytes_read_); | 142 cursor_ = new net::DrainableIOBuffer(io_buffer_.get(), bytes_read_); |
| 135 Write(); | 143 Write(); |
| 136 } | 144 } |
| 137 } | 145 } |
| 138 | 146 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (error == base::File::FILE_OK && flush_error != net::OK) { | 243 if (error == base::File::FILE_OK && flush_error != net::OK) { |
| 236 // If the Flush introduced an error, overwrite the status. | 244 // If the Flush introduced an error, overwrite the status. |
| 237 // Otherwise, keep the original error status. | 245 // Otherwise, keep the original error status. |
| 238 error = NetErrorToFileError(flush_error); | 246 error = NetErrorToFileError(flush_error); |
| 239 progress_status = GetCompletionStatusOnError(); | 247 progress_status = GetCompletionStatusOnError(); |
| 240 } | 248 } |
| 241 write_callback_.Run(error, bytes_written, progress_status); | 249 write_callback_.Run(error, bytes_written, progress_status); |
| 242 } | 250 } |
| 243 | 251 |
| 244 } // namespace storage | 252 } // namespace storage |
| OLD | NEW |