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 if (!num_bytes_to_read) { |
| 55 callback.Run(mojo::files::ERROR_OK, mojo::Array<uint8_t>()); |
| 56 return; |
| 57 } |
| 58 |
| 59 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 60 // unsupported/EINVAL is better.) |
| 61 callback.Run(mojo::files::ERROR_UNAVAILABLE, mojo::Array<uint8_t>()); |
| 62 } |
| 63 |
| 64 void OutputStreamFile::Write(mojo::Array<uint8_t> bytes_to_write, |
| 65 int64_t offset, |
| 66 mojo::files::Whence whence, |
| 67 const WriteCallback& callback) { |
| 68 MOJO_DCHECK(!bytes_to_write.is_null()); |
| 69 |
| 70 if (is_closed_) { |
| 71 callback.Run(mojo::files::ERROR_CLOSED, 0); |
| 72 return; |
| 73 } |
| 74 |
| 75 if (offset != 0 || whence != mojo::files::WHENCE_FROM_CURRENT) { |
| 76 // TODO(vtl): Is this the "right" behavior? |
| 77 callback.Run(mojo::files::ERROR_INVALID_ARGUMENT, 0); |
| 78 return; |
| 79 } |
| 80 |
| 81 if (!bytes_to_write.size()) { |
| 82 callback.Run(mojo::files::ERROR_OK, 0); |
| 83 return; |
| 84 } |
| 85 |
| 86 // We require the client to handle all the output, so we run the callback now. |
| 87 // TODO(vtl): This means that the callback will be run (and the response |
| 88 // message sent), even if the client decides to destroy us -- and thus close |
| 89 // the message pipe -- in |OnDataReceived()|. This may makes throttling |
| 90 // slightly less effective -- but increase parallelism -- since the writer may |
| 91 // enqueue another write immediately. |
| 92 callback.Run(mojo::files::ERROR_OK, |
| 93 static_cast<uint32_t>(bytes_to_write.size())); |
| 94 |
| 95 if (client_) |
| 96 client_->OnDataReceived(&bytes_to_write.front(), bytes_to_write.size()); |
| 97 } |
| 98 |
| 99 void OutputStreamFile::ReadToStream(mojo::ScopedDataPipeProducerHandle source, |
| 100 int64_t offset, |
| 101 mojo::files::Whence whence, |
| 102 int64_t num_bytes_to_read, |
| 103 const ReadToStreamCallback& callback) { |
| 104 if (is_closed_) { |
| 105 callback.Run(mojo::files::ERROR_CLOSED); |
| 106 return; |
| 107 } |
| 108 |
| 109 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 110 // unsupported/EINVAL is better.) |
| 111 callback.Run(mojo::files::ERROR_UNAVAILABLE); |
| 112 } |
| 113 |
| 114 void OutputStreamFile::WriteFromStream( |
| 115 mojo::ScopedDataPipeConsumerHandle sink, |
| 116 int64_t offset, |
| 117 mojo::files::Whence whence, |
| 118 const WriteFromStreamCallback& callback) { |
| 119 if (is_closed_) { |
| 120 callback.Run(mojo::files::ERROR_CLOSED); |
| 121 return; |
| 122 } |
| 123 |
| 124 // TODO(vtl) |
| 125 MOJO_DLOG(ERROR) << "Not implemented"; |
| 126 callback.Run(mojo::files::ERROR_UNIMPLEMENTED); |
| 127 } |
| 128 |
| 129 void OutputStreamFile::Tell(const TellCallback& callback) { |
| 130 if (is_closed_) { |
| 131 callback.Run(mojo::files::ERROR_CLOSED, 0); |
| 132 return; |
| 133 } |
| 134 |
| 135 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 136 // unsupported/EINVAL is better.) |
| 137 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); |
| 138 } |
| 139 |
| 140 void OutputStreamFile::Seek(int64_t offset, |
| 141 mojo::files::Whence whence, |
| 142 const SeekCallback& callback) { |
| 143 if (is_closed_) { |
| 144 callback.Run(mojo::files::ERROR_CLOSED, 0); |
| 145 return; |
| 146 } |
| 147 |
| 148 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 149 // unsupported/EINVAL is better.) |
| 150 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); |
| 151 } |
| 152 |
| 153 void OutputStreamFile::Stat(const StatCallback& callback) { |
| 154 if (is_closed_) { |
| 155 callback.Run(mojo::files::ERROR_CLOSED, nullptr); |
| 156 return; |
| 157 } |
| 158 |
| 159 // TODO(vtl) |
| 160 MOJO_DLOG(ERROR) << "Not implemented"; |
| 161 callback.Run(mojo::files::ERROR_UNIMPLEMENTED, nullptr); |
| 162 } |
| 163 |
| 164 void OutputStreamFile::Truncate(int64_t size, |
| 165 const TruncateCallback& callback) { |
| 166 if (is_closed_) { |
| 167 callback.Run(mojo::files::ERROR_CLOSED); |
| 168 return; |
| 169 } |
| 170 |
| 171 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 172 // unsupported/EINVAL is better.) |
| 173 callback.Run(mojo::files::ERROR_UNAVAILABLE); |
| 174 } |
| 175 |
| 176 void OutputStreamFile::Touch(mojo::files::TimespecOrNowPtr atime, |
| 177 mojo::files::TimespecOrNowPtr mtime, |
| 178 const TouchCallback& callback) { |
| 179 if (is_closed_) { |
| 180 callback.Run(mojo::files::ERROR_CLOSED); |
| 181 return; |
| 182 } |
| 183 |
| 184 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 185 // unsupported/EINVAL is better.) |
| 186 callback.Run(mojo::files::ERROR_UNAVAILABLE); |
| 187 } |
| 188 |
| 189 void OutputStreamFile::Dup(mojo::InterfaceRequest<mojo::files::File> file, |
| 190 const DupCallback& callback) { |
| 191 if (is_closed_) { |
| 192 callback.Run(mojo::files::ERROR_CLOSED); |
| 193 return; |
| 194 } |
| 195 |
| 196 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 197 // unsupported/EINVAL is better.) |
| 198 callback.Run(mojo::files::ERROR_UNAVAILABLE); |
| 199 } |
| 200 |
| 201 void OutputStreamFile::Reopen(mojo::InterfaceRequest<mojo::files::File> file, |
| 202 uint32_t open_flags, |
| 203 const ReopenCallback& callback) { |
| 204 if (is_closed_) { |
| 205 callback.Run(mojo::files::ERROR_CLOSED); |
| 206 return; |
| 207 } |
| 208 |
| 209 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 210 // unsupported/EINVAL is better.) |
| 211 callback.Run(mojo::files::ERROR_UNAVAILABLE); |
| 212 } |
| 213 |
| 214 void OutputStreamFile::AsBuffer(const AsBufferCallback& callback) { |
| 215 if (is_closed_) { |
| 216 callback.Run(mojo::files::ERROR_CLOSED, mojo::ScopedSharedBufferHandle()); |
| 217 return; |
| 218 } |
| 219 |
| 220 // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe |
| 221 // unsupported/EINVAL is better.) |
| 222 callback.Run(mojo::files::ERROR_UNAVAILABLE, |
| 223 mojo::ScopedSharedBufferHandle()); |
| 224 } |
| 225 |
| 226 void OutputStreamFile::Ioctl(uint32_t request, |
| 227 mojo::Array<uint32_t> in_values, |
| 228 const IoctlCallback& callback) { |
| 229 if (is_closed_) { |
| 230 callback.Run(mojo::files::ERROR_CLOSED, mojo::Array<uint32_t>()); |
| 231 return; |
| 232 } |
| 233 |
| 234 callback.Run(mojo::files::ERROR_UNIMPLEMENTED, mojo::Array<uint32_t>()); |
| 235 } |
| 236 |
| 237 } // namespace files_impl |
OLD | NEW |