#ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_
#define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_H_
#include <fcntl.h>
#include <signal.h>
#include <cstdio>
#include <memory>
#include <string>
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/process/process.h"
#include "chromeos/process_proxy/process_output_watcher.h"
namespace base {
class SingleThreadTaskRunner;
class TaskRunner;
}
namespace chromeos {
class ProcessOutputWatcher;
}
namespace chromeos {
class ProcessProxy : public base::RefCountedThreadSafe<ProcessProxy> {
public:
using OutputCallback =
base::RepeatingCallback<void(ProcessOutputType output_type,
const std::string& output_data)>;
ProcessProxy();
ProcessProxy(const ProcessProxy&) = delete;
ProcessProxy& operator=(const ProcessProxy&) = delete;
bool Open(const base::CommandLine& cmdline,
const std::string& user_id_hash,
std::string* id);
bool StartWatchingOutput(
const scoped_refptr<base::SingleThreadTaskRunner>& watcher_runner,
const scoped_refptr<base::SequencedTaskRunner>& callback_runner,
const OutputCallback& callback);
void Write(const std::string& text, base::OnceCallback<void(bool)> callback);
void Close();
bool OnTerminalResize(int width, int height);
void AckOutput();
const base::Process* GetProcessForTesting();
private:
friend class base::RefCountedThreadSafe<ProcessProxy>;
~ProcessProxy();
bool CreatePseudoTerminalPair(int *pt_pair);
bool LaunchProcess(const base::CommandLine& cmdline,
const std::string& user_id_hash,
int slave_fd,
std::string* id);
void OnProcessOutput(ProcessOutputType type, const std::string& output);
void CallOnProcessOutputCallback(ProcessOutputType type,
const std::string& output);
void StopWatching();
void CloseFdPair(int* pipe);
void CloseFd(int* fd);
void ClearFdPair(int* pipe);
bool process_launched_;
bool callback_set_;
OutputCallback callback_;
scoped_refptr<base::TaskRunner> callback_runner_;
scoped_refptr<base::SingleThreadTaskRunner> watcher_runner_;
std::unique_ptr<ProcessOutputWatcher> output_watcher_;
base::Process process_;
int pt_pair_[2];
};
}
#endif