180922886Sopenharmony_ci/*
280922886Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
480922886Sopenharmony_ci * you may not use this file except in compliance with the License.
580922886Sopenharmony_ci * You may obtain a copy of the License at
680922886Sopenharmony_ci *
780922886Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
880922886Sopenharmony_ci *
980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1280922886Sopenharmony_ci * See the License for the specific language governing permissions and
1380922886Sopenharmony_ci * limitations under the License.
1480922886Sopenharmony_ci */
1580922886Sopenharmony_ci
1680922886Sopenharmony_ci#include "avsession_users_manager.h"
1780922886Sopenharmony_ci#include "account_manager_adapter.h"
1880922886Sopenharmony_ci#include "avsession_utils.h"
1980922886Sopenharmony_ci
2080922886Sopenharmony_cinamespace OHOS::AVSession {
2180922886Sopenharmony_ciAVSessionUsersManager& AVSessionUsersManager::GetInstance()
2280922886Sopenharmony_ci{
2380922886Sopenharmony_ci    static AVSessionUsersManager usersManager;
2480922886Sopenharmony_ci    return usersManager;
2580922886Sopenharmony_ci}
2680922886Sopenharmony_ci
2780922886Sopenharmony_civoid AVSessionUsersManager::Init()
2880922886Sopenharmony_ci{
2980922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
3080922886Sopenharmony_ci    AccountManagerAdapter::GetInstance().Init();
3180922886Sopenharmony_ci    AccountManagerAdapter::GetInstance().AddAccountEventsListener([this] (const std::string &type, const int &userId) {
3280922886Sopenharmony_ci        SLOGI("get event for %{public}d with type %{public}s, curUser: %{public}d", userId, type.c_str(), curUserId_);
3380922886Sopenharmony_ci        if (type == AccountManagerAdapter::accountEventSwitched) {
3480922886Sopenharmony_ci            curUserId_ = userId;
3580922886Sopenharmony_ci            auto it = std::find(aliveUsers_.begin(), aliveUsers_.end(), curUserId_);
3680922886Sopenharmony_ci            if (it == aliveUsers_.end()) {
3780922886Sopenharmony_ci                aliveUsers_.push_back(curUserId_);
3880922886Sopenharmony_ci            }
3980922886Sopenharmony_ci        } else if (type == AccountManagerAdapter::accountEventRemoved) {
4080922886Sopenharmony_ci            HandleUserRemoved(userId);
4180922886Sopenharmony_ci        }
4280922886Sopenharmony_ci    });
4380922886Sopenharmony_ci    curUserId_ = AccountManagerAdapter::GetInstance().GetCurrentAccountUserId();
4480922886Sopenharmony_ci    aliveUsers_.push_back(curUserId_);
4580922886Sopenharmony_ci}
4680922886Sopenharmony_ci
4780922886Sopenharmony_civoid AVSessionUsersManager::HandleUserRemoved(int32_t userId)
4880922886Sopenharmony_ci{
4980922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
5080922886Sopenharmony_ci    SLOGI("HandleUserRemoved for user %{public}d", userId);
5180922886Sopenharmony_ci    auto iterForStack = sessionStackMapByUserId_.find(userId);
5280922886Sopenharmony_ci    if (iterForStack != sessionStackMapByUserId_.end()) {
5380922886Sopenharmony_ci        std::shared_ptr<SessionStack> stackPtr = iterForStack->second;
5480922886Sopenharmony_ci        CHECK_AND_RETURN_LOG(stackPtr != nullptr, "HandleUserRemoved with nullptr stack error");
5580922886Sopenharmony_ci        std::vector<sptr<AVSessionItem>> allSession = stackPtr->GetAllSessions();
5680922886Sopenharmony_ci        for (auto& sessionItem : allSession) {
5780922886Sopenharmony_ci            CHECK_AND_RETURN_LOG(sessionItem != nullptr, "HandleUserRemoved session null");
5880922886Sopenharmony_ci            std::string sessionId = sessionItem->GetSessionId();
5980922886Sopenharmony_ci            stackPtr->RemoveSession(sessionId);
6080922886Sopenharmony_ci            GetContainerFromAll().RemoveSession(sessionId);
6180922886Sopenharmony_ci        }
6280922886Sopenharmony_ci        sessionStackMapByUserId_.erase(iterForStack);
6380922886Sopenharmony_ci    }
6480922886Sopenharmony_ci    auto iterForFrontList = frontSessionListMapByUserId_.find(userId);
6580922886Sopenharmony_ci    if (iterForFrontList != frontSessionListMapByUserId_.end()) {
6680922886Sopenharmony_ci        frontSessionListMapByUserId_.erase(iterForFrontList);
6780922886Sopenharmony_ci    }
6880922886Sopenharmony_ci    auto iterForListenerMap = sessionListenersMapByUserId_.find(userId);
6980922886Sopenharmony_ci    if (iterForListenerMap != sessionListenersMapByUserId_.end()) {
7080922886Sopenharmony_ci        sessionListenersMapByUserId_.erase(iterForListenerMap);
7180922886Sopenharmony_ci    }
7280922886Sopenharmony_ci    auto iterForTop = topSessionsMapByUserId_.find(userId);
7380922886Sopenharmony_ci    if (iterForTop != topSessionsMapByUserId_.end()) {
7480922886Sopenharmony_ci        topSessionsMapByUserId_.erase(iterForTop);
7580922886Sopenharmony_ci    }
7680922886Sopenharmony_ci    aliveUsers_.remove_if([userId](int32_t element) { return element == userId; });
7780922886Sopenharmony_ci}
7880922886Sopenharmony_ci
7980922886Sopenharmony_ci
8080922886Sopenharmony_ciSessionStack& AVSessionUsersManager::GetContainer()
8180922886Sopenharmony_ci{
8280922886Sopenharmony_ci    return GetContainerFromUser(curUserId_);
8380922886Sopenharmony_ci}
8480922886Sopenharmony_ci
8580922886Sopenharmony_ciSessionStack& AVSessionUsersManager::GetContainerFromUser(int32_t userId)
8680922886Sopenharmony_ci{
8780922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
8880922886Sopenharmony_ci    std::shared_ptr<SessionStack> stackPtr = nullptr;
8980922886Sopenharmony_ci    auto iter = sessionStackMapByUserId_.find(userId);
9080922886Sopenharmony_ci    if (iter != sessionStackMapByUserId_.end()) {
9180922886Sopenharmony_ci        stackPtr = iter->second;
9280922886Sopenharmony_ci    } else {
9380922886Sopenharmony_ci        SLOGI("create new stack for user %{public}d", userId);
9480922886Sopenharmony_ci        stackPtr = std::make_shared<SessionStack>();
9580922886Sopenharmony_ci        sessionStackMapByUserId_[userId] = stackPtr;
9680922886Sopenharmony_ci    }
9780922886Sopenharmony_ci    if (stackPtr == nullptr) {
9880922886Sopenharmony_ci        SLOGE("error finding sessionStack ptr null, return default!");
9980922886Sopenharmony_ci        static SessionStack sessionStack;
10080922886Sopenharmony_ci        return sessionStack;
10180922886Sopenharmony_ci    }
10280922886Sopenharmony_ci    return *stackPtr;
10380922886Sopenharmony_ci}
10480922886Sopenharmony_ci
10580922886Sopenharmony_ciSessionStack& AVSessionUsersManager::GetContainerFromAll()
10680922886Sopenharmony_ci{
10780922886Sopenharmony_ci    if (sessionStackForAll_ == nullptr) {
10880922886Sopenharmony_ci        sessionStackForAll_ = std::make_shared<SessionStack>();
10980922886Sopenharmony_ci    }
11080922886Sopenharmony_ci    return *sessionStackForAll_;
11180922886Sopenharmony_ci}
11280922886Sopenharmony_ci
11380922886Sopenharmony_cistd::shared_ptr<std::list<sptr<AVSessionItem>>> AVSessionUsersManager::GetCurSessionListForFront()
11480922886Sopenharmony_ci{
11580922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
11680922886Sopenharmony_ci    std::shared_ptr<std::list<sptr<AVSessionItem>>> sessionListForFront = nullptr;
11780922886Sopenharmony_ci    auto iterForFrontList = frontSessionListMapByUserId_.find(curUserId_);
11880922886Sopenharmony_ci    if (iterForFrontList != frontSessionListMapByUserId_.end()) {
11980922886Sopenharmony_ci        sessionListForFront = iterForFrontList->second;
12080922886Sopenharmony_ci    } else {
12180922886Sopenharmony_ci        SLOGI("GetCurSessionListForFront without curUser: %{public}d, create new", curUserId_);
12280922886Sopenharmony_ci        sessionListForFront = std::make_shared<std::list<sptr<AVSessionItem>>>();
12380922886Sopenharmony_ci        frontSessionListMapByUserId_[curUserId_] = sessionListForFront;
12480922886Sopenharmony_ci    }
12580922886Sopenharmony_ci    return sessionListForFront;
12680922886Sopenharmony_ci}
12780922886Sopenharmony_ci
12880922886Sopenharmony_ciint32_t AVSessionUsersManager::GetCurrentUserId()
12980922886Sopenharmony_ci{
13080922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
13180922886Sopenharmony_ci    return curUserId_;
13280922886Sopenharmony_ci}
13380922886Sopenharmony_ci
13480922886Sopenharmony_cistd::string AVSessionUsersManager::GetDirForCurrentUser(int32_t userId)
13580922886Sopenharmony_ci{
13680922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
13780922886Sopenharmony_ci    if (curUserId_ < 0) {
13880922886Sopenharmony_ci        return AVSESSION_FILE_PUBLIC_DIR;
13980922886Sopenharmony_ci    } else if (userId <= 0) {
14080922886Sopenharmony_ci        return AVSESSION_FILE_DIR_HEAD + std::to_string(curUserId_) + AVSESSION_FILE_DIR_TAIL;
14180922886Sopenharmony_ci    } else {
14280922886Sopenharmony_ci        SLOGI("GetDirForCurrentUser with specific userId:%{public}d", userId);
14380922886Sopenharmony_ci        return AVSESSION_FILE_DIR_HEAD + std::to_string(userId) + AVSESSION_FILE_DIR_TAIL;
14480922886Sopenharmony_ci    }
14580922886Sopenharmony_ci}
14680922886Sopenharmony_ci
14780922886Sopenharmony_ciint32_t AVSessionUsersManager::AddSessionForCurrentUser(pid_t pid,
14880922886Sopenharmony_ci    const std::string& abilityName, sptr<AVSessionItem>& item)
14980922886Sopenharmony_ci{
15080922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
15180922886Sopenharmony_ci    SLOGI("add session for user %{public}d", curUserId_);
15280922886Sopenharmony_ci    int32_t ret = AVSESSION_ERROR;
15380922886Sopenharmony_ci    ret = GetContainerFromAll().AddSession(pid, abilityName, item);
15480922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "error when add session for all");
15580922886Sopenharmony_ci    ret = GetContainerFromUser(curUserId_).AddSession(pid, abilityName, item);
15680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "error when add session for user");
15780922886Sopenharmony_ci    return ret;
15880922886Sopenharmony_ci}
15980922886Sopenharmony_ci
16080922886Sopenharmony_cisptr<AVSessionItem> AVSessionUsersManager::RemoveSessionForAllUser(pid_t pid, const std::string& abilityName)
16180922886Sopenharmony_ci{
16280922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
16380922886Sopenharmony_ci    sptr<AVSessionItem> result;
16480922886Sopenharmony_ci    SLOGI("remove session for pid %{public}d,abilityName %{public}s", static_cast<int>(pid), abilityName.c_str());
16580922886Sopenharmony_ci    result = GetContainerFromAll().RemoveSession(pid, abilityName);
16680922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(result != nullptr, result, "remove session from all get nullptr");
16780922886Sopenharmony_ci    std::string sessionId = result->GetSessionId();
16880922886Sopenharmony_ci    int32_t userId = result->GetUserId();
16980922886Sopenharmony_ci    GetContainerFromUser(userId).RemoveSession(pid, abilityName);
17080922886Sopenharmony_ci    std::string fileName = AVSessionUtils::GetCachePathName(userId) + sessionId + AVSessionUtils::GetFileSuffix();
17180922886Sopenharmony_ci    AVSessionUtils::DeleteFile(fileName);
17280922886Sopenharmony_ci    return result;
17380922886Sopenharmony_ci}
17480922886Sopenharmony_ci
17580922886Sopenharmony_cisptr<AVSessionItem> AVSessionUsersManager::RemoveSessionForAllUser(const std::string& sessionId)
17680922886Sopenharmony_ci{
17780922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
17880922886Sopenharmony_ci    sptr<AVSessionItem> result;
17980922886Sopenharmony_ci    SLOGI("remove session for sessionId %{public}s", AVSessionUtils::GetAnonySessionId(sessionId).c_str());
18080922886Sopenharmony_ci    result = GetContainerFromAll().RemoveSession(sessionId);
18180922886Sopenharmony_ci    CHECK_AND_RETURN_RET_LOG(result != nullptr, result, "remove session from all get nullptr");
18280922886Sopenharmony_ci    int32_t userId = result->GetUserId();
18380922886Sopenharmony_ci    GetContainerFromUser(userId).RemoveSession(sessionId);
18480922886Sopenharmony_ci    std::string fileName = AVSessionUtils::GetCachePathName(userId) + sessionId + AVSessionUtils::GetFileSuffix();
18580922886Sopenharmony_ci    AVSessionUtils::DeleteFile(fileName);
18680922886Sopenharmony_ci    return result;
18780922886Sopenharmony_ci}
18880922886Sopenharmony_ci
18980922886Sopenharmony_cistd::vector<sptr<AVSessionItem>> AVSessionUsersManager::RemoveSessionForAllUser(pid_t pid)
19080922886Sopenharmony_ci{
19180922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
19280922886Sopenharmony_ci    SLOGI("remove session for only pid %{public}d", static_cast<int>(pid));
19380922886Sopenharmony_ci    std::vector<sptr<AVSessionItem>> result;
19480922886Sopenharmony_ci    result = GetContainerFromAll().GetSessionsByPid(pid);
19580922886Sopenharmony_ci    for (auto& sessionItem : result) {
19680922886Sopenharmony_ci        CHECK_AND_RETURN_RET_LOG(sessionItem != nullptr, result, "RemoveSessionForAllUser session null");
19780922886Sopenharmony_ci        std::string sessionId = sessionItem->GetSessionId();
19880922886Sopenharmony_ci        int32_t userId = sessionItem->GetUserId();
19980922886Sopenharmony_ci        GetContainerFromUser(userId).RemoveSession(sessionId);
20080922886Sopenharmony_ci        GetContainerFromAll().RemoveSession(sessionId);
20180922886Sopenharmony_ci    }
20280922886Sopenharmony_ci    return result;
20380922886Sopenharmony_ci}
20480922886Sopenharmony_ci
20580922886Sopenharmony_civoid AVSessionUsersManager::AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener)
20680922886Sopenharmony_ci{
20780922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
20880922886Sopenharmony_ci    SLOGI("add sessionListener for pid %{public}d, curUser %{public}d", static_cast<int>(pid), curUserId_);
20980922886Sopenharmony_ci    auto iterForListenerMap = sessionListenersMapByUserId_.find(curUserId_);
21080922886Sopenharmony_ci    if (iterForListenerMap != sessionListenersMapByUserId_.end()) {
21180922886Sopenharmony_ci        (iterForListenerMap->second)[pid] = listener;
21280922886Sopenharmony_ci    } else {
21380922886Sopenharmony_ci        std::map<pid_t, sptr<ISessionListener>> listenerMap;
21480922886Sopenharmony_ci        listenerMap[pid] = listener;
21580922886Sopenharmony_ci        sessionListenersMapByUserId_[curUserId_] = listenerMap;
21680922886Sopenharmony_ci    }
21780922886Sopenharmony_ci}
21880922886Sopenharmony_ci
21980922886Sopenharmony_civoid AVSessionUsersManager::AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener)
22080922886Sopenharmony_ci{
22180922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
22280922886Sopenharmony_ci    SLOGI("add sessionListener for pid %{public}d, for all users", static_cast<int>(pid));
22380922886Sopenharmony_ci    sessionListenersMap_[pid] = listener;
22480922886Sopenharmony_ci}
22580922886Sopenharmony_ci
22680922886Sopenharmony_civoid AVSessionUsersManager::RemoveSessionListener(pid_t pid)
22780922886Sopenharmony_ci{
22880922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
22980922886Sopenharmony_ci    SLOGI("remove sessionListener for pid %{public}d, curUser %{public}d", static_cast<int>(pid), curUserId_);
23080922886Sopenharmony_ci    auto iterForListenerMap = sessionListenersMapByUserId_.find(curUserId_);
23180922886Sopenharmony_ci    if (iterForListenerMap != sessionListenersMapByUserId_.end()) {
23280922886Sopenharmony_ci        (iterForListenerMap->second).erase(pid);
23380922886Sopenharmony_ci    }
23480922886Sopenharmony_ci    sessionListenersMap_.erase(pid);
23580922886Sopenharmony_ci}
23680922886Sopenharmony_ci
23780922886Sopenharmony_cistd::map<pid_t, sptr<ISessionListener>>& AVSessionUsersManager::GetSessionListener()
23880922886Sopenharmony_ci{
23980922886Sopenharmony_ci    return GetSessionListenerForCurUser();
24080922886Sopenharmony_ci}
24180922886Sopenharmony_ci
24280922886Sopenharmony_cistd::map<pid_t, sptr<ISessionListener>>& AVSessionUsersManager::GetSessionListenerForCurUser()
24380922886Sopenharmony_ci{
24480922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
24580922886Sopenharmony_ci    auto iterForListenerMap = sessionListenersMapByUserId_.find(curUserId_);
24680922886Sopenharmony_ci    if (iterForListenerMap != sessionListenersMapByUserId_.end()) {
24780922886Sopenharmony_ci        return iterForListenerMap->second;
24880922886Sopenharmony_ci    } else {
24980922886Sopenharmony_ci        std::map<pid_t, sptr<ISessionListener>> listenerMap;
25080922886Sopenharmony_ci        sessionListenersMapByUserId_[curUserId_] = listenerMap;
25180922886Sopenharmony_ci        SLOGI("get session listener map with null, create new map and return for user %{public}d", curUserId_);
25280922886Sopenharmony_ci        return sessionListenersMapByUserId_[curUserId_];
25380922886Sopenharmony_ci    }
25480922886Sopenharmony_ci}
25580922886Sopenharmony_ci
25680922886Sopenharmony_cistd::map<pid_t, sptr<ISessionListener>>& AVSessionUsersManager::GetSessionListenerForAllUsers()
25780922886Sopenharmony_ci{
25880922886Sopenharmony_ci    return sessionListenersMap_;
25980922886Sopenharmony_ci}
26080922886Sopenharmony_ci
26180922886Sopenharmony_civoid AVSessionUsersManager::NotifyAccountsEvent(const std::string &type, const int &userId)
26280922886Sopenharmony_ci{
26380922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
26480922886Sopenharmony_ci    // lock for AccountEventsListener callback
26580922886Sopenharmony_ci    AccountManagerAdapter::GetInstance().HandleAccountsEvent(type, userId);
26680922886Sopenharmony_ci}
26780922886Sopenharmony_ci
26880922886Sopenharmony_civoid AVSessionUsersManager::SetTopSession(sptr<AVSessionItem> session)
26980922886Sopenharmony_ci{
27080922886Sopenharmony_ci    SetTopSession(session, curUserId_);
27180922886Sopenharmony_ci}
27280922886Sopenharmony_ci
27380922886Sopenharmony_civoid AVSessionUsersManager::SetTopSession(sptr<AVSessionItem> session, int32_t userId)
27480922886Sopenharmony_ci{
27580922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
27680922886Sopenharmony_ci    topSessionsMapByUserId_[userId] = session;
27780922886Sopenharmony_ci}
27880922886Sopenharmony_ci
27980922886Sopenharmony_cisptr<AVSessionItem> AVSessionUsersManager::GetTopSession()
28080922886Sopenharmony_ci{
28180922886Sopenharmony_ci    return GetTopSession(curUserId_);
28280922886Sopenharmony_ci}
28380922886Sopenharmony_ci
28480922886Sopenharmony_cisptr<AVSessionItem> AVSessionUsersManager::GetTopSession(int32_t userId)
28580922886Sopenharmony_ci{
28680922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
28780922886Sopenharmony_ci    auto iterForTop = topSessionsMapByUserId_.find(userId);
28880922886Sopenharmony_ci    if (iterForTop != topSessionsMapByUserId_.end()) {
28980922886Sopenharmony_ci        return iterForTop->second;
29080922886Sopenharmony_ci    }
29180922886Sopenharmony_ci    return nullptr;
29280922886Sopenharmony_ci}
29380922886Sopenharmony_ci
29480922886Sopenharmony_civoid AVSessionUsersManager::ClearCache()
29580922886Sopenharmony_ci{
29680922886Sopenharmony_ci    std::lock_guard lockGuard(userLock_);
29780922886Sopenharmony_ci    for (const auto& userId : aliveUsers_) {
29880922886Sopenharmony_ci        std::string cachePath(AVSessionUtils::GetCachePathName(userId));
29980922886Sopenharmony_ci        AVSessionUtils::DeleteCacheFiles(cachePath);
30080922886Sopenharmony_ci    }
30180922886Sopenharmony_ci}
30280922886Sopenharmony_ci}
303