1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_CLIENT_H
17 #define RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_CLIENT_H
18 
19 #include <cstdint>                                  // for int64_t, uint32_t
20 #include <unordered_map>                            // for unordered_map
21 #include <unordered_set>                              // for unordered_set
22 #include <mutex>                                    // for mutex
23 #include <iosfwd>                                   // for string
24 #include <list>                                     // for list
25 #include "errors.h"                                 // for ErrCode
26 #include "iremote_object.h"                         // for IRemoteObject, IRemoteObject::DeathR...
27 #include "ires_sched_service.h"                     // for IResSchedService
28 #include "nocopyable.h"                             // for DISALLOW_COPY_AND_MOVE
29 #include "refbase.h"                                // for sptr, wptr
30 #include "res_sched_systemload_notifier_client.h"   // for ResSchedSystemloadNotifierClient
31 #include "res_sched_systemload_notifier_stub.h"     // for ResSchedSystemloadNotifierStub
32 #include "system_ability_status_change_stub.h"      // for SystemAbilityStatusChangeStub
33 #include "res_sched_event_listener.h"               // for ResSchedEvenetListener
34 #include "res_sched_event_listener_stub.h"          // for ResSchedEvenetListenerStub
35 #include "res_type.h"                               // for ResType
36 
37 namespace OHOS {
38 namespace ResourceSchedule {
39 class ResSchedSvcStatusChange : public SystemAbilityStatusChangeStub {
40 public:
41     ResSchedSvcStatusChange() = default;
42     ~ResSchedSvcStatusChange() = default;
43     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
44     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
45 };
46 /*
47  * this class wraped the functions of IResSchedService,effect is the same.
48  * but through ResSchedClient, you don't need to get IResSchedService from samgr,
49  * just use the functions is ok.
50  */
51 class ResSchedClient {
52     friend ResSchedSvcStatusChange;
53 public:
54     /**
55      * @brief Get the Instance object.
56      *
57      * @return Returns ResSchedClient&.
58      */
59     static ResSchedClient& GetInstance();
60 
61     /**
62      * @brief Report resource data to the resource schedule service through inter-process communication.
63      *
64      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
65      * @param value Indicates the value of the resource type, defined by the developers.
66      * @param mapPayload Indicates the context info of the resource type event.
67      */
68     void ReportData(uint32_t resType, int64_t value, const std::unordered_map<std::string, std::string>& mapPayload);
69 
70     /**
71      * @brief Report the synchronization event to the resource schedule service.
72      *
73      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
74      * @param value Indicates the value of the resource type, defined by the developers.
75      * @param payload Indicates the context info of the resource type event.
76      * @param reply Indicates the return value of service processing.
77      */
78     int32_t ReportSyncEvent(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
79         nlohmann::json& reply);
80 
81     /**
82      * @brief Kill process with pid.
83      *
84      * @param mapPayload Indicates the context info of the kill message.
85      */
86     int32_t KillProcess(const std::unordered_map<std::string, std::string>& mapPayload);
87 
88     /**
89      * @brief Stop remote Object, reset ResSchedClient.
90      */
91     void StopRemoteObject();
92 
93     /**
94      * @brief Register systemload level listener.
95      *
96      * @param callbackObj systemload level listener object.
97      */
98     void RegisterSystemloadNotifier(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
99 
100     /**
101      * @brief UnRegister systemload level listener.
102      *
103      * @param callbackObj systemload level listener object
104      */
105     void UnRegisterSystemloadNotifier(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
106 
107         /**
108      * @brief Register event listener.
109      *
110      * @param eventListener event listener object.
111      * @param eventType event type.
112      */
113     void RegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
114         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
115 
116     /**
117      * @brief UnRegister event listener.
118      *
119      * @param eventListener event listener object.
120      * @param eventType event type
121      */
122     void UnRegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
123         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
124 
125     /**
126      * @brief client get systemload level.
127      */
128     int32_t GetSystemloadLevel();
129 
130     /**
131      * @brief is allowed appliacation preload through resource scheduling services.
132      *
133      * @param bundleName bundleName of the application.
134      */
135     bool IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode);
136 
137 protected:
138     ResSchedClient() = default;
139     virtual ~ResSchedClient();
140 
141 private:
142     void AddResSaListenerLocked();
143     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
144     int32_t InitSystemloadListenersLocked();
145     int32_t InitInnerEventListenerLocked();
146     void UnRegisterSystemloadListenersLocked();
147     void UnRegisterEventListenerLocked(uint32_t eventType, uint32_t listenerGroup);
148     void RecoverEventListener();
149     class SystemloadLevelListener : public ResSchedSystemloadNotifierStub {
150     public:
151         SystemloadLevelListener() = default;
152         virtual ~SystemloadLevelListener();
153         void RegisterSystemloadLevelCb(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
154         void UnRegisterSystemloadLevelCb(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
155         bool IsSystemloadCbArrayEmpty();
156         void OnSystemloadLevel(int32_t level) override;
157     private:
158         std::mutex listMutex_;
159         std::list<sptr<ResSchedSystemloadNotifierClient>> systemloadLevelCbs_;
160     };
161     class InnerEventListener : public ResSchedEventListenerStub {
162     public:
163         InnerEventListener() = default;
164         virtual ~InnerEventListener();
165         void RegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
166             uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
167         void UnRegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
168             uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
169         void OnReceiveEvent(uint32_t eventType, uint32_t eventValue, uint32_t listenerGroup,
170             const nlohmann::json& extInfo) override;
171         bool IsInnerEventMapEmpty(uint32_t eventType, uint32_t listenerGroup);
172         std::unordered_map<uint32_t, std::list<uint32_t>> GetRegisteredTypesAndGroup();
173     private:
174         std::mutex eventMutex_;
175         std::unordered_map<uint32_t, std::unordered_map<uint32_t,
176             std::list<sptr<ResSchedEventListener>>>> eventListeners_;
177     };
178     class ResSchedDeathRecipient : public IRemoteObject::DeathRecipient {
179     public:
180         explicit ResSchedDeathRecipient(ResSchedClient &resSchedClient);
181 
182         ~ResSchedDeathRecipient();
183 
184         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
185 
186     private:
187         ResSchedClient &resSchedClient_;
188     };
189     sptr<IResSchedService> GetProxy();
190     ErrCode TryConnect();
191     std::mutex mutex_;
192     sptr<ResSchedDeathRecipient> recipient_;
193     sptr<IRemoteObject> remoteObject_;
194     sptr<IResSchedService> rss_;
195     sptr<SystemloadLevelListener> systemloadLevelListener_;
196     sptr<InnerEventListener> innerEventListener_;
197     sptr<ResSchedSvcStatusChange> resSchedSvcStatusListener_;
198     bool systemloadCbRegistered_ = false;
199     std::unordered_map<uint32_t, std::unordered_set<uint32_t>> registeredInnerEvents;
200     DISALLOW_COPY_AND_MOVE(ResSchedClient);
201 };
202 } // namespace ResourceSchedule
203 } // namespace OHOS
204 
205 #endif // RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_CLIENT_H
206