OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/process/process_iterator.h" |
15 #include "base/threading/thread_checker.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/threading/thread.h" |
16 #include "chrome/browser/chromeos/arc/arc_process.h" | 18 #include "chrome/browser/chromeos/arc/arc_process.h" |
17 #include "components/arc/arc_bridge_service.h" | 19 #include "components/arc/arc_bridge_service.h" |
18 #include "components/arc/arc_service.h" | 20 #include "components/arc/arc_service.h" |
19 #include "components/arc/instance_holder.h" | 21 #include "components/arc/instance_holder.h" |
20 | 22 |
21 namespace arc { | 23 namespace arc { |
22 | 24 |
23 // A single global entry to get a list of ARC processes. | 25 // A single global entry to get a list of ARC processes. |
24 // | 26 // |
25 // Call RequestProcessList() on the main UI thread to get a list of all ARC | 27 // Call RequestAppProcessList() / RequestSystemProcessList() on the main UI |
26 // processes. It returns vector<arc::ArcProcess>, which includes pid <-> nspid | 28 // thread to get a list of all ARC app / system processes. It returns |
27 // mapping. | 29 // vector<arc::ArcProcess>, which includes pid <-> nspid mapping. |
28 // Example: | 30 // Example: |
29 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} | 31 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} |
30 // | 32 // |
31 // arc::ArcProcessService* arc_process_service = | 33 // arc::ArcProcessService* arc_process_service = |
32 // arc::ArcProcessService::Get(); | 34 // arc::ArcProcessService::Get(); |
33 // if (!arc_process_service || | 35 // if (!arc_process_service || |
34 // !arc_process_service->RequestProcessList( | 36 // !arc_process_service->RequestAppProcessList( |
35 // base::Bind(&OnUpdateProcessList)) { | 37 // base::Bind(&OnUpdateProcessList)) { |
36 // LOG(ERROR) << "ARC process instance not ready."; | 38 // LOG(ERROR) << "ARC process instance not ready."; |
37 // } | 39 // } |
| 40 // |
| 41 // [System Process] |
| 42 // The system process here is defined by the scope. If the process is produced |
| 43 // under system_server in Android, we regard it as one of Android app process. |
| 44 // Otherwise, the processes that are introduced by init would then be regarded |
| 45 // as System Process. RequestAppProcessList() is responsible for app processes |
| 46 // while RequestSystemProcessList() is responsible for System Processes. |
38 class ArcProcessService | 47 class ArcProcessService |
39 : public ArcService, | 48 : public ArcService, |
40 public InstanceHolder<mojom::ProcessInstance>::Observer { | 49 public InstanceHolder<mojom::ProcessInstance>::Observer { |
41 public: | 50 public: |
42 using RequestProcessListCallback = | 51 using RequestProcessListCallback = |
43 base::Callback<void(const std::vector<ArcProcess>&)>; | 52 base::Callback<void(const std::vector<ArcProcess>&)>; |
44 | 53 |
45 explicit ArcProcessService(ArcBridgeService* bridge_service); | 54 explicit ArcProcessService(ArcBridgeService* bridge_service); |
46 ~ArcProcessService() override; | 55 ~ArcProcessService() override; |
47 | 56 |
48 // Returns nullptr before the global instance is ready. | 57 // Returns nullptr before the global instance is ready. |
49 static ArcProcessService* Get(); | 58 static ArcProcessService* Get(); |
50 | 59 |
51 // InstanceHolder<mojom::ProcessInstance>::Observer overrides. | 60 // InstanceHolder<mojom::ProcessInstance>::Observer overrides. |
52 void OnInstanceReady() override; | 61 void OnInstanceReady() override; |
53 | 62 |
54 // Returns true if ARC IPC is ready for process list request, | 63 // Returns true if ARC IPC is ready for process list request, |
55 // otherwise false. | 64 // otherwise false. |
56 bool RequestProcessList(RequestProcessListCallback callback); | 65 bool RequestAppProcessList(RequestProcessListCallback callback); |
| 66 void RequestSystemProcessList(RequestProcessListCallback callback); |
| 67 |
| 68 using PidMap = std::map<base::ProcessId, base::ProcessId>; |
| 69 |
| 70 class NSPidToPidMap : public base::RefCountedThreadSafe<NSPidToPidMap> { |
| 71 public: |
| 72 NSPidToPidMap(); |
| 73 base::ProcessId& operator[](const base::ProcessId& key) { |
| 74 return pidmap_[key]; |
| 75 } |
| 76 const base::ProcessId& at(const base::ProcessId& key) const { |
| 77 return pidmap_.at(key); |
| 78 } |
| 79 PidMap::size_type erase(const base::ProcessId& key) { |
| 80 return pidmap_.erase(key); |
| 81 } |
| 82 PidMap::const_iterator begin() const { return pidmap_.begin(); } |
| 83 PidMap::const_iterator end() const { return pidmap_.end(); } |
| 84 PidMap::const_iterator find(const base::ProcessId& key) const { |
| 85 return pidmap_.find(key); |
| 86 } |
| 87 void clear() { pidmap_.clear(); } |
| 88 |
| 89 private: |
| 90 friend base::RefCountedThreadSafe<NSPidToPidMap>; |
| 91 ~NSPidToPidMap(); |
| 92 |
| 93 PidMap pidmap_; |
| 94 DISALLOW_COPY_AND_ASSIGN(NSPidToPidMap); |
| 95 }; |
57 | 96 |
58 private: | 97 private: |
59 void Reset(); | |
60 | |
61 void OnReceiveProcessList( | 98 void OnReceiveProcessList( |
62 const RequestProcessListCallback& callback, | 99 const RequestProcessListCallback& callback, |
63 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); | 100 const mojo::Array<arc::mojom::RunningAppProcessInfoPtr> |
| 101 instance_processes); |
64 | 102 |
65 void CallbackRelay( | 103 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(); |
66 const RequestProcessListCallback& callback, | |
67 const std::vector<ArcProcess>* ret_processes); | |
68 | 104 |
69 void UpdateAndReturnProcessList( | 105 // There are some expensive tasks such as traverse whole process tree that |
70 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 106 // we can't do it on the UI thread. Thus we need an additional thread to |
71 std::vector<ArcProcess>* ret_processes); | 107 // handle |
72 | 108 // such tasks. |
73 void PopulateProcessList( | 109 base::Thread heavy_task_thread_; |
74 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | |
75 std::vector<ArcProcess>* ret_processes); | |
76 | |
77 void UpdateNspidToPidMap(); | |
78 | 110 |
79 // Keep a cache pid mapping of all arc processes so to minimize the number of | 111 // Keep a cache pid mapping of all arc processes so to minimize the number of |
80 // nspid lookup from /proc/<PID>/status. | 112 // nspid lookup from /proc/<PID>/status. |
81 // To play safe, always modify |nspid_to_pid_| on the worker thread. | 113 // To play safe, always modify |nspid_to_pid_| on the |heavy_task_thread_|. |
82 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; | 114 scoped_refptr<NSPidToPidMap> nspid_to_pid_; |
83 | |
84 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | |
85 | |
86 // To ensure internal state changes are done on the same worker thread. | |
87 base::ThreadChecker thread_checker_; | |
88 | 115 |
89 // Always keep this the last member of this class to make sure it's the | 116 // Always keep this the last member of this class to make sure it's the |
90 // first thing to be destructed. | 117 // first thing to be destructed. |
91 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; | 118 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; |
92 | 119 |
93 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); | 120 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); |
94 }; | 121 }; |
95 | 122 |
96 } // namespace arc | 123 } // namespace arc |
97 | 124 |
98 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 125 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
OLD | NEW |