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 base::ProcessId& operator[](const base::ProcessId& key) { | |
73 return pidmap_[key]; | |
74 } | |
75 base::ProcessId& operator[](base::ProcessId&& key) { return pidmap_[key]; } | |
cylee1
2016/08/01 10:14:23
curious: why do you need both versions at line 75
Hsu-Cheng
2016/08/02 03:58:55
Yes, you are right, line 75 is redundant. The use
| |
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 PidMap pidmap_; | |
91 DISALLOW_COPY_AND_ASSIGN(NSPidToPidMap); | |
92 }; | |
57 | 93 |
58 private: | 94 private: |
59 void Reset(); | |
60 | |
61 void OnReceiveProcessList( | 95 void OnReceiveProcessList( |
62 const RequestProcessListCallback& callback, | 96 const RequestProcessListCallback& callback, |
63 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); | 97 const mojo::Array<arc::mojom::RunningAppProcessInfoPtr> |
98 instance_processes); | |
64 | 99 |
65 void CallbackRelay( | 100 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(); |
66 const RequestProcessListCallback& callback, | |
67 const std::vector<ArcProcess>* ret_processes); | |
68 | 101 |
69 void UpdateAndReturnProcessList( | 102 // There are some expensive tasks such as traverse whole process tree that |
70 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 103 // we can't do it on the UI thread. Thus we need an additional thread to |
71 std::vector<ArcProcess>* ret_processes); | 104 // handle |
72 | 105 // such tasks. |
73 void PopulateProcessList( | 106 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 | 107 |
79 // Keep a cache pid mapping of all arc processes so to minimize the number of | 108 // Keep a cache pid mapping of all arc processes so to minimize the number of |
80 // nspid lookup from /proc/<PID>/status. | 109 // nspid lookup from /proc/<PID>/status. |
81 // To play safe, always modify |nspid_to_pid_| on the worker thread. | 110 // To play safe, always modify |nspid_to_pid_| on the |heavy_task_thread_|. |
82 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; | 111 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 | 112 |
89 // Always keep this the last member of this class to make sure it's the | 113 // Always keep this the last member of this class to make sure it's the |
90 // first thing to be destructed. | 114 // first thing to be destructed. |
91 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; | 115 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; |
92 | 116 |
93 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); | 117 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); |
94 }; | 118 }; |
95 | 119 |
96 } // namespace arc | 120 } // namespace arc |
97 | 121 |
98 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 122 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
OLD | NEW |