1 /*
2  * Copyright (c) 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_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H
17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 
23 #include "ffrt.h"
24 #include "iremote_object.h"
25 #include "refbase.h"
26 #include "res_type.h"
27 #include "single_instance.h"
28 
29 namespace OHOS {
30 namespace ResourceSchedule {
31 class NotifierMgr {
32     DECLARE_SINGLE_INSTANCE_BASE(NotifierMgr);
33 public:
34     struct NotifierInfo {
35         sptr<IRemoteObject> notifier = nullptr;
36         bool foreground = true;
37         bool hapApp = false;
38         int32_t level = ResType::SystemloadLevel::LOW;
39     };
40     using NotifierMap = std::map<int32_t, NotifierInfo>;
41     void Init();
42     void Deinit();
43     void RegisterNotifier(int32_t pid, const sptr<IRemoteObject>& notifier);
44     void UnRegisterNotifier(int32_t pid);
45     void OnRemoteNotifierDied(const sptr<IRemoteObject>& notifier);
46     void OnDeviceLevelChanged(int32_t type, int32_t level);
47     void OnApplicationStateChange(int32_t state, int32_t pid);
48     int32_t GetSystemloadLevel();
49     std::vector<std::pair<int32_t, bool>> DumpRegisterInfo();
50 private:
51     NotifierMgr() = default;
52     ~NotifierMgr();
53     void RemoveNotifierLock(const sptr<IRemoteObject>& notifier);
54     void OnDeviceLevelChangedLock(int32_t level);
55     void HandleDeviceLevelChange(std::vector<sptr<IRemoteObject>>& notifierVec, int32_t level);
56     bool IsHapApp();
57 
58     bool initialized_ = false;
59     std::mutex notifierMutex_;
60     NotifierMap notifierMap_;
61     ResType::SystemloadLevel systemloadLevel_ = ResType::SystemloadLevel::LOW;
62     sptr<IRemoteObject::DeathRecipient> notifierDeathRecipient_ = nullptr;
63     std::shared_ptr<ffrt::queue> notifierHandler_ = nullptr;
64 };
65 } // ResourceSchedule
66 } // OHOS
67 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H