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 #include "app_provision_info_rdb.h"
17 #include "app_log_wrapper.h"
18 #include "appexecfwk_errors.h"
19 #include "bundle_util.h"
20 #include "scope_guard.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const std::string APP_PROVISION_INFO_RDB_TABLE_NAME = "app_provision_info";
26 // app provision info table key
27 const std::string BUNDLE_NAME = "BUNDLE_NAME";
28 const std::string VERSION_CODE = "VERSION_CODE";
29 const std::string VERSION_NAME = "VERSION_NAME";
30 const std::string UUID = "UUID";
31 const std::string TYPE = "TYPE";
32 const std::string APP_DISTRIBUTION_TYPE = "APP_DISTRIBUTION_TYPE";
33 const std::string DEVELOPER_ID = "DEVELOPER_ID";
34 const std::string CERTIFICATE = "CERTIFICATE";
35 const std::string APL = "APL";
36 const std::string ISSUER = "ISSUER";
37 const std::string VALIDITY_NOT_BEFORE = "VALIDITY_NOT_BEFORE";
38 const std::string VALIDITY_NOT_AFTER = "VALIDITY_NOT_AFTER";
39 const std::string SPECIFIED_DISTRIBUTED_TYPE = "SPECIFIED_DISTRIBUTED_TYPE";
40 const std::string ADDITIONAL_INFO = "ADDITIONAL_INFO";
41 const std::string DEFAULT_VALUE = "";
42 const std::string APP_IDENTIFIER = "APP_IDENTIFIER";
43 const int32_t INDEX_BUNDLE_NAME = 0;
44 const int32_t INDEX_VERSION_CODE = 1;
45 const int32_t INDEX_VERSION_NAME = 2;
46 const int32_t INDEX_UUID = 3;
47 const int32_t INDEX_TYPE = 4;
48 const int32_t INDEX_APP_DISTRIBUTION_TYPE = 5;
49 const int32_t INDEX_DEVELOPER_ID = 6;
50 const int32_t INDEX_CERTIFICATE = 7;
51 const int32_t INDEX_APL = 8;
52 const int32_t INDEX_ISSUER = 9;
53 const int32_t INDEX_VALIDITY_NOT_BEFORE = 10;
54 const int32_t INDEX_VALIDITY_NOT_AFTER = 11;
55 const int32_t INDEX_SPECIFIED_DISTRIBUTED_TYPE = 12;
56 const int32_t INDEX_ADDITIONAL_INFO = 13;
57 const int32_t INDEX_APP_IDENTIFIER = 14;
58 }
59
AppProvisionInfoManagerRdb()60 AppProvisionInfoManagerRdb::AppProvisionInfoManagerRdb()
61 {
62 APP_LOGD("create AppProvisionInfoManagerRdb.");
63 BmsRdbConfig bmsRdbConfig;
64 bmsRdbConfig.dbName = Constants::BUNDLE_RDB_NAME;
65 bmsRdbConfig.tableName = APP_PROVISION_INFO_RDB_TABLE_NAME;
66 bmsRdbConfig.createTableSql = std::string(
67 "CREATE TABLE IF NOT EXISTS "
68 + APP_PROVISION_INFO_RDB_TABLE_NAME
69 + "(BUNDLE_NAME TEXT PRIMARY KEY NOT NULL, "
70 + "VERSION_CODE INTEGER, VERSION_NAME TEXT, UUID TEXT, "
71 + "TYPE TEXT, APP_DISTRIBUTION_TYPE TEXT, DEVELOPER_ID TEXT, CERTIFICATE TEXT, "
72 + "APL TEXT, ISSUER TEXT, VALIDITY_NOT_BEFORE INTEGER, VALIDITY_NOT_AFTER INTEGER);");
73 // SPECIFIED_DISTRIBUTED_TYPE and ADDITIONAL_INFO insert to old database
74 bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " + APP_PROVISION_INFO_RDB_TABLE_NAME +
75 " ADD SPECIFIED_DISTRIBUTED_TYPE TEXT;"));
76 bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " + APP_PROVISION_INFO_RDB_TABLE_NAME +
77 " ADD ADDITIONAL_INFO TEXT;"));
78 bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " + APP_PROVISION_INFO_RDB_TABLE_NAME +
79 " ADD APP_IDENTIFIER TEXT;"));
80 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
81 rdbDataManager_->CreateTable();
82 }
83
~AppProvisionInfoManagerRdb()84 AppProvisionInfoManagerRdb::~AppProvisionInfoManagerRdb()
85 {
86 APP_LOGD("destroy AppProvisionInfoManagerRdb.");
87 }
88
AddAppProvisionInfo(const std::string & bundleName,const AppProvisionInfo & appProvisionInfo)89 bool AppProvisionInfoManagerRdb::AddAppProvisionInfo(const std::string &bundleName,
90 const AppProvisionInfo &appProvisionInfo)
91 {
92 if (bundleName.empty()) {
93 APP_LOGE("AddAppProvisionInfo failed, bundleName is empty");
94 return false;
95 }
96 NativeRdb::ValuesBucket valuesBucket;
97 valuesBucket.PutString(BUNDLE_NAME, bundleName);
98 valuesBucket.PutLong(VERSION_CODE, static_cast<int64_t>(appProvisionInfo.versionCode));
99 valuesBucket.PutString(VERSION_NAME, appProvisionInfo.versionName);
100 valuesBucket.PutString(UUID, appProvisionInfo.uuid);
101 valuesBucket.PutString(TYPE, appProvisionInfo.type);
102 valuesBucket.PutString(APP_DISTRIBUTION_TYPE, appProvisionInfo.appDistributionType);
103 valuesBucket.PutString(DEVELOPER_ID, appProvisionInfo.developerId);
104 valuesBucket.PutString(CERTIFICATE, appProvisionInfo.certificate);
105 valuesBucket.PutString(APL, appProvisionInfo.apl);
106 valuesBucket.PutString(ISSUER, appProvisionInfo.issuer);
107 valuesBucket.PutLong(VALIDITY_NOT_BEFORE, appProvisionInfo.validity.notBefore);
108 valuesBucket.PutLong(VALIDITY_NOT_AFTER, appProvisionInfo.validity.notAfter);
109 valuesBucket.PutString(SPECIFIED_DISTRIBUTED_TYPE, DEFAULT_VALUE);
110 valuesBucket.PutString(ADDITIONAL_INFO, DEFAULT_VALUE);
111 valuesBucket.PutString(APP_IDENTIFIER, appProvisionInfo.appIdentifier);
112
113 return rdbDataManager_->InsertData(valuesBucket);
114 }
115
DeleteAppProvisionInfo(const std::string & bundleName)116 bool AppProvisionInfoManagerRdb::DeleteAppProvisionInfo(const std::string &bundleName)
117 {
118 if (bundleName.empty()) {
119 return false;
120 }
121 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
122 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
123 return rdbDataManager_->DeleteData(absRdbPredicates);
124 }
125
GetAppProvisionInfo(const std::string & bundleName,AppProvisionInfo & appProvisionInfo)126 bool AppProvisionInfoManagerRdb::GetAppProvisionInfo(const std::string &bundleName,
127 AppProvisionInfo &appProvisionInfo)
128 {
129 if (bundleName.empty()) {
130 return false;
131 }
132 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
133 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
134 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
135 if (absSharedResultSet == nullptr) {
136 APP_LOGE("AppProvisionInfoManagerRdb GetAppProvisionInfo failed.");
137 return false;
138 }
139 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
140 return ConvertToAppProvision(absSharedResultSet, appProvisionInfo);
141 }
142
GetAllAppProvisionInfoBundleName(std::unordered_set<std::string> & bundleNames)143 bool AppProvisionInfoManagerRdb::GetAllAppProvisionInfoBundleName(std::unordered_set<std::string> &bundleNames)
144 {
145 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
146 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
147 if (absSharedResultSet == nullptr) {
148 APP_LOGE("GetAppProvisionInfo failed.");
149 return false;
150 }
151 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
152
153 auto ret = absSharedResultSet->GoToFirstRow();
154 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GoToFirstRow failed, ret: %{public}d");
155 do {
156 std::string name;
157 ret = absSharedResultSet->GetString(INDEX_BUNDLE_NAME, name);
158 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString bundleName failed, ret: %{public}d");
159 bundleNames.insert(name);
160 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
161 return true;
162 }
163
ConvertToAppProvision(const std::shared_ptr<NativeRdb::AbsSharedResultSet> & absSharedResultSet,AppProvisionInfo & appProvisionInfo)164 bool AppProvisionInfoManagerRdb::ConvertToAppProvision(
165 const std::shared_ptr<NativeRdb::AbsSharedResultSet> &absSharedResultSet,
166 AppProvisionInfo &appProvisionInfo)
167 {
168 if (absSharedResultSet == nullptr) {
169 APP_LOGE("absSharedResultSet is nullptr");
170 return false;
171 }
172 auto ret = absSharedResultSet->GoToFirstRow();
173 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GoToFirstRow failed, ret: %{public}d");
174 int64_t versionCode;
175 ret = absSharedResultSet->GetLong(INDEX_VERSION_CODE, versionCode);
176 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString versionCode failed, ret: %{public}d");
177 appProvisionInfo.versionCode = static_cast<uint32_t>(versionCode);
178 ret = absSharedResultSet->GetString(INDEX_VERSION_NAME, appProvisionInfo.versionName);
179 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString versionName failed, ret: %{public}d");
180 ret = absSharedResultSet->GetString(INDEX_UUID, appProvisionInfo.uuid);
181 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString uuid failed, ret: %{public}d");
182 ret = absSharedResultSet->GetString(INDEX_TYPE, appProvisionInfo.type);
183 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString type failed, ret: %{public}d");
184 ret = absSharedResultSet->GetString(INDEX_APP_DISTRIBUTION_TYPE, appProvisionInfo.appDistributionType);
185 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString appDistributionType failed, ret: %{public}d");
186 ret = absSharedResultSet->GetString(INDEX_DEVELOPER_ID, appProvisionInfo.developerId);
187 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString developerId failed, ret: %{public}d");
188 ret = absSharedResultSet->GetString(INDEX_CERTIFICATE, appProvisionInfo.certificate);
189 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString certificate failed, ret: %{public}d");
190 ret = absSharedResultSet->GetString(INDEX_APL, appProvisionInfo.apl);
191 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString apl failed, ret: %{public}d");
192 ret = absSharedResultSet->GetString(INDEX_ISSUER, appProvisionInfo.issuer);
193 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString issuer failed, ret: %{public}d");
194 ret = absSharedResultSet->GetLong(INDEX_VALIDITY_NOT_BEFORE, appProvisionInfo.validity.notBefore);
195 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString notBefore failed, ret: %{public}d");
196 ret = absSharedResultSet->GetLong(INDEX_VALIDITY_NOT_AFTER, appProvisionInfo.validity.notAfter);
197 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString notAfter failed, ret: %{public}d");
198 ret = absSharedResultSet->GetString(INDEX_APP_IDENTIFIER, appProvisionInfo.appIdentifier);
199 CHECK_RDB_RESULT_RETURN_IF_FAIL(ret, "GetString appIdentifier failed, ret: %{public}d");
200 return true;
201 }
202
SetSpecifiedDistributionType(const std::string & bundleName,const std::string & specifiedDistributionType)203 bool AppProvisionInfoManagerRdb::SetSpecifiedDistributionType(
204 const std::string &bundleName, const std::string &specifiedDistributionType)
205 {
206 if (bundleName.empty()) {
207 return false;
208 }
209 NativeRdb::ValuesBucket valuesBucket;
210 valuesBucket.PutString(SPECIFIED_DISTRIBUTED_TYPE, specifiedDistributionType);
211 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
212 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
213 if (!rdbDataManager_->UpdateData(valuesBucket, absRdbPredicates)) {
214 APP_LOGE("bundleName: %{public}s SetSpecifiedDistributionType failed.", bundleName.c_str());
215 return false;
216 }
217 return true;
218 }
219
GetSpecifiedDistributionType(const std::string & bundleName,std::string & specifiedDistributionType)220 bool AppProvisionInfoManagerRdb::GetSpecifiedDistributionType(
221 const std::string &bundleName, std::string &specifiedDistributionType)
222 {
223 if (bundleName.empty()) {
224 return false;
225 }
226 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
227 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
228 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
229 if (absSharedResultSet == nullptr) {
230 APP_LOGW("bundleName: %{public}s, GetSpecifiedDistributionType QueryData failed.", bundleName.c_str());
231 return false;
232 }
233 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
234 auto ret = absSharedResultSet->GoToFirstRow();
235 if (ret != NativeRdb::E_OK) {
236 APP_LOGW("bundleName: %{public}s, AppProvisionInfoManagerRdb GetSpecifiedDistributionType failed.",
237 bundleName.c_str());
238 return false;
239 }
240 ret = absSharedResultSet->GetString(INDEX_SPECIFIED_DISTRIBUTED_TYPE, specifiedDistributionType);
241 if (ret != NativeRdb::E_OK) {
242 APP_LOGE("bundleName: %{public}s GetSpecifiedDistributionType GetString failed.", bundleName.c_str());
243 return false;
244 }
245 return true;
246 }
247
SetAdditionalInfo(const std::string & bundleName,const std::string & additionalInfo)248 bool AppProvisionInfoManagerRdb::SetAdditionalInfo(
249 const std::string &bundleName, const std::string &additionalInfo)
250 {
251 if (bundleName.empty()) {
252 return false;
253 }
254 NativeRdb::ValuesBucket valuesBucket;
255 valuesBucket.PutString(ADDITIONAL_INFO, additionalInfo);
256 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
257 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
258 if (!rdbDataManager_->UpdateData(valuesBucket, absRdbPredicates)) {
259 APP_LOGE("bundleName: %{public}s SetAdditionalInfo failed.", bundleName.c_str());
260 return false;
261 }
262 return true;
263 }
264
GetAdditionalInfo(const std::string & bundleName,std::string & additionalInfo)265 bool AppProvisionInfoManagerRdb::GetAdditionalInfo(
266 const std::string &bundleName, std::string &additionalInfo)
267 {
268 if (bundleName.empty()) {
269 return false;
270 }
271 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_PROVISION_INFO_RDB_TABLE_NAME);
272 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
273 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
274 if (absSharedResultSet == nullptr) {
275 APP_LOGW("bundleName: %{public}s, GetAdditionalInfo QueryData failed.", bundleName.c_str());
276 return false;
277 }
278 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
279 auto ret = absSharedResultSet->GoToFirstRow();
280 if (ret != NativeRdb::E_OK) {
281 APP_LOGW("bundleName: %{public}s, AppProvisionInfoManagerRdb GetAdditionalInfo failed.", bundleName.c_str());
282 return false;
283 }
284 ret = absSharedResultSet->GetString(INDEX_ADDITIONAL_INFO, additionalInfo);
285 if (ret != NativeRdb::E_OK) {
286 APP_LOGE("bundleName: %{public}s, AppProvisionInfoManagerRdb GetAdditionalInfo failed.", bundleName.c_str());
287 return false;
288 }
289 return true;
290 }
291 } // namespace AppExecFwk
292 } // namespace OHOS
293