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 #ifndef OHOS_AVSESSION_USERS_MANAGER_H
17 #define OHOS_AVSESSION_USERS_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <list>
22 
23 #include "session_stack.h"
24 #include "avsession_log.h"
25 #include "isession_listener.h"
26 
27 namespace OHOS::AVSession {
28 class AVSessionUsersManager {
29 public:
30     static AVSessionUsersManager& GetInstance();
31 
32     void Init();
33 
34     SessionStack& GetContainer();
35 
36     SessionStack& GetContainerFromUser(int32_t userId);
37 
38     SessionStack& GetContainerFromAll();
39 
40     std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurSessionListForFront();
41 
42     int32_t GetCurrentUserId();
43 
44     std::string GetDirForCurrentUser(int32_t userId = 0);
45 
46     int32_t AddSessionForCurrentUser(pid_t pid, const std::string& abilityName, sptr<AVSessionItem>& item);
47 
48     sptr<AVSessionItem> RemoveSessionForAllUser(pid_t pid, const std::string& abilityName);
49 
50     sptr<AVSessionItem> RemoveSessionForAllUser(const std::string& sessionId);
51 
52     std::vector<sptr<AVSessionItem>> RemoveSessionForAllUser(pid_t pid);
53 
54     void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
55 
56     void AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener);
57 
58     void RemoveSessionListener(pid_t pid);
59 
60     std::map<pid_t, sptr<ISessionListener>>& GetSessionListener();
61 
62     std::map<pid_t, sptr<ISessionListener>>& GetSessionListenerForCurUser();
63 
64     std::map<pid_t, sptr<ISessionListener>>& GetSessionListenerForAllUsers();
65 
66     void NotifyAccountsEvent(const std::string &type, const int &userId);
67 
68     void SetTopSession(sptr<AVSessionItem> session);
69 
70     void SetTopSession(sptr<AVSessionItem> session, int32_t userId);
71 
72     sptr<AVSessionItem> GetTopSession();
73 
74     sptr<AVSessionItem> GetTopSession(int32_t userId);
75 
76     void ClearCache();
77 
78     static constexpr const char* accountEventSwitched = "SWITCHED";
79     static constexpr const char* accountEventRemoved = "REMOVED";
80 
81 private:
82     void HandleUserRemoved(int32_t userId);
83 
84     std::map<int32_t, std::shared_ptr<SessionStack>> sessionStackMapByUserId_;
85     std::shared_ptr<SessionStack> sessionStackForAll_;
86     std::map<int32_t, std::shared_ptr<std::list<sptr<AVSessionItem>>>> frontSessionListMapByUserId_;
87     std::map<int32_t, std::map<pid_t, sptr<ISessionListener>>> sessionListenersMapByUserId_;
88     std::map<pid_t, sptr<ISessionListener>> sessionListenersMap_;
89     std::map<pid_t, sptr<AVSessionItem>> topSessionsMapByUserId_;
90 
91     const std::string AVSESSION_FILE_PUBLIC_DIR = "/data/service/el2/public/av_session/";
92     const std::string AVSESSION_FILE_DIR_HEAD = "/data/service/el2/";
93     const std::string AVSESSION_FILE_DIR_TAIL = "/av_session/";
94 
95     int32_t curUserId_ = -1;
96     std::list<int32_t> aliveUsers_;
97     std::recursive_mutex userLock_;
98 };
99 }
100 #endif // OHOS_AVSESSION_USERS_MANAGER_H
101