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"
24 // Proxy to a single ChromeOS process.
25 // 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
27 // must be destroyed. This is done in Close.
28 class ProcessProxy
: public base::RefCountedThreadSafe
<ProcessProxy
> {
32 // Opens a process using command |command|. |pid| is set to new process' pid.
33 bool Open(const std::string
& command
, pid_t
* pid
);
35 // Triggers watcher object on |watch_thread|. |watch_thread| gets blocked, so
36 // it should not be one of commonly used threads. It should be thread created
37 // specifically for running process output watcher.
38 bool StartWatchingOnThread(base::Thread
* watch_thread
,
39 const ProcessOutputCallback
& callback
);
41 // Sends some data to the process.
42 bool Write(const std::string
& text
);
44 // Closes the process.
45 // Must be called if we want this to be eventually deleted.
48 // Notifies underlaying process of terminal size change.
49 bool OnTerminalResize(int width
, int height
);
52 friend class base::RefCountedThreadSafe
<ProcessProxy
>;
53 // We want this be used as ref counted object only.
56 // Create master and slave end of pseudo terminal that will be used to
57 // communicate with process.
58 // pt_pair[0] -> master, pt_pair[1] -> slave.
59 // pt_pair must be allocated (to size at least 2).
60 bool CreatePseudoTerminalPair(int *pt_pair
);
62 bool LaunchProcess(const std::string
& command
, int slave_fd
, pid_t
* pid
);
64 // Gets called by output watcher when the process writes something to its
66 void OnProcessOutput(ProcessOutputType type
, const std::string
& output
);
67 void CallOnProcessOutputCallback(ProcessOutputType type
,
68 const std::string
& output
);
72 // Methods for cleaning up pipes.
73 void CloseAllFdPairs();
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 void ClearAllFdPairs();
79 // Expects array of 2 file descripters.
80 void ClearFdPair(int* pipe
);
82 bool process_launched_
;
86 ProcessOutputCallback callback_
;
87 scoped_refptr
<base::TaskRunner
> callback_runner_
;
89 bool watcher_started_
;
92 int shutdown_pipe_
[2];
94 DISALLOW_COPY_AND_ASSIGN(ProcessProxy
);
97 } // namespace chromeos
99 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_