| Index: mojo/services/files/public/cpp/lib/output_stream_file.cc
|
| diff --git a/mojo/services/files/public/cpp/lib/output_stream_file.cc b/mojo/services/files/public/cpp/lib/output_stream_file.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..06e05c3a567c36ce988fdee39f11ae604f3f13cb
|
| --- /dev/null
|
| +++ b/mojo/services/files/public/cpp/lib/output_stream_file.cc
|
| @@ -0,0 +1,237 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "mojo/services/files/public/cpp/output_stream_file.h"
|
| +
|
| +#include "mojo/public/cpp/environment/logging.h"
|
| +
|
| +namespace files_impl {
|
| +
|
| +// static
|
| +std::unique_ptr<OutputStreamFile> OutputStreamFile::Create(
|
| + Client* client,
|
| + mojo::InterfaceRequest<mojo::files::File> request) {
|
| + // TODO(vtl): Use make_unique when we have C++14.
|
| + return std::unique_ptr<OutputStreamFile>(
|
| + new OutputStreamFile(client, request.Pass()));
|
| +}
|
| +
|
| +OutputStreamFile::~OutputStreamFile() {}
|
| +
|
| +OutputStreamFile::OutputStreamFile(
|
| + Client* client,
|
| + mojo::InterfaceRequest<mojo::files::File> request)
|
| + : client_(client), is_closed_(false), binding_(this, request.Pass()) {
|
| + binding_.set_connection_error_handler([this]() {
|
| + if (client_)
|
| + client_->OnClosed();
|
| + });
|
| +}
|
| +
|
| +void OutputStreamFile::Close(const CloseCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + is_closed_ = true;
|
| + callback.Run(mojo::files::ERROR_OK);
|
| +
|
| + if (client_)
|
| + client_->OnClosed();
|
| +}
|
| +
|
| +void OutputStreamFile::Read(uint32_t num_bytes_to_read,
|
| + int64_t offset,
|
| + mojo::files::Whence whence,
|
| + const ReadCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, mojo::Array<uint8_t>());
|
| + return;
|
| + }
|
| +
|
| + if (!num_bytes_to_read) {
|
| + callback.Run(mojo::files::ERROR_OK, mojo::Array<uint8_t>());
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE, mojo::Array<uint8_t>());
|
| +}
|
| +
|
| +void OutputStreamFile::Write(mojo::Array<uint8_t> bytes_to_write,
|
| + int64_t offset,
|
| + mojo::files::Whence whence,
|
| + const WriteCallback& callback) {
|
| + MOJO_DCHECK(!bytes_to_write.is_null());
|
| +
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, 0);
|
| + return;
|
| + }
|
| +
|
| + if (offset != 0 || whence != mojo::files::WHENCE_FROM_CURRENT) {
|
| + // TODO(vtl): Is this the "right" behavior?
|
| + callback.Run(mojo::files::ERROR_INVALID_ARGUMENT, 0);
|
| + return;
|
| + }
|
| +
|
| + if (!bytes_to_write.size()) {
|
| + callback.Run(mojo::files::ERROR_OK, 0);
|
| + return;
|
| + }
|
| +
|
| + // We require the client to handle all the output, so we run the callback now.
|
| + // TODO(vtl): This means that the callback will be run (and the response
|
| + // message sent), even if the client decides to destroy us -- and thus close
|
| + // the message pipe -- in |OnDataReceived()|. This may makes throttling
|
| + // slightly less effective -- but increase parallelism -- since the writer may
|
| + // enqueue another write immediately.
|
| + callback.Run(mojo::files::ERROR_OK,
|
| + static_cast<uint32_t>(bytes_to_write.size()));
|
| +
|
| + if (client_)
|
| + client_->OnDataReceived(&bytes_to_write.front(), bytes_to_write.size());
|
| +}
|
| +
|
| +void OutputStreamFile::ReadToStream(mojo::ScopedDataPipeProducerHandle source,
|
| + int64_t offset,
|
| + mojo::files::Whence whence,
|
| + int64_t num_bytes_to_read,
|
| + const ReadToStreamCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE);
|
| +}
|
| +
|
| +void OutputStreamFile::WriteFromStream(
|
| + mojo::ScopedDataPipeConsumerHandle sink,
|
| + int64_t offset,
|
| + mojo::files::Whence whence,
|
| + const WriteFromStreamCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl)
|
| + MOJO_DLOG(ERROR) << "Not implemented";
|
| + callback.Run(mojo::files::ERROR_UNIMPLEMENTED);
|
| +}
|
| +
|
| +void OutputStreamFile::Tell(const TellCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, 0);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE, 0);
|
| +}
|
| +
|
| +void OutputStreamFile::Seek(int64_t offset,
|
| + mojo::files::Whence whence,
|
| + const SeekCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, 0);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE, 0);
|
| +}
|
| +
|
| +void OutputStreamFile::Stat(const StatCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, nullptr);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl)
|
| + MOJO_DLOG(ERROR) << "Not implemented";
|
| + callback.Run(mojo::files::ERROR_UNIMPLEMENTED, nullptr);
|
| +}
|
| +
|
| +void OutputStreamFile::Truncate(int64_t size,
|
| + const TruncateCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE);
|
| +}
|
| +
|
| +void OutputStreamFile::Touch(mojo::files::TimespecOrNowPtr atime,
|
| + mojo::files::TimespecOrNowPtr mtime,
|
| + const TouchCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE);
|
| +}
|
| +
|
| +void OutputStreamFile::Dup(mojo::InterfaceRequest<mojo::files::File> file,
|
| + const DupCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE);
|
| +}
|
| +
|
| +void OutputStreamFile::Reopen(mojo::InterfaceRequest<mojo::files::File> file,
|
| + uint32_t open_flags,
|
| + const ReopenCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED);
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE);
|
| +}
|
| +
|
| +void OutputStreamFile::AsBuffer(const AsBufferCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, mojo::ScopedSharedBufferHandle());
|
| + return;
|
| + }
|
| +
|
| + // TODO(vtl): Is this what we want? (Also is "unavailable" right? Maybe
|
| + // unsupported/EINVAL is better.)
|
| + callback.Run(mojo::files::ERROR_UNAVAILABLE,
|
| + mojo::ScopedSharedBufferHandle());
|
| +}
|
| +
|
| +void OutputStreamFile::Ioctl(uint32_t request,
|
| + mojo::Array<uint32_t> in_values,
|
| + const IoctlCallback& callback) {
|
| + if (is_closed_) {
|
| + callback.Run(mojo::files::ERROR_CLOSED, mojo::Array<uint32_t>());
|
| + return;
|
| + }
|
| +
|
| + callback.Run(mojo::files::ERROR_UNIMPLEMENTED, mojo::Array<uint32_t>());
|
| +}
|
| +
|
| +} // namespace files_impl
|
|
|