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_SERVICE_PROXY_H
17 #define RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_SERVICE_PROXY_H
18 
19 #include <cstdint>               // for int64_t, uint32_t
20 #include "iremote_proxy.h"       // for IRemoteProxy
21 #include "ires_sched_service.h"  // for IResSchedService
22 #include "nlohmann/json.hpp"     // for Value
23 #include "nocopyable.h"          // for DISALLOW_COPY_AND_MOVE
24 
25 namespace OHOS { class IRemoteObject; }
26 
27 namespace OHOS {
28 namespace ResourceSchedule {
29 class ResSchedServiceProxy : public IRemoteProxy<IResSchedService> {
30 public:
31     /**
32      * @brief Report resource data to the resource schedule service through inter-process communication.
33      *
34      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
35      * @param value Indicates the value of the resource type, defined by the developers.
36      * @param payload Indicates the context info of the resource type event.
37      */
38     void ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload) override;
39 
40     /**
41      * @brief Report the synchronization event to the resource schedule service.
42      *
43      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
44      * @param value Indicates the value of the resource type, defined by the developers.
45      * @param payload Indicates the context info of the resource type event.
46      * @param reply Indicates the return value of service processing.
47      */
48     int32_t ReportSyncEvent(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
49         nlohmann::json& reply) override;
50 
51     /**
52      * @brief Kill process with pid.
53      *
54      * @param payload Indicates the context info of the kill message.
55      */
56     int32_t KillProcess(const nlohmann::json& payload) override;
57 
58     /**
59      * @brief Register systemload remote listener.
60      *
61      * @param notifier remote listener object
62      */
63     void RegisterSystemloadNotifier(const sptr<IRemoteObject>& notifier) override;
64 
65     /**
66      * @brief UnRegister systemload remote listener.
67      */
68     void UnRegisterSystemloadNotifier() override;
69 
70     /**
71      * @brief client get systemload level.
72      */
73     int32_t GetSystemloadLevel() override;
74 
75     /**
76      * @brief is allowed appliacation preload through resource scheduling services.
77      *
78      * @param bundleName bundleName of the application.
79      */
80     bool IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode) override;
81 
82     /**
83      * @brief Register event listener.
84      *
85      * @param listener remote listener object
86      * @param eventType the event type
87      */
88     void RegisterEventListener(const sptr<IRemoteObject>& listener, uint32_t eventType,
89         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override;
90 
91     /**
92      * @brief UnRegister event listener.
93      *
94      * @param eventType the event type
95      */
96     void UnRegisterEventListener(uint32_t eventType,
97         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON) override;
98 public:
99     /**
100      * @brief Construct a new ResSchedServiceProxy object.
101      *
102      * @param impl RemoteObject.
103      */
ResSchedServiceProxy(const sptr<IRemoteObject> & impl)104     explicit ResSchedServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IResSchedService>(impl) {}
105 
106     /**
107      * @brief Destroy the ResSchedServiceProxy object
108      */
~ResSchedServiceProxy()109     virtual ~ResSchedServiceProxy() {}
110 
111 private:
112     int32_t WriteParcelForReportData(const uint32_t resType, const int64_t value, const nlohmann::json& payload,
113         MessageParcel& data);
114     bool StringToJson(const std::string& str, nlohmann::json& jsonObj);
115     int32_t SendRequestToRemote(const uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
116 
117 private:
118     DISALLOW_COPY_AND_MOVE(ResSchedServiceProxy);
119     static inline BrokerDelegator<ResSchedServiceProxy> delegator_;
120 };
121 } // namespace ResourceSchedule
122 } // namespace OHOS
123 
124 #endif // RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_SERVICE_PROXY_H
125