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 #pragma once 18 19 #include "AIBinderDeathRegistrationWrapper.h" 20 #include "WatchdogProcessService.h" 21 22 #include <aidl/android/automotive/watchdog/TimeoutLength.h> 23 #include <aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.h> 24 #include <aidl/android/automotive/watchdog/internal/PackageInfo.h> 25 #include <aidl/android/automotive/watchdog/internal/PackageIoOveruseStats.h> 26 #include <aidl/android/automotive/watchdog/internal/ResourceStats.h> 27 #include <aidl/android/automotive/watchdog/internal/UserPackageIoUsageStats.h> 28 #include <android-base/result.h> 29 #include <android/binder_auto_utils.h> 30 #include <gtest/gtest_prod.h> 31 #include <utils/Mutex.h> 32 #include <utils/StrongPointer.h> 33 34 #include <shared_mutex> 35 36 namespace android { 37 namespace automotive { 38 namespace watchdog { 39 40 class ServiceManager; 41 42 // Forward declaration for testing use only. 43 namespace internal { 44 45 class WatchdogServiceHelperPeer; 46 47 } // namespace internal 48 49 class WatchdogServiceHelperInterface : virtual public android::RefBase { 50 public: 51 virtual bool isServiceConnected() = 0; 52 virtual ndk::ScopedAStatus registerService( 53 const std::shared_ptr< 54 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 55 service) = 0; 56 virtual ndk::ScopedAStatus unregisterService( 57 const std::shared_ptr< 58 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 59 service) = 0; 60 virtual void handleBinderDeath(void* cookie) = 0; 61 62 // Helper methods for APIs in ICarWatchdogServiceForSystem.aidl. 63 virtual ndk::ScopedAStatus checkIfAlive( 64 const ndk::SpAIBinder& who, int32_t sessionId, 65 aidl::android::automotive::watchdog::TimeoutLength timeout) const = 0; 66 virtual ndk::ScopedAStatus prepareProcessTermination(const ndk::SpAIBinder& who) = 0; 67 virtual ndk::ScopedAStatus getPackageInfosForUids( 68 const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes, 69 std::vector<aidl::android::automotive::watchdog::internal::PackageInfo>* packageInfos) 70 const = 0; 71 virtual ndk::ScopedAStatus resetResourceOveruseStats( 72 const std::vector<std::string>& packageNames) const = 0; 73 virtual ndk::ScopedAStatus onLatestResourceStats( 74 const std::vector<aidl::android::automotive::watchdog::internal::ResourceStats>& 75 resourceStats) const = 0; 76 virtual ndk::ScopedAStatus requestAidlVhalPid() const = 0; 77 virtual ndk::ScopedAStatus requestTodayIoUsageStats() const = 0; 78 79 protected: 80 virtual android::base::Result<void> init( 81 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService) = 0; 82 virtual void terminate() = 0; 83 84 private: 85 friend class ServiceManager; 86 }; 87 88 // WatchdogServiceHelper implements the helper functions for the outbound API requests to 89 // the CarWatchdogService. This class doesn't handle the inbound APIs requests from 90 // CarWatchdogService except the registration APIs. 91 class WatchdogServiceHelper final : public WatchdogServiceHelperInterface { 92 public: 93 WatchdogServiceHelper(); 94 isServiceConnected()95 bool isServiceConnected() { 96 std::shared_lock readLock(mRWMutex); 97 return mService != nullptr; 98 } 99 ndk::ScopedAStatus registerService( 100 const std::shared_ptr< 101 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 102 service) override; 103 ndk::ScopedAStatus unregisterService( 104 const std::shared_ptr< 105 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 106 service) override; 107 void handleBinderDeath(void* cookie) override; 108 109 // Helper methods for ICarWatchdogServiceForSystem.aidl. 110 ndk::ScopedAStatus checkIfAlive( 111 const ndk::SpAIBinder& who, int32_t sessionId, 112 aidl::android::automotive::watchdog::TimeoutLength timeout) const override; 113 ndk::ScopedAStatus prepareProcessTermination(const ndk::SpAIBinder& who) override; 114 ndk::ScopedAStatus getPackageInfosForUids( 115 const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes, 116 std::vector<aidl::android::automotive::watchdog::internal::PackageInfo>* packageInfos) 117 const override; 118 ndk::ScopedAStatus resetResourceOveruseStats( 119 const std::vector<std::string>& packageNames) const override; 120 ndk::ScopedAStatus onLatestResourceStats( 121 const std::vector<aidl::android::automotive::watchdog::internal::ResourceStats>& 122 resourceStats) const override; 123 ndk::ScopedAStatus requestAidlVhalPid() const override; 124 ndk::ScopedAStatus requestTodayIoUsageStats() const override; 125 126 protected: 127 android::base::Result<void> init( 128 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService); 129 void terminate(); 130 131 private: 132 void unregisterServiceLocked(bool doUnregisterFromProcessService); 133 134 android::sp<WatchdogProcessServiceInterface> mWatchdogProcessService; 135 ndk::ScopedAIBinder_DeathRecipient mWatchdogServiceDeathRecipient; 136 android::sp<AIBinderDeathRegistrationWrapperInterface> mDeathRegistrationWrapper; 137 138 mutable std::shared_mutex mRWMutex; 139 std::shared_ptr<aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem> 140 mService GUARDED_BY(mRWMutex); 141 142 friend class ServiceManager; 143 144 // For unit tests. 145 friend class internal::WatchdogServiceHelperPeer; 146 FRIEND_TEST(WatchdogServiceHelperTest, TestInit); 147 FRIEND_TEST(WatchdogServiceHelperTest, 148 TestErrorOnInitWithErrorFromWatchdogProcessServiceRegistration); 149 FRIEND_TEST(WatchdogServiceHelperTest, TestErrorOnInitWithNullWatchdogProcessServiceInstance); 150 FRIEND_TEST(WatchdogServiceHelperTest, TestTerminate); 151 }; 152 153 } // namespace watchdog 154 } // namespace automotive 155 } // namespace android 156