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     TLOGI(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)198 void WindowAdapterLite::GetFocusWindowInfo(FocusChangeInfo& focusInfo)
199 {
200     INIT_PROXY_CHECK_RETURN();
201     WLOGFI("use Foucus window info proxy");
202 
203     auto wmsProxy = GetWindowManagerServiceProxy();
204     CHECK_PROXY_RETURN_IF_NULL(wmsProxy);
205     wmsProxy->GetFocusWindowInfo(focusInfo);
206 }
207 
GetWindowModeType(WindowModeType & windowModeType)208 WMError WindowAdapterLite::GetWindowModeType(WindowModeType& windowModeType)
209 {
210     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
211     WLOGFD("get window mode type");
212 
213     auto wmsProxy = GetWindowManagerServiceProxy();
214     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
215     return wmsProxy->GetWindowModeType(windowModeType);
216 }
217 
GetMainWindowInfos(int32_t topNum,std::vector<MainWindowInfo> & topNInfo)218 WMError WindowAdapterLite::GetMainWindowInfos(int32_t topNum, std::vector<MainWindowInfo>& topNInfo)
219 {
220     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
221     TLOGD(WmsLogTag::WMS_MAIN, "get top main window info");
222 
223     auto wmsProxy = GetWindowManagerServiceProxy();
224     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
225     return wmsProxy->GetMainWindowInfos(topNum, topNInfo);
226 }
227 
GetAllMainWindowInfos(std::vector<MainWindowInfo> & infos)228 WMError WindowAdapterLite::GetAllMainWindowInfos(std::vector<MainWindowInfo>& infos)
229 {
230     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
231     TLOGD(WmsLogTag::WMS_MAIN, "get all main window info");
232 
233     auto wmsProxy = GetWindowManagerServiceProxy();
234     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
235     return wmsProxy->GetAllMainWindowInfos(infos);
236 }
237 
ClearMainSessions(const std::vector<int32_t> & persistentIds)238 WMError WindowAdapterLite::ClearMainSessions(const std::vector<int32_t>& persistentIds)
239 {
240     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
241     TLOGD(WmsLogTag::WMS_MAIN, "clear main sessions.");
242 
243     auto wmsProxy = GetWindowManagerServiceProxy();
244     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
245     std::vector<int32_t> clearFailedIds;
246     return wmsProxy->ClearMainSessions(persistentIds, clearFailedIds);
247 }
248 
ClearMainSessions(const std::vector<int32_t> & persistentIds,std::vector<int32_t> & clearFailedIds)249 WMError WindowAdapterLite::ClearMainSessions(const std::vector<int32_t>& persistentIds,
250     std::vector<int32_t>& clearFailedIds)
251 {
252     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
253     TLOGD(WmsLogTag::WMS_MAIN, "clear main sessions with failed ids.");
254 
255     auto wmsProxy = GetWindowManagerServiceProxy();
256     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
257     return wmsProxy->ClearMainSessions(persistentIds, clearFailedIds);
258 }
259 
RaiseWindowToTop(int32_t persistentId)260 WMError WindowAdapterLite::RaiseWindowToTop(int32_t persistentId)
261 {
262     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
263 
264     auto wmsProxy = GetWindowManagerServiceProxy();
265     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
266     return static_cast<WMError>(wmsProxy->RaiseWindowToTop(persistentId));
267 }
268 
RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc & callbackFunc)269 WMError WindowAdapterLite::RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc)
270 {
271     TLOGD(WmsLogTag::WMS_MAIN, "register listener");
272     return SessionManagerLite::GetInstance().RegisterWMSConnectionChangedListener(callbackFunc);
273 }
274 
GetWindowStyleType(WindowStyleType & windowStyleType)275 WMError WindowAdapterLite::GetWindowStyleType(WindowStyleType& windowStyleType)
276 {
277     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
278     auto wmsProxy = GetWindowManagerServiceProxy();
279     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
280     return wmsProxy->GetWindowStyleType(windowStyleType);
281 }
282 
GetWindowManagerServiceProxy() const283 sptr<IWindowManagerLite> WindowAdapterLite::GetWindowManagerServiceProxy() const
284 {
285     std::lock_guard<std::mutex> lock(mutex_);
286     return windowManagerServiceProxy_;
287 }
288 
TerminateSessionByPersistentId(int32_t persistentId)289 WMError WindowAdapterLite::TerminateSessionByPersistentId(int32_t persistentId)
290 {
291     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
292 
293     auto wmsProxy = GetWindowManagerServiceProxy();
294     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
295     return wmsProxy->TerminateSessionByPersistentId(persistentId);
296 }
297 
GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>> & infos)298 WMError WindowAdapterLite::GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos)
299 {
300     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
301 
302     auto wmsProxy = GetWindowManagerServiceProxy();
303     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
304     return wmsProxy->GetAccessibilityWindowInfo(infos);
305 }
306 
CloseTargetFloatWindow(const std::string & bundleName)307 WMError WindowAdapterLite::CloseTargetFloatWindow(const std::string& bundleName)
308 {
309     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
310     auto wmsProxy = GetWindowManagerServiceProxy();
311     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
312     return wmsProxy->CloseTargetFloatWindow(bundleName);
313 }
314 
CloseTargetPiPWindow(const std::string & bundleName)315 WMError WindowAdapterLite::CloseTargetPiPWindow(const std::string& bundleName)
316 {
317     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
318     auto wmsProxy = GetWindowManagerServiceProxy();
319     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
320     return wmsProxy->CloseTargetPiPWindow(bundleName);
321 }
322 
GetCurrentPiPWindowInfo(std::string & bundleName)323 WMError WindowAdapterLite::GetCurrentPiPWindowInfo(std::string& bundleName)
324 {
325     INIT_PROXY_CHECK_RETURN(WMError::WM_ERROR_SAMGR);
326     auto wmsProxy = GetWindowManagerServiceProxy();
327     CHECK_PROXY_RETURN_ERROR_IF_NULL(wmsProxy, WMError::WM_ERROR_SAMGR);
328     return wmsProxy->GetCurrentPiPWindowInfo(bundleName);
329 }
330 } // namespace Rosen
331 } // namespace OHOS
332