• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
74 protected:
75     virtual android::base::Result<void> init(
76             const android::sp<WatchdogProcessService>& watchdogProcessService) = 0;
77     virtual void terminate() = 0;
78 
79 private:
80     friend class ServiceManager;
81 };
82 
83 // WatchdogServiceHelper implements the helper functions for the outbound API requests to
84 // the CarWatchdogService. This class doesn't handle the inbound APIs requests from
85 // CarWatchdogService except the registration APIs.
86 class WatchdogServiceHelper final : public IWatchdogServiceHelper {
87 public:
WatchdogServiceHelper()88     WatchdogServiceHelper() : mService(nullptr), mWatchdogProcessService(nullptr) {}
89     ~WatchdogServiceHelper();
90 
91     android::binder::Status registerService(
92             const android::sp<
93                     android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service)
94             override;
95     android::binder::Status unregisterService(
96             const android::sp<
97                     android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service)
98             override;
99     void binderDied(const android::wp<android::IBinder>& who) override;
100 
101     // Helper methods for ICarWatchdogServiceForSystem.aidl.
102     android::binder::Status checkIfAlive(const android::wp<android::IBinder>& who,
103                                          int32_t sessionId, TimeoutLength timeout) const override;
104     android::binder::Status prepareProcessTermination(
105             const android::wp<android::IBinder>& who) override;
106     android::binder::Status getPackageInfosForUids(
107             const std::vector<int32_t>& uids, const std::vector<std::string>& vendorPackagePrefixes,
108             std::vector<android::automotive::watchdog::internal::PackageInfo>* packageInfos);
109     android::binder::Status latestIoOveruseStats(
110             const std::vector<android::automotive::watchdog::internal::PackageIoOveruseStats>&
111                     packageIoOveruseStats);
112     android::binder::Status resetResourceOveruseStats(const std::vector<std::string>& packageNames);
113 
114 protected:
115     android::base::Result<void> init(
116             const android::sp<WatchdogProcessService>& watchdogProcessService);
117     void terminate();
118 
119 private:
120     void unregisterServiceLocked();
121 
122     mutable std::shared_mutex mRWMutex;
123     android::sp<android::automotive::watchdog::internal::ICarWatchdogServiceForSystem> mService
124             GUARDED_BY(mRWMutex);
125     android::sp<WatchdogProcessService> mWatchdogProcessService;
126 
127     friend class ServiceManager;
128 
129     // For unit tests.
130     friend class internal::WatchdogServiceHelperPeer;
131     FRIEND_TEST(WatchdogServiceHelperTest, TestInit);
132     FRIEND_TEST(WatchdogServiceHelperTest,
133                 TestErrorOnInitWithErrorFromWatchdogProcessServiceRegistration);
134     FRIEND_TEST(WatchdogServiceHelperTest, TestErrorOnInitWithNullWatchdogProcessServiceInstance);
135     FRIEND_TEST(WatchdogServiceHelperTest, TestTerminate);
136 };
137 
138 }  // namespace watchdog
139 }  // namespace automotive
140 }  // namespace android
141 
142 #endif  // CPP_WATCHDOG_SERVER_SRC_WATCHDOGSERVICEHELPER_H_
143