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_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_H
18 
19 #include <iremote_stub.h>
20 #include <shared_mutex>
21 
22 #include "mock_session_manager_service_interface.h"
23 #include "session_manager_service_interface.h"
24 #include "wm_single_instance.h"
25 #include "zidl/scene_session_manager_interface.h"
26 
27 namespace OHOS::Rosen {
28 class SSMDeathRecipient : public IRemoteObject::DeathRecipient {
29 public:
30     virtual void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override;
31 };
32 class FoundationDeathRecipient : public IRemoteObject::DeathRecipient {
33 public:
34     virtual void OnRemoteDied(const wptr<IRemoteObject>& wptrDeath) override;
35 };
36 
37 class SessionManager {
38     WM_DECLARE_SINGLE_INSTANCE_BASE(SessionManager);
39 
40 public:
41     using SessionRecoverCallbackFunc = std::function<void()>;
42     using WindowManagerRecoverCallbackFunc = std::function<void()>;
43     using WMSConnectionChangedCallbackFunc = std::function<void(int32_t, int32_t, bool)>;
44     using UserSwitchCallbackFunc = std::function<void()>;
45     void RegisterWindowManagerRecoverCallbackFunc(const WindowManagerRecoverCallbackFunc& callbackFunc);
46     void RecoverSessionManagerService(const sptr<ISessionManagerService>& sessionManagerService);
47     void OnWMSConnectionChanged(
48         int32_t userId, int32_t screenId, bool isConnected, const sptr<ISessionManagerService>& sessionManagerService);
49     void ClearSessionManagerProxy();
50     void Clear();
51     WMError RegisterWMSConnectionChangedListener(const WMSConnectionChangedCallbackFunc& callbackFunc);
52     void RegisterUserSwitchListener(const UserSwitchCallbackFunc& callbackFunc);
53 
54     sptr<ISceneSessionManager> GetSceneSessionManagerProxy();
55     void OnFoundationDied();
56 
57 protected:
58     SessionManager() = default;
59     virtual ~SessionManager();
60 
61 private:
62     void InitSessionManagerServiceProxy();
63     WMError InitMockSMSProxy();
64     void InitSceneSessionManagerProxy();
65     void OnWMSConnectionChangedCallback(int32_t userId, int32_t screenId, bool isConnected, bool isCallbackRegistered);
66     void OnUserSwitch(const sptr<ISessionManagerService>& sessionManagerService);
67     void RegisterSMSRecoverListener();
68     UserSwitchCallbackFunc userSwitchCallbackFunc_ = nullptr;
69 
70     std::recursive_mutex mutex_;
71     sptr<IMockSessionManagerInterface> mockSessionManagerServiceProxy_ = nullptr;
72     sptr<ISessionManagerService> sessionManagerServiceProxy_ = nullptr;
73     sptr<ISceneSessionManager> sceneSessionManagerProxy_ = nullptr;
74     bool isRecoverListenerRegistered_ = false;
75     sptr<IRemoteObject> smsRecoverListener_ = nullptr;
76     sptr<SSMDeathRecipient> ssmDeath_ = nullptr;
77     sptr<FoundationDeathRecipient> foundationDeath_ = nullptr;
78     bool isFoundationListenerRegistered_ = false;
79     // above guarded by mutex_
80 
81     std::recursive_mutex recoverMutex_;
82     WindowManagerRecoverCallbackFunc windowManagerRecoverFunc_ = nullptr;
83     // above guarded by recoverMutex_
84 
85     std::mutex wmsConnectionMutex_;
86     bool isWMSConnected_ = false;
87     int32_t currentWMSUserId_ = INVALID_USER_ID;
88     int32_t currentScreenId_ = DEFAULT_SCREEN_ID;
89     WMSConnectionChangedCallbackFunc wmsConnectionChangedFunc_ = nullptr;
90     // above guarded by wmsConnectionMutex_, among OnWMSConnectionChanged for wms connection event, user switched,
91     // register WMSConnectionChangedListener.
92 };
93 } // namespace OHOS::Rosen
94 
95 #endif // OHOS_ROSEN_WINDOW_SCENE_SESSION_MANAGER_H
96