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