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/task_runner.h" | 9 #include "base/task_runner.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 } | 84 } |
85 return result; | 85 return result; |
86 } | 86 } |
87 | 87 |
88 int URLFetcherFileWriter::Write(IOBuffer* buffer, | 88 int URLFetcherFileWriter::Write(IOBuffer* buffer, |
89 int num_bytes, | 89 int num_bytes, |
90 const CompletionCallback& callback) { | 90 const CompletionCallback& callback) { |
91 DCHECK(file_stream_); | 91 DCHECK(file_stream_); |
92 DCHECK(owns_file_); | 92 DCHECK(owns_file_); |
93 | 93 |
94 ContinueWrite(new DrainableIOBuffer(buffer, num_bytes), callback, OK); | 94 int result = file_stream_->Write(buffer, num_bytes, |
95 return ERR_IO_PENDING; | 95 base::Bind(&URLFetcherFileWriter::DidWrite, |
96 weak_factory_.GetWeakPtr(), | |
97 callback)); | |
98 DCHECK_LT(result, 0); | |
wtc
2013/05/23 01:00:16
Does this DCHECK mean we expect to get either ERR_
hashimoto
2013/05/23 04:33:24
I thought DCHECK_LT(result, 0) was a good idea bec
| |
99 return result; | |
96 } | 100 } |
97 | 101 |
98 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 102 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { |
99 file_stream_.reset(); | 103 file_stream_.reset(); |
100 return OK; | 104 return OK; |
101 } | 105 } |
102 | 106 |
103 void URLFetcherFileWriter::ContinueWrite( | 107 void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback, |
104 scoped_refptr<DrainableIOBuffer> buffer, | 108 int result) { |
105 const CompletionCallback& callback, | |
106 int result) { | |
107 // |file_stream_| should be alive when write is in progress. | |
108 DCHECK(file_stream_); | |
109 | |
110 if (result < 0) { | 109 if (result < 0) { |
111 error_code_ = result; | 110 error_code_ = result; |
112 CloseAndDeleteFile(); | 111 CloseAndDeleteFile(); |
113 callback.Run(result); | |
114 return; | |
115 } | 112 } |
116 | 113 callback.Run(result); |
117 total_bytes_written_ += result; | |
118 buffer->DidConsume(result); | |
119 | |
120 if (buffer->BytesRemaining() > 0) { | |
121 file_stream_->Write(buffer, buffer->BytesRemaining(), | |
122 base::Bind(&URLFetcherFileWriter::ContinueWrite, | |
123 weak_factory_.GetWeakPtr(), | |
124 buffer, | |
125 callback)); | |
126 } else { | |
127 // Finished writing buffer to the file. | |
128 callback.Run(buffer->size()); | |
129 } | |
130 } | 114 } |
131 | 115 |
132 void URLFetcherFileWriter::DisownFile() { | 116 void URLFetcherFileWriter::DisownFile() { |
133 // Disowning is done by the delegate's OnURLFetchComplete method. | 117 // Disowning is done by the delegate's OnURLFetchComplete method. |
134 // The file should be closed by the time that method is called. | 118 // The file should be closed by the time that method is called. |
135 DCHECK(!file_stream_); | 119 DCHECK(!file_stream_); |
136 | 120 |
137 owns_file_ = false; | 121 owns_file_ = false; |
138 } | 122 } |
139 | 123 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 total_bytes_written_ = 0; | 160 total_bytes_written_ = 0; |
177 owns_file_ = true; | 161 owns_file_ = true; |
178 } else { | 162 } else { |
179 error_code_ = result; | 163 error_code_ = result; |
180 CloseAndDeleteFile(); | 164 CloseAndDeleteFile(); |
181 } | 165 } |
182 callback.Run(result); | 166 callback.Run(result); |
183 } | 167 } |
184 | 168 |
185 } // namespace net | 169 } // namespace net |
OLD | NEW |