OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/fileapi/local_file_writer.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/location.h" | |
9 #include "net/base/file_stream.h" | |
10 #include "net/base/io_buffer.h" | |
11 | |
12 namespace fileapi { | |
13 | |
14 namespace { | |
15 | |
16 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN | | |
17 base::PLATFORM_FILE_WRITE | | |
18 base::PLATFORM_FILE_ASYNC; | |
19 | |
20 void EmptyCompletionCallback(int) {} | |
21 | |
22 } // anonymous namespace | |
kinuko
2012/04/20 11:26:37
nit: just 'namespace' would be ok (and more common
kinaba
2012/04/23 08:56:41
Done.
| |
23 | |
24 LocalFileWriter::LocalFileWriter(const FilePath& file_path) | |
25 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
26 file_path_(file_path), | |
27 has_pending_operation_(false) {} | |
28 | |
29 LocalFileWriter::~LocalFileWriter() { | |
30 DCHECK(!has_pending_operation_); | |
31 DCHECK(cancel_callback_.is_null()); | |
32 | |
33 weak_factory_.InvalidateWeakPtrs(); | |
34 } | |
35 | |
36 int LocalFileWriter::Write(net::IOBuffer* buf, int buf_len, | |
37 const net::CompletionCallback& callback) { | |
38 DCHECK(!has_pending_operation_); | |
39 DCHECK(cancel_callback_.is_null()); | |
40 | |
41 has_pending_operation_ = true; | |
42 if (stream_impl_.get()) { | |
43 int result = InitiateWrite(buf, buf_len, callback); | |
44 if (result != net::ERR_IO_PENDING) | |
45 has_pending_operation_ = false; | |
46 return result; | |
47 } | |
48 return InitiateOpen(base::Bind(&LocalFileWriter::ReadyToWrite, | |
49 weak_factory_.GetWeakPtr(), | |
50 make_scoped_refptr(buf), buf_len, callback)); | |
51 } | |
52 | |
53 int LocalFileWriter::Seek(int64 offset, | |
54 const net::Int64CompletionCallback& callback) { | |
55 DCHECK(!has_pending_operation_); | |
56 DCHECK(cancel_callback_.is_null()); | |
57 | |
58 has_pending_operation_ = true; | |
59 if (stream_impl_.get()) { | |
60 int result = InitiateSeek(offset, callback); | |
61 if (result != net::ERR_IO_PENDING) | |
62 has_pending_operation_ = false; | |
63 return result; | |
64 } | |
65 return InitiateOpen(base::Bind(&LocalFileWriter::ReadyToSeek, | |
66 weak_factory_.GetWeakPtr(), offset, callback)); | |
67 } | |
68 | |
69 void LocalFileWriter::Cancel(const net::CompletionCallback& callback) { | |
70 DCHECK(has_pending_operation_); | |
kinuko
2012/04/20 11:26:37
Could we simply run the callback (asynchronously)
kinaba
2012/04/23 08:56:41
I agree. Done.
| |
71 DCHECK(cancel_callback_.is_null()); | |
72 | |
73 DCHECK(!callback.is_null()); | |
74 cancel_callback_ = callback; | |
75 } | |
76 | |
77 int LocalFileWriter::InitiateOpen( | |
78 const net::CompletionCallback& main_operation) { | |
79 DCHECK(has_pending_operation_); | |
80 DCHECK(!stream_impl_.get()); | |
81 | |
82 stream_impl_.reset(new net::FileStream(NULL)); | |
83 return stream_impl_->Open(file_path_, kOpenFlagsForWrite, | |
84 base::Bind(&LocalFileWriter::DidOpen, | |
85 weak_factory_.GetWeakPtr(), | |
86 main_operation)); | |
87 } | |
88 | |
89 void LocalFileWriter::DidOpen(const net::CompletionCallback& main_operation, | |
90 int result) { | |
91 DCHECK(has_pending_operation_); | |
92 DCHECK(stream_impl_.get()); | |
93 | |
94 if (result != net::OK) | |
95 stream_impl_.reset(NULL); | |
96 if (CancelIfRequested()) | |
97 return; | |
98 main_operation.Run(result); | |
99 } | |
100 | |
101 void LocalFileWriter::ReadyToWrite(net::IOBuffer* buf, int buf_len, | |
102 const net::CompletionCallback& callback, | |
103 int file_open_result) { | |
104 DCHECK(has_pending_operation_); | |
105 | |
106 if (file_open_result != net::OK) { | |
107 has_pending_operation_ = false; | |
108 callback.Run(file_open_result); | |
109 return; | |
110 } | |
111 | |
112 int result = InitiateWrite(buf, buf_len, callback); | |
113 if (result != net::ERR_IO_PENDING) { | |
114 has_pending_operation_ = false; | |
115 callback.Run(result); | |
116 } | |
117 } | |
118 | |
119 void LocalFileWriter::ReadyToSeek(int64 offset, | |
120 const net::Int64CompletionCallback& callback, | |
121 int file_open_result) { | |
122 DCHECK(has_pending_operation_); | |
123 | |
124 if (file_open_result != net::OK) { | |
125 has_pending_operation_ = false; | |
126 callback.Run(file_open_result); | |
127 return; | |
128 } | |
129 | |
130 int result = InitiateSeek(offset, callback); | |
131 if (result != net::ERR_IO_PENDING) { | |
132 has_pending_operation_ = false; | |
133 callback.Run(result); | |
134 } | |
135 } | |
136 | |
137 int LocalFileWriter::InitiateWrite(net::IOBuffer* buf, int buf_len, | |
138 const net::CompletionCallback& callback) { | |
139 DCHECK(has_pending_operation_); | |
140 DCHECK(stream_impl_.get()); | |
141 | |
142 return stream_impl_->Write(buf, buf_len, | |
143 base::Bind(&LocalFileWriter::DidWrite, | |
144 weak_factory_.GetWeakPtr(), | |
145 callback)); | |
146 } | |
147 | |
148 int LocalFileWriter::InitiateSeek( | |
149 int64 offset, const net::Int64CompletionCallback& callback) { | |
150 DCHECK(has_pending_operation_); | |
151 DCHECK(stream_impl_.get()); | |
152 | |
153 return stream_impl_->Seek(net::FROM_BEGIN, offset, | |
154 base::Bind(&LocalFileWriter::DidSeek, | |
155 weak_factory_.GetWeakPtr(), | |
156 callback)); | |
157 } | |
158 | |
159 void LocalFileWriter::DidWrite(const net::CompletionCallback& callback, | |
160 int result) { | |
161 DCHECK(has_pending_operation_); | |
162 | |
163 if (CancelIfRequested()) | |
164 return; | |
165 has_pending_operation_ = false; | |
166 callback.Run(result); | |
167 } | |
168 | |
169 void LocalFileWriter::DidSeek(const net::Int64CompletionCallback& callback, | |
170 int64 result) { | |
171 DCHECK(has_pending_operation_); | |
172 | |
173 if (CancelIfRequested()) | |
174 return; | |
175 has_pending_operation_ = false; | |
176 callback.Run(result); | |
177 } | |
178 | |
179 bool LocalFileWriter::CancelIfRequested() { | |
180 DCHECK(has_pending_operation_); | |
181 | |
182 if (cancel_callback_.is_null()) | |
183 return false; | |
184 | |
185 net::CompletionCallback pending_cancel = cancel_callback_; | |
186 has_pending_operation_ = false; | |
187 cancel_callback_.Reset(); | |
188 pending_cancel.Run(net::OK); | |
189 return true; | |
190 } | |
191 | |
192 } // namespace fileapi | |
OLD | NEW |