• 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 "session_manager.h"
17 
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 
21 #include "window_manager_hilog.h"
22 #include "mock_session_manager_service_interface.h"
23 
24 namespace OHOS::Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "SessionManager" };
27 }
28 
WM_IMPLEMENT_SINGLE_INSTANCE(SessionManager)29 WM_IMPLEMENT_SINGLE_INSTANCE(SessionManager)
30 
31 void SessionManager::ClearSessionManagerProxy()
32 {
33     std::unique_lock<std::shared_mutex> lock(proxyMutex_);
34     if (mockSessionManagerServiceProxy_ != nullptr) {
35         mockSessionManagerServiceProxy_ = nullptr;
36     }
37     if (sessionManagerServiceProxy_ != nullptr) {
38         sessionManagerServiceProxy_ = nullptr;
39     }
40     if (sceneSessionManagerProxy_ != nullptr) {
41         sceneSessionManagerProxy_ = nullptr;
42     }
43     if (screenSessionManagerProxy_ != nullptr) {
44         screenSessionManagerProxy_ = nullptr;
45     }
46     if (screenLockManagerProxy_ != nullptr) {
47         screenLockManagerProxy_ = nullptr;
48     }
49 }
50 
GetScreenLockManagerProxy()51 sptr<ScreenLock::ScreenLockManagerInterface> SessionManager::GetScreenLockManagerProxy()
52 {
53     std::shared_lock<std::shared_mutex> lock(proxyMutex_);
54     InitSessionManagerServiceProxy();
55     InitScreenLockManagerProxy();
56     return screenLockManagerProxy_;
57 }
58 
GetScreenSessionManagerProxy()59 sptr<IScreenSessionManager> SessionManager::GetScreenSessionManagerProxy()
60 {
61     std::shared_lock<std::shared_mutex> lock(proxyMutex_);
62     InitSessionManagerServiceProxy();
63     InitScreenSessionManagerProxy();
64     return screenSessionManagerProxy_;
65 }
66 
GetSceneSessionManagerProxy()67 sptr<ISceneSessionManager> SessionManager::GetSceneSessionManagerProxy()
68 {
69     std::shared_lock<std::shared_mutex> lock(proxyMutex_);
70     InitSessionManagerServiceProxy();
71     InitSceneSessionManagerProxy();
72     return sceneSessionManagerProxy_;
73 }
74 
InitSessionManagerServiceProxy()75 void SessionManager::InitSessionManagerServiceProxy()
76 {
77     if (sessionManagerServiceProxy_) {
78         return;
79     }
80     sptr<ISystemAbilityManager> systemAbilityManager =
81         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
82     if (!systemAbilityManager) {
83             WLOGFE("Failed to get system ability mgr.");
84             return;
85         }
86     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(WINDOW_MANAGER_SERVICE_ID);
87     if (!remoteObject) {
88         WLOGFE("Remote object is nullptr");
89         return;
90     }
91     mockSessionManagerServiceProxy_ = iface_cast<IMockSessionManagerInterface>(remoteObject);
92     sessionManagerServiceProxy_ = iface_cast<ISessionManagerService>(
93             mockSessionManagerServiceProxy_->GetSessionManagerService());
94     if (!sessionManagerServiceProxy_) {
95         WLOGFE("sessionManagerServiceProxy_ is nullptr");
96     }
97 }
98 
InitScreenSessionManagerProxy()99 void SessionManager::InitScreenSessionManagerProxy()
100 {
101     if (screenSessionManagerProxy_) {
102         return;
103     }
104     if (!sessionManagerServiceProxy_) {
105         WLOGFW("Get screen session manager proxy failed, sessionManagerServiceProxy_ is nullptr");
106         return;
107     }
108     sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetScreenSessionManagerService();
109     if (!remoteObject) {
110         WLOGFW("Get screen session manager proxy failed, screen session manager service is null");
111         return;
112     }
113     screenSessionManagerProxy_ = iface_cast<IScreenSessionManager>(remoteObject);
114     if (!screenSessionManagerProxy_) {
115         WLOGFW("Get screen session manager proxy failed, nullptr");
116     }
117 }
118 
InitSceneSessionManagerProxy()119 void SessionManager::InitSceneSessionManagerProxy()
120 {
121     if (sceneSessionManagerProxy_) {
122         return;
123     }
124     if (!sessionManagerServiceProxy_) {
125         WLOGFE("sessionManagerServiceProxy_ is nullptr");
126         return;
127     }
128 
129     sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetSceneSessionManager();
130     if (!remoteObject) {
131         WLOGFW("Get scene session manager proxy failed, scene session manager service is null");
132         return;
133     }
134     sceneSessionManagerProxy_ = iface_cast<ISceneSessionManager>(remoteObject);
135     if (!sceneSessionManagerProxy_) {
136         WLOGFW("Get scene session manager proxy failed, nullptr");
137     }
138 }
139 
CreateAndConnectSpecificSession(const sptr<ISessionStage> & sessionStage,const sptr<IWindowEventChannel> & eventChannel,const std::shared_ptr<RSSurfaceNode> & surfaceNode,sptr<WindowSessionProperty> property,int32_t & persistentId,sptr<ISession> & session,sptr<IRemoteObject> token)140 void SessionManager::CreateAndConnectSpecificSession(const sptr<ISessionStage>& sessionStage,
141     const sptr<IWindowEventChannel>& eventChannel, const std::shared_ptr<RSSurfaceNode>& surfaceNode,
142     sptr<WindowSessionProperty> property, int32_t& persistentId, sptr<ISession>& session, sptr<IRemoteObject> token)
143 {
144     WLOGFD("CreateAndConnectSpecificSession");
145     GetSceneSessionManagerProxy();
146     if (!sceneSessionManagerProxy_) {
147         WLOGFE("sceneSessionManagerProxy_ is nullptr");
148         return;
149     }
150     sceneSessionManagerProxy_->CreateAndConnectSpecificSession(sessionStage, eventChannel,
151         surfaceNode, property, persistentId, session, token);
152 }
153 
DestroyAndDisconnectSpecificSession(const int32_t & persistentId)154 void SessionManager::DestroyAndDisconnectSpecificSession(const int32_t& persistentId)
155 {
156     WLOGFD("DestroyAndDisconnectSpecificSession");
157     GetSceneSessionManagerProxy();
158     if (!sceneSessionManagerProxy_) {
159         WLOGFE("sceneSessionManagerProxy_ is nullptr");
160         return;
161     }
162     sceneSessionManagerProxy_->DestroyAndDisconnectSpecificSession(persistentId);
163 }
164 
UpdateProperty(sptr<WindowSessionProperty> & property,WSPropertyChangeAction action)165 WMError SessionManager::UpdateProperty(sptr<WindowSessionProperty>& property, WSPropertyChangeAction action)
166 {
167     WLOGFD("UpdateProperty");
168     InitSceneSessionManagerProxy();
169     if (!sceneSessionManagerProxy_) {
170         WLOGFE("sceneSessionManagerProxy_ is nullptr");
171         return WMError::WM_DO_NOTHING;
172     }
173     return sceneSessionManagerProxy_->UpdateProperty(property, action);
174 }
175 
SetSessionGravity(int32_t persistentId,SessionGravity gravity,uint32_t percent)176 WMError SessionManager::SetSessionGravity(int32_t persistentId, SessionGravity gravity, uint32_t percent)
177 {
178     WLOGFD("SetWindowGravity");
179     InitSceneSessionManagerProxy();
180     if (!sceneSessionManagerProxy_) {
181         WLOGFE("sceneSessionManagerProxy_ is nullptr");
182         return WMError::WM_DO_NOTHING;
183     }
184     return static_cast<WMError>(sceneSessionManagerProxy_->SetSessionGravity(persistentId, gravity, percent));
185 }
186 
BindDialogTarget(uint64_t persistentId,sptr<IRemoteObject> targetToken)187 WMError SessionManager::BindDialogTarget(uint64_t persistentId, sptr<IRemoteObject> targetToken)
188 {
189     WLOGFD("BindDialogTarget");
190     InitSceneSessionManagerProxy();
191     if (!sceneSessionManagerProxy_) {
192         WLOGFE("sceneSessionManagerProxy_ is nullptr");
193         return WMError::WM_DO_NOTHING;
194     }
195     return static_cast<WMError>(sceneSessionManagerProxy_->BindDialogTarget(persistentId, targetToken));
196 }
197 
InitScreenLockManagerProxy()198 void SessionManager::InitScreenLockManagerProxy()
199 {
200     if (screenLockManagerProxy_) {
201         return;
202     }
203     if (!sessionManagerServiceProxy_) {
204         WLOGFE("Get screen session manager proxy failed, sessionManagerServiceProxy_ is nullptr");
205         return;
206     }
207     sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy_->GetScreenLockManagerService();
208     if (!remoteObject) {
209         WLOGFE("Get screenlock manager proxy failed, screenlock manager service is null");
210         return;
211     }
212 
213     screenLockManagerProxy_ = iface_cast<ScreenLock::ScreenLockManagerInterface>(remoteObject);
214     if (!screenLockManagerProxy_) {
215         WLOGFW("Get screenlock manager proxy failed, nullptr");
216     }
217 }
218 } // namespace OHOS::Rosen
219