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 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( 42 const std::vector<AtmDataType>& delDataTypes, const std::vector<GenericValues>& delValues, 43 const std::vector<AtmDataType>& addDataTypes, const std::vector<std::vector<GenericValues>>& addValues); 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 const std::shared_ptr<NativeRdb::RdbStore>& db); 52 int32_t RestoreAndDeleteIfCorrupt(const int32_t resultCode, int32_t& deletedRows, 53 const NativeRdb::RdbPredicates& predicates, const std::shared_ptr<NativeRdb::RdbStore>& db); 54 int32_t RestoreAndUpdateIfCorrupt(const int32_t resultCode, int32_t& changedRows, 55 const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates, 56 const std::shared_ptr<NativeRdb::RdbStore>& db); 57 int32_t RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& predicates, 58 const std::vector<std::string>& columns, std::shared_ptr<NativeRdb::AbsSharedResultSet>& queryResultSet, 59 const std::shared_ptr<NativeRdb::RdbStore>& db); 60 void InitRdb(); 61 62 int32_t AddValues(const AtmDataType type, const std::vector<GenericValues>& addValues); 63 int32_t RemoveValues(const AtmDataType type, const GenericValues& conditionValue); 64 65 int32_t RestoreAndCommitIfCorrupt(const int32_t resultCode, const std::shared_ptr<NativeRdb::RdbStore>& db); 66 67 OHOS::Utils::RWLock rwLock_; 68 std::shared_ptr<NativeRdb::RdbStore> db_ = nullptr; 69 std::mutex dbLock_; 70 }; 71 } // namespace AccessToken 72 } // namespace Security 73 } // namespace OHOS 74 75 #endif // ACCESS_TOKEN_DB_H 76