This source file includes following definitions.
- ProcessOutputTypeToString
- Get
- OpenProcess
- SendInput
- CloseProcess
- OnTerminalResize
- OnProcessOutput
#include "chromeos/process_proxy/process_proxy_registry.h"
#include "base/bind.h"
namespace chromeos {
namespace {
const char kWatcherThreadName[] = "ProcessWatcherThread";
const char kStdoutOutputType[] = "stdout";
const char kStderrOutputType[] = "stderr";
const char kExitOutputType[] = "exit";
const char* ProcessOutputTypeToString(ProcessOutputType type) {
switch (type) {
case PROCESS_OUTPUT_TYPE_OUT:
return kStdoutOutputType;
case PROCESS_OUTPUT_TYPE_ERR:
return kStderrOutputType;
case PROCESS_OUTPUT_TYPE_EXIT:
return kExitOutputType;
default:
return NULL;
}
}
static base::LazyInstance<ProcessProxyRegistry> g_process_proxy_registry =
LAZY_INSTANCE_INITIALIZER;
}
ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo() {
}
ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo(
const ProcessProxyInfo& other) {
DCHECK(!other.proxy.get() && !other.watcher_thread.get());
}
ProcessProxyRegistry::ProcessProxyInfo::~ProcessProxyInfo() {
}
ProcessProxyRegistry::ProcessProxyRegistry() {
}
ProcessProxyRegistry::~ProcessProxyRegistry() {
DetachFromThread();
while (!proxy_map_.empty())
CloseProcess(proxy_map_.begin()->first);
}
ProcessProxyRegistry* ProcessProxyRegistry::Get() {
return g_process_proxy_registry.Pointer();
}
bool ProcessProxyRegistry::OpenProcess(
const std::string& command,
pid_t* pid,
const ProcessOutputCallbackWithPid& callback) {
DCHECK(CalledOnValidThread());
scoped_ptr<base::Thread> watcher_thread(new base::Thread(kWatcherThreadName));
if (!watcher_thread->Start()) {
return false;
}
scoped_refptr<ProcessProxy> proxy(new ProcessProxy());
if (!proxy->Open(command, pid))
return false;
if (!proxy->StartWatchingOnThread(watcher_thread.get(),
base::Bind(&ProcessProxyRegistry::OnProcessOutput,
base::Unretained(this), *pid))) {
proxy->Close();
watcher_thread->Stop();
return false;
}
DCHECK(proxy_map_.find(*pid) == proxy_map_.end());
ProcessProxyInfo& info = proxy_map_[*pid];
info.proxy.swap(proxy);
info.watcher_thread.reset(watcher_thread.release());
info.process_id = *pid;
info.callback = callback;
return true;
}
bool ProcessProxyRegistry::SendInput(pid_t pid, const std::string& data) {
DCHECK(CalledOnValidThread());
std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid);
if (it == proxy_map_.end())
return false;
return it->second.proxy->Write(data);
}
bool ProcessProxyRegistry::CloseProcess(pid_t pid) {
DCHECK(CalledOnValidThread());
std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid);
if (it == proxy_map_.end())
return false;
it->second.proxy->Close();
it->second.watcher_thread->Stop();
proxy_map_.erase(it);
return true;
}
bool ProcessProxyRegistry::OnTerminalResize(pid_t pid, int width, int height) {
DCHECK(CalledOnValidThread());
std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid);
if (it == proxy_map_.end())
return false;
return it->second.proxy->OnTerminalResize(width, height);
}
void ProcessProxyRegistry::OnProcessOutput(pid_t pid,
ProcessOutputType type, const std::string& data) {
DCHECK(CalledOnValidThread());
const char* type_str = ProcessOutputTypeToString(type);
DCHECK(type_str);
std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid);
if (it == proxy_map_.end())
return;
it->second.callback.Run(pid, std::string(type_str), data);
if (type == PROCESS_OUTPUT_TYPE_EXIT)
CloseProcess(pid);
}
}