• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             TLOGI(WmsLogTag::WMS_MAIN, "insert pid: %{public}d, type: %{public}u",
46                 pid, static_cast<uint32_t>(type));
47             windowManagerPidAgentMap_.insert(std::map<int32_t,
48                 std::map<WindowManagerAgentType, sptr<IWindowManagerAgent>>>::value_type(pid, typeAgentMap));
49         }
50         std::pair<int32_t, WindowManagerAgentType> pidPair = {pid, type};
51         windowManagerAgentPairMap_.insert(std::map<sptr<IRemoteObject>,
52             std::pair<int32_t, WindowManagerAgentType>>::value_type(windowManagerAgent->AsObject(), pidPair));
53         if (type == WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE &&
54             windowManagementMode_ != WindowManagementMode::UNDEFINED) {
55             NotifyWindowStyleChange(windowManagementMode_ == WindowManagementMode::FREEFORM ?
56                 WindowStyleType::WINDOW_STYLE_FREE_MULTI_WINDOW : WindowStyleType::WINDOW_STYLE_DEFAULT);
57             windowManagementMode_ = WindowManagementMode::UNDEFINED;
58         }
59         return WMError::WM_OK;
60     } else {
61         return WMError::WM_ERROR_NULLPTR;
62     }
63 }
64 
UnregisterWindowManagerAgent(const sptr<IWindowManagerAgent> & windowManagerAgent,WindowManagerAgentType type,int32_t pid)65 WMError SessionManagerAgentController::UnregisterWindowManagerAgent(const sptr<IWindowManagerAgent>& windowManagerAgent,
66     WindowManagerAgentType type, int32_t pid)
67 {
68     TLOGI(WmsLogTag::WMS_SYSTEM, "type: %{public}u", static_cast<uint32_t>(type));
69     if (smAgentContainer_.UnregisterAgent(windowManagerAgent, type)) {
70         std::lock_guard<std::mutex> lock(windowManagerAgentPidMapMutex_);
71         auto it = windowManagerPidAgentMap_.find(pid);
72         if (it != windowManagerPidAgentMap_.end()) {
73             auto& typeAgentMap = it->second;
74             auto typeAgentIter = typeAgentMap.find(type);
75             if (typeAgentIter != typeAgentMap.end()) {
76                 windowManagerAgentPairMap_.erase((typeAgentIter->second)->AsObject());
77                 typeAgentMap.erase(type);
78                 if (typeAgentMap.empty()) {
79                     TLOGI(WmsLogTag::WMS_MAIN, "erase pid: %{public}d, type: %{public}u",
80                         pid, static_cast<uint32_t>(type));
81                     windowManagerPidAgentMap_.erase(pid);
82                 }
83             }
84         }
85         return WMError::WM_OK;
86     } else {
87         return WMError::WM_ERROR_NULLPTR;
88     }
89 }
90 
UpdateCameraFloatWindowStatus(uint32_t accessTokenId,bool isShowing)91 void SessionManagerAgentController::UpdateCameraFloatWindowStatus(uint32_t accessTokenId, bool isShowing)
92 {
93     for (auto& agent : smAgentContainer_.GetAgentsByType(
94         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT)) {
95         if (agent != nullptr) {
96             agent->UpdateCameraFloatWindowStatus(accessTokenId, isShowing);
97         }
98     }
99 }
100 
UpdateFocusChangeInfo(const sptr<FocusChangeInfo> & focusChangeInfo,bool isFocused)101 void SessionManagerAgentController::UpdateFocusChangeInfo(const sptr<FocusChangeInfo>& focusChangeInfo, bool isFocused)
102 {
103     TLOGD(WmsLogTag::WMS_FOCUS, "in");
104     for (auto& agent : smAgentContainer_.GetAgentsByType(
105         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_FOCUS)) {
106         if (agent != nullptr) {
107             agent->UpdateFocusChangeInfo(focusChangeInfo, isFocused);
108         } else {
109             TLOGW(WmsLogTag::WMS_FOCUS, "agent is invalid");
110         }
111     }
112 }
113 
UpdateWindowModeTypeInfo(WindowModeType type)114 void SessionManagerAgentController::UpdateWindowModeTypeInfo(WindowModeType type)
115 {
116     TLOGD(WmsLogTag::WMS_MAIN, "SessionManagerAgentController UpdateWindowModeTypeInfo type: %{public}d",
117         static_cast<uint8_t>(type));
118     for (auto& agent : smAgentContainer_.GetAgentsByType(
119         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_MODE)) {
120         if (agent != nullptr) {
121             agent->UpdateWindowModeTypeInfo(type);
122         }
123     }
124 }
125 
NotifyAccessibilityWindowInfo(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)126 void SessionManagerAgentController::NotifyAccessibilityWindowInfo(
127     const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type)
128 {
129     for (auto& agent : smAgentContainer_.GetAgentsByType(
130         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_UPDATE)) {
131         if (agent != nullptr) {
132             agent->NotifyAccessibilityWindowInfo(infos, type);
133         }
134     }
135 }
136 
NotifyWaterMarkFlagChangedResult(bool hasWaterMark)137 void SessionManagerAgentController::NotifyWaterMarkFlagChangedResult(bool hasWaterMark)
138 {
139     WLOGFD("NotifyWaterMarkFlagChanged with result:%{public}u", static_cast<uint32_t>(hasWaterMark));
140     for (auto& agent : smAgentContainer_.GetAgentsByType(
141         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WATER_MARK_FLAG)) {
142         if (agent != nullptr) {
143             agent->NotifyWaterMarkFlagChangedResult(hasWaterMark);
144         }
145     }
146 }
147 
UpdateWindowVisibilityInfo(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfos)148 void SessionManagerAgentController::UpdateWindowVisibilityInfo(
149     const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfos)
150 {
151     for (auto& agent : smAgentContainer_.GetAgentsByType(
152         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_VISIBILITY)) {
153         agent->UpdateWindowVisibilityInfo(windowVisibilityInfos);
154     }
155 }
156 
UpdateVisibleWindowNum(const std::vector<VisibleWindowNumInfo> & visibleWindowNumInfo)157 void SessionManagerAgentController::UpdateVisibleWindowNum(
158     const std::vector<VisibleWindowNumInfo>& visibleWindowNumInfo)
159 {
160     for (const auto& num : visibleWindowNumInfo) {
161         TLOGI(WmsLogTag::WMS_MAIN, "displayId=%{public}d, visibleWindowNum=%{public}d",
162             num.displayId, num.visibleWindowNum);
163     }
164     for (auto& agent : smAgentContainer_.GetAgentsByType(
165         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_VISIBLE_WINDOW_NUM)) {
166         if (agent != nullptr) {
167             agent->UpdateVisibleWindowNum(visibleWindowNumInfo);
168         }
169     }
170 }
171 
UpdateWindowDrawingContentInfo(const std::vector<sptr<WindowDrawingContentInfo>> & windowDrawingContentInfos)172 void SessionManagerAgentController::UpdateWindowDrawingContentInfo(
173     const std::vector<sptr<WindowDrawingContentInfo>>& windowDrawingContentInfos)
174 {
175     WLOGFD("Size:%{public}zu", windowDrawingContentInfos.size());
176     for (auto& agent : smAgentContainer_.GetAgentsByType(
177         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_DRAWING_STATE)) {
178         agent->UpdateWindowDrawingContentInfo(windowDrawingContentInfos);
179     }
180 }
181 
UpdateCameraWindowStatus(uint32_t accessTokenId,bool isShowing)182 void SessionManagerAgentController::UpdateCameraWindowStatus(uint32_t accessTokenId, bool isShowing)
183 {
184     TLOGI(WmsLogTag::WMS_SYSTEM, "accessTokenId:%{private}u, isShowing:%{public}d", accessTokenId,
185         static_cast<int>(isShowing));
186     for (auto &agent: smAgentContainer_.GetAgentsByType(
187         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_WINDOW)) {
188         if (agent != nullptr) {
189             agent->UpdateCameraWindowStatus(accessTokenId, isShowing);
190         }
191     }
192 }
193 
DoAfterAgentDeath(const sptr<IRemoteObject> & remoteObject)194 void SessionManagerAgentController::DoAfterAgentDeath(const sptr<IRemoteObject>& remoteObject)
195 {
196     std::lock_guard<std::mutex> lock(windowManagerAgentPidMapMutex_);
197     auto it = windowManagerAgentPairMap_.find(remoteObject);
198     if (it != windowManagerAgentPairMap_.end()) {
199         auto [pid, type] = it->second;
200         auto pidIter = windowManagerPidAgentMap_.find(pid);
201         if (pidIter != windowManagerPidAgentMap_.end()) {
202             auto& typeAgentMap = pidIter->second;
203             TLOGI(WmsLogTag::WMS_MAIN, "type: %{public}u", static_cast<uint32_t>(type));
204             typeAgentMap.erase(type);
205             if (typeAgentMap.empty()) {
206                 TLOGI(WmsLogTag::WMS_MAIN, "pid: %{public}d", pid);
207                 windowManagerPidAgentMap_.erase(pid);
208             }
209         }
210         windowManagerAgentPairMap_.erase(remoteObject);
211     }
212 }
213 
NotifyGestureNavigationEnabledResult(bool enable)214 void SessionManagerAgentController::NotifyGestureNavigationEnabledResult(bool enable)
215 {
216     WLOGFD("result:%{public}d", enable);
217     for (auto& agent : smAgentContainer_.GetAgentsByType(
218         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_GESTURE_NAVIGATION_ENABLED)) {
219         if (agent != nullptr) {
220             agent->NotifyGestureNavigationEnabledResult(enable);
221         }
222     }
223 }
224 
NotifyWindowStyleChange(WindowStyleType type)225 void SessionManagerAgentController::NotifyWindowStyleChange(WindowStyleType type)
226 {
227     TLOGD(WmsLogTag::WMS_MAIN, "windowStyletype: %{public}d",
228           static_cast<uint8_t>(type));
229     bool hasNotified = false;
230     for (auto& agent : smAgentContainer_.GetAgentsByType(
231         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_STYLE)) {
232         if (agent != nullptr) {
233             agent->NotifyWindowStyleChange(type);
234             hasNotified = true;
235         }
236     }
237     if (!hasNotified) {
238         windowManagementMode_ = type == WindowStyleType::WINDOW_STYLE_FREE_MULTI_WINDOW ?
239             WindowManagementMode::FREEFORM : WindowManagementMode::FULLSCREEN;
240     }
241 }
242 
NotifyCallingWindowDisplayChanged(const CallingWindowInfo & callingWindowInfo)243 void SessionManagerAgentController::NotifyCallingWindowDisplayChanged(const CallingWindowInfo& callingWindowInfo)
244 {
245     TLOGD(WmsLogTag::WMS_KEYBOARD, "notify persistentId: %{public}d, pid: %{public}d, "
246         "displayId: %{public}d, userId: %{public}d", callingWindowInfo.windowId_,
247         callingWindowInfo.callingPid_, static_cast<int32_t>(callingWindowInfo.displayId_), callingWindowInfo.userId_);
248     for (const auto& agent : smAgentContainer_.GetAgentsByType(
249         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CALLING_DISPLAY)) {
250         if (agent != nullptr) {
251             agent->NotifyCallingWindowDisplayChanged(callingWindowInfo);
252         } else {
253             TLOGE(WmsLogTag::WMS_KEYBOARD, "agent is nullptr, userId: %{public}d, displayId: %{public}d, "
254                 "persistentId: %{public}d", callingWindowInfo.userId_,
255                 static_cast<int32_t>(callingWindowInfo.displayId_), callingWindowInfo.windowId_);
256         }
257     }
258 }
259 
NotifyWindowPidVisibilityChanged(const sptr<WindowPidVisibilityInfo> & info)260 void SessionManagerAgentController::NotifyWindowPidVisibilityChanged(
261     const sptr<WindowPidVisibilityInfo>& info)
262 {
263     for (auto& agent : smAgentContainer_.GetAgentsByType(
264         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_WINDOW_PID_VISIBILITY)) {
265         if (agent != nullptr) {
266             agent->NotifyWindowPidVisibilityChanged(info);
267         }
268     }
269 }
270 
UpdatePiPWindowStateChanged(const std::string & bundleName,bool isForeground)271 void SessionManagerAgentController::UpdatePiPWindowStateChanged(const std::string& bundleName, bool isForeground)
272 {
273     for (auto& agent : smAgentContainer_.GetAgentsByType(
274         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PIP)) {
275         if (agent != nullptr) {
276             agent->UpdatePiPWindowStateChanged(bundleName, isForeground);
277         }
278     }
279 }
280 
NotifyWindowSystemBarPropertyChange(WindowType type,const SystemBarProperty & systemBarProperty)281 void SessionManagerAgentController::NotifyWindowSystemBarPropertyChange(
282     WindowType type, const SystemBarProperty& systemBarProperty)
283 {
284     for (auto& agent : smAgentContainer_.GetAgentsByType(
285         WindowManagerAgentType::WINDOW_MANAGER_AGENT_STATUS_BAR_PROPERTY)) {
286         if (agent != nullptr) {
287             agent->NotifyWindowSystemBarPropertyChange(type, systemBarProperty);
288         }
289     }
290 }
291 
NotifyWindowPropertyChange(uint32_t propertyDirtyFlags,const std::vector<std::unordered_map<WindowInfoKey,WindowChangeInfoType>> & windowInfoList)292 void SessionManagerAgentController::NotifyWindowPropertyChange(uint32_t propertyDirtyFlags,
293     const std::vector<std::unordered_map<WindowInfoKey, WindowChangeInfoType>>& windowInfoList)
294 {
295     for (const auto& agent : smAgentContainer_.GetAgentsByType(
296         WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_PROPERTY)) {
297         if (agent != nullptr) {
298             agent->NotifyWindowPropertyChange(propertyDirtyFlags, windowInfoList);
299         }
300     }
301 }
302 } // namespace Rosen
303 } // namespace OHOS