122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#include "user_session_manager.h"
1722736c2fSopenharmony_ci
1822736c2fSopenharmony_cinamespace OHOS {
1922736c2fSopenharmony_cinamespace MiscServices {
2022736c2fSopenharmony_ci
2122736c2fSopenharmony_ciUserSessionManager &UserSessionManager::GetInstance()
2222736c2fSopenharmony_ci{
2322736c2fSopenharmony_ci    static UserSessionManager manager;
2422736c2fSopenharmony_ci    return manager;
2522736c2fSopenharmony_ci}
2622736c2fSopenharmony_ci
2722736c2fSopenharmony_cistd::unordered_map<int32_t, std::shared_ptr<PerUserSession>> UserSessionManager::GetUserSessions()
2822736c2fSopenharmony_ci{
2922736c2fSopenharmony_ci    std::lock_guard<std::mutex> lock(userSessionsLock_);
3022736c2fSopenharmony_ci    return userSessions_;
3122736c2fSopenharmony_ci}
3222736c2fSopenharmony_ci
3322736c2fSopenharmony_cistd::shared_ptr<PerUserSession> UserSessionManager::GetUserSession(int32_t userId)
3422736c2fSopenharmony_ci{
3522736c2fSopenharmony_ci    std::lock_guard<std::mutex> lock(userSessionsLock_);
3622736c2fSopenharmony_ci    auto session = userSessions_.find(userId);
3722736c2fSopenharmony_ci    if (session == userSessions_.end()) {
3822736c2fSopenharmony_ci        return nullptr;
3922736c2fSopenharmony_ci    }
4022736c2fSopenharmony_ci    return session->second;
4122736c2fSopenharmony_ci}
4222736c2fSopenharmony_ci
4322736c2fSopenharmony_civoid UserSessionManager::AddUserSession(int32_t userId)
4422736c2fSopenharmony_ci{
4522736c2fSopenharmony_ci    std::lock_guard<std::mutex> lock(userSessionsLock_);
4622736c2fSopenharmony_ci    auto session = userSessions_.find(userId);
4722736c2fSopenharmony_ci    if (session != userSessions_.end()) {
4822736c2fSopenharmony_ci        return;
4922736c2fSopenharmony_ci    }
5022736c2fSopenharmony_ci    auto sessionTemp = std::make_shared<PerUserSession>(userId, eventHandler_);
5122736c2fSopenharmony_ci    userSessions_.insert({ userId, sessionTemp });
5222736c2fSopenharmony_ci}
5322736c2fSopenharmony_ci
5422736c2fSopenharmony_civoid UserSessionManager::RemoveUserSession(int32_t userId)
5522736c2fSopenharmony_ci{
5622736c2fSopenharmony_ci    std::lock_guard<std::mutex> lock(userSessionsLock_);
5722736c2fSopenharmony_ci    userSessions_.erase(userId);
5822736c2fSopenharmony_ci}
5922736c2fSopenharmony_ci
6022736c2fSopenharmony_civoid UserSessionManager::SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler)
6122736c2fSopenharmony_ci{
6222736c2fSopenharmony_ci    eventHandler_ = eventHandler;
6322736c2fSopenharmony_ci}
6422736c2fSopenharmony_ci} // namespace MiscServices
6522736c2fSopenharmony_ci} // namespace OHOS