| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // The main point of this class is to cache ARC proc nspid<->pid mapping | 5 // The main point of this class is to cache ARC proc nspid<->pid mapping |
| 6 // globally. Since the calculation is costly, a dedicated worker thread is | 6 // globally. Since the calculation is costly, a dedicated worker thread is |
| 7 // used. All read/write of its internal data structure (i.e., the mapping) | 7 // used. All read/write of its internal data structure (i.e., the mapping) |
| 8 // should be on this thread. | 8 // should be on this thread. |
| 9 | 9 |
| 10 #include "chrome/browser/chromeos/arc/arc_process_service.h" | 10 #include "chrome/browser/chromeos/arc/arc_process_service.h" |
| 11 | 11 |
| 12 #include <queue> | 12 #include <queue> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/callback_forward.h" |
| 16 #include "base/process/process.h" | 17 #include "base/process/process.h" |
| 17 #include "base/process/process_iterator.h" | 18 #include "base/process/process_iterator.h" |
| 18 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 19 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 | 22 |
| 22 namespace arc { | 23 namespace arc { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const char kSequenceToken[] = "arc_process_service"; | 27 constexpr char kSequenceToken[] = "arc_process_service"; |
| 27 | 28 |
| 28 // Weak pointer. This class is owned by ArcServiceManager. | 29 // Weak pointer. This class is owned by ArcServiceManager. |
| 29 ArcProcessService* g_arc_process_service = nullptr; | 30 ArcProcessService* g_arc_process_service = nullptr; |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 using base::kNullProcessId; | 34 using base::kNullProcessId; |
| 34 using base::Process; | 35 using base::Process; |
| 35 using base::ProcessId; | 36 using base::ProcessId; |
| 36 using base::SequencedWorkerPool; | 37 using base::SequencedWorkerPool; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 FROM_HERE, | 71 FROM_HERE, |
| 71 base::Bind(&ArcProcessService::Reset, | 72 base::Bind(&ArcProcessService::Reset, |
| 72 weak_ptr_factory_.GetWeakPtr())); | 73 weak_ptr_factory_.GetWeakPtr())); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void ArcProcessService::Reset() { | 76 void ArcProcessService::Reset() { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 nspid_to_pid_.clear(); | 78 nspid_to_pid_.clear(); |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool ArcProcessService::RequestProcessList( | 81 void ArcProcessService::PostTaskToOwnThreadAndReply( |
| 82 const base::Closure& task, |
| 83 const base::Closure& reply) { |
| 84 auto runner = worker_pool_->GetSequencedTaskRunner( |
| 85 worker_pool_->GetNamedSequenceToken(kSequenceToken)); |
| 86 runner->PostTaskAndReply(FROM_HERE, task, reply); |
| 87 } |
| 88 |
| 89 void ArcProcessService::RequestSystemProcessList( |
| 81 RequestProcessListCallback callback) { | 90 RequestProcessListCallback callback) { |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 91 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 | 92 |
| 93 auto ret_processes = new vector<ArcProcess>(); |
| 94 PostTaskToOwnThreadAndReply( |
| 95 base::Bind(&ArcProcessService::GetArcSystemProcessList, |
| 96 weak_ptr_factory_.GetWeakPtr(), |
| 97 base::Unretained(ret_processes)), |
| 98 base::Bind(&ArcProcessService::CallbackRelay, |
| 99 weak_ptr_factory_.GetWeakPtr(), callback, |
| 100 base::Owned(ret_processes))); |
| 101 } |
| 102 |
| 103 void ArcProcessService::GetArcSystemProcessList( |
| 104 vector<ArcProcess>* ret_processes) { |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 |
| 107 const base::ProcessIterator::ProcessEntries& entry_list = |
| 108 base::ProcessIterator(nullptr).Snapshot(); |
| 109 ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
| 110 |
| 111 if (arc_init_pid == kNullProcessId) { |
| 112 return; |
| 113 } |
| 114 |
| 115 // Enumerate the child processes of ARC init for gathering ARC System |
| 116 // Procceses |
| 117 for (const base::ProcessEntry& entry : entry_list) { |
| 118 // TODO(hctsai): For now, we only gather direct child process of init, need |
| 119 // to get the processes below. |
| 120 if (entry.parent_pid() == arc_init_pid) { |
| 121 ProcessId child_pid = entry.pid(); |
| 122 ProcessId child_nspid = base::Process(child_pid).GetPidInNamespace(); |
| 123 std::string process_name = |
| 124 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; |
| 125 if (child_nspid != kNullProcessId) { |
| 126 ArcProcess arc_process(child_nspid, child_pid, process_name, |
| 127 mojom::ProcessState::PERSISTENT); |
| 128 ret_processes->push_back(std::move(arc_process)); |
| 129 } |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 bool ArcProcessService::RequestAppProcessList( |
| 135 RequestProcessListCallback callback) { |
| 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 137 |
| 84 arc::mojom::ProcessInstance* process_instance = | 138 arc::mojom::ProcessInstance* process_instance = |
| 85 arc_bridge_service()->process_instance(); | 139 arc_bridge_service()->process_instance(); |
| 86 if (!process_instance) { | 140 if (!process_instance) { |
| 87 return false; | 141 return false; |
| 88 } | 142 } |
| 89 process_instance->RequestProcessList( | 143 process_instance->RequestProcessList( |
| 90 base::Bind(&ArcProcessService::OnReceiveProcessList, | 144 base::Bind(&ArcProcessService::OnReceiveProcessList, |
| 91 weak_ptr_factory_.GetWeakPtr(), | 145 weak_ptr_factory_.GetWeakPtr(), |
| 92 callback)); | 146 callback)); |
| 93 return true; | 147 return true; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 104 auto ret_processes = new vector<ArcProcess>(); | 158 auto ret_processes = new vector<ArcProcess>(); |
| 105 // Post to its dedicated worker thread to avoid race condition. | 159 // Post to its dedicated worker thread to avoid race condition. |
| 106 // Since no two tasks with the same token should be run at the same. | 160 // Since no two tasks with the same token should be run at the same. |
| 107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to | 161 // Note: GetSequencedTaskRunner's shutdown behavior defaults to |
| 108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). | 162 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). |
| 109 // So in theory using Unretained(this) should be fine since the life cycle | 163 // So in theory using Unretained(this) should be fine since the life cycle |
| 110 // of |this| is the same as the main browser. | 164 // of |this| is the same as the main browser. |
| 111 // To be safe I still use weak pointers, but weak_ptrs can only bind to | 165 // To be safe I still use weak pointers, but weak_ptrs can only bind to |
| 112 // methods without return values. That's why I can't use | 166 // methods without return values. That's why I can't use |
| 113 // PostTaskAndReplyWithResult but handle the return object by myself. | 167 // PostTaskAndReplyWithResult but handle the return object by myself. |
| 114 auto runner = worker_pool_->GetSequencedTaskRunner( | 168 PostTaskToOwnThreadAndReply( |
| 115 worker_pool_->GetNamedSequenceToken(kSequenceToken)); | |
| 116 runner->PostTaskAndReply( | |
| 117 FROM_HERE, | |
| 118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, | 169 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, |
| 119 weak_ptr_factory_.GetWeakPtr(), | 170 weak_ptr_factory_.GetWeakPtr(), base::Owned(raw_processes), |
| 120 base::Owned(raw_processes), | |
| 121 base::Unretained(ret_processes)), | 171 base::Unretained(ret_processes)), |
| 122 base::Bind(&ArcProcessService::CallbackRelay, | 172 base::Bind(&ArcProcessService::CallbackRelay, |
| 123 weak_ptr_factory_.GetWeakPtr(), | 173 weak_ptr_factory_.GetWeakPtr(), callback, |
| 124 callback, | |
| 125 base::Owned(ret_processes))); | 174 base::Owned(ret_processes))); |
| 126 } | 175 } |
| 127 | 176 |
| 128 void ArcProcessService::CallbackRelay( | 177 void ArcProcessService::CallbackRelay( |
| 129 const RequestProcessListCallback& callback, | 178 const RequestProcessListCallback& callback, |
| 130 const vector<ArcProcess>* ret_processes) { | 179 const vector<ArcProcess>* ret_processes) { |
| 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 132 callback.Run(*ret_processes); | 181 callback.Run(*ret_processes); |
| 133 } | 182 } |
| 134 | 183 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 // processes may have the same nspid. We need to get the proper subset of | 250 // processes may have the same nspid. We need to get the proper subset of |
| 202 // processes to create correct nspid -> pid map. | 251 // processes to create correct nspid -> pid map. |
| 203 | 252 |
| 204 // Construct the process tree. | 253 // Construct the process tree. |
| 205 // NB: This can contain a loop in case of race conditions. | 254 // NB: This can contain a loop in case of race conditions. |
| 206 map<ProcessId, vector<ProcessId> > process_tree; | 255 map<ProcessId, vector<ProcessId> > process_tree; |
| 207 for (const base::ProcessEntry& entry : entry_list) | 256 for (const base::ProcessEntry& entry : entry_list) |
| 208 process_tree[entry.parent_pid()].push_back(entry.pid()); | 257 process_tree[entry.parent_pid()].push_back(entry.pid()); |
| 209 | 258 |
| 210 // Find the ARC init process. | 259 // Find the ARC init process. |
| 211 ProcessId arc_init_pid = kNullProcessId; | 260 ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
| 212 for (const base::ProcessEntry& entry : entry_list) { | |
| 213 // TODO(nya): Add more constraints to avoid mismatches. | |
| 214 std::string process_name = | |
| 215 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; | |
| 216 if (process_name == "/init") { | |
| 217 arc_init_pid = entry.pid(); | |
| 218 break; | |
| 219 } | |
| 220 } | |
| 221 | 261 |
| 222 // Enumerate all processes under ARC init and create nspid -> pid map. | 262 // Enumerate all processes under ARC init and create nspid -> pid map. |
| 223 if (arc_init_pid != kNullProcessId) { | 263 if (arc_init_pid != kNullProcessId) { |
| 224 std::queue<ProcessId> queue; | 264 std::queue<ProcessId> queue; |
| 225 std::set<ProcessId> visited; | 265 std::set<ProcessId> visited; |
| 226 queue.push(arc_init_pid); | 266 queue.push(arc_init_pid); |
| 227 while (!queue.empty()) { | 267 while (!queue.empty()) { |
| 228 ProcessId pid = queue.front(); | 268 ProcessId pid = queue.front(); |
| 229 queue.pop(); | 269 queue.pop(); |
| 230 // Do not visit the same process twice. Otherwise we may enter an infinite | 270 // Do not visit the same process twice. Otherwise we may enter an infinite |
| (...skipping 10 matching lines...) Expand all Loading... |
| 241 if (nspid != kNullProcessId && | 281 if (nspid != kNullProcessId && |
| 242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) | 282 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) |
| 243 nspid_to_pid_[nspid] = pid; | 283 nspid_to_pid_[nspid] = pid; |
| 244 | 284 |
| 245 for (ProcessId child_pid : process_tree[pid]) | 285 for (ProcessId child_pid : process_tree[pid]) |
| 246 queue.push(child_pid); | 286 queue.push(child_pid); |
| 247 } | 287 } |
| 248 } | 288 } |
| 249 } | 289 } |
| 250 | 290 |
| 291 // static |
| 292 ProcessId ArcProcessService::GetArcInitProcessId( |
| 293 const base::ProcessIterator::ProcessEntries& entry_list) { |
| 294 // Find the ARC init process. |
| 295 for (const base::ProcessEntry& entry : entry_list) { |
| 296 // TODO(nya): Add more constraints to avoid mismatches. |
| 297 std::string process_name = |
| 298 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; |
| 299 if (process_name == "/init") { |
| 300 return entry.pid(); |
| 301 } |
| 302 } |
| 303 return kNullProcessId; |
| 304 } |
| 305 |
| 251 } // namespace arc | 306 } // namespace arc |
| OLD | NEW |