1 /*
2  * Copyright (c) 2021-2024 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 ACCESS_TOKEN_DB_H
17 #define ACCESS_TOKEN_DB_H
18 
19 #include <vector>
20 
21 #include "access_token.h"
22 
23 #include "access_token_db_util.h"
24 #include "generic_values.h"
25 #include "nocopyable.h"
26 #include "rdb_predicates.h"
27 #include "rdb_store.h"
28 #include "rwlock.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 class AccessTokenDb final {
34 public:
35     static AccessTokenDb& GetInstance();
36     virtual ~AccessTokenDb() = default;
37 
38     int32_t Add(const AtmDataType type, const std::vector<GenericValues>& values);
39     int32_t Remove(const AtmDataType type, const GenericValues& conditionValue);
40     int32_t Modify(const AtmDataType type, const GenericValues& modifyValue, const GenericValues& conditionValue);
41     int32_t Find(AtmDataType type, const GenericValues& conditionValue, std::vector<GenericValues>& results);
42     int32_t DeleteAndInsertHap(AccessTokenID tokenId, const std::vector<GenericValues>& hapInfoValues,
43         const std::vector<GenericValues>& permDefValues, const std::vector<GenericValues>& permStateValues);
44 
45 private:
46     AccessTokenDb();
47     DISALLOW_COPY_AND_MOVE(AccessTokenDb);
48 
49     int32_t RestoreAndInsertIfCorrupt(const int32_t resultCode, int64_t& outInsertNum,
50         const std::string& tableName, const std::vector<NativeRdb::ValuesBucket>& buckets);
51     int32_t RestoreAndDeleteIfCorrupt(const int32_t resultCode, int32_t& deletedRows,
52         const NativeRdb::RdbPredicates& predicates);
53     int32_t RestoreAndUpdateIfCorrupt(const int32_t resultCode, int32_t& changedRows,
54         const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates);
55     int32_t RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& predicates,
56         const std::vector<std::string>& columns, std::shared_ptr<NativeRdb::AbsSharedResultSet>& queryResultSet);
57     int32_t DeleteAndAddSingleTable(const GenericValues delCondition, const std::string& tableName,
58         const std::vector<GenericValues>& addValues);
59     int32_t DeleteAndAddRecord(AccessTokenID tokenId, const std::vector<GenericValues>& hapInfoValues,
60         const std::vector<GenericValues>& permDefValues, const std::vector<GenericValues>& permStateValues);
61 
62     OHOS::Utils::RWLock rwLock_;
63     std::shared_ptr<NativeRdb::RdbStore> db_ = nullptr;
64 };
65 } // namespace AccessToken
66 } // namespace Security
67 } // namespace OHOS
68 
69 #endif // ACCESS_TOKEN_DB_H
70