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/callback.h" | |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/process/process.h" | 16 #include "base/process/process.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
| 18 #include "chrome/browser/chromeos/arc/arc_process.h" | 19 #include "chrome/browser/chromeos/arc/arc_process.h" |
| 19 #include "chrome/browser/chromeos/arc/arc_process_service.h" | 20 #include "chrome/browser/chromeos/arc/arc_process_service.h" |
| 20 #include "components/arc/common/process.mojom.h" | 21 #include "components/arc/common/process.mojom.h" |
| 21 | 22 |
| 22 namespace task_management { | 23 namespace task_management { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const int kUpdateProcessListDelaySeconds = 1; | 27 const int kUpdateAppProcessListDelaySeconds = 1; |
| 28 const int kUpdateSystemProcessListDelaySeconds = 3; | |
| 27 | 29 |
| 28 } // namespace | 30 } // namespace |
| 29 | 31 |
| 30 using std::set; | 32 using std::set; |
| 31 using arc::ArcProcess; | 33 using arc::ArcProcess; |
| 32 using base::Process; | 34 using base::Process; |
| 33 using base::ProcessId; | 35 using base::ProcessId; |
| 34 | 36 |
| 35 ArcProcessTaskProvider::ArcProcessTaskProvider() | 37 ArcProcessTaskProvider::ArcProcessTaskProvider() |
| 36 : is_updating_(false), weak_ptr_factory_(this) {} | 38 : is_updating_(false), weak_ptr_factory_(this) {} |
| 37 | 39 |
| 38 ArcProcessTaskProvider::~ArcProcessTaskProvider() {} | 40 ArcProcessTaskProvider::~ArcProcessTaskProvider() {} |
| 39 | 41 |
| 40 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, | 42 Task* ArcProcessTaskProvider::GetTaskOfUrlRequest(int origin_pid, |
| 41 int child_id, | 43 int child_id, |
| 42 int route_id) { | 44 int route_id) { |
| 43 // ARC tasks are not associated with any URL request. | 45 // ARC tasks are not associated with any URL request. |
| 44 return nullptr; | 46 return nullptr; |
| 45 } | 47 } |
| 46 | 48 |
| 47 void ArcProcessTaskProvider::OnUpdateProcessList( | 49 void ArcProcessTaskProvider::UpdateProcessList( |
| 50 std::map<base::ProcessId, std::unique_ptr<ArcProcessTask>>& pid_to_task, | |
|
Luis Héctor Chávez
2016/07/15 16:19:42
conding guidelines say that parameters should eith
Hsu-Cheng
2016/07/27 08:08:00
Done.
| |
| 48 const std::vector<ArcProcess>& processes) { | 51 const std::vector<ArcProcess>& processes) { |
| 49 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); | |
| 50 | |
| 51 if (!is_updating_) | 52 if (!is_updating_) |
| 52 return; | 53 return; |
| 53 | 54 |
| 54 // NB: |processes| can be already stale here because it is sent via IPC, and | 55 // 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 | 56 // we can never avoid that. See also the comment at the declaration of |
| 56 // ArcProcessTaskProvider. | 57 // ArcProcessTaskProvider. |
| 57 | 58 |
| 58 set<ProcessId> nspid_to_remove; | 59 set<ProcessId> nspid_to_remove; |
|
Luis Héctor Chávez
2016/07/15 16:19:42
std::unordered_set?
Hsu-Cheng
2016/07/27 08:07:59
Done.
| |
| 59 for (const auto& entry : nspid_to_task_) | 60 for (const auto& entry : pid_to_task) |
| 60 nspid_to_remove.insert(entry.first); | 61 nspid_to_remove.insert(entry.first); |
| 61 | 62 |
| 62 for (const auto& entry : processes) { | 63 for (const auto& entry : processes) { |
| 63 if (nspid_to_remove.erase(entry.nspid()) == 0) { | 64 if (nspid_to_remove.erase(entry.nspid()) == 0) { |
| 64 // New arc process. | 65 // New arc process. |
| 65 std::unique_ptr<ArcProcessTask>& task = nspid_to_task_[entry.nspid()]; | 66 std::unique_ptr<ArcProcessTask>& task = pid_to_task[entry.nspid()]; |
|
Luis Héctor Chávez
2016/07/15 16:19:42
const? Same in L79
Hsu-Cheng
2016/07/27 08:08:00
'task' invokes reset() below
| |
| 66 // After calling NotifyObserverTaskAdded(), the raw pointer of |task| is | 67 // After calling NotifyObserverTaskAdded(), the raw pointer of |task| is |
| 67 // remebered somewhere else. One should not (implicitly) delete the | 68 // remebered somewhere else. One should not (implicitly) delete the |
| 68 // referenced object before calling NotifyObserverTaskRemoved() first | 69 // referenced object before calling NotifyObserverTaskRemoved() first |
| 69 // (crbug.com/587707). | 70 // (crbug.com/587707). |
| 70 DCHECK(!task.get()) << | 71 DCHECK(!task.get()) << |
| 71 "Task with the same pid should not be added twice."; | 72 "Task with the same pid should not be added twice."; |
| 72 task.reset(new ArcProcessTask(entry.pid(), entry.nspid(), | 73 task.reset(new ArcProcessTask(entry.pid(), entry.nspid(), |
| 73 entry.process_name(), entry.process_state(), | 74 entry.process_name(), entry.process_state(), |
| 74 entry.packages())); | 75 entry.packages())); |
| 75 NotifyObserverTaskAdded(task.get()); | 76 NotifyObserverTaskAdded(task.get()); |
| 76 } else { | 77 } else { |
| 77 // Update process state of existing process. | 78 // Update process state of existing process. |
| 78 std::unique_ptr<ArcProcessTask>& task = nspid_to_task_[entry.nspid()]; | 79 std::unique_ptr<ArcProcessTask>& task = pid_to_task[entry.nspid()]; |
| 79 DCHECK(task.get()); | 80 DCHECK(task.get()); |
| 80 task->SetProcessState(entry.process_state()); | 81 task->SetProcessState(entry.process_state()); |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 for (const auto& entry : nspid_to_remove) { | 85 for (const auto& entry : nspid_to_remove) { |
| 85 // Stale arc process. | 86 // Stale arc process. |
| 86 NotifyObserverTaskRemoved(nspid_to_task_[entry].get()); | 87 NotifyObserverTaskRemoved(pid_to_task[entry].get()); |
| 87 nspid_to_task_.erase(entry); | 88 pid_to_task.erase(entry); |
| 88 } | 89 } |
| 89 ScheduleNextRequest(); | |
| 90 } | 90 } |
| 91 | 91 |
| 92 void ArcProcessTaskProvider::RequestProcessList() { | 92 void ArcProcessTaskProvider::OnUpdateProcessList( |
| 93 std::map<base::ProcessId, std::unique_ptr<ArcProcessTask>>& pid_to_task, | |
| 94 const std::vector<ArcProcess>& processes, | |
| 95 const base::Closure& scheduler) { | |
| 96 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); | |
| 97 | |
| 98 UpdateProcessList(pid_to_task, processes); | |
| 99 scheduler.Run(); | |
| 100 } | |
| 101 | |
| 102 void ArcProcessTaskProvider::OnUpdateAppProcessList( | |
| 103 const std::vector<ArcProcess>& processes) { | |
| 104 OnUpdateProcessList( | |
| 105 nspid_to_task_, | |
| 106 processes, | |
| 107 base::Bind(&ArcProcessTaskProvider::ScheduleNextAppRequest, | |
| 108 weak_ptr_factory_.GetWeakPtr())); | |
|
cylee1
2016/07/15 23:17:03
I'm still hesitated that if you need WeakPtr since
Hsu-Cheng
2016/07/27 08:08:00
Removed the scheduler (and also the WeakPtr)
| |
| 109 } | |
| 110 | |
| 111 void ArcProcessTaskProvider::OnUpdateSystemProcessList( | |
| 112 const std::vector<ArcProcess>& processes) { | |
| 113 OnUpdateProcessList( | |
| 114 nspid_to_sys_task_, | |
| 115 processes, | |
| 116 base::Bind(&ArcProcessTaskProvider::ScheduleNextSystemRequest, | |
| 117 weak_ptr_factory_.GetWeakPtr())); | |
| 118 } | |
| 119 | |
| 120 void ArcProcessTaskProvider::RequestAppProcessList() { | |
| 93 arc::ArcProcessService* arc_process_service = | 121 arc::ArcProcessService* arc_process_service = |
| 94 arc::ArcProcessService::Get(); | 122 arc::ArcProcessService::Get(); |
| 95 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateProcessList, | 123 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateAppProcessList, |
| 96 weak_ptr_factory_.GetWeakPtr()); | 124 weak_ptr_factory_.GetWeakPtr()); |
| 97 if (!arc_process_service || | 125 if (!arc_process_service || |
| 98 !arc_process_service->RequestProcessList(callback)) { | 126 !arc_process_service->RequestAppProcessList(callback)) { |
| 99 VLOG(2) << "ARC process instance is not ready."; | 127 VLOG(2) << "ARC process instance is not ready."; |
| 100 // Update with the empty ARC process list. | 128 ScheduleNextAppRequest(); |
| 101 // Note that this can happen in the middle of the session if the user has | 129 return; |
| 102 // just opted out from ARC. | |
| 103 callback.Run(std::vector<ArcProcess>()); | |
| 104 } | 130 } |
| 105 } | 131 } |
| 106 | 132 |
| 133 void ArcProcessTaskProvider::RequestSystemProcessList() { | |
| 134 arc::ArcProcessService* arc_process_service = | |
| 135 arc::ArcProcessService::Get(); | |
| 136 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
|
Luis Héctor Chávez
2016/07/15 16:19:42
unused?
Hsu-Cheng
2016/07/27 08:07:59
Done.
| |
| 137 weak_ptr_factory_.GetWeakPtr()); | |
| 138 if (!arc_process_service) { | |
| 139 VLOG(2) << "ARC process instance is not ready."; | |
| 140 ScheduleNextSystemRequest(); | |
| 141 return; | |
| 142 } | |
| 143 arc_process_service->RequestSystemProcessList( | |
|
Luis Héctor Chávez
2016/07/15 16:19:42
be consistent: either have both this and RequestAp
cylee1
2016/07/15 23:17:03
RequestSystemProcessList never fail however (retur
Luis Héctor Chávez
2016/07/15 23:43:46
oh sorry, didn't notice that. disregard this then.
Hsu-Cheng
2016/07/27 08:08:00
Acknowledged.
| |
| 144 base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
| 145 weak_ptr_factory_.GetWeakPtr())); | |
| 146 } | |
| 147 | |
| 107 void ArcProcessTaskProvider::StartUpdating() { | 148 void ArcProcessTaskProvider::StartUpdating() { |
| 108 is_updating_ = true; | 149 is_updating_ = true; |
| 109 RequestProcessList(); | 150 RequestAppProcessList(); |
| 151 RequestSystemProcessList(); | |
| 110 } | 152 } |
| 111 | 153 |
| 112 void ArcProcessTaskProvider::StopUpdating() { | 154 void ArcProcessTaskProvider::StopUpdating() { |
| 113 is_updating_ = false; | 155 is_updating_ = false; |
| 114 nspid_to_task_.clear(); | 156 nspid_to_task_.clear(); |
| 157 nspid_to_sys_task_.clear(); | |
| 115 } | 158 } |
| 116 | 159 |
| 117 void ArcProcessTaskProvider::ScheduleNextRequest() { | 160 void ArcProcessTaskProvider::ScheduleNextRequest(const base::Closure& task, |
| 161 const int delaySeconds) { | |
| 118 if (!is_updating_) | 162 if (!is_updating_) |
| 119 return; | 163 return; |
| 120 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | 164 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList |
| 121 // message when the process list changed. As of today, ARC does not send | 165 // message when the process list changed. As of today, ARC does not send |
| 122 // the process list unless we request it by RequestProcessList message. | 166 // the process list unless we request it by RequestAppProcessList message. |
| 123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 167 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 124 FROM_HERE, | 168 FROM_HERE, task, base::TimeDelta::FromSeconds(delaySeconds)); |
| 125 base::Bind(&ArcProcessTaskProvider::RequestProcessList, | 169 } |
| 170 | |
| 171 void ArcProcessTaskProvider::ScheduleNextAppRequest() { | |
| 172 ScheduleNextRequest( | |
| 173 base::Bind(&ArcProcessTaskProvider::RequestAppProcessList, | |
| 126 weak_ptr_factory_.GetWeakPtr()), | 174 weak_ptr_factory_.GetWeakPtr()), |
| 127 base::TimeDelta::FromSeconds(kUpdateProcessListDelaySeconds)); | 175 kUpdateAppProcessListDelaySeconds); |
| 176 } | |
| 177 | |
| 178 void ArcProcessTaskProvider::ScheduleNextSystemRequest() { | |
| 179 ScheduleNextRequest( | |
| 180 base::Bind(&ArcProcessTaskProvider::RequestSystemProcessList, | |
| 181 weak_ptr_factory_.GetWeakPtr()), | |
| 182 kUpdateSystemProcessListDelaySeconds); | |
| 128 } | 183 } |
| 129 | 184 |
| 130 } // namespace task_management | 185 } // namespace task_management |
| OLD | NEW |