| 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_PROXY_H_ | 5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_ |
| 6 #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_ | 6 #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_ |
| 7 | 7 |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 | 10 |
| 11 #include <cstdio> | 11 #include <cstdio> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "chromeos/process_proxy/process_output_watcher.h" | 15 #include "chromeos/process_proxy/process_output_watcher.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class SingleThreadTaskRunner; |
| 18 class TaskRunner; | 19 class TaskRunner; |
| 19 class Thread; | |
| 20 } // namespace base | 20 } // namespace base |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 class ProcessOutputWatcher; |
| 24 } // namespace chromeos |
| 25 |
| 26 namespace chromeos { |
| 23 | 27 |
| 24 // Proxy to a single ChromeOS process. | 28 // Proxy to a single ChromeOS process. |
| 25 // This is refcounted. Note that output watcher, when it gets triggered owns a | 29 // This is refcounted. Note that output watcher, when it gets triggered owns a |
| 26 // a callback with ref to this, so in order for this to be freed, the watcher | 30 // a callback with ref to this, so in order for this to be freed, the watcher |
| 27 // must be destroyed. This is done in Close. | 31 // must be destroyed. This is done in Close. |
| 28 class ProcessProxy : public base::RefCountedThreadSafe<ProcessProxy> { | 32 class ProcessProxy : public base::RefCountedThreadSafe<ProcessProxy> { |
| 29 public: | 33 public: |
| 30 ProcessProxy(); | 34 ProcessProxy(); |
| 31 | 35 |
| 32 // Opens a process using command |command|. |pid| is set to new process' pid. | 36 // Opens a process using command |command|. |pid| is set to new process' pid. |
| 33 bool Open(const std::string& command, pid_t* pid); | 37 bool Open(const std::string& command, pid_t* pid); |
| 34 | 38 |
| 35 // Triggers watcher object on |watch_thread|. |watch_thread| gets blocked, so | 39 bool StartWatchingOutput( |
| 36 // it should not be one of commonly used threads. It should be thread created | 40 const scoped_refptr<base::SingleThreadTaskRunner>& watcher_runner, |
| 37 // specifically for running process output watcher. | 41 const ProcessOutputCallback& callback); |
| 38 bool StartWatchingOnThread(base::Thread* watch_thread, | |
| 39 const ProcessOutputCallback& callback); | |
| 40 | 42 |
| 41 // Sends some data to the process. | 43 // Sends some data to the process. |
| 42 bool Write(const std::string& text); | 44 bool Write(const std::string& text); |
| 43 | 45 |
| 44 // Closes the process. | 46 // Closes the process. |
| 45 // Must be called if we want this to be eventually deleted. | 47 // Must be called if we want this to be eventually deleted. |
| 46 void Close(); | 48 void Close(); |
| 47 | 49 |
| 48 // Notifies underlaying process of terminal size change. | 50 // Notifies underlaying process of terminal size change. |
| 49 bool OnTerminalResize(int width, int height); | 51 bool OnTerminalResize(int width, int height); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 60 bool CreatePseudoTerminalPair(int *pt_pair); | 62 bool CreatePseudoTerminalPair(int *pt_pair); |
| 61 | 63 |
| 62 bool LaunchProcess(const std::string& command, int slave_fd, pid_t* pid); | 64 bool LaunchProcess(const std::string& command, int slave_fd, pid_t* pid); |
| 63 | 65 |
| 64 // Gets called by output watcher when the process writes something to its | 66 // Gets called by output watcher when the process writes something to its |
| 65 // output streams. | 67 // output streams. |
| 66 void OnProcessOutput(ProcessOutputType type, const std::string& output); | 68 void OnProcessOutput(ProcessOutputType type, const std::string& output); |
| 67 void CallOnProcessOutputCallback(ProcessOutputType type, | 69 void CallOnProcessOutputCallback(ProcessOutputType type, |
| 68 const std::string& output); | 70 const std::string& output); |
| 69 | 71 |
| 70 bool StopWatching(); | 72 void StopWatching(); |
| 71 | 73 |
| 72 // Methods for cleaning up pipes. | |
| 73 void CloseAllFdPairs(); | |
| 74 // Expects array of 2 file descripters. | 74 // Expects array of 2 file descripters. |
| 75 void CloseFdPair(int* pipe); | 75 void CloseFdPair(int* pipe); |
| 76 // Expects pointer to single file descriptor. | 76 // Expects pointer to single file descriptor. |
| 77 void CloseFd(int* fd); | 77 void CloseFd(int* fd); |
| 78 void ClearAllFdPairs(); | |
| 79 // Expects array of 2 file descripters. | 78 // Expects array of 2 file descripters. |
| 80 void ClearFdPair(int* pipe); | 79 void ClearFdPair(int* pipe); |
| 81 | 80 |
| 82 bool process_launched_; | 81 bool process_launched_; |
| 83 pid_t pid_; | 82 pid_t pid_; |
| 84 | 83 |
| 85 bool callback_set_; | 84 bool callback_set_; |
| 86 ProcessOutputCallback callback_; | 85 ProcessOutputCallback callback_; |
| 87 scoped_refptr<base::TaskRunner> callback_runner_; | 86 scoped_refptr<base::TaskRunner> callback_runner_; |
| 87 scoped_refptr<base::SingleThreadTaskRunner> watcher_runner_; |
| 88 | 88 |
| 89 bool watcher_started_; | 89 scoped_ptr<ProcessOutputWatcher> output_watcher_; |
| 90 | 90 |
| 91 int pt_pair_[2]; | 91 int pt_pair_[2]; |
| 92 int shutdown_pipe_[2]; | |
| 93 | 92 |
| 94 DISALLOW_COPY_AND_ASSIGN(ProcessProxy); | 93 DISALLOW_COPY_AND_ASSIGN(ProcessProxy); |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 } // namespace chromeos | 96 } // namespace chromeos |
| 98 | 97 |
| 99 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_ | 98 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_ |
| OLD | NEW |