Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/url_request/url_fetcher_response_writer.h" | 5 #include "net/url_request/url_fetcher_response_writer.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | |
| 7 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 10 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 11 #include "net/base/file_stream.h" | 12 #include "net/base/file_stream.h" |
| 12 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 33 return OK; | 34 return OK; |
| 34 } | 35 } |
| 35 | 36 |
| 36 int URLFetcherStringWriter::Write(IOBuffer* buffer, | 37 int URLFetcherStringWriter::Write(IOBuffer* buffer, |
| 37 int num_bytes, | 38 int num_bytes, |
| 38 const CompletionCallback& callback) { | 39 const CompletionCallback& callback) { |
| 39 data_.append(buffer->data(), num_bytes); | 40 data_.append(buffer->data(), num_bytes); |
| 40 return num_bytes; | 41 return num_bytes; |
| 41 } | 42 } |
| 42 | 43 |
| 43 int URLFetcherStringWriter::Finish(const CompletionCallback& callback) { | 44 int URLFetcherStringWriter::Finish(int net_error, |
| 45 const CompletionCallback& callback) { | |
| 44 // Do nothing. | 46 // Do nothing. |
| 45 return OK; | 47 return OK; |
| 46 } | 48 } |
| 47 | 49 |
| 48 URLFetcherStringWriter* URLFetcherStringWriter::AsStringWriter() { | 50 URLFetcherStringWriter* URLFetcherStringWriter::AsStringWriter() { |
| 49 return this; | 51 return this; |
| 50 } | 52 } |
| 51 | 53 |
| 52 URLFetcherFileWriter::URLFetcherFileWriter( | 54 URLFetcherFileWriter::URLFetcherFileWriter( |
| 53 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 55 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 54 const base::FilePath& file_path) | 56 const base::FilePath& file_path) |
| 55 : file_task_runner_(file_task_runner), | 57 : file_task_runner_(file_task_runner), |
| 56 file_path_(file_path), | 58 file_path_(file_path), |
| 57 owns_file_(false), | 59 owns_file_(false), |
| 58 weak_factory_(this) { | 60 weak_factory_(this) { |
| 59 DCHECK(file_task_runner_.get()); | 61 DCHECK(file_task_runner_.get()); |
| 60 } | 62 } |
| 61 | 63 |
| 62 URLFetcherFileWriter::~URLFetcherFileWriter() { | 64 URLFetcherFileWriter::~URLFetcherFileWriter() { |
| 63 CloseAndDeleteFile(); | 65 CloseAndDeleteFile(); |
| 64 } | 66 } |
| 65 | 67 |
| 66 int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) { | 68 int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) { |
| 67 file_stream_.reset(new FileStream(file_task_runner_)); | 69 file_stream_.reset(new FileStream(file_task_runner_)); |
|
mmenke
2016/10/19 20:49:34
DCHECK(!callback_);? Same for all of these, excep
xunjieli
2016/10/20 15:57:49
Done.
| |
| 68 | 70 |
| 69 int result = ERR_IO_PENDING; | 71 int result = ERR_IO_PENDING; |
| 72 owns_file_ = true; | |
| 70 if (file_path_.empty()) { | 73 if (file_path_.empty()) { |
| 71 base::FilePath* temp_file_path = new base::FilePath; | 74 base::FilePath* temp_file_path = new base::FilePath; |
| 72 base::PostTaskAndReplyWithResult( | 75 base::PostTaskAndReplyWithResult( |
| 73 file_task_runner_.get(), | 76 file_task_runner_.get(), |
| 74 FROM_HERE, | 77 FROM_HERE, |
| 75 base::Bind(&base::CreateTemporaryFile, temp_file_path), | 78 base::Bind(&base::CreateTemporaryFile, temp_file_path), |
| 76 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, | 79 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, |
| 77 weak_factory_.GetWeakPtr(), | 80 weak_factory_.GetWeakPtr(), |
| 78 callback, | |
| 79 base::Owned(temp_file_path))); | 81 base::Owned(temp_file_path))); |
| 80 } else { | 82 } else { |
| 81 result = file_stream_->Open( | 83 result = file_stream_->Open( |
| 82 file_path_, | 84 file_path_, |
| 83 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | | 85 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
| 84 base::File::FLAG_CREATE_ALWAYS, | 86 base::File::FLAG_CREATE_ALWAYS, |
| 85 base::Bind(&URLFetcherFileWriter::DidOpenFile, | 87 base::Bind(&URLFetcherFileWriter::OnIOCompleted, |
| 86 weak_factory_.GetWeakPtr(), | 88 weak_factory_.GetWeakPtr())); |
| 87 callback)); | |
| 88 DCHECK_NE(OK, result); | 89 DCHECK_NE(OK, result); |
| 89 } | 90 } |
| 91 | |
| 92 if (result == ERR_IO_PENDING) { | |
| 93 callback_ = callback; | |
| 94 return result; | |
| 95 } | |
| 96 if (result < 0) | |
| 97 CloseAndDeleteFile(); | |
| 90 return result; | 98 return result; |
| 91 } | 99 } |
| 92 | 100 |
| 93 int URLFetcherFileWriter::Write(IOBuffer* buffer, | 101 int URLFetcherFileWriter::Write(IOBuffer* buffer, |
| 94 int num_bytes, | 102 int num_bytes, |
| 95 const CompletionCallback& callback) { | 103 const CompletionCallback& callback) { |
| 96 DCHECK(file_stream_); | 104 DCHECK(file_stream_); |
| 97 DCHECK(owns_file_); | 105 DCHECK(owns_file_); |
| 98 | 106 |
| 99 int result = file_stream_->Write(buffer, num_bytes, | 107 int result = file_stream_->Write(buffer, num_bytes, |
| 100 base::Bind(&URLFetcherFileWriter::DidWrite, | 108 base::Bind(&URLFetcherFileWriter::OnIOComplet ed, |
|
mmenke
2016/10/19 20:49:34
nit: Line too long, run git cl format.
xunjieli
2016/10/20 15:57:49
Done.
| |
| 101 weak_factory_.GetWeakPtr(), | 109 weak_factory_.GetWeakPtr())); |
| 102 callback)); | 110 if (result == ERR_IO_PENDING) { |
| 103 if (result < 0 && result != ERR_IO_PENDING) | 111 callback_ = callback; |
| 112 return result; | |
| 113 } | |
| 114 if (result < 0) | |
| 104 CloseAndDeleteFile(); | 115 CloseAndDeleteFile(); |
| 105 | |
| 106 return result; | 116 return result; |
| 107 } | 117 } |
| 108 | 118 |
| 109 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 119 int URLFetcherFileWriter::Finish(int net_error, |
| 120 const CompletionCallback& callback) { | |
| 121 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 122 // If an error occurred, simply delete the file after any pending operation | |
| 123 // is done. Do not call file_stream_->Close() because there might be an | |
| 124 // operation pending. See crbug.com/487732. | |
| 125 if (net_error < 0) { | |
| 126 // Cancel any pending callback. | |
| 127 callback_.Reset(); | |
| 128 CloseAndDeleteFile(); | |
|
mmenke
2016/10/19 20:49:34
BUG: If Finish() is called with an error when the
xunjieli
2016/10/20 15:57:49
Done. I haven't thought of that. That indeed can h
| |
| 129 return OK; | |
| 130 } | |
|
mmenke
2016/10/19 20:49:34
DCHECK(!callback_);
xunjieli
2016/10/20 15:57:49
Done.
| |
| 110 // If the file_stream_ still exists at this point, close it. | 131 // If the file_stream_ still exists at this point, close it. |
| 111 if (file_stream_) { | 132 if (file_stream_) { |
| 112 int result = file_stream_->Close(base::Bind( | 133 int result = file_stream_->Close(base::Bind( |
| 113 &URLFetcherFileWriter::CloseComplete, | 134 &URLFetcherFileWriter::CloseComplete, weak_factory_.GetWeakPtr())); |
| 114 weak_factory_.GetWeakPtr(), callback)); | 135 if (result == ERR_IO_PENDING) { |
| 115 if (result != ERR_IO_PENDING) | 136 callback_ = callback; |
| 116 file_stream_.reset(); | 137 return result; |
| 138 } | |
| 139 file_stream_.reset(); | |
| 117 return result; | 140 return result; |
| 118 } | 141 } |
| 119 return OK; | 142 return OK; |
| 120 } | 143 } |
| 121 | 144 |
| 122 URLFetcherFileWriter* URLFetcherFileWriter::AsFileWriter() { | 145 URLFetcherFileWriter* URLFetcherFileWriter::AsFileWriter() { |
| 123 return this; | 146 return this; |
| 124 } | 147 } |
| 125 | 148 |
| 126 void URLFetcherFileWriter::DisownFile() { | 149 void URLFetcherFileWriter::DisownFile() { |
| 127 // Disowning is done by the delegate's OnURLFetchComplete method. | 150 // Disowning is done by the delegate's OnURLFetchComplete method. |
| 128 // The file should be closed by the time that method is called. | 151 // The file should be closed by the time that method is called. |
| 129 DCHECK(!file_stream_); | 152 DCHECK(!file_stream_); |
| 130 | 153 |
| 131 owns_file_ = false; | 154 owns_file_ = false; |
| 132 } | 155 } |
| 133 | 156 |
| 134 void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback, | |
| 135 int result) { | |
| 136 if (result < 0) | |
| 137 CloseAndDeleteFile(); | |
| 138 | |
| 139 callback.Run(result); | |
| 140 } | |
| 141 | |
| 142 void URLFetcherFileWriter::CloseAndDeleteFile() { | 157 void URLFetcherFileWriter::CloseAndDeleteFile() { |
| 143 if (!owns_file_) | 158 if (!owns_file_) |
| 144 return; | 159 return; |
| 145 | 160 |
| 146 file_stream_.reset(); | 161 file_stream_.reset(); |
| 147 DisownFile(); | 162 DisownFile(); |
| 148 file_task_runner_->PostTask(FROM_HERE, | 163 file_task_runner_->PostTask(FROM_HERE, |
| 149 base::Bind(base::IgnoreResult(&base::DeleteFile), | 164 base::Bind(base::IgnoreResult(&base::DeleteFile), |
| 150 file_path_, | 165 file_path_, |
| 151 false /* recursive */)); | 166 false /* recursive */)); |
| 152 } | 167 } |
| 153 | 168 |
| 154 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback, | 169 void URLFetcherFileWriter::DidCreateTempFile(base::FilePath* temp_file_path, |
| 155 base::FilePath* temp_file_path, | |
| 156 bool success) { | 170 bool success) { |
| 157 if (!success) { | 171 if (!success) { |
| 158 callback.Run(ERR_FILE_NOT_FOUND); | 172 OnIOCompleted(ERR_FILE_NOT_FOUND); |
| 159 return; | 173 return; |
| 160 } | 174 } |
| 161 file_path_ = *temp_file_path; | 175 file_path_ = *temp_file_path; |
| 162 owns_file_ = true; | |
| 163 const int result = file_stream_->Open( | 176 const int result = file_stream_->Open( |
| 164 file_path_, | 177 file_path_, |
| 165 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | | 178 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
| 166 base::File::FLAG_OPEN, | 179 base::File::FLAG_OPEN, |
| 167 base::Bind(&URLFetcherFileWriter::DidOpenFile, | 180 base::Bind(&URLFetcherFileWriter::OnIOCompleted, |
| 168 weak_factory_.GetWeakPtr(), | 181 weak_factory_.GetWeakPtr())); |
| 169 callback)); | |
| 170 if (result != ERR_IO_PENDING) | 182 if (result != ERR_IO_PENDING) |
| 171 DidOpenFile(callback, result); | 183 OnIOCompleted(result); |
| 172 } | 184 } |
| 173 | 185 |
| 174 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, | 186 void URLFetcherFileWriter::OnIOCompleted(int result) { |
| 175 int result) { | 187 if (result < OK) |
| 176 if (result == OK) | |
| 177 owns_file_ = true; | |
| 178 else | |
| 179 CloseAndDeleteFile(); | 188 CloseAndDeleteFile(); |
| 180 | 189 |
| 181 callback.Run(result); | 190 if (!callback_.is_null()) |
| 191 base::ResetAndReturn(&callback_).Run(result); | |
| 182 } | 192 } |
| 183 | 193 |
| 184 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, | 194 void URLFetcherFileWriter::CloseComplete(int result) { |
| 185 int result) { | |
| 186 // Destroy |file_stream_| whether or not the close succeeded. | 195 // Destroy |file_stream_| whether or not the close succeeded. |
| 187 file_stream_.reset(); | 196 file_stream_.reset(); |
| 188 callback.Run(result); | 197 if (!callback_.is_null()) |
| 198 base::ResetAndReturn(&callback_).Run(result); | |
| 189 } | 199 } |
| 190 | 200 |
| 191 } // namespace net | 201 } // namespace net |
| OLD | NEW |