[email protected] | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 5 | #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
| 6 | #define CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/callback.h" |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 11 | #include "chromeos/chromeos_export.h" |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 12 | |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 13 | namespace chromeos { |
| 14 | |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 15 | enum ProcessOutputType { |
| 16 | PROCESS_OUTPUT_TYPE_OUT, |
[email protected] | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 17 | PROCESS_OUTPUT_TYPE_ERR, |
| 18 | PROCESS_OUTPUT_TYPE_EXIT |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 19 | }; |
| 20 | |
[email protected] | e24f876 | 2011-12-20 00:10:04 | [diff] [blame] | 21 | typedef base::Callback<void(ProcessOutputType, const std::string&)> |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 22 | ProcessOutputCallback; |
| 23 | |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 24 | // Observes output on |out_fd| and invokes |callback| when some output is |
| 25 | // detected. It assumes UTF8 output. |
| 26 | // If output is detected in |stop_fd|, the watcher is stopped. |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 27 | // This class should live on its own thread because running class makes |
| 28 | // underlying thread block. It deletes itself when watching is stopped. |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 29 | class CHROMEOS_EXPORT ProcessOutputWatcher { |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 30 | public: |
tbarzic | 296875c | 2015-05-14 19:40:37 | [diff] [blame^] | 31 | // Verifies that fds that we got are properly set. |
| 32 | static bool VerifyFileDescriptor(int fd); |
| 33 | |
[email protected] | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 34 | ProcessOutputWatcher(int out_fd, int stop_fd, |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 35 | const ProcessOutputCallback& callback); |
| 36 | |
| 37 | // This will block current thread!!!! |
| 38 | void Start(); |
| 39 | |
| 40 | private: |
| 41 | // The object will destroy itself when it stops watching process output. |
| 42 | ~ProcessOutputWatcher(); |
| 43 | |
| 44 | // Listens to output from supplied fds. It guarantees data written to one fd |
| 45 | // will be reported in order that it has been written (this is not true across |
| 46 | // fds, it would be nicer if it was). |
| 47 | void WatchProcessOutput(); |
| 48 | |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 49 | // Reads data from fd, and when it's done, invokes callback function. |
[email protected] | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 50 | void ReadFromFd(ProcessOutputType type, int* fd); |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 51 | |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 52 | // Checks if the read buffer has any trailing incomplete UTF8 characters and |
| 53 | // returns the read buffer size without them. |
| 54 | size_t OutputSizeWithoutIncompleteUTF8(); |
| 55 | |
| 56 | // Processes new |read_buffer_| state and notifies observer about new process |
| 57 | // output. |
| 58 | void ReportOutput(ProcessOutputType type, size_t new_bytes_count); |
| 59 | |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 60 | // It will just delete this. |
| 61 | void OnStop(); |
| 62 | |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 63 | char read_buffer_[256]; |
| 64 | // Maximum read buffer content size. |
| 65 | size_t read_buffer_capacity_; |
| 66 | // Current read bufferi content size. |
| 67 | size_t read_buffer_size_; |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 68 | |
| 69 | int out_fd_; |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 70 | int stop_fd_; |
| 71 | int max_fd_; |
| 72 | |
| 73 | // Callback that will be invoked when some output is detected. |
| 74 | ProcessOutputCallback on_read_callback_; |
| 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher); |
| 77 | }; |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 78 | |
| 79 | } // namespace chromeos |
| 80 | |
| 81 | #endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |