1 /*
2  * Copyright (c) 2021-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 #ifndef PERMISSION_DEFINITION_CACHE_H
17 #define PERMISSION_DEFINITION_CACHE_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "data_translator.h"
23 #include "permission_def.h"
24 
25 #include "rwlock.h"
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 class PermissionDefinitionCache final {
32 public:
33     static PermissionDefinitionCache& GetInstance();
34 
35     virtual ~PermissionDefinitionCache();
36 
37     bool Insert(const PermissionDef& info, AccessTokenID tokenId);
38 
39     bool Update(const PermissionDef& info, AccessTokenID tokenId);
40 
41     void DeleteByToken(AccessTokenID tokenId);
42 
43     int FindByPermissionName(const std::string& permissionName, PermissionDef& info);
44 
45     bool IsSystemGrantedPermission(const std::string& permissionName);
46 
47     bool IsUserGrantedPermission(const std::string& permissionName);
48 
49     bool HasDefinition(const std::string& permissionName);
50 
51     bool IsHapPermissionDefEmpty();
52 
53     void StorePermissionDef(std::vector<GenericValues>& valueList);
54 
55     void StorePermissionDef(AccessTokenID tokenID, std::vector<GenericValues>& valueList);
56 
57     void GetDefPermissionsByTokenId(std::vector<PermissionDef>& permList, AccessTokenID tokenId);
58 
59     int32_t RestorePermDefInfo(std::vector<GenericValues>& permDefRes);
60 
61     bool HasHapPermissionDefinitionForHap(const std::string& permissionName);
62 
63     uint32_t GetDefPermissionsSize();
64 
65 private:
66     PermissionDefinitionCache();
67 
68     bool IsGrantedModeEqualInner(const std::string& permissionName, int grantMode) const;
69 
70     DISALLOW_COPY_AND_MOVE(PermissionDefinitionCache);
71 
72     /**
73      * key: the permission name.
74      * value: the object of PermissionDefData.
75      */
76     std::map<std::string, PermissionDefData> permissionDefinitionMap_;
77 
78     bool hasHapPermissionDefinition_ = false;
79 
80     OHOS::Utils::RWLock cacheLock_;
81 };
82 } // namespace AccessToken
83 } // namespace Security
84 } // namespace OHOS
85 #endif // PERMISSION_DEFINITION_CACHE_H
86