• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "nlohmann/json.hpp"
37 
38 namespace OHOS {
39 namespace ResourceSchedule {
40 class ResSchedSvcStatusChange : public SystemAbilityStatusChangeStub {
41 public:
42     ResSchedSvcStatusChange() = default;
43     ~ResSchedSvcStatusChange() = default;
44     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
45     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
46 };
47 /*
48  * this class wraped the functions of IResSchedService,effect is the same.
49  * but through ResSchedClient, you don't need to get IResSchedService from samgr,
50  * just use the functions is ok.
51  */
52 class ResSchedClient {
53     friend ResSchedSvcStatusChange;
54 public:
55     /**
56      * @brief Get the Instance object.
57      *
58      * @return Returns ResSchedClient&.
59      */
60     static ResSchedClient& GetInstance();
61 
62     /**
63      * @brief Report resource data to the resource schedule service through inter-process communication.
64      *
65      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
66      * @param value Indicates the value of the resource type, defined by the developers.
67      * @param mapPayload Indicates the context info of the resource type event.
68      */
69     void ReportData(uint32_t resType, int64_t value, const std::unordered_map<std::string, std::string>& mapPayload);
70 
71     /**
72      * @brief Report the synchronization event to the resource schedule service.
73      *
74      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
75      * @param value Indicates the value of the resource type, defined by the developers.
76      * @param payload Indicates the context info of the resource type event.
77      * @param reply Indicates the return value of service processing.
78      */
79     int32_t ReportSyncEvent(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
80         nlohmann::json& reply);
81 
82     /**
83      * @brief Report the synchronization event to the resource schedule service.
84      *
85      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
86      * @param value Indicates the value of the resource type, defined by the developers.
87      * @param payload Indicates the context info of the resource type event.
88      * @param reply Indicates the return value of service processing.
89      */
90     int32_t ReportSyncEvent(const uint32_t resType, const int64_t value,
91         const std::unordered_map<std::string, std::string>& payload,
92         std::unordered_map<std::string, std::string>& reply);
93 
94     /**
95      * @brief Kill process with pid.
96      *
97      * @param mapPayload Indicates the context info of the kill message.
98      */
99     int32_t KillProcess(const std::unordered_map<std::string, std::string>& mapPayload);
100 
101     /**
102      * @brief Stop remote Object, reset ResSchedClient.
103      */
104     void StopRemoteObject();
105 
106     /**
107      * @brief Register systemload level listener.
108      *
109      * @param callbackObj systemload level listener object.
110      */
111     void RegisterSystemloadNotifier(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
112 
113     /**
114      * @brief UnRegister systemload level listener.
115      *
116      * @param callbackObj systemload level listener object
117      */
118     void UnRegisterSystemloadNotifier(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
119 
120         /**
121      * @brief Register event listener.
122      *
123      * @param eventListener event listener object.
124      * @param eventType event type.
125      */
126     void RegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
127         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
128 
129     /**
130      * @brief UnRegister event listener.
131      *
132      * @param eventListener event listener object.
133      * @param eventType event type
134      */
135     void UnRegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
136         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
137 
138     /**
139      * @brief client get systemload level.
140      */
141     int32_t GetSystemloadLevel();
142 
143     /**
144      * @brief is allowed appliacation preload through resource scheduling services.
145      *
146      * @param bundleName bundleName of the application.
147      */
148     bool IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode);
149 
150     /**
151      * @brief Check whether the application is in the link jump white list.
152      * Calling from application process.
153      *
154      * @param isAllowedLinkJump true if the application is in the link jump white list.
155      */
156     int32_t IsAllowedLinkJump(bool& isAllowedLinkJump);
157 
158 protected:
159     ResSchedClient() = default;
160     virtual ~ResSchedClient();
161 
162 private:
163     void AddResSaListenerLocked();
164     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
165     int32_t InitSystemloadListenersLocked();
166     int32_t InitInnerEventListenerLocked();
167     void UnRegisterSystemloadListenersLocked();
168     void UnRegisterEventListenerLocked(uint32_t eventType, uint32_t listenerGroup);
169     int32_t ReportMutexBeforeStartEvent(const uint32_t resType, const int64_t value,
170         const nlohmann::json& payload, nlohmann::json& reply, sptr<IResSchedService>& proxy);
171     void RecoverEventListener();
172     class SystemloadLevelListener : public ResSchedSystemloadNotifierStub {
173     public:
174         SystemloadLevelListener() = default;
175         virtual ~SystemloadLevelListener();
176         void RegisterSystemloadLevelCb(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
177         void UnRegisterSystemloadLevelCb(const sptr<ResSchedSystemloadNotifierClient>& callbackObj);
178         bool IsSystemloadCbArrayEmpty();
179         ErrCode OnSystemloadLevel(int32_t level) override;
180     private:
181         std::mutex listMutex_;
182         std::list<sptr<ResSchedSystemloadNotifierClient>> systemloadLevelCbs_;
183     };
184     class InnerEventListener : public ResSchedEventListenerStub {
185     public:
186         InnerEventListener() = default;
187         virtual ~InnerEventListener();
188         void RegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
189             uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
190         void UnRegisterEventListener(const sptr<ResSchedEventListener>& eventListener, uint32_t eventType,
191             uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
192         ErrCode OnReceiveEvent(uint32_t eventType, uint32_t eventValue, uint32_t listenerGroup,
193             const std::string& extInfo) override;
194         bool IsInnerEventMapEmpty(uint32_t eventType, uint32_t listenerGroup);
195         std::unordered_map<uint32_t, std::list<uint32_t>> GetRegisteredTypesAndGroup();
196     private:
197         std::mutex eventMutex_;
198         std::unordered_map<uint32_t, std::unordered_map<uint32_t,
199             std::list<sptr<ResSchedEventListener>>>> eventListeners_;
200     };
201     class ResSchedDeathRecipient : public IRemoteObject::DeathRecipient {
202     public:
203         explicit ResSchedDeathRecipient(ResSchedClient &resSchedClient);
204 
205         ~ResSchedDeathRecipient();
206 
207         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
208 
209     private:
210         ResSchedClient &resSchedClient_;
211     };
212     sptr<IResSchedService> GetProxy();
213     ErrCode TryConnect();
214     std::mutex mutex_;
215     sptr<ResSchedDeathRecipient> recipient_;
216     sptr<IRemoteObject> remoteObject_;
217     sptr<IResSchedService> rss_;
218     sptr<SystemloadLevelListener> systemloadLevelListener_;
219     sptr<InnerEventListener> innerEventListener_;
220     sptr<ResSchedSvcStatusChange> resSchedSvcStatusListener_;
221     bool systemloadCbRegistered_ = false;
222     bool isGetResTypeList_ = false;
223     std::set<uint32_t> resTypeList_;
224     std::unordered_map<uint32_t, std::unordered_set<uint32_t>> registeredInnerEvents;
225     DISALLOW_COPY_AND_MOVE(ResSchedClient);
226 };
227 } // namespace ResourceSchedule
228 } // namespace OHOS
229 
230 #endif // RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_CLIENT_H
231