• 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 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 = 300;
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(
65         int32_t uid, const std::string &deviceName, const std::string &bundleName, uint64_t expiredTime);
66     /* query if permission record is expired by time info */
67     bool IsRecordExpired(const struct UsbRightAppInfo &info, uint64_t expiredTime);
68 
69     /* add (user, device, app) record */
70     int32_t AddRightRecord(const std::string &deviceName, const std::string &bundleName, struct UsbRightAppInfo &info);
71 
72     /* query (user) record */
73     int32_t QueryUserRightRecord(int32_t uid, std::vector<struct UsbRightAppInfo> &infos);
74     /* query (user, device) record */
75     int32_t QueryDeviceRightRecord(
76         int32_t uid, const std::string &deviceName, std::vector<struct UsbRightAppInfo> &infos);
77     /* query (user, app) record */
78     int32_t QueryAppRightRecord(int32_t uid, const std::string &bundleName, std::vector<struct UsbRightAppInfo> &infos);
79     /* query (user, device, app) record */
80     int32_t QueryRightRecord(int32_t uid, const std::string &deviceName, const std::string &bundleName,
81         std::vector<struct UsbRightAppInfo> &infos);
82     /* query users */
83     int32_t QueryRightRecordUids(std::vector<std::string> &uids);
84     /* query apps */
85     int32_t QueryRightRecordApps(int32_t uid, std::vector<std::string> &apps);
86 
87     /* update (user, device, app) record */
88     int32_t UpdateRightRecord(
89         int32_t uid, const std::string &deviceName, const std::string &bundleName, struct UsbRightAppInfo &info);
90 
91     /* add or update (user, device, app) record */
92     int32_t AddOrUpdateRightRecord(
93         int32_t uid, const std::string &deviceName, const std::string &bundleName, struct UsbRightAppInfo &info);
94 
95     /* delete (user, device, app) record */
96     int32_t DeleteRightRecord(int32_t uid, const std::string &deviceName, const std::string &bundleName);
97     /* delete (user, device) record */
98     int32_t DeleteDeviceRightRecord(int32_t uid, const std::string &deviceName);
99     /* delete (user, app) record */
100     int32_t DeleteAppRightRecord(int32_t uid, const std::string &bundleName);
101     /* delete (user, apps) record */
102     int32_t DeleteAppsRightRecord(int32_t uid, const std::vector<std::string> &bundleNames);
103     /* delete (user) record */
104     int32_t DeleteUidRightRecord(int32_t uid);
105     /* delete (user, time) expired record */
106     int32_t DeleteNormalExpiredRightRecord(int32_t uid, uint64_t expiredTime);
107     /* delete (validTime, device) record */
108     int32_t DeleteValidPeriodRightRecord(long validPeriod, const std::string &deviceName);
109 
110 private:
111     UsbRightDbHelper();
112     DISALLOW_COPY_AND_MOVE(UsbRightDbHelper);
113 
114     int32_t CheckIfNeedUpdateEx(
115         bool &isUpdate, int32_t uid, const std::string &deviceName, const std::string &bundleName);
116     int32_t AddOrUpdateRightRecordEx(bool isUpdate, int32_t uid, const std::string &deviceName,
117         const std::string &bundleName, struct UsbRightAppInfo &info);
118     int32_t QueryRightRecordCount(void);
119     int32_t GetResultSetTableInfo(
120         const std::shared_ptr<OHOS::NativeRdb::ResultSet> &resultSet, struct UsbRightTableInfo &table);
121     int32_t GetResultRightRecordEx(const std::shared_ptr<OHOS::NativeRdb::ResultSet> &resultSet,
122         std::vector<struct UsbRightAppInfo> &infos);
123     int32_t QueryAndGetResult(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
124         const std::vector<std::string> &columns, std::vector<struct UsbRightAppInfo> &infos);
125     int32_t QueryAndGetResultColumnValues(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
126         const std::vector<std::string> &columns, const std::string &columnName, std::vector<std::string> &columnValues);
127     int32_t DeleteAndNoOtherOperation(const std::string &whereClause, const std::vector<std::string> &whereArgs);
128     int32_t DeleteAndNoOtherOperation(const OHOS::NativeRdb::RdbPredicates &rdbPredicates);
129 
130     static std::shared_ptr<UsbRightDbHelper> instance_;
131     std::mutex databaseMutex_;
132     std::shared_ptr<UsbRightDataBase> rightDatabase_;
133 };
134 
135 } // namespace USB
136 } // namespace OHOS
137 
138 #endif // USB_RIGHT_DB_HELPER_H
139