1 /** 2 * Copyright (c) 2020, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CPP_WATCHDOG_SERVER_SRC_WATCHDOGSERVICEHELPER_H_ 18 #define CPP_WATCHDOG_SERVER_SRC_WATCHDOGSERVICEHELPER_H_ 19 20 #include "WatchdogProcessService.h" 21 22 #include <android-base/result.h> 23 #include <android/automotive/watchdog/TimeoutLength.h> 24 #include <android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.h> 25 #include <android/automotive/watchdog/internal/PackageInfo.h> 26 #include <android/automotive/watchdog/internal/PackageIoOveruseStats.h> 27 #include <binder/IBinder.h> 28 #include <binder/Status.h> 29 #include <gtest/gtest_prod.h> 30 #include <utils/Mutex.h> 31 #include <utils/StrongPointer.h> 32 33 #include <shared_mutex> 34 35 namespace android { 36 namespace automotive { 37 namespace watchdog { 38 39 class ServiceManager; 40 41 // Forward declaration for testing use only. 42 namespace internal { 43 44 class WatchdogServiceHelperPeer; 45 46 } // namespace internal 47 48 class WatchdogServiceHelperInterface : public android::IBinder::DeathRecipient { 49 public: 50 virtual android::binder::Status registerService( 51 const android::sp< 52 android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 53 service) = 0; 54 virtual android::binder::Status unregisterService( 55 const android::sp< 56 android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 57 service) = 0; 58 59 // Helper methods for APIs in ICarWatchdogServiceForSystem.aidl. 60 virtual android::binder::Status checkIfAlive(const android::wp<android::IBinder>& who, 61 int32_t sessionId, 62 TimeoutLength timeout) const = 0; 63 virtual android::binder::Status prepareProcessTermination( 64 const android::wp<android::IBinder>& who) = 0; 65 virtual android::binder::Status getPackageInfosForUids( 66 const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes, 67 std::vector<android::automotive::watchdog::internal::PackageInfo>* packageInfos) = 0; 68 virtual android::binder::Status latestIoOveruseStats( 69 const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>& 70 packageIoOveruseStats) = 0; 71 virtual android::binder::Status resetResourceOveruseStats( 72 const std::vector<std::string>& packageNames) = 0; 73 virtual android::binder::Status getTodayIoUsageStats( 74 std::vector<android::automotive::watchdog::internal::UserPackageIoUsageStats>* 75 userPackageIoUsageStats) = 0; 76 77 protected: 78 virtual android::base::Result<void> init( 79 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService) = 0; 80 virtual void terminate() = 0; 81 82 private: 83 friend class ServiceManager; 84 }; 85 86 // WatchdogServiceHelper implements the helper functions for the outbound API requests to 87 // the CarWatchdogService. This class doesn't handle the inbound APIs requests from 88 // CarWatchdogService except the registration APIs. 89 class WatchdogServiceHelper final : public WatchdogServiceHelperInterface { 90 public: WatchdogServiceHelper()91 WatchdogServiceHelper() : mWatchdogProcessService(nullptr), mService(nullptr) {} 92 93 android::binder::Status registerService( 94 const android::sp< 95 android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service) 96 override; 97 android::binder::Status unregisterService( 98 const android::sp< 99 android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service) 100 override; 101 void binderDied(const android::wp<android::IBinder>& who) override; 102 103 // Helper methods for ICarWatchdogServiceForSystem.aidl. 104 android::binder::Status checkIfAlive(const android::wp<android::IBinder>& who, 105 int32_t sessionId, TimeoutLength timeout) const override; 106 android::binder::Status prepareProcessTermination( 107 const android::wp<android::IBinder>& who) override; 108 android::binder::Status getPackageInfosForUids( 109 const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes, 110 std::vector<android::automotive::watchdog::internal::PackageInfo>* packageInfos); 111 android::binder::Status latestIoOveruseStats( 112 const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>& 113 packageIoOveruseStats); 114 android::binder::Status resetResourceOveruseStats(const std::vector<std::string>& packageNames); 115 android::binder::Status getTodayIoUsageStats( 116 std::vector<android::automotive::watchdog::internal::UserPackageIoUsageStats>* 117 userPackageIoUsageStats); 118 119 protected: 120 android::base::Result<void> init( 121 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService); 122 void terminate(); 123 124 private: 125 void unregisterServiceLocked(); 126 127 android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService; 128 129 mutable std::shared_mutex mRWMutex; 130 android::sp<android::automotive::watchdog::internal::ICarWatchdogServiceForSystem> mService 131 GUARDED_BY(mRWMutex); 132 133 friend class ServiceManager; 134 135 // For unit tests. 136 friend class internal::WatchdogServiceHelperPeer; 137 FRIEND_TEST(WatchdogServiceHelperTest, TestInit); 138 FRIEND_TEST(WatchdogServiceHelperTest, 139 TestErrorOnInitWithErrorFromWatchdogProcessServiceRegistration); 140 FRIEND_TEST(WatchdogServiceHelperTest, TestErrorOnInitWithNullWatchdogProcessServiceInstance); 141 FRIEND_TEST(WatchdogServiceHelperTest, TestTerminate); 142 }; 143 144 } // namespace watchdog 145 } // namespace automotive 146 } // namespace android 147 148 #endif // CPP_WATCHDOG_SERVER_SRC_WATCHDOGSERVICEHELPER_H_ 149