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_manager/providers/arc/arc_process_task_provider.h" | 5 #include "chrome/browser/task_manager/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_manager { | 23 namespace task_manager { |
| 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 ArcTaskMap* pid_to_task, | |
| 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; |
| 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()]; |
| 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::OnUpdateAppProcessList( |
| 93 const std::vector<ArcProcess>& processes) { | |
| 94 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateAppProcessList"); | |
| 95 UpdateProcessList(&nspid_to_task_, processes); | |
| 96 ScheduleNextAppRequest(); | |
| 97 } | |
| 98 | |
| 99 void ArcProcessTaskProvider::OnUpdateSystemProcessList( | |
| 100 const std::vector<ArcProcess>& processes) { | |
| 101 UpdateProcessList(&nspid_to_sys_task_, processes); | |
| 102 ScheduleNextSystemRequest(); | |
| 103 } | |
| 104 | |
| 105 void ArcProcessTaskProvider::RequestAppProcessList() { | |
| 93 arc::ArcProcessService* arc_process_service = | 106 arc::ArcProcessService* arc_process_service = |
| 94 arc::ArcProcessService::Get(); | 107 arc::ArcProcessService::Get(); |
| 95 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateProcessList, | 108 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateAppProcessList, |
| 96 weak_ptr_factory_.GetWeakPtr()); | 109 weak_ptr_factory_.GetWeakPtr()); |
| 97 if (!arc_process_service || | 110 if (!arc_process_service || |
| 98 !arc_process_service->RequestProcessList(callback)) { | 111 !arc_process_service->RequestAppProcessList(callback)) { |
| 99 VLOG(2) << "ARC process instance is not ready."; | 112 VLOG(2) << "ARC process instance is not ready."; |
| 100 // Update with the empty ARC process list. | 113 ScheduleNextAppRequest(); |
| 101 // Note that this can happen in the middle of the session if the user has | 114 return; |
| 102 // just opted out from ARC. | |
| 103 callback.Run(std::vector<ArcProcess>()); | |
| 104 } | 115 } |
| 105 } | 116 } |
| 106 | 117 |
| 118 void ArcProcessTaskProvider::RequestSystemProcessList() { | |
| 119 arc::ArcProcessService* arc_process_service = arc::ArcProcessService::Get(); | |
| 120 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
| 121 weak_ptr_factory_.GetWeakPtr()); | |
| 122 if (!arc_process_service) { | |
| 123 VLOG(2) << "ARC process instance is not ready."; | |
| 124 ScheduleNextSystemRequest(); | |
| 125 return; | |
| 126 } | |
| 127 arc_process_service->RequestSystemProcessList(callback); | |
| 128 } | |
| 129 | |
| 107 void ArcProcessTaskProvider::StartUpdating() { | 130 void ArcProcessTaskProvider::StartUpdating() { |
| 108 is_updating_ = true; | 131 is_updating_ = true; |
| 109 RequestProcessList(); | 132 RequestAppProcessList(); |
| 133 RequestSystemProcessList(); | |
| 110 } | 134 } |
| 111 | 135 |
| 112 void ArcProcessTaskProvider::StopUpdating() { | 136 void ArcProcessTaskProvider::StopUpdating() { |
| 113 is_updating_ = false; | 137 is_updating_ = false; |
| 114 nspid_to_task_.clear(); | 138 nspid_to_task_.clear(); |
| 139 nspid_to_sys_task_.clear(); | |
| 115 } | 140 } |
| 116 | 141 |
| 117 void ArcProcessTaskProvider::ScheduleNextRequest() { | 142 void ArcProcessTaskProvider::ScheduleNextRequest(const base::Closure& task, |
| 143 const int delaySeconds) { | |
| 118 if (!is_updating_) | 144 if (!is_updating_) |
| 119 return; | 145 return; |
| 120 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | 146 // 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 | 147 // message when the process list changed. As of today, ARC does not send |
| 122 // the process list unless we request it by RequestProcessList message. | 148 // the process list unless we request it by RequestAppProcessList message. |
| 123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 149 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 124 FROM_HERE, | 150 FROM_HERE, task, base::TimeDelta::FromSeconds(delaySeconds)); |
| 125 base::Bind(&ArcProcessTaskProvider::RequestProcessList, | 151 } |
| 126 weak_ptr_factory_.GetWeakPtr()), | 152 |
| 127 base::TimeDelta::FromSeconds(kUpdateProcessListDelaySeconds)); | 153 void ArcProcessTaskProvider::ScheduleNextAppRequest() { |
| 154 ScheduleNextRequest(base::Bind(&ArcProcessTaskProvider::RequestAppProcessList, | |
| 155 base::Unretained(this)), | |
|
afakhry
2016/08/26 15:55:30
You should use a weak_ptr here.
cylee1
2016/08/29 10:51:09
I had discussed it with hctsai@. This is actually
afakhry
2016/08/30 16:37:57
Hmm, it's not really an overkill since you already
cylee1
2016/08/30 17:03:39
I'll respect to your decision.
| |
| 156 kUpdateAppProcessListDelaySeconds); | |
| 157 } | |
| 158 | |
| 159 void ArcProcessTaskProvider::ScheduleNextSystemRequest() { | |
| 160 ScheduleNextRequest( | |
| 161 base::Bind(&ArcProcessTaskProvider::RequestSystemProcessList, | |
| 162 base::Unretained(this)), | |
|
afakhry
2016/08/26 15:55:30
here too.
| |
| 163 kUpdateSystemProcessListDelaySeconds); | |
| 128 } | 164 } |
| 129 | 165 |
| 130 } // namespace task_manager | 166 } // namespace task_manager |
| OLD | NEW |