• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <mutex>                 // for mutex
22 #include <iosfwd>                // for string
23 #include "errors.h"              // for ErrCode
24 #include "iremote_object.h"      // for IRemoteObject, IRemoteObject::DeathR...
25 #include "ires_sched_service.h"  // for IResSchedService
26 #include "nocopyable.h"          // for DISALLOW_COPY_AND_MOVE
27 #include "refbase.h"             // for sptr, wptr
28 
29 namespace OHOS {
30 namespace ResourceSchedule {
31 /*
32  * this class wraped the functions of IResSchedService,effect is the same.
33  * but through ResSchedClient, you don't need to get IResSchedService from samgr,
34  * just use the functions is ok.
35  */
36 class ResSchedClient {
37 public:
38     /**
39      * @brief Get the Instance object.
40      *
41      * @return Returns ResSchedClient&.
42      */
43     static ResSchedClient& GetInstance();
44 
45     /**
46      * @brief Report resource data to the resource schedule service through inter-process communication.
47      *
48      * @param resType Indicates the resource type, all of the type have listed in res_type.h.
49      * @param value Indicates the value of the resource type, defined by the developers.
50      * @param mapPayload Indicates the context info of the resource type event.
51      */
52     void ReportData(uint32_t resType, int64_t value, const std::unordered_map<std::string, std::string>& mapPayload);
53 
54     /**
55      * @brief Kill process with pid.
56      *
57      * @param mapPayload Indicates the context info of the kill message.
58      */
59     int32_t KillProcess(const std::unordered_map<std::string, std::string>& mapPayload);
60 
61     /**
62      * @brief Stop remote Object, reset ResSchedClient.
63      */
64     void StopRemoteObject();
65 
66 protected:
67     ResSchedClient() = default;
68     virtual ~ResSchedClient() = default;
69 
70 private:
71     class ResSchedDeathRecipient : public IRemoteObject::DeathRecipient {
72     public:
73         explicit ResSchedDeathRecipient(ResSchedClient &resSchedClient);
74 
75         ~ResSchedDeathRecipient();
76 
77         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
78 
79     private:
80         ResSchedClient &resSchedClient_;
81     };
82     ErrCode TryConnect();
83     std::mutex mutex_;
84     sptr<ResSchedDeathRecipient> recipient_;
85     sptr<IRemoteObject> remoteObject_;
86     sptr<IResSchedService> rss_;
87     DISALLOW_COPY_AND_MOVE(ResSchedClient);
88 };
89 } // namespace ResourceSchedule
90 } // namespace OHOS
91 
92 #endif // RESSCHED_INTERFACES_INNERKITS_RESSCHED_CLIENT_INCLUDE_RES_SCHED_CLIENT_H
93