• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 C_REQUEST_DATABASE_H
17 #define C_REQUEST_DATABASE_H
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "c_progress.h"
23 #include "c_task_config.h"
24 #include "c_task_info.h"
25 #include "cxx.h"
26 #include "rdb_errno.h"
27 #include "rdb_helper.h"
28 #include "rdb_open_callback.h"
29 #include "rdb_predicates.h"
30 #include "rdb_store.h"
31 #include "result_set.h"
32 #include "value_object.h"
33 
34 namespace OHOS::Request {
35 constexpr const char *DB_NAME = "/data/service/el1/public/database/request/request.db";
36 constexpr int DATABASE_VERSION = 1;
37 constexpr const char *REQUEST_DATABASE_VERSION_4_1_RELEASE = "API11_4.1-release";
38 constexpr const char *REQUEST_DATABASE_VERSION = "API12_5.0-release";
39 constexpr const char *REQUEST_TASK_TABLE_NAME = "request_task";
40 constexpr int QUERY_ERR = -1;
41 constexpr int QUERY_OK = 0;
42 constexpr int WITHOUT_VERSION_TABLE = 40;
43 constexpr int API11_4_1_RELEASE = 41;
44 constexpr int API12_5_0_RELEASE = 50;
45 constexpr int INVALID_VERSION = -50;
46 constexpr int CHECK_VERSION_FAILED = -1;
47 
48 constexpr const char *CHECK_REQUEST_VERSION = "SELECT name FROM sqlite_master WHERE type='table' AND "
49                                               "name='request_version'";
50 
51 constexpr const char *CREATE_REQUEST_VERSION_TABLE = "CREATE TABLE IF NOT EXISTS request_version "
52                                                      "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
53                                                      "version TEXT, "
54                                                      "task_table TEXT)";
55 
56 constexpr const char *CREATE_REQUEST_TASK_TABLE = "CREATE TABLE IF NOT EXISTS request_task "
57                                                   "(task_id INTEGER PRIMARY KEY, "
58                                                   "uid INTEGER, "
59                                                   "token_id INTEGER, "
60                                                   "action INTEGER, "
61                                                   "mode INTEGER, "
62                                                   "cover INTEGER, "
63                                                   "network INTEGER, "
64                                                   "metered INTEGER, "
65                                                   "roaming INTEGER, "
66                                                   "ctime INTEGER, "
67                                                   "mtime INTEGER, "
68                                                   "reason INTEGER, "
69                                                   "gauge INTEGER, "
70                                                   "retry INTEGER, "
71                                                   "redirect INTEGER, "
72                                                   "tries INTEGER, "
73                                                   "version INTEGER, "
74                                                   "config_idx INTEGER, "
75                                                   "begins INTEGER, "
76                                                   "ends INTEGER, "
77                                                   "precise INTEGER, "
78                                                   "priority INTEGER, "
79                                                   "background INTEGER, "
80                                                   "bundle TEXT, "
81                                                   "url TEXT, "
82                                                   "data TEXT, "
83                                                   "token TEXT, "
84                                                   "title TEXT, "
85                                                   "description TEXT, "
86                                                   "method TEXT, "
87                                                   "headers TEXT, "
88                                                   "config_extras TEXT, "
89                                                   "mime_type TEXT, "
90                                                   "state INTEGER, "
91                                                   "idx INTEGER, "
92                                                   "total_processed INTEGER, "
93                                                   "sizes TEXT, "
94                                                   "processed TEXT, "
95                                                   "extras TEXT, "
96                                                   "form_items BLOB, "
97                                                   "file_specs BLOB, "
98                                                   "each_file_status BLOB, "
99                                                   "body_file_names BLOB, "
100                                                   "certs_paths BLOB)";
101 
102 constexpr const char *REQUEST_TASK_TABLE_ADD_PROXY = "ALTER TABLE request_task ADD COLUMN proxy TEXT";
103 
104 constexpr const char *REQUEST_TASK_TABLE_ADD_CERTIFICATE_PINS = "ALTER TABLE request_task ADD COLUMN "
105                                                                 "certificate_pins TEXT";
106 constexpr const char *REQUEST_TASK_TABLE_ADD_BUNDLE_TYPE = "ALTER TABLE request_task ADD COLUMN bundle_type TEXT";
107 constexpr const char *REQUEST_TASK_TABLE_ADD_ATOMIC_ACCOUNT = "ALTER TABLE request_task ADD COLUMN atomic_account "
108                                                               "TEXT";
109 
110 constexpr const char *REQUEST_TASK_TABLE_ADD_UID_INDEX = "CREATE INDEX uid_index on request_task(uid)";
111 struct TaskFilter;
112 struct NetworkInfo;
113 struct TaskQosInfo;
114 class RequestDataBase {
115 public:
116     static RequestDataBase &GetInstance(std::string path, bool encryptStatus);
117     RequestDataBase(const RequestDataBase &) = delete;
118     RequestDataBase &operator=(const RequestDataBase &) = delete;
119     bool Insert(const std::string &table, const OHOS::NativeRdb::ValuesBucket &insertValues);
120     bool Update(const OHOS::NativeRdb::ValuesBucket values, const OHOS::NativeRdb::AbsRdbPredicates &predicates);
121     std::shared_ptr<OHOS::NativeRdb::ResultSet> Query(
122         const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns);
123     bool Delete(const OHOS::NativeRdb::AbsRdbPredicates &predicates);
124     int ExecuteSql(rust::str sql);
125     int QueryInteger(rust::str sql, rust::vec<rust::i64> &res);
126     int QueryText(rust::str sql, rust::vec<rust::string> &res);
127     int GetAppTaskQosInfos(rust::str sql, rust::vec<TaskQosInfo> &res);
128     int GetTaskQosInfo(rust::str sql, TaskQosInfo &res);
129     void CheckAndRebuildDataBase(int errCode);
130 
131 private:
132     RequestDataBase(std::string path, bool encryptStatus);
133 
134 private:
135     std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
136 };
137 
GetDatabaseInstance(rust::str path,bool encryptStatus)138 inline RequestDataBase *GetDatabaseInstance(rust::str path, bool encryptStatus)
139 {
140     return &RequestDataBase::GetInstance(std::string(path), encryptStatus);
141 }
142 
143 class RequestDBOpenCallback : public OHOS::NativeRdb::RdbOpenCallback {
144 public:
145     int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
146     int OnOpen(OHOS::NativeRdb::RdbStore &rdbStore) override;
147     int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
148     int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
149 };
150 } // namespace OHOS::Request
151 
152 #ifdef __cplusplus
153 extern "C" {
154 #endif
155 
156 struct CVectorWrapper {
157     uint32_t *ptr;
158     uint64_t len;
159 };
160 
161 // Request Database Modify.
162 bool RecordRequestTask(CTaskInfo *taskInfo, CTaskConfig *taskConfig);
163 bool UpdateRequestTask(uint32_t taskId, CUpdateInfo *updateInfo);
164 bool UpdateRequestTaskState(uint32_t taskId, CUpdateStateInfo *updateStateInfo);
165 void RequestDBRemoveRecordsFromTime(uint64_t time);
166 CTaskInfo *GetTaskInfo(uint32_t taskId);
167 CTaskConfig *QueryTaskConfig(uint32_t taskId);
168 
169 #ifdef __cplusplus
170 }
171 #endif
172 #endif // C_REQUEST_DATABASE_H