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