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