1 /*
2  * Copyright (c) 2024 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 "user_session_manager.h"
17 
18 namespace OHOS {
19 namespace MiscServices {
20 
GetInstance()21 UserSessionManager &UserSessionManager::GetInstance()
22 {
23     static UserSessionManager manager;
24     return manager;
25 }
26 
GetUserSessions()27 std::unordered_map<int32_t, std::shared_ptr<PerUserSession>> UserSessionManager::GetUserSessions()
28 {
29     std::lock_guard<std::mutex> lock(userSessionsLock_);
30     return userSessions_;
31 }
32 
GetUserSession(int32_t userId)33 std::shared_ptr<PerUserSession> UserSessionManager::GetUserSession(int32_t userId)
34 {
35     std::lock_guard<std::mutex> lock(userSessionsLock_);
36     auto session = userSessions_.find(userId);
37     if (session == userSessions_.end()) {
38         return nullptr;
39     }
40     return session->second;
41 }
42 
AddUserSession(int32_t userId)43 void UserSessionManager::AddUserSession(int32_t userId)
44 {
45     std::lock_guard<std::mutex> lock(userSessionsLock_);
46     auto session = userSessions_.find(userId);
47     if (session != userSessions_.end()) {
48         return;
49     }
50     auto sessionTemp = std::make_shared<PerUserSession>(userId, eventHandler_);
51     userSessions_.insert({ userId, sessionTemp });
52 }
53 
RemoveUserSession(int32_t userId)54 void UserSessionManager::RemoveUserSession(int32_t userId)
55 {
56     std::lock_guard<std::mutex> lock(userSessionsLock_);
57     userSessions_.erase(userId);
58 }
59 
SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> & eventHandler)60 void UserSessionManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler)
61 {
62     eventHandler_ = eventHandler;
63 }
64 } // namespace MiscServices
65 } // namespace OHOS