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 "webkit/fileapi/local_file_stream_writer.h" | 5 #include "webkit/fileapi/local_file_stream_writer.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "net/base/file_stream.h" | 9 #include "net/base/file_stream.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 int LocalFileStreamWriter::Cancel(const net::CompletionCallback& callback) { | 58 int LocalFileStreamWriter::Cancel(const net::CompletionCallback& callback) { |
59 if (!has_pending_operation_) | 59 if (!has_pending_operation_) |
60 return net::ERR_UNEXPECTED; | 60 return net::ERR_UNEXPECTED; |
61 | 61 |
62 DCHECK(!callback.is_null()); | 62 DCHECK(!callback.is_null()); |
63 cancel_callback_ = callback; | 63 cancel_callback_ = callback; |
64 return net::ERR_IO_PENDING; | 64 return net::ERR_IO_PENDING; |
65 } | 65 } |
66 | 66 |
| 67 int LocalFileStreamWriter::Flush(const net::CompletionCallback& callback) { |
| 68 DCHECK(!has_pending_operation_); |
| 69 DCHECK(cancel_callback_.is_null()); |
| 70 |
| 71 // Write() is not called yet, so there's nothing to flush. |
| 72 if (!stream_impl_.get()) |
| 73 return net::OK; |
| 74 |
| 75 has_pending_operation_ = true; |
| 76 int result = InitiateFlush(callback); |
| 77 if (result != net::ERR_IO_PENDING) |
| 78 has_pending_operation_ = false; |
| 79 return result; |
| 80 } |
| 81 |
67 int LocalFileStreamWriter::InitiateOpen( | 82 int LocalFileStreamWriter::InitiateOpen( |
68 const net::CompletionCallback& error_callback, | 83 const net::CompletionCallback& error_callback, |
69 const base::Closure& main_operation) { | 84 const base::Closure& main_operation) { |
70 DCHECK(has_pending_operation_); | 85 DCHECK(has_pending_operation_); |
71 DCHECK(!stream_impl_.get()); | 86 DCHECK(!stream_impl_.get()); |
72 | 87 |
73 stream_impl_.reset(new net::FileStream(NULL)); | 88 stream_impl_.reset(new net::FileStream(NULL)); |
74 return stream_impl_->Open(file_path_, | 89 return stream_impl_->Open(file_path_, |
75 kOpenFlagsForWrite, | 90 kOpenFlagsForWrite, |
76 base::Bind(&LocalFileStreamWriter::DidOpen, | 91 base::Bind(&LocalFileStreamWriter::DidOpen, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 void LocalFileStreamWriter::DidWrite(const net::CompletionCallback& callback, | 187 void LocalFileStreamWriter::DidWrite(const net::CompletionCallback& callback, |
173 int result) { | 188 int result) { |
174 DCHECK(has_pending_operation_); | 189 DCHECK(has_pending_operation_); |
175 | 190 |
176 if (CancelIfRequested()) | 191 if (CancelIfRequested()) |
177 return; | 192 return; |
178 has_pending_operation_ = false; | 193 has_pending_operation_ = false; |
179 callback.Run(result); | 194 callback.Run(result); |
180 } | 195 } |
181 | 196 |
| 197 int LocalFileStreamWriter::InitiateFlush( |
| 198 const net::CompletionCallback& callback) { |
| 199 DCHECK(has_pending_operation_); |
| 200 DCHECK(stream_impl_.get()); |
| 201 |
| 202 return stream_impl_->Flush(base::Bind(&LocalFileStreamWriter::DidFlush, |
| 203 weak_factory_.GetWeakPtr(), |
| 204 callback)); |
| 205 } |
| 206 |
| 207 void LocalFileStreamWriter::DidFlush(const net::CompletionCallback& callback, |
| 208 int result) { |
| 209 DCHECK(has_pending_operation_); |
| 210 |
| 211 if (CancelIfRequested()) |
| 212 return; |
| 213 has_pending_operation_ = false; |
| 214 callback.Run(result); |
| 215 } |
| 216 |
182 bool LocalFileStreamWriter::CancelIfRequested() { | 217 bool LocalFileStreamWriter::CancelIfRequested() { |
183 DCHECK(has_pending_operation_); | 218 DCHECK(has_pending_operation_); |
184 | 219 |
185 if (cancel_callback_.is_null()) | 220 if (cancel_callback_.is_null()) |
186 return false; | 221 return false; |
187 | 222 |
188 net::CompletionCallback pending_cancel = cancel_callback_; | 223 net::CompletionCallback pending_cancel = cancel_callback_; |
189 has_pending_operation_ = false; | 224 has_pending_operation_ = false; |
190 cancel_callback_.Reset(); | 225 cancel_callback_.Reset(); |
191 pending_cancel.Run(net::OK); | 226 pending_cancel.Run(net::OK); |
192 return true; | 227 return true; |
193 } | 228 } |
194 | 229 |
195 } // namespace fileapi | 230 } // namespace fileapi |
OLD | NEW |