• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "session_stack.h"
17 #include "avsession_errors.h"
18 #include "avsession_sysevent.h"
19 #include "avsession_utils.h"
20 
21 namespace OHOS::AVSession {
AddSession(pid_t pid,const std::string & abilityName,sptr<AVSessionItem> & item)22 int32_t SessionStack::AddSession(pid_t pid, const std::string& abilityName, sptr<AVSessionItem>& item)
23 {
24     if (sessions_.size() >= SessionContainer::SESSION_NUM_MAX) {
25         return ERR_SESSION_EXCEED_MAX;
26     }
27     sessions_.insert(std::make_pair(std::make_pair(pid, abilityName), item));
28     stack_.push_front(item);
29     HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_CREATE_SESSION);
30     return AVSESSION_SUCCESS;
31 }
32 
RemoveSession(pid_t pid)33 std::vector<sptr<AVSessionItem>> SessionStack::RemoveSession(pid_t pid)
34 {
35     std::vector<sptr<AVSessionItem>> result;
36     for (auto it = sessions_.begin(); it != sessions_.end();) {
37         if (it->first.first == pid) {
38             std::string sessionId = it->second->GetSessionId();
39             std::string fileName = AVSessionUtils::GetCachePathName() + sessionId + AVSessionUtils::GetFileSuffix();
40             AVSessionUtils::DeleteFile(fileName);
41             result.push_back(it->second);
42             stack_.remove(it->second);
43             it = sessions_.erase(it);
44             HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_DELETE_SESSION);
45         } else {
46             it++;
47         }
48     }
49     return result;
50 }
51 
RemoveSession(const std::string & sessionId)52 sptr<AVSessionItem> SessionStack::RemoveSession(const std::string& sessionId)
53 {
54     sptr<AVSessionItem> result;
55     for (auto it = sessions_.begin(); it != sessions_.end();) {
56         if (it->second->GetSessionId() == sessionId) {
57             std::string fileName = AVSessionUtils::GetCachePathName() + sessionId + AVSessionUtils::GetFileSuffix();
58             AVSessionUtils::DeleteFile(fileName);
59             result = it->second;
60             stack_.remove(it->second);
61             it = sessions_.erase(it);
62             HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_DELETE_SESSION);
63         } else {
64             it++;
65         }
66     }
67     return result;
68 }
69 
RemoveSession(pid_t pid,const std::string & abilityName)70 sptr<AVSessionItem> SessionStack::RemoveSession(pid_t pid, const std::string& abilityName)
71 {
72     auto it = sessions_.find(std::make_pair(pid, abilityName));
73     if (it == sessions_.end()) {
74         return nullptr;
75     }
76     HISYSEVENT_ADD_OPERATION_COUNT(Operation::OPT_DELETE_SESSION);
77     auto result = it->second;
78     std::string sessionId = result->GetSessionId();
79     std::string fileName = AVSessionUtils::GetCachePathName() + sessionId + AVSessionUtils::GetFileSuffix();
80     AVSessionUtils::DeleteFile(fileName);
81     sessions_.erase(it);
82     stack_.remove(result);
83     return result;
84 }
85 
GetSession(pid_t pid,const std::string & abilityName)86 sptr<AVSessionItem> SessionStack::GetSession(pid_t pid, const std::string& abilityName)
87 {
88     auto it = sessions_.find(std::make_pair(pid, abilityName));
89     if (it == sessions_.end()) {
90         return nullptr;
91     }
92     return it->second;
93 }
94 
GetSessionById(const std::string & sessionId)95 sptr<AVSessionItem> SessionStack::GetSessionById(const std::string& sessionId)
96 {
97     for (const auto& session : stack_) {
98         if (session->GetSessionId() == sessionId) {
99             return session;
100         }
101     }
102     return nullptr;
103 }
104 
GetAllSessions()105 std::vector<sptr<AVSessionItem>> SessionStack::GetAllSessions()
106 {
107     std::vector<sptr<AVSessionItem>> result;
108     for (const auto& session : stack_) {
109         result.push_back(session);
110     }
111     return result;
112 }
113 } // namespace OHOS::AVSession