• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 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 "rdb_predicates.h"
26 #include "rdb_store.h"
27 #include "rwlock.h"
28 #include "singleton.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 class AccessTokenDb final : public DelayedSingleton<AccessTokenDb> {
34 public:
35     AccessTokenDb();
36     ~AccessTokenDb();
37 
38     int32_t Modify(const AtmDataType type, const GenericValues& modifyValue, const GenericValues& conditionValue);
39     int32_t Find(AtmDataType type, const GenericValues& conditionValue, std::vector<GenericValues>& results);
40     std::shared_ptr<NativeRdb::RdbStore> GetRdb();
41     int32_t DeleteAndInsertValues(const std::vector<DelInfo>& delInfoVec, const std::vector<AddInfo>& addInfoVec);
42 
43 private:
44     DISALLOW_COPY_AND_MOVE(AccessTokenDb);
45 
46     int32_t RestoreAndInsertIfCorrupt(const int32_t resultCode, int64_t& outInsertNum,
47         const std::string& tableName, const std::vector<NativeRdb::ValuesBucket>& buckets,
48         const std::shared_ptr<NativeRdb::RdbStore>& db);
49     int32_t RestoreAndDeleteIfCorrupt(const int32_t resultCode, int32_t& deletedRows,
50         const NativeRdb::RdbPredicates& predicates, const std::shared_ptr<NativeRdb::RdbStore>& db);
51     int32_t RestoreAndUpdateIfCorrupt(const int32_t resultCode, int32_t& changedRows,
52         const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates,
53         const std::shared_ptr<NativeRdb::RdbStore>& db);
54     int32_t RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& predicates,
55         const std::vector<std::string>& columns, std::shared_ptr<NativeRdb::AbsSharedResultSet>& queryResultSet,
56         const std::shared_ptr<NativeRdb::RdbStore>& db);
57     void InitRdb();
58 
59     int32_t AddValues(const AtmDataType type, const std::vector<GenericValues>& addValues);
60     int32_t RemoveValues(const AtmDataType type, const GenericValues& conditionValue);
61 
62     int32_t RestoreAndCommitIfCorrupt(const int32_t resultCode, const std::shared_ptr<NativeRdb::RdbStore>& db);
63 
64     OHOS::Utils::RWLock rwLock_;
65     std::shared_ptr<NativeRdb::RdbStore> db_ = nullptr;
66     std::mutex dbLock_;
67 };
68 } // namespace AccessToken
69 } // namespace Security
70 } // namespace OHOS
71 
72 #endif // ACCESS_TOKEN_DB_H
73