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_CGROUP_EVENT_HANDLER_H_
17 #define CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_CGROUP_EVENT_HANDLER_H_
18 
19 #include <sys/types.h>
20 #include "ffrt.h"
21 #include "ffrt_inner.h"
22 #include "nlohmann/json.hpp"
23 #include "supervisor.h"
24 #include "wm_common.h"
25 
26 namespace OHOS {
27 namespace ResourceSchedule {
28 using OHOS::Rosen::WindowType;
29 
30 class CgroupEventHandler {
31 public:
32     explicit CgroupEventHandler(const std::string &queueName);
33     ~CgroupEventHandler();
34     void ProcessEvent(uint32_t eventId, int64_t eventParam);
35     void SetSupervisor(std::shared_ptr<Supervisor> supervisor);
36     void HandleAbilityAdded(int32_t saId, const std::string& deviceId);
37     void HandleAbilityRemoved(int32_t saId, const std::string& deviceId);
38     void HandleApplicationStateChanged(uid_t uid, pid_t pid, const std::string& bundleName, int32_t state);
39     void HandleOnAppStopped(uid_t uid, const std::string& bundleName);
40     void HandleProcessStateChanged(uid_t uid, pid_t pid, const std::string& bundleName, int32_t state);
41     void HandleAbilityStateChanged(uid_t uid, pid_t pid, const std::string& bundleName,
42         const std::string& abilityName, uintptr_t token, int32_t abilityState, int32_t abilityType);
43     void HandleExtensionStateChanged(uid_t uid, pid_t pid, const std::string& bundleName,
44         const std::string& abilityName, uintptr_t token, int32_t extensionState, int32_t abilityType);
45     void HandleProcessCreated(uid_t uid, pid_t pid, int32_t hostPid, int32_t processType,
46         const std::string& bundleName, int32_t extensionType);
47     void HandleProcessDied(uid_t uid, pid_t pid, const std::string& bundleName);
48     void HandleTransientTaskStart(uid_t uid, pid_t pid, const std::string& packageName);
49     void HandleTransientTaskEnd(uid_t uid, pid_t pid, const std::string& packageName);
50     void HandleContinuousTaskCancel(uid_t uid, pid_t pid, int32_t typeId, int32_t abilityId);
51     void HandleContinuousTaskUpdate(
52         uid_t uid, pid_t pid, const std::vector<uint32_t>& typeIds, int32_t abilityId);
53     void HandleFocusedWindow(uint32_t windowId, uintptr_t abilityToken,
54         WindowType windowType, uint64_t displayId, int32_t pid, int32_t uid);
55     void HandleUnfocusedWindow(uint32_t windowId, uintptr_t abilityToken,
56         WindowType windowType, uint64_t displayId, int32_t pid, int32_t uid);
57     void HandleWindowVisibilityChanged(uint32_t windowId, uint32_t visibilityState,
58         WindowType windowType, int32_t pid, int32_t uid);
59     void HandleDrawingContentChangeWindow(uint32_t windowId, WindowType windowType,
60         bool drawingContentState, int32_t pid, int32_t uid);
61     void HandleReportMMIProcess(uint32_t resType, int64_t value, const nlohmann::json& payload);
62     void HandleReportRenderThread(uint32_t resType, int64_t value, const nlohmann::json& payload);
63     void HandleReportKeyThread(uint32_t resType, int64_t value, const nlohmann::json& payload);
64     void HandleReportWindowState(uint32_t resType, int64_t value, const nlohmann::json& payload);
65     void HandleReportAudioState(uint32_t resType, int64_t value, const nlohmann::json& payload);
66     void HandleReportWebviewAudioState(uint32_t resType, int64_t value, const nlohmann::json& payload);
67     void HandleReportRunningLockEvent(uint32_t resType, int64_t value, const nlohmann::json& payload);
68     void HandleReportHisysEvent(uint32_t resType, int64_t value, const nlohmann::json& payload);
69     void HandleReportAvCodecEvent(uint32_t resType, int64_t value, const nlohmann::json& payload);
70     void HandleSceneBoardState(uint32_t resType, int64_t value, const nlohmann::json& payload);
71     void HandleWebviewScreenCapture(uint32_t resType, int64_t value, const nlohmann::json& payload);
72     void HandleReportScreenCaptureEvent(uint32_t resType, int64_t value, const nlohmann::json& payload);
73     void HandleReportWebviewVideoState(uint32_t resType, int64_t value, const nlohmann::json& payload);
74     void PostTask(const std::function<void()> task);
75     void PostTask(const std::function<void()> task, const std::string &taskName, const int32_t delayTime);
76     void RemoveTask(const std::string &taskName);
77 
78 private:
79     bool CheckVisibilityForRenderProcess(ProcessRecord &pr, ProcessRecord &mainProc);
80     bool GetProcInfoByPayload(int32_t &uid, int32_t &pid, std::shared_ptr<Application>& app,
81         std::shared_ptr<ProcessRecord>& procRecord, const nlohmann::json& payload);
82     bool ParsePayload(int32_t& uid, int32_t& pid, const nlohmann::json& payload);
83     bool ParsePayload(int32_t& uid, int32_t& pid, int32_t& tid, int64_t value, const nlohmann::json& payload);
84     bool ParseValue(int32_t& value, const char* name, const nlohmann::json& payload);
85     void UpdateActivepWebRenderInfo(int32_t uid, int32_t pid, int32_t windowId, int32_t state,
86         const std::shared_ptr<ProcessRecord>& proc);
87     std::shared_ptr<Supervisor> supervisor_;
88     std::shared_ptr<ffrt::queue> cgroupEventQueue_;
89     std::unordered_map<std::string, ffrt::task_handle> delayTaskMap_;
90     ffrt::mutex delayTaskMapMutex_;
91     int32_t ffrtSwitch_ = 1000; // delayTime transfer to ffrt
92 };
93 } // namespace ResourceSchedule
94 } // namespace OHOS
95 
96 #endif // CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_CGROUP_EVENT_HANDLER_H_
97