1 // Copyright (C) 2020 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_ 16 #define IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_ 17 18 #include "binder/package_change_observer.h" 19 20 #include <android/content/pm/IPackageManagerNative.h> 21 #include <binder/IServiceManager.h> 22 23 #include <optional> 24 #include <unordered_map> 25 26 namespace iorap::binder { 27 28 using IPackageManager = android::content::pm::IPackageManagerNative; 29 // A map between package name and its version. 30 using VersionMap = std::unordered_map<std::string, int64_t>; 31 32 class PackageManagerRemote; 33 34 class PackageManagerDeathRecipient : public android::IBinder::DeathRecipient { 35 public: PackageManagerDeathRecipient(std::shared_ptr<PackageManagerRemote> package_manager,android::sp<PackageChangeObserver> observer)36 PackageManagerDeathRecipient(std::shared_ptr<PackageManagerRemote> package_manager, 37 android::sp<PackageChangeObserver> observer) : 38 package_manager_(package_manager), observer_(observer) {} 39 // android::IBinder::DeathRecipient override: 40 void binderDied(const android::wp<android::IBinder>& /* who */) override; 41 42 private: 43 std::shared_ptr<PackageManagerRemote> package_manager_; 44 45 android::sp<PackageChangeObserver> observer_; 46 }; 47 48 class PackageManagerRemote { 49 public: 50 static std::shared_ptr<PackageManagerRemote> Create(); 51 52 // Gets the package version based on the package name. 53 std::optional<int64_t> GetPackageVersion(const std::string& package_name); 54 55 // Gets a map of package name and its version. 56 std::optional<VersionMap> GetPackageVersionMap(); 57 58 void RegisterPackageChangeObserver(android::sp<PackageChangeObserver> observer); 59 60 void UnregisterPackageChangeObserver(android::sp<PackageChangeObserver> observer); 61 62 void RegisterPackageManagerDeathRecipient( 63 android::sp<PackageManagerDeathRecipient> death_recipient); 64 65 private: 66 template <typename T> 67 android::binder::Status InvokeRemote(T&& lambda); 68 69 // Reconnects to the package manager service. 70 // Retry until timeout. 71 bool ReconnectWithTimeout(int64_t timeout_ms); 72 73 // Gets the package manager service. 74 static android::sp<IPackageManager> GetPackageService(); 75 76 // Gets all package names. 77 std::optional<std::vector<std::string>> GetAllPackages(); 78 79 android::sp<IPackageManager> package_service_; 80 }; 81 } // namespace iorap::package_manager 82 83 #endif // IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_ 84