| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/task_management/providers/arc/arc_process_task_provider
.h" | 5 #include "chrome/browser/task_management/providers/arc/arc_process_task_provider
.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 queue.push(child_pid); | 74 queue.push(child_pid); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 return nspid_to_pid; | 77 return nspid_to_pid; |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 namespace task_management { | 82 namespace task_management { |
| 83 | 83 |
| 84 ArcProcessTaskProvider::ArcProcessTaskProvider() | 84 ArcProcessTaskProvider::ArcProcessTaskProvider() : weak_ptr_factory_(this) {} |
| 85 : weak_ptr_factory_(this) { | |
| 86 } | |
| 87 | 85 |
| 88 ArcProcessTaskProvider::~ArcProcessTaskProvider() { | 86 ArcProcessTaskProvider::~ArcProcessTaskProvider() {} |
| 89 } | |
| 90 | 87 |
| 91 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, | 88 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, |
| 92 int child_id, | 89 int child_id, |
| 93 int route_id) { | 90 int route_id) { |
| 94 // ARC tasks are not associated with any URL request. | 91 // ARC tasks are not associated with any URL request. |
| 95 return nullptr; | 92 return nullptr; |
| 96 } | 93 } |
| 97 | 94 |
| 98 void ArcProcessTaskProvider::OnUpdateProcessList( | 95 void ArcProcessTaskProvider::OnUpdateProcessList( |
| 99 const std::vector<arc::RunningAppProcessInfo>& processes) { | 96 mojo::Array<arc::RunningAppProcessInfoPtr> processes) { |
| 100 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); | 97 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); |
| 101 | 98 |
| 102 // NB: |processes| can be already stale here because it is sent via IPC, and | 99 // NB: |processes| can be already stale here because it is sent via IPC, and |
| 103 // we can never avoid that. See also the comment at the declaration of | 100 // we can never avoid that. See also the comment at the declaration of |
| 104 // ArcProcessTaskProvider. | 101 // ArcProcessTaskProvider. |
| 105 | 102 |
| 106 std::vector<arc::RunningAppProcessInfo> added_processes; | 103 std::vector<arc::RunningAppProcessInfo> added_processes; |
| 107 std::set<base::ProcessId> removed_nspids; | 104 std::set<base::ProcessId> removed_nspids; |
| 108 for (const auto& entry : nspid_to_task_) | 105 for (const auto& entry : nspid_to_task_) |
| 109 removed_nspids.insert(entry.first); | 106 removed_nspids.insert(entry.first); |
| 110 for (const arc::RunningAppProcessInfo& process : processes) { | 107 for (size_t i = 0; i < processes.size(); ++i) { |
| 111 if (nspid_to_task_.count(process.pid)) | 108 const arc::RunningAppProcessInfoPtr& process = processes[i]; |
| 112 removed_nspids.erase(process.pid); | 109 if (nspid_to_task_.count(process->pid)) |
| 110 removed_nspids.erase(process->pid); |
| 113 else | 111 else |
| 114 added_processes.push_back(process); | 112 added_processes.push_back(*process); |
| 115 } | 113 } |
| 116 | 114 |
| 117 // Remove stale tasks. | 115 // Remove stale tasks. |
| 118 RemoveTasks(removed_nspids); | 116 RemoveTasks(removed_nspids); |
| 119 | 117 |
| 120 // Add new tasks. | 118 // Add new tasks. |
| 121 // Note: ComputeNspidToPidMap() is an expensive operation, so try to reduce | 119 // Note: ComputeNspidToPidMap() is an expensive operation, so try to reduce |
| 122 // the number of times it is called. | 120 // the number of times it is called. |
| 123 if (!added_processes.empty()) { | 121 if (!added_processes.empty()) { |
| 124 base::PostTaskAndReplyWithResult( | 122 base::PostTaskAndReplyWithResult( |
| 125 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), | 123 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
| 126 FROM_HERE, | 124 FROM_HERE, |
| 127 base::Bind(&ComputeNspidToPidMap), | 125 base::Bind(&ComputeNspidToPidMap), |
| 128 base::Bind(&ArcProcessTaskProvider::AddTasks, | 126 base::Bind(&ArcProcessTaskProvider::AddTasks, |
| 129 weak_ptr_factory_.GetWeakPtr(), | 127 weak_ptr_factory_.GetWeakPtr(), |
| 130 added_processes)); | 128 added_processes)); |
| 131 } | 129 } |
| 132 } | 130 } |
| 133 | 131 |
| 134 void ArcProcessTaskProvider::RequestProcessList() { | 132 void ArcProcessTaskProvider::RequestProcessList() { |
| 135 arc::ArcBridgeService::Get()->RequestProcessList(); | 133 arc::ProcessInstance* arc_process_instance = |
| 134 arc::ArcBridgeService::Get()->process_instance(); |
| 135 if (!arc_process_instance) { |
| 136 VLOG(2) << "ARC process instance is not ready."; |
| 137 return; |
| 138 } |
| 139 arc_process_instance->RequestProcessList( |
| 140 base::Bind(&ArcProcessTaskProvider::OnUpdateProcessList, |
| 141 weak_ptr_factory_.GetWeakPtr())); |
| 136 } | 142 } |
| 137 | 143 |
| 138 void ArcProcessTaskProvider::RemoveTasks( | 144 void ArcProcessTaskProvider::RemoveTasks( |
| 139 const std::set<base::ProcessId>& removed_nspids) { | 145 const std::set<base::ProcessId>& removed_nspids) { |
| 140 for (base::ProcessId nspid : removed_nspids) { | 146 for (base::ProcessId nspid : removed_nspids) { |
| 141 NotifyObserverTaskRemoved(nspid_to_task_[nspid].get()); | 147 NotifyObserverTaskRemoved(nspid_to_task_[nspid].get()); |
| 142 nspid_to_task_.erase(nspid); | 148 nspid_to_task_.erase(nspid); |
| 143 } | 149 } |
| 144 } | 150 } |
| 145 | 151 |
| 146 void ArcProcessTaskProvider::AddTasks( | 152 void ArcProcessTaskProvider::AddTasks( |
| 147 const std::vector<arc::RunningAppProcessInfo>& added_processes, | 153 const std::vector<arc::RunningAppProcessInfo>& added_processes, |
| 148 const std::map<base::ProcessId, base::ProcessId>& nspid_to_pid) { | 154 const std::map<base::ProcessId, base::ProcessId>& nspid_to_pid) { |
| 149 for (const arc::RunningAppProcessInfo& process : added_processes) { | 155 for (const arc::RunningAppProcessInfo& process : added_processes) { |
| 150 auto iter = nspid_to_pid.find(process.pid); | 156 auto iter = nspid_to_pid.find(process.pid); |
| 151 if (iter == nspid_to_pid.end()) { | 157 if (iter == nspid_to_pid.end()) { |
| 152 // The process may have exited just after the snapshot was generated. | 158 // The process may have exited just after the snapshot was generated. |
| 153 continue; | 159 continue; |
| 154 } | 160 } |
| 155 base::ProcessId pid = iter->second; | 161 base::ProcessId pid = iter->second; |
| 156 scoped_ptr<ArcProcessTask>& task = nspid_to_task_[process.pid]; | 162 scoped_ptr<ArcProcessTask>& task = nspid_to_task_[process.pid]; |
| 157 task.reset(new ArcProcessTask(pid, process.pid, process.process_name)); | 163 task.reset(new ArcProcessTask(pid, process.pid, process.process_name)); |
| 158 NotifyObserverTaskAdded(task.get()); | 164 NotifyObserverTaskAdded(task.get()); |
| 159 } | 165 } |
| 160 } | 166 } |
| 161 | 167 |
| 162 void ArcProcessTaskProvider::StartUpdating() { | 168 void ArcProcessTaskProvider::StartUpdating() { |
| 163 arc::ArcBridgeService::Get()->AddProcessObserver(this); | |
| 164 RequestProcessList(); | 169 RequestProcessList(); |
| 165 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | 170 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList |
| 166 // message when the process list changed. As of today, ARC does not send | 171 // message when the process list changed. As of today, ARC does not send |
| 167 // the process list unless we request it by RequestProcessList message. | 172 // the process list unless we request it by RequestProcessList message. |
| 168 timer_.Start( | 173 timer_.Start( |
| 169 FROM_HERE, | 174 FROM_HERE, |
| 170 base::TimeDelta::FromSeconds(1), | 175 base::TimeDelta::FromSeconds(1), |
| 171 this, | 176 this, |
| 172 &ArcProcessTaskProvider::RequestProcessList); | 177 &ArcProcessTaskProvider::RequestProcessList); |
| 173 } | 178 } |
| 174 | 179 |
| 175 void ArcProcessTaskProvider::StopUpdating() { | 180 void ArcProcessTaskProvider::StopUpdating() { |
| 176 arc::ArcBridgeService::Get()->RemoveProcessObserver(this); | |
| 177 timer_.Stop(); | 181 timer_.Stop(); |
| 178 nspid_to_task_.clear(); | 182 nspid_to_task_.clear(); |
| 179 } | 183 } |
| 180 | 184 |
| 181 } // namespace task_management | 185 } // namespace task_management |
| OLD | NEW |