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