• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(int32_t userId);
41 
42     std::shared_ptr<std::list<sptr<AVSessionItem>>> GetCurSessionListForKeyEvent(int32_t userId);
43 
44     int32_t GetCurrentUserId();
45 
46     std::string GetDirForCurrentUser(int32_t userId = 0);
47 
48     int32_t AddSessionForCurrentUser(pid_t pid, const std::string& abilityName, sptr<AVSessionItem>& item);
49 
50     int32_t UpdateSessionForCurrentUser(pid_t pid, const std::string& oldAbilityName,
51         const std::string& newAbilityName, sptr<AVSessionItem>& item);
52 
53     sptr<AVSessionItem> RemoveSessionForAllUser(pid_t pid, const std::string& abilityName);
54 
55     sptr<AVSessionItem> RemoveSessionForAllUser(const std::string& sessionId);
56 
57     std::vector<sptr<AVSessionItem>> RemoveSessionForAllUser(pid_t pid);
58 
59     void AddSessionListener(pid_t pid, const sptr<ISessionListener>& listener);
60 
61     void AddSessionListenerForAllUsers(pid_t pid, const sptr<ISessionListener>& listener);
62 
63     void RemoveSessionListener(pid_t pid);
64 
65     std::map<pid_t, sptr<ISessionListener>>& GetSessionListener(int32_t userId = 0);
66 
67     std::map<pid_t, sptr<ISessionListener>>& GetSessionListenerForAllUsers();
68 
69     void NotifyAccountsEvent(const std::string &type, const int &userId);
70 
71     void SetTopSession(sptr<AVSessionItem> session);
72 
73     void SetTopSession(sptr<AVSessionItem> session, int32_t userId);
74 
75     sptr<AVSessionItem> GetTopSession();
76 
77     sptr<AVSessionItem> GetTopSession(int32_t userId);
78 
79     void ClearCache();
80 
81     static constexpr const char* accountEventSwitched = "SWITCHED";
82     static constexpr const char* accountEventRemoved = "REMOVED";
83 
84 private:
85     void HandleUserRemoved(int32_t userId);
86 
87     std::map<int32_t, std::shared_ptr<SessionStack>> sessionStackMapByUserId_;
88     std::shared_ptr<SessionStack> sessionStackForAll_;
89     std::map<int32_t, std::shared_ptr<std::list<sptr<AVSessionItem>>>> frontSessionListMapByUserId_;
90     std::map<int32_t, std::shared_ptr<std::list<sptr<AVSessionItem>>>> keyEventListMapByUserId_;
91     std::map<int32_t, std::map<pid_t, sptr<ISessionListener>>> sessionListenersMapByUserId_;
92     std::map<pid_t, sptr<ISessionListener>> sessionListenersMap_;
93     std::map<pid_t, sptr<AVSessionItem>> topSessionsMapByUserId_;
94 
95     const std::string AVSESSION_FILE_PUBLIC_DIR = "/data/service/el2/public/av_session/";
96     const std::string AVSESSION_FILE_DIR_HEAD = "/data/service/el2/";
97     const std::string AVSESSION_FILE_DIR_TAIL = "/av_session/";
98 
99     int32_t curUserId_ = 100;
100     std::list<int32_t> aliveUsers_;
101     std::recursive_mutex userLock_;
102 };
103 }
104 #endif // OHOS_AVSESSION_USERS_MANAGER_H
105