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