1 /*
2  * Copyright (c) 2023 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 APP_DOMAIN_VERIFY_SERVICE_INCLUDE_RDB_DATA_MANAGER_H
17 #define APP_DOMAIN_VERIFY_SERVICE_INCLUDE_RDB_DATA_MANAGER_H
18 
19 #include <mutex>
20 
21 #include "app_domain_verify_rdb_config.h"
22 #include "app_domain_verify_rdb_open_callback.h"
23 #include "rdb_helper.h"
24 #include "values_bucket.h"
25 #include "ffrt.h"
26 
27 namespace OHOS {
28 namespace AppDomainVerify {
29 struct RdbDataItem {
30     std::string bundleName;
31     std::string appIdentifier;
32     std::string domain;
33     int status = 0;
34     std::string verifyTs;
35     int count = 0;
36 };
37 
38 class AppDomainVerifyRdbDataManager : public std::enable_shared_from_this<AppDomainVerifyRdbDataManager> {
39 public:
40     AppDomainVerifyRdbDataManager(const AppDomainVerifyRdbConfig& rdbConfig);
41     virtual ~AppDomainVerifyRdbDataManager();
42     bool InsertData(const RdbDataItem& rdbDataItem);
43     bool DeleteData(const std::string& bundleName);
44     bool QueryAllData(std::unordered_map<std::string, std::vector<RdbDataItem>>& dataMap);
45     bool QueryBundleNameByDomain(const std::string& domain, std::vector<RdbDataItem>& items);
46     bool QueryDomainByBundleName(const std::string& bundleName, std::vector<RdbDataItem>& items);
47     bool CreateTable();
48 
49 private:
50     void DeleteIfCannotAccess();
51     std::shared_ptr<NativeRdb::RdbStore> GetRdbStore();
52     void DelayCloseRdbStore();
53     bool CheckRdbReturnIfOk(int errcode);
54     bool CheckRdbStoreExist(const std::shared_ptr<NativeRdb::RdbStore>& rdbStore);
55     bool Query(const NativeRdb::AbsRdbPredicates& predicates, const std::vector<std::string>& columns,
56         std::vector<RdbDataItem>& items);
57 
58 private:
59     std::mutex rdbMutex_;
60     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
61     AppDomainVerifyRdbConfig appDomainVerifyRdbConfig_;
62     std::shared_ptr<ffrt::queue> continuationHandler_;
63 };
64 }  // namespace AppDomainVerify
65 }  // namespace OHOS
66 #endif  // APP_DOMAIN_VERIFY_SERVICE_INCLUDE_RDB_DATA_MANAGER_H
67