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.
5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_
6 #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_
14 #include "base/memory/ref_counted.h"
15 #include "chromeos/process_proxy/process_output_watcher.h"
18 class SingleThreadTaskRunner
;
23 class ProcessOutputWatcher
;
24 } // namespace chromeos
28 // Proxy to a single ChromeOS process.
29 // This is refcounted. Note that output watcher, when it gets triggered owns a
30 // a callback with ref to this, so in order for this to be freed, the watcher
31 // must be destroyed. This is done in Close.
32 class ProcessProxy
: public base::RefCountedThreadSafe
<ProcessProxy
> {
36 // Opens a process using command |command|. |pid| is set to new process' pid.
37 bool Open(const std::string
& command
, pid_t
* pid
);
39 bool StartWatchingOutput(
40 const scoped_refptr
<base::SingleThreadTaskRunner
>& watcher_runner
,
41 const ProcessOutputCallback
& callback
);
43 // Sends some data to the process.
44 bool Write(const std::string
& text
);
46 // Closes the process.
47 // Must be called if we want this to be eventually deleted.
50 // Notifies underlaying process of terminal size change.
51 bool OnTerminalResize(int width
, int height
);
54 friend class base::RefCountedThreadSafe
<ProcessProxy
>;
55 // We want this be used as ref counted object only.
58 // Create master and slave end of pseudo terminal that will be used to
59 // communicate with process.
60 // pt_pair[0] -> master, pt_pair[1] -> slave.
61 // pt_pair must be allocated (to size at least 2).
62 bool CreatePseudoTerminalPair(int *pt_pair
);
64 bool LaunchProcess(const std::string
& command
, int slave_fd
, pid_t
* pid
);
66 // Gets called by output watcher when the process writes something to its
68 void OnProcessOutput(ProcessOutputType type
, const std::string
& output
);
69 void CallOnProcessOutputCallback(ProcessOutputType type
,
70 const std::string
& output
);
74 // Expects array of 2 file descripters.
75 void CloseFdPair(int* pipe
);
76 // Expects pointer to single file descriptor.
77 void CloseFd(int* fd
);
78 // Expects array of 2 file descripters.
79 void ClearFdPair(int* pipe
);
81 bool process_launched_
;
85 ProcessOutputCallback callback_
;
86 scoped_refptr
<base::TaskRunner
> callback_runner_
;
87 scoped_refptr
<base::SingleThreadTaskRunner
> watcher_runner_
;
89 scoped_ptr
<ProcessOutputWatcher
> output_watcher_
;
93 DISALLOW_COPY_AND_ASSIGN(ProcessProxy
);
96 } // namespace chromeos
98 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_