• 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 "mock_session_manager_service.h"
17 
18 #include <cstdint>
19 #include <fcntl.h>
20 #include <securec.h>
21 #include <unistd.h>
22 
23 #include <bundle_mgr_interface.h>
24 #include <system_ability_definition.h>
25 #include <cinttypes>
26 #include <csignal>
27 #include <iomanip>
28 #include <ipc_skeleton.h>
29 #include <iservice_registry.h>
30 #include <map>
31 #include <sstream>
32 
33 #include "window_manager_hilog.h"
34 #include "unique_fd.h"
35 #include "parameters.h"
36 #include "root_scene.h"
37 #include "string_ex.h"
38 #include "wm_common.h"
39 #include "ws_common.h"
40 #include "scene_board_judgement.h"
41 #include "scene_session_manager_interface.h"
42 #include "session_manager_service_interface.h"
43 #include "screen_session_manager_lite.h"
44 #include "common/include/session_permission.h"
45 
46 #define PATH_LEN 1024
47 #define O_RDWR   02
48 
49 namespace OHOS {
50 namespace Rosen {
51 namespace {
52 
53 const std::u16string DEFAULT_USTRING = u"error";
54 const char DEFAULT_STRING[] = "error";
55 const std::string ARG_DUMP_HELP = "-h";
56 const std::string ARG_DUMP_ALL = "-a";
57 const std::string ARG_DUMP_WINDOW = "-w";
58 const std::string KEY_SCENE_BOARD_TEST_ENABLE = "persist.scb.testmode.enable";
59 const std::string SCENE_BOARD_BUNDLE_NAME = "com.ohos.sceneboard";
60 const std::string TEST_MODULE_NAME_SUFFIX = "_test";
61 const std::string BOOTEVENT_WMS_READY = "bootevent.wms.ready";
62 
GetUserIdByCallingUid()63 inline int32_t GetUserIdByCallingUid()
64 {
65     int32_t uid = IPCSkeleton::GetCallingUid();
66     TLOGD(WmsLogTag::WMS_MULTI_USER, "get calling uid(%{public}d)", uid);
67     if (uid <= INVALID_UID) {
68         TLOGE(WmsLogTag::WMS_MULTI_USER, "uid is illegal: %{public}d", uid);
69         return INVALID_USER_ID;
70     }
71     return GetUserIdByUid(uid);
72 }
73 } // namespace
74 
75 
76 class ClientListenerDeathRecipient : public IRemoteObject::DeathRecipient {
77 public:
ClientListenerDeathRecipient(int32_t userId,int32_t pid,bool isLite)78     explicit ClientListenerDeathRecipient(int32_t userId, int32_t pid, bool isLite)
79         : userId_(userId), pid_(pid), isLite_(isLite)
80     {}
81 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)82     void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override
83     {
84         TLOGD(WmsLogTag::WMS_RECOVER, "Client died, pid = %{public}d, isLite = %{public}d", pid_, isLite_);
85         if (isLite_) {
86             MockSessionManagerService::GetInstance().UnregisterSMSLiteRecoverListener(userId_, pid_);
87         } else {
88             MockSessionManagerService::GetInstance().UnregisterSMSRecoverListener(userId_, pid_);
89         }
90     }
91 
92 private:
93     int32_t userId_;
94     int32_t pid_;
95     bool isLite_;
96 };
97 
WM_IMPLEMENT_SINGLE_INSTANCE(MockSessionManagerService)98 WM_IMPLEMENT_SINGLE_INSTANCE(MockSessionManagerService)
99 MockSessionManagerService::SMSDeathRecipient::SMSDeathRecipient(int32_t userId)
100     : userId_(userId), screenId_(DEFAULT_SCREEN_ID)
101 {}
102 
OnRemoteDied(const wptr<IRemoteObject> & object)103 void MockSessionManagerService::SMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
104 {
105     TLOGI(WmsLogTag::WMS_MULTI_USER, "Scb died with userId_=%{public}d, screenId_=%{public}d", userId_, screenId_);
106     MockSessionManagerService::GetInstance().NotifyWMSConnectionChanged(userId_, screenId_, false);
107     MockSessionManagerService::GetInstance().RemoveSMSDeathRecipientByUserId(userId_);
108     MockSessionManagerService::GetInstance().RemoveSessionManagerServiceByUserId(userId_);
109     auto sessionManagerService = object.promote();
110     if (!sessionManagerService) {
111         TLOGE(WmsLogTag::WMS_MULTI_USER, "sessionManagerService is null");
112         return;
113     }
114 
115     if (IsSceneBoardTestMode()) {
116         TLOGI(WmsLogTag::WMS_MULTI_USER, "SceneBoard is testing, do not kill foundation.");
117         return;
118     }
119     TLOGW(WmsLogTag::WMS_MULTI_USER, "SessionManagerService died!");
120 }
121 
SetScreenId(int32_t screenId)122 void MockSessionManagerService::SMSDeathRecipient::SetScreenId(int32_t screenId)
123 {
124     TLOGI(WmsLogTag::WMS_MULTI_USER, "screenId=%{public}d", screenId);
125     screenId_ = screenId;
126 }
127 
MockSessionManagerService()128 MockSessionManagerService::MockSessionManagerService()
129     : SystemAbility(WINDOW_MANAGER_SERVICE_ID, true), currentWMSUserId_(INVALID_USER_ID),
130       currentScreenId_(DEFAULT_SCREEN_ID)
131 {}
132 
RegisterMockSessionManagerService()133 bool MockSessionManagerService::RegisterMockSessionManagerService()
134 {
135     static bool isRegistered = false;
136     static bool isPublished = false;
137     if (isRegistered && isPublished) {
138         TLOGW(WmsLogTag::WMS_MULTI_USER, "WindowManagerService SA has already been registered and published");
139         return true;
140     }
141     if (!isRegistered) {
142         isRegistered = SystemAbility::MakeAndRegisterAbility(this);
143         if (isRegistered) {
144             TLOGI(WmsLogTag::WMS_MULTI_USER, "Successfully registered WindowManagerService SA");
145         } else {
146             TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to register WindowManagerService SA");
147         }
148     }
149     if (!isPublished) {
150         isPublished = Publish(this);
151         if (isPublished) {
152             TLOGI(WmsLogTag::WMS_MULTI_USER, "Successfully published WindowManagerService SA");
153         } else {
154             TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to publish WindowManagerService SA");
155         }
156     }
157     return isRegistered && isPublished;
158 }
159 
OnStart()160 void MockSessionManagerService::OnStart()
161 {
162     WLOGFD("OnStart begin");
163 }
164 
Str16ToStr8(const std::u16string & str)165 static std::string Str16ToStr8(const std::u16string& str)
166 {
167     if (str == DEFAULT_USTRING) {
168         return DEFAULT_STRING;
169     }
170     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(DEFAULT_STRING);
171     std::string result = convert.to_bytes(str);
172     return result == DEFAULT_STRING ? "" : result;
173 }
174 
Dump(int fd,const std::vector<std::u16string> & args)175 int MockSessionManagerService::Dump(int fd, const std::vector<std::u16string>& args)
176 {
177     TLOGD(WmsLogTag::DEFAULT, "dump begin fd: %{public}d", fd);
178     if (fd < 0) {
179         return -1;
180     }
181     (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash
182     std::vector<std::string> params;
183     for (auto& arg : args) {
184         params.emplace_back(Str16ToStr8(arg));
185     }
186 
187     std::string dumpInfo;
188     if (params.empty()) {
189         ShowHelpInfo(dumpInfo);
190     } else if (params.size() == 1 && params[0] == ARG_DUMP_HELP) { // 1: params num
191         ShowHelpInfo(dumpInfo);
192     } else {
193         int errCode = DumpSessionInfo(params, dumpInfo);
194         if (errCode != 0) {
195             ShowIllegalArgsInfo(dumpInfo);
196         }
197     }
198     int ret = write(fd, dumpInfo.c_str(), dumpInfo.length());
199     if (ret < 0) {
200         WLOGFE("write error");
201         return -1; // WMError::WM_ERROR_INVALID_OPERATION;
202     }
203     TLOGD(WmsLogTag::DEFAULT, "dump end");
204     return 0;
205 }
206 
SetSessionManagerService(const sptr<IRemoteObject> & sessionManagerService)207 bool MockSessionManagerService::SetSessionManagerService(const sptr<IRemoteObject>& sessionManagerService)
208 {
209     if (!sessionManagerService) {
210         WLOGFE("sessionManagerService is nullptr");
211         return false;
212     }
213     currentWMSUserId_ = GetUserIdByCallingUid();
214     if (currentWMSUserId_ <= INVALID_USER_ID) {
215         TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", currentWMSUserId_);
216         return false;
217     }
218     {
219         std::unique_lock<std::shared_mutex> lock(sessionManagerServiceMapLock_);
220         sessionManagerServiceMap_[currentWMSUserId_] = sessionManagerService;
221     }
222     RecoverSCBSnapshotSkipByUserId(currentWMSUserId_);
223     auto smsDeathRecipient = GetSMSDeathRecipientByUserId(currentWMSUserId_);
224     if (!smsDeathRecipient) {
225         smsDeathRecipient = new SMSDeathRecipient(currentWMSUserId_);
226         std::unique_lock<std::shared_mutex> lock(smsDeathRecipientMapLock_);
227         if (!smsDeathRecipient) {
228             TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to create death Recipient ptr smsDeathRecipient");
229             return false;
230         }
231         smsDeathRecipientMap_[currentWMSUserId_] = smsDeathRecipient;
232     }
233     if (sessionManagerService->IsProxyObject() && !sessionManagerService->AddDeathRecipient(smsDeathRecipient)) {
234         TLOGE(WmsLogTag::WMS_MULTI_USER, "Failed to add death recipient");
235         return false;
236     }
237     RegisterMockSessionManagerService();
238     TLOGI(WmsLogTag::WMS_MULTI_USER, "sessionManagerService set success!");
239     system::SetParameter(BOOTEVENT_WMS_READY.c_str(), "true");
240     GetSceneSessionManager();
241 
242     return true;
243 }
244 
GetSMSDeathRecipientByUserId(int32_t userId)245 sptr<MockSessionManagerService::SMSDeathRecipient> MockSessionManagerService::GetSMSDeathRecipientByUserId(
246     int32_t userId)
247 {
248     std::shared_lock<std::shared_mutex> lock(smsDeathRecipientMapLock_);
249     auto iter = smsDeathRecipientMap_.find(userId);
250     if (iter != smsDeathRecipientMap_.end()) {
251         TLOGD(WmsLogTag::WMS_MULTI_USER, "Get SMS death recipient with userId=%{public}d", userId);
252         return iter->second;
253     } else {
254         TLOGW(WmsLogTag::WMS_MULTI_USER, "Get SMS death recipient failed with userId=%{public}d", userId);
255         return nullptr;
256     }
257 }
258 
RemoveSMSDeathRecipientByUserId(int32_t userId)259 void MockSessionManagerService::RemoveSMSDeathRecipientByUserId(int32_t userId)
260 {
261     TLOGI(WmsLogTag::WMS_MULTI_USER, "userId: %{public}d", userId);
262     auto sessionManagerService = GetSessionManagerServiceByUserId(userId);
263     std::unique_lock<std::shared_mutex> lock(smsDeathRecipientMapLock_);
264     auto iter = smsDeathRecipientMap_.find(userId);
265     if (iter != smsDeathRecipientMap_.end() && iter->second) {
266         if (sessionManagerService != nullptr) {
267             sessionManagerService->RemoveDeathRecipient(iter->second);
268         }
269     }
270 }
271 
GetSessionManagerService(sptr<IRemoteObject> & sessionManagerService)272 ErrCode MockSessionManagerService::GetSessionManagerService(sptr<IRemoteObject>& sessionManagerService)
273 {
274     int32_t clientUserId = GetUserIdByCallingUid();
275     if (clientUserId <= INVALID_USER_ID) {
276         TLOGE(WmsLogTag::WMS_MULTI_USER, "userId is illegal: %{public}d", clientUserId);
277         return ERR_INVALID_VALUE;
278     }
279     if (clientUserId == SYSTEM_USERID) {
280         TLOGD(WmsLogTag::WMS_MULTI_USER, "System user, return current sessionManagerService with %{public}d",
281               currentWMSUserId_);
282         clientUserId = currentWMSUserId_;
283     }
284     sessionManagerService = GetSessionManagerServiceByUserId(clientUserId);
285     return ERR_OK;
286 }
287 
GetSessionManagerServiceByUserId(int32_t userId)288 sptr<IRemoteObject> MockSessionManagerService::GetSessionManagerServiceByUserId(int32_t userId)
289 {
290     std::shared_lock<std::shared_mutex> lock(sessionManagerServiceMapLock_);
291     auto iter = sessionManagerServiceMap_.find(userId);
292     if (iter != sessionManagerServiceMap_.end()) {
293         TLOGD(WmsLogTag::WMS_MULTI_USER, "Get session manager service success with userId=%{public}d", userId);
294         return iter->second;
295     } else {
296         TLOGE(WmsLogTag::WMS_MULTI_USER, "Get session manager service failed with userId=%{public}d", userId);
297         return nullptr;
298     }
299 }
300 
RemoveSessionManagerServiceByUserId(int32_t userId)301 void MockSessionManagerService::RemoveSessionManagerServiceByUserId(int32_t userId)
302 {
303     TLOGI(WmsLogTag::WMS_MULTI_USER, "userId: %{public}d", userId);
304     std::unique_lock<std::shared_mutex> lock(sessionManagerServiceMapLock_);
305     auto iter = sessionManagerServiceMap_.find(userId);
306     if (iter != sessionManagerServiceMap_.end()) {
307         sessionManagerServiceMap_.erase(iter);
308     }
309 }
310 
NotifySceneBoardAvailable()311 ErrCode MockSessionManagerService::NotifySceneBoardAvailable()
312 {
313     if (!SessionPermission::IsSystemCalling()) {
314         TLOGE(WmsLogTag::WMS_RECOVER, "permission denied");
315         return ERR_PERMISSION_DENIED;
316     }
317     int32_t userId = GetUserIdByCallingUid();
318     if (userId <= INVALID_USER_ID) {
319         TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", userId);
320         return ERR_INVALID_VALUE;
321     }
322     TLOGI(WmsLogTag::WMS_RECOVER, "scene board is available with userId=%{public}d", userId);
323 
324     NotifySceneBoardAvailableToLiteClient(SYSTEM_USERID);
325     NotifySceneBoardAvailableToLiteClient(userId);
326 
327     NotifySceneBoardAvailableToClient(SYSTEM_USERID);
328     NotifySceneBoardAvailableToClient(userId);
329     return ERR_OK;
330 }
331 
RegisterSMSRecoverListener(const sptr<IRemoteObject> & listener)332 ErrCode MockSessionManagerService::RegisterSMSRecoverListener(const sptr<IRemoteObject>& listener)
333 {
334     if (listener == nullptr) {
335         TLOGE(WmsLogTag::WMS_RECOVER, "listener is nullptr");
336         return ERR_INVALID_VALUE;
337     }
338 
339     int32_t clientUserId = GetUserIdByCallingUid();
340     if (clientUserId <= INVALID_USER_ID) {
341         TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId);
342         return ERR_INVALID_VALUE;
343     }
344     int32_t pid = IPCSkeleton::GetCallingRealPid();
345     TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid);
346     sptr<ClientListenerDeathRecipient> clientDeathListener = new ClientListenerDeathRecipient(clientUserId, pid, false);
347     listener->AddDeathRecipient(clientDeathListener);
348     sptr<ISessionManagerServiceRecoverListener> smsListener;
349     {
350         std::unique_lock<std::shared_mutex> lock(smsRecoverListenerLock_);
351         smsListener = iface_cast<ISessionManagerServiceRecoverListener>(listener);
352         smsRecoverListenerMap_[clientUserId][pid] = smsListener;
353     }
354     if (clientUserId != SYSTEM_USERID) {
355         return ERR_WOULD_BLOCK;
356     }
357     bool isWMSConnected = false;
358     {
359         std::lock_guard<std::mutex> lock(wmsConnectionStatusLock_);
360         if (wmsConnectionStatusMap_.find(currentWMSUserId_) != wmsConnectionStatusMap_.end()) {
361             isWMSConnected = wmsConnectionStatusMap_[currentWMSUserId_];
362         }
363     }
364     if (smsListener && isWMSConnected) {
365         auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
366         if (sessionManagerService == nullptr) {
367             TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
368             return ERR_DEAD_OBJECT;
369         }
370         TLOGI(WmsLogTag::WMS_RECOVER, "WMS ready,notify client");
371         smsListener->OnWMSConnectionChanged(currentWMSUserId_, currentScreenId_, true, sessionManagerService);
372     }
373     return ERR_OK;
374 }
375 
GetSMSRecoverListenerMap(int32_t userId)376 std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* MockSessionManagerService::GetSMSRecoverListenerMap(
377     int32_t userId)
378 {
379     auto iter = smsRecoverListenerMap_.find(userId);
380     if (iter != smsRecoverListenerMap_.end()) {
381         return &iter->second;
382     }
383     return nullptr;
384 }
385 
UnregisterSMSRecoverListener()386 ErrCode MockSessionManagerService::UnregisterSMSRecoverListener()
387 {
388     int32_t clientUserId = GetUserIdByCallingUid();
389     if (clientUserId <= INVALID_USER_ID) {
390         TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId);
391         return ERR_INVALID_VALUE;
392     }
393     int32_t pid = IPCSkeleton::GetCallingRealPid();
394     TLOGD(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid);
395     UnregisterSMSRecoverListener(clientUserId, pid);
396     return ERR_OK;
397 }
398 
UnregisterSMSRecoverListener(int32_t userId,int32_t pid)399 void MockSessionManagerService::UnregisterSMSRecoverListener(int32_t userId, int32_t pid)
400 {
401     TLOGD(WmsLogTag::WMS_RECOVER, "userId = %{public}d, pid = %{public}d", userId, pid);
402     std::unique_lock<std::shared_mutex> lock(smsRecoverListenerLock_);
403     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsRecoverListenerMap =
404         GetSMSRecoverListenerMap(userId);
405     if (!smsRecoverListenerMap) {
406         TLOGW(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null");
407         return;
408     }
409     auto iter = smsRecoverListenerMap->find(pid);
410     if (iter != smsRecoverListenerMap->end()) {
411         smsRecoverListenerMap->erase(iter);
412     }
413 }
414 
NotifySceneBoardAvailableToClient(int32_t userId)415 void MockSessionManagerService::NotifySceneBoardAvailableToClient(int32_t userId)
416 {
417     std::shared_lock<std::shared_mutex> lock(smsRecoverListenerLock_);
418     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsRecoverListenerMap =
419         GetSMSRecoverListenerMap(userId);
420     if (!smsRecoverListenerMap) {
421         TLOGW(WmsLogTag::WMS_RECOVER, "smsRecoverListenerMap is null");
422         return;
423     }
424     TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, userId,
425         static_cast<uint64_t>(smsRecoverListenerMap->size()));
426     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
427     if (sessionManagerService == nullptr) {
428         TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
429         return;
430     }
431     for (auto& iter : *smsRecoverListenerMap) {
432         if (iter.second != nullptr) {
433             TLOGI(WmsLogTag::WMS_RECOVER, "Call OnSessionManagerServiceRecover pid = %{public}d", iter.first);
434             iter.second->OnSessionManagerServiceRecover(sessionManagerService);
435         }
436     }
437 }
438 
RegisterSMSLiteRecoverListener(const sptr<IRemoteObject> & listener)439 ErrCode MockSessionManagerService::RegisterSMSLiteRecoverListener(const sptr<IRemoteObject>& listener)
440 {
441     if (listener == nullptr) {
442         TLOGE(WmsLogTag::WMS_RECOVER, "Lite listener is nullptr");
443         return ERR_INVALID_VALUE;
444     }
445     int32_t clientUserId = GetUserIdByCallingUid();
446     if (clientUserId <= INVALID_USER_ID) {
447         TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId);
448         return ERR_INVALID_VALUE;
449     }
450     int32_t pid = IPCSkeleton::GetCallingRealPid();
451     TLOGI(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid);
452     sptr<ClientListenerDeathRecipient> clientDeathListener = new ClientListenerDeathRecipient(clientUserId, pid, true);
453     listener->AddDeathRecipient(clientDeathListener);
454     sptr<ISessionManagerServiceRecoverListener> smsListener;
455     {
456         std::unique_lock<std::shared_mutex> lock(smsLiteRecoverListenerLock_);
457         smsListener = iface_cast<ISessionManagerServiceRecoverListener>(listener);
458         smsLiteRecoverListenerMap_[clientUserId][pid] = smsListener;
459     }
460     if (clientUserId != SYSTEM_USERID) {
461         return ERR_WOULD_BLOCK;
462     }
463     bool isWMSConnected = false;
464     {
465         std::lock_guard<std::mutex> lock(wmsConnectionStatusLock_);
466         if (wmsConnectionStatusMap_.find(currentWMSUserId_) != wmsConnectionStatusMap_.end()) {
467             isWMSConnected = wmsConnectionStatusMap_[currentWMSUserId_];
468         }
469     }
470     if (smsListener && isWMSConnected) {
471         auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
472         if (sessionManagerService == nullptr) {
473             TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
474             return ERR_DEAD_OBJECT;
475         }
476         TLOGD(WmsLogTag::WMS_MULTI_USER, "Lite wms is already connected, notify client");
477         smsListener->OnWMSConnectionChanged(currentWMSUserId_, currentScreenId_, true, sessionManagerService);
478     }
479     return ERR_OK;
480 }
481 
GetSMSLiteRecoverListenerMap(int32_t userId)482 std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* MockSessionManagerService::GetSMSLiteRecoverListenerMap(
483     int32_t userId)
484 {
485     auto iter = smsLiteRecoverListenerMap_.find(userId);
486     if (iter != smsLiteRecoverListenerMap_.end()) {
487         return &iter->second;
488     }
489     return nullptr;
490 }
491 
UnregisterSMSLiteRecoverListener()492 ErrCode MockSessionManagerService::UnregisterSMSLiteRecoverListener()
493 {
494     int32_t clientUserId = GetUserIdByCallingUid();
495     if (clientUserId <= INVALID_USER_ID) {
496         TLOGE(WmsLogTag::WMS_RECOVER, "userId is illegal: %{public}d", clientUserId);
497         return ERR_INVALID_VALUE;
498     }
499     int32_t pid = IPCSkeleton::GetCallingRealPid();
500     TLOGD(WmsLogTag::WMS_RECOVER, "clientUserId = %{public}d, pid = %{public}d", clientUserId, pid);
501     UnregisterSMSLiteRecoverListener(clientUserId, pid);
502     return ERR_OK;
503 }
504 
UnregisterSMSLiteRecoverListener(int32_t userId,int32_t pid)505 void MockSessionManagerService::UnregisterSMSLiteRecoverListener(int32_t userId, int32_t pid)
506 {
507     TLOGD(WmsLogTag::WMS_RECOVER, "userId = %{public}d, pid = %{public}d", userId, pid);
508     std::unique_lock<std::shared_mutex> lock(smsLiteRecoverListenerLock_);
509     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsLiteRecoverListenerMap =
510         GetSMSLiteRecoverListenerMap(userId);
511     if (!smsLiteRecoverListenerMap) {
512         return;
513     }
514     auto iter = smsLiteRecoverListenerMap->find(pid);
515     if (iter != smsLiteRecoverListenerMap->end()) {
516         smsLiteRecoverListenerMap->erase(iter);
517     }
518 }
519 
NotifySceneBoardAvailableToLiteClient(int32_t userId)520 void MockSessionManagerService::NotifySceneBoardAvailableToLiteClient(int32_t userId)
521 {
522     std::shared_lock<std::shared_mutex> lock(smsLiteRecoverListenerLock_);
523     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsLiteRecoverListenerMap =
524         GetSMSLiteRecoverListenerMap(userId);
525     if (!smsLiteRecoverListenerMap) {
526         return;
527     }
528     TLOGI(WmsLogTag::WMS_RECOVER, "userId=%{public}d, Remote process count = %{public}" PRIu64, userId,
529         static_cast<uint64_t>(smsLiteRecoverListenerMap->size()));
530     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
531     if (sessionManagerService == nullptr) {
532         TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
533         return;
534     }
535     for (auto& iter : *smsLiteRecoverListenerMap) {
536         if (iter.second != nullptr) {
537             TLOGI(WmsLogTag::WMS_RECOVER,
538                 "Call OnSessionManagerServiceRecover Lite pid = %{public}d, ref count = %{public}" PRId32, iter.first,
539                 iter.second->GetSptrRefCount());
540             iter.second->OnSessionManagerServiceRecover(sessionManagerService);
541         }
542     }
543 }
544 
NotifyWMSConnected(int32_t userId,int32_t screenId,bool isColdStart)545 void MockSessionManagerService::NotifyWMSConnected(int32_t userId, int32_t screenId, bool isColdStart)
546 {
547     TLOGI(WmsLogTag::WMS_MULTI_USER, "userId = %{public}d, screenId = %{public}d, isColdStart = %{public}d", userId,
548         screenId, isColdStart);
549     currentScreenId_ = screenId;
550     currentWMSUserId_ = userId;
551     auto smsDeathRecipient = GetSMSDeathRecipientByUserId(currentWMSUserId_);
552     if (smsDeathRecipient != nullptr) {
553         smsDeathRecipient->SetScreenId(screenId);
554     }
555     if (!isColdStart) {
556         TLOGI(WmsLogTag::WMS_MULTI_USER, "User switched");
557         GetSceneSessionManager();
558     }
559     NotifyWMSConnectionChanged(userId, screenId, true);
560 }
561 
NotifyWMSConnectionChanged(int32_t wmsUserId,int32_t screenId,bool isConnected)562 void MockSessionManagerService::NotifyWMSConnectionChanged(int32_t wmsUserId, int32_t screenId, bool isConnected)
563 {
564     TLOGI(WmsLogTag::WMS_MULTI_USER, "wmsUserId = %{public}d, isConnected = %{public}d", wmsUserId, isConnected);
565     {
566         std::lock_guard<std::mutex> lock(wmsConnectionStatusLock_);
567         wmsConnectionStatusMap_[wmsUserId] = isConnected;
568     }
569     NotifyWMSConnectionChangedToLiteClient(wmsUserId, screenId, isConnected);
570     NotifyWMSConnectionChangedToClient(wmsUserId, screenId, isConnected);
571 }
572 
NotifyWMSConnectionChangedToClient(int32_t wmsUserId,int32_t screenId,bool isConnected)573 void MockSessionManagerService::NotifyWMSConnectionChangedToClient(
574     int32_t wmsUserId, int32_t screenId, bool isConnected)
575 {
576     std::shared_lock<std::shared_mutex> lock(smsRecoverListenerLock_);
577     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsRecoverListenerMap =
578         GetSMSRecoverListenerMap(SYSTEM_USERID);
579     if (!smsRecoverListenerMap) {
580         TLOGW(WmsLogTag::WMS_MULTI_USER, "smsRecoverListenerMap is null");
581         return;
582     }
583     TLOGD(WmsLogTag::WMS_MULTI_USER,
584           "wmsUserId = %{public}d, isConnected = %{public}d, remote process count = %{public}zu",
585           wmsUserId, isConnected, smsRecoverListenerMap->size());
586     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
587     if (sessionManagerService == nullptr) {
588         TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
589         return;
590     }
591     for (auto& iter : *smsRecoverListenerMap) {
592         if (iter.second != nullptr) {
593             TLOGD(WmsLogTag::WMS_MULTI_USER, "Call OnWMSConnectionChanged pid = %{public}d", iter.first);
594             iter.second->OnWMSConnectionChanged(wmsUserId, screenId, isConnected, sessionManagerService);
595         }
596     }
597 }
598 
NotifyWMSConnectionChangedToLiteClient(int32_t wmsUserId,int32_t screenId,bool isConnected)599 void MockSessionManagerService::NotifyWMSConnectionChangedToLiteClient(
600     int32_t wmsUserId, int32_t screenId, bool isConnected)
601 {
602     std::shared_lock<std::shared_mutex> lock(smsLiteRecoverListenerLock_);
603     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* smsLiteRecoverListenerMap =
604         GetSMSLiteRecoverListenerMap(SYSTEM_USERID);
605     if (!smsLiteRecoverListenerMap) {
606         return;
607     }
608     TLOGD(WmsLogTag::WMS_MULTI_USER, "wmsUserId = %{public}d, isConnected = %{public}d", wmsUserId, isConnected);
609     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
610     if (sessionManagerService == nullptr) {
611         TLOGE(WmsLogTag::WMS_RECOVER, "SessionManagerService is null");
612         return;
613     }
614     for (auto& iter : *smsLiteRecoverListenerMap) {
615         if (iter.second != nullptr) {
616             TLOGD(WmsLogTag::WMS_MULTI_USER,
617                   "Call OnWMSConnectionChanged Lite pid = %{public}d, ref count = %{public}d",
618                   iter.first, iter.second->GetSptrRefCount());
619             iter.second->OnWMSConnectionChanged(wmsUserId, screenId, isConnected, sessionManagerService);
620         }
621     }
622 }
623 
GetScreenSessionManagerLite(sptr<IRemoteObject> & screenSessionManagerLite)624 ErrCode MockSessionManagerService::GetScreenSessionManagerLite(sptr<IRemoteObject>& screenSessionManagerLite)
625 {
626     if (screenSessionManager_) {
627         screenSessionManagerLite = screenSessionManager_;
628         return ERR_OK;
629     }
630     screenSessionManager_ = ScreenSessionManagerLite::GetInstance().AsObject();
631     screenSessionManagerLite = screenSessionManager_;
632     return ERR_OK;
633 }
634 
ShowIllegalArgsInfo(std::string & dumpInfo)635 void MockSessionManagerService::ShowIllegalArgsInfo(std::string& dumpInfo)
636 {
637     dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
638 }
639 
GetSceneSessionManager()640 sptr<IRemoteObject> MockSessionManagerService::GetSceneSessionManager()
641 {
642     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
643     if (sessionManagerService == nullptr) {
644         WLOGFE("SessionManagerService is null");
645         return nullptr;
646     }
647     sptr<ISessionManagerService> sessionManagerServiceProxy = iface_cast<ISessionManagerService>(sessionManagerService);
648     if (!sessionManagerServiceProxy) {
649         WLOGFE("sessionManagerServiceProxy is nullptr");
650         return nullptr;
651     }
652     sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy->GetSceneSessionManager();
653     if (!remoteObject) {
654         WLOGFW("Get scene session manager proxy failed, scene session manager service is null");
655         return sptr<IRemoteObject>(nullptr);
656     }
657     sceneSessionManager_ = remoteObject;
658     return sceneSessionManager_;
659 }
660 
DumpSessionInfo(const std::vector<std::string> & args,std::string & dumpInfo)661 int MockSessionManagerService::DumpSessionInfo(const std::vector<std::string>& args, std::string& dumpInfo)
662 {
663     if (args.empty()) {
664         return -1;  // WMError::WM_ERROR_INVALID_PARAM;
665     }
666     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
667     if (sessionManagerService == nullptr) {
668         WLOGFE("sessionManagerService is nullptr");
669         return -1;
670     }
671     if (!sceneSessionManager_) {
672         WLOGFW("Get scene session manager ...");
673         GetSceneSessionManager();
674         if (!sceneSessionManager_) {
675             WLOGFW("Get scene session manager proxy failed, nullptr");
676             return -1;
677         }
678     }
679     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
680     WSError ret = sceneSessionManagerProxy->GetSessionDumpInfo(args, dumpInfo);
681     if (ret != WSError::WS_OK) {
682         WLOGFD("sessionManagerService set success!");
683         return -1;
684     }
685     return 0; // WMError::WM_OK;
686 }
687 
ShowHelpInfo(std::string & dumpInfo)688 void MockSessionManagerService::ShowHelpInfo(std::string& dumpInfo)
689 {
690     dumpInfo.append("Usage:\n")
691         .append(" -h                             ")
692         .append("|help text for the tool\n")
693         .append(" -a                             ")
694         .append("|dump all window information in the system\n")
695         .append(" -w {window id} [ArkUI Option]  ")
696         .append("|dump specified window information\n")
697         .append(" ------------------------------------[ArkUI Option]------------------------------------ \n");
698     ShowAceDumpHelp(dumpInfo);
699 }
700 
ShowAceDumpHelp(std::string & dumpInfo)701 void MockSessionManagerService::ShowAceDumpHelp(std::string& dumpInfo)
702 {
703 }
704 
IsSceneBoardTestMode()705 bool MockSessionManagerService::SMSDeathRecipient::IsSceneBoardTestMode()
706 {
707     if (!OHOS::system::GetBoolParameter(KEY_SCENE_BOARD_TEST_ENABLE, false)) {
708         WLOGFD("SceneBoard testmode is disabled.");
709         return false;
710     }
711     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
712     if (systemAbilityMgr == nullptr) {
713         WLOGFE("Failed to get SystemAbilityManager.");
714         return false;
715     }
716 
717     auto bmsObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
718     if (bmsObj == nullptr) {
719         WLOGFE("Failed to get BundleManagerService.");
720         return false;
721     }
722     sptr<AppExecFwk::IBundleMgr> bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(bmsObj);
723     AppExecFwk::BundleInfo bundleInfo;
724     int uid = IPCSkeleton::GetCallingUid();
725     int userId = uid / 200000;
726     bool result = bundleMgr_->GetBundleInfo(SCENE_BOARD_BUNDLE_NAME,
727         AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
728     if (!result) {
729         WLOGFE("Failed to query bundleInfo, userId:%{public}d", userId);
730         return true;
731     }
732     auto hapModulesList = bundleInfo.hapModuleInfos;
733     if (hapModulesList.empty()) {
734         WLOGFE("hapModulesList is empty");
735         return false;
736     }
737     std::string suffix = TEST_MODULE_NAME_SUFFIX;
738     for (auto hapModule: hapModulesList) {
739         std::string moduleName = hapModule.moduleName;
740         if (moduleName.length() < suffix.length()) {
741             continue;
742         }
743         if (moduleName.compare(moduleName.length() - suffix.length(), suffix.length(), suffix) == 0) {
744             WLOGFI("Found test module name: %{public}s", moduleName.c_str());
745             return true;
746         }
747     }
748     return false;
749 }
750 
GetProcessSurfaceNodeIdByPersistentId(const int32_t pid,const std::vector<uint64_t> & windowIdList,std::vector<uint64_t> & surfaceNodeIds)751 void MockSessionManagerService::GetProcessSurfaceNodeIdByPersistentId(const int32_t pid,
752     const std::vector<uint64_t>& windowIdList, std::vector<uint64_t>& surfaceNodeIds)
753 {
754     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
755     if (sessionManagerService == nullptr) {
756         WLOGFE("sessionManagerService is nullptr");
757         return;
758     }
759     if (!sceneSessionManager_) {
760         WLOGFW("Get scene session manager ...");
761         GetSceneSessionManager();
762         if (!sceneSessionManager_) {
763             WLOGFW("Get scene session manager proxy failed, nullptr");
764             return;
765         }
766     }
767     if (windowIdList.empty()) {
768         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "windowIdList is null, no need to get surfaceNodeId");
769         return;
770     }
771     std::vector<int32_t> persistentIds;
772     for (uint64_t id : windowIdList) {
773         persistentIds.push_back(static_cast<int32_t>(id));
774     }
775     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
776     WMError ret = sceneSessionManagerProxy->GetProcessSurfaceNodeIdByPersistentId(
777         pid, persistentIds, surfaceNodeIds);
778     if (ret != WMError::WM_OK) {
779         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "Get process surfaceNodeId by persistentId failed!");
780     }
781 }
782 
AddSkipSelfWhenShowOnVirtualScreenList(const std::vector<int32_t> & persistentIds)783 void MockSessionManagerService::AddSkipSelfWhenShowOnVirtualScreenList(const std::vector<int32_t>& persistentIds)
784 {
785     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
786     if (sessionManagerService == nullptr) {
787         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr");
788         return;
789     }
790     if (!sceneSessionManager_) {
791         GetSceneSessionManager();
792         if (!sceneSessionManager_) {
793             TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr");
794             return;
795         }
796     }
797     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
798     if (sceneSessionManagerProxy == nullptr) {
799         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr");
800         return;
801     }
802     WMError ret = sceneSessionManagerProxy->AddSkipSelfWhenShowOnVirtualScreenList(persistentIds);
803     if (ret != WMError::WM_OK) {
804         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "add virtual screen black list failed!");
805     }
806 }
807 
RemoveSkipSelfWhenShowOnVirtualScreenList(const std::vector<int32_t> & persistentIds)808 void MockSessionManagerService::RemoveSkipSelfWhenShowOnVirtualScreenList(const std::vector<int32_t>& persistentIds)
809 {
810     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
811     if (sessionManagerService == nullptr) {
812         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr");
813         return;
814     }
815     if (!sceneSessionManager_) {
816         GetSceneSessionManager();
817         if (!sceneSessionManager_) {
818             TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr");
819             return;
820         }
821     }
822     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
823     if (sceneSessionManagerProxy == nullptr) {
824         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr");
825         return;
826     }
827     WMError ret = sceneSessionManagerProxy->RemoveSkipSelfWhenShowOnVirtualScreenList(persistentIds);
828     if (ret != WMError::WM_OK) {
829         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "remove virtual screen black list failed!");
830     }
831 }
832 
SetScreenPrivacyWindowTagSwitch(uint64_t screenId,const std::vector<std::string> & privacyWindowTags,bool enable)833 void MockSessionManagerService::SetScreenPrivacyWindowTagSwitch(
834     uint64_t screenId, const std::vector<std::string>& privacyWindowTags, bool enable)
835 {
836     if (privacyWindowTags.empty()) {
837         TLOGI(WmsLogTag::WMS_ATTRIBUTE, "PrivacyWindowTags is empty");
838         return;
839     }
840     auto sessionManagerService = GetSessionManagerServiceByUserId(currentWMSUserId_);
841     if (sessionManagerService == nullptr) {
842         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerService is nullptr");
843         return;
844     }
845     if (!sceneSessionManager_) {
846         GetSceneSessionManager();
847         if (!sceneSessionManager_) {
848             TLOGW(WmsLogTag::WMS_ATTRIBUTE, "get scene session manager proxy failed, nullptr");
849             return;
850         }
851     }
852     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
853     if (sceneSessionManagerProxy == nullptr) {
854         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "sessionManagerServiceProxy is nullptr");
855         return;
856     }
857     WMError ret = sceneSessionManagerProxy->SetScreenPrivacyWindowTagSwitch(screenId, privacyWindowTags, enable);
858     if (ret != WMError::WM_OK) {
859         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "remove virtual screen black list failed!");
860     }
861 }
862 
GetSceneSessionManagerByUserId(int32_t userId)863 sptr<IRemoteObject> MockSessionManagerService::GetSceneSessionManagerByUserId(int32_t userId)
864 {
865     auto sessionManagerService = GetSessionManagerServiceByUserId(userId);
866     if (sessionManagerService == nullptr) {
867         WLOGFE("sessionManagerService is nullptr");
868         return nullptr;
869     }
870     sptr<ISessionManagerService> sessionManagerServiceProxy =
871         iface_cast<ISessionManagerService>(sessionManagerService);
872     if (sessionManagerServiceProxy == nullptr) {
873         WLOGFE("sessionManagerServiceProxy is nullptr");
874         return nullptr;
875     }
876     sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy->GetSceneSessionManager();
877     if (remoteObject == nullptr) {
878         WLOGFW("Get scene session manager proxy failed");
879     }
880     return remoteObject;
881 }
882 
RecoverSCBSnapshotSkipByUserId(int32_t userId)883 ErrCode MockSessionManagerService::RecoverSCBSnapshotSkipByUserId(int32_t userId)
884 {
885     std::unique_lock<std::mutex> lock(userIdBundleNamesMapLock_);
886     auto iter = userIdBundleNamesMap_.find(userId);
887     if (iter == userIdBundleNamesMap_.end()) {
888         return ERR_INVALID_VALUE;
889     }
890     sptr<IRemoteObject> remoteObject = GetSceneSessionManagerByUserId(userId);
891     if (!remoteObject) {
892         return ERR_DEAD_OBJECT;
893     }
894     return NotifySCBSnapshotSkipByUserIdAndBundleNames(userId, iter->second, remoteObject);
895 }
896 
NotifySCBSnapshotSkipByUserIdAndBundleNames(int32_t userId,const std::vector<std::string> & bundleNameList,const sptr<IRemoteObject> & remoteObject)897 ErrCode MockSessionManagerService::NotifySCBSnapshotSkipByUserIdAndBundleNames(int32_t userId,
898     const std::vector<std::string>& bundleNameList, const sptr<IRemoteObject>& remoteObject)
899 {
900     sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(remoteObject);
901     WMError ret = sceneSessionManagerProxy->SkipSnapshotByUserIdAndBundleNames(userId, bundleNameList);
902     if (ret != WMError::WM_OK) {
903         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "failed!");
904         return ERR_INVALID_OPERATION;
905     }
906     return ERR_OK;
907 }
908 
SetSnapshotSkipByUserIdAndBundleNames(int32_t userId,const std::vector<std::string> & bundleNameList)909 ErrCode MockSessionManagerService::SetSnapshotSkipByUserIdAndBundleNames(int32_t userId,
910     const std::vector<std::string>& bundleNameList)
911 {
912     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
913         TLOGE(WmsLogTag::WMS_MULTI_USER, "service not supported!");
914         return ERR_WOULD_BLOCK;
915     }
916     if (!SessionPermission::VerifyCallingPermission("ohos.permission.MANAGE_EDM_POLICY")) {
917         TLOGE(WmsLogTag::WMS_MULTI_USER, "permission denied!");
918         return ERR_PERMISSION_DENIED;
919     }
920     return SetSnapshotSkipByUserIdAndBundleNamesInner(userId, bundleNameList);
921 }
922 
SetSnapshotSkipByUserIdAndBundleNamesInner(int32_t userId,const std::vector<std::string> & bundleNameList)923 ErrCode MockSessionManagerService::SetSnapshotSkipByUserIdAndBundleNamesInner(int32_t userId,
924     const std::vector<std::string>& bundleNameList)
925 {
926     {
927         std::unique_lock<std::mutex> lock(userIdBundleNamesMapLock_);
928         userIdBundleNamesMap_[userId] = bundleNameList;
929     }
930     sptr<IRemoteObject> remoteObject = GetSceneSessionManagerByUserId(userId);
931     if (!remoteObject) {
932         TLOGW(WmsLogTag::WMS_MULTI_USER, "user:%{public}d isn't active.", userId);
933         return ERR_OK;
934     }
935     return NotifySCBSnapshotSkipByUserIdAndBundleNames(userId, bundleNameList, remoteObject);
936 }
937 
SetSnapshotSkipByIdNamesMap(const std::unordered_map<int32_t,std::vector<std::string>> & userIdAndBunldeNames)938 ErrCode MockSessionManagerService::SetSnapshotSkipByIdNamesMap(
939     const std::unordered_map<int32_t, std::vector<std::string>>& userIdAndBunldeNames)
940 {
941     if (!SceneBoardJudgement::IsSceneBoardEnabled()) {
942         TLOGE(WmsLogTag::WMS_MULTI_USER, "service not supported!");
943         return ERR_WOULD_BLOCK;
944     }
945     if (!SessionPermission::VerifyCallingPermission("ohos.permission.MANAGE_EDM_POLICY")) {
946         TLOGE(WmsLogTag::WMS_MULTI_USER, "permission denied");
947         return ERR_PERMISSION_DENIED;
948     }
949     return SetSnapshotSkipByIdNamesMapInner(userIdAndBunldeNames);
950 }
951 
SetSnapshotSkipByIdNamesMapInner(const std::unordered_map<int32_t,std::vector<std::string>> & userIdAndBunldeNames)952 ErrCode MockSessionManagerService::SetSnapshotSkipByIdNamesMapInner(
953     const std::unordered_map<int32_t, std::vector<std::string>>& userIdAndBunldeNames)
954 {
955     std::unique_lock<std::mutex> lock(userIdBundleNamesMapLock_);
956     userIdBundleNamesMap_ = userIdAndBunldeNames;
957     for (const auto& [userId, bundleNameList] : userIdBundleNamesMap_) {
958         sptr<IRemoteObject> remoteObject = GetSceneSessionManagerByUserId(userId);
959         if (!remoteObject) {
960             TLOGW(WmsLogTag::WMS_MULTI_USER, "user:%{public}d isn't active", userId);
961             continue;
962         }
963         ErrCode err = NotifySCBSnapshotSkipByUserIdAndBundleNames(userId, bundleNameList, remoteObject);
964         if (err != ERR_OK) {
965             TLOGE(WmsLogTag::DEFAULT, "failed");
966             return err;
967         }
968     }
969     return ERR_OK;
970 }
971 } // namespace Rosen
972 } // namespace OHOS