Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_fetcher_response_writer.h" |
| 6 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 7 | #include "base/file_util.h" |
| 8 | #include "base/location.h" |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 9 | #include "base/sequenced_task_runner.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 10 | #include "base/task_runner_util.h" |
| 11 | #include "net/base/file_stream.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "net/base/io_buffer.h" |
| 13 | #include "net/base/net_errors.h" |
| 14 | |
| 15 | namespace net { |
| 16 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 17 | URLFetcherStringWriter* URLFetcherResponseWriter::AsStringWriter() { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | URLFetcherFileWriter* URLFetcherResponseWriter::AsFileWriter() { |
| 22 | return NULL; |
| 23 | } |
| 24 | |
| 25 | URLFetcherStringWriter::URLFetcherStringWriter() { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | URLFetcherStringWriter::~URLFetcherStringWriter() { |
| 29 | } |
| 30 | |
| 31 | int URLFetcherStringWriter::Initialize(const CompletionCallback& callback) { |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 32 | data_.clear(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | return OK; |
| 34 | } |
| 35 | |
| 36 | int URLFetcherStringWriter::Write(IOBuffer* buffer, |
| 37 | int num_bytes, |
| 38 | const CompletionCallback& callback) { |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 39 | data_.append(buffer->data(), num_bytes); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 40 | return num_bytes; |
| 41 | } |
| 42 | |
| 43 | int URLFetcherStringWriter::Finish(const CompletionCallback& callback) { |
| 44 | // Do nothing. |
| 45 | return OK; |
| 46 | } |
| 47 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 48 | URLFetcherStringWriter* URLFetcherStringWriter::AsStringWriter() { |
| 49 | return this; |
| 50 | } |
| 51 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 52 | URLFetcherFileWriter::URLFetcherFileWriter( |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 53 | scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 54 | const base::FilePath& file_path) |
Torne (Richard Coles) | cedac22 | 2014-06-03 10:58:34 +0100 | [diff] [blame] | 55 | : file_task_runner_(file_task_runner), |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 56 | file_path_(file_path), |
Torne (Richard Coles) | cedac22 | 2014-06-03 10:58:34 +0100 | [diff] [blame] | 57 | owns_file_(false), |
| 58 | weak_factory_(this) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 59 | DCHECK(file_task_runner_.get()); |
| 60 | } |
| 61 | |
| 62 | URLFetcherFileWriter::~URLFetcherFileWriter() { |
| 63 | CloseAndDeleteFile(); |
| 64 | } |
| 65 | |
| 66 | int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) { |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 67 | file_stream_.reset(new FileStream(file_task_runner_)); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 68 | |
| 69 | int result = ERR_IO_PENDING; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 70 | if (file_path_.empty()) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 71 | base::FilePath* temp_file_path = new base::FilePath; |
| 72 | base::PostTaskAndReplyWithResult( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 73 | file_task_runner_.get(), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 74 | FROM_HERE, |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 75 | base::Bind(&base::CreateTemporaryFile, temp_file_path), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 76 | base::Bind(&URLFetcherFileWriter::DidCreateTempFile, |
| 77 | weak_factory_.GetWeakPtr(), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 78 | callback, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 79 | base::Owned(temp_file_path))); |
| 80 | } else { |
| 81 | result = file_stream_->Open( |
| 82 | file_path_, |
Torne (Richard Coles) | 23730a6 | 2014-03-21 14:25:57 +0000 | [diff] [blame] | 83 | base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
| 84 | base::File::FLAG_CREATE_ALWAYS, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 85 | base::Bind(&URLFetcherFileWriter::DidOpenFile, |
| 86 | weak_factory_.GetWeakPtr(), |
| 87 | callback)); |
| 88 | DCHECK_NE(OK, result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 89 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 90 | return result; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | int URLFetcherFileWriter::Write(IOBuffer* buffer, |
| 94 | int num_bytes, |
| 95 | const CompletionCallback& callback) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 96 | DCHECK(file_stream_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 97 | DCHECK(owns_file_); |
| 98 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 99 | int result = file_stream_->Write(buffer, num_bytes, |
| 100 | base::Bind(&URLFetcherFileWriter::DidWrite, |
| 101 | weak_factory_.GetWeakPtr(), |
| 102 | callback)); |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 103 | if (result < 0 && result != ERR_IO_PENDING) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 104 | CloseAndDeleteFile(); |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 105 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 106 | return result; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 110 | // If the file_stream_ still exists at this point, close it. |
| 111 | if (file_stream_) { |
| 112 | int result = file_stream_->Close(base::Bind( |
| 113 | &URLFetcherFileWriter::CloseComplete, |
| 114 | weak_factory_.GetWeakPtr(), callback)); |
| 115 | if (result != ERR_IO_PENDING) |
| 116 | file_stream_.reset(); |
| 117 | return result; |
| 118 | } |
| 119 | return OK; |
Torne (Richard Coles) | 58537e2 | 2013-09-12 12:10:22 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 122 | URLFetcherFileWriter* URLFetcherFileWriter::AsFileWriter() { |
| 123 | return this; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void URLFetcherFileWriter::DisownFile() { |
| 127 | // Disowning is done by the delegate's OnURLFetchComplete method. |
| 128 | // The file should be closed by the time that method is called. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 129 | DCHECK(!file_stream_); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | |
| 131 | owns_file_ = false; |
| 132 | } |
| 133 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 134 | void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback, |
| 135 | int result) { |
| 136 | if (result < 0) |
| 137 | CloseAndDeleteFile(); |
| 138 | |
| 139 | callback.Run(result); |
| 140 | } |
| 141 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 142 | void URLFetcherFileWriter::CloseAndDeleteFile() { |
| 143 | if (!owns_file_) |
| 144 | return; |
| 145 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 146 | file_stream_.reset(); |
| 147 | DisownFile(); |
| 148 | file_task_runner_->PostTask(FROM_HERE, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 149 | base::Bind(base::IgnoreResult(&base::DeleteFile), |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 150 | file_path_, |
| 151 | false /* recursive */)); |
| 152 | } |
| 153 | |
| 154 | void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback, |
| 155 | base::FilePath* temp_file_path, |
| 156 | bool success) { |
| 157 | if (!success) { |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 158 | callback.Run(ERR_FILE_NOT_FOUND); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 161 | file_path_ = *temp_file_path; |
| 162 | owns_file_ = true; |
| 163 | const int result = file_stream_->Open( |
| 164 | file_path_, |
Torne (Richard Coles) | 23730a6 | 2014-03-21 14:25:57 +0000 | [diff] [blame] | 165 | base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
| 166 | base::File::FLAG_OPEN, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 167 | base::Bind(&URLFetcherFileWriter::DidOpenFile, |
| 168 | weak_factory_.GetWeakPtr(), |
| 169 | callback)); |
| 170 | if (result != ERR_IO_PENDING) |
| 171 | DidOpenFile(callback, result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 174 | void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, |
| 175 | int result) { |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 176 | if (result == OK) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 177 | owns_file_ = true; |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 178 | else |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 179 | CloseAndDeleteFile(); |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 180 | |
| 181 | callback.Run(result); |
| 182 | } |
| 183 | |
| 184 | void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, |
| 185 | int result) { |
| 186 | // Destroy |file_stream_| whether or not the close succeeded. |
| 187 | file_stream_.reset(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 188 | callback.Run(result); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // namespace net |