Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_process_service.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_process_service.cc b/chrome/browser/chromeos/arc/arc_process_service.cc |
| index 550ea0ae0c2f5a62cf0815969f2168c52a3a353c..b8072afcf13a8ecb9650867c431424974768ce70 100644 |
| --- a/chrome/browser/chromeos/arc/arc_process_service.cc |
| +++ b/chrome/browser/chromeos/arc/arc_process_service.cc |
| @@ -13,6 +13,7 @@ |
| #include <set> |
| #include <string> |
| +#include "base/callback_forward.h" |
| #include "base/process/process.h" |
| #include "base/process/process_iterator.h" |
| #include "base/task_runner_util.h" |
| @@ -23,7 +24,7 @@ namespace arc { |
| namespace { |
| -const char kSequenceToken[] = "arc_process_service"; |
| +constexpr char kSequenceToken[] = "arc_process_service"; |
| // Weak pointer. This class is owned by ArcServiceManager. |
| ArcProcessService* g_arc_process_service = nullptr; |
| @@ -77,7 +78,58 @@ void ArcProcessService::Reset() { |
| nspid_to_pid_.clear(); |
| } |
| -bool ArcProcessService::RequestProcessList( |
| +void ArcProcessService::PostTaskToOwnThreadAndReply( |
| + const base::Closure& task, |
| + const base::Closure& reply) { |
| + auto runner = worker_pool_->GetSequencedTaskRunner( |
| + worker_pool_->GetNamedSequenceToken(kSequenceToken)); |
| + runner->PostTaskAndReply(FROM_HERE, task, reply); |
| +} |
| + |
| +void ArcProcessService::RequestSystemProcessList( |
| + RequestProcessListCallback callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + auto ret_processes = new vector<ArcProcess>(); |
| + PostTaskToOwnThreadAndReply( |
| + base::Bind(&ArcProcessService::GetArcSystemProcessList, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Unretained(ret_processes)), |
| + base::Bind(&ArcProcessService::CallbackRelay, |
| + weak_ptr_factory_.GetWeakPtr(), callback, |
| + base::Owned(ret_processes))); |
| +} |
| + |
| +void ArcProcessService::GetArcSystemProcessList( |
| + vector<ArcProcess>* ret_processes) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + const base::ProcessIterator::ProcessEntries& entry_list = |
| + base::ProcessIterator(nullptr).Snapshot(); |
| + ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
| + |
| + if (arc_init_pid == kNullProcessId) { |
| + return; |
| + } |
| + |
| + // Enumerate the child processes of ARC init for gathering ARC System |
| + // Procceses |
|
cylee1
2016/06/30 03:45:52
nit: spelling : Processes..
Hsu-Cheng
2016/06/30 09:56:24
Done.
|
| + for (const base::ProcessEntry& entry : entry_list) { |
| + if (entry.parent_pid() == arc_init_pid) { |
| + ProcessId child_pid = entry.pid(); |
| + ProcessId child_nspid = base::Process(child_pid).GetPidInNamespace(); |
| + std::string process_name = |
|
cylee1
2016/06/30 03:45:52
nit: can be const
Hsu-Cheng
2016/06/30 09:56:24
Done.
|
| + !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; |
| + if (child_nspid != kNullProcessId) { |
| + ArcProcess arc_process(child_nspid, child_pid, process_name, |
| + mojom::ProcessState::PERSISTENT); |
| + ret_processes->push_back(std::move(arc_process)); |
|
cylee1
2016/06/30 03:45:52
Just learnt that you can probably just use the C++
Hsu-Cheng
2016/06/30 09:56:24
magic indeed ... Done
|
| + } |
| + } |
| + } |
| +} |
| + |
| +bool ArcProcessService::RequestAppProcessList( |
| RequestProcessListCallback callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| @@ -111,17 +163,12 @@ void ArcProcessService::OnReceiveProcessList( |
| // To be safe I still use weak pointers, but weak_ptrs can only bind to |
| // methods without return values. That's why I can't use |
| // PostTaskAndReplyWithResult but handle the return object by myself. |
| - auto runner = worker_pool_->GetSequencedTaskRunner( |
| - worker_pool_->GetNamedSequenceToken(kSequenceToken)); |
| - runner->PostTaskAndReply( |
| - FROM_HERE, |
| + PostTaskToOwnThreadAndReply( |
| base::Bind(&ArcProcessService::UpdateAndReturnProcessList, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - base::Owned(raw_processes), |
| + weak_ptr_factory_.GetWeakPtr(), base::Owned(raw_processes), |
| base::Unretained(ret_processes)), |
| base::Bind(&ArcProcessService::CallbackRelay, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - callback, |
| + weak_ptr_factory_.GetWeakPtr(), callback, |
| base::Owned(ret_processes))); |
| } |
| @@ -208,16 +255,7 @@ void ArcProcessService::UpdateNspidToPidMap() { |
| process_tree[entry.parent_pid()].push_back(entry.pid()); |
| // Find the ARC init process. |
| - ProcessId arc_init_pid = kNullProcessId; |
| - for (const base::ProcessEntry& entry : entry_list) { |
| - // TODO(nya): Add more constraints to avoid mismatches. |
| - std::string process_name = |
| - !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; |
| - if (process_name == "/init") { |
| - arc_init_pid = entry.pid(); |
| - break; |
| - } |
| - } |
| + ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
| // Enumerate all processes under ARC init and create nspid -> pid map. |
| if (arc_init_pid != kNullProcessId) { |
| @@ -248,4 +286,19 @@ void ArcProcessService::UpdateNspidToPidMap() { |
| } |
| } |
| +// static |
| +ProcessId ArcProcessService::GetArcInitProcessId( |
| + const base::ProcessIterator::ProcessEntries& entry_list) { |
| + // Find the ARC init process. |
| + for (const base::ProcessEntry& entry : entry_list) { |
| + // TODO(nya): Add more constraints to avoid mismatches. |
| + std::string process_name = |
| + !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; |
| + if (process_name == "/init") { |
| + return entry.pid(); |
| + } |
| + } |
| + return kNullProcessId; |
| +} |
| + |
| } // namespace arc |