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
18namespace OHOS {
19namespace MiscServices {
20
21UserSessionManager &UserSessionManager::GetInstance()
22{
23    static UserSessionManager manager;
24    return manager;
25}
26
27std::unordered_map<int32_t, std::shared_ptr<PerUserSession>> UserSessionManager::GetUserSessions()
28{
29    std::lock_guard<std::mutex> lock(userSessionsLock_);
30    return userSessions_;
31}
32
33std::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
43void 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
54void UserSessionManager::RemoveUserSession(int32_t userId)
55{
56    std::lock_guard<std::mutex> lock(userSessionsLock_);
57    userSessions_.erase(userId);
58}
59
60void UserSessionManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler)
61{
62    eventHandler_ = eventHandler;
63}
64} // namespace MiscServices
65} // namespace OHOS