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 #include "window_state_observer.h"
17
18 #include "cgroup_sched_log.h"
19 #include "cgroup_event_handler.h"
20 #include "res_type.h"
21 #include "sched_controller.h"
22 #include "ressched_utils.h"
23
24 namespace OHOS {
25 namespace ResourceSchedule {
OnFocused(const sptr<FocusChangeInfo> & focusChangeInfo)26 void WindowStateObserver::OnFocused(const sptr<FocusChangeInfo>& focusChangeInfo)
27 {
28 auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
29 if (cgHandler && focusChangeInfo) {
30 auto windowId = focusChangeInfo->windowId_;
31 auto token = reinterpret_cast<uintptr_t>(focusChangeInfo->abilityToken_.GetRefPtr());
32 auto windowType = focusChangeInfo->windowType_;
33 auto displayId = focusChangeInfo->displayId_;
34 auto pid = focusChangeInfo->pid_;
35 auto uid = focusChangeInfo->uid_;
36
37 cgHandler->PostTask([cgHandler, windowId, token, windowType, displayId, pid, uid] {
38 cgHandler->HandleFocusedWindow(windowId, token, windowType, displayId, pid, uid);
39 });
40 }
41 }
42
OnUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)43 void WindowStateObserver::OnUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)
44 {
45 auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
46 if (cgHandler && focusChangeInfo) {
47 auto windowId = focusChangeInfo->windowId_;
48 auto token = reinterpret_cast<uintptr_t>(focusChangeInfo->abilityToken_.GetRefPtr());
49 auto windowType = focusChangeInfo->windowType_;
50 auto displayId = focusChangeInfo->displayId_;
51 auto pid = focusChangeInfo->pid_;
52 auto uid = focusChangeInfo->uid_;
53
54 cgHandler->PostTask([cgHandler, windowId, token, windowType, displayId, pid, uid] {
55 cgHandler->HandleUnfocusedWindow(windowId, token, windowType, displayId, pid, uid);
56 });
57 }
58 }
59
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)60 void WindowVisibilityObserver::OnWindowVisibilityChanged(
61 const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo)
62 {
63 auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
64 if (!cgHandler) {
65 return;
66 }
67 for (auto& info : windowVisibilityInfo) {
68 if (!info) {
69 continue;
70 }
71 auto windowId = info->windowId_;
72 auto isVisible = info->isVisible_;
73 auto windowType = info->windowType_;
74 auto pid = info->pid_;
75 auto uid = info->uid_;
76 cgHandler->PostTask([cgHandler, windowId, isVisible, windowType, pid, uid] {
77 cgHandler->HandleWindowVisibilityChanged(windowId, isVisible, windowType, pid, uid);
78 });
79 nlohmann::json payload;
80 payload["pid"] = std::to_string(pid);
81 payload["uid"] = std::to_string(uid);
82 payload["windowId"] = std::to_string(windowId);
83 payload["windowType"] = std::to_string((int32_t)windowType);
84 ResSchedUtils::GetInstance().ReportDataInProcess(ResType::RES_TYPE_WINDOW_VISIBILITY_CHANGE,
85 isVisible ? ResType::WindowVisibilityStatus::VISIBLE : ResType::WindowVisibilityStatus::INVISIBLE, payload);
86 }
87 }
88 } // namespace ResourceSchedule
89 } // namespace OHOS
90