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#ifndef OHOS_AVSESSION_USERS_MANAGER_H
1780922886Sopenharmony_ci#define OHOS_AVSESSION_USERS_MANAGER_H
1880922886Sopenharmony_ci
1980922886Sopenharmony_ci#include <map>
2080922886Sopenharmony_ci#include <mutex>
2180922886Sopenharmony_ci#include <list>
2280922886Sopenharmony_ci
2380922886Sopenharmony_ci#include "session_stack.h"
2480922886Sopenharmony_ci#include "avsession_log.h"
2580922886Sopenharmony_ci#include "isession_listener.h"
2680922886Sopenharmony_ci
2780922886Sopenharmony_cinamespace OHOS::AVSession {
2880922886Sopenharmony_ciclass AVSessionUsersManager {
2980922886Sopenharmony_cipublic:
3080922886Sopenharmony_ci    static AVSessionUsersManager& GetInstance();
3180922886Sopenharmony_ci
3280922886Sopenharmony_ci    void Init();
3380922886Sopenharmony_ci
3480922886Sopenharmony_ci    SessionStack& GetContainer();
3580922886Sopenharmony_ci
3680922886Sopenharmony_ci    SessionStack& GetContainerFromUser(int32_t userId);
3780922886Sopenharmony_ci
3880922886Sopenharmony_ci    SessionStack& GetContainerFromAll();
3980922886Sopenharmony_ci
4080922886Sopenharmony_ci    std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurSessionListForFront();
4180922886Sopenharmony_ci
4280922886Sopenharmony_ci    int32_t GetCurrentUserId();
4380922886Sopenharmony_ci
4480922886Sopenharmony_ci    std::string GetDirForCurrentUser(int32_t userId = 0);
4580922886Sopenharmony_ci
4680922886Sopenharmony_ci    int32_t AddSessionForCurrentUser(pid_t pid, const std::string& abilityName, sptr<AVSessionItem>& item);
4780922886Sopenharmony_ci
4880922886Sopenharmony_ci    sptr<AVSessionItem> RemoveSessionForAllUser(pid_t pid, const std::string& abilityName);
4980922886Sopenharmony_ci
5080922886Sopenharmony_ci    sptr<AVSessionItem> RemoveSessionForAllUser(const std::string& sessionId);
5180922886Sopenharmony_ci
5280922886Sopenharmony_ci    std::vector<sptr<AVSessionItem>> RemoveSessionForAllUser(pid_t pid);
5380922886Sopenharmony_ci
5480922886Sopenharmony_ci    void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
5580922886Sopenharmony_ci
5680922886Sopenharmony_ci    void AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener);
5780922886Sopenharmony_ci
5880922886Sopenharmony_ci    void RemoveSessionListener(pid_t pid);
5980922886Sopenharmony_ci
6080922886Sopenharmony_ci    std::map<pid_t, sptr<ISessionListener>>& GetSessionListener();
6180922886Sopenharmony_ci
6280922886Sopenharmony_ci    std::map<pid_t, sptr<ISessionListener>>& GetSessionListenerForCurUser();
6380922886Sopenharmony_ci
6480922886Sopenharmony_ci    std::map<pid_t, sptr<ISessionListener>>& GetSessionListenerForAllUsers();
6580922886Sopenharmony_ci
6680922886Sopenharmony_ci    void NotifyAccountsEvent(const std::string &type, const int &userId);
6780922886Sopenharmony_ci
6880922886Sopenharmony_ci    void SetTopSession(sptr<AVSessionItem> session);
6980922886Sopenharmony_ci
7080922886Sopenharmony_ci    void SetTopSession(sptr<AVSessionItem> session, int32_t userId);
7180922886Sopenharmony_ci
7280922886Sopenharmony_ci    sptr<AVSessionItem> GetTopSession();
7380922886Sopenharmony_ci
7480922886Sopenharmony_ci    sptr<AVSessionItem> GetTopSession(int32_t userId);
7580922886Sopenharmony_ci
7680922886Sopenharmony_ci    void ClearCache();
7780922886Sopenharmony_ci
7880922886Sopenharmony_ci    static constexpr const char* accountEventSwitched = "SWITCHED";
7980922886Sopenharmony_ci    static constexpr const char* accountEventRemoved = "REMOVED";
8080922886Sopenharmony_ci
8180922886Sopenharmony_ciprivate:
8280922886Sopenharmony_ci    void HandleUserRemoved(int32_t userId);
8380922886Sopenharmony_ci
8480922886Sopenharmony_ci    std::map<int32_t, std::shared_ptr<SessionStack>> sessionStackMapByUserId_;
8580922886Sopenharmony_ci    std::shared_ptr<SessionStack> sessionStackForAll_;
8680922886Sopenharmony_ci    std::map<int32_t, std::shared_ptr<std::list<sptr<AVSessionItem>>>> frontSessionListMapByUserId_;
8780922886Sopenharmony_ci    std::map<int32_t, std::map<pid_t, sptr<ISessionListener>>> sessionListenersMapByUserId_;
8880922886Sopenharmony_ci    std::map<pid_t, sptr<ISessionListener>> sessionListenersMap_;
8980922886Sopenharmony_ci    std::map<pid_t, sptr<AVSessionItem>> topSessionsMapByUserId_;
9080922886Sopenharmony_ci
9180922886Sopenharmony_ci    const std::string AVSESSION_FILE_PUBLIC_DIR = "/data/service/el2/public/av_session/";
9280922886Sopenharmony_ci    const std::string AVSESSION_FILE_DIR_HEAD = "/data/service/el2/";
9380922886Sopenharmony_ci    const std::string AVSESSION_FILE_DIR_TAIL = "/av_session/";
9480922886Sopenharmony_ci
9580922886Sopenharmony_ci    int32_t curUserId_ = -1;
9680922886Sopenharmony_ci    std::list<int32_t> aliveUsers_;
9780922886Sopenharmony_ci    std::recursive_mutex userLock_;
9880922886Sopenharmony_ci};
9980922886Sopenharmony_ci}
10080922886Sopenharmony_ci#endif // OHOS_AVSESSION_USERS_MANAGER_H
101