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::UpdateProcessList( |
49 std::map<base::ProcessId, std::unique_ptr<ArcProcessTask>>& pid_to_task, | |
48 const std::vector<ArcProcess>& processes) { | 50 const std::vector<ArcProcess>& processes) { |
49 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateProcessList"); | |
50 | |
51 if (!is_updating_) | 51 if (!is_updating_) |
52 return; | 52 return; |
53 | 53 |
54 // NB: |processes| can be already stale here because it is sent via IPC, and | 54 // 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 | 55 // we can never avoid that. See also the comment at the declaration of |
56 // ArcProcessTaskProvider. | 56 // ArcProcessTaskProvider. |
57 | 57 |
58 set<ProcessId> nspid_to_remove; | 58 set<ProcessId> nspid_to_remove; |
59 for (const auto& entry : nspid_to_task_) | 59 for (const auto& entry : pid_to_task) |
60 nspid_to_remove.insert(entry.first); | 60 nspid_to_remove.insert(entry.first); |
61 | 61 |
62 for (const auto& entry : processes) { | 62 for (const auto& entry : processes) { |
63 if (nspid_to_remove.erase(entry.nspid()) == 0) { | 63 if (nspid_to_remove.erase(entry.nspid()) == 0) { |
64 // New arc process. | 64 // New arc process. |
65 std::unique_ptr<ArcProcessTask>& task = nspid_to_task_[entry.nspid()]; | 65 std::unique_ptr<ArcProcessTask>& task = pid_to_task[entry.nspid()]; |
66 // After calling NotifyObserverTaskAdded(), the raw pointer of |task| is | 66 // After calling NotifyObserverTaskAdded(), the raw pointer of |task| is |
67 // remebered somewhere else. One should not (implicitly) delete the | 67 // remebered somewhere else. One should not (implicitly) delete the |
68 // referenced object before calling NotifyObserverTaskRemoved() first | 68 // referenced object before calling NotifyObserverTaskRemoved() first |
69 // (crbug.com/587707). | 69 // (crbug.com/587707). |
70 DCHECK(!task.get()) << | 70 DCHECK(!task.get()) << |
71 "Task with the same pid should not be added twice."; | 71 "Task with the same pid should not be added twice."; |
72 task.reset(new ArcProcessTask(entry.pid(), entry.nspid(), | 72 task.reset(new ArcProcessTask(entry.pid(), entry.nspid(), |
73 entry.process_name(), entry.process_state(), | 73 entry.process_name(), entry.process_state(), |
74 entry.packages())); | 74 entry.packages())); |
75 NotifyObserverTaskAdded(task.get()); | 75 NotifyObserverTaskAdded(task.get()); |
76 } else { | 76 } else { |
77 // Update process state of existing process. | 77 // Update process state of existing process. |
78 std::unique_ptr<ArcProcessTask>& task = nspid_to_task_[entry.nspid()]; | 78 std::unique_ptr<ArcProcessTask>& task = pid_to_task[entry.nspid()]; |
79 DCHECK(task.get()); | 79 DCHECK(task.get()); |
80 task->SetProcessState(entry.process_state()); | 80 task->SetProcessState(entry.process_state()); |
81 } | 81 } |
82 } | 82 } |
83 | 83 |
84 for (const auto& entry : nspid_to_remove) { | 84 for (const auto& entry : nspid_to_remove) { |
85 // Stale arc process. | 85 // Stale arc process. |
86 NotifyObserverTaskRemoved(nspid_to_task_[entry].get()); | 86 NotifyObserverTaskRemoved(pid_to_task[entry].get()); |
87 nspid_to_task_.erase(entry); | 87 pid_to_task.erase(entry); |
88 } | 88 } |
89 ScheduleNextRequest(); | |
90 } | 89 } |
91 | 90 |
92 void ArcProcessTaskProvider::RequestProcessList() { | 91 void ArcProcessTaskProvider::OnUpdateAppProcessList( |
92 const std::vector<ArcProcess>& processes) { | |
93 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateAppProcessList"); | |
94 | |
95 UpdateProcessList(nspid_to_task_, processes); | |
96 ScheduleNextAppRequest(); | |
97 } | |
98 | |
99 void ArcProcessTaskProvider::OnUpdateSystemProcessList( | |
100 const std::vector<ArcProcess>& processes) { | |
101 TRACE_EVENT0("browser", "ArcProcessTaskProvider::OnUpdateSystemProcessList"); | |
102 | |
103 UpdateProcessList(pid_to_sys_task_, processes); | |
cylee1
2016/06/30 03:45:52
it's called pid_to_sys_task_ but its key is actual
Hsu-Cheng
2016/06/30 09:56:24
Done.
| |
104 ScheduleNextSystemRequest(); | |
105 } | |
106 | |
107 void ArcProcessTaskProvider::RequestAppProcessList() { | |
93 arc::ArcProcessService* arc_process_service = | 108 arc::ArcProcessService* arc_process_service = |
94 arc::ArcProcessService::Get(); | 109 arc::ArcProcessService::Get(); |
95 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateProcessList, | 110 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateAppProcessList, |
96 weak_ptr_factory_.GetWeakPtr()); | 111 weak_ptr_factory_.GetWeakPtr()); |
97 if (!arc_process_service || | 112 if (!arc_process_service || |
98 !arc_process_service->RequestProcessList(callback)) { | 113 !arc_process_service->RequestAppProcessList(callback)) { |
99 VLOG(2) << "ARC process instance is not ready."; | 114 VLOG(2) << "ARC process instance is not ready."; |
100 // Update with the empty ARC process list. | |
101 // Note that this can happen in the middle of the session if the user has | |
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 = | |
120 arc::ArcProcessService::Get(); | |
121 auto callback = base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
122 weak_ptr_factory_.GetWeakPtr()); | |
123 if (!arc_process_service) { | |
124 VLOG(2) << "ARC process instance is not ready."; | |
125 } | |
126 arc_process_service->RequestSystemProcessList( | |
127 base::Bind(&ArcProcessTaskProvider::OnUpdateSystemProcessList, | |
128 weak_ptr_factory_.GetWeakPtr())); | |
129 ScheduleNextSystemRequest(); | |
130 } | |
131 | |
107 void ArcProcessTaskProvider::StartUpdating() { | 132 void ArcProcessTaskProvider::StartUpdating() { |
108 is_updating_ = true; | 133 is_updating_ = true; |
109 RequestProcessList(); | 134 RequestAppProcessList(); |
135 RequestSystemProcessList(); | |
110 } | 136 } |
111 | 137 |
112 void ArcProcessTaskProvider::StopUpdating() { | 138 void ArcProcessTaskProvider::StopUpdating() { |
113 is_updating_ = false; | 139 is_updating_ = false; |
114 nspid_to_task_.clear(); | 140 nspid_to_task_.clear(); |
141 pid_to_sys_task_.clear(); | |
115 } | 142 } |
116 | 143 |
117 void ArcProcessTaskProvider::ScheduleNextRequest() { | 144 void ArcProcessTaskProvider::ScheduleNextAppRequest() { |
118 if (!is_updating_) | 145 if (!is_updating_) |
119 return; | 146 return; |
120 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | 147 // 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 | 148 // message when the process list changed. As of today, ARC does not send |
122 // the process list unless we request it by RequestProcessList message. | 149 // the process list unless we request it by RequestAppProcessList message. |
123 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
124 FROM_HERE, | 151 FROM_HERE, |
125 base::Bind(&ArcProcessTaskProvider::RequestProcessList, | 152 base::Bind(&ArcProcessTaskProvider::RequestAppProcessList, |
126 weak_ptr_factory_.GetWeakPtr()), | 153 weak_ptr_factory_.GetWeakPtr()), |
127 base::TimeDelta::FromSeconds(kUpdateProcessListDelaySeconds)); | 154 base::TimeDelta::FromSeconds(kUpdateAppProcessListDelaySeconds)); |
155 } | |
156 | |
157 void ArcProcessTaskProvider::ScheduleNextSystemRequest() { | |
158 if (!is_updating_) | |
159 return; | |
160 // TODO(nya): Remove this timer once ARC starts to send us UpdateProcessList | |
161 // message when the process list changed. As of today, ARC does not send | |
162 // the process list unless we request it by RequestSystemProcessList message. | |
163 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
164 FROM_HERE, | |
165 base::Bind(&ArcProcessTaskProvider::RequestSystemProcessList, | |
166 weak_ptr_factory_.GetWeakPtr()), | |
167 base::TimeDelta::FromSeconds(kUpdateSystemProcessListDelaySeconds)); | |
128 } | 168 } |
129 | 169 |
130 } // namespace task_management | 170 } // namespace task_management |
OLD | NEW |