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     /*
51      * Window Snapshot
52      */
53     int32_t SetSnapshotSkipByUserIdAndBundleNames(int32_t userId,
54         const std::vector<std::string>& bundleNameList) override;
55     int32_t SetSnapshotSkipByIdNamesMap(const std::unordered_map<int32_t,
56         std::vector<std::string>>& userIdAndBunldeNames) override;
57 
58 protected:
59     MockSessionManagerService();
60     virtual ~MockSessionManagerService() = default;
61 
62 private:
63     class SMSDeathRecipient : public IRemoteObject::DeathRecipient {
64     public:
65         explicit SMSDeathRecipient(int32_t userId);
66         void OnRemoteDied(const wptr<IRemoteObject>& object) override;
67         bool IsSceneBoardTestMode();
68         void SetScreenId(int32_t screenId);
69         bool needKillService_ { true };
70 
71     private:
72         int32_t userId_;
73         int32_t screenId_;
74     };
75     sptr<SMSDeathRecipient> GetSMSDeathRecipientByUserId(int32_t userId);
76     void RemoveSMSDeathRecipientByUserId(int32_t userId);
77     sptr<IRemoteObject> GetSessionManagerServiceByUserId(int32_t userId);
78     void RemoveSessionManagerServiceByUserId(int32_t userId);
79     bool RegisterMockSessionManagerService();
80     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* GetSMSRecoverListenerMap(int32_t userId);
81     std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>* GetSMSLiteRecoverListenerMap(int32_t userId);
82     void NotifySceneBoardAvailableToClient(int32_t userId);
83     void NotifySceneBoardAvailableToLiteClient(int32_t userId);
84     void NotifyWMSConnectionChanged(int32_t wmsUserId, int32_t screenId, bool isConnected);
85     void NotifyWMSConnectionChangedToClient(int32_t wmsUserId, int32_t screenId, bool isConnected);
86     void NotifyWMSConnectionChangedToLiteClient(int32_t wmsUserId, int32_t screenId, bool isConnected);
87     int DumpSessionInfo(const std::vector<std::string>& args, std::string& dumpInfo);
88     void ShowHelpInfo(std::string& dumpInfo);
89     void ShowAceDumpHelp(std::string& dumpInfo);
90     void ShowIllegalArgsInfo(std::string& dumpInfo);
91 
92     /*
93      * Window Snapshot
94      */
95     sptr<IRemoteObject> GetSceneSessionManagerByUserId(int32_t userId);
96     int32_t RecoverSCBSnapshotSkipByUserId(int32_t userId);
97     int32_t NotifySCBSnapshotSkipByUserIdAndBundleNames(int32_t userId,
98         const std::vector<std::string>& bundleNameList, const sptr<IRemoteObject>& remoteObject);
99 
100     static void WriteStringToFile(int32_t pid, const char* str);
101 
102     sptr<IRemoteObject> screenSessionManager_;
103     sptr<IRemoteObject> sceneSessionManager_;
104 
105     std::shared_mutex smsDeathRecipientMapLock_;
106     std::map<int32_t, sptr<SMSDeathRecipient>> smsDeathRecipientMap_;
107 
108     std::shared_mutex sessionManagerServiceMapLock_;
109     std::map<int32_t, sptr<IRemoteObject>> sessionManagerServiceMap_;
110 
111     std::shared_mutex smsRecoverListenerLock_;
112     std::map<int32_t, std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>> smsRecoverListenerMap_;
113 
114     std::shared_mutex smsLiteRecoverListenerLock_;
115     std::map<int32_t, std::map<int32_t, sptr<ISessionManagerServiceRecoverListener>>> smsLiteRecoverListenerMap_;
116 
117     std::mutex wmsConnectionStatusLock_;
118     std::map<int32_t, bool> wmsConnectionStatusMap_;
119 
120     /*
121      * Window Snapshot
122      */
123     std::mutex userIdBundleNamesMapLock_;
124     std::unordered_map<int32_t, std::vector<std::string>> userIdBundleNamesMap_;
125 
126     int32_t currentWMSUserId_;
127     int32_t currentScreenId_;
128 };
129 } // namespace Rosen
130 } // namespace OHOS
131 
132 #endif // OHOS_ROSEN_MOCK_SESSION_MANAGER_SERVICE_H
133