1 /*
2  * Copyright (c) 2023 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 "session_manager_agent_controller.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "SessionManagerAgentController"};
22 }
WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerAgentController)23 WM_IMPLEMENT_SINGLE_INSTANCE(SessionManagerAgentController)
24 
25 WMError SessionManagerAgentController::RegisterWindowManagerAgent(const sptr<IWindowManagerAgent>& windowManagerAgent,
26     WindowManagerAgentType type, int32_t pid)
27 {
28     TLOGI(WmsLogTag::WMS_SYSTEM, "type: %{public}u", static_cast<uint32_t>(type));
29     if (smAgentContainer_.RegisterAgent(windowManagerAgent, type)) {
30         std::lock_guard<std::mutex> lock(windowManagerAgentPidMapMutex_);
31         auto it = windowManagerPidAgentMap_.find(pid);
32         if (it != windowManagerPidAgentMap_.end()) {
33             auto& typeAgentMap = it->second;
34             auto typeAgentIter = typeAgentMap.find(type);
35             if (typeAgentIter != typeAgentMap.end()) {
36                 smAgentContainer_.UnregisterAgent(typeAgentIter->second, type);
37                 windowManagerAgentPairMap_.erase((typeAgentIter->second)->AsObject());
38             }
39             typeAgentMap.insert(std::map<WindowManagerAgentType,
40                 sptr<IWindowManagerAgent>>::value_type(type, windowManagerAgent));
41         } else {
42             std::map<WindowManagerAgentType, sptr<IWindowManagerAgent>> typeAgentMap;
43             typeAgentMap.insert(std::map<WindowManagerAgentType,
44                 sptr<IWindowManagerAgent>>::value_type(type, windowManagerAgent));
45             windowManagerPidAgentMap_.insert(std::map<int32_t,
46                 std::map<WindowManagerAgentType, sptr<IWindowManagerAgent>>>::value_type(pid, typeAgentMap));
47         }
48         std::pair<int32_t, WindowManagerAgentType> pidPair = {pid, type};
49         windowManagerAgentPairMap_.insert(std::map<sptr<IRemoteObject>,
50             std::pair<int32_t, WindowManagerAgentType>>::value_type(windowManagerAgent->AsObject(), pidPair));
51         return WMError::WM_OK;
52     } else {
53         return WMError::WM_ERROR_NULLPTR;
54     }
55 }
56 
UnregisterWindowManagerAgent(const sptr<IWindowManagerAgent> & windowManagerAgent,WindowManagerAgentType type,int32_t pid)57 WMError SessionManagerAgentController::UnregisterWindowManagerAgent(const sptr<IWindowManagerAgent>& windowManagerAgent,
58     WindowManagerAgentType type, int32_t pid)
59 {
60     TLOGI(WmsLogTag::WMS_SYSTEM, "type: %{public}u", static_cast<uint32_t>(type));
61     if (smAgentContainer_.UnregisterAgent(windowManagerAgent, type)) {
62         std::lock_guard<std::mutex> lock(windowManagerAgentPidMapMutex_);
63         auto it = windowManagerPidAgentMap_.find(pid);
64         if (it != windowManagerPidAgentMap_.end()) {
65             auto& typeAgentMap = it->second;
66             auto typeAgentIter = typeAgentMap.find(type);
67             if (typeAgentIter != typeAgentMap.end()) {
68                 windowManagerAgentPairMap_.erase((typeAgentIter->second)->AsObject());
69                 typeAgentMap.erase(type);
70                 if (typeAgentMap.empty()) {
71                     windowManagerPidAgentMap_.erase(pid);
72                 }
73             }
74         }
75         return WMError::WM_OK;
76     } else {
77         return WMError::WM_ERROR_NULLPTR;
78     }
79 }
80 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId,bool isShowing)81 void SessionManagerAgentController::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing)
82 {
83     for (auto& agent : smAgentContainer_.GetAgentsByType(
84         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT)) {
85         if (agent != nullptr) {
86             agent->UpdateCameraFloatWindowStatus(accessTokenId, isShowing);
87         }
88     }
89 }
90 
UpdateFocusChangeInfo(const sptr<FocusChangeInfo> & focusChangeInfo,bool isFocused)91 void SessionManagerAgentController::UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool isFocused)
92 {
93     for (auto& agent : smAgentContainer_.GetAgentsByType(
94         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS)) {
95         if (agent != nullptr) {
96             agent->UpdateFocusChangeInfo(focusChangeInfo, isFocused);
97         }
98     }
99 }
100 
UpdateWindowModeTypeInfo(WindowModeType type)101 void SessionManagerAgentController::UpdateWindowModeTypeInfo(WindowModeType type)
102 {
103     TLOGD(WmsLogTag::WMS_MAIN, "SessionManagerAgentController UpdateWindowModeTypeInfo type: %{public}d",
104         static_cast<uint8_t>(type));
105     for (auto& agent : smAgentContainer_.GetAgentsByType(
106         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE)) {
107         if (agent != nullptr) {
108             agent->UpdateWindowModeTypeInfo(type);
109         }
110     }
111 }
112 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)113 void SessionManagerAgentController::NotifyAccessibilityWindowInfo(
114     const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type)
115 {
116     for (auto& agent : smAgentContainer_.GetAgentsByType(
117         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE)) {
118         if (agent != nullptr) {
119             agent->NotifyAccessibilityWindowInfo(infos, type);
120         }
121     }
122 }
123 
124 
NotifyWaterMarkFlagChangedResult(bool hasWaterMark)125 void SessionManagerAgentController::NotifyWaterMarkFlagChangedResult(bool hasWaterMark)
126 {
127     WLOGFD("NotifyWaterMarkFlagChanged with result:%{public}u", static_cast<uint32_t>(hasWaterMark));
128     for (auto& agent : smAgentContainer_.GetAgentsByType(
129         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG)) {
130         if (agent != nullptr) {
131             agent->NotifyWaterMarkFlagChangedResult(hasWaterMark);
132         }
133     }
134 }
135 
UpdateWindowVisibilityInfo(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos)136 void SessionManagerAgentController::UpdateWindowVisibilityInfo(
137     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
138 {
139     for (auto& agent : smAgentContainer_.GetAgentsByType(
140         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY)) {
141         agent->UpdateWindowVisibilityInfo(windowVisibilityInfos);
142     }
143 }
144 
UpdateVisibleWindowNum(const std::vector<VisibleWindowNumInfo> & visibleWindowNumInfo)145 void SessionManagerAgentController::UpdateVisibleWindowNum(
146     const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)
147 {
148     for (const auto& num : visibleWindowNumInfo) {
149         TLOGI(WmsLogTag::WMS_MAIN, "displayId = %{public}d, visibleWindowNum = %{public}d",
150             num.displayId, num.visibleWindowNum);
151     }
152     for (auto& agent : smAgentContainer_.GetAgentsByType(
153         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM)) {
154         if (agent != nullptr) {
155             agent->UpdateVisibleWindowNum(visibleWindowNumInfo);
156         }
157     }
158 }
159 
UpdateWindowDrawingContentInfo(const std::vector<sptr<WindowDrawingContentInfo>> & windowDrawingContentInfos)160 void SessionManagerAgentController::UpdateWindowDrawingContentInfo(
161     const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos)
162 {
163     WLOGFD("Size:%{public}zu", windowDrawingContentInfos.size());
164     for (auto& agent : smAgentContainer_.GetAgentsByType(
165         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE)) {
166         agent->UpdateWindowDrawingContentInfo(windowDrawingContentInfos);
167     }
168 }
169 
UpdateCameraWindowStatus(uint32_t accessTokenId,bool isShowing)170 void SessionManagerAgentController::UpdateCameraWindowStatus(uint32_t accessTokenId, bool isShowing)
171 {
172     TLOGI(WmsLogTag::WMS_SYSTEM, "accessTokenId:%{private}u, isShowing:%{public}d", accessTokenId,
173         static_cast<int>(isShowing));
174     for (auto &agent: smAgentContainer_.GetAgentsByType(
175         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW)) {
176         if (agent != nullptr) {
177             agent->UpdateCameraWindowStatus(accessTokenId, isShowing);
178         }
179     }
180 }
181 
DoAfterAgentDeath(const sptr<IRemoteObject> & remoteObject)182 void SessionManagerAgentController::DoAfterAgentDeath(const sptr<IRemoteObject>& remoteObject)
183 {
184     std::lock_guard<std::mutex> lock(windowManagerAgentPidMapMutex_);
185     auto it = windowManagerAgentPairMap_.find(remoteObject);
186     if (it != windowManagerAgentPairMap_.end()) {
187         auto [pid, type] = it->second;
188         auto pidIter = windowManagerPidAgentMap_.find(pid);
189         if (pidIter != windowManagerPidAgentMap_.end()) {
190             auto& typeAgentMap = pidIter->second;
191             typeAgentMap.erase(type);
192             if (typeAgentMap.empty()) {
193                 windowManagerPidAgentMap_.erase(pid);
194             }
195         }
196         windowManagerAgentPairMap_.erase(remoteObject);
197     }
198 }
199 
NotifyGestureNavigationEnabledResult(bool enable)200 void SessionManagerAgentController::NotifyGestureNavigationEnabledResult(bool enable)
201 {
202     WLOGFD("result:%{public}d", enable);
203     for (auto& agent : smAgentContainer_.GetAgentsByType(
204         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED)) {
205         if (agent != nullptr) {
206             agent->NotifyGestureNavigationEnabledResult(enable);
207         }
208     }
209 }
210 
NotifyWindowStyleChange(WindowStyleType type)211 void SessionManagerAgentController::NotifyWindowStyleChange(WindowStyleType type)
212 {
213     TLOGD(WmsLogTag::WMS_MAIN, "windowStyletype: %{public}d",
214           static_cast<uint8_t>(type));
215     for (auto& agent : smAgentContainer_.GetAgentsByType(
216         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE)) {
217         if (agent != nullptr) {
218             agent->NotifyWindowStyleChange(type);
219         }
220     }
221 }
222 
UpdatePiPWindowStateChanged(const std::string & bundleName,bool isForeground)223 void SessionManagerAgentController::UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground)
224 {
225     for (auto& agent : smAgentContainer_.GetAgentsByType(
226         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP)) {
227         if (agent != nullptr) {
228             agent->UpdatePiPWindowStateChanged(bundleName, isForeground);
229         }
230     }
231 }
232 } // namespace Rosen
233 } // namespace OHOS