• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "admin_container.h"
17 #include "edm_log.h"
18 
19 namespace OHOS {
20 namespace EDM {
21 
22 using ReadLock = std::shared_lock<std::shared_mutex>;
23 using WriteLock = std::unique_lock<std::shared_mutex>;
24 
GetInstance()25 std::shared_ptr<AdminContainer> AdminContainer::GetInstance()
26 {
27     static std::shared_ptr<AdminContainer> instance = std::make_shared<AdminContainer>();
28     return instance;
29 }
30 
AdminContainer()31 AdminContainer::AdminContainer()
32 {
33     EDMLOGI("AdminContainer::AdminContainer");
34 }
35 
~AdminContainer()36 AdminContainer::~AdminContainer()
37 {
38     EDMLOGI("AdminContainer::~AdminContainer");
39     admins_.clear();
40 }
41 
SetAdminByUserId(int32_t userId,const Admin & adminItem)42 void AdminContainer::SetAdminByUserId(int32_t userId, const Admin &adminItem)
43 {
44     WriteLock lock(adminMutex_);
45     std::vector<std::shared_ptr<Admin>> userAdmin;
46     GetAdminByUserId(userId, userAdmin);
47 
48     std::shared_ptr<Admin> admin = std::make_shared<Admin>(adminItem);
49     userAdmin.emplace_back(admin);
50     admins_[userId] = userAdmin;
51 }
52 
DeleteAdmin(const std::string & packageName,int32_t userId)53 bool AdminContainer::DeleteAdmin(const std::string &packageName, int32_t userId)
54 {
55     WriteLock lock(adminMutex_);
56     auto iterMap = admins_.find(userId);
57     if (iterMap == admins_.end()) {
58         EDMLOGI("DeleteAdmin::get userId Admin failed.");
59         return false;
60     }
61 
62     auto iter = std::remove_if(iterMap->second.begin(), iterMap->second.end(),
63         [&](std::shared_ptr<Admin> admin) { return admin->adminInfo_.packageName_ == packageName; });
64     iterMap->second.erase(iter, iterMap->second.end());
65     if (iterMap->second.empty()) {
66         admins_.erase(iterMap);
67     }
68     return true;
69 }
70 
GetAdminCopyByUserId(int32_t userId,std::vector<std::shared_ptr<Admin>> & admins)71 bool AdminContainer::GetAdminCopyByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &admins)
72 {
73     ReadLock lock(adminMutex_);
74     admins.clear();
75     auto iter = admins_.find(userId);
76     if (iter == admins_.end()) {
77         EDMLOGW("GetAdminCopyByUserId::get userId Admin failed.");
78         return false;
79     }
80     for (const auto &item : iter->second) {
81         admins.emplace_back(std::make_shared<Admin>(*item));
82     }
83     return true;
84 }
85 
GetAdminCopyBySubscribeEvent(ManagedEvent event,std::unordered_map<int32_t,std::vector<std::shared_ptr<Admin>>> & subscribeAdmins)86 void AdminContainer::GetAdminCopyBySubscribeEvent(ManagedEvent event,
87     std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> &subscribeAdmins)
88 {
89     ReadLock lock(adminMutex_);
90     for (const auto &adminItem : admins_) {
91         std::vector<std::shared_ptr<Admin>> subAdmin;
92         for (const auto &it : adminItem.second) {
93             std::vector<ManagedEvent> events = it->adminInfo_.managedEvents_;
94             if (std::find(events.begin(), events.end(), event) != events.end()) {
95                 subAdmin.push_back(std::make_shared<Admin>(*it));
96             }
97         }
98         if (!subAdmin.empty()) {
99             subscribeAdmins[adminItem.first] = subAdmin;
100         }
101     }
102 }
103 
GetAdminByUserId(int32_t userId,std::vector<std::shared_ptr<Admin>> & userAdmin)104 bool AdminContainer::GetAdminByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &userAdmin)
105 {
106     userAdmin.clear();
107     auto iter = admins_.find(userId);
108     if (iter == admins_.end()) {
109         EDMLOGW("GetAdminByUserId::get userId Admin failed.");
110         return false;
111     }
112     userAdmin = iter->second;
113     return true;
114 }
115 
HasAdmin(int32_t userId)116 bool AdminContainer::HasAdmin(int32_t userId)
117 {
118     ReadLock lock(adminMutex_);
119     auto iter = admins_.find(userId);
120     if (iter == admins_.end()) {
121         return false;
122     }
123     return true;
124 }
125 
IsAdminExist()126 bool AdminContainer::IsAdminExist()
127 {
128     ReadLock lock(adminMutex_);
129     return !admins_.empty();
130 }
131 
UpdateAdmin(int32_t userId,const std::string & packageName,uint32_t updateCode,const Admin & adminItem)132 void AdminContainer::UpdateAdmin(int32_t userId, const std::string &packageName, uint32_t updateCode,
133     const Admin &adminItem)
134 {
135     WriteLock lock(adminMutex_);
136     std::vector<std::shared_ptr<Admin>> userAdmin;
137     if (!GetAdminByUserId(userId, userAdmin)) {
138         EDMLOGW("UpdateAdmin::get userId Admin failed.");
139         return;
140     }
141     for (const auto &item : userAdmin) {
142         if (item->adminInfo_.packageName_ == packageName) {
143             if (updateCode & PACKAGE_NAME) {
144                 item->adminInfo_.packageName_ = adminItem.adminInfo_.packageName_;
145             }
146             if (updateCode & CLASS_NAME) {
147                 item->adminInfo_.className_ = adminItem.adminInfo_.className_;
148             }
149             if (updateCode & ENTI_NFO) {
150                 item->adminInfo_.entInfo_ = adminItem.adminInfo_.entInfo_;
151             }
152             if (updateCode & PERMISSION) {
153                 item->adminInfo_.permission_ = adminItem.adminInfo_.permission_;
154             }
155             if (updateCode & MANAGED_EVENTS) {
156                 item->adminInfo_.managedEvents_ = adminItem.adminInfo_.managedEvents_;
157             }
158             if (updateCode & PARENT_ADMIN_NAME) {
159                 item->adminInfo_.parentAdminName_ = adminItem.adminInfo_.parentAdminName_;
160             }
161             if (updateCode & ACCESSIBLE_POLICIES) {
162                 item->adminInfo_.accessiblePolicies_ = adminItem.adminInfo_.accessiblePolicies_;
163             }
164             if (updateCode & ADMIN_TYPE) {
165                 item->adminInfo_.adminType_ = adminItem.adminInfo_.adminType_;
166             }
167             if (updateCode & IS_DEBUG) {
168                 item->adminInfo_.isDebug_ = adminItem.adminInfo_.isDebug_;
169             }
170             if (updateCode & RUNNING_MODE) {
171                 item->adminInfo_.runningMode_ = adminItem.adminInfo_.runningMode_;
172             }
173         }
174     }
175     return;
176 }
177 
UpdateParentAdminName(int32_t userId,const std::string & parentAdminName,const std::string & updateName)178 void AdminContainer::UpdateParentAdminName(int32_t userId, const std::string &parentAdminName,
179     const std::string &updateName)
180 {
181     WriteLock lock(adminMutex_);
182     std::vector<std::shared_ptr<Admin>> userAdmin;
183     if (!GetAdminByUserId(userId, userAdmin)) {
184         EDMLOGW("UpdateParentAdminName::get userId Admin failed.");
185         return;
186     }
187 
188     for (const auto &admin : userAdmin) {
189         if ((admin->GetAdminType() == AdminType::SUB_SUPER_ADMIN ||
190             admin->GetAdminType() == AdminType::VIRTUAL_ADMIN) &&
191             admin->adminInfo_.parentAdminName_ == parentAdminName) {
192             admin->adminInfo_.parentAdminName_ = updateName;
193         }
194     }
195 }
196 
InitAdmins(std::unordered_map<int32_t,std::vector<std::shared_ptr<Admin>>> admins)197 void AdminContainer::InitAdmins(std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> admins)
198 {
199     WriteLock lock(adminMutex_);
200     admins_ = admins;
201 }
202 
Dump()203 void AdminContainer::Dump()
204 {
205     ReadLock lock(adminMutex_);
206     for (const auto &entry : admins_) {
207         EDMLOGI("AdminContainer::Dump %{public}d.", entry.first);
208         for (const auto &admin : entry.second) {
209             EDMLOGI("AdminContainer::Dump admin info adminType_ %{public}d.",
210                 admin->adminInfo_.adminType_);
211             EDMLOGI("AdminContainer::Dump admin info packageName_ %{public}s.",
212                 admin->adminInfo_.packageName_.c_str());
213             EDMLOGI("AdminContainer::Dump admin info parentAdminName_ %{public}s.",
214                 admin->adminInfo_.parentAdminName_.c_str());
215         }
216     }
217 }
218 
ClearAdmins()219 void AdminContainer::ClearAdmins()
220 {
221     WriteLock lock(adminMutex_);
222     admins_.clear();
223 }
224 
InsertAdmins(int32_t userId,std::vector<std::shared_ptr<Admin>> admins)225 void AdminContainer::InsertAdmins(int32_t userId, std::vector<std::shared_ptr<Admin>> admins)
226 {
227     WriteLock lock(adminMutex_);
228     admins_[userId] = admins;
229 }
230 } // namespace EDM
231 } // namespace OHOS