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 NET_ACCESS_POLICY_RDB_H
17 #define NET_ACCESS_POLICY_RDB_H
18 
19 #include "rdb_errno.h"
20 #include "rdb_helper.h"
21 #include "rdb_open_callback.h"
22 #include "rdb_store.h"
23 #include "result_set.h"
24 #include "rdb_sql_utils.h"
25 #include "net_access_policy.h"
26 #include "rdb_predicates.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace NetAccessPolicyRdbFiledConst {
31     const std::string FILED_UID = "uid";
32     const std::string FILED_WIFI_POLICY = "wifiPolicy";
33     const std::string FILED_CELLULAR_POLICY = "cellularPolicy";
34     const std::string FILED_SET_FROM_CONFIG_FLAG = "setFromConfigFlag";
35     const std::string FILED_IS_BROKER = "isBroker";
36 
37     constexpr int32_t FILED_COLUMN_INDEX_ZERO = 0;
38     constexpr int32_t FILED_COLUMN_INDEX_ONE = 1;
39     constexpr int32_t FILED_COLUMN_INDEX_TWO = 2;
40     constexpr int32_t FILED_COLUMN_INDEX_THREE = 3;
41     constexpr int32_t FILED_COLUMN_INDEX_FOUR = 4;
42 }
43 
44 typedef struct NetAccessPolicyData {
45     int32_t uid;
46     int32_t wifiPolicy;
47     int32_t cellularPolicy;
48     int32_t setFromConfigFlag;
49     int32_t isBroker;
50 }NetAccessPolicyData;
51 
52 class NetAccessPolicyRDB {
53 public:
54     static NetAccessPolicyRDB& GetInstance();
55     NetAccessPolicyRDB() = default;
56     ~NetAccessPolicyRDB() = default;
57     int32_t InitRdbStore();
58     int32_t InsertData(NetAccessPolicyData policy);
59     int32_t DeleteByUid(const int32_t uid);
60     int32_t UpdateByUid(int32_t uid, NetAccessPolicyData policy);
IsRdbNull()61     bool IsRdbNull()
62     {
63         return rdbStore_ == nullptr;
64     }
65     std::vector<NetAccessPolicyData> QueryAll();
66     int32_t QueryByUid(int uid, NetAccessPolicyData &uidPolicy);
67 
68     class RdbDataOpenCallback : public NativeRdb::RdbOpenCallback {
69     public:
70         int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
71         int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
72     private:
73         void UpgradeDbVersionTo(NativeRdb::RdbStore &store, int newVersion);
74         void AddIsBroker(NativeRdb::RdbStore &store, int newVersion);
75     };
76     friend class RdbDataOpenCallback;
77 
78 private:
79     std::shared_ptr<NativeRdb::RdbStore> rdbStore_ { nullptr };
80     NetAccessPolicyData policy_;
81 };
82 } // namespace NetManagerStandard
83 } // namespace OHOS
84 #endif // NET_ACCESS_POLICY_RDB_H
85