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