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 // |MotermDriver| is a class providing a |mojo.files.File| interface, |
| 6 // implementing termios-type features (e.g., line editing; TODO(vtl): lots to do |
| 7 // here), and appropriately processing bytes to/from the terminal (which gets |
| 8 // and sends "raw" data). In essence, this class includes what would |
| 9 // traditionally be called the terminal driver in a Unix kernel. |
| 10 |
| 11 #ifndef APPS_MOTERM_MOTERM_DRIVER_H_ |
| 12 #define APPS_MOTERM_MOTERM_DRIVER_H_ |
| 13 |
| 14 #include <stddef.h> |
| 15 #include <stdint.h> |
| 16 |
| 17 #include <deque> |
| 18 |
| 19 #include "base/macros.h" |
| 20 #include "base/memory/weak_ptr.h" |
| 21 #include "mojo/public/cpp/bindings/interface_request.h" |
| 22 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 23 #include "mojo/services/files/public/interfaces/file.mojom.h" |
| 24 |
| 25 // TODO(vtl): Maybe we should Mojo-fy the driver and run it as a separate app? |
| 26 class MotermDriver : public mojo::files::File { |
| 27 public: |
| 28 // The |Client| is basically the terminal implementation itself, to which |
| 29 // processed output (from an application, to the "file", through the driver) |
| 30 // is sent. It also receives other notifications (e.g., when the "file" is |
| 31 // closed or otherwise "broken"). |
| 32 class Client { |
| 33 public: |
| 34 // Called when we receive data (for the client to "display"). |
| 35 // TODO(vtl): Maybe add a way to throttle (e.g., for the client to not |
| 36 // accept all the data). |
| 37 // TODO(vtl): It's "probably OK" to call |Detach()| from inside this method, |
| 38 // but not verified. |
| 39 virtual void OnDataReceived(const void* bytes, size_t num_bytes) = 0; |
| 40 |
| 41 // Called when the terminal "file" is closed (via |Close()|. (The client may |
| 42 // optionally call |Detach()| in response.) |
| 43 virtual void OnClosed() = 0; |
| 44 |
| 45 // Called when this object is destroyed (which will happen if the other end |
| 46 // of the message pipe is closed). The client must not call |Detach()| in |
| 47 // response. (Obviously, this will only be called if the client has not |
| 48 // called |Detach()|.) |
| 49 virtual void OnDestroyed() = 0; |
| 50 }; |
| 51 |
| 52 // Static factory method. |client| must either outlive us, or if not call |
| 53 // |Detach()| before being destroyed. |
| 54 static base::WeakPtr<MotermDriver> Create( |
| 55 Client* client, |
| 56 mojo::InterfaceRequest<mojo::files::File> request); |
| 57 |
| 58 // Called by the client when it wishes to detach (i.e., no longer receive |
| 59 // calls). This will terminate the connection and us. |
| 60 void Detach(); |
| 61 |
| 62 // Called by the client when it has data to send. |
| 63 // TODO(vtl): Add a way to throttle. (We could do this via an |OnDataSent()| |
| 64 // ack, or in other ways as well, e.g., reporting how much data we have |
| 65 // enqueued.) For now, just queue data infinitely. |
| 66 // TODO(vtl): This is misnamed -- it's really "here's some input". |
| 67 void SendData(const void* bytes, size_t num_bytes); |
| 68 |
| 69 private: |
| 70 struct PendingRead { |
| 71 PendingRead(uint32_t num_bytes, const ReadCallback& callback); |
| 72 ~PendingRead(); |
| 73 |
| 74 uint32_t num_bytes; |
| 75 ReadCallback callback; |
| 76 }; |
| 77 |
| 78 MotermDriver(Client* client, |
| 79 mojo::InterfaceRequest<mojo::files::File> request); |
| 80 |
| 81 // We should only be deleted by "ourself" (via the strong binding). |
| 82 friend class mojo::StrongBinding<mojo::files::File>; |
| 83 ~MotermDriver() override; |
| 84 |
| 85 void HandleCanonicalModeInput(uint8_t ch); |
| 86 void CompletePendingReads(); |
| 87 void FlushInputLine(); |
| 88 |
| 89 void HandleOutput(const uint8_t* bytes, size_t num_bytes); |
| 90 |
| 91 // |mojo::files::File| implementation: |
| 92 void Close(const CloseCallback& callback) override; |
| 93 void Read(uint32_t num_bytes_to_read, |
| 94 int64_t offset, |
| 95 mojo::files::Whence whence, |
| 96 const ReadCallback& callback) override; |
| 97 void Write(mojo::Array<uint8_t> bytes_to_write, |
| 98 int64_t offset, |
| 99 mojo::files::Whence whence, |
| 100 const WriteCallback& callback) override; |
| 101 void ReadToStream(mojo::ScopedDataPipeProducerHandle source, |
| 102 int64_t offset, |
| 103 mojo::files::Whence whence, |
| 104 int64_t num_bytes_to_read, |
| 105 const ReadToStreamCallback& callback) override; |
| 106 void WriteFromStream(mojo::ScopedDataPipeConsumerHandle sink, |
| 107 int64_t offset, |
| 108 mojo::files::Whence whence, |
| 109 const WriteFromStreamCallback& callback) override; |
| 110 void Tell(const TellCallback& callback) override; |
| 111 void Seek(int64_t offset, |
| 112 mojo::files::Whence whence, |
| 113 const SeekCallback& callback) override; |
| 114 void Stat(const StatCallback& callback) override; |
| 115 void Truncate(int64_t size, const TruncateCallback& callback) override; |
| 116 void Touch(mojo::files::TimespecOrNowPtr atime, |
| 117 mojo::files::TimespecOrNowPtr mtime, |
| 118 const TouchCallback& callback) override; |
| 119 void Dup(mojo::InterfaceRequest<mojo::files::File> file, |
| 120 const DupCallback& callback) override; |
| 121 void Reopen(mojo::InterfaceRequest<mojo::files::File> file, |
| 122 uint32_t open_flags, |
| 123 const ReopenCallback& callback) override; |
| 124 void AsBuffer(const AsBufferCallback& callback) override; |
| 125 void Ioctl(uint32_t request, |
| 126 mojo::Array<uint32_t> in_values, |
| 127 const IoctlCallback& callback) override; |
| 128 |
| 129 Client* client_; // Set until |Detach()| is called. |
| 130 bool is_closed_; |
| 131 |
| 132 std::deque<uint8_t> send_data_queue_; |
| 133 // For canonical mode. Feeds into |send_data_queue_|. |
| 134 std::deque<uint8_t> input_line_queue_; |
| 135 std::deque<PendingRead> pending_read_queue_; |
| 136 |
| 137 // Note: This binding must be after |pending_read_queue_|, so that it gets |
| 138 // torn down before the callbacks in |pending_read_queue_| are destroyed |
| 139 // (otherwise we'll get |DCHECK()| failures). |
| 140 mojo::StrongBinding<mojo::files::File> binding_; |
| 141 |
| 142 // Terminal driver settings: |
| 143 // (Names roughly correspond to termios names.) |
| 144 // TODO(vtl): Add a way to set/change settings (including using ioctls). |
| 145 // TODO(vtl): Our support for termios requirements is very far from complete. |
| 146 |
| 147 // Input settings: |
| 148 |
| 149 // Canonical, a.k.a. "cooked", mode. If true, will echo, line buffer, etc. If |
| 150 // false, no input processing is done (other than perhaps to slightly |
| 151 // time-delay the availability of input -- we don't do this). |
| 152 bool icanon_; |
| 153 |
| 154 // If true, will convert input CRs to NLs (in canonical mode). |
| 155 bool icrnl_; |
| 156 |
| 157 uint8_t veof_; |
| 158 uint8_t verase_; |
| 159 |
| 160 // Output settings: |
| 161 // If true, will convert output CRs to CR-NL pairs. |
| 162 bool onlcr_; |
| 163 |
| 164 base::WeakPtrFactory<MotermDriver> weak_factory_; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(MotermDriver); |
| 167 }; |
| 168 |
| 169 #endif // APPS_MOTERM_MOTERM_DRIVER_H_ |
OLD | NEW |