1 /*
2  * Copyright (c) 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 OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H
17 #define OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H
18 
19 #include "rdb_data_manager.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 enum RdbResult : int32_t {
24     Rdb_OK = 0,
25     /* Representative database initialization failed */
26     Rdb_Init_Err,
27     /* Failed to parse initialization file */
28     Rdb_Parse_File_Err,
29     /* Parameter check failed */
30     Rdb_Parameter_Err,
31     /* Failed to query permission settings for resident processes */
32     Rdb_Permissions_Err,
33     /* Database query failed, key may not exist */
34     Rdb_Search_Record_Err
35 };
36 
37 class ScopeGuard final {
38 public:
39     using Function = std::function<void()>;
ScopeGuard(Function fn)40     explicit ScopeGuard(Function fn) : fn_(fn), dismissed(false) {}
41 
~ScopeGuard()42     ~ScopeGuard()
43     {
44         if (!dismissed) {
45             fn_();
46         }
47     }
48 
Dismiss()49     void Dismiss()
50     {
51         dismissed = true;
52     }
53 
54 private:
55     Function fn_;
56     bool dismissed;
57 };
58 
59 class AmsResidentProcessRdbCallBack : public NativeRdb::RdbOpenCallback {
60 public:
61     AmsResidentProcessRdbCallBack(const AmsRdbConfig &rdbConfig);
62     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
63     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
64     int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
65     int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override;
66     int32_t onCorruption(std::string databaseFile) override;
67 
68 private:
69     AmsRdbConfig rdbConfig_;
70 };
71 
72 class AmsResidentProcessRdb final {
73 public:
AmsResidentProcessRdb()74     AmsResidentProcessRdb() {}
~AmsResidentProcessRdb()75     ~AmsResidentProcessRdb() {}
76     static AmsResidentProcessRdb &GetInstance();
77     int32_t Init();
78     int32_t VerifyConfigurationPermissions(const std::string &bundleName, const std::string &callerName);
79     int32_t GetResidentProcessEnable(const std::string &bundleName, bool &enable);
80     int32_t UpdateResidentProcessEnable(const std::string &bundleName, bool enable);
81     int32_t RemoveData(std::string &bundleName);
82 
83 private:
84     std::unique_ptr<RdbDataManager> rdbMgr_ = nullptr;
85 };
86 } // namespace AbilityRuntime
87 } // namespace OHOS
88 #endif // OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H