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_forward.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" |
15 #include "base/process/process_iterator.h" | |
14 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.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 | 21 |
20 namespace arc { | 22 namespace arc { |
21 | 23 |
22 // A single global entry to get a list of ARC processes. | 24 // A single global entry to get a list of ARC processes. |
23 // | 25 // |
24 // Call RequestProcessList() on the main UI thread to get a list of all ARC | 26 // Call RequestAppProcessList() / RequestSystemProcessList() on the main UI |
25 // processes. It returns vector<arc::ArcProcess>, which includes pid <-> nspid | 27 // thread to get a list of all ARC app / system processes. It returns |
26 // mapping. | 28 // vector<arc::ArcProcess>, which includes pid <-> nspid mapping. |
27 // Example: | 29 // Example: |
28 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} | 30 // void OnUpdateProcessList(const vector<arc::ArcProcess>&) {...} |
29 // | 31 // |
30 // arc::ArcProcessService* arc_process_service = | 32 // arc::ArcProcessService* arc_process_service = |
31 // arc::ArcProcessService::Get(); | 33 // arc::ArcProcessService::Get(); |
32 // if (!arc_process_service || | 34 // if (!arc_process_service || |
33 // !arc_process_service->RequestProcessList( | 35 // !arc_process_service->RequestAppProcessList( |
34 // base::Bind(&OnUpdateProcessList)) { | 36 // base::Bind(&OnUpdateProcessList)) { |
35 // LOG(ERROR) << "ARC process instance not ready."; | 37 // LOG(ERROR) << "ARC process instance not ready."; |
36 // } | 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. | |
37 class ArcProcessService : public ArcService, | 46 class ArcProcessService : public ArcService, |
38 public ArcBridgeService::Observer { | 47 public ArcBridgeService::Observer, |
48 public base::RefCountedThreadSafe<ArcProcessService> { | |
Luis Héctor Chávez
2016/07/06 19:49:15
This is a bit problematic, since RefCounted needs
| |
39 public: | 49 public: |
40 using RequestProcessListCallback = | 50 using RequestProcessListCallback = |
41 base::Callback<void(const std::vector<ArcProcess>&)>; | 51 base::Callback<void(const std::vector<ArcProcess>&)>; |
42 | 52 |
43 explicit ArcProcessService(ArcBridgeService* bridge_service); | 53 explicit ArcProcessService(ArcBridgeService* bridge_service); |
44 ~ArcProcessService() override; | 54 ~ArcProcessService() override; |
45 | 55 |
46 // Returns nullptr before the global instance is ready. | 56 // Returns nullptr before the global instance is ready. |
47 static ArcProcessService* Get(); | 57 static ArcProcessService* Get(); |
48 | 58 |
49 // ArcBridgeService::Observer overrides. | 59 // ArcBridgeService::Observer overrides. |
50 void OnProcessInstanceReady() override; | 60 void OnProcessInstanceReady() override; |
51 | 61 |
52 // Returns true if ARC IPC is ready for process list request, | 62 // Returns true if ARC IPC is ready for process list request, |
53 // otherwise false. | 63 // otherwise false. |
54 bool RequestProcessList(RequestProcessListCallback callback); | 64 bool RequestAppProcessList(RequestProcessListCallback callback); |
65 void RequestSystemProcessList(RequestProcessListCallback callback); | |
55 | 66 |
56 private: | 67 private: |
57 void Reset(); | 68 void Reset(); |
58 | 69 |
59 void OnReceiveProcessList( | 70 void OnReceiveProcessList( |
60 const RequestProcessListCallback& callback, | 71 const RequestProcessListCallback& callback, |
61 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); | 72 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); |
62 | 73 |
63 void CallbackRelay( | 74 std::vector<ArcProcess> UpdateAndReturnProcessList( |
64 const RequestProcessListCallback& callback, | 75 const std::vector<ArcProcess>& raw_processes); |
65 const std::vector<ArcProcess>* ret_processes); | |
66 | 76 |
67 void UpdateAndReturnProcessList( | 77 std::vector<ArcProcess> PopulateProcessList( |
68 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 78 const std::vector<ArcProcess>& raw_processes); |
69 std::vector<ArcProcess>* ret_processes); | |
70 | 79 |
71 void PopulateProcessList( | 80 std::vector<ArcProcess> ConvertMojoProcess( |
72 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 81 mojo::Array<arc::mojom::RunningAppProcessInfoPtr>& mojo_processes); |
73 std::vector<ArcProcess>* ret_processes); | |
74 | 82 |
75 void UpdateNspidToPidMap(); | 83 void UpdateNspidToPidMap(); |
76 | 84 |
85 static std::vector<ArcProcess> GetArcSystemProcessList(); | |
Luis Héctor Chávez
2016/07/06 20:28:12
Prefer having these two in the anonymous namespace
| |
86 | |
87 static base::ProcessId GetArcInitProcessId( | |
88 const base::ProcessIterator::ProcessEntries& entry_list); | |
89 | |
77 // Keep a cache pid mapping of all arc processes so to minimize the number of | 90 // Keep a cache pid mapping of all arc processes so to minimize the number of |
78 // nspid lookup from /proc/<PID>/status. | 91 // nspid lookup from /proc/<PID>/status. |
79 // To play safe, always modify |nspid_to_pid_| on the worker thread. | 92 // To play safe, always modify |nspid_to_pid_| on the worker thread. |
80 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; | 93 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; |
81 | 94 |
82 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 95 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
83 | 96 |
84 // To ensure internal state changes are done on the same worker thread. | 97 // To ensure internal state changes are done on the same worker thread. |
85 base::ThreadChecker thread_checker_; | 98 base::ThreadChecker thread_checker_; |
86 | 99 |
87 // Always keep this the last member of this class to make sure it's the | 100 // Always keep this the last member of this class to make sure it's the |
88 // first thing to be destructed. | 101 // first thing to be destructed. |
89 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; | 102 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; |
90 | 103 |
91 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); | 104 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); |
92 }; | 105 }; |
93 | 106 |
94 } // namespace arc | 107 } // namespace arc |
95 | 108 |
96 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 109 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
OLD | NEW |