| 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 "input stream" ("input" from the point of view of the client -- | |
| 7 // i.e., the client can write/stream input from it, but not write or seek). | |
| 8 | |
| 9 #ifndef MOJO_SERVICES_FILES_PUBLIC_CPP_INPUT_STREAM_FILE_H_ | |
| 10 #define MOJO_SERVICES_FILES_PUBLIC_CPP_INPUT_STREAM_FILE_H_ | |
| 11 | |
| 12 #include <stddef.h> | |
| 13 #include <stdint.h> | |
| 14 | |
| 15 #include <deque> | |
| 16 #include <memory> | |
| 17 | |
| 18 #include "mojo/public/cpp/bindings/array.h" | |
| 19 #include "mojo/public/cpp/bindings/binding.h" | |
| 20 #include "mojo/public/cpp/bindings/callback.h" | |
| 21 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 22 #include "mojo/public/cpp/system/macros.h" | |
| 23 #include "mojo/services/files/public/interfaces/file.mojom.h" | |
| 24 #include "mojo/services/files/public/interfaces/types.mojom.h" | |
| 25 | |
| 26 namespace files_impl { | |
| 27 | |
| 28 class InputStreamFile : public mojo::files::File { | |
| 29 public: | |
| 30 // The |Client| receives data written to the stream "file" as well as other | |
| 31 // notifications (e.g., of the "file" being closed). From any of the methods | |
| 32 // below, the client may choose to destroy the |InputStreamFile|. | |
| 33 class Client { | |
| 34 public: | |
| 35 // Called to request data from the stream "file". This can provide data | |
| 36 // synchronously by returning true and setting |*error| and |*data|, or | |
| 37 // asynchronously by returning false and calling the callback when data is | |
| 38 // available. | |
| 39 // - In both cases, a data buffer with zero data can be used to signify | |
| 40 // "end of stream". | |
| 41 // - In both cases, a non-OK error code can be provided instead (in which | |
| 42 // case any data buffer is ignored). | |
| 43 // - In the asynchronous case, calls to |RequestData()| will not be | |
| 44 // overlapped, i.e., no more calls to |RequestData()| will be made until | |
| 45 // its callback has been called. | |
| 46 // - If this object is destroyed with a callback pending, the callback | |
| 47 // should *not* be called. | |
| 48 // - The callback should not be called from within |RequestData()| | |
| 49 // (instead, the client should complete synchronously by returning a | |
| 50 // buffer). | |
| 51 // - However, from within the callback, |RequestData()| may be called | |
| 52 // again. | |
| 53 // TODO(vtl): We should also support "nonblocking" I/O (i.e., always respond | |
| 54 // immediately, possibly with "would block"). | |
| 55 using RequestDataCallback = mojo::Callback<void(mojo::files::Error error, | |
| 56 mojo::Array<uint8_t> data)>; | |
| 57 virtual bool RequestData(size_t max_num_bytes, | |
| 58 mojo::files::Error* error, | |
| 59 mojo::Array<uint8_t>* data, | |
| 60 const RequestDataCallback& callback) = 0; | |
| 61 | |
| 62 // Called when the stream "file" is closed, via |Close()| or due to the | |
| 63 // other end of the message pipe being closed. (This will not be called due | |
| 64 // the |InputStreamFile| being destroyed.) | |
| 65 virtual void OnClosed() = 0; | |
| 66 | |
| 67 protected: | |
| 68 virtual ~Client() {} | |
| 69 }; | |
| 70 | |
| 71 // Static factory method. |client| may be null, but if not it should typically | |
| 72 // outlive us (see |set_client()|). | |
| 73 static std::unique_ptr<InputStreamFile> Create( | |
| 74 Client* client, | |
| 75 mojo::InterfaceRequest<mojo::files::File> request); | |
| 76 | |
| 77 ~InputStreamFile() override; | |
| 78 | |
| 79 // Sets the client (which may be null, in which case all |Read()|s from the | |
| 80 // stream "file" will just fail). If non-null, |client| must be valid whenever | |
| 81 // the run (a.k.a. message) loop is run, i.e., whenever a client method may be | |
| 82 // called. | |
| 83 // | |
| 84 // Note: Since it's unusual for reads to fail and then succeed later, one | |
| 85 // should avoid setting a null client and then setting a non-null client. | |
| 86 void set_client(Client* client) { client_ = client; } | |
| 87 | |
| 88 private: | |
| 89 InputStreamFile(Client* client, | |
| 90 mojo::InterfaceRequest<mojo::files::File> request); | |
| 91 | |
| 92 // We should only be deleted by "ourself" (via the strong binding). | |
| 93 friend class mojo::Binding<mojo::files::File>; | |
| 94 | |
| 95 // |mojo::files::File| implementation: | |
| 96 void Close(const CloseCallback& callback) override; | |
| 97 void Read(uint32_t num_bytes_to_read, | |
| 98 int64_t offset, | |
| 99 mojo::files::Whence whence, | |
| 100 const ReadCallback& callback) override; | |
| 101 void Write(mojo::Array<uint8_t> bytes_to_write, | |
| 102 int64_t offset, | |
| 103 mojo::files::Whence whence, | |
| 104 const WriteCallback& callback) override; | |
| 105 void ReadToStream(mojo::ScopedDataPipeProducerHandle source, | |
| 106 int64_t offset, | |
| 107 mojo::files::Whence whence, | |
| 108 int64_t num_bytes_to_read, | |
| 109 const ReadToStreamCallback& callback) override; | |
| 110 void WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, | |
| 111 int64_t offset, | |
| 112 mojo::files::Whence whence, | |
| 113 const WriteFromStreamCallback& callback) override; | |
| 114 void Tell(const TellCallback& callback) override; | |
| 115 void Seek(int64_t offset, | |
| 116 mojo::files::Whence whence, | |
| 117 const SeekCallback& callback) override; | |
| 118 void Stat(const StatCallback& callback) override; | |
| 119 void Truncate(int64_t size, const TruncateCallback& callback) override; | |
| 120 void Touch(mojo::files::TimespecOrNowPtr atime, | |
| 121 mojo::files::TimespecOrNowPtr mtime, | |
| 122 const TouchCallback& callback) override; | |
| 123 void Dup(mojo::InterfaceRequest<mojo::files::File> file, | |
| 124 const DupCallback& callback) override; | |
| 125 void Reopen(mojo::InterfaceRequest<mojo::files::File> file, | |
| 126 uint32_t open_flags, | |
| 127 const ReopenCallback& callback) override; | |
| 128 void AsBuffer(const AsBufferCallback& callback) override; | |
| 129 void Ioctl(uint32_t request, | |
| 130 mojo::Array<uint32_t> in_values, | |
| 131 const IoctlCallback& callback) override; | |
| 132 | |
| 133 // Warning: |this| may be destroyed by |StartRead()|. | |
| 134 void StartRead(); | |
| 135 void CompleteRead(mojo::files::Error error, mojo::Array<uint8_t> data); | |
| 136 | |
| 137 struct PendingRead { | |
| 138 PendingRead(uint32_t num_bytes, const ReadCallback& callback); | |
| 139 ~PendingRead(); | |
| 140 | |
| 141 uint32_t num_bytes; | |
| 142 ReadCallback callback; | |
| 143 }; | |
| 144 | |
| 145 Client* client_; | |
| 146 bool is_closed_; | |
| 147 std::deque<PendingRead> pending_read_queue_; | |
| 148 | |
| 149 // If non-null |*was_destroyed_| is set to true on destruction. | |
| 150 bool* was_destroyed_; | |
| 151 | |
| 152 mojo::Binding<mojo::files::File> binding_; | |
| 153 | |
| 154 MOJO_DISALLOW_COPY_AND_ASSIGN(InputStreamFile); | |
| 155 }; | |
| 156 | |
| 157 } // namespace files_impl | |
| 158 | |
| 159 #endif // MOJO_SERVICES_FILES_PUBLIC_CPP_INPUT_STREAM_FILE_H_ | |
| OLD | NEW |