OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/services/files/public/cpp/input_stream_file.h" | 5 #include "mojo/services/files/public/cpp/input_stream_file.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "mojo/public/cpp/environment/logging.h" | 9 #include "mojo/public/cpp/environment/logging.h" |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 was_destroyed_(nullptr), | 38 was_destroyed_(nullptr), |
39 binding_(this, request.Pass()) { | 39 binding_(this, request.Pass()) { |
40 binding_.set_connection_error_handler([this]() { | 40 binding_.set_connection_error_handler([this]() { |
41 if (client_) | 41 if (client_) |
42 client_->OnClosed(); | 42 client_->OnClosed(); |
43 }); | 43 }); |
44 } | 44 } |
45 | 45 |
46 void InputStreamFile::Close(const CloseCallback& callback) { | 46 void InputStreamFile::Close(const CloseCallback& callback) { |
47 if (is_closed_) { | 47 if (is_closed_) { |
48 callback.Run(mojo::files::ERROR_CLOSED); | 48 callback.Run(mojo::files::Error::CLOSED); |
49 return; | 49 return; |
50 } | 50 } |
51 | 51 |
52 // TODO(vtl): Call pending read callbacks? | 52 // TODO(vtl): Call pending read callbacks? |
53 | 53 |
54 is_closed_ = true; | 54 is_closed_ = true; |
55 callback.Run(mojo::files::ERROR_OK); | 55 callback.Run(mojo::files::Error::OK); |
56 | 56 |
57 if (client_) | 57 if (client_) |
58 client_->OnClosed(); | 58 client_->OnClosed(); |
59 } | 59 } |
60 | 60 |
61 void InputStreamFile::Read(uint32_t num_bytes_to_read, | 61 void InputStreamFile::Read(uint32_t num_bytes_to_read, |
62 int64_t offset, | 62 int64_t offset, |
63 mojo::files::Whence whence, | 63 mojo::files::Whence whence, |
64 const ReadCallback& callback) { | 64 const ReadCallback& callback) { |
65 if (is_closed_) { | 65 if (is_closed_) { |
66 callback.Run(mojo::files::ERROR_CLOSED, mojo::Array<uint8_t>()); | 66 callback.Run(mojo::files::Error::CLOSED, mojo::Array<uint8_t>()); |
67 return; | 67 return; |
68 } | 68 } |
69 | 69 |
70 if (offset != 0 || whence != mojo::files::WHENCE_FROM_CURRENT) { | 70 if (offset != 0 || whence != mojo::files::Whence::FROM_CURRENT) { |
71 // TODO(vtl): Is this the "right" behavior? | 71 // TODO(vtl): Is this the "right" behavior? |
72 callback.Run(mojo::files::ERROR_INVALID_ARGUMENT, mojo::Array<uint8_t>()); | 72 callback.Run(mojo::files::Error::INVALID_ARGUMENT, mojo::Array<uint8_t>()); |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 bool should_start_read = pending_read_queue_.empty(); | 76 bool should_start_read = pending_read_queue_.empty(); |
77 pending_read_queue_.push_back(PendingRead(num_bytes_to_read, callback)); | 77 pending_read_queue_.push_back(PendingRead(num_bytes_to_read, callback)); |
78 if (should_start_read) | 78 if (should_start_read) |
79 StartRead(); | 79 StartRead(); |
80 } | 80 } |
81 | 81 |
82 void InputStreamFile::Write(mojo::Array<uint8_t> bytes_to_write, | 82 void InputStreamFile::Write(mojo::Array<uint8_t> bytes_to_write, |
83 int64_t offset, | 83 int64_t offset, |
84 mojo::files::Whence whence, | 84 mojo::files::Whence whence, |
85 const WriteCallback& callback) { | 85 const WriteCallback& callback) { |
86 MOJO_DCHECK(!bytes_to_write.is_null()); | 86 MOJO_DCHECK(!bytes_to_write.is_null()); |
87 | 87 |
88 if (is_closed_) { | 88 if (is_closed_) { |
89 callback.Run(mojo::files::ERROR_CLOSED, 0); | 89 callback.Run(mojo::files::Error::CLOSED, 0); |
90 return; | 90 return; |
91 } | 91 } |
92 | 92 |
93 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 93 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
94 // unsupported/EINVAL is better.) | 94 // unsupported/EINVAL is better.) |
95 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); | 95 callback.Run(mojo::files::Error::UNAVAILABLE, 0); |
96 } | 96 } |
97 | 97 |
98 void InputStreamFile::ReadToStream(mojo::ScopedDataPipeProducerHandle source, | 98 void InputStreamFile::ReadToStream(mojo::ScopedDataPipeProducerHandle source, |
99 int64_t offset, | 99 int64_t offset, |
100 mojo::files::Whence whence, | 100 mojo::files::Whence whence, |
101 int64_t num_bytes_to_read, | 101 int64_t num_bytes_to_read, |
102 const ReadToStreamCallback& callback) { | 102 const ReadToStreamCallback& callback) { |
103 if (is_closed_) { | 103 if (is_closed_) { |
104 callback.Run(mojo::files::ERROR_CLOSED); | 104 callback.Run(mojo::files::Error::CLOSED); |
105 return; | 105 return; |
106 } | 106 } |
107 | 107 |
108 // TODO(vtl) | 108 // TODO(vtl) |
109 MOJO_DLOG(ERROR) << "Not implemented"; | 109 MOJO_DLOG(ERROR) << "Not implemented"; |
110 callback.Run(mojo::files::ERROR_UNIMPLEMENTED); | 110 callback.Run(mojo::files::Error::UNIMPLEMENTED); |
111 } | 111 } |
112 | 112 |
113 void InputStreamFile::WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, | 113 void InputStreamFile::WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, |
114 int64_t offset, | 114 int64_t offset, |
115 mojo::files::Whence whence, | 115 mojo::files::Whence whence, |
116 const WriteFromStreamCallback& callback) { | 116 const WriteFromStreamCallback& callback) { |
117 if (is_closed_) { | 117 if (is_closed_) { |
118 callback.Run(mojo::files::ERROR_CLOSED); | 118 callback.Run(mojo::files::Error::CLOSED); |
119 return; | 119 return; |
120 } | 120 } |
121 | 121 |
122 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 122 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
123 // unsupported/EINVAL is better.) | 123 // unsupported/EINVAL is better.) |
124 callback.Run(mojo::files::ERROR_UNAVAILABLE); | 124 callback.Run(mojo::files::Error::UNAVAILABLE); |
125 } | 125 } |
126 | 126 |
127 void InputStreamFile::Tell(const TellCallback& callback) { | 127 void InputStreamFile::Tell(const TellCallback& callback) { |
128 if (is_closed_) { | 128 if (is_closed_) { |
129 callback.Run(mojo::files::ERROR_CLOSED, 0); | 129 callback.Run(mojo::files::Error::CLOSED, 0); |
130 return; | 130 return; |
131 } | 131 } |
132 | 132 |
133 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 133 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
134 // unsupported/EINVAL is better.) | 134 // unsupported/EINVAL is better.) |
135 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); | 135 callback.Run(mojo::files::Error::UNAVAILABLE, 0); |
136 } | 136 } |
137 | 137 |
138 void InputStreamFile::Seek(int64_t offset, | 138 void InputStreamFile::Seek(int64_t offset, |
139 mojo::files::Whence whence, | 139 mojo::files::Whence whence, |
140 const SeekCallback& callback) { | 140 const SeekCallback& callback) { |
141 if (is_closed_) { | 141 if (is_closed_) { |
142 callback.Run(mojo::files::ERROR_CLOSED, 0); | 142 callback.Run(mojo::files::Error::CLOSED, 0); |
143 return; | 143 return; |
144 } | 144 } |
145 | 145 |
146 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 146 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
147 // unsupported/EINVAL is better.) | 147 // unsupported/EINVAL is better.) |
148 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); | 148 callback.Run(mojo::files::Error::UNAVAILABLE, 0); |
149 } | 149 } |
150 | 150 |
151 void InputStreamFile::Stat(const StatCallback& callback) { | 151 void InputStreamFile::Stat(const StatCallback& callback) { |
152 if (is_closed_) { | 152 if (is_closed_) { |
153 callback.Run(mojo::files::ERROR_CLOSED, nullptr); | 153 callback.Run(mojo::files::Error::CLOSED, nullptr); |
154 return; | 154 return; |
155 } | 155 } |
156 | 156 |
157 // TODO(vtl) | 157 // TODO(vtl) |
158 MOJO_DLOG(ERROR) << "Not implemented"; | 158 MOJO_DLOG(ERROR) << "Not implemented"; |
159 callback.Run(mojo::files::ERROR_UNIMPLEMENTED, nullptr); | 159 callback.Run(mojo::files::Error::UNIMPLEMENTED, nullptr); |
160 } | 160 } |
161 | 161 |
162 void InputStreamFile::Truncate(int64_t size, const TruncateCallback& callback) { | 162 void InputStreamFile::Truncate(int64_t size, const TruncateCallback& callback) { |
163 if (is_closed_) { | 163 if (is_closed_) { |
164 callback.Run(mojo::files::ERROR_CLOSED); | 164 callback.Run(mojo::files::Error::CLOSED); |
165 return; | 165 return; |
166 } | 166 } |
167 | 167 |
168 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 168 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
169 // unsupported/EINVAL is better.) | 169 // unsupported/EINVAL is better.) |
170 callback.Run(mojo::files::ERROR_UNAVAILABLE); | 170 callback.Run(mojo::files::Error::UNAVAILABLE); |
171 } | 171 } |
172 | 172 |
173 void InputStreamFile::Touch(mojo::files::TimespecOrNowPtr atime, | 173 void InputStreamFile::Touch(mojo::files::TimespecOrNowPtr atime, |
174 mojo::files::TimespecOrNowPtr mtime, | 174 mojo::files::TimespecOrNowPtr mtime, |
175 const TouchCallback& callback) { | 175 const TouchCallback& callback) { |
176 if (is_closed_) { | 176 if (is_closed_) { |
177 callback.Run(mojo::files::ERROR_CLOSED); | 177 callback.Run(mojo::files::Error::CLOSED); |
178 return; | 178 return; |
179 } | 179 } |
180 | 180 |
181 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 181 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
182 // unsupported/EINVAL is better.) | 182 // unsupported/EINVAL is better.) |
183 callback.Run(mojo::files::ERROR_UNAVAILABLE); | 183 callback.Run(mojo::files::Error::UNAVAILABLE); |
184 } | 184 } |
185 | 185 |
186 void InputStreamFile::Dup(mojo::InterfaceRequest<mojo::files::File> file, | 186 void InputStreamFile::Dup(mojo::InterfaceRequest<mojo::files::File> file, |
187 const DupCallback& callback) { | 187 const DupCallback& callback) { |
188 if (is_closed_) { | 188 if (is_closed_) { |
189 callback.Run(mojo::files::ERROR_CLOSED); | 189 callback.Run(mojo::files::Error::CLOSED); |
190 return; | 190 return; |
191 } | 191 } |
192 | 192 |
193 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 193 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
194 // unsupported/EINVAL is better.) | 194 // unsupported/EINVAL is better.) |
195 callback.Run(mojo::files::ERROR_UNAVAILABLE); | 195 callback.Run(mojo::files::Error::UNAVAILABLE); |
196 } | 196 } |
197 | 197 |
198 void InputStreamFile::Reopen(mojo::InterfaceRequest<mojo::files::File> file, | 198 void InputStreamFile::Reopen(mojo::InterfaceRequest<mojo::files::File> file, |
199 uint32_t open_flags, | 199 uint32_t open_flags, |
200 const ReopenCallback& callback) { | 200 const ReopenCallback& callback) { |
201 if (is_closed_) { | 201 if (is_closed_) { |
202 callback.Run(mojo::files::ERROR_CLOSED); | 202 callback.Run(mojo::files::Error::CLOSED); |
203 return; | 203 return; |
204 } | 204 } |
205 | 205 |
206 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 206 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
207 // unsupported/EINVAL is better.) | 207 // unsupported/EINVAL is better.) |
208 callback.Run(mojo::files::ERROR_UNAVAILABLE); | 208 callback.Run(mojo::files::Error::UNAVAILABLE); |
209 } | 209 } |
210 | 210 |
211 void InputStreamFile::AsBuffer(const AsBufferCallback& callback) { | 211 void InputStreamFile::AsBuffer(const AsBufferCallback& callback) { |
212 if (is_closed_) { | 212 if (is_closed_) { |
213 callback.Run(mojo::files::ERROR_CLOSED, mojo::ScopedSharedBufferHandle()); | 213 callback.Run(mojo::files::Error::CLOSED, mojo::ScopedSharedBufferHandle()); |
214 return; | 214 return; |
215 } | 215 } |
216 | 216 |
217 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | 217 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
218 // unsupported/EINVAL is better.) | 218 // unsupported/EINVAL is better.) |
219 callback.Run(mojo::files::ERROR_UNAVAILABLE, | 219 callback.Run(mojo::files::Error::UNAVAILABLE, |
220 mojo::ScopedSharedBufferHandle()); | 220 mojo::ScopedSharedBufferHandle()); |
221 } | 221 } |
222 | 222 |
223 void InputStreamFile::Ioctl(uint32_t request, | 223 void InputStreamFile::Ioctl(uint32_t request, |
224 mojo::Array<uint32_t> in_values, | 224 mojo::Array<uint32_t> in_values, |
225 const IoctlCallback& callback) { | 225 const IoctlCallback& callback) { |
226 if (is_closed_) { | 226 if (is_closed_) { |
227 callback.Run(mojo::files::ERROR_CLOSED, mojo::Array<uint32_t>()); | 227 callback.Run(mojo::files::Error::CLOSED, mojo::Array<uint32_t>()); |
228 return; | 228 return; |
229 } | 229 } |
230 | 230 |
231 callback.Run(mojo::files::ERROR_UNIMPLEMENTED, mojo::Array<uint32_t>()); | 231 callback.Run(mojo::files::Error::UNIMPLEMENTED, mojo::Array<uint32_t>()); |
232 } | 232 } |
233 | 233 |
234 void InputStreamFile::StartRead() { | 234 void InputStreamFile::StartRead() { |
235 MOJO_DCHECK(!pending_read_queue_.empty()); | 235 MOJO_DCHECK(!pending_read_queue_.empty()); |
236 | 236 |
237 // If we don't have a client, just drain all the reads. | 237 // If we don't have a client, just drain all the reads. |
238 if (!client_) { | 238 if (!client_) { |
239 while (!pending_read_queue_.empty()) { | 239 while (!pending_read_queue_.empty()) { |
240 // TODO(vtl): Is this what we want? | 240 // TODO(vtl): Is this what we want? |
241 pending_read_queue_.front().callback.Run(mojo::files::ERROR_UNAVAILABLE, | 241 pending_read_queue_.front().callback.Run(mojo::files::Error::UNAVAILABLE, |
242 mojo::Array<uint8_t>()); | 242 mojo::Array<uint8_t>()); |
243 pending_read_queue_.pop_front(); | 243 pending_read_queue_.pop_front(); |
244 } | 244 } |
245 return; | 245 return; |
246 } | 246 } |
247 | 247 |
248 do { | 248 do { |
249 // Find a non-zero-byte read, completing any zero-byte reads at the front of | 249 // Find a non-zero-byte read, completing any zero-byte reads at the front of |
250 // the queue. Note that we do this in FIFO order (thus couldn't have | 250 // the queue. Note that we do this in FIFO order (thus couldn't have |
251 // completed them earlier). | 251 // completed them earlier). |
252 while (!pending_read_queue_.front().num_bytes) { | 252 while (!pending_read_queue_.front().num_bytes) { |
253 pending_read_queue_.front().callback.Run(mojo::files::ERROR_OK, | 253 pending_read_queue_.front().callback.Run(mojo::files::Error::OK, |
254 mojo::Array<uint8_t>()); | 254 mojo::Array<uint8_t>()); |
255 pending_read_queue_.pop_front(); | 255 pending_read_queue_.pop_front(); |
256 | 256 |
257 if (pending_read_queue_.empty()) | 257 if (pending_read_queue_.empty()) |
258 return; | 258 return; |
259 } | 259 } |
260 | 260 |
261 // Binding |this| is OK, since the client must not call the callback if we | 261 // Binding |this| is OK, since the client must not call the callback if we |
262 // are destroyed. | 262 // are destroyed. |
263 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 263 mojo::files::Error error = mojo::files::Error::INTERNAL; |
264 mojo::Array<uint8_t> data; | 264 mojo::Array<uint8_t> data; |
265 // Detect if we were destroyed inside |RequestData()|. | 265 // Detect if we were destroyed inside |RequestData()|. |
266 bool was_destroyed = false; | 266 bool was_destroyed = false; |
267 MOJO_CHECK(!was_destroyed_); | 267 MOJO_CHECK(!was_destroyed_); |
268 was_destroyed_ = &was_destroyed; | 268 was_destroyed_ = &was_destroyed; |
269 bool synchronous_completion = client_->RequestData( | 269 bool synchronous_completion = client_->RequestData( |
270 pending_read_queue_.front().num_bytes, &error, &data, | 270 pending_read_queue_.front().num_bytes, &error, &data, |
271 [this](mojo::files::Error e, mojo::Array<uint8_t> d) { | 271 [this](mojo::files::Error e, mojo::Array<uint8_t> d) { |
272 CompleteRead(e, d.Pass()); | 272 CompleteRead(e, d.Pass()); |
273 if (!pending_read_queue_.empty()) | 273 if (!pending_read_queue_.empty()) |
274 StartRead(); | 274 StartRead(); |
275 }); | 275 }); |
276 if (was_destroyed) | 276 if (was_destroyed) |
277 return; | 277 return; |
278 was_destroyed_ = nullptr; | 278 was_destroyed_ = nullptr; |
279 if (synchronous_completion) | 279 if (synchronous_completion) |
280 CompleteRead(error, data.Pass()); | 280 CompleteRead(error, data.Pass()); |
281 else | 281 else |
282 return; // Asynchronous completion. | 282 return; // Asynchronous completion. |
283 } while (!pending_read_queue_.empty()); | 283 } while (!pending_read_queue_.empty()); |
284 } | 284 } |
285 | 285 |
286 void InputStreamFile::CompleteRead(mojo::files::Error error, | 286 void InputStreamFile::CompleteRead(mojo::files::Error error, |
287 mojo::Array<uint8_t> data) { | 287 mojo::Array<uint8_t> data) { |
288 MOJO_CHECK(!pending_read_queue_.empty()); | 288 MOJO_CHECK(!pending_read_queue_.empty()); |
289 | 289 |
290 if (error != mojo::files::ERROR_OK) { | 290 if (error != mojo::files::Error::OK) { |
291 pending_read_queue_.front().callback.Run(error, mojo::Array<uint8_t>()); | 291 pending_read_queue_.front().callback.Run(error, mojo::Array<uint8_t>()); |
292 pending_read_queue_.pop_front(); | 292 pending_read_queue_.pop_front(); |
293 return; | 293 return; |
294 } | 294 } |
295 | 295 |
296 MOJO_CHECK(!data.is_null()); | 296 MOJO_CHECK(!data.is_null()); |
297 MOJO_CHECK(data.size() <= pending_read_queue_.front().num_bytes); | 297 MOJO_CHECK(data.size() <= pending_read_queue_.front().num_bytes); |
298 pending_read_queue_.front().callback.Run(mojo::files::ERROR_OK, data.Pass()); | 298 pending_read_queue_.front().callback.Run(mojo::files::Error::OK, data.Pass()); |
299 pending_read_queue_.pop_front(); | 299 pending_read_queue_.pop_front(); |
300 } | 300 } |
301 | 301 |
302 } // namespace files_impl | 302 } // namespace files_impl |
OLD | NEW |