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_WINDOW_SCENE_SESSION_MANAGER_LITE_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_LITE_H
18 
19 #include <functional>
20 #include <shared_mutex>
21 
22 #include "session_manager_service_interface.h"
23 #include "mock_session_manager_service_interface.h"
24 #include "zidl/scene_session_manager_lite_interface.h"
25 #include "zidl/screen_session_manager_lite_interface.h"
26 #include "wm_single_instance.h"
27 #include "wm_common.h"
28 
29 namespace OHOS::Rosen {
30 class SSMDeathRecipientLite : public IRemoteObject::DeathRecipient {
31 public:
32     virtual void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override;
33 };
34 
35 class FoundationDeathRecipientLite : public IRemoteObject::DeathRecipient {
36 public:
37     virtual void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override;
38 };
39 
40 class SessionManagerLite {
41 WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManagerLite);
42 public:
43     using UserSwitchCallbackFunc = std::function<void()>;
44     using WMSConnectionChangedCallbackFunc = std::function<void(int32_t, int32_t, bool)>;
45     void ClearSessionManagerProxy();
46     void Clear();
47 
48     sptr<ISceneSessionManagerLite> GetSceneSessionManagerLiteProxy();
49     sptr<IScreenSessionManagerLite> GetScreenSessionManagerLiteProxy();
50 
51     sptr<ISessionManagerService> GetSessionManagerServiceProxy();
52 
53     void SaveSessionListener(const sptr<ISessionListener>& listener);
54     void DeleteSessionListener(const sptr<ISessionListener>& listener);
55     void RecoverSessionManagerService(const sptr<ISessionManagerService>& sessionManagerService);
56     void RegisterUserSwitchListener(const UserSwitchCallbackFunc& callbackFunc);
57     void OnWMSConnectionChanged(
58         int32_t userId, int32_t screenId, bool isConnected, const sptr<ISessionManagerService>& sessionManagerService);
59     void OnFoundationDied();
60 
61     WMError RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc);
62 
63 protected:
64     SessionManagerLite() = default;
65     virtual ~SessionManagerLite();
66 
67 private:
68     void InitSessionManagerServiceProxy();
69     void InitSceneSessionManagerLiteProxy();
70     void InitScreenSessionManagerLiteProxy();
71     void OnUserSwitch(const sptr<ISessionManagerService>& sessionManagerService);
72     void DeleteAllSessionListeners();
73     void ReregisterSessionListener() const;
74     void RegisterSMSRecoverListener();
75     void OnWMSConnectionChangedCallback(int32_t userId, int32_t screenId, bool isConnected, bool isCallbackRegistered);
76     WMError InitMockSMSProxy();
77     UserSwitchCallbackFunc userSwitchCallbackFunc_ = nullptr;
78 
79     std::recursive_mutex mutex_;
80     sptr<IMockSessionManagerInterface> mockSessionManagerServiceProxy_ = nullptr;
81     sptr<ISessionManagerService> sessionManagerServiceProxy_ = nullptr;
82     sptr<ISceneSessionManagerLite> sceneSessionManagerLiteProxy_ = nullptr;
83     sptr<IScreenSessionManagerLite> screenSessionManagerLiteProxy_ = nullptr;
84     sptr<SSMDeathRecipientLite> ssmDeath_ = nullptr;
85     sptr<IRemoteObject> smsRecoverListener_ = nullptr;
86     sptr<FoundationDeathRecipientLite> foundationDeath_ = nullptr;
87     bool recoverListenerRegistered_ = false;
88     bool isFoundationListenerRegistered_ = false;
89     // above guarded by mutex_
90 
91     std::recursive_mutex listenerLock_;
92     std::vector<sptr<ISessionListener>> sessionListeners_;
93     // above guarded by listenerLock_
94 
95     std::mutex wmsConnectionMutex_;
96     int32_t currentWMSUserId_ = INVALID_USER_ID;
97     int32_t currentScreenId_ = DEFAULT_SCREEN_ID;
98     bool isWMSConnected_ = false;
99     WMSConnectionChangedCallbackFunc wmsConnectionChangedFunc_ = nullptr;
100     // above guarded by wmsConnectionMutex_, among OnWMSConnectionChanged for wms connection event, user switched,
101     // register WMSConnectionChangedListener.
102 };
103 } // namespace OHOS::Rosen
104 
105 #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_LITE_H
106