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