| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "chromeos/chromeos_export.h" | 11 #include "chromeos/chromeos_export.h" |
| 12 | 12 |
| 13 namespace { | |
| 14 | |
| 15 const int kReadBufferSize = 256; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 namespace chromeos { | 13 namespace chromeos { |
| 20 | 14 |
| 21 enum ProcessOutputType { | 15 enum ProcessOutputType { |
| 22 PROCESS_OUTPUT_TYPE_OUT, | 16 PROCESS_OUTPUT_TYPE_OUT, |
| 23 PROCESS_OUTPUT_TYPE_ERR, | 17 PROCESS_OUTPUT_TYPE_ERR, |
| 24 PROCESS_OUTPUT_TYPE_EXIT | 18 PROCESS_OUTPUT_TYPE_EXIT |
| 25 }; | 19 }; |
| 26 | 20 |
| 27 typedef base::Callback<void(ProcessOutputType, const std::string&)> | 21 typedef base::Callback<void(ProcessOutputType, const std::string&)> |
| 28 ProcessOutputCallback; | 22 ProcessOutputCallback; |
| 29 | 23 |
| 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. |
| 30 // This class should live on its own thread because running class makes | 27 // This class should live on its own thread because running class makes |
| 31 // underlying thread block. It deletes itself when watching is stopped. | 28 // underlying thread block. It deletes itself when watching is stopped. |
| 32 class CHROMEOS_EXPORT ProcessOutputWatcher { | 29 class CHROMEOS_EXPORT ProcessOutputWatcher { |
| 33 public: | 30 public: |
| 34 ProcessOutputWatcher(int out_fd, int stop_fd, | 31 ProcessOutputWatcher(int out_fd, int stop_fd, |
| 35 const ProcessOutputCallback& callback); | 32 const ProcessOutputCallback& callback); |
| 36 | 33 |
| 37 // This will block current thread!!!! | 34 // This will block current thread!!!! |
| 38 void Start(); | 35 void Start(); |
| 39 | 36 |
| 40 private: | 37 private: |
| 41 // The object will destroy itself when it stops watching process output. | 38 // The object will destroy itself when it stops watching process output. |
| 42 ~ProcessOutputWatcher(); | 39 ~ProcessOutputWatcher(); |
| 43 | 40 |
| 44 // Listens to output from supplied fds. It guarantees data written to one fd | 41 // 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 | 42 // will be reported in order that it has been written (this is not true across |
| 46 // fds, it would be nicer if it was). | 43 // fds, it would be nicer if it was). |
| 47 void WatchProcessOutput(); | 44 void WatchProcessOutput(); |
| 48 | 45 |
| 49 // Verifies that fds that we got are properly set. | 46 // Verifies that fds that we got are properly set. |
| 50 void VerifyFileDescriptor(int fd); | 47 void VerifyFileDescriptor(int fd); |
| 51 | 48 |
| 52 // Reads data from fd, and when it's done, invokes callback function. | 49 // Reads data from fd, and when it's done, invokes callback function. |
| 53 void ReadFromFd(ProcessOutputType type, int* fd); | 50 void ReadFromFd(ProcessOutputType type, int* fd); |
| 54 | 51 |
| 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 |
| 55 // It will just delete this. | 60 // It will just delete this. |
| 56 void OnStop(); | 61 void OnStop(); |
| 57 | 62 |
| 58 char read_buffer_[kReadBufferSize]; | 63 char read_buffer_[256]; |
| 59 ssize_t read_buffer_size_; | 64 // Maximum read buffer content size. |
| 65 size_t read_buffer_capacity_; |
| 66 // Current read bufferi content size. |
| 67 size_t read_buffer_size_; |
| 60 | 68 |
| 61 int out_fd_; | 69 int out_fd_; |
| 62 int stop_fd_; | 70 int stop_fd_; |
| 63 int max_fd_; | 71 int max_fd_; |
| 64 | 72 |
| 65 // Callback that will be invoked when some output is detected. | 73 // Callback that will be invoked when some output is detected. |
| 66 ProcessOutputCallback on_read_callback_; | 74 ProcessOutputCallback on_read_callback_; |
| 67 | 75 |
| 68 DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher); | 76 DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher); |
| 69 }; | 77 }; |
| 70 | 78 |
| 71 } // namespace chromeos | 79 } // namespace chromeos |
| 72 | 80 |
| 73 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ | 81 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_ |
| OLD | NEW |