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" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 void ArcProcessService::Reset() { | 75 void ArcProcessService::Reset() { |
76 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(thread_checker_.CalledOnValidThread()); |
77 nspid_to_pid_.clear(); | 77 nspid_to_pid_.clear(); |
78 } | 78 } |
79 | 79 |
80 bool ArcProcessService::RequestProcessList( | 80 bool ArcProcessService::RequestProcessList( |
81 RequestProcessListCallback callback) { | 81 RequestProcessListCallback callback) { |
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
83 | 83 |
84 arc::ProcessInstance* process_instance = | 84 arc::mojom::ProcessInstance* process_instance = |
85 arc_bridge_service()->process_instance(); | 85 arc_bridge_service()->process_instance(); |
86 if (!process_instance) { | 86 if (!process_instance) { |
87 return false; | 87 return false; |
88 } | 88 } |
89 process_instance->RequestProcessList( | 89 process_instance->RequestProcessList( |
90 base::Bind(&ArcProcessService::OnReceiveProcessList, | 90 base::Bind(&ArcProcessService::OnReceiveProcessList, |
91 weak_ptr_factory_.GetWeakPtr(), | 91 weak_ptr_factory_.GetWeakPtr(), |
92 callback)); | 92 callback)); |
93 return true; | 93 return true; |
94 } | 94 } |
95 | 95 |
96 void ArcProcessService::OnReceiveProcessList( | 96 void ArcProcessService::OnReceiveProcessList( |
97 const RequestProcessListCallback& callback, | 97 const RequestProcessListCallback& callback, |
98 mojo::Array<arc::RunningAppProcessInfoPtr> mojo_processes) { | 98 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { |
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
100 | 100 |
101 auto raw_processes = new vector<RunningAppProcessInfoPtr>(); | 101 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); |
102 mojo_processes.Swap(raw_processes); | 102 mojo_processes.Swap(raw_processes); |
103 | 103 |
104 auto ret_processes = new vector<ArcProcess>(); | 104 auto ret_processes = new vector<ArcProcess>(); |
105 // Post to its dedicated worker thread to avoid race condition. | 105 // 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. | 106 // Since no two tasks with the same token should be run at the same. |
107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to | 107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to |
108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). | 108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). |
109 // So in theory using Unretained(this) should be fine since the life cycle | 109 // So in theory using Unretained(this) should be fine since the life cycle |
110 // of |this| is the same as the main browser. | 110 // 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 | 111 // To be safe I still use weak pointers, but weak_ptrs can only bind to |
(...skipping 14 matching lines...) Expand all Loading... |
126 } | 126 } |
127 | 127 |
128 void ArcProcessService::CallbackRelay( | 128 void ArcProcessService::CallbackRelay( |
129 const RequestProcessListCallback& callback, | 129 const RequestProcessListCallback& callback, |
130 const vector<ArcProcess>* ret_processes) { | 130 const vector<ArcProcess>* ret_processes) { |
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
132 callback.Run(*ret_processes); | 132 callback.Run(*ret_processes); |
133 } | 133 } |
134 | 134 |
135 void ArcProcessService::UpdateAndReturnProcessList( | 135 void ArcProcessService::UpdateAndReturnProcessList( |
136 const vector<arc::RunningAppProcessInfoPtr>* raw_processes, | 136 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
137 vector<ArcProcess>* ret_processes) { | 137 vector<ArcProcess>* ret_processes) { |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
139 | 139 |
140 // Cleanup dead pids in the cache |nspid_to_pid_|. | 140 // Cleanup dead pids in the cache |nspid_to_pid_|. |
141 set<ProcessId> nspid_to_remove; | 141 set<ProcessId> nspid_to_remove; |
142 for (const auto& entry : nspid_to_pid_) { | 142 for (const auto& entry : nspid_to_pid_) { |
143 nspid_to_remove.insert(entry.first); | 143 nspid_to_remove.insert(entry.first); |
144 } | 144 } |
145 bool unmapped_nspid = false; | 145 bool unmapped_nspid = false; |
146 for (const auto& entry : *raw_processes) { | 146 for (const auto& entry : *raw_processes) { |
147 // erase() returns 0 if coudln't find the key. It means a new process. | 147 // erase() returns 0 if coudln't find the key. It means a new process. |
148 if (nspid_to_remove.erase(entry->pid) == 0) { | 148 if (nspid_to_remove.erase(entry->pid) == 0) { |
149 nspid_to_pid_[entry->pid] = kNullProcessId; | 149 nspid_to_pid_[entry->pid] = kNullProcessId; |
150 unmapped_nspid = true; | 150 unmapped_nspid = true; |
151 } | 151 } |
152 } | 152 } |
153 for (const auto& entry : nspid_to_remove) { | 153 for (const auto& entry : nspid_to_remove) { |
154 nspid_to_pid_.erase(entry); | 154 nspid_to_pid_.erase(entry); |
155 } | 155 } |
156 | 156 |
157 // The operation is costly so avoid calling it when possible. | 157 // The operation is costly so avoid calling it when possible. |
158 if (unmapped_nspid) { | 158 if (unmapped_nspid) { |
159 UpdateNspidToPidMap(); | 159 UpdateNspidToPidMap(); |
160 } | 160 } |
161 | 161 |
162 PopulateProcessList(raw_processes, ret_processes); | 162 PopulateProcessList(raw_processes, ret_processes); |
163 } | 163 } |
164 | 164 |
165 void ArcProcessService::PopulateProcessList( | 165 void ArcProcessService::PopulateProcessList( |
166 const vector<arc::RunningAppProcessInfoPtr>* raw_processes, | 166 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
167 vector<ArcProcess>* ret_processes) { | 167 vector<ArcProcess>* ret_processes) { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 168 DCHECK(thread_checker_.CalledOnValidThread()); |
169 for (const auto& entry : *raw_processes) { | 169 for (const auto& entry : *raw_processes) { |
170 const auto it = nspid_to_pid_.find(entry->pid); | 170 const auto it = nspid_to_pid_.find(entry->pid); |
171 // In case the process already dies so couldn't find corresponding pid. | 171 // In case the process already dies so couldn't find corresponding pid. |
172 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { | 172 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { |
173 ArcProcess arc_process = { | 173 ArcProcess arc_process = { |
174 entry->pid, it->second, entry->process_name, entry->process_state}; | 174 entry->pid, it->second, entry->process_name, entry->process_state}; |
175 ret_processes->push_back(arc_process); | 175 ret_processes->push_back(arc_process); |
176 } | 176 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) | 235 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) |
236 nspid_to_pid_[nspid] = pid; | 236 nspid_to_pid_[nspid] = pid; |
237 | 237 |
238 for (ProcessId child_pid : process_tree[pid]) | 238 for (ProcessId child_pid : process_tree[pid]) |
239 queue.push(child_pid); | 239 queue.push(child_pid); |
240 } | 240 } |
241 } | 241 } |
242 } | 242 } |
243 | 243 |
244 } // namespace arc | 244 } // namespace arc |
OLD | NEW |