• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (!focusChangeInfo) {
29         return;
30     }
31 
32     auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
33     if (cgHandler) {
34         auto windowId = focusChangeInfo->windowId_;
35         auto token = reinterpret_cast<uintptr_t>(focusChangeInfo->abilityToken_.GetRefPtr());
36         auto windowType = focusChangeInfo->windowType_;
37         auto displayId = focusChangeInfo->displayId_;
38         auto pid = focusChangeInfo->pid_;
39         auto uid = focusChangeInfo->uid_;
40 
41         cgHandler->PostTask([cgHandler, windowId, token, windowType, displayId, pid, uid] {
42             cgHandler->HandleFocusedWindow(windowId, token, windowType, displayId, pid, uid);
43         });
44     }
45 
46     nlohmann::json payload;
47     payload["pid"] = std::to_string(focusChangeInfo->pid_);
48     payload["uid"] = std::to_string(focusChangeInfo->uid_);
49     payload["windowId"] = std::to_string(focusChangeInfo->windowId_);
50     payload["windowType"] = std::to_string((int32_t)(focusChangeInfo->windowType_));
51     payload["displayId"] = std::to_string(focusChangeInfo->displayId_);
52     ResSchedUtils::GetInstance().ReportDataInProcess(
53         ResType::RES_TYPE_WINDOW_FOCUS, ResType::WindowFocusStatus::WINDOW_FOCUS, payload);
54 }
55 
OnUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)56 void WindowStateObserver::OnUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo)
57 {
58     if (!focusChangeInfo) {
59         return;
60     }
61 
62     auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
63     if (cgHandler) {
64         auto windowId = focusChangeInfo->windowId_;
65         auto token = reinterpret_cast<uintptr_t>(focusChangeInfo->abilityToken_.GetRefPtr());
66         auto windowType = focusChangeInfo->windowType_;
67         auto displayId = focusChangeInfo->displayId_;
68         auto pid = focusChangeInfo->pid_;
69         auto uid = focusChangeInfo->uid_;
70 
71         cgHandler->PostTask([cgHandler, windowId, token, windowType, displayId, pid, uid] {
72             cgHandler->HandleUnfocusedWindow(windowId, token, windowType, displayId, pid, uid);
73         });
74     }
75 
76     nlohmann::json payload;
77     payload["pid"] = std::to_string(focusChangeInfo->pid_);
78     payload["uid"] = std::to_string(focusChangeInfo->uid_);
79     payload["windowId"] = std::to_string(focusChangeInfo->windowId_);
80     payload["windowType"] = std::to_string((int32_t)(focusChangeInfo->windowType_));
81     payload["displayId"] = std::to_string(focusChangeInfo->displayId_);
82     ResSchedUtils::GetInstance().ReportDataInProcess(
83         ResType::RES_TYPE_WINDOW_FOCUS, ResType::WindowFocusStatus::WINDOW_UNFOCUS, payload);
84 }
85 
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)86 void WindowVisibilityObserver::OnWindowVisibilityChanged(
87     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo)
88 {
89     auto cgHandler = SchedController::GetInstance().GetCgroupEventHandler();
90     if (!cgHandler) {
91         return;
92     }
93     for (auto& info : windowVisibilityInfo) {
94         if (!info) {
95             continue;
96         }
97         auto windowId = info->windowId_;
98         auto isVisible = info->isVisible_;
99         auto windowType = info->windowType_;
100         auto pid = info->pid_;
101         auto uid = info->uid_;
102         cgHandler->PostTask([cgHandler, windowId, isVisible, windowType, pid, uid] {
103             cgHandler->HandleWindowVisibilityChanged(windowId, isVisible, windowType, pid, uid);
104         });
105         nlohmann::json payload;
106         payload["pid"] = std::to_string(pid);
107         payload["uid"] = std::to_string(uid);
108         payload["windowId"] = std::to_string(windowId);
109         payload["windowType"] = std::to_string((int32_t)windowType);
110         ResSchedUtils::GetInstance().ReportDataInProcess(ResType::RES_TYPE_WINDOW_VISIBILITY_CHANGE,
111             isVisible ? ResType::WindowVisibilityStatus::VISIBLE : ResType::WindowVisibilityStatus::INVISIBLE, payload);
112     }
113 }
114 } // namespace ResourceSchedule
115 } // namespace OHOS
116