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 "permission_record_repository.h"
17
18 #include "accesstoken_log.h"
19 #include "permission_used_record_db.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PermissionRecordRepository"
27 };
28 }
29
GetInstance()30 PermissionRecordRepository& PermissionRecordRepository::GetInstance()
31 {
32 static PermissionRecordRepository instance;
33 return instance;
34 }
35
PermissionRecordRepository()36 PermissionRecordRepository::PermissionRecordRepository()
37 {
38 }
39
~PermissionRecordRepository()40 PermissionRecordRepository::~PermissionRecordRepository()
41 {
42 }
43
AddRecordValues(const std::vector<GenericValues> & recordValues)44 bool PermissionRecordRepository::AddRecordValues(const std::vector<GenericValues>& recordValues)
45 {
46 if (PermissionUsedRecordDb::GetInstance().Add(PermissionUsedRecordDb::PERMISSION_RECORD, recordValues)
47 != PermissionUsedRecordDb::SUCCESS) {
48 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD table add fail");
49 return false;
50 }
51 return true;
52 }
53
FindRecordValues(const std::set<int32_t> & opCodeList,const GenericValues & andConditionValues,std::vector<GenericValues> & recordValues)54 bool PermissionRecordRepository::FindRecordValues(const std::set<int32_t>& opCodeList,
55 const GenericValues& andConditionValues, std::vector<GenericValues>& recordValues)
56 {
57 if (PermissionUsedRecordDb::GetInstance().FindByConditions(PermissionUsedRecordDb::PERMISSION_RECORD,
58 opCodeList, andConditionValues, recordValues) != PermissionUsedRecordDb::SUCCESS) {
59 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD table find fail");
60 return false;
61 }
62 return true;
63 }
64
RemoveRecordValues(const GenericValues & conditionValues)65 bool PermissionRecordRepository::RemoveRecordValues(const GenericValues& conditionValues)
66 {
67 if (PermissionUsedRecordDb::GetInstance().Remove(PermissionUsedRecordDb::PERMISSION_RECORD, conditionValues)
68 != PermissionUsedRecordDb::SUCCESS) {
69 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD table add fail");
70 return false;
71 }
72 return true;
73 }
74
GetAllRecordValuesByKey(const std::string & condition,std::vector<GenericValues> & resultValues)75 bool PermissionRecordRepository::GetAllRecordValuesByKey(
76 const std::string& condition, std::vector<GenericValues>& resultValues)
77 {
78 if (PermissionUsedRecordDb::GetInstance().GetDistinctValue(PermissionUsedRecordDb::PERMISSION_RECORD,
79 condition, resultValues) != PermissionUsedRecordDb::SUCCESS) {
80 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD table add fail");
81 return false;
82 }
83 return true;
84 }
85
CountRecordValues(GenericValues & resultValues)86 void PermissionRecordRepository::CountRecordValues(GenericValues& resultValues)
87 {
88 PermissionUsedRecordDb::GetInstance().Count(PermissionUsedRecordDb::PERMISSION_RECORD, resultValues);
89 }
90
DeleteExpireRecordsValues(const GenericValues & andConditions)91 bool PermissionRecordRepository::DeleteExpireRecordsValues(const GenericValues& andConditions)
92 {
93 if (PermissionUsedRecordDb::GetInstance().DeleteExpireRecords(PermissionUsedRecordDb::PERMISSION_RECORD,
94 andConditions) != PermissionUsedRecordDb::SUCCESS) {
95 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD delete fail");
96 return false;
97 }
98 return true;
99 }
100
DeleteExcessiveSizeRecordValues(uint32_t excessiveSize)101 bool PermissionRecordRepository::DeleteExcessiveSizeRecordValues(uint32_t excessiveSize)
102 {
103 if (PermissionUsedRecordDb::GetInstance().DeleteExcessiveRecords(PermissionUsedRecordDb::PERMISSION_RECORD,
104 excessiveSize) != PermissionUsedRecordDb::SUCCESS) {
105 ACCESSTOKEN_LOG_ERROR(LABEL, "PERMISSION_RECORD delete fail");
106 return false;
107 }
108 return true;
109 }
110 } // namespace AccessToken
111 } // namespace Security
112 } // namespace OHOS