1 /*
2  * Copyright (c) 2022 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 CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SCHED_CONTROLLER_H_
17 #define CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SCHED_CONTROLLER_H_
18 
19 #include <sys/types.h>
20 #include <string>
21 #include "hilog/log.h"
22 #include "nlohmann/json.hpp"
23 #include "refbase.h"
24 
25 namespace OHOS {
26 namespace ResourceSchedule {
27 class RmsApplicationStateObserver;
28 #ifdef CONFIG_BGTASK_MGR
29 class BackgroundTaskObserver;
30 #endif
31 class WindowStateObserver;
32 class WindowVisibilityObserver;
33 class WindowUpdateStateObserver;
34 class WindowDrawingContentObserver;
35 class WindowModeObserver;
36 class Supervisor;
37 class CgroupAdjuster;
38 class CgroupEventHandler;
39 
40 class SchedController {
41 public:
42     static SchedController& GetInstance();
43 
44     void Init();
45     void Deinit();
46     bool SubscribeAppState();
47     void UnsubscribeAppState();
48     bool SubscribeBackgroundTask();
49     void UnsubscribeBackgroundTask();
50     void SubscribeWindowState();
51     void SubscribeWindowModeChange();
52     void UnsubscribeWindowState();
53     void UnsubscribeWindowModeChange();
54     void UnregisterStateObservers();
55     int GetProcessGroup(pid_t pid);
56     void ReportAbilityStatus(int32_t saId, const std::string& deviceId, uint32_t status);
57     void DispatchResource(uint32_t resType, int64_t value, const nlohmann::json& payload);
58     void DispatchOtherResource(uint32_t resType, int64_t value, const nlohmann::json& payload);
59     std::string GetBundleNameByUid(const int32_t uid);
60 #ifdef POWER_MANAGER_ENABLE
61     void GetRunningLockState();
62 #endif
63 
GetCgroupEventHandler()64     const inline std::shared_ptr<CgroupEventHandler> GetCgroupEventHandler() const
65     {
66         return cgHandler_;
67     }
68 
GetSupervisor()69     const inline std::shared_ptr<Supervisor> GetSupervisor() const
70     {
71         return supervisor_;
72     }
73 
74 private:
75     SchedController() = default;
76     ~SchedController() = default;
77 
78     SchedController(const SchedController&) = delete;
79     SchedController& operator=(const SchedController &) = delete;
80     SchedController(SchedController&&) = delete;
81     SchedController& operator=(SchedController&&) = delete;
82 
83     std::shared_ptr<CgroupEventHandler> cgHandler_;
84     std::shared_ptr<Supervisor> supervisor_;
85     sptr<RmsApplicationStateObserver> appStateObserver_;
86 #ifdef CONFIG_BGTASK_MGR
87     bool isBgtaskSubscribed_ {false};
88     std::shared_ptr<BackgroundTaskObserver> backgroundTaskObserver_;
89 #endif
90     sptr<WindowStateObserver> windowStateObserver_;
91     sptr<WindowVisibilityObserver> windowVisibilityObserver_;
92     sptr<WindowDrawingContentObserver> windowDrawingContentObserver_;
93     std::unordered_map<uint32_t, std::function<void(std::shared_ptr<CgroupEventHandler>,
94         uint32_t, int64_t, const nlohmann::json&)>> dispatchResFuncMap_;
95     sptr<WindowModeObserver> windowModeObserver_;
96 
97     inline void InitCgroupHandler();
98     inline void InitCgroupAdjuster();
99     inline void InitSupervisor();
100     inline void InitAppStartupSceneRec();
101     inline void DeinitAppStartupSceneRec();
102     void InitDispatchResFuncMap();
103 };
104 } // namespace ResourceSchedule
105 } // namespace OHOS
106 #endif // CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SCHED_CONTROLLER_H_
107