| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/process_proxy/process_proxy_registry.h" | 5 #include "chromeos/process_proxy/process_proxy_registry.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 | 8 |
| 9 namespace chromeos { | 9 namespace chromeos { |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const char kWatcherThreadName[] = "ProcessWatcherThread"; | 13 const char kWatcherThreadName[] = "ProcessWatcherThread"; |
| 14 | 14 |
| 15 const char kStdoutOutputType[] = "stdout"; | 15 const char kStdoutOutputType[] = "stdout"; |
| 16 const char kStderrOutputType[] = "stderr"; | |
| 17 const char kExitOutputType[] = "exit"; | 16 const char kExitOutputType[] = "exit"; |
| 18 | 17 |
| 19 const char* ProcessOutputTypeToString(ProcessOutputType type) { | 18 const char* ProcessOutputTypeToString(ProcessOutputType type) { |
| 20 switch (type) { | 19 switch (type) { |
| 21 case PROCESS_OUTPUT_TYPE_OUT: | 20 case PROCESS_OUTPUT_TYPE_OUT: |
| 22 return kStdoutOutputType; | 21 return kStdoutOutputType; |
| 23 case PROCESS_OUTPUT_TYPE_ERR: | |
| 24 return kStderrOutputType; | |
| 25 case PROCESS_OUTPUT_TYPE_EXIT: | 22 case PROCESS_OUTPUT_TYPE_EXIT: |
| 26 return kExitOutputType; | 23 return kExitOutputType; |
| 27 default: | 24 default: |
| 28 return NULL; | 25 return NULL; |
| 29 } | 26 } |
| 30 } | 27 } |
| 31 | 28 |
| 32 static base::LazyInstance<ProcessProxyRegistry> g_process_proxy_registry = | 29 static base::LazyInstance<ProcessProxyRegistry> g_process_proxy_registry = |
| 33 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| 34 | 31 |
| 35 } // namespace | 32 } // namespace |
| 36 | 33 |
| 37 ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo() { | 34 ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo() { |
| 38 } | 35 } |
| 39 | 36 |
| 40 ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo( | 37 ProcessProxyRegistry::ProcessProxyInfo::ProcessProxyInfo( |
| 41 const ProcessProxyInfo& other) { | 38 const ProcessProxyInfo& other) { |
| 42 // This should be called with empty info only. | 39 // This should be called with empty info only. |
| 43 DCHECK(!other.proxy.get() && !other.watcher_thread.get()); | 40 DCHECK(!other.proxy.get()); |
| 44 } | 41 } |
| 45 | 42 |
| 46 ProcessProxyRegistry::ProcessProxyInfo::~ProcessProxyInfo() { | 43 ProcessProxyRegistry::ProcessProxyInfo::~ProcessProxyInfo() { |
| 47 } | 44 } |
| 48 | 45 |
| 49 ProcessProxyRegistry::ProcessProxyRegistry() { | 46 ProcessProxyRegistry::ProcessProxyRegistry() { |
| 50 } | 47 } |
| 51 | 48 |
| 52 ProcessProxyRegistry::~ProcessProxyRegistry() { | 49 ProcessProxyRegistry::~ProcessProxyRegistry() { |
| 53 // TODO(tbarzic): Fix issue with ProcessProxyRegistry being destroyed | 50 // TODO(tbarzic): Fix issue with ProcessProxyRegistry being destroyed |
| 54 // on a different thread (it's a LazyInstance). | 51 // on a different thread (it's a LazyInstance). |
| 55 DetachFromThread(); | 52 DetachFromThread(); |
| 56 | 53 |
| 54 ShutDown(); |
| 55 } |
| 56 |
| 57 void ProcessProxyRegistry::ShutDown() { |
| 57 // Close all proxies we own. | 58 // Close all proxies we own. |
| 58 while (!proxy_map_.empty()) | 59 while (!proxy_map_.empty()) |
| 59 CloseProcess(proxy_map_.begin()->first); | 60 CloseProcess(proxy_map_.begin()->first); |
| 61 |
| 62 if (watcher_thread_) { |
| 63 watcher_thread_->Stop(); |
| 64 watcher_thread_.reset(); |
| 65 } |
| 60 } | 66 } |
| 61 | 67 |
| 62 // static | 68 // static |
| 63 ProcessProxyRegistry* ProcessProxyRegistry::Get() { | 69 ProcessProxyRegistry* ProcessProxyRegistry::Get() { |
| 64 return g_process_proxy_registry.Pointer(); | 70 return g_process_proxy_registry.Pointer(); |
| 65 } | 71 } |
| 66 | 72 |
| 67 bool ProcessProxyRegistry::OpenProcess( | 73 bool ProcessProxyRegistry::OpenProcess( |
| 68 const std::string& command, | 74 const std::string& command, |
| 69 pid_t* pid, | 75 pid_t* pid, |
| 70 const ProcessOutputCallbackWithPid& callback) { | 76 const ProcessOutputCallbackWithPid& callback) { |
| 71 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
| 72 | 78 |
| 73 // TODO(tbarzic): Instead of creating a new thread for each new process proxy, | 79 if (!EnsureWatcherThreadStarted()) |
| 74 // use one thread for all processes. | |
| 75 // We will need new thread for proxy's outpu watcher. | |
| 76 scoped_ptr<base::Thread> watcher_thread(new base::Thread(kWatcherThreadName)); | |
| 77 if (!watcher_thread->Start()) { | |
| 78 return false; | 80 return false; |
| 79 } | |
| 80 | 81 |
| 81 // Create and open new proxy. | 82 // Create and open new proxy. |
| 82 scoped_refptr<ProcessProxy> proxy(new ProcessProxy()); | 83 scoped_refptr<ProcessProxy> proxy(new ProcessProxy()); |
| 83 if (!proxy->Open(command, pid)) | 84 if (!proxy->Open(command, pid)) |
| 84 return false; | 85 return false; |
| 85 | 86 |
| 86 // Kick off watcher. | 87 // Kick off watcher. |
| 87 // We can use Unretained because proxy will stop calling callback after it is | 88 // We can use Unretained because proxy will stop calling callback after it is |
| 88 // closed, which is done befire this object goes away. | 89 // closed, which is done before this object goes away. |
| 89 if (!proxy->StartWatchingOnThread(watcher_thread.get(), | 90 if (!proxy->StartWatchingOutput( |
| 90 base::Bind(&ProcessProxyRegistry::OnProcessOutput, | 91 watcher_thread_->task_runner(), |
| 91 base::Unretained(this), *pid))) { | 92 base::Bind(&ProcessProxyRegistry::OnProcessOutput, |
| 93 base::Unretained(this), *pid))) { |
| 92 proxy->Close(); | 94 proxy->Close(); |
| 93 watcher_thread->Stop(); | |
| 94 return false; | 95 return false; |
| 95 } | 96 } |
| 96 | 97 |
| 97 DCHECK(proxy_map_.find(*pid) == proxy_map_.end()); | 98 DCHECK(proxy_map_.find(*pid) == proxy_map_.end()); |
| 98 | 99 |
| 99 // Save info for newly created proxy. We cannot do this before ProcessProxy is | 100 // Save info for newly created proxy. We cannot do this before ProcessProxy is |
| 100 // created because we don't know |pid| then. | 101 // created because we don't know |pid| then. |
| 101 ProcessProxyInfo& info = proxy_map_[*pid]; | 102 ProcessProxyInfo& info = proxy_map_[*pid]; |
| 102 info.proxy.swap(proxy); | 103 info.proxy.swap(proxy); |
| 103 info.watcher_thread.reset(watcher_thread.release()); | |
| 104 info.process_id = *pid; | 104 info.process_id = *pid; |
| 105 info.callback = callback; | 105 info.callback = callback; |
| 106 return true; | 106 return true; |
| 107 } | 107 } |
| 108 | 108 |
| 109 bool ProcessProxyRegistry::SendInput(pid_t pid, const std::string& data) { | 109 bool ProcessProxyRegistry::SendInput(pid_t pid, const std::string& data) { |
| 110 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
| 111 | 111 |
| 112 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 112 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 113 if (it == proxy_map_.end()) | 113 if (it == proxy_map_.end()) |
| 114 return false; | 114 return false; |
| 115 return it->second.proxy->Write(data); | 115 return it->second.proxy->Write(data); |
| 116 } | 116 } |
| 117 | 117 |
| 118 bool ProcessProxyRegistry::CloseProcess(pid_t pid) { | 118 bool ProcessProxyRegistry::CloseProcess(pid_t pid) { |
| 119 DCHECK(CalledOnValidThread()); | 119 DCHECK(CalledOnValidThread()); |
| 120 | 120 |
| 121 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 121 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 122 if (it == proxy_map_.end()) | 122 if (it == proxy_map_.end()) |
| 123 return false; | 123 return false; |
| 124 | 124 |
| 125 it->second.proxy->Close(); | 125 it->second.proxy->Close(); |
| 126 it->second.watcher_thread->Stop(); | |
| 127 proxy_map_.erase(it); | 126 proxy_map_.erase(it); |
| 128 return true; | 127 return true; |
| 129 } | 128 } |
| 130 | 129 |
| 131 bool ProcessProxyRegistry::OnTerminalResize(pid_t pid, int width, int height) { | 130 bool ProcessProxyRegistry::OnTerminalResize(pid_t pid, int width, int height) { |
| 132 DCHECK(CalledOnValidThread()); | 131 DCHECK(CalledOnValidThread()); |
| 133 | 132 |
| 134 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 133 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 135 if (it == proxy_map_.end()) | 134 if (it == proxy_map_.end()) |
| 136 return false; | 135 return false; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 149 if (it == proxy_map_.end()) | 148 if (it == proxy_map_.end()) |
| 150 return; | 149 return; |
| 151 it->second.callback.Run(pid, std::string(type_str), data); | 150 it->second.callback.Run(pid, std::string(type_str), data); |
| 152 | 151 |
| 153 // Contact with the slave end of the terminal has been lost. We have to close | 152 // Contact with the slave end of the terminal has been lost. We have to close |
| 154 // the process. | 153 // the process. |
| 155 if (type == PROCESS_OUTPUT_TYPE_EXIT) | 154 if (type == PROCESS_OUTPUT_TYPE_EXIT) |
| 156 CloseProcess(pid); | 155 CloseProcess(pid); |
| 157 } | 156 } |
| 158 | 157 |
| 158 bool ProcessProxyRegistry::EnsureWatcherThreadStarted() { |
| 159 if (watcher_thread_.get()) |
| 160 return true; |
| 161 |
| 162 // TODO(tbarzic): Change process output watcher to watch for fd readability on |
| 163 // FILE thread, and move output reading to worker thread instead of |
| 164 // spinning a new thread. |
| 165 watcher_thread_.reset(new base::Thread(kWatcherThreadName)); |
| 166 return watcher_thread_->StartWithOptions( |
| 167 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 168 } |
| 169 |
| 159 } // namespace chromeos | 170 } // namespace chromeos |
| OLD | NEW |