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 #ifndef SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_ |
| 6 #define SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <memory> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" |
| 16 #include "mojo/public/cpp/bindings/array.h" |
| 17 #include "mojo/services/files/public/interfaces/file.mojom.h" |
| 18 #include "mojo/services/files/public/interfaces/types.mojom.h" |
| 19 |
| 20 namespace native_support { |
| 21 |
| 22 // Reads from an FD (as possible without blocking) and writes the result to a |
| 23 // |mojo::files::File*|. This object must be used on an I/O thread. |
| 24 class FDToMojoFileRedirector : public base::MessageLoopForIO::Watcher { |
| 25 public: |
| 26 // Both |fd| and |file| must outlive this object. |
| 27 FDToMojoFileRedirector(int fd, mojo::files::File* file, size_t buffer_size); |
| 28 ~FDToMojoFileRedirector() override; |
| 29 |
| 30 void Start(); |
| 31 void Stop(); |
| 32 |
| 33 private: |
| 34 // |base::MessageLoopForIO::Watcher| implementation: |
| 35 void OnFileCanReadWithoutBlocking(int fd) override; |
| 36 void OnFileCanWriteWithoutBlocking(int fd) override; |
| 37 |
| 38 void DoWrite(); |
| 39 void DidWrite(mojo::files::Error error, uint32_t num_bytes_written); |
| 40 |
| 41 const int fd_; |
| 42 mojo::files::File* const file_; |
| 43 const size_t buffer_size_; |
| 44 std::unique_ptr<char[]> buffer_; |
| 45 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 46 |
| 47 size_t num_bytes_; |
| 48 size_t offset_; |
| 49 |
| 50 base::WeakPtrFactory<FDToMojoFileRedirector> weak_factory_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(FDToMojoFileRedirector); |
| 53 }; |
| 54 |
| 55 // Reads from a |mojo::files::File*| and writes the result to an FD. |
| 56 // TODO(vtl): This is very cheesy for now. It just sets |fd| to non-blocking, |
| 57 // and assumes that we'll never block on writing to it. (This is mostly "OK" for |
| 58 // interactive input redirection.) |
| 59 class MojoFileToFDRedirector { |
| 60 public: |
| 61 // Both |file| and |fd| must outlive this object. |
| 62 MojoFileToFDRedirector(mojo::files::File* file, int fd, size_t buffer_size); |
| 63 ~MojoFileToFDRedirector(); |
| 64 |
| 65 void Start(); |
| 66 void Stop(); |
| 67 |
| 68 private: |
| 69 void DidRead(mojo::files::Error error, mojo::Array<uint8_t> bytes_read); |
| 70 |
| 71 mojo::files::File* const file_; |
| 72 const int fd_; |
| 73 const size_t buffer_size_; |
| 74 bool running_; |
| 75 bool read_pending_; |
| 76 |
| 77 base::WeakPtrFactory<MojoFileToFDRedirector> weak_factory_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(MojoFileToFDRedirector); |
| 80 }; |
| 81 |
| 82 } // namespace native_support |
| 83 |
| 84 #endif // SERVICES_NATIVE_SUPPORT_REDIRECTORS_H_ |
OLD | NEW |