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 USB_RIGHT_DB_HELPER_H
17 #define USB_RIGHT_DB_HELPER_H
18 
19 #include "data_ability_predicates.h"
20 #include "rdb_errno.h"
21 #include "rdb_helper.h"
22 #include "rdb_open_callback.h"
23 #include "rdb_predicates.h"
24 #include "rdb_store.h"
25 #include "result_set.h"
26 #include "usb_right_database.h"
27 #include "value_object.h"
28 
29 namespace OHOS {
30 namespace USB {
31 
32 /* unit: second */
33 constexpr int64_t USB_RIGHT_VALID_PERIOD_MIN = 0;
34 constexpr int64_t USB_RIGHT_VALID_PERIOD_MAX = 0xFFFFFFFFL;
35 constexpr uint64_t USB_RIGHT_VALID_PERIOD_SET = 0x0FFFFFFFL;
36 
37 struct UsbRightAppInfo {
38     uint32_t primaryKeyId; /* table primary key */
39     int32_t uid;           /* app install user id */
40     uint64_t installTime;  /* app install time */
41     uint64_t updateTime;   /* app update time */
42     uint64_t requestTime;  /* app request permission time */
43     uint64_t validPeriod;  /* app permission valid period */
44 };
45 
46 struct UsbRightTableInfo {
47     int32_t rowCount;
48     int32_t columnCount;
49     int32_t primaryKeyIndex;
50     int32_t uidIndex;
51     int32_t installTimeIndex;
52     int32_t updateTimeIndex;
53     int32_t requestTimeIndex;
54     int32_t validPeriodIndex;
55     int32_t deviceNameIndex;
56     int32_t bundleNameIndex;
57 };
58 
59 class UsbRightDbHelper {
60 public:
61     static std::shared_ptr<UsbRightDbHelper> GetInstance();
62 
63     /* query if permission record is expired by query database */
64     bool IsRecordExpired(int32_t uid, const std::string &deviceName, const std::string &bundleName,
65         const std::string &tokenId, uint64_t expiredTime);
66     /* query if permission record is expired by time info */
67     bool IsRecordExpired(const struct UsbRightAppInfo &info, uint64_t expiredTime);
68     /* add (user, device, app) record */
69     int32_t AddRightRecord(const std::string &deviceName, const std::string &bundleName, struct UsbRightAppInfo &info);
70     /* query (user) record */
71     int32_t QueryUserRightRecord(int32_t uid, std::vector<struct UsbRightAppInfo> &infos);
72     /* query (user, device) record */
73     int32_t QueryDeviceRightRecord(
74         int32_t uid, const std::string &deviceName, std::vector<struct UsbRightAppInfo> &infos);
75     /* query (user, app) record */
76     int32_t QueryAppRightRecord(int32_t uid, const std::string &bundleName, std::vector<struct UsbRightAppInfo> &infos);
77     /* query (user, device, app, tokenId) record */
78     int32_t QueryRightRecord(int32_t uid, const std::string &deviceName, const std::string &bundleName,
79         const std::string &tokenId, std::vector<struct UsbRightAppInfo> &infos);
80     /* query users */
81     int32_t QueryRightRecordUids(std::vector<std::string> &uids);
82     /* query apps */
83     int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string> &apps);
84 
85     /* update (user, device, app) record */
86     int32_t UpdateRightRecord(
87         int32_t uid, const std::string &deviceName, const std::string &bundleName, struct UsbRightAppInfo &info);
88 
89     /* add or update (user, device, app, tokenId) record */
90     int32_t AddOrUpdateRightRecord(int32_t uid, const std::string &deviceName, const std::string &bundleName,
91         const std::string &tokenId, struct UsbRightAppInfo &info);
92 
93     /* delete (user, device, app, tokenId) record */
94     int32_t DeleteRightRecord(int32_t uid, const std::string &deviceName,
95         const std::string &bundleName, const std::string &tokenId);
96     /* delete (user, device) record */
97     int32_t DeleteDeviceRightRecord(int32_t uid, const std::string &deviceName);
98     /* delete (user, app) record */
99     int32_t DeleteAppRightRecord(int32_t uid, const std::string &bundleName);
100     /* delete (user, apps) record */
101     int32_t DeleteAppsRightRecord(int32_t uid, const std::vector<std::string> &bundleNames);
102     /* delete (user) record */
103     int32_t DeleteUidRightRecord(int32_t uid);
104     /* delete (user, time) expired record */
105     int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime);
106     /* delete (validTime, device) record */
107     int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string &deviceName);
108 
109 private:
110     UsbRightDbHelper();
111     DISALLOW_COPY_AND_MOVE(UsbRightDbHelper);
112 
113     int32_t CheckIfNeedUpdateEx(bool &isUpdate, int32_t uid, const std::string &deviceName,
114         const std::string &bundleName, const std::string &tokenId);
115     int32_t AddOrUpdateRightRecordEx(bool isUpdate, int32_t uid, const std::string &deviceName,
116         const std::string &bundleName, const std::string &tokenId, struct UsbRightAppInfo &info);
117     int32_t QueryRightRecordCount(void);
118     int32_t GetResultSetTableInfo(
119         const std::shared_ptr<OHOS::NativeRdb::ResultSet> &resultSet, struct UsbRightTableInfo &table);
120     int32_t GetResultRightRecordEx(const std::shared_ptr<OHOS::NativeRdb::ResultSet> &resultSet,
121         std::vector<struct UsbRightAppInfo> &infos);
122     int32_t QueryAndGetResult(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
123         const std::vector<std::string> &columns, std::vector<struct UsbRightAppInfo> &infos);
124     int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
125         const std::vector<std::string> &columns, const std::string &columnName, std::vector<std::string> &columnValues);
126     int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector<std::string> &whereArgs);
127     int32_t DeleteAndNoOtherOperation(const OHOS::NativeRdb::RdbPredicates &rdbPredicates);
128 
129     static std::shared_ptr<UsbRightDbHelper> instance_;
130     std::mutex databaseMutex_;
131     std::shared_ptr<UsbRightDataBase> rightDatabase_;
132 };
133 
134 } // namespace USB
135 } // namespace OHOS
136 
137 #endif // USB_RIGHT_DB_HELPER_H
138