| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ | 5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
| 6 #define CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ | 6 #define CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 15 #include "base/files/file_descriptor_watcher_posix.h" |
| 14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "chromeos/chromeos_export.h" | 18 #include "chromeos/chromeos_export.h" |
| 18 | 19 |
| 19 namespace chromeos { | 20 namespace chromeos { |
| 20 | 21 |
| 21 enum ProcessOutputType { | 22 enum ProcessOutputType { |
| 22 PROCESS_OUTPUT_TYPE_OUT, | 23 PROCESS_OUTPUT_TYPE_OUT, |
| 23 PROCESS_OUTPUT_TYPE_EXIT | 24 PROCESS_OUTPUT_TYPE_EXIT |
| 24 }; | 25 }; |
| 25 | 26 |
| 26 typedef base::Callback<void(ProcessOutputType, | 27 typedef base::Callback<void(ProcessOutputType, |
| 27 const std::string&, | 28 const std::string&, |
| 28 const base::Closure&)> ProcessOutputCallback; | 29 const base::Closure&)> ProcessOutputCallback; |
| 29 | 30 |
| 30 // Observes output on |out_fd| and invokes |callback| when some output is | 31 // Observes output on |out_fd| and invokes |callback| when some output is |
| 31 // detected. It assumes UTF8 output. | 32 // detected. It assumes UTF8 output. |
| 32 class CHROMEOS_EXPORT ProcessOutputWatcher | 33 class CHROMEOS_EXPORT ProcessOutputWatcher { |
| 33 : public base::MessageLoopForIO::Watcher { | |
| 34 public: | 34 public: |
| 35 ProcessOutputWatcher(int out_fd, const ProcessOutputCallback& callback); | 35 ProcessOutputWatcher(int out_fd, const ProcessOutputCallback& callback); |
| 36 ~ProcessOutputWatcher() override; | 36 ~ProcessOutputWatcher(); |
| 37 | 37 |
| 38 void Start(); | 38 void Start(); |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 // MessageLoopForIO::Watcher overrides: | 41 // Called when |process_output_file_| is readable without blocking. |
| 42 void OnFileCanReadWithoutBlocking(int fd) override; | 42 void OnProcessOutputCanReadWithoutBlocking(); |
| 43 void OnFileCanWriteWithoutBlocking(int fd) override; | |
| 44 | 43 |
| 45 // Listens to output from fd passed to the constructor. | 44 // Listens to output from fd passed to the constructor. |
| 46 void WatchProcessOutput(); | 45 void WatchProcessOutput(); |
| 47 | 46 |
| 48 // Reads data from fd and invokes callback |on_read_callback_| with read data. | 47 // Reads data from fd and invokes callback |on_read_callback_| with read data. |
| 49 void ReadFromFd(int fd); | 48 void ReadFromFd(int fd); |
| 50 | 49 |
| 51 // Checks if the read buffer has any trailing incomplete UTF8 characters and | 50 // Checks if the read buffer has any trailing incomplete UTF8 characters and |
| 52 // returns the read buffer size without them. | 51 // returns the read buffer size without them. |
| 53 size_t OutputSizeWithoutIncompleteUTF8(); | 52 size_t OutputSizeWithoutIncompleteUTF8(); |
| 54 | 53 |
| 55 // Processes new |read_buffer_| state and notifies observer about new process | 54 // Processes new |read_buffer_| state and notifies observer about new process |
| 56 // output. | 55 // output. |
| 57 void ReportOutput(ProcessOutputType type, | 56 void ReportOutput(ProcessOutputType type, |
| 58 size_t new_bytes_count, | 57 size_t new_bytes_count, |
| 59 const base::Closure& callback); | 58 const base::Closure& callback); |
| 60 | 59 |
| 61 char read_buffer_[4096]; | 60 char read_buffer_[4096]; |
| 62 // Maximum read buffer content size. | 61 // Maximum read buffer content size. |
| 63 size_t read_buffer_capacity_; | 62 size_t read_buffer_capacity_; |
| 64 // Current read bufferi content size. | 63 // Current read bufferi content size. |
| 65 size_t read_buffer_size_; | 64 size_t read_buffer_size_; |
| 66 | 65 |
| 67 // Contains file descsriptor to which watched process output is written. | 66 // Contains file descsriptor to which watched process output is written. |
| 68 base::File process_output_file_; | 67 base::File process_output_file_; |
| 69 base::MessageLoopForIO::FileDescriptorWatcher output_file_watcher_; | 68 std::unique_ptr<base::FileDescriptorWatcher::Controller> output_file_watcher_; |
| 70 | 69 |
| 71 // Callback that will be invoked when some output is detected. | 70 // Callback that will be invoked when some output is detected. |
| 72 ProcessOutputCallback on_read_callback_; | 71 ProcessOutputCallback on_read_callback_; |
| 73 | 72 |
| 74 base::WeakPtrFactory<ProcessOutputWatcher> weak_factory_; | 73 base::WeakPtrFactory<ProcessOutputWatcher> weak_factory_; |
| 75 | 74 |
| 76 DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher); | 75 DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher); |
| 77 }; | 76 }; |
| 78 | 77 |
| 79 } // namespace chromeos | 78 } // namespace chromeos |
| 80 | 79 |
| 81 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ | 80 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
| OLD | NEW |