// Copyright (C) 2020 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_ #define IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_ #include "binder/package_change_observer.h" #include #include #include #include namespace iorap::binder { using IPackageManager = android::content::pm::IPackageManagerNative; // A map between package name and its version. using VersionMap = std::unordered_map; class PackageManagerRemote; class PackageManagerDeathRecipient : public android::IBinder::DeathRecipient { public: PackageManagerDeathRecipient(std::shared_ptr package_manager, android::sp observer) : package_manager_(package_manager), observer_(observer) {} // android::IBinder::DeathRecipient override: void binderDied(const android::wp& /* who */) override; private: std::shared_ptr package_manager_; android::sp observer_; }; class PackageManagerRemote { public: static std::shared_ptr Create(); // Gets the package version based on the package name. std::optional GetPackageVersion(const std::string& package_name); // Gets a map of package name and its version. std::optional GetPackageVersionMap(); void RegisterPackageChangeObserver(android::sp observer); void UnregisterPackageChangeObserver(android::sp observer); void RegisterPackageManagerDeathRecipient( android::sp death_recipient); private: template android::binder::Status InvokeRemote(T&& lambda); // Reconnects to the package manager service. // Retry until timeout. bool ReconnectWithTimeout(int64_t timeout_ms); // Gets the package manager service. static android::sp GetPackageService(); // Gets all package names. std::optional> GetAllPackages(); android::sp package_service_; }; } // namespace iorap::package_manager #endif // IORAP_SRC_PACKAGE_MANAGER_REMOTE_H_