• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-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 #ifndef SERVICES_EDM_INCLUDE_ADMIN_CONTAINER_H
17 #define SERVICES_EDM_INCLUDE_ADMIN_CONTAINER_H
18 
19 #include <map>
20 #include <memory>
21 #include <shared_mutex>
22 #include <unordered_map>
23 #include "admin.h"
24 
25 
26 namespace OHOS {
27 namespace EDM {
28 #define PACKAGE_NAME 1
29 #define CLASS_NAME (1 << 1)
30 #define ENTI_NFO (1 << 2)
31 #define PERMISSION (1 << 3)
32 #define MANAGED_EVENTS (1 << 4)
33 #define PARENT_ADMIN_NAME (1 << 5)
34 #define ACCESSIBLE_POLICIES (1 << 6)
35 #define ADMIN_TYPE (1 << 7)
36 #define IS_DEBUG (1 << 8)
37 #define RUNNING_MODE (1 << 9)
38 
39 /**
40  * This class provides thread-safe access to admin data, with all public methods
41  * implementing synchronization control through internal locking mechanisms.
42  * It is suitable for admin information management scenarios in multi-threaded environments.
43  */
44 class AdminContainer {
45 public:
46     AdminContainer();
47 
48     ~AdminContainer();
49 
50     static std::shared_ptr<AdminContainer> GetInstance();
51 
52     void SetAdminByUserId(int32_t userId, const Admin &adminItem);
53 
54     bool DeleteAdmin(const std::string &packageName, int32_t userId);
55 
56     /**
57      * Get a thread-safe independent copy of the admin object for the specified user ID.
58      */
59     bool GetAdminCopyByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &admins);
60 
61     /**
62      * Get a thread-safe independent copy of the admin object for the specified event.
63      */
64     void GetAdminCopyBySubscribeEvent(ManagedEvent event,
65         std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> &subscribeAdmins);
66 
67     bool HasAdmin(int32_t userId);
68 
69     bool IsAdminExist();
70 
71     void UpdateAdmin(int32_t userId, const std::string &packageName, uint32_t updateCode, const Admin &adminItem);
72 
73     void UpdateParentAdminName(int32_t userId, const std::string &parentAdminName, const std::string &updateName);
74 
75     void InitAdmins(std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> admins);
76 
77     void ClearAdmins();
78 
79     void InsertAdmins(int32_t userId, std::vector<std::shared_ptr<Admin>> admins);
80 
81     void Dump();
82 
83 private:
84     bool GetAdminByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &userAdmin);
85 
86     std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> admins_;
87     std::shared_mutex adminMutex_;
88 };
89 } // namespace EDM
90 } // namespace OHOS
91 
92 #endif // SERVICES_EDM_INCLUDE_ADMIN_CONTAINER_H