OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/services/files/public/cpp/output_stream_file.h" | |
6 | |
7 #include "mojo/public/cpp/environment/logging.h" | |
8 | |
9 namespace files_impl { | |
10 | |
11 // static | |
12 std::unique_ptr<OutputStreamFile> OutputStreamFile::Create( | |
13 Client* client, | |
14 mojo::InterfaceRequest<mojo::files::File> request) { | |
15 // TODO(vtl): Use make_unique when we have C++14. | |
16 return std::unique_ptr<OutputStreamFile>( | |
17 new OutputStreamFile(client, request.Pass())); | |
18 } | |
19 | |
20 OutputStreamFile::~OutputStreamFile() {} | |
21 | |
22 OutputStreamFile::OutputStreamFile( | |
23 Client* client, | |
24 mojo::InterfaceRequest<mojo::files::File> request) | |
25 : client_(client), is_closed_(false), binding_(this, request.Pass()) { | |
26 binding_.set_connection_error_handler([this]() { | |
27 if (client_) | |
28 client_->OnClosed(); | |
29 }); | |
30 } | |
31 | |
32 void OutputStreamFile::Close(const CloseCallback& callback) { | |
33 if (is_closed_) { | |
34 callback.Run(mojo::files::Error::CLOSED); | |
35 return; | |
36 } | |
37 | |
38 is_closed_ = true; | |
39 callback.Run(mojo::files::Error::OK); | |
40 | |
41 if (client_) | |
42 client_->OnClosed(); | |
43 } | |
44 | |
45 void OutputStreamFile::Read(uint32_t num_bytes_to_read, | |
46 int64_t offset, | |
47 mojo::files::Whence whence, | |
48 const ReadCallback& callback) { | |
49 if (is_closed_) { | |
50 callback.Run(mojo::files::Error::CLOSED, mojo::Array<uint8_t>()); | |
51 return; | |
52 } | |
53 | |
54 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
55 // unsupported/EINVAL is better.) | |
56 callback.Run(mojo::files::Error::UNAVAILABLE, mojo::Array<uint8_t>()); | |
57 } | |
58 | |
59 void OutputStreamFile::Write(mojo::Array<uint8_t> bytes_to_write, | |
60 int64_t offset, | |
61 mojo::files::Whence whence, | |
62 const WriteCallback& callback) { | |
63 MOJO_DCHECK(!bytes_to_write.is_null()); | |
64 | |
65 if (is_closed_) { | |
66 callback.Run(mojo::files::Error::CLOSED, 0); | |
67 return; | |
68 } | |
69 | |
70 if (offset != 0 || whence != mojo::files::Whence::FROM_CURRENT) { | |
71 // TODO(vtl): Is this the "right" behavior? | |
72 callback.Run(mojo::files::Error::INVALID_ARGUMENT, 0); | |
73 return; | |
74 } | |
75 | |
76 if (!bytes_to_write.size()) { | |
77 callback.Run(mojo::files::Error::OK, 0); | |
78 return; | |
79 } | |
80 | |
81 // We require the client to handle all the output, so we run the callback now. | |
82 // TODO(vtl): This means that the callback will be run (and the response | |
83 // message sent), even if the client decides to destroy us -- and thus close | |
84 // the message pipe -- in |OnDataReceived()|. This may makes throttling | |
85 // slightly less effective -- but increase parallelism -- since the writer may | |
86 // enqueue another write immediately. | |
87 callback.Run(mojo::files::Error::OK, | |
88 static_cast<uint32_t>(bytes_to_write.size())); | |
89 | |
90 if (client_) | |
91 client_->OnDataReceived(&bytes_to_write.front(), bytes_to_write.size()); | |
92 } | |
93 | |
94 void OutputStreamFile::ReadToStream(mojo::ScopedDataPipeProducerHandle source, | |
95 int64_t offset, | |
96 mojo::files::Whence whence, | |
97 int64_t num_bytes_to_read, | |
98 const ReadToStreamCallback& callback) { | |
99 if (is_closed_) { | |
100 callback.Run(mojo::files::Error::CLOSED); | |
101 return; | |
102 } | |
103 | |
104 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
105 // unsupported/EINVAL is better.) | |
106 callback.Run(mojo::files::Error::UNAVAILABLE); | |
107 } | |
108 | |
109 void OutputStreamFile::WriteFromStream( | |
110 mojo::ScopedDataPipeConsumerHandle sink, | |
111 int64_t offset, | |
112 mojo::files::Whence whence, | |
113 const WriteFromStreamCallback& callback) { | |
114 if (is_closed_) { | |
115 callback.Run(mojo::files::Error::CLOSED); | |
116 return; | |
117 } | |
118 | |
119 // TODO(vtl) | |
120 MOJO_DLOG(ERROR) << "Not implemented"; | |
121 callback.Run(mojo::files::Error::UNIMPLEMENTED); | |
122 } | |
123 | |
124 void OutputStreamFile::Tell(const TellCallback& callback) { | |
125 if (is_closed_) { | |
126 callback.Run(mojo::files::Error::CLOSED, 0); | |
127 return; | |
128 } | |
129 | |
130 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
131 // unsupported/EINVAL is better.) | |
132 callback.Run(mojo::files::Error::UNAVAILABLE, 0); | |
133 } | |
134 | |
135 void OutputStreamFile::Seek(int64_t offset, | |
136 mojo::files::Whence whence, | |
137 const SeekCallback& callback) { | |
138 if (is_closed_) { | |
139 callback.Run(mojo::files::Error::CLOSED, 0); | |
140 return; | |
141 } | |
142 | |
143 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
144 // unsupported/EINVAL is better.) | |
145 callback.Run(mojo::files::Error::UNAVAILABLE, 0); | |
146 } | |
147 | |
148 void OutputStreamFile::Stat(const StatCallback& callback) { | |
149 if (is_closed_) { | |
150 callback.Run(mojo::files::Error::CLOSED, nullptr); | |
151 return; | |
152 } | |
153 | |
154 // TODO(vtl) | |
155 MOJO_DLOG(ERROR) << "Not implemented"; | |
156 callback.Run(mojo::files::Error::UNIMPLEMENTED, nullptr); | |
157 } | |
158 | |
159 void OutputStreamFile::Truncate(int64_t size, | |
160 const TruncateCallback& callback) { | |
161 if (is_closed_) { | |
162 callback.Run(mojo::files::Error::CLOSED); | |
163 return; | |
164 } | |
165 | |
166 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
167 // unsupported/EINVAL is better.) | |
168 callback.Run(mojo::files::Error::UNAVAILABLE); | |
169 } | |
170 | |
171 void OutputStreamFile::Touch(mojo::files::TimespecOrNowPtr atime, | |
172 mojo::files::TimespecOrNowPtr mtime, | |
173 const TouchCallback& callback) { | |
174 if (is_closed_) { | |
175 callback.Run(mojo::files::Error::CLOSED); | |
176 return; | |
177 } | |
178 | |
179 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
180 // unsupported/EINVAL is better.) | |
181 callback.Run(mojo::files::Error::UNAVAILABLE); | |
182 } | |
183 | |
184 void OutputStreamFile::Dup(mojo::InterfaceRequest<mojo::files::File> file, | |
185 const DupCallback& callback) { | |
186 if (is_closed_) { | |
187 callback.Run(mojo::files::Error::CLOSED); | |
188 return; | |
189 } | |
190 | |
191 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
192 // unsupported/EINVAL is better.) | |
193 callback.Run(mojo::files::Error::UNAVAILABLE); | |
194 } | |
195 | |
196 void OutputStreamFile::Reopen(mojo::InterfaceRequest<mojo::files::File> file, | |
197 uint32_t open_flags, | |
198 const ReopenCallback& callback) { | |
199 if (is_closed_) { | |
200 callback.Run(mojo::files::Error::CLOSED); | |
201 return; | |
202 } | |
203 | |
204 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
205 // unsupported/EINVAL is better.) | |
206 callback.Run(mojo::files::Error::UNAVAILABLE); | |
207 } | |
208 | |
209 void OutputStreamFile::AsBuffer(const AsBufferCallback& callback) { | |
210 if (is_closed_) { | |
211 callback.Run(mojo::files::Error::CLOSED, mojo::ScopedSharedBufferHandle()); | |
212 return; | |
213 } | |
214 | |
215 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe | |
216 // unsupported/EINVAL is better.) | |
217 callback.Run(mojo::files::Error::UNAVAILABLE, | |
218 mojo::ScopedSharedBufferHandle()); | |
219 } | |
220 | |
221 void OutputStreamFile::Ioctl(uint32_t request, | |
222 mojo::Array<uint32_t> in_values, | |
223 const IoctlCallback& callback) { | |
224 if (is_closed_) { | |
225 callback.Run(mojo::files::Error::CLOSED, mojo::Array<uint32_t>()); | |
226 return; | |
227 } | |
228 | |
229 callback.Run(mojo::files::Error::UNIMPLEMENTED, mojo::Array<uint32_t>()); | |
230 } | |
231 | |
232 } // namespace files_impl | |
OLD | NEW |