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 #ifndef OHOS_ROSEN_MOCK_SESSION_MANAGER_SERVICE_H
17 #define OHOS_ROSEN_MOCK_SESSION_MANAGER_SERVICE_H
18 
19 #include <shared_mutex>
20 #include <system_ability.h>
21 
22 #include "wm_single_instance.h"
23 #include "zidl/mock_session_manager_service_stub.h"
24 #include "zidl/session_manager_service_recover_interface.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class MockSessionManagerService : public SystemAbility, public MockSessionManagerServiceStub {
29 DECLARE_SYSTEM_ABILITY(MockSessionManagerService);
30 WM_DECLARE_SINGLE_INSTANCE_BASE(MockSessionManagerService);
31 public:
32     bool SetSessionManagerService(const sptr<IRemoteObject>& sessionManagerService);
33     void NotifySceneBoardAvailable() override;
34     sptr<IRemoteObject> GetSessionManagerService() override;
35     sptr<IRemoteObject> GetScreenSessionManagerLite() override;
36     sptr<IRemoteObject> GetSceneSessionManager();
37     void RegisterSMSRecoverListener(const sptr<IRemoteObject>& listener) override;
38     void UnregisterSMSRecoverListener() override;
39     void UnregisterSMSRecoverListener(int32_t userId, int32_t pid);
40     void RegisterSMSLiteRecoverListener(const sptr<IRemoteObject>& listener) override;
41     void UnregisterSMSLiteRecoverListener() override;
42     void UnregisterSMSLiteRecoverListener(int32_t userId, int32_t pid);
43     void OnStart() override;
44     int Dump(int fd, const std::vector<std::u16string> &args) override;
45     void NotifyWMSConnected(int32_t userId, int32_t screenId, bool isColdStart);
NotifyNotKillService()46     void NotifyNotKillService() {}
47     void GetProcessSurfaceNodeIdByPersistentId(const int32_t pid,
48         const std::vector<uint64_t>& windowIdList, std::vector<uint64_t>& surfaceNodeIds);
49 
50 protected:
51     MockSessionManagerService();
52     virtual ~MockSessionManagerService() = default;
53 
54 private:
55     class SMSDeathRecipient : public IRemoteObject::DeathRecipient {
56     public:
57         explicit SMSDeathRecipient(int32_t userId);
58         void OnRemoteDied(const wptr<IRemoteObject>& object) override;
59         bool IsSceneBoardTestMode();
60         void SetScreenId(int32_t screenId);
61         bool needKillService_ { true };
62 
63     private:
64         int32_t userId_;
65         int32_t screenId_;
66     };
67     sptr<SMSDeathRecipient> GetSMSDeathRecipientByUserId(int32_t userId);
68     void RemoveSMSDeathRecipientByUserId(int32_t userId);
69     sptr<IRemoteObject> GetSessionManagerServiceByUserId(int32_t userId);
70     void RemoveSessionManagerServiceByUserId(int32_t userId);
71     bool RegisterMockSessionManagerService();
72     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* GetSMSRecoverListenerMap(int32_t userId);
73     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* GetSMSLiteRecoverListenerMap(int32_t userId);
74     void NotifySceneBoardAvailableToClient(int32_t userId);
75     void NotifySceneBoardAvailableToLiteClient(int32_t userId);
76     void NotifyWMSConnectionChanged(int32_t wmsUserId, int32_t screenId, bool isConnected);
77     void NotifyWMSConnectionChangedToClient(int32_t wmsUserId, int32_t screenId, bool isConnected);
78     void NotifyWMSConnectionChangedToLiteClient(int32_t wmsUserId, int32_t screenId, bool isConnected);
79     int DumpSessionInfo(const std::vector<std::string>& args, std::string& dumpInfo);
80     void ShowHelpInfo(std::string& dumpInfo);
81     void ShowAceDumpHelp(std::string& dumpInfo);
82     void ShowIllegalArgsInfo(std::string& dumpInfo);
83 
84     static void WriteStringToFile(int32_t pid, const char* str);
85 
86     sptr<IRemoteObject> screenSessionManager_;
87     sptr<IRemoteObject> sceneSessionManager_;
88 
89     std::shared_mutex smsDeathRecipientMapLock_;
90     std::map<int32_t, sptr<SMSDeathRecipient>> smsDeathRecipientMap_;
91 
92     std::shared_mutex sessionManagerServiceMapLock_;
93     std::map<int32_t, sptr<IRemoteObject>> sessionManagerServiceMap_;
94 
95     std::shared_mutex smsRecoverListenerLock_;
96     std::map<int32_t, std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>> smsRecoverListenerMap_;
97 
98     std::shared_mutex smsLiteRecoverListenerLock_;
99     std::map<int32_t, std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>> smsLiteRecoverListenerMap_;
100 
101     std::mutex wmsConnectionStatusLock_;
102     std::map<int32_t, bool> wmsConnectionStatusMap_;
103 
104     int32_t currentWMSUserId_;
105     int32_t currentScreenId_;
106 };
107 } // namespace Rosen
108 } // namespace OHOS
109 
110 #endif // OHOS_ROSEN_MOCK_SESSION_MANAGER_SERVICE_H
111