Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "chrome/browser/chromeos/arc/arc_process.h" | 18 #include "chrome/browser/chromeos/arc/arc_process.h" |
| 19 #include "chrome/browser/chromeos/arc/arc_process_service.h" | 19 #include "chrome/browser/chromeos/arc/arc_process_service.h" |
| 20 #include "components/arc/common/process.mojom.h" | 20 #include "components/arc/common/process.mojom.h" |
| 21 | 21 |
| 22 namespace task_management { | 22 namespace task_management { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const int kUpdateProcessListDelaySeconds = 1; | 26 const int kUpdateAppProcessListDelaySeconds = 1; |
| 27 const int kUpdateSystemProcessListDelaySeconds = 3; | |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 using std::set; | 31 using std::set; |
| 31 using arc::ArcProcess; | 32 using arc::ArcProcess; |
| 32 using base::Process; | 33 using base::Process; |
| 33 using base::ProcessId; | 34 using base::ProcessId; |
| 34 | 35 |
| 35 ArcProcessTaskProvider::ArcProcessTaskProvider() | 36 ArcProcessTaskProvider::ArcProcessTaskProvider() |
| 36 : is_updating_(false), weak_ptr_factory_(this) {} | 37 : is_updating_(false), weak_ptr_factory_(this) {} |
| 37 | 38 |
| 38 ArcProcessTaskProvider::~ArcProcessTaskProvider() {} | 39 ArcProcessTaskProvider::~ArcProcessTaskProvider() {} |
| 39 | 40 |
| 40 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, | 41 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, |
| 41 int child_id, | 42 int child_id, |
| 42 int route_id) { | 43 int route_id) { |
| 43 // ARC tasks are not associated with any URL request. | 44 // ARC tasks are not associated with any URL request. |
| 44 return nullptr; | 45 return nullptr; |
| 45 } | 46 } |
| 46 | 47 |
| 47 void ArcProcessTaskProvider::OnUpdateProcessList( | 48 void ArcProcessTaskProvider::UpdateAppProcessList( |
| 48 const std::vector<ArcProcess>& processes) { | 49 const std::vector<ArcProcess>& processes) { |
| 49 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); | |
| 50 | |
| 51 if (!is_updating_) | 50 if (!is_updating_) |
| 52 return; | 51 return; |
| 53 | 52 |
| 54 // NB: |processes| can be already stale here because it is sent via IPC, and | 53 // NB: |processes| can be already stale here because it is sent via IPC, and |
| 55 // we can never avoid that. See also the comment at the declaration of | 54 // we can never avoid that. See also the comment at the declaration of |
| 56 // ArcProcessTaskProvider. | 55 // ArcProcessTaskProvider. |
| 57 | 56 |
| 58 set<ProcessId> nspid_to_remove; | 57 set<ProcessId> nspid_to_remove; |
| 59 for (const auto& entry : nspid_to_task_) | 58 for (const auto& entry : nspid_to_task_) |
| 60 nspid_to_remove.insert(entry.first); | 59 nspid_to_remove.insert(entry.first); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 78 DCHECK(task.get()); | 77 DCHECK(task.get()); |
| 79 task->SetProcessState(entry.process_state); | 78 task->SetProcessState(entry.process_state); |
| 80 } | 79 } |
| 81 } | 80 } |
| 82 | 81 |
| 83 for (const auto& entry : nspid_to_remove) { | 82 for (const auto& entry : nspid_to_remove) { |
| 84 // Stale arc process. | 83 // Stale arc process. |
| 85 NotifyObserverTaskRemoved(nspid_to_task_[entry].get()); | 84 NotifyObserverTaskRemoved(nspid_to_task_[entry].get()); |
| 86 nspid_to_task_.erase(entry); | 85 nspid_to_task_.erase(entry); |
| 87 } | 86 } |
| 88 ScheduleNextRequest(); | |
| 89 } | 87 } |
| 90 | 88 |
| 91 void ArcProcessTaskProvider::RequestProcessList() { | 89 void ArcProcessTaskProvider::UpdateSystemProcessList( |
|
cylee1
2016/06/02 20:58:45
The logic is almost identical to the original func
Hsu-Cheng
2016/06/29 10:33:57
Done.
| |
| 90 const std::vector<ArcProcess>& processes) { | |
| 91 if (!is_updating_) | |
| 92 return; | |
| 93 | |
| 94 set<ProcessId> pid_to_remove; | |
| 95 for (const auto& it : pid_to_sys_task_) | |
| 96 pid_to_remove.insert(it.first); | |
| 97 | |
| 98 for (const auto& it : processes) { | |
| 99 if (pid_to_remove.erase(it.pid) == 0) { | |
| 100 // New arc process. | |
| 101 std::unique_ptr<ArcProcessTask>& task = pid_to_sys_task_[it.pid]; | |
| 102 // After calling NotifyObserverTaskAdded(), the raw pointer of |task| is | |
| 103 // remebered somewhere else. One should not (implicitly) delete the | |
| 104 // referenced object before calling NotifyObserverTaskRemoved() first | |
| 105 // (crbug.com/587707). | |
| 106 DCHECK(!task.get()) << | |
| 107 "Task with the same pid should not be added twice."; | |
| 108 task.reset(new ArcProcessTask( | |
| 109 it.pid, it.nspid, it.process_name, it.process_state)); | |
| 110 NotifyObserverTaskAdded(task.get()); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 for (const auto& it : pid_to_remove) { | |
| 115 // Stale arc process. | |
| 116 NotifyObserverTaskRemoved(pid_to_sys_task_[it].get()); | |
| 117 pid_to_sys_task_.erase(it); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void ArcProcessTaskProvider::OnUpdateAppProcessList( | |
|
cylee1
2016/06/02 20:58:45
I don't quite understand the benefit of separating
Hsu-Cheng
2016/06/29 10:33:57
Consolidated the logic here. This is because the c
| |
| 122 const std::vector<ArcProcess>& processes) { | |
| 123 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateAppProcessList"); | |
| 124 | |
| 125 UpdateAppProcessList(processes); | |
| 126 ScheduleNextAppRequest(); | |
| 127 } | |
| 128 | |
| 129 void ArcProcessTaskProvider::OnUpdateSystemProcessList( | |
| 130 const std::vector<ArcProcess>& processes) { | |
| 131 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateSystemProcessList"); | |
| 132 | |
| 133 UpdateSystemProcessList(processes); | |
| 134 ScheduleNextSystemRequest(); | |
| 135 } | |
| 136 | |
| 137 void ArcProcessTaskProvider::RequestAppProcessList() { | |
| 92 arc::ArcProcessService* arc_process_service = | 138 arc::ArcProcessService* arc_process_service = |
| 93 arc::ArcProcessService::Get(); | 139 arc::ArcProcessService::Get(); |
| 94 if (!arc_process_service || | 140 if (!arc_process_service || |
| 95 !arc_process_service->RequestProcessList( | 141 !arc_process_service->RequestAppProcessList( |
| 96 base::Bind(&ArcProcessTaskProvider::OnUpdateProcessList, | 142 base::Bind(&ArcProcessTaskProvider::OnUpdateAppProcessList, |
| 97 weak_ptr_factory_.GetWeakPtr()))) { | 143 weak_ptr_factory_.GetWeakPtr()))) { |
| 98 VLOG(2) << "ARC process instance is not ready."; | 144 VLOG(2) << "ARC process instance is not ready."; |
| 99 ScheduleNextRequest(); | 145 ScheduleNextAppRequest(); |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void ArcProcessTaskProvider::RequestSystemProcessList() { | |
| 150 arc::ArcProcessService* arc_process_service = | |
| 151 arc::ArcProcessService::Get(); | |
| 152 if (!arc_process_service || | |
| 153 !arc_process_service->RequestSystemProcessList( | |
| 154 base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
| 155 weak_ptr_factory_.GetWeakPtr()))) { | |
| 156 VLOG(2) << "ARC process instance is not ready."; | |
| 157 ScheduleNextSystemRequest(); | |
| 100 } | 158 } |
| 101 } | 159 } |
| 102 | 160 |
| 103 void ArcProcessTaskProvider::StartUpdating() { | 161 void ArcProcessTaskProvider::StartUpdating() { |
| 104 is_updating_ = true; | 162 is_updating_ = true; |
| 105 RequestProcessList(); | 163 RequestAppProcessList(); |
| 164 RequestSystemProcessList(); | |
| 106 } | 165 } |
| 107 | 166 |
| 108 void ArcProcessTaskProvider::StopUpdating() { | 167 void ArcProcessTaskProvider::StopUpdating() { |
| 109 is_updating_ = false; | 168 is_updating_ = false; |
| 110 nspid_to_task_.clear(); | 169 nspid_to_task_.clear(); |
| 170 pid_to_sys_task_.clear(); | |
| 111 } | 171 } |
| 112 | 172 |
| 113 void ArcProcessTaskProvider::ScheduleNextRequest() { | 173 void ArcProcessTaskProvider::ScheduleNextAppRequest() { |
| 114 if (!is_updating_) | 174 if (!is_updating_) |
| 115 return; | 175 return; |
| 116 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | 176 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList |
| 177 // message when the process list changed. As of today, ARC does not send | |
| 178 // the process list unless we request it by RequestProcessList message. | |
| 179 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 180 FROM_HERE, | |
| 181 base::Bind(&ArcProcessTaskProvider::RequestAppProcessList, | |
| 182 weak_ptr_factory_.GetWeakPtr()), | |
| 183 base::TimeDelta::FromSeconds(kUpdateAppProcessListDelaySeconds)); | |
| 184 } | |
| 185 | |
| 186 void ArcProcessTaskProvider::ScheduleNextSystemRequest() { | |
| 187 if (!is_updating_) | |
| 188 return; | |
| 189 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | |
| 117 // message when the process list changed. As of today, ARC does not send | 190 // message when the process list changed. As of today, ARC does not send |
| 118 // the process list unless we request it by RequestProcessList message. | 191 // the process list unless we request it by RequestProcessList message. |
| 119 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 192 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 120 FROM_HERE, | 193 FROM_HERE, |
| 121 base::Bind(&ArcProcessTaskProvider::RequestProcessList, | 194 base::Bind(&ArcProcessTaskProvider::RequestSystemProcessList, |
| 122 weak_ptr_factory_.GetWeakPtr()), | 195 weak_ptr_factory_.GetWeakPtr()), |
| 123 base::TimeDelta::FromSeconds(kUpdateProcessListDelaySeconds)); | 196 base::TimeDelta::FromSeconds(kUpdateSystemProcessListDelaySeconds)); |
| 124 } | 197 } |
| 125 | 198 |
| 126 } // namespace task_management | 199 } // namespace task_management |
| OLD | NEW |