| 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 // This is a helper class for implementing a |mojo::files::File| that behaves | |
| 6 // like an "output stream" ("output" from the point of view of the client -- | |
| 7 // i.e., the client can write/stream output to it, but not read or seek). | |
| 8 | |
| 9 #ifndef MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
| 10 #define MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
| 11 | |
| 12 #include <stddef.h> | |
| 13 | |
| 14 #include <memory> | |
| 15 | |
| 16 #include "mojo/public/cpp/bindings/binding.h" | |
| 17 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 18 #include "mojo/public/cpp/system/macros.h" | |
| 19 #include "mojo/services/files/public/interfaces/file.mojom.h" | |
| 20 #include "mojo/services/files/public/interfaces/types.mojom.h" | |
| 21 | |
| 22 namespace files_impl { | |
| 23 | |
| 24 class OutputStreamFile : public mojo::files::File { | |
| 25 public: | |
| 26 // The |Client| receives data written to the stream "file" as well as other | |
| 27 // notifications (e.g., of the "file" being closed). From any of the methods | |
| 28 // below, the client may choose to destroy the |OutputStreamFile|. | |
| 29 class Client { | |
| 30 public: | |
| 31 // Called when we receive data from the stream "file". | |
| 32 // TODO(vtl): Maybe add a way to throttle (e.g., for the client to not | |
| 33 // accept all the data). | |
| 34 virtual void OnDataReceived(const void* bytes, size_t num_bytes) = 0; | |
| 35 | |
| 36 // Called when the stream "file" is closed, via |Close()| or due to the | |
| 37 // other end of the message pipe being closed. (This will not be called due | |
| 38 // the |OutputStreamFile| being destroyed.) | |
| 39 virtual void OnClosed() = 0; | |
| 40 | |
| 41 protected: | |
| 42 virtual ~Client() {} | |
| 43 }; | |
| 44 | |
| 45 // Static factory method. |client| may be null, but if not it should typically | |
| 46 // outlive us (see |set_client()|). | |
| 47 static std::unique_ptr<OutputStreamFile> Create( | |
| 48 Client* client, | |
| 49 mojo::InterfaceRequest<mojo::files::File> request); | |
| 50 | |
| 51 ~OutputStreamFile() override; | |
| 52 | |
| 53 // Sets the client (which may be null, in which case all |Write()|s to the | |
| 54 // stream "file" will just succeed). If non-null, |client| must be valid | |
| 55 // whenever the run (a.k.a. message) loop is run, i.e., whenever a client | |
| 56 // method may be called. | |
| 57 void set_client(Client* client) { client_ = client; } | |
| 58 | |
| 59 private: | |
| 60 OutputStreamFile(Client* client, | |
| 61 mojo::InterfaceRequest<mojo::files::File> request); | |
| 62 | |
| 63 // We should only be deleted by "ourself" (via the strong binding). | |
| 64 friend class mojo::Binding<mojo::files::File>; | |
| 65 | |
| 66 // |mojo::files::File| implementation: | |
| 67 void Close(const CloseCallback& callback) override; | |
| 68 void Read(uint32_t num_bytes_to_read, | |
| 69 int64_t offset, | |
| 70 mojo::files::Whence whence, | |
| 71 const ReadCallback& callback) override; | |
| 72 void Write(mojo::Array<uint8_t> bytes_to_write, | |
| 73 int64_t offset, | |
| 74 mojo::files::Whence whence, | |
| 75 const WriteCallback& callback) override; | |
| 76 void ReadToStream(mojo::ScopedDataPipeProducerHandle source, | |
| 77 int64_t offset, | |
| 78 mojo::files::Whence whence, | |
| 79 int64_t num_bytes_to_read, | |
| 80 const ReadToStreamCallback& callback) override; | |
| 81 void WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, | |
| 82 int64_t offset, | |
| 83 mojo::files::Whence whence, | |
| 84 const WriteFromStreamCallback& callback) override; | |
| 85 void Tell(const TellCallback& callback) override; | |
| 86 void Seek(int64_t offset, | |
| 87 mojo::files::Whence whence, | |
| 88 const SeekCallback& callback) override; | |
| 89 void Stat(const StatCallback& callback) override; | |
| 90 void Truncate(int64_t size, const TruncateCallback& callback) override; | |
| 91 void Touch(mojo::files::TimespecOrNowPtr atime, | |
| 92 mojo::files::TimespecOrNowPtr mtime, | |
| 93 const TouchCallback& callback) override; | |
| 94 void Dup(mojo::InterfaceRequest<mojo::files::File> file, | |
| 95 const DupCallback& callback) override; | |
| 96 void Reopen(mojo::InterfaceRequest<mojo::files::File> file, | |
| 97 uint32_t open_flags, | |
| 98 const ReopenCallback& callback) override; | |
| 99 void AsBuffer(const AsBufferCallback& callback) override; | |
| 100 void Ioctl(uint32_t request, | |
| 101 mojo::Array<uint32_t> in_values, | |
| 102 const IoctlCallback& callback) override; | |
| 103 | |
| 104 Client* client_; | |
| 105 bool is_closed_; | |
| 106 | |
| 107 mojo::Binding<mojo::files::File> binding_; | |
| 108 | |
| 109 MOJO_DISALLOW_COPY_AND_ASSIGN(OutputStreamFile); | |
| 110 }; | |
| 111 | |
| 112 } // namespace files_impl | |
| 113 | |
| 114 #endif // MOJO_SERVICES_FILES_PUBLIC_CPP_OUTPUT_STREAM_FILE_H_ | |
| OLD | NEW |