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 IWatchdogServiceHelper : 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<WatchdogProcessService>& 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 IWatchdogServiceHelper {
90 public:
WatchdogServiceHelper()91     WatchdogServiceHelper() : mService(nullptr), mWatchdogProcessService(nullptr) {}
92     ~WatchdogServiceHelper();
93 
94     android::binder::Status registerService(
95             const android::sp<
96                     android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service)
97             override;
98     android::binder::Status unregisterService(
99             const android::sp<
100                     android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service)
101             override;
102     void binderDied(const android::wp<android::IBinder>& who) override;
103 
104     // Helper methods for ICarWatchdogServiceForSystem.aidl.
105     android::binder::Status checkIfAlive(const android::wp<android::IBinder>& who,
106                                          int32_t sessionId, TimeoutLength timeout) const override;
107     android::binder::Status prepareProcessTermination(
108             const android::wp<android::IBinder>& who) override;
109     android::binder::Status getPackageInfosForUids(
110             const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes,
111             std::vector<android::automotive::watchdog::internal::PackageInfo>* packageInfos);
112     android::binder::Status latestIoOveruseStats(
113             const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>&
114                     packageIoOveruseStats);
115     android::binder::Status resetResourceOveruseStats(const std::vector<std::string>& packageNames);
116     android::binder::Status getTodayIoUsageStats(
117             std::vector<android::automotive::watchdog::internal::UserPackageIoUsageStats>*
118                     userPackageIoUsageStats);
119 
120 protected:
121     android::base::Result<void> init(
122             const android::sp<WatchdogProcessService>& watchdogProcessService);
123     void terminate();
124 
125 private:
126     void unregisterServiceLocked();
127 
128     mutable std::shared_mutex mRWMutex;
129     android::sp<android::automotive::watchdog::internal::ICarWatchdogServiceForSystem> mService
130             GUARDED_BY(mRWMutex);
131     android::sp<WatchdogProcessService> mWatchdogProcessService;
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