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" |
16 #include "chrome/browser/chromeos/arc/arc_process.h" | 17 #include "chrome/browser/chromeos/arc/arc_process.h" |
17 #include "components/arc/arc_bridge_service.h" | 18 #include "components/arc/arc_bridge_service.h" |
18 #include "components/arc/arc_service.h" | 19 #include "components/arc/arc_service.h" |
19 #include "components/arc/instance_holder.h" | 20 #include "components/arc/instance_holder.h" |
20 | 21 |
21 namespace arc { | 22 namespace arc { |
22 | 23 |
23 // A single global entry to get a list of ARC processes. | 24 // A single global entry to get a list of ARC processes. |
24 // | 25 // |
25 // Call RequestProcessList() on the main UI thread to get a list of all ARC | 26 // Call RequestAppProcessList() / RequestSystemProcessList() on the main UI |
26 // processes. It returns vector<arc::ArcProcess>, which includes pid <-> nspid | 27 // thread to get a list of all ARC app / system processes. It returns |
27 // mapping. | 28 // vector<arc::ArcProcess>, which includes pid <-> nspid mapping. |
28 // Example: | 29 // Example: |
29 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} | 30 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} |
30 // | 31 // |
31 // arc::ArcProcessService* arc_process_service = | 32 // arc::ArcProcessService* arc_process_service = |
32 // arc::ArcProcessService::Get(); | 33 // arc::ArcProcessService::Get(); |
33 // if (!arc_process_service || | 34 // if (!arc_process_service || |
34 // !arc_process_service->RequestProcessList( | 35 // !arc_process_service->RequestAppProcessList( |
35 // base::Bind(&OnUpdateProcessList)) { | 36 // base::Bind(&OnUpdateProcessList)) { |
36 // LOG(ERROR) << "ARC process instance not ready."; | 37 // LOG(ERROR) << "ARC process instance not ready."; |
37 // } | 38 // } |
39 // | |
40 // [System Process] | |
41 // The system process here is defined by the scope. If the process is produced | |
42 // under system_server in Android, we regard it as one of Android app process. | |
43 // Otherwise, the processes that are introduced by init would then be regarded | |
44 // as System Process. RequestAppProcessList() is responsible for app processes | |
45 // while RequestSystemProcessList() is responsible for System Processes. | |
38 class ArcProcessService | 46 class ArcProcessService |
39 : public ArcService, | 47 : public ArcService, |
40 public InstanceHolder<mojom::ProcessInstance>::Observer { | 48 public InstanceHolder<mojom::ProcessInstance>::Observer { |
41 public: | 49 public: |
42 using RequestProcessListCallback = | 50 using RequestProcessListCallback = |
43 base::Callback<void(const std::vector<ArcProcess>&)>; | 51 base::Callback<void(const std::vector<ArcProcess>&)>; |
44 | 52 |
45 explicit ArcProcessService(ArcBridgeService* bridge_service); | 53 explicit ArcProcessService(ArcBridgeService* bridge_service); |
46 ~ArcProcessService() override; | 54 ~ArcProcessService() override; |
47 | 55 |
48 // Returns nullptr before the global instance is ready. | 56 // Returns nullptr before the global instance is ready. |
49 static ArcProcessService* Get(); | 57 static ArcProcessService* Get(); |
50 | 58 |
51 // InstanceHolder<mojom::ProcessInstance>::Observer overrides. | 59 // InstanceHolder<mojom::ProcessInstance>::Observer overrides. |
52 void OnInstanceReady() override; | 60 void OnInstanceReady() override; |
53 | 61 |
54 // Returns true if ARC IPC is ready for process list request, | 62 // Returns true if ARC IPC is ready for process list request, |
55 // otherwise false. | 63 // otherwise false. |
56 bool RequestProcessList(RequestProcessListCallback callback); | 64 bool RequestAppProcessList(RequestProcessListCallback callback); |
65 void RequestSystemProcessList(RequestProcessListCallback callback); | |
57 | 66 |
58 private: | 67 private: |
68 using PidMap = std::map<base::ProcessId, base::ProcessId>; | |
69 | |
70 class NSPidToPidMap : public PidMap, | |
Luis Héctor Chávez
2016/07/15 16:19:42
Favor composition over inheritance. std::map also
cylee1
2016/07/15 23:17:03
agree
Hsu-Cheng
2016/07/27 08:07:59
Done.
| |
71 public base::RefCountedThreadSafe<NSPidToPidMap> { | |
72 private: | |
73 friend class base::RefCountedThreadSafe<NSPidToPidMap>; | |
74 | |
75 virtual ~NSPidToPidMap(); | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(NSPidToPidMap); | |
78 }; | |
79 | |
59 void Reset(); | 80 void Reset(); |
60 | 81 |
61 void OnReceiveProcessList( | 82 void OnReceiveProcessList( |
62 const RequestProcessListCallback& callback, | 83 const RequestProcessListCallback& callback, |
63 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); | 84 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> instance_processes); |
64 | 85 |
65 void CallbackRelay( | 86 static std::vector<ArcProcess> UpdateAndReturnProcessList( |
Luis Héctor Chávez
2016/07/15 16:19:42
prefer having private static methods as standalone
Hsu-Cheng
2016/07/27 08:07:59
Done.
| |
66 const RequestProcessListCallback& callback, | 87 scoped_refptr<NSPidToPidMap> pid_map, |
67 const std::vector<ArcProcess>* ret_processes); | 88 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes); |
68 | 89 |
69 void UpdateAndReturnProcessList( | 90 static std::vector<ArcProcess> FilterProcessList( |
70 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 91 NSPidToPidMap& pid_map, |
71 std::vector<ArcProcess>* ret_processes); | 92 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes); |
72 | 93 |
73 void PopulateProcessList( | 94 // Computes and updates the map from PID in ARC namespace to PID in system |
74 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 95 // namespace. |
75 std::vector<ArcProcess>* ret_processes); | 96 static void UpdateNspidToPidMap(NSPidToPidMap& pid_map); |
76 | 97 |
77 void UpdateNspidToPidMap(); | 98 // There are some expensive tasks such as traverse whole process tree that |
99 // we can't do it on the UI thread. Thus we need a task runner to handle such | |
100 // heavy tasks. | |
101 scoped_refptr<base::SingleThreadTaskRunner> heavy_task_runner_; | |
78 | 102 |
79 // Keep a cache pid mapping of all arc processes so to minimize the number of | 103 // Keep a cache pid mapping of all arc processes so to minimize the number of |
80 // nspid lookup from /proc/<PID>/status. | 104 // nspid lookup from /proc/<PID>/status. |
81 // To play safe, always modify |nspid_to_pid_| on the worker thread. | 105 // To play safe, always modify |nspid_to_pid_| on the worker thread. |
82 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; | 106 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 | 107 |
89 // Always keep this the last member of this class to make sure it's the | 108 // Always keep this the last member of this class to make sure it's the |
90 // first thing to be destructed. | 109 // first thing to be destructed. |
91 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; | 110 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; |
92 | 111 |
93 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); | 112 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); |
94 }; | 113 }; |
95 | 114 |
96 } // namespace arc | 115 } // namespace arc |
97 | 116 |
98 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 117 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
OLD | NEW |