| 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/callback_forward.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 { |
| 39 public: | 48 public: |
| 40 using RequestProcessListCallback = | 49 using RequestProcessListCallback = |
| 41 base::Callback<void(const std::vector<ArcProcess>&)>; | 50 base::Callback<void(const std::vector<ArcProcess>&)>; |
| 42 | 51 |
| 43 explicit ArcProcessService(ArcBridgeService* bridge_service); | 52 explicit ArcProcessService(ArcBridgeService* bridge_service); |
| 44 ~ArcProcessService() override; | 53 ~ArcProcessService() override; |
| 45 | 54 |
| 46 // Returns nullptr before the global instance is ready. | 55 // Returns nullptr before the global instance is ready. |
| 47 static ArcProcessService* Get(); | 56 static ArcProcessService* Get(); |
| 48 | 57 |
| 49 // ArcBridgeService::Observer overrides. | 58 // ArcBridgeService::Observer overrides. |
| 50 void OnProcessInstanceReady() override; | 59 void OnProcessInstanceReady() override; |
| 51 | 60 |
| 52 // Returns true if ARC IPC is ready for process list request, | 61 // Returns true if ARC IPC is ready for process list request, |
| 53 // otherwise false. | 62 // otherwise false. |
| 54 bool RequestProcessList(RequestProcessListCallback callback); | 63 bool RequestAppProcessList(RequestProcessListCallback callback); |
| 64 void RequestSystemProcessList(RequestProcessListCallback callback); |
| 55 | 65 |
| 56 private: | 66 private: |
| 57 void Reset(); | 67 void Reset(); |
| 58 | 68 |
| 69 void GetArcSystemProcessList(std::vector<ArcProcess>* ret_processes); |
| 70 |
| 71 static base::ProcessId GetArcInitProcessId( |
| 72 const base::ProcessIterator::ProcessEntries& entry_list); |
| 73 |
| 59 void OnReceiveProcessList( | 74 void OnReceiveProcessList( |
| 60 const RequestProcessListCallback& callback, | 75 const RequestProcessListCallback& callback, |
| 61 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); | 76 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes); |
| 62 | 77 |
| 63 void CallbackRelay( | 78 void CallbackRelay( |
| 64 const RequestProcessListCallback& callback, | 79 const RequestProcessListCallback& callback, |
| 65 const std::vector<ArcProcess>* ret_processes); | 80 const std::vector<ArcProcess>* ret_processes); |
| 66 | 81 |
| 67 void UpdateAndReturnProcessList( | 82 void UpdateAndReturnProcessList( |
| 68 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 83 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
| 69 std::vector<ArcProcess>* ret_processes); | 84 std::vector<ArcProcess>* ret_processes); |
| 70 | 85 |
| 71 void PopulateProcessList( | 86 void PopulateProcessList( |
| 72 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 87 const std::vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
| 73 std::vector<ArcProcess>* ret_processes); | 88 std::vector<ArcProcess>* ret_processes); |
| 74 | 89 |
| 75 void UpdateNspidToPidMap(); | 90 void UpdateNspidToPidMap(); |
| 76 | 91 |
| 92 void PostTaskToOwnThreadAndReply(const base::Closure& task, |
| 93 const base::Closure& reply); |
| 94 |
| 77 // Keep a cache pid mapping of all arc processes so to minimize the number of | 95 // Keep a cache pid mapping of all arc processes so to minimize the number of |
| 78 // nspid lookup from /proc/<PID>/status. | 96 // nspid lookup from /proc/<PID>/status. |
| 79 // To play safe, always modify |nspid_to_pid_| on the worker thread. | 97 // To play safe, always modify |nspid_to_pid_| on the worker thread. |
| 80 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; | 98 std::map<base::ProcessId, base::ProcessId> nspid_to_pid_; |
| 81 | 99 |
| 82 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 100 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
| 83 | 101 |
| 84 // To ensure internal state changes are done on the same worker thread. | 102 // To ensure internal state changes are done on the same worker thread. |
| 85 base::ThreadChecker thread_checker_; | 103 base::ThreadChecker thread_checker_; |
| 86 | 104 |
| 87 // Always keep this the last member of this class to make sure it's the | 105 // Always keep this the last member of this class to make sure it's the |
| 88 // first thing to be destructed. | 106 // first thing to be destructed. |
| 89 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; | 107 base::WeakPtrFactory<ArcProcessService> weak_ptr_factory_; |
| 90 | 108 |
| 91 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); | 109 DISALLOW_COPY_AND_ASSIGN(ArcProcessService); |
| 92 }; | 110 }; |
| 93 | 111 |
| 94 } // namespace arc | 112 } // namespace arc |
| 95 | 113 |
| 96 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ | 114 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_PROCESS_SERVICE_H_ |
| OLD | NEW |