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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 base::FilePath* temp_file_path = new base::FilePath; | 71 base::FilePath* temp_file_path = new base::FilePath; |
72 base::PostTaskAndReplyWithResult( | 72 base::PostTaskAndReplyWithResult( |
73 file_task_runner_.get(), | 73 file_task_runner_.get(), |
74 FROM_HERE, | 74 FROM_HERE, |
75 base::Bind(&base::CreateTemporaryFile, temp_file_path), | 75 base::Bind(&base::CreateTemporaryFile, temp_file_path), |
76 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, | 76 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, |
77 weak_factory_.GetWeakPtr(), | 77 weak_factory_.GetWeakPtr(), |
78 callback, | 78 callback, |
79 base::Owned(temp_file_path))); | 79 base::Owned(temp_file_path))); |
80 } else { | 80 } else { |
81 result = file_stream_->Open( | 81 result = |
82 file_path_, | 82 file_stream_->Open(file_path_, |
83 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | | 83 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
84 base::File::FLAG_CREATE_ALWAYS, | 84 base::File::FLAG_CREATE_ALWAYS, |
85 base::Bind(&URLFetcherFileWriter::DidOpenFile, | 85 base::Bind(&URLFetcherFileWriter::DidOpenFile, |
86 weak_factory_.GetWeakPtr(), | 86 weak_factory_.GetWeakPtr(), |
87 callback)); | 87 callback)); |
88 DCHECK_NE(OK, result); | 88 DCHECK_NE(OK, result); |
89 } | 89 } |
90 return result; | 90 return result; |
91 } | 91 } |
92 | 92 |
93 int URLFetcherFileWriter::Write(IOBuffer* buffer, | 93 int URLFetcherFileWriter::Write(IOBuffer* buffer, |
94 int num_bytes, | 94 int num_bytes, |
95 const CompletionCallback& callback) { | 95 const CompletionCallback& callback) { |
96 DCHECK(file_stream_); | 96 DCHECK(file_stream_); |
97 DCHECK(owns_file_); | 97 DCHECK(owns_file_); |
98 | 98 |
99 int result = file_stream_->Write(buffer, num_bytes, | 99 int result = file_stream_->Write(buffer, |
| 100 num_bytes, |
100 base::Bind(&URLFetcherFileWriter::DidWrite, | 101 base::Bind(&URLFetcherFileWriter::DidWrite, |
101 weak_factory_.GetWeakPtr(), | 102 weak_factory_.GetWeakPtr(), |
102 callback)); | 103 callback)); |
103 if (result < 0 && result != ERR_IO_PENDING) | 104 if (result < 0 && result != ERR_IO_PENDING) |
104 CloseAndDeleteFile(); | 105 CloseAndDeleteFile(); |
105 | 106 |
106 return result; | 107 return result; |
107 } | 108 } |
108 | 109 |
109 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 110 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { |
110 // If the file_stream_ still exists at this point, close it. | 111 // If the file_stream_ still exists at this point, close it. |
111 if (file_stream_) { | 112 if (file_stream_) { |
112 int result = file_stream_->Close(base::Bind( | 113 int result = |
113 &URLFetcherFileWriter::CloseComplete, | 114 file_stream_->Close(base::Bind(&URLFetcherFileWriter::CloseComplete, |
114 weak_factory_.GetWeakPtr(), callback)); | 115 weak_factory_.GetWeakPtr(), |
| 116 callback)); |
115 if (result != ERR_IO_PENDING) | 117 if (result != ERR_IO_PENDING) |
116 file_stream_.reset(); | 118 file_stream_.reset(); |
117 return result; | 119 return result; |
118 } | 120 } |
119 return OK; | 121 return OK; |
120 } | 122 } |
121 | 123 |
122 URLFetcherFileWriter* URLFetcherFileWriter::AsFileWriter() { | 124 URLFetcherFileWriter* URLFetcherFileWriter::AsFileWriter() { |
123 return this; | 125 return this; |
124 } | 126 } |
(...skipping 30 matching lines...) Expand all Loading... |
155 base::FilePath* temp_file_path, | 157 base::FilePath* temp_file_path, |
156 bool success) { | 158 bool success) { |
157 if (!success) { | 159 if (!success) { |
158 callback.Run(ERR_FILE_NOT_FOUND); | 160 callback.Run(ERR_FILE_NOT_FOUND); |
159 return; | 161 return; |
160 } | 162 } |
161 file_path_ = *temp_file_path; | 163 file_path_ = *temp_file_path; |
162 owns_file_ = true; | 164 owns_file_ = true; |
163 const int result = file_stream_->Open( | 165 const int result = file_stream_->Open( |
164 file_path_, | 166 file_path_, |
165 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | | 167 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | base::File::FLAG_OPEN, |
166 base::File::FLAG_OPEN, | |
167 base::Bind(&URLFetcherFileWriter::DidOpenFile, | 168 base::Bind(&URLFetcherFileWriter::DidOpenFile, |
168 weak_factory_.GetWeakPtr(), | 169 weak_factory_.GetWeakPtr(), |
169 callback)); | 170 callback)); |
170 if (result != ERR_IO_PENDING) | 171 if (result != ERR_IO_PENDING) |
171 DidOpenFile(callback, result); | 172 DidOpenFile(callback, result); |
172 } | 173 } |
173 | 174 |
174 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, | 175 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, |
175 int result) { | 176 int result) { |
176 if (result == OK) | 177 if (result == OK) |
177 owns_file_ = true; | 178 owns_file_ = true; |
178 else | 179 else |
179 CloseAndDeleteFile(); | 180 CloseAndDeleteFile(); |
180 | 181 |
181 callback.Run(result); | 182 callback.Run(result); |
182 } | 183 } |
183 | 184 |
184 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, | 185 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, |
185 int result) { | 186 int result) { |
186 // Destroy |file_stream_| whether or not the close succeeded. | 187 // Destroy |file_stream_| whether or not the close succeeded. |
187 file_stream_.reset(); | 188 file_stream_.reset(); |
188 callback.Run(result); | 189 callback.Run(result); |
189 } | 190 } |
190 | 191 |
191 } // namespace net | 192 } // namespace net |
OLD | NEW |