| 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 "chrome/browser/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 { |
| 10 |
| 9 namespace { | 11 namespace { |
| 10 | 12 |
| 11 const char kWatcherThreadName[] = "ProcessWatcherThread"; | 13 const char kWatcherThreadName[] = "ProcessWatcherThread"; |
| 12 | 14 |
| 13 const char kStdoutOutputType[] = "stdout"; | 15 const char kStdoutOutputType[] = "stdout"; |
| 14 const char kStderrOutputType[] = "stderr"; | 16 const char kStderrOutputType[] = "stderr"; |
| 15 const char kExitOutputType[] = "exit"; | 17 const char kExitOutputType[] = "exit"; |
| 16 | 18 |
| 17 const char* ProcessOutputTypeToString(ProcessOutputType type) { | 19 const char* ProcessOutputTypeToString(ProcessOutputType type) { |
| 18 switch (type) { | 20 switch (type) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 41 DCHECK(!other.proxy.get() && !other.watcher_thread.get()); | 43 DCHECK(!other.proxy.get() && !other.watcher_thread.get()); |
| 42 } | 44 } |
| 43 | 45 |
| 44 ProcessProxyRegistry::ProcessProxyInfo::~ProcessProxyInfo() { | 46 ProcessProxyRegistry::ProcessProxyInfo::~ProcessProxyInfo() { |
| 45 } | 47 } |
| 46 | 48 |
| 47 ProcessProxyRegistry::ProcessProxyRegistry() { | 49 ProcessProxyRegistry::ProcessProxyRegistry() { |
| 48 } | 50 } |
| 49 | 51 |
| 50 ProcessProxyRegistry::~ProcessProxyRegistry() { | 52 ProcessProxyRegistry::~ProcessProxyRegistry() { |
| 53 // TODO(tbarzic): Fix issue with ProcessProxyRegistry being destroyed |
| 54 // on a different thread (it's a LazyInstance). |
| 55 DetachFromThread(); |
| 56 |
| 51 // Close all proxies we own. | 57 // Close all proxies we own. |
| 52 while (!proxy_map_.empty()) | 58 while (!proxy_map_.empty()) |
| 53 CloseProcess(proxy_map_.begin()->first); | 59 CloseProcess(proxy_map_.begin()->first); |
| 54 } | 60 } |
| 55 | 61 |
| 56 // static | 62 // static |
| 57 ProcessProxyRegistry* ProcessProxyRegistry::Get() { | 63 ProcessProxyRegistry* ProcessProxyRegistry::Get() { |
| 58 return g_process_proxy_registry.Pointer(); | 64 return g_process_proxy_registry.Pointer(); |
| 59 } | 65 } |
| 60 | 66 |
| 61 bool ProcessProxyRegistry::OpenProcess(const std::string& command, pid_t* pid, | 67 bool ProcessProxyRegistry::OpenProcess( |
| 68 const std::string& command, |
| 69 pid_t* pid, |
| 62 const ProcessOutputCallbackWithPid& callback) { | 70 const ProcessOutputCallbackWithPid& callback) { |
| 71 DCHECK(CalledOnValidThread()); |
| 72 |
| 63 // TODO(tbarzic): Instead of creating a new thread for each new process proxy, | 73 // TODO(tbarzic): Instead of creating a new thread for each new process proxy, |
| 64 // use one thread for all processes. | 74 // use one thread for all processes. |
| 65 // We will need new thread for proxy's outpu watcher. | 75 // We will need new thread for proxy's outpu watcher. |
| 66 scoped_ptr<base::Thread> watcher_thread(new base::Thread(kWatcherThreadName)); | 76 scoped_ptr<base::Thread> watcher_thread(new base::Thread(kWatcherThreadName)); |
| 67 if (!watcher_thread->Start()) { | 77 if (!watcher_thread->Start()) { |
| 68 return false; | 78 return false; |
| 69 } | 79 } |
| 70 | 80 |
| 71 // Create and open new proxy. | 81 // Create and open new proxy. |
| 72 scoped_refptr<ProcessProxy> proxy(new ProcessProxy()); | 82 scoped_refptr<ProcessProxy> proxy(new ProcessProxy()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 90 // created because we don't know |pid| then. | 100 // created because we don't know |pid| then. |
| 91 ProcessProxyInfo& info = proxy_map_[*pid]; | 101 ProcessProxyInfo& info = proxy_map_[*pid]; |
| 92 info.proxy.swap(proxy); | 102 info.proxy.swap(proxy); |
| 93 info.watcher_thread.reset(watcher_thread.release()); | 103 info.watcher_thread.reset(watcher_thread.release()); |
| 94 info.process_id = *pid; | 104 info.process_id = *pid; |
| 95 info.callback = callback; | 105 info.callback = callback; |
| 96 return true; | 106 return true; |
| 97 } | 107 } |
| 98 | 108 |
| 99 bool ProcessProxyRegistry::SendInput(pid_t pid, const std::string& data) { | 109 bool ProcessProxyRegistry::SendInput(pid_t pid, const std::string& data) { |
| 110 DCHECK(CalledOnValidThread()); |
| 111 |
| 100 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 112 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 101 if (it == proxy_map_.end()) | 113 if (it == proxy_map_.end()) |
| 102 return false; | 114 return false; |
| 103 return it->second.proxy->Write(data); | 115 return it->second.proxy->Write(data); |
| 104 } | 116 } |
| 105 | 117 |
| 106 bool ProcessProxyRegistry::CloseProcess(pid_t pid) { | 118 bool ProcessProxyRegistry::CloseProcess(pid_t pid) { |
| 119 DCHECK(CalledOnValidThread()); |
| 120 |
| 107 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 121 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 108 if (it == proxy_map_.end()) | 122 if (it == proxy_map_.end()) |
| 109 return false; | 123 return false; |
| 110 | 124 |
| 111 it->second.proxy->Close(); | 125 it->second.proxy->Close(); |
| 112 it->second.watcher_thread->Stop(); | 126 it->second.watcher_thread->Stop(); |
| 113 proxy_map_.erase(it); | 127 proxy_map_.erase(it); |
| 114 return true; | 128 return true; |
| 115 } | 129 } |
| 116 | 130 |
| 117 bool ProcessProxyRegistry::OnTerminalResize(pid_t pid, int width, int height) { | 131 bool ProcessProxyRegistry::OnTerminalResize(pid_t pid, int width, int height) { |
| 132 DCHECK(CalledOnValidThread()); |
| 133 |
| 118 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 134 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 119 if (it == proxy_map_.end()) | 135 if (it == proxy_map_.end()) |
| 120 return false; | 136 return false; |
| 121 | 137 |
| 122 return it->second.proxy->OnTerminalResize(width, height); | 138 return it->second.proxy->OnTerminalResize(width, height); |
| 123 } | 139 } |
| 124 | 140 |
| 125 void ProcessProxyRegistry::OnProcessOutput(pid_t pid, | 141 void ProcessProxyRegistry::OnProcessOutput(pid_t pid, |
| 126 ProcessOutputType type, const std::string& data) { | 142 ProcessOutputType type, const std::string& data) { |
| 143 DCHECK(CalledOnValidThread()); |
| 144 |
| 127 const char* type_str = ProcessOutputTypeToString(type); | 145 const char* type_str = ProcessOutputTypeToString(type); |
| 128 DCHECK(type_str); | 146 DCHECK(type_str); |
| 129 | 147 |
| 130 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); | 148 std::map<pid_t, ProcessProxyInfo>::iterator it = proxy_map_.find(pid); |
| 131 if (it == proxy_map_.end()) | 149 if (it == proxy_map_.end()) |
| 132 return; | 150 return; |
| 133 it->second.callback.Run(pid, std::string(type_str), data); | 151 it->second.callback.Run(pid, std::string(type_str), data); |
| 134 | 152 |
| 135 // Contact with the slave end of the terminal has been lost. We have to close | 153 // Contact with the slave end of the terminal has been lost. We have to close |
| 136 // the process. | 154 // the process. |
| 137 if (type == PROCESS_OUTPUT_TYPE_EXIT) | 155 if (type == PROCESS_OUTPUT_TYPE_EXIT) |
| 138 CloseProcess(pid); | 156 CloseProcess(pid); |
| 139 } | 157 } |
| 140 | 158 |
| 159 } // namespace chromeos |
| OLD | NEW |