1 /*
2 * Copyright (c) 2021-2022 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 "data_center/database/form_db_cache.h"
17
18 #include <cinttypes>
19
20 #include "fms_log_wrapper.h"
21 #include "bms_mgr/form_bms_helper.h"
22 #include "data_center/form_data_mgr.h"
23 #include "data_center/database/form_db_info.h"
24 #include "form_mgr_errors.h"
25 #include "form_provider/form_provider_mgr.h"
26 #include "common/util/form_util.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
FormDbCache()30 FormDbCache::FormDbCache()
31 {
32 HILOG_INFO("create");
33 }
34
~FormDbCache()35 FormDbCache::~FormDbCache()
36 {
37 HILOG_INFO("destroy");
38 }
39
40 /**
41 * @brief Load form data from DB to DbCache when starting.
42 * @return Void.
43 */
Start()44 void FormDbCache::Start()
45 {
46 std::vector<InnerFormInfo> innerFormInfos;
47 innerFormInfos.clear();
48 if (FormInfoRdbStorageMgr::GetInstance().LoadFormData(innerFormInfos) != ERR_OK) {
49 HILOG_ERROR("LoadFormData failed");
50 return;
51 }
52 HILOG_INFO("load form data, size: %{public}zu", innerFormInfos.size());
53 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
54 for (unsigned int i = 0; i < innerFormInfos.size(); i++) {
55 FormDBInfo formDBInfo = innerFormInfos.at(i).GetFormDBInfo();
56 formDBInfos_.emplace_back(formDBInfo);
57 }
58 }
59
60 /**
61 * @brief Save or update form data to DbCache and DB.
62 * @param formDBInfo Form data.
63 * @return Returns ERR_OK on success, others on failure.
64 */
SaveFormInfo(const FormDBInfo & formDBInfo)65 ErrCode FormDbCache::SaveFormInfo(const FormDBInfo &formDBInfo)
66 {
67 HILOG_INFO("formId:%{public}" PRId64, formDBInfo.formId);
68 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
69 auto iter = find(formDBInfos_.begin(), formDBInfos_.end(), formDBInfo);
70 if (iter != formDBInfos_.end()) {
71 if (iter->Compare(formDBInfo) == false) {
72 HILOG_WARN("need update, formId[%{public}" PRId64 "]", formDBInfo.formId);
73 *iter = formDBInfo;
74 InnerFormInfo innerFormInfo(formDBInfo);
75 return FormInfoRdbStorageMgr::GetInstance().ModifyStorageFormData(innerFormInfo);
76 } else {
77 HILOG_WARN("already exist, formId[%{public}" PRId64 "].", formDBInfo.formId);
78 return ERR_OK;
79 }
80 } else {
81 formDBInfos_.emplace_back(formDBInfo);
82 InnerFormInfo innerFormInfo(formDBInfo);
83 return FormInfoRdbStorageMgr::GetInstance().SaveStorageFormData(innerFormInfo);
84 }
85 }
86
87 /**
88 * @brief Save or update form data to DbCache and DB.
89 * @param formDBInfo Form data.
90 * @return Returns ERR_OK on success, others on failure.(NoLock)
91 */
SaveFormInfoNolock(const FormDBInfo & formDBInfo)92 ErrCode FormDbCache::SaveFormInfoNolock(const FormDBInfo &formDBInfo)
93 {
94 HILOG_INFO("formId:%{public}" PRId64, formDBInfo.formId);
95 auto iter = find(formDBInfos_.begin(), formDBInfos_.end(), formDBInfo);
96 if (iter != formDBInfos_.end()) {
97 if (iter->Compare(formDBInfo) == false) {
98 HILOG_WARN("need update, formId[%{public}" PRId64 "].", formDBInfo.formId);
99 *iter = formDBInfo;
100 InnerFormInfo innerFormInfo(formDBInfo);
101 return FormInfoRdbStorageMgr::GetInstance().ModifyStorageFormData(innerFormInfo);
102 } else {
103 HILOG_WARN("already exist, formId[%{public}" PRId64 "].", formDBInfo.formId);
104 return ERR_OK;
105 }
106 } else {
107 formDBInfos_.emplace_back(formDBInfo);
108 InnerFormInfo innerFormInfo(formDBInfo);
109 return FormInfoRdbStorageMgr::GetInstance().SaveStorageFormData(innerFormInfo);
110 }
111 }
112
113 /**
114 * @brief Delete form data in DbCache and DB with formId.
115 * @param formId form data Id.
116 * @return Returns ERR_OK on success, others on failure.
117 */
DeleteFormInfo(int64_t formId)118 ErrCode FormDbCache::DeleteFormInfo(int64_t formId)
119 {
120 HILOG_INFO("form: %{public}" PRId64, formId);
121 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
122 FormDBInfo tmpForm;
123 tmpForm.formId = formId;
124 auto iter = find(formDBInfos_.begin(), formDBInfos_.end(), tmpForm);
125 if (iter == formDBInfos_.end()) {
126 HILOG_WARN("not find form:%{public}" PRId64, formId);
127 } else {
128 formDBInfos_.erase(iter);
129 }
130 if (FormInfoRdbStorageMgr::GetInstance().DeleteStorageFormData(std::to_string(formId)) == ERR_OK) {
131 return ERR_OK;
132 } else {
133 return ERR_APPEXECFWK_FORM_COMMON_CODE;
134 }
135 }
136 /**
137 * @brief Delete form data in DbCache and DB with formId.
138 * @param formId form data Id.
139 * @param removedDBForms Removed db form infos
140 * @return Returns ERR_OK on success, others on failure.
141 */
DeleteFormInfoByBundleName(const std::string & bundleName,const int32_t userId,std::vector<FormDBInfo> & removedDBForms)142 ErrCode FormDbCache::DeleteFormInfoByBundleName(const std::string &bundleName, const int32_t userId,
143 std::vector<FormDBInfo> &removedDBForms)
144 {
145 HILOG_INFO("bundleName: %{public}s", bundleName.c_str());
146 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
147 std::vector<FormDBInfo>::iterator itRecord;
148 for (itRecord = formDBInfos_.begin(); itRecord != formDBInfos_.end();) {
149 if ((bundleName == itRecord->bundleName) && (userId == itRecord->providerUserId)) {
150 int64_t formId = itRecord->formId;
151 if (FormInfoRdbStorageMgr::GetInstance().DeleteStorageFormData(std::to_string(formId)) == ERR_OK) {
152 removedDBForms.emplace_back(*itRecord);
153 itRecord = formDBInfos_.erase(itRecord);
154 } else {
155 itRecord++;
156 }
157 } else {
158 itRecord++;
159 }
160 }
161 return ERR_OK;
162 }
163
164 /**
165 * @brief Get all form data from DbCache.
166 * @param formDBInfos Storage all DbCache.
167 * @return Void.
168 */
GetAllFormInfo(std::vector<FormDBInfo> & formDBInfos)169 void FormDbCache::GetAllFormInfo(std::vector<FormDBInfo> &formDBInfos)
170 {
171 HILOG_INFO("call");
172 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
173 formDBInfos = formDBInfos_;
174 }
175
176 /**
177 * @brief Get all form data in DbCache and DB by bundleName.
178 * @param bundleName BundleName.
179 * @param userId user ID.
180 * @param formDBInfos all db form infos
181 */
GetAllFormDBInfoByBundleName(const std::string & bundleName,const int32_t userId,std::vector<FormDBInfo> & formDBInfos)182 void FormDbCache::GetAllFormDBInfoByBundleName(const std::string &bundleName, const int32_t userId,
183 std::vector<FormDBInfo> &formDBInfos)
184 {
185 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
186 for (auto dbInfo : formDBInfos_) {
187 if (bundleName == dbInfo.bundleName && dbInfo.userId == userId) {
188 formDBInfos.push_back(dbInfo);
189 }
190 }
191 }
192
193 /**
194 * @brief Get record from DB cache with formId
195 * @param formId Form data Id
196 * @param record Form data
197 * @return Returns ERR_OK on success, others on failure.
198 */
GetDBRecord(const int64_t formId,FormRecord & record) const199 ErrCode FormDbCache::GetDBRecord(const int64_t formId, FormRecord &record) const
200 {
201 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
202 for (const FormDBInfo &dbInfo : formDBInfos_) {
203 if (dbInfo.formId == formId) {
204 record.userId = dbInfo.userId;
205 record.providerUserId= dbInfo.providerUserId;
206 record.formName = dbInfo.formName;
207 record.bundleName = dbInfo.bundleName;
208 record.moduleName = dbInfo.moduleName;
209 record.abilityName = dbInfo.abilityName;
210 record.formUserUids = dbInfo.formUserUids;
211 record.formLocation = dbInfo.formLocation;
212 record.enableForm = dbInfo.enableForm;
213 record.lockForm = dbInfo.lockForm;
214 return ERR_OK;
215 }
216 }
217 HILOG_ERROR("not find formId[%{public}" PRId64 "]", formId);
218 return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
219 }
220 /**
221 * @brief Get record from DB cache with formId
222 * @param formId Form data Id
223 * @param record Form db data
224 * @return Returns ERR_OK on success, others on failure.
225 */
GetDBRecord(const int64_t formId,FormDBInfo & record) const226 ErrCode FormDbCache::GetDBRecord(const int64_t formId, FormDBInfo &record) const
227 {
228 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
229 for (const FormDBInfo &dbInfo : formDBInfos_) {
230 if (dbInfo.formId == formId) {
231 record = dbInfo;
232 return ERR_OK;
233 }
234 }
235 HILOG_ERROR("not find formId[%{public}" PRId64 "]", formId);
236 return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
237 }
238 /**
239 * @brief Use record save or update DB data and DB cache with formId
240 * @param formId Form data Id
241 * @param record Form data
242 * @return Returns ERR_OK on success, others on failure.
243 */
UpdateDBRecord(const int64_t formId,const FormRecord & record) const244 ErrCode FormDbCache::UpdateDBRecord(const int64_t formId, const FormRecord &record) const
245 {
246 FormDBInfo formDBInfo(formId, record);
247 return FormDbCache::GetInstance().SaveFormInfo(formDBInfo);
248 }
249 /**
250 * @brief Get no host db record.
251 * @param uid The caller uid.
252 * @param noHostFormDBList no host db record list.
253 * @param foundFormsMap Form Id list.
254 * @return Returns ERR_OK on success, others on failure.
255 */
GetNoHostDBForms(const int uid,std::map<FormIdKey,std::set<int64_t>> & noHostFormDBList,std::map<int64_t,bool> & foundFormsMap)256 ErrCode FormDbCache::GetNoHostDBForms(const int uid, std::map<FormIdKey,
257 std::set<int64_t>> &noHostFormDBList, std::map<int64_t, bool> &foundFormsMap)
258 {
259 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
260 for (FormDBInfo& dbInfo : formDBInfos_) {
261 if (dbInfo.Contains(uid)) {
262 dbInfo.Remove(uid);
263 if (dbInfo.formUserUids.empty()) {
264 FormIdKey formIdKey(dbInfo.bundleName, dbInfo.abilityName);
265 auto itIdsSet = noHostFormDBList.find(formIdKey);
266 if (itIdsSet == noHostFormDBList.end()) {
267 std::set<int64_t> formIdsSet;
268 formIdsSet.emplace(dbInfo.formId);
269 noHostFormDBList.emplace(formIdKey, formIdsSet);
270 } else {
271 itIdsSet->second.emplace(dbInfo.formId);
272 }
273 } else {
274 foundFormsMap.emplace(dbInfo.formId, false);
275 SaveFormInfoNolock(dbInfo);
276 FormBmsHelper::GetInstance().NotifyModuleNotRemovable(dbInfo.bundleName, dbInfo.moduleName);
277 }
278 }
279 }
280 return ERR_OK;
281 }
282 /**
283 * @brief Get match count by bundleName and moduleName.
284 * @param bundleName BundleName.
285 * @param moduleName ModuleName.
286 * @return Returns match count.
287 */
GetMatchCount(const std::string & bundleName,const std::string & moduleName)288 int FormDbCache::GetMatchCount(const std::string &bundleName, const std::string &moduleName)
289 {
290 int32_t matchCount = 0;
291 std::vector<FormDBInfo> formDBInfos;
292 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
293 for (const FormDBInfo &dbInfo : formDBInfos_) {
294 if (dbInfo.bundleName == bundleName && dbInfo.moduleName == moduleName) {
295 ++matchCount;
296 }
297 }
298 return matchCount;
299 }
300 /**
301 * @brief delete forms bu userId.
302 *
303 * @param userId user ID.
304 */
DeleteDBFormsByUserId(const int32_t userId)305 void FormDbCache::DeleteDBFormsByUserId(const int32_t userId)
306 {
307 HILOG_INFO("userId: %{public}d", userId);
308 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
309 std::vector<FormDBInfo>::iterator itRecord;
310 for (itRecord = formDBInfos_.begin(); itRecord != formDBInfos_.end();) {
311 if (userId == itRecord->providerUserId) {
312 int64_t formId = itRecord->formId;
313 if (FormInfoRdbStorageMgr::GetInstance().DeleteStorageFormData(std::to_string(formId)) == ERR_OK) {
314 itRecord = formDBInfos_.erase(itRecord);
315 } else {
316 HILOG_ERROR("fail delete form, formId[%{public}" PRId64 "]", formId);
317 itRecord++;
318 }
319 } else {
320 itRecord++;
321 }
322 }
323 }
324
325 /**
326 * @brief handle get no host invalid DB forms.
327 * @param userId User ID.
328 * @param callingUid The UID of the proxy.
329 * @param matchedFormIds The set of the valid forms.
330 * @param noHostDBFormsMap The map of the no host forms.
331 * @param foundFormsMap The map of the found forms.
332 */
GetNoHostInvalidDBForms(int32_t userId,int32_t callingUid,std::set<int64_t> & matchedFormIds,std::map<FormIdKey,std::set<int64_t>> & noHostDBFormsMap,std::map<int64_t,bool> & foundFormsMap)333 void FormDbCache::GetNoHostInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
334 std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
335 std::map<int64_t, bool> &foundFormsMap)
336 {
337 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
338 for (auto &formRecord: formDBInfos_) {
339 int64_t formId = formRecord.formId;
340 // check UID
341 auto iter = std::find(formRecord.formUserUids.begin(), formRecord.formUserUids.end(), callingUid);
342 if (iter == formRecord.formUserUids.end()) {
343 continue;
344 }
345 // check valid form set
346 if (matchedFormIds.find(formId) != matchedFormIds.end()) {
347 continue;
348 }
349 // checks if the form provider is the currently active user.
350 if (formRecord.providerUserId != userId) {
351 continue;
352 }
353
354 HILOG_WARN("found invalid form:%{public}" PRId64, formId);
355 formRecord.formUserUids.erase(iter);
356 if (formRecord.formUserUids.empty()) {
357 FormIdKey formIdKey(formRecord.bundleName, formRecord.abilityName);
358 auto itIdsSet = noHostDBFormsMap.find(formIdKey);
359 if (itIdsSet == noHostDBFormsMap.end()) {
360 std::set<int64_t> formIdsSet;
361 formIdsSet.emplace(formId);
362 noHostDBFormsMap.emplace(formIdKey, formIdsSet);
363 } else {
364 itIdsSet->second.emplace(formId);
365 }
366 } else {
367 foundFormsMap.emplace(formId, false);
368 SaveFormInfoNolock(formRecord);
369 FormBmsHelper::GetInstance().NotifyModuleNotRemovable(formRecord.bundleName, formRecord.moduleName);
370 }
371 }
372 }
373
374 /**
375 * @brief handle delete no host DB forms.
376 * @param callingUid The UID of the proxy.
377 * @param noHostFormDbMap The map of the no host forms.
378 * @param foundFormsMap The map of the found forms.
379 */
BatchDeleteNoHostDBForms(int32_t callingUid,std::map<FormIdKey,std::set<int64_t>> & noHostDBFormsMap,std::map<int64_t,bool> & foundFormsMap)380 void FormDbCache::BatchDeleteNoHostDBForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
381 std::map<int64_t, bool> &foundFormsMap)
382 {
383 std::set<FormIdKey> removableModuleSet;
384 for (auto &element : noHostDBFormsMap) {
385 std::set<int64_t> &formIds = element.second;
386 FormIdKey formIdKey = element.first;
387 std::string bundleName = formIdKey.bundleName;
388 std::string abilityName = formIdKey.abilityName;
389 FormProviderMgr::GetInstance().NotifyProviderFormsBatchDelete(bundleName, abilityName, formIds);
390 for (const int64_t formId : formIds) {
391 foundFormsMap.emplace(formId, true);
392 FormDBInfo dbInfo;
393 int errCode = GetDBRecord(formId, dbInfo);
394 if (errCode == ERR_OK) {
395 FormIdKey removableModuleFormIdKey(dbInfo.bundleName, dbInfo.moduleName);
396 removableModuleSet.emplace(removableModuleFormIdKey);
397 DeleteFormInfo(formId);
398 }
399 FormDataMgr::GetInstance().StopRenderingForm(formId);
400 FormDataMgr::GetInstance().DeleteFormRecord(formId);
401 }
402 }
403
404 for (const FormIdKey &item : removableModuleSet) {
405 int32_t matchCount = GetMatchCount(item.bundleName, item.moduleName);
406 if (matchCount == 0) {
407 FormBmsHelper::GetInstance().NotifyModuleRemovable(item.bundleName, item.moduleName);
408 }
409 }
410 }
411
412 /**
413 * @brief handle delete invalid DB forms.
414 * @param userId User ID.
415 * @param callingUid The UID of the proxy.
416 * @param matchedFormIds The set of the valid forms.
417 * @param removedFormsMap The map of the removed invalid forms.
418 * @return Returns ERR_OK on success, others on failure.
419 */
DeleteInvalidDBForms(int32_t userId,int32_t callingUid,std::set<int64_t> & matchedFormIds,std::map<int64_t,bool> & removedFormsMap)420 ErrCode FormDbCache::DeleteInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
421 std::map<int64_t, bool> &removedFormsMap)
422 {
423 HILOG_INFO("userId:%{public}d, callingUid:%{public}d", userId, callingUid);
424 std::map<int64_t, bool> foundFormsMap {};
425 std::map<FormIdKey, std::set<int64_t>> noHostDBFormsMap {};
426
427 GetNoHostInvalidDBForms(userId, callingUid, matchedFormIds, noHostDBFormsMap, foundFormsMap);
428
429 if (!foundFormsMap.empty()) {
430 for (const auto &element : foundFormsMap) {
431 FormDataMgr::GetInstance().DeleteFormUserUid(element.first, callingUid);
432 }
433 }
434
435 BatchDeleteNoHostDBForms(callingUid, noHostDBFormsMap, foundFormsMap);
436 HILOG_DEBUG("foundFormsMap size:%{public}zu", foundFormsMap.size());
437 HILOG_DEBUG("noHostDBFormsMap size:%{public}zu", noHostDBFormsMap.size());
438
439 if (!foundFormsMap.empty()) {
440 removedFormsMap.insert(foundFormsMap.begin(), foundFormsMap.end());
441 #ifdef THEME_MGR_ENABLE
442 DeleteThemeForms(removedFormsMap);
443 #endif
444 }
445
446 HILOG_INFO("done");
447 return ERR_OK;
448 }
449
450 #ifdef THEME_MGR_ENABLE
DeleteThemeForms(std::map<int64_t,bool> & removedFormsMap)451 void FormDbCache::DeleteThemeForms(std::map<int64_t, bool> &removedFormsMap)
452 {
453 HILOG_INFO("call");
454 std::vector<int64_t> removeList;
455 for (const auto &element : removedFormsMap) {
456 FormDBInfo dbInfo;
457 ErrCode getDbRet = FormDbCache::GetInstance().GetDBRecord(element.first, dbInfo);
458 HILOG_INFO("getDbRet:%{public}d, isThemeForm:%{public}d", getDbRet, dbInfo.isThemeForm);
459 if (getDbRet == ERR_OK && dbInfo.isThemeForm) {
460 removeList.emplace_back(element.first);
461 }
462 }
463 if (!removeList.empty()) {
464 ThemeManager::ThemeManagerClient::GetInstance().DeleteForm(removeList);
465 }
466 }
467 #endif
468
IsHostOwner(int64_t formId,int32_t hostUid)469 bool FormDbCache::IsHostOwner(int64_t formId, int32_t hostUid)
470 {
471 FormDBInfo dbInfo;
472 if (GetDBRecord(formId, dbInfo) != ERR_OK) {
473 HILOG_ERROR("get db record failed. formId:%{public}s", std::to_string(formId).c_str());
474 return false;
475 }
476
477 auto iter = std::find(dbInfo.formUserUids.begin(), dbInfo.formUserUids.end(), hostUid);
478 if (iter != dbInfo.formUserUids.end()) {
479 return true;
480 }
481
482 return false;
483 }
484
UpdateFormLocation(const int64_t formId,const int32_t formLocation)485 ErrCode FormDbCache::UpdateFormLocation(const int64_t formId, const int32_t formLocation)
486 {
487 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
488 std::vector<FormDBInfo>::iterator itRecord;
489 for (itRecord = formDBInfos_.begin(); itRecord != formDBInfos_.end();) {
490 if (itRecord->formId == formId) {
491 itRecord->formLocation = (Constants::FormLocation)formLocation;
492 InnerFormInfo innerFormInfo(*itRecord);
493 return FormInfoRdbStorageMgr::GetInstance().ModifyStorageFormData(innerFormInfo);
494 }
495 ++itRecord;
496 }
497 return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
498 }
499
500 /**
501 * @brief Get form counts from DbCache by calling user id.
502 * @param currentAccountId current account ID.
503 * @param callingUid calling user ID.
504 * @return Returns form counts.
505 */
GetFormCountsByCallingUid(const int32_t currentAccountId,const int callingUid)506 int FormDbCache::GetFormCountsByCallingUid(const int32_t currentAccountId, const int callingUid)
507 {
508 int callingUidFormCounts = 0;
509 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
510 for (const auto &record : formDBInfos_) {
511 if (record.providerUserId != currentAccountId) {
512 continue;
513 }
514 for (const auto &userUid : record.formUserUids) {
515 if (userUid != callingUid) {
516 continue;
517 }
518 callingUidFormCounts++;
519 }
520 }
521 return callingUidFormCounts;
522 }
523
524 /**
525 * @brief Get all form data size from DbCache.
526 * @return Returns form data size.
527 */
GetAllFormInfoSize()528 int32_t FormDbCache::GetAllFormInfoSize()
529 {
530 HILOG_INFO("call");
531 std::lock_guard<std::mutex> lock(formDBInfosMutex_);
532 return static_cast<int32_t>(formDBInfos_.size());
533 }
534
GetMultiAppFormVersionCode(const std::string & bundleName)535 uint32_t FormDbCache::GetMultiAppFormVersionCode(const std::string &bundleName)
536 {
537 std::lock_guard<std::mutex> lock(multiAppFormVersionCodeMutex_);
538 auto iter = multiAppFormVersionCodeMap_.find(bundleName);
539 if (iter != multiAppFormVersionCodeMap_.end()) {
540 return iter->second;
541 }
542 std::string versionCode;
543 if (FormInfoRdbStorageMgr::GetInstance().GetMultiAppFormVersionCode(bundleName, versionCode) != ERR_OK ||
544 versionCode.empty()) {
545 return 0;
546 }
547 uint32_t code = static_cast<uint32_t>(FormUtil::ConvertStringToInt(versionCode));
548 multiAppFormVersionCodeMap_.emplace(bundleName, code);
549 return code;
550 }
551
UpdateMultiAppFormVersionCode(const std::string & bundleName,uint32_t versionCode)552 void FormDbCache::UpdateMultiAppFormVersionCode(const std::string &bundleName, uint32_t versionCode)
553 {
554 std::lock_guard<std::mutex> lock(multiAppFormVersionCodeMutex_);
555 auto iter = multiAppFormVersionCodeMap_.find(bundleName);
556 if (iter != multiAppFormVersionCodeMap_.end() && iter->second == versionCode) {
557 return;
558 }
559 multiAppFormVersionCodeMap_[bundleName] = versionCode;
560 FormInfoRdbStorageMgr::GetInstance().UpdateMultiAppFormVersionCode(bundleName, std::to_string(versionCode));
561 }
562 } // namespace AppExecFwk
563 } // namespace OHOS
564