[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 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 8 | #include <stddef.h> |
9 | |||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 10 | #include <string> |
11 | |||||
12 | #include "base/callback.h" | ||||
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 13 | #include "base/files/file.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 14 | #include "base/macros.h" |
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
16 | #include "base/message_loop/message_loop.h" | ||||
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 17 | #include "chromeos/chromeos_export.h" |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 18 | |
[email protected] | 3a01162b | 2013-03-12 20:22:48 | [diff] [blame] | 19 | namespace chromeos { |
20 | |||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 21 | enum ProcessOutputType { |
22 | PROCESS_OUTPUT_TYPE_OUT, | ||||
[email protected] | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 23 | PROCESS_OUTPUT_TYPE_EXIT |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 24 | }; |
25 | |||||
tbarzic | 170f12c | 2016-01-08 05:15:29 | [diff] [blame] | 26 | typedef base::Callback<void(ProcessOutputType, |
27 | const std::string&, | ||||
28 | const base::Closure&)> ProcessOutputCallback; | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 29 | |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 30 | // Observes output on |out_fd| and invokes |callback| when some output is |
31 | // detected. It assumes UTF8 output. | ||||
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 32 | class CHROMEOS_EXPORT ProcessOutputWatcher |
33 | : public base::MessageLoopForIO::Watcher { | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 34 | public: |
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 35 | ProcessOutputWatcher(int out_fd, const ProcessOutputCallback& callback); |
36 | ~ProcessOutputWatcher() override; | ||||
tbarzic | 296875c | 2015-05-14 19:40:37 | [diff] [blame] | 37 | |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 38 | void Start(); |
39 | |||||
40 | private: | ||||
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 41 | // MessageLoopForIO::Watcher overrides: |
42 | void OnFileCanReadWithoutBlocking(int fd) override; | ||||
43 | void OnFileCanWriteWithoutBlocking(int fd) override; | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 44 | |
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 45 | // Listens to output from fd passed to the constructor. |
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 46 | void WatchProcessOutput(); |
47 | |||||
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 48 | // Reads data from fd and invokes callback |on_read_callback_| with read data. |
49 | void ReadFromFd(int fd); | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 50 | |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 51 | // Checks if the read buffer has any trailing incomplete UTF8 characters and |
52 | // returns the read buffer size without them. | ||||
53 | size_t OutputSizeWithoutIncompleteUTF8(); | ||||
54 | |||||
55 | // Processes new |read_buffer_| state and notifies observer about new process | ||||
56 | // output. | ||||
tbarzic | 170f12c | 2016-01-08 05:15:29 | [diff] [blame] | 57 | void ReportOutput(ProcessOutputType type, |
58 | size_t new_bytes_count, | ||||
59 | const base::Closure& callback); | ||||
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 60 | |
tbarzic | 170f12c | 2016-01-08 05:15:29 | [diff] [blame] | 61 | char read_buffer_[4096]; |
[email protected] | 65bbd9d2 | 2014-05-15 20:34:37 | [diff] [blame] | 62 | // Maximum read buffer content size. |
63 | size_t read_buffer_capacity_; | ||||
64 | // Current read bufferi content size. | ||||
65 | size_t read_buffer_size_; | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 66 | |
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 67 | // Contains file descsriptor to which watched process output is written. |
68 | base::File process_output_file_; | ||||
69 | base::MessageLoopForIO::FileDescriptorWatcher output_file_watcher_; | ||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 70 | |
71 | // Callback that will be invoked when some output is detected. | ||||
72 | ProcessOutputCallback on_read_callback_; | ||||
73 | |||||
tbarzic | 6ca5055 | 2015-08-10 22:32:45 | [diff] [blame] | 74 | base::WeakPtrFactory<ProcessOutputWatcher> weak_factory_; |
75 | |||||
[email protected] | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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_ |