Girish | 4e531c8 | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2023, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_MEDIA_UIDOBSERVER_H_ |
| 19 | #define ANDROID_MEDIA_UIDOBSERVER_H_ |
| 20 | |
| 21 | #include <map> |
| 22 | #include <set> |
| 23 | #include <mutex> |
| 24 | #include <functional> |
| 25 | #include <binder/ActivityManager.h> |
| 26 | #include <binder/IUidObserver.h> |
| 27 | #include <binder/BinderService.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | using OnProcessTerminated = std::function<void(int32_t pid, uid_t)>; |
| 32 | |
| 33 | struct ProcessInfoInterface; |
| 34 | |
| 35 | // |
| 36 | // UidObserver class |
| 37 | // |
| 38 | // This class implements a callback mechanism to notify the termination of the |
| 39 | // process/applications that are registered with this class. |
| 40 | // |
| 41 | // It uses ActivityManager get notification on when an UID is not existent |
| 42 | // anymore. |
| 43 | // Since one UID could have multiple PIDs, it uses ActivityManager |
| 44 | // (through ProcessInfoInterface) to query for the process/application |
| 45 | // state for the pids. |
| 46 | // |
| 47 | class UidObserver : |
| 48 | public BnUidObserver, |
| 49 | public virtual IBinder::DeathRecipient, |
| 50 | public virtual IServiceManager::LocalRegistrationCallback { |
| 51 | public: |
| 52 | explicit UidObserver(const sp<ProcessInfoInterface>& processInfo, |
| 53 | OnProcessTerminated onProcessTerminated); |
| 54 | virtual ~UidObserver(); |
| 55 | |
| 56 | // Start registration (with Application Manager) |
| 57 | void start(); |
| 58 | // Stop registration (with Application Manager) |
| 59 | void stop(); |
| 60 | |
| 61 | // Add this pid/uid to set of Uid to be observed. |
| 62 | void add(int pid, uid_t uid); |
| 63 | |
| 64 | private: |
| 65 | UidObserver() = delete; |
| 66 | UidObserver(const UidObserver&) = delete; |
| 67 | UidObserver(UidObserver&&) = delete; |
| 68 | UidObserver& operator=(const UidObserver&) = delete; |
| 69 | UidObserver& operator=(UidObserver&&) = delete; |
| 70 | |
| 71 | // IUidObserver implementation. |
| 72 | void onUidGone(uid_t uid, bool disabled) override; |
| 73 | void onUidActive(uid_t uid) override; |
| 74 | void onUidIdle(uid_t uid, bool disabled) override; |
| 75 | void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq, |
| 76 | int32_t capability) override; |
Austin Borger | dddb755 | 2023-03-30 17:53:01 -0700 | [diff] [blame^] | 77 | void onUidProcAdjChanged(uid_t uid, int32_t adj) override; |
Girish | 4e531c8 | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 78 | |
| 79 | // IServiceManager::LocalRegistrationCallback implementation. |
| 80 | void onServiceRegistration(const String16& name, |
| 81 | const sp<IBinder>& binder) override; |
| 82 | |
| 83 | // IBinder::DeathRecipient implementation. |
| 84 | void binderDied(const wp<IBinder> &who) override; |
| 85 | |
| 86 | // Registers with Application Manager for UID gone event |
| 87 | // to track the termination of Applications. |
| 88 | void registerWithActivityManager(); |
| 89 | |
| 90 | /* |
| 91 | * For a list of input pids, it will check whether the corresponding |
| 92 | * processes are already terminated or not. |
| 93 | * |
| 94 | * @param[in] pids List of pids to check whether they are terminated. |
| 95 | * @param[out] terminatedPids List of pid of terminated processes. |
| 96 | * |
| 97 | * Upon return, terminatedPids returns list of all the termibated pids |
| 98 | * that will be a subset of input pids (in that order). |
| 99 | * If none of the input pids have terminated, terminatedPids will be empty. |
| 100 | */ |
| 101 | void getTerminatedProcesses(const std::vector<int32_t>& pids, |
| 102 | std::vector<int32_t>& terminatedPids); |
| 103 | |
| 104 | bool mRegistered = false; |
| 105 | std::mutex mLock; |
| 106 | ActivityManager mAm; |
| 107 | // map of UID and all the PIDs associated with it |
| 108 | // as one UID could have multiple PIDs. |
| 109 | std::map<uid_t, std::set<int32_t>> mUids; |
| 110 | OnProcessTerminated mOnProcessTerminated; |
| 111 | sp<ProcessInfoInterface> mProcessInfo; |
| 112 | }; |
| 113 | |
| 114 | } // namespace android |
| 115 | |
| 116 | #endif //ANDROID_MEDIA_UIDOBSERVER_H_ |