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 #ifndef PERMISSION_USED_RECORD_DB_H
17 #define PERMISSION_USED_RECORD_DB_H
18 
19 #include <set>
20 
21 #include "generic_values.h"
22 #include "permission_record.h"
23 
24 #include "nocopyable.h"
25 #include "rwlock.h"
26 #include "sqlite_helper.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 struct SqliteTable {
32 public:
33     std::string tableName_;
34     std::vector<std::string> tableColumnNames_;
35 };
36 class PermissionUsedRecordDb : public SqliteHelper {
37 public:
38     enum DataType {
39         PERMISSION_RECORD,
40         PERMISSION_USED_TYPE,
41     };
42     enum ExecuteResult { FAILURE = -1, SUCCESS };
43     static PermissionUsedRecordDb& GetInstance();
44 
45     ~PermissionUsedRecordDb() override;
46 
47     int32_t Add(DataType type, const std::vector<GenericValues>& values);
48     int32_t Remove(DataType type, const GenericValues& conditions);
49     int32_t FindByConditions(DataType type, const std::set<int32_t>& opCodeList, const GenericValues& andConditions,
50         std::vector<GenericValues>& results, int32_t databaseQueryCount);
51     int32_t Count(DataType type);
52     int32_t DeleteExpireRecords(DataType type, const GenericValues& andConditions);
53     int32_t DeleteExcessiveRecords(DataType type, uint32_t excessiveSize);
54     int32_t Update(DataType type, const GenericValues& modifyValue, const GenericValues& conditionValue);
55     int32_t Query(DataType type, const GenericValues& conditionValue, std::vector<GenericValues>& results);
56 
57     void OnCreate() override;
58     void OnUpdate(int32_t version) override;
59 
60 private:
61     PermissionUsedRecordDb();
62     DISALLOW_COPY_AND_MOVE(PermissionUsedRecordDb);
63 
64     std::map<DataType, SqliteTable> dataTypeToSqlTable_;
65     OHOS::Utils::RWLock rwLock_;
66 
67     int32_t CreatePermissionRecordTable() const;
68     int32_t CreatePermissionUsedTypeTable() const;
69     int32_t InsertLockScreenStatusColumn() const;
70     int32_t InsertPermissionUsedTypeColumn() const;
71     int32_t UpdatePermissionRecordTablePrimaryKey() const;
72 
73     std::string CreateInsertPrepareSqlCmd(DataType type) const;
74     std::string CreateDeletePrepareSqlCmd(
75         DataType type, const std::vector<std::string>& columnNames = std::vector<std::string>()) const;
76     std::string CreateSelectByConditionPrepareSqlCmd(const int32_t tokenId, DataType type,
77         const std::set<int32_t>& opCodeList, const std::vector<std::string>& andColumns,
78         int32_t databaseQueryCount) const;
79     std::string CreateUpdatePrepareSqlCmd(DataType type, const std::vector<std::string>& modifyColumns,
80         const std::vector<std::string>& conditionColumns) const;
81     std::string CreateCountPrepareSqlCmd(DataType type) const;
82     std::string CreateDeleteExpireRecordsPrepareSqlCmd(DataType type,
83         const std::vector<std::string>& andColumns) const;
84     std::string CreateDeleteExcessiveRecordsPrepareSqlCmd(DataType type, uint32_t excessiveSize) const;
85     std::string CreateQueryPrepareSqlCmd(DataType type, const std::vector<std::string>& conditionColumns) const;
86 
87 private:
88     inline static constexpr const char* PERMISSION_RECORD_TABLE = "permission_record_table";
89     inline static constexpr const char* PERMISSION_USED_TYPE_TABLE = "permission_used_type_table";
90     inline static constexpr const char* DATABASE_NAME = "permission_used_record.db";
91     inline static constexpr const char* DATABASE_PATH = "/data/service/el1/public/access_token/";
92     static const int32_t DATABASE_VERSION = 4;
93 };
94 } // namespace AccessToken
95 } // namespace Security
96 } // namespace OHOS
97 
98 #endif // PERMISSION_USED_RECORD_DB_H
99