• 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 Licenses 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 be 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_adapter_lite.h"
17 #include "window_manager_hilog.h"
18 #include "wm_common.h"
19 #include "scene_board_judgement.h"
20 #include "session_manager_lite.h"
21 #include "focus_change_info.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAdapterLite"};
27 }
WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite)28 WM_IMPLEMENT_SINGLE_INSTANCE(WindowAdapterLite)
29 
30 #define INIT_PROXY_CHECK_RETURN(ret)        \
31     do {                                    \
32         if (!InitSSMProxy()) {              \
33             WLOGFE("InitSSMProxy failed!"); \
34             return ret;                     \
35         }                                   \
36     } while (false)
37 
38 #define CHECK_PROXY_RETURN_ERROR_IF_NULL(proxy, ret)                      \
39     do {                                                                  \
40         if ((proxy) == nullptr) {                                         \
41             TLOGE(WmsLogTag::DEFAULT, "window manager proxy is nullptr"); \
42             return ret;                                                   \
43         }                                                                 \
44     } while(false)
45 
46 #define CHECK_PROXY_RETURN_IF_NULL(proxy)                                 \
47     do {                                                                  \
48         if ((proxy) == nullptr) {                                         \
49             TLOGE(WmsLogTag::DEFAULT, "window manager proxy is nullptr"); \
50             return;                                                       \
51         }                                                                 \
52     } while(false)
53 
54 WMError WindowAdapterLite::RegisterWindowManagerAgent(WindowManagerAgentType type,
55     const sptr<IWindowManagerAgent>& windowManagerAgent)
56 {
57     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
58 
59     auto wmsProxy = GetWindowManagerServiceProxy();
60     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
61 
62     {
63         std::lock_guard<std::mutex> lock(windowManagerLiteAgentMapMutex_);
64         if (windowManagerLiteAgentMap_.find(type) == windowManagerLiteAgentMap_.end()) {
65             windowManagerLiteAgentMap_[type] = std::set<sptr<IWindowManagerAgent>>();
66         }
67         windowManagerLiteAgentMap_[type].insert(windowManagerAgent);
68     }
69 
70     return wmsProxy->RegisterWindowManagerAgent(type, windowManagerAgent);
71 }
72 
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)73 WMError WindowAdapterLite::UnregisterWindowManagerAgent(WindowManagerAgentType type,
74     const sptr<IWindowManagerAgent>& windowManagerAgent)
75 {
76     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
77 
78     auto wmsProxy = GetWindowManagerServiceProxy();
79     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
80     auto ret = wmsProxy->UnregisterWindowManagerAgent(type, windowManagerAgent);
81 
82     std::lock_guard<std::mutex> lock(windowManagerLiteAgentMapMutex_);
83     if (windowManagerLiteAgentMap_.find(type) == windowManagerLiteAgentMap_.end()) {
84         TLOGW(WmsLogTag::WMS_MULTI_USER, "WindowManagerAgentType=%{public}d not found", type);
85         return ret;
86     }
87     auto& agentSet = windowManagerLiteAgentMap_[type];
88     auto agent = std::find(agentSet.begin(), agentSet.end(), windowManagerAgent);
89     if (agent == agentSet.end()) {
90         TLOGW(WmsLogTag::WMS_MULTI_USER, "Cannot find agent,  type=%{public}d", type);
91         return ret;
92     }
93     agentSet.erase(agent);
94     return ret;
95 }
96 
97 // called after InitSSMProxy()
ReregisterWindowManagerLiteAgent()98 void WindowAdapterLite::ReregisterWindowManagerLiteAgent()
99 {
100     auto wmsProxy = GetWindowManagerServiceProxy();
101     CHECK_PROXY_RETURN_IF_NULL(wmsProxy);
102 
103     std::lock_guard<std::mutex> lock(windowManagerLiteAgentMapMutex_);
104     for (const auto& it : windowManagerLiteAgentMap_) {
105         TLOGI(WmsLogTag::WMS_MULTI_USER, "Window manager agent type=%{public}" PRIu32 ", size=%{public}" PRIu64,
106             it.first, static_cast<uint64_t>(it.second.size()));
107         for (auto& agent : it.second) {
108             if (wmsProxy->RegisterWindowManagerAgent(it.first, agent) != WMError::WM_OK) {
109                 TLOGW(WmsLogTag::WMS_MULTI_USER, "Reregister window manager agent failed");
110             }
111         }
112     }
113 }
114 
CheckWindowId(int32_t windowId,int32_t & pid)115 WMError WindowAdapterLite::CheckWindowId(int32_t windowId, int32_t& pid)
116 {
117     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
118 
119     auto wmsProxy = GetWindowManagerServiceProxy();
120     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
121     return wmsProxy->CheckWindowId(windowId, pid);
122 }
123 
GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>> & infos)124 WMError WindowAdapterLite::GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos)
125 {
126     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
127 
128     auto wmsProxy = GetWindowManagerServiceProxy();
129     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
130     return wmsProxy->GetVisibilityWindowInfo(infos);
131 }
132 
InitSSMProxy()133 bool WindowAdapterLite::InitSSMProxy()
134 {
135     std::lock_guard<std::mutex> lock(mutex_);
136     if (!isProxyValid_) {
137         windowManagerServiceProxy_ = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
138         if (!windowManagerServiceProxy_ || !windowManagerServiceProxy_->AsObject()) {
139             WLOGFE("Failed to get scene session manager lite proxy");
140             return false;
141         }
142         wmsDeath_ = new (std::nothrow) WMSDeathRecipient();
143         if (!wmsDeath_) {
144             WLOGFE("Failed to create death recipient WMSDeathRecipient");
145             return false;
146         }
147         sptr<IRemoteObject> remoteObject = windowManagerServiceProxy_->AsObject();
148         if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(wmsDeath_)) {
149             WLOGFE("Failed to add death recipient");
150             return false;
151         }
152         // U0 system user needs to subscribe OnUserSwitch event
153         int32_t clientUserId = GetUserIdByUid(getuid());
154         if (clientUserId == SYSTEM_USERID && !isRegisteredUserSwitchListener_) {
155             SessionManagerLite::GetInstance().RegisterUserSwitchListener([this] { this->OnUserSwitch(); });
156             isRegisteredUserSwitchListener_ = true;
157         }
158         isProxyValid_ = true;
159     }
160     return true;
161 }
162 
OnUserSwitch()163 void WindowAdapterLite::OnUserSwitch()
164 {
165     TLOGD(WmsLogTag::WMS_MULTI_USER, "User switched lite");
166     ClearWindowAdapter();
167     InitSSMProxy();
168     ReregisterWindowManagerLiteAgent();
169 }
170 
ClearWindowAdapter()171 void WindowAdapterLite::ClearWindowAdapter()
172 {
173     WLOGD("ClearWindowAdapter");
174     std::lock_guard<std::mutex> lock(mutex_);
175     if (windowManagerServiceProxy_ != nullptr && windowManagerServiceProxy_->AsObject() != nullptr) {
176         windowManagerServiceProxy_->AsObject()->RemoveDeathRecipient(wmsDeath_);
177     }
178     isProxyValid_ = false;
179     windowManagerServiceProxy_ = nullptr;
180 }
181 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)182 void WMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
183 {
184     if (wptrDeath == nullptr) {
185         WLOGFE("wptrDeath is null");
186         return;
187     }
188     sptr<IRemoteObject> object = wptrDeath.promote();
189     if (!object) {
190         WLOGFE("object is null");
191         return;
192     }
193     WLOGI("wms OnRemoteDied");
194     SingletonContainer::Get<WindowAdapterLite>().ClearWindowAdapter();
195     SingletonContainer::Get<SessionManagerLite>().ClearSessionManagerProxy();
196 }
197 
GetFocusWindowInfo(FocusChangeInfo & focusInfo,DisplayId displayId)198 void WindowAdapterLite::GetFocusWindowInfo(FocusChangeInfo& focusInfo, DisplayId displayId)
199 {
200     INIT_PROXY_CHECK_RETURN();
201     WLOGFD("use Foucus window info proxy");
202 
203     auto wmsProxy = GetWindowManagerServiceProxy();
204     CHECK_PROXY_RETURN_IF_NULL(wmsProxy);
205     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
206         wmsProxy->GetFocusWindowInfo(focusInfo, displayId);
207     } else {
208         wmsProxy->GetFocusWindowInfo(focusInfo);
209     }
210 }
211 
GetWindowModeType(WindowModeType & windowModeType)212 WMError WindowAdapterLite::GetWindowModeType(WindowModeType& windowModeType)
213 {
214     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
215     WLOGFD("get window mode type");
216 
217     auto wmsProxy = GetWindowManagerServiceProxy();
218     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
219     return wmsProxy->GetWindowModeType(windowModeType);
220 }
221 
GetMainWindowInfos(int32_t topNum,std::vector<MainWindowInfo> & topNInfo)222 WMError WindowAdapterLite::GetMainWindowInfos(int32_t topNum, std::vector<MainWindowInfo>& topNInfo)
223 {
224     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
225     TLOGD(WmsLogTag::WMS_MAIN, "get top main window info");
226 
227     auto wmsProxy = GetWindowManagerServiceProxy();
228     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
229     return wmsProxy->GetMainWindowInfos(topNum, topNInfo);
230 }
231 
GetCallingWindowInfo(CallingWindowInfo & callingWindowInfo)232 WMError WindowAdapterLite::GetCallingWindowInfo(CallingWindowInfo& callingWindowInfo)
233 {
234     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
235     TLOGD(WmsLogTag::WMS_KEYBOARD, "get calling window info");
236 
237     auto wmsProxy = GetWindowManagerServiceProxy();
238     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
239     return wmsProxy->GetCallingWindowInfo(callingWindowInfo);
240 }
241 
GetAllMainWindowInfos(std::vector<MainWindowInfo> & infos)242 WMError WindowAdapterLite::GetAllMainWindowInfos(std::vector<MainWindowInfo>& infos)
243 {
244     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
245     TLOGD(WmsLogTag::WMS_MAIN, "get all main window info");
246 
247     auto wmsProxy = GetWindowManagerServiceProxy();
248     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
249     return wmsProxy->GetAllMainWindowInfos(infos);
250 }
251 
ClearMainSessions(const std::vector<int32_t> & persistentIds)252 WMError WindowAdapterLite::ClearMainSessions(const std::vector<int32_t>& persistentIds)
253 {
254     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
255     TLOGD(WmsLogTag::WMS_MAIN, "clear main sessions.");
256 
257     auto wmsProxy = GetWindowManagerServiceProxy();
258     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
259     std::vector<int32_t> clearFailedIds;
260     return wmsProxy->ClearMainSessions(persistentIds, clearFailedIds);
261 }
262 
ClearMainSessions(const std::vector<int32_t> & persistentIds,std::vector<int32_t> & clearFailedIds)263 WMError WindowAdapterLite::ClearMainSessions(const std::vector<int32_t>& persistentIds,
264     std::vector<int32_t>& clearFailedIds)
265 {
266     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
267     TLOGD(WmsLogTag::WMS_MAIN, "clear main sessions with failed ids.");
268 
269     auto wmsProxy = GetWindowManagerServiceProxy();
270     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
271     return wmsProxy->ClearMainSessions(persistentIds, clearFailedIds);
272 }
273 
RaiseWindowToTop(int32_t persistentId)274 WMError WindowAdapterLite::RaiseWindowToTop(int32_t persistentId)
275 {
276     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
277 
278     auto wmsProxy = GetWindowManagerServiceProxy();
279     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
280     return static_cast<WMError>(wmsProxy->RaiseWindowToTop(persistentId));
281 }
282 
RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc & callbackFunc)283 WMError WindowAdapterLite::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc)
284 {
285     TLOGD(WmsLogTag::WMS_MAIN, "register listener");
286     return SessionManagerLite::GetInstance().RegisterWMSConnectionChangedListener(callbackFunc);
287 }
288 
GetWindowStyleType(WindowStyleType & windowStyleType)289 WMError WindowAdapterLite::GetWindowStyleType(WindowStyleType& windowStyleType)
290 {
291     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
292     auto wmsProxy = GetWindowManagerServiceProxy();
293     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
294     return wmsProxy->GetWindowStyleType(windowStyleType);
295 }
296 
GetWindowManagerServiceProxy() const297 sptr<IWindowManagerLite> WindowAdapterLite::GetWindowManagerServiceProxy() const
298 {
299     std::lock_guard<std::mutex> lock(mutex_);
300     return windowManagerServiceProxy_;
301 }
302 
TerminateSessionByPersistentId(int32_t persistentId)303 WMError WindowAdapterLite::TerminateSessionByPersistentId(int32_t persistentId)
304 {
305     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
306 
307     auto wmsProxy = GetWindowManagerServiceProxy();
308     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
309     return wmsProxy->TerminateSessionByPersistentId(persistentId);
310 }
311 
CloseTargetFloatWindow(const std::string & bundleName)312 WMError WindowAdapterLite::CloseTargetFloatWindow(const std::string& bundleName)
313 {
314     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
315     auto wmsProxy = GetWindowManagerServiceProxy();
316     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
317     return wmsProxy->CloseTargetFloatWindow(bundleName);
318 }
319 
CloseTargetPiPWindow(const std::string & bundleName)320 WMError WindowAdapterLite::CloseTargetPiPWindow(const std::string& bundleName)
321 {
322     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
323     auto wmsProxy = GetWindowManagerServiceProxy();
324     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
325     return wmsProxy->CloseTargetPiPWindow(bundleName);
326 }
327 
GetCurrentPiPWindowInfo(std::string & bundleName)328 WMError WindowAdapterLite::GetCurrentPiPWindowInfo(std::string& bundleName)
329 {
330     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
331     auto wmsProxy = GetWindowManagerServiceProxy();
332     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
333     return wmsProxy->GetCurrentPiPWindowInfo(bundleName);
334 }
335 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos)336 WMError WindowAdapterLite::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos)
337 {
338     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
339 
340     auto wmsProxy = GetWindowManagerServiceProxy();
341     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
342     return wmsProxy->GetAccessibilityWindowInfo(infos);
343 }
344 
ListWindowInfo(const WindowInfoOption & windowInfoOption,std::vector<sptr<WindowInfo>> & infos)345 WMError WindowAdapterLite::ListWindowInfo(const WindowInfoOption& windowInfoOption,
346     std::vector<sptr<WindowInfo>>& infos)
347 {
348     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
349     auto wmsProxy = GetWindowManagerServiceProxy();
350     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
351     return wmsProxy->ListWindowInfo(windowInfoOption, infos);
352 }
353 } // namespace Rosen
354 } // namespace OHOS
355