• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "form_mgr_adapter.h"
17 
18 #include <cinttypes>
19 #include <regex>
20 
21 #include "ability_manager_errors.h"
22 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
23 #include "bundle_active_client.h"
24 #endif
25 #include "form_acquire_connection.h"
26 #include "form_acquire_state_connection.h"
27 #include "form_ams_helper.h"
28 #include "form_background_connection.h"
29 #include "form_bms_helper.h"
30 #include "form_cache_mgr.h"
31 #include "form_cast_temp_connection.h"
32 #include "form_constants.h"
33 #include "form_data_mgr.h"
34 #include "form_db_cache.h"
35 #include "form_db_info.h"
36 #include "form_dump_mgr.h"
37 #include "form_event_notify_connection.h"
38 #include "form_info_mgr.h"
39 #include "form_mgr_errors.h"
40 #include "form_provider_info.h"
41 #include "form_provider_interface.h"
42 #include "form_provider_mgr.h"
43 #include "form_render_connection.h"
44 #include "form_render_mgr.h"
45 #include "form_share_mgr.h"
46 #include "form_supply_callback.h"
47 #include "form_timer_mgr.h"
48 #include "form_util.h"
49 #include "hilog_wrapper.h"
50 #include "hitrace_meter.h"
51 #include "if_system_ability_manager.h"
52 #include "in_process_call_wrapper.h"
53 #include "ipc_skeleton.h"
54 #include "iservice_registry.h"
55 #include "system_ability_definition.h"
56 
57 namespace OHOS {
58 namespace AppExecFwk {
59 namespace {
60 constexpr int32_t CALLING_UID_TRANSFORM_DIVISOR = 200000;
61 constexpr int32_t SYSTEM_UID = 1000;
62 const std::string POINT_ETS = ".ets";
63 } // namespace
64 
FormMgrAdapter()65 FormMgrAdapter::FormMgrAdapter()
66 {
67 }
~FormMgrAdapter()68 FormMgrAdapter::~FormMgrAdapter()
69 {
70 }
71 /**
72  * @brief Add form with want, send want to form manager service.
73  * @param formId The Id of the forms to add.
74  * @param want The want of the form to add.
75  * @param callerToken Caller ability token.
76  * @param formInfo Form info.
77  * @return Returns ERR_OK on success, others on failure.
78  */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)79 int FormMgrAdapter::AddForm(const int64_t formId, const Want &want,
80     const sptr<IRemoteObject> &callerToken, FormJsInfo &formInfo)
81 {
82     if (formId < 0 || callerToken == nullptr) {
83         HILOG_ERROR("%{public}s fail, callerToken can not be NULL", __func__);
84         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
85     }
86 
87     // check form count limit
88     bool tempFormFlag = want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false);
89     int callingUid = IPCSkeleton::GetCallingUid();
90     int checkCode = 0;
91     if (tempFormFlag && !FormRenderMgr::GetInstance().IsRerenderForRenderServiceDied(formId)) {
92         if (formId > 0) {
93             HILOG_ERROR("%{public}s fail, temp form id is invalid, formId:%{public}" PRId64 "", __func__, formId);
94             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
95         }
96         checkCode = FormDataMgr::GetInstance().CheckTempEnoughForm();
97     } else {
98         if (formId == 0) {
99             // get current userId
100             int32_t currentUserId = GetCurrentUserId(callingUid);
101             checkCode = FormDataMgr::GetInstance().CheckEnoughForm(callingUid, currentUserId);
102         }
103     }
104     if (checkCode != 0) {
105         HILOG_ERROR("%{public}s fail, too much forms in system", __func__);
106         return checkCode;
107     }
108 
109     // get from config info
110     FormItemInfo formItemInfo;
111     int32_t errCode = GetFormConfigInfo(want, formItemInfo);
112     if (errCode != ERR_OK) {
113         HILOG_ERROR("%{public}s fail, get form config info failed.", __func__);
114         return errCode;
115     }
116     formItemInfo.SetFormId(formId);
117 
118     // publish form
119     if (formId > 0 && FormDataMgr::GetInstance().IsRequestPublishForm(formId)) {
120         return AddRequestPublishForm(formItemInfo, want, callerToken, formInfo);
121     }
122 
123     Want newWant(want);
124     // in application form
125     if (formItemInfo.GetProviderBundleName() == formItemInfo.GetHostBundleName()) {
126         HILOG_DEBUG("form in application");
127         newWant.SetParam(Constants::PARAM_FORM_HOST_TOKEN, callerToken);
128     }
129 
130     WantParams wantParams = newWant.GetParams();
131     // share form
132     if (formId == 0 && DelayedSingleton<FormShareMgr>::GetInstance()->IsShareForm(newWant)) {
133         DelayedSingleton<FormShareMgr>::GetInstance()->AddProviderData(newWant, wantParams);
134     }
135     if (formId > 0) {
136         return AllotFormById(formItemInfo, callerToken, wantParams, formInfo);
137     } else {
138         return AllotFormByInfo(formItemInfo, callerToken, wantParams, formInfo);
139     }
140 }
141 
142 /**
143  * @brief Delete forms with formIds, send formIds to form manager service.
144  * @param formId The Id of the forms to delete.
145  * @param callerToken Caller ability token.
146  * @return Returns ERR_OK on success, others on failure.
147  */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)148 int FormMgrAdapter::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
149 {
150     if (formId <= 0 || callerToken == nullptr) {
151         HILOG_ERROR("%{public}s, deleteForm invalid param", __func__);
152         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
153     }
154 
155     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
156     // remove connection for in application form
157     FormSupplyCallback::GetInstance()->RemoveConnection(matchedFormId, callerToken);
158     if (FormDataMgr::GetInstance().ExistTempForm(matchedFormId)) {
159         // delete temp form if receive delete form call
160         return HandleDeleteTempForm(matchedFormId, callerToken);
161     }
162     return HandleDeleteForm(matchedFormId, callerToken);
163 }
164 
165 /**
166  * @brief Stop rendering form.
167  * @param formId The Id of the forms to delete.
168  * @param compId The compId of the forms to delete.
169  * @return Returns ERR_OK on success, others on failure.
170  */
StopRenderingForm(const int64_t formId,const std::string & compId)171 int FormMgrAdapter::StopRenderingForm(const int64_t formId, const std::string &compId)
172 {
173     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
174     if (formId <= 0 || compId.empty()) {
175         HILOG_ERROR("%{public}s, deleteForm invalid param", __func__);
176         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
177     }
178 
179     FormRecord record;
180     FormDataMgr::GetInstance().GetFormRecord(formId, record);
181     FormRenderMgr::GetInstance().StopRenderingForm(formId, record, compId);
182     return ERR_OK;
183 }
184 
185 
186 /**
187  * @brief Release forms with formIds, send formIds to form Mgr service.
188  * @param formId The Id of the forms to release.
189  * @param callerToken Caller ability token.
190  * @param delCache Delete Cache or not.
191  * @return Returns ERR_OK on success, others on failure.
192  */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)193 int FormMgrAdapter::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
194 {
195     HILOG_INFO("%{public}s called.", __func__);
196     if (formId <= 0 || callerToken == nullptr) {
197         HILOG_ERROR("%{public}s, releaseForm invalid param", __func__);
198         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
199     }
200     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
201     // remove connection for in application form
202     FormSupplyCallback::GetInstance()->RemoveConnection(matchedFormId, callerToken);
203     if (FormDataMgr::GetInstance().ExistTempForm(matchedFormId)) {
204         // delete temp form if receive release form call
205         return HandleDeleteTempForm(matchedFormId, callerToken);
206     }
207     FormRecord record;
208     FormDataMgr::GetInstance().GetFormRecord(formId, record);
209     FormRenderMgr::GetInstance().StopRenderingForm(formId, record, "", callerToken);
210     FormRecord dbRecord;
211     if (FormDbCache::GetInstance().GetDBRecord(matchedFormId, dbRecord) != ERR_OK) {
212         HILOG_ERROR("%{public}s, not exist such db form:%{public}" PRId64 "", __func__, formId);
213         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
214     }
215     int callingUid = IPCSkeleton::GetCallingUid();
216     int32_t userId = GetCurrentUserId(callingUid);
217     bool isSelfDbFormId = (userId == dbRecord.userId) && ((std::find(dbRecord.formUserUids.begin(),
218         dbRecord.formUserUids.end(), callingUid) != dbRecord.formUserUids.end()) ? true : false);
219     if (!isSelfDbFormId) {
220         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 "", __func__, formId);
221         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
222     }
223     if (delCache) {
224         ErrCode result = HandleReleaseForm(matchedFormId, callerToken);
225         if (result != ERR_OK) {
226             HILOG_ERROR("%{public}s, release form error.", __func__);
227             return result;
228         }
229     }
230 
231     if (!FormDataMgr::GetInstance().DeleteHostRecord(callerToken, matchedFormId)) {
232         HILOG_ERROR("%{public}s, failed to remove host record", __func__);
233         return ERR_APPEXECFWK_FORM_COMMON_CODE;
234     }
235     if (!FormTimerMgr::GetInstance().RemoveFormTimer(matchedFormId)) {
236         HILOG_ERROR("%{public}s, remove timer error", __func__);
237         return ERR_APPEXECFWK_FORM_COMMON_CODE;
238     }
239     return ERR_OK;
240 }
241 
242 /**
243  * @brief Handle release form.
244  * @param formId The form id.
245  * @param callerToken Caller ability token.
246  * @return Returns ERR_OK on success, others on failure.
247  */
HandleReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)248 ErrCode FormMgrAdapter::HandleReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
249 {
250     HILOG_INFO("%{public}s called.", __func__);
251     if (!FormDataMgr::GetInstance().ExistFormRecord(formId)) {
252         HILOG_ERROR("%{public}s, not exist such db or temp form:%{public}" PRId64 "", __func__, formId);
253         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
254     }
255 
256     FormHostRecord hostRecord;
257     bool hasRecord = FormDataMgr::GetInstance().GetMatchedHostClient(callerToken, hostRecord);
258     bool isSelfId = hasRecord && hostRecord.Contains(formId);
259     if (!isSelfId) {
260         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 "", __func__, formId);
261         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
262     }
263 
264     HILOG_DEBUG("%{public}s, release formRecords, formId: %{public}" PRId64 "", __func__, formId);
265     FormDataMgr::GetInstance().DeleteFormUserUid(formId, IPCSkeleton::GetCallingUid());
266     if (!FormDataMgr::GetInstance().HasFormUserUids(formId)) {
267         FormDataMgr::GetInstance().DeleteFormRecord(formId);
268         if (!FormTimerMgr::GetInstance().RemoveFormTimer(formId)) {
269             HILOG_ERROR("%{public}s, remove timer error", __func__);
270             return ERR_APPEXECFWK_FORM_COMMON_CODE;
271         }
272     }
273     return ERR_OK;
274 }
275 
276 /**
277  * @brief Handle delete form.
278  * @param formId The form id.
279  * @param callerToken Caller ability token.
280  * @return Returns ERR_OK on success, others on failure.
281  */
HandleDeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)282 ErrCode FormMgrAdapter::HandleDeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
283 {
284     FormRecord dbRecord;
285     if (FormDbCache::GetInstance().GetDBRecord(formId, dbRecord) != ERR_OK) {
286         HILOG_ERROR("%{public}s, not exist such db form:%{public}" PRId64 "", __func__, formId);
287         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
288     }
289     FormRecord record;
290     FormDataMgr::GetInstance().GetFormRecord(formId, record);
291     FormRenderMgr::GetInstance().StopRenderingForm(formId, record, "", callerToken);
292 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
293     DeviceUsageStats::BundleActiveEvent event(record.bundleName, record.moduleName, record.formName,
294         record.specification, record.formId, DeviceUsageStats::BundleActiveEvent::FORM_IS_REMOVED);
295 #endif
296     int callingUid = IPCSkeleton::GetCallingUid();
297     int32_t userId = GetCurrentUserId(callingUid);
298     bool isSelfDbFormId = (userId == dbRecord.userId) && ((std::find(dbRecord.formUserUids.begin(),
299         dbRecord.formUserUids.end(), callingUid) != dbRecord.formUserUids.end()) ? true : false);
300     if (!isSelfDbFormId) {
301         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 ", callingUid:%{public}d",
302             __func__, formId, callingUid);
303         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
304     }
305 
306     ErrCode result = HandleDeleteFormCache(dbRecord, callingUid, formId);
307     if (result != ERR_OK) {
308         return result;
309     }
310 
311     if (!FormDataMgr::GetInstance().DeleteHostRecord(callerToken, formId)) {
312         HILOG_ERROR("%{public}s, failed to remove host record", __func__);
313         return ERR_APPEXECFWK_FORM_COMMON_CODE;
314     }
315 
316 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
317     DeviceUsageStats::BundleActiveClient::GetInstance().ReportEvent(event, userId);
318 #endif
319     return ERR_OK;
320 }
321 
322 /**
323  * @brief Handle delete temp form.
324  * @param formId The form id.
325  * @param callerToken Caller ability token.
326  * @return Returns ERR_OK on success, others on failure.
327  */
HandleDeleteTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)328 ErrCode FormMgrAdapter::HandleDeleteTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
329 {
330     HILOG_INFO("%{public}s called.", __func__);
331     int uid = IPCSkeleton::GetCallingUid();
332     FormRecord record;
333     bool isFormRecExist = FormDataMgr::GetInstance().GetFormRecord(formId, record);
334     bool isSelfTempFormId = false;
335     if (isFormRecExist && record.formTempFlag) {
336         int32_t userId = GetCurrentUserId(uid);
337         isSelfTempFormId = (userId == record.userId) && ((std::find(record.formUserUids.begin(),
338             record.formUserUids.end(), uid) != record.formUserUids.end()) ? true : false);
339     }
340     if (!isSelfTempFormId) {
341         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 "", __func__, formId);
342         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
343     }
344     FormRenderMgr::GetInstance().StopRenderingForm(formId, record, "", callerToken);
345     FormDataMgr::GetInstance().DeleteFormUserUid(formId, uid);
346     if (!FormDataMgr::GetInstance().HasFormUserUids(formId)) {
347         int result = FormProviderMgr::GetInstance().NotifyProviderFormDelete(formId, record);
348         if (result != ERR_OK) {
349             HILOG_ERROR("%{public}s, failed!", __func__);
350             FormDataMgr::GetInstance().AddFormUserUid(formId, uid);
351             return result;
352         }
353         if (!FormDataMgr::GetInstance().DeleteTempForm(formId)) {
354             HILOG_ERROR("%{public}s, form id is not existed.", __func__);
355             return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
356         }
357         FormDataMgr::GetInstance().DeleteFormRecord(formId);
358         if (!FormCacheMgr::GetInstance().DeleteData(formId)) {
359             HILOG_ERROR("%{public}s, failed to remove cache data", __func__);
360             return ERR_APPEXECFWK_FORM_COMMON_CODE;
361         }
362     }
363 
364     if (!FormDataMgr::GetInstance().DeleteHostRecord(callerToken, formId)) {
365         HILOG_ERROR("%{public}s, failed to remove host record", __func__);
366         return ERR_APPEXECFWK_FORM_COMMON_CODE;
367     }
368 
369     HILOG_DEBUG("%{public}s, record.formUserUids size: %{public}zu", __func__, record.formUserUids.size());
370     return ERR_OK;
371 }
372 
373 /**
374  * @brief Handle delete form cache.
375  * @param dbRecord Form storage information.
376  * @param uid calling user id.
377  * @param formId The form id.
378  * @return Returns ERR_OK on success, others on failure.
379  */
HandleDeleteFormCache(FormRecord & dbRecord,const int uid,const int64_t formId)380 ErrCode FormMgrAdapter::HandleDeleteFormCache(FormRecord &dbRecord, const int uid, const int64_t formId)
381 {
382     HILOG_DEBUG("%{public}s, delete formDBRecords, formId: %{public}" PRId64 "", __func__, formId);
383     auto iter = std::find(dbRecord.formUserUids.begin(), dbRecord.formUserUids.end(), uid);
384     if (iter != dbRecord.formUserUids.end()) {
385         dbRecord.formUserUids.erase(iter);
386     }
387 
388     ErrCode result = ERR_OK;
389     if (dbRecord.formUserUids.empty()) {
390         result = FormProviderMgr::GetInstance().NotifyProviderFormDelete(formId, dbRecord);
391         if (result != ERR_OK) {
392             HILOG_ERROR("%{public}s, failed to notify provider form delete", __func__);
393             return result;
394         }
395         if (!FormDataMgr::GetInstance().DeleteFormRecord(formId)) {
396             HILOG_ERROR("%{public}s, failed to remove cache data", __func__);
397             return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
398         }
399         if (result = FormDbCache::GetInstance().DeleteFormInfo(formId); result != ERR_OK) {
400             HILOG_ERROR("%{public}s, failed to remove db data", __func__);
401             return result;
402         }
403 
404         int32_t matchCount = FormDbCache::GetInstance().GetMatchCount(dbRecord.bundleName, dbRecord.moduleName);
405         if (matchCount == 0) {
406             FormBmsHelper::GetInstance().NotifyModuleRemovable(dbRecord.bundleName, dbRecord.moduleName);
407         }
408 
409         if (!FormCacheMgr::GetInstance().DeleteData(formId)) {
410             HILOG_ERROR("%{public}s, failed to remove cache data", __func__);
411             return ERR_APPEXECFWK_FORM_COMMON_CODE;
412         }
413         if (!FormTimerMgr::GetInstance().RemoveFormTimer(formId)) {
414             HILOG_ERROR("%{public}s, remove timer error", __func__);
415             return ERR_APPEXECFWK_FORM_COMMON_CODE;
416         }
417         return ERR_OK;
418     }
419 
420     if (result = FormDbCache::GetInstance().UpdateDBRecord(formId, dbRecord); result != ERR_OK) {
421         return result;
422     }
423 
424     HILOG_DEBUG("%{public}s, dbRecord.formUserUids size: %{public}zu", __func__, dbRecord.formUserUids.size());
425     FormBmsHelper::GetInstance().NotifyModuleNotRemovable(dbRecord.bundleName, dbRecord.moduleName);
426     FormDataMgr::GetInstance().DeleteFormUserUid(formId, uid);
427     return result;
428 }
429 
430 /**
431  * @brief Update form with formId, send formId to form manager service.
432  * @param formId The Id of the form to update.
433  * @param bundleName Provider ability bundleName.
434  * @param formProviderData form provider data.
435  * @return Returns ERR_OK on success, others on failure.
436  */
UpdateForm(const int64_t formId,const std::string & bundleName,const FormProviderData & formProviderData)437 int FormMgrAdapter::UpdateForm(const int64_t formId,
438     const std::string &bundleName, const FormProviderData &formProviderData)
439 {
440     HILOG_INFO("%{public}s start.", __func__);
441 
442     // check formId and bundleName
443     if (formId <= 0 || bundleName.empty()) {
444         HILOG_ERROR("%{public}s error, invalid formId or bundleName.", __func__);
445         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
446     }
447     // check bundle uid for permission
448     int32_t callingUid = IPCSkeleton::GetCallingUid();
449     int32_t userId = GetCurrentUserId(callingUid);
450 
451     // get uid
452     int32_t bundleUid = FormBmsHelper::GetInstance().GetUidByBundleName(bundleName, userId);
453     if (bundleUid != callingUid) {
454         HILOG_ERROR("%{public}s error, permission denied, the updated form is not your own.", __func__);
455         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
456     }
457 
458     // find matched formId
459     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
460 
461     // check exist and get the formRecord
462     FormRecord formRecord;
463     if (!FormDataMgr::GetInstance().GetFormRecord(matchedFormId, formRecord)) {
464         HILOG_ERROR("%{public}s error, not exist such form:%{public}" PRId64 ".", __func__, matchedFormId);
465         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
466     }
467 
468     if (userId != formRecord.userId && !checkFormHostHasSaUid(formRecord)) {
469         HILOG_ERROR("%{public}s error, not under current user, formId:%{public}" PRId64 ".", __func__, matchedFormId);
470         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
471     }
472 
473     // check bundleName match
474     if (formRecord.bundleName.compare(bundleName) != 0) {
475         HILOG_ERROR("%{public}s error, not match bundleName:%{public}s.", __func__, bundleName.c_str());
476         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
477     }
478 
479     if (formRecord.uiSyntax == FormType::ETS) {
480         WantParams wantParams;
481         return FormRenderMgr::GetInstance().UpdateRenderingForm(formId, formProviderData, wantParams, true);
482     } else {
483         // update Form
484        return FormProviderMgr::GetInstance().UpdateForm(matchedFormId, formRecord, formProviderData);
485     }
486 }
487 
488 /**
489  * @brief Request form with formId and want, send formId and want to form manager service.
490  * @param formId The Id of the form to update.
491  * @param callerToken Caller ability token.
492  * @param want The want of the form to request.
493  * @return Returns ERR_OK on success, others on failure.
494  */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)495 int FormMgrAdapter::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
496 {
497     HILOG_INFO("%{public}s called.", __func__);
498     if (formId <= 0 || callerToken == nullptr) {
499         HILOG_ERROR("%{public}s fail, invalid formId or callerToken.", __func__);
500         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
501     }
502 
503     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
504     if (!FormDataMgr::GetInstance().ExistFormRecord(matchedFormId)) {
505         HILOG_ERROR("%{public}s fail, not exist such formId:%{public}" PRId64 ".", __func__, matchedFormId);
506         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
507     }
508 
509     FormHostRecord formHostRecord;
510     bool isHostExist = FormDataMgr::GetInstance().GetMatchedHostClient(callerToken, formHostRecord);
511     if (!isHostExist) {
512         HILOG_ERROR("%{public}s fail, cannot find target client.", __func__);
513         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
514     }
515 
516     if (!formHostRecord.Contains(matchedFormId)) {
517         HILOG_ERROR("%{public}s fail, form is not self-owned.", __func__);
518         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
519     }
520 
521     HILOG_INFO("%{public}s, find target client.", __func__);
522     Want reqWant(want);
523     int callingUid = IPCSkeleton::GetCallingUid();
524     int32_t userId = GetCurrentUserId(callingUid);
525     reqWant.SetParam(Constants::PARAM_FORM_USER_ID, userId);
526     return FormProviderMgr::GetInstance().RefreshForm(matchedFormId, reqWant, true);
527 }
528 
529 /**
530  * @brief Form visible/invisible notify, send formIds to form manager service.
531  * @param formIds The vector of form Ids.
532  * @param callerToken Caller ability token.
533  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
534  * @return Returns ERR_OK on success, others on failure.
535  */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)536 ErrCode FormMgrAdapter::NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds,
537     const sptr<IRemoteObject> &callerToken, const int32_t formVisibleType)
538 {
539     HILOG_INFO("%{public}s called.", __func__);
540     if (callerToken == nullptr) {
541         HILOG_ERROR("%{public}s fail, callerToken can not be NULL.", __func__);
542         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
543     }
544 
545     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
546     if (iBundleMgr == nullptr) {
547         HILOG_ERROR("%{public}s fail, failed to get IBundleMgr.", __func__);
548         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
549     }
550 
551     int64_t matchedFormId = 0;
552     std::map<std::string, std::vector<int64_t>> eventMaps;
553     for (int64_t formId : formIds) {
554         if (formId <= 0) {
555             HILOG_WARN("%{public}s, formId %{public}" PRId64 " is less than 0", __func__, formId);
556             continue;
557         }
558         matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
559         FormRecord formRecord;
560         // Update provider info to host
561         if (!UpdateProviderInfoToHost(matchedFormId, callerToken, formVisibleType, formRecord)) {
562             continue;
563         }
564 
565         // Check if the form provider is system app
566         if (!CheckIsSystemAppByBundleName(iBundleMgr, formRecord.bundleName)) {
567             continue;
568         }
569 
570         // Create eventMaps
571         if (!CreateHandleEventMap(matchedFormId, formRecord, eventMaps)) {
572             continue;
573         }
574     }
575 
576     for (auto iter = eventMaps.begin(); iter != eventMaps.end(); iter++) {
577         if (HandleEventNotify(iter->first, iter->second, formVisibleType) != ERR_OK) {
578             HILOG_WARN("%{public}s fail, HandleEventNotify error, key is %{public}s.", __func__, iter->first.c_str());
579         }
580     }
581 
582     return ERR_OK;
583 }
584 
585 /**
586  * @brief Temp form to normal form.
587  * @param formId The Id of the form.
588  * @param callerToken Caller ability token.
589  * @return Returns ERR_OK on success, others on failure.
590  */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)591 int FormMgrAdapter::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
592 {
593     if (formId <= 0 || callerToken == nullptr) {
594         HILOG_ERROR("%{public}s, invalid formId or callerToken", __func__);
595         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
596     }
597 
598     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
599     if (!FormDataMgr::GetInstance().ExistFormRecord(matchedFormId) ||
600         !FormDataMgr::GetInstance().ExistTempForm(matchedFormId)) {
601         HILOG_ERROR("%{public}s, not exist such temp form:%{public}" PRId64 "", __func__, matchedFormId);
602         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
603     }
604 
605     FormHostRecord record;
606     bool hasRecord = FormDataMgr::GetInstance().GetMatchedHostClient(callerToken, record);
607     if (!hasRecord || !record.Contains(matchedFormId)) {
608         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 "", __func__, matchedFormId);
609         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
610     }
611 
612     int callingUid = IPCSkeleton::GetCallingUid();
613     int32_t userId = GetCurrentUserId(callingUid);
614     int checkCode = FormDataMgr::GetInstance().CheckEnoughForm(callingUid, userId);
615     if (checkCode != 0) {
616         HILOG_ERROR("%{public}s, %{public}" PRId64 " failed,because if too mush forms", __func__, matchedFormId);
617         return checkCode;
618     }
619 
620     FormRecord formRecord;
621     if (!FormDataMgr::GetInstance().GetFormRecord(matchedFormId, formRecord)) {
622         HILOG_ERROR("%{public}s fail, not exist such form:%{public}" PRId64 ".", __func__, matchedFormId);
623         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
624     }
625     int bindSupplierCheckCode = HandleCastTempForm(matchedFormId, formRecord);
626     if (bindSupplierCheckCode != 0) {
627         HILOG_ERROR("%{public}s, cast temp form bindSupplier failed", __func__);
628         return bindSupplierCheckCode;
629     }
630 
631     if (!FormDataMgr::GetInstance().DeleteTempForm(matchedFormId)) {
632         HILOG_ERROR("%{public}s fail, delete temp form error, formId:%{public}" PRId64 ".", __func__, matchedFormId);
633         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
634     }
635     if (!FormDataMgr::GetInstance().ModifyFormTempFlag(matchedFormId, false)) {
636         HILOG_ERROR("%{public}s fail, modify form temp flag error, formId:%{public}" PRId64 ".",
637             __func__, matchedFormId);
638         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
639     }
640     if (!FormDataMgr::GetInstance().AddFormUserUid(matchedFormId, callingUid)) {
641         HILOG_ERROR("%{public}s fail, add form user uid error, formId:%{public}" PRId64 ".", __func__, matchedFormId);
642         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
643     }
644 
645     if (!FormDataMgr::GetInstance().GetFormRecord(matchedFormId, formRecord)) {
646         HILOG_ERROR("%{public}s fail, not exist such form:%{public}" PRId64 ".", __func__, matchedFormId);
647         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
648     }
649 
650     ErrCode errorCode = FormDbCache::GetInstance().UpdateDBRecord(matchedFormId, formRecord);
651     if (errorCode != ERR_OK) {
652         HILOG_ERROR("%{public}s fail, update db record error, formId:%{public}" PRId64 ".", __func__, matchedFormId);
653         return errorCode;
654     }
655 
656     // start timer
657     return AddFormTimer(formRecord);
658 }
659 /**
660  * @brief Handle cast temp form.
661  * @param formId The form id.
662  * @param record Form information.
663  * @return Returns ERR_OK on success, others on failure.
664  */
HandleCastTempForm(const int64_t formId,const FormRecord & record)665 ErrCode FormMgrAdapter::HandleCastTempForm(const int64_t formId, const FormRecord &record)
666 {
667     HILOG_DEBUG("%{public}s, cast temp form to normal form, notify supplier, package:%{public}s, class:%{public}s",
668         __func__, record.bundleName.c_str(), record.abilityName.c_str());
669     sptr<IAbilityConnection> castTempConnection = new FormCastTempConnection(formId,
670         record.bundleName, record.abilityName);
671 
672     Want want;
673     want.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
674     want.SetElementName(record.bundleName, record.abilityName);
675     ErrCode errorCode = FormAmsHelper::GetInstance().ConnectServiceAbility(want, castTempConnection);
676     if (errorCode != ERR_OK) {
677         HILOG_ERROR("%{public}s fail, ConnectServiceAbility failed.", __func__);
678         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
679     }
680     return ERR_OK;
681 }
682 /**
683  * @brief Dump all of form storage infos.
684  * @param formInfos All of form storage infos.
685  * @return Returns ERR_OK on success, others on failure.
686  */
DumpStorageFormInfos(std::string & formInfos) const687 int FormMgrAdapter::DumpStorageFormInfos(std::string &formInfos) const
688 {
689     std::vector<FormDBInfo> formDBInfos;
690     FormDbCache::GetInstance().GetAllFormInfo(formDBInfos);
691     if (formDBInfos.empty()) {
692         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
693     }
694     std::sort(formDBInfos.begin(), formDBInfos.end(),
695         [] (const FormDBInfo &formDBInfoA, const FormDBInfo &formDBInfoB) -> bool {
696         return formDBInfoA.formId < formDBInfoB.formId;
697     });
698     FormDumpMgr::GetInstance().DumpStorageFormInfos(formDBInfos, formInfos);
699     return ERR_OK;
700 }
701 /**
702  * @brief Dump form info by a bundle name.
703  * @param bundleName The bundle name of form provider.
704  * @param formInfos Form infos.
705  * @return Returns ERR_OK on success, others on failure.
706  */
DumpFormInfoByBundleName(const std::string & bundleName,std::string & formInfos) const707 int FormMgrAdapter::DumpFormInfoByBundleName(const std::string &bundleName, std::string &formInfos) const
708 {
709     HILOG_INFO("%{public}s called.", __func__);
710     std::vector<FormRecord> formRecordInfos;
711     if (!FormDataMgr::GetInstance().GetFormRecord(bundleName, formRecordInfos)) {
712         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
713     }
714     FormDumpMgr::GetInstance().DumpFormInfos(formRecordInfos, formInfos);
715     return ERR_OK;
716 }
717 /**
718  * @brief Dump form info by a bundle name.
719  * @param formId The id of the form.
720  * @param formInfo Form info.
721  * @return Returns ERR_OK on success, others on failure.
722  */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo) const723 int FormMgrAdapter::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo) const
724 {
725     HILOG_INFO("%{public}s called.", __func__);
726     int reply = ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
727 
728     FormRecord formRecord;
729     if (FormDataMgr::GetInstance().GetFormRecord(formId, formRecord)) {
730         FormDumpMgr::GetInstance().DumpFormInfo(formRecord, formInfo);
731         reply = ERR_OK;
732     }
733 
734     std::vector<FormHostRecord> formHostRecords;
735     FormDataMgr::GetInstance().GetFormHostRecord(formId, formHostRecords);
736     for (const auto &iter : formHostRecords) {
737         FormDumpMgr::GetInstance().DumpFormHostInfo(iter, formInfo);
738         reply = ERR_OK;
739     }
740 
741     return reply;
742 }
743 /**
744  * @brief Dump form timer by form id.
745  * @param formId The id of the form.
746  * @param isTimingService "true" or "false".
747  * @return Returns ERR_OK on success, others on failure.
748  */
DumpFormTimerByFormId(const std::int64_t formId,std::string & isTimingService) const749 int FormMgrAdapter::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService) const
750 {
751     HILOG_INFO("%{public}s called.", __func__);
752     FormTimer formTimer;
753     UpdateAtItem updateAtItem;
754     DynamicRefreshItem dynamicItem;
755     bool resultInter = FormTimerMgr::GetInstance().GetIntervalTimer(formId, formTimer);
756     bool resultUpdate = FormTimerMgr::GetInstance().GetUpdateAtTimer(formId, updateAtItem);
757     bool resultDynamic = FormTimerMgr::GetInstance().GetDynamicItem(formId, dynamicItem);
758     HILOG_INFO("%{public}s resultInter:%{public}d,resultUpdate:%{public}d,resultDynamic:%{public}d",
759         __func__, resultInter, resultUpdate, resultDynamic);
760     if (resultInter || resultUpdate || resultDynamic) {
761         isTimingService = "true";
762     } else {
763         isTimingService = "false";
764     }
765     return ERR_OK;
766 }
767 /**
768  * @brief Get form configure info.
769  * @param want The want of the request.
770  * @param formItemInfo Form configure info.
771  * @return Returns ERR_OK on success, others on failure.
772  */
GetFormConfigInfo(const Want & want,FormItemInfo & formConfigInfo)773 ErrCode FormMgrAdapter::GetFormConfigInfo(const Want &want, FormItemInfo &formConfigInfo)
774 {
775     HILOG_DEBUG("GetFormConfigInfo start.");
776     BundleInfo bundleInfo;
777     std::string packageName;
778     ErrCode errCode = GetBundleInfo(want, bundleInfo, packageName);
779     if (errCode != ERR_OK) {
780         HILOG_ERROR("Get bundle info failed");
781         return errCode;
782     }
783 
784     FormInfo formInfo;
785     errCode = GetFormInfo(want, formInfo);
786     if (errCode != ERR_OK) {
787         HILOG_ERROR("Get target form info failed");
788         return errCode;
789     }
790     if (!formInfo.IsValid()) {
791         HILOG_ERROR("The form info is invalid");
792         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
793     }
794 
795     errCode = GetFormItemInfo(want, bundleInfo, formInfo, formConfigInfo);
796     if (errCode != ERR_OK) {
797         HILOG_ERROR("Get form item info failed.");
798         return errCode;
799     }
800     formConfigInfo.SetPackageName(packageName);
801     formConfigInfo.SetDeviceId(want.GetElement().GetDeviceID());
802 
803     if (!formConfigInfo.IsValidItem()) {
804         HILOG_ERROR("%{public}s fail, input param itemInfo is invalid", __func__);
805         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
806     }
807 
808     HILOG_DEBUG("GetFormConfigInfo end.");
809     return ERR_OK;
810 }
811 /**
812  * @brief Allocate form by formId.
813  * @param info Form configure info.
814  * @param callerToken Caller ability token.
815  * @param wantParams WantParams of the request.
816  * @param formInfo Form info for form host.
817  * @return Returns ERR_OK on success, others on failure.
818  */
AllotFormById(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const WantParams & wantParams,FormJsInfo & formInfo)819 ErrCode FormMgrAdapter::AllotFormById(const FormItemInfo &info,
820     const sptr<IRemoteObject> &callerToken, const WantParams &wantParams, FormJsInfo &formInfo)
821 {
822     int64_t formId = FormDataMgr::GetInstance().PaddingUdidHash(info.GetFormId());
823     FormRecord record;
824     bool hasRecord = FormDataMgr::GetInstance().GetFormRecord(formId, record);
825     if (hasRecord && record.formTempFlag && !FormRenderMgr::GetInstance().IsRerenderForRenderServiceDied(formId)) {
826         HILOG_ERROR("%{public}s, addForm can not acquire temp form when select form id", __func__);
827         return ERR_APPEXECFWK_FORM_COMMON_CODE;
828     }
829 
830     // ark ts form can only exist with one form host
831     if (info.GetUiSyntax() == FormType::ETS &&
832         !FormDbCache::GetInstance().IsHostOwner(formId, IPCSkeleton::GetCallingUid())) {
833         HILOG_ERROR("the specified form id does not exist in caller. formId: %{public}s.",
834             std::to_string(formId).c_str());
835         return ERR_APPEXECFWK_FORM_CFG_NOT_MATCH_ID;
836     }
837 
838     // get current userId
839     int callingUid = IPCSkeleton::GetCallingUid();
840     int32_t currentUserId = GetCurrentUserId(callingUid);
841     if (hasRecord && (record.userId == currentUserId
842         || FormDataMgr::GetInstance().IsCallingUidValid(record.formUserUids))) {
843         if (!info.IsMatch(record)) {
844             HILOG_ERROR("%{public}s, formId and item info not match:%{public}" PRId64 "", __func__, formId);
845             return ERR_APPEXECFWK_FORM_CFG_NOT_MATCH_ID;
846         }
847         return AddExistFormRecord(info, callerToken, record, formId, wantParams, formInfo);
848     }
849 
850     // find in db but not in cache
851     FormRecord dbRecord;
852     ErrCode getDbRet = FormDbCache::GetInstance().GetDBRecord(formId, dbRecord);
853     if (getDbRet == ERR_OK && (dbRecord.userId == currentUserId
854         || FormDataMgr::GetInstance().IsCallingUidValid(dbRecord.formUserUids))) {
855         return AddNewFormRecord(info, formId, callerToken, wantParams, formInfo);
856     }
857 
858     HILOG_INFO("%{public}s, addForm no such form %{public}" PRId64 "", __func__, formId);
859 
860     // delete form data in provider
861     FormRecord delRecord;
862     delRecord.bundleName = info.GetProviderBundleName();
863     delRecord.abilityName = info.GetAbilityName();
864     FormProviderMgr::GetInstance().NotifyProviderFormDelete(formId, delRecord);
865 
866     return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
867 }
868 
AddExistFormRecord(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const FormRecord & record,const int64_t formId,const WantParams & wantParams,FormJsInfo & formInfo)869 ErrCode FormMgrAdapter::AddExistFormRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken,
870     const FormRecord &record, const int64_t formId, const WantParams &wantParams, FormJsInfo &formInfo)
871 {
872     HILOG_INFO("%{public}s call, formId:%{public}" PRId64 "", __func__, formId);
873     // allot form host record
874     int callingUid = IPCSkeleton::GetCallingUid();
875     bool isCreated = FormDataMgr::GetInstance().AllotFormHostRecord(info, callerToken, formId, callingUid);
876     if (!isCreated) {
877         HILOG_ERROR("%{public}s fail, AllotFormHostRecord failed when no matched formRecord", __func__);
878         return ERR_APPEXECFWK_FORM_COMMON_CODE;
879     }
880 
881     FormRecord newRecord(record);
882     std::string cacheData;
883     if (FormCacheMgr::GetInstance().GetData(formId, cacheData)) {
884         newRecord.formProviderInfo.SetFormDataString(cacheData);
885     }
886     FormRenderMgr::GetInstance().RenderForm(newRecord, wantParams, callerToken);
887     if (newRecord.needRefresh || !FormCacheMgr::GetInstance().IsExist(newRecord.formId)) {
888         newRecord.isInited = false;
889         FormDataMgr::GetInstance().SetFormCacheInited(formId, false);
890 
891         // acquire formInfo from provider
892         ErrCode errorCode = AcquireProviderFormInfoAsync(formId, info, wantParams);
893         if (errorCode != ERR_OK) {
894             HILOG_ERROR("%{public}s fail, AcquireProviderFormInfoAsync failed", __func__);
895             return errorCode;
896         }
897     }
898 
899     // Add new form user uid.
900     FormDataMgr::GetInstance().AddFormUserUid(formId, callingUid);
901     if (std::find(newRecord.formUserUids.begin(), newRecord.formUserUids.end(), callingUid) ==
902         newRecord.formUserUids.end()) {
903         newRecord.formUserUids.emplace_back(callingUid);
904     }
905 
906     // create form info for js
907     if (FormCacheMgr::GetInstance().GetData(formId, cacheData)) {
908         formInfo.formData = cacheData;
909     }
910     FormDataMgr::GetInstance().CreateFormJsInfo(formId, record, formInfo);
911 
912     // start update timer
913     ErrCode errorCode = AddFormTimer(newRecord);
914     if (errorCode != ERR_OK) {
915         return errorCode;
916     }
917 
918     if (!newRecord.formTempFlag) {
919         return FormDbCache::GetInstance().UpdateDBRecord(formId, newRecord);
920     }
921     return ERR_OK;
922 }
923 
924 /**
925  * @brief Allocate form by form configure info.
926  * @param info Form configure info.
927  * @param callerToken Caller ability token.
928  * @param wantParams WantParams of the request.
929  * @param formInfo Form info for form host.
930  * @return Returns ERR_OK on success, others on failure.
931  */
AllotFormByInfo(const FormItemInfo & info,const sptr<IRemoteObject> & callerToken,const WantParams & wantParams,FormJsInfo & formInfo)932 ErrCode FormMgrAdapter::AllotFormByInfo(const FormItemInfo &info,
933     const sptr<IRemoteObject> &callerToken, const WantParams &wantParams, FormJsInfo &formInfo)
934 {
935     // generate formId
936     int64_t newFormId = FormDataMgr::GetInstance().GenerateFormId();
937     if (newFormId < 0) {
938         HILOG_ERROR("%{public}s fail, generateFormId no invalid formId", __func__);
939         return ERR_APPEXECFWK_FORM_COMMON_CODE;
940     }
941     HILOG_DEBUG("newFormId:%{public}" PRId64 "", newFormId);
942     return AddNewFormRecord(info, newFormId, callerToken, wantParams, formInfo);
943 }
944 
945 /**
946  * @brief Add new form record.
947  * @param info Form configure info.
948  * @param formId The form id.
949  * @param callerToken Caller ability token.
950  * @param wantParams WantParams of the request.
951  * @param formInfo Form info for form host.
952  * @return Returns ERR_OK on success, others on failure.
953  */
AddNewFormRecord(const FormItemInfo & info,const int64_t formId,const sptr<IRemoteObject> & callerToken,const WantParams & wantParams,FormJsInfo & formInfo)954 ErrCode FormMgrAdapter::AddNewFormRecord(const FormItemInfo &info, const int64_t formId,
955     const sptr<IRemoteObject> &callerToken, const WantParams &wantParams, FormJsInfo &formInfo)
956 {
957     HILOG_INFO("%{public}s start", __func__);
958     FormItemInfo newInfo(info);
959     newInfo.SetFormId(formId);
960     // allot form host record
961     int callingUid = IPCSkeleton::GetCallingUid();
962     if (!FormDataMgr::GetInstance().AllotFormHostRecord(newInfo, callerToken, formId, callingUid)) {
963         HILOG_ERROR("%{public}s fail, AllotFormHostRecord failed when no matched formRecord", __func__);
964         return ERR_APPEXECFWK_FORM_COMMON_CODE;
965     }
966 
967     // get current userId
968     int32_t currentUserId = GetCurrentUserId(callingUid);
969     // allot form record
970     FormRecord formRecord = FormDataMgr::GetInstance().AllotFormRecord(newInfo, callingUid, currentUserId);
971 
972     FormRenderMgr::GetInstance().RenderForm(formRecord, wantParams, callerToken);
973 
974     // acquire formInfo from provider
975     ErrCode errorCode = AcquireProviderFormInfoAsync(formId, newInfo, wantParams);
976     if (errorCode != ERR_OK) {
977         HILOG_ERROR("%{public}s fail, AcquireProviderFormInfoAsync failed", __func__);
978         return errorCode;
979     }
980 
981     // create form info for js
982     FormDataMgr::GetInstance().CreateFormJsInfo(formId, formRecord, formInfo);
983 
984     // storage info
985     if (!newInfo.IsTemporaryForm()) {
986         errorCode = FormDbCache::GetInstance().UpdateDBRecord(formId, formRecord);
987         if (errorCode != ERR_OK) {
988             HILOG_ERROR("%{public}s fail, UpdateDBRecord failed", __func__);
989             return errorCode;
990         }
991     }
992 
993     // start update timer
994     if (info.GetProviderBundleName() != info.GetHostBundleName()) {
995         return AddFormTimer(formRecord);
996     }
997 
998     return ERR_OK;
999 }
1000 
1001 /**
1002  * @brief Add form timer.
1003  * @param formRecord Form information.
1004  * @return Returns ERR_OK on success, others on failure.
1005  */
AddFormTimer(const FormRecord & formRecord)1006 ErrCode FormMgrAdapter::AddFormTimer(const FormRecord &formRecord)
1007 {
1008     HILOG_INFO("%{public}s start", __func__);
1009     if (!formRecord.isEnableUpdate || formRecord.formTempFlag) {
1010         HILOG_INFO("%{public}s isEnableUpdate:%{public}d formTempFlag:%{public}d.",
1011             __func__, formRecord.isEnableUpdate, formRecord.formTempFlag);
1012         return ERR_OK;
1013     }
1014     if (formRecord.updateDuration > 0) {
1015         bool ret = FormTimerMgr::GetInstance().AddFormTimer(formRecord.formId,
1016             formRecord.updateDuration, formRecord.userId);
1017         return ret ? ERR_OK : ERR_APPEXECFWK_FORM_COMMON_CODE;
1018     }
1019     if (formRecord.updateAtHour >= 0 && formRecord.updateAtMin >= 0) {
1020         bool ret = FormTimerMgr::GetInstance().AddFormTimer(formRecord.formId,
1021             formRecord.updateAtHour, formRecord.updateAtMin, formRecord.userId);
1022         return ret ? ERR_OK : ERR_APPEXECFWK_FORM_COMMON_CODE;
1023     }
1024     HILOG_INFO("%{public}s no need add form timer.", __func__);
1025     return ERR_OK;
1026 }
1027 
1028 /**
1029  * @brief Send event notify to form provider. The event notify type include FORM_VISIBLE and FORM_INVISIBLE.
1030  *
1031  * @param providerKey The provider key string which consists of the provider bundle name and ability name.
1032  * @param formIdsByProvider The vector of form Ids which have the same provider.
1033  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
1034  * @return Returns ERR_OK on success, others on failure.
1035  */
HandleEventNotify(const std::string & providerKey,const std::vector<int64_t> & formIdsByProvider,const int32_t formVisibleType)1036 ErrCode FormMgrAdapter::HandleEventNotify(const std::string &providerKey, const std::vector<int64_t> &formIdsByProvider,
1037     const int32_t formVisibleType)
1038 {
1039     HILOG_INFO("%{public}s called.", __func__);
1040     size_t position = providerKey.find(Constants::NAME_DELIMITER);
1041     std::string bundleName = providerKey.substr(0, position);
1042     std::string abilityName = providerKey.substr(position + strlen(Constants::NAME_DELIMITER));
1043     sptr<IAbilityConnection> formEventNotifyConnection = new (std::nothrow) FormEventNotifyConnection(formIdsByProvider,
1044         formVisibleType, bundleName, abilityName);
1045     if (formEventNotifyConnection == nullptr) {
1046         HILOG_ERROR("failed to create FormEventNotifyConnection.");
1047         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1048     }
1049     Want connectWant;
1050     connectWant.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
1051     connectWant.SetElementName(bundleName, abilityName);
1052     ErrCode errorCode = FormAmsHelper::GetInstance().ConnectServiceAbility(connectWant, formEventNotifyConnection);
1053     if (errorCode != ERR_OK) {
1054         HILOG_ERROR("%{public}s fail, ConnectServiceAbility failed.", __func__);
1055         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
1056     }
1057 
1058     return ERR_OK;
1059 }
1060 
1061 /**
1062  * @brief Acquire form data from form provider.
1063  * @param formId The Id of the form.
1064  * @param info Form configure info.
1065  * @param wantParams WantParams of the request.
1066  * @return Returns ERR_OK on success, others on failure.
1067  */
AcquireProviderFormInfoAsync(const int64_t formId,const FormItemInfo & info,const WantParams & wantParams)1068 ErrCode FormMgrAdapter::AcquireProviderFormInfoAsync(const int64_t formId,
1069     const FormItemInfo &info, const WantParams &wantParams)
1070 {
1071     if (formId <= 0) {
1072         HILOG_ERROR("%{public}s fail, formId should be greater than 0", __func__);
1073         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1074     }
1075 
1076     Want newWant;
1077     newWant.SetParams(wantParams);
1078     auto hostToken = newWant.GetRemoteObject(Constants::PARAM_FORM_HOST_TOKEN);
1079     sptr<IAbilityConnection> formAcquireConnection = new (std::nothrow) FormAcquireConnection(formId, info, wantParams,
1080         hostToken);
1081     if (formAcquireConnection == nullptr) {
1082         HILOG_ERROR("formAcquireConnection is null.");
1083         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
1084     }
1085     Want want;
1086     want.SetElementName(info.GetProviderBundleName(), info.GetAbilityName());
1087     want.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
1088     ErrCode errorCode = FormAmsHelper::GetInstance().ConnectServiceAbility(want, formAcquireConnection);
1089     if (errorCode != ERR_OK) {
1090         HILOG_ERROR("%{public}s fail, ConnectServiceAbility failed.", __func__);
1091         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
1092     }
1093     return ERR_OK;
1094 }
1095 
1096 /**
1097  * @brief Get bundle info.
1098  * @param want The want of the request.
1099  * @param bundleInfo Bundle info.
1100  * @param packageName Package name.
1101  * @return Returns ERR_OK on success, others on failure.
1102  */
GetBundleInfo(const AAFwk::Want & want,BundleInfo & bundleInfo,std::string & packageName)1103 ErrCode FormMgrAdapter::GetBundleInfo(const AAFwk::Want &want, BundleInfo &bundleInfo, std::string &packageName)
1104 {
1105     HILOG_DEBUG("GetBundleInfo start.");
1106     std::string bundleName = want.GetElement().GetBundleName();
1107     std::string abilityName = want.GetElement().GetAbilityName();
1108     std::string deviceId = want.GetElement().GetDeviceID();
1109     std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1110     if (bundleName.empty() || abilityName.empty() || moduleName.empty()) {
1111         HILOG_ERROR("GetBundleInfo bundleName or abilityName or moduleName is invalid");
1112         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1113     }
1114 
1115     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
1116     if (iBundleMgr == nullptr) {
1117         HILOG_ERROR("GetBundleMgr, failed to get IBundleMgr.");
1118         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
1119     }
1120 
1121     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES,
1122         bundleInfo, FormUtil::GetCurrentAccountId()))) {
1123         HILOG_ERROR("GetBundleInfo, failed to get bundle info.");
1124         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
1125     }
1126 
1127     bool moduleExist = false;
1128     for (const auto &moduleInfo : bundleInfo.moduleNames) {
1129         HILOG_DEBUG("bundleInfo.moduleNames, module name:%{public}s", moduleInfo.c_str());
1130         if (moduleInfo.compare(moduleName) == 0) {
1131             moduleExist = true;
1132             break;
1133         }
1134     }
1135     if (!moduleExist) {
1136         HILOG_ERROR("GetBundleInfo no such module, name:%{public}s", moduleName.c_str());
1137         return ERR_APPEXECFWK_FORM_NO_SUCH_MODULE;
1138     }
1139 
1140     packageName = bundleName + moduleName;
1141     HILOG_DEBUG("GetBundleInfo end.");
1142     return ERR_OK;
1143 }
1144 /**
1145  * @brief Get form info.
1146  * @param want The want of the request.
1147  * @param formInfo Form info.
1148  * @return Returns ERR_OK on success, others on failure.
1149  */
GetFormInfo(const AAFwk::Want & want,FormInfo & formInfo)1150 ErrCode FormMgrAdapter::GetFormInfo(const AAFwk::Want &want, FormInfo &formInfo)
1151 {
1152     HILOG_DEBUG("GetFormInfo start.");
1153     std::string bundleName = want.GetElement().GetBundleName();
1154     std::string abilityName = want.GetElement().GetAbilityName();
1155     std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1156     if (bundleName.empty() || abilityName.empty() || moduleName.empty()) {
1157         HILOG_ERROR("bundleName or abilityName or moduleName is invalid");
1158         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1159     }
1160 
1161     std::vector<FormInfo> formInfos {};
1162     ErrCode errCode = FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos);
1163     if (errCode != ERR_OK) {
1164         HILOG_ERROR("GetFormsInfoByModule, failed to get form config info.");
1165         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
1166     }
1167 
1168     std::string formName = want.GetStringParam(Constants::PARAM_FORM_NAME_KEY);
1169     if (formName.empty()) {
1170         for (const auto &form : formInfos) {
1171             if (form.defaultFlag && form.abilityName == abilityName) {
1172                 formInfo = form;
1173                 formInfo.moduleName = moduleName;
1174                 HILOG_DEBUG("GetFormInfo end.");
1175                 return ERR_OK;
1176             }
1177         }
1178     } else  {
1179         for (const auto &form : formInfos) {
1180             if (form.name == formName && form.abilityName == abilityName) {
1181                 formInfo = form;
1182                 formInfo.moduleName = moduleName;
1183                 HILOG_DEBUG("GetFormInfo end.");
1184                 return ERR_OK;
1185             }
1186         }
1187     }
1188     HILOG_ERROR("failed to get form info failed. ability name is %{public}s, form name is %{public}s",
1189         abilityName.c_str(), formName.c_str());
1190     return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
1191 }
1192 /**
1193  * @brief Get form configure info.
1194  * @param want The want of the request.
1195  * @param bundleInfo Bundle info.
1196  * @param formInfo Form info.
1197  * @param formItemInfo Form configure info.
1198  * @return Returns ERR_OK on success, others on failure.
1199  */
GetFormItemInfo(const AAFwk::Want & want,const BundleInfo & bundleInfo,const FormInfo & formInfo,FormItemInfo & formItemInfo)1200 ErrCode FormMgrAdapter::GetFormItemInfo(const AAFwk::Want &want, const BundleInfo &bundleInfo,
1201     const FormInfo &formInfo, FormItemInfo &formItemInfo)
1202 {
1203     HILOG_DEBUG("GetFormItemInfo start.");
1204     int32_t dimensionId = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, formInfo.defaultDimension);
1205     if (!IsDimensionValid(formInfo, dimensionId)) {
1206         HILOG_ERROR("GetFormItemInfo failed, dimension is not valid.");
1207         return ERR_APPEXECFWK_FORM_NO_SUCH_DIMENSION;
1208     }
1209 
1210     ErrCode ret = CreateFormItemInfo(bundleInfo, formInfo, formItemInfo, want);
1211     if (ret != ERR_OK) {
1212         HILOG_ERROR("GetFormItemInfo failed, CreateFormItemInfo failed.");
1213         return ret;
1214     }
1215     formItemInfo.SetSpecificationId(dimensionId);
1216     formItemInfo.SetTemporaryFlag(want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false));
1217     HILOG_DEBUG("GetFormItemInfo end.");
1218     return ERR_OK;
1219 }
1220 /**
1221  * @brief Dimension valid check.
1222  * @param formInfo Form info.
1223  * @param dimensionId Dimension id.
1224  * @return Returns true on success, false on failure.
1225  */
IsDimensionValid(const FormInfo & formInfo,int dimensionId) const1226 bool FormMgrAdapter::IsDimensionValid(const FormInfo &formInfo, int dimensionId) const
1227 {
1228     if (formInfo.supportDimensions.empty()) {
1229         HILOG_ERROR("Js form, no support dimension.");
1230         return false;
1231     }
1232 
1233     for (size_t i = 0; i < formInfo.supportDimensions.size() && i < Constants::MAX_LAYOUT; i++) {
1234         if (formInfo.supportDimensions[i] == dimensionId) {
1235             return true;
1236         }
1237     }
1238 
1239     HILOG_ERROR("No matched dimension found for %{public}d.", dimensionId);
1240     return false;
1241 }
1242 /**
1243  * @brief Create form configure info.
1244  * @param bundleInfo Bundle info.
1245  * @param formInfo Form info.
1246  * @param itemInfo Form configure info.
1247  * @return Returns ERR_OK on success, others on failure.
1248  */
CreateFormItemInfo(const BundleInfo & bundleInfo,const FormInfo & formInfo,FormItemInfo & itemInfo,const AAFwk::Want & want)1249 ErrCode FormMgrAdapter::CreateFormItemInfo(const BundleInfo &bundleInfo,
1250     const FormInfo &formInfo, FormItemInfo &itemInfo, const AAFwk::Want &want)
1251 {
1252     itemInfo.SetProviderBundleName(bundleInfo.name);
1253     itemInfo.SetVersionCode(bundleInfo.versionCode);
1254     itemInfo.SetVersionName(bundleInfo.versionName);
1255     itemInfo.SetCompatibleVersion(bundleInfo.compatibleVersion);
1256 
1257     std::string hostBundleName;
1258     bool isSaUid = IPCSkeleton::GetCallingUid() == SYSTEM_UID;
1259     ErrCode ret = ERR_APPEXECFWK_FORM_COMMON_CODE;
1260     if (isSaUid) {
1261         hostBundleName = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_HOST_BUNDLENAME_KEY);
1262         HILOG_INFO("sa uid call CreateFormItemInfo, hostBundleName is %{public}s", hostBundleName.c_str());
1263         ret = ERR_OK;
1264     } else {
1265         ret = FormBmsHelper::GetInstance().GetCallerBundleName(hostBundleName);
1266     }
1267     if (ret != ERR_OK) {
1268         HILOG_ERROR("GetFormsInfoByModule, failed to get form config info.");
1269         return ret;
1270     }
1271     itemInfo.SetHostBundleName(hostBundleName);
1272     itemInfo.SetAbilityName(formInfo.abilityName);
1273     itemInfo.SetModuleName(formInfo.moduleName); // formInfo.moduleName: bundleMgr do not set
1274     itemInfo.SetFormName(formInfo.name);
1275     itemInfo.SetEnableUpdateFlag(formInfo.updateEnabled);
1276     itemInfo.SetUpdateDuration(formInfo.updateDuration);
1277     itemInfo.SetScheduledUpdateTime(formInfo.scheduledUpdateTime);
1278     itemInfo.SetJsComponentName(formInfo.jsComponentName);
1279     itemInfo.SetFormVisibleNotify(formInfo.formVisibleNotify);
1280     auto formSrc = formInfo.src;
1281     if (formSrc.rfind(POINT_ETS) == formSrc.size() - POINT_ETS.size()) {
1282         formSrc.erase(formSrc.end() - POINT_ETS.size(), formSrc.end());
1283     }
1284     itemInfo.SetFormSrc(formSrc);
1285     itemInfo.SetFormWindow(formInfo.window);
1286     itemInfo.SetType(formInfo.type);
1287     itemInfo.SetUiSyntax(formInfo.uiSyntax);
1288 
1289     for (const auto &abilityInfo : bundleInfo.abilityInfos) {
1290         if (abilityInfo.name == formInfo.abilityName) {
1291             itemInfo.SetAbilityModuleName(abilityInfo.moduleName);
1292             if (!abilityInfo.isModuleJson) {
1293                 itemInfo.SetFormSrc("");
1294             }
1295         }
1296         auto hapPath = abilityInfo.hapPath;
1297         if (hapPath.find(Constants::ABS_CODE_PATH) != std::string::npos) {
1298             hapPath = std::regex_replace(hapPath, std::regex(Constants::ABS_CODE_PATH), Constants::LOCAL_BUNDLES);
1299         }
1300         itemInfo.AddModuleInfo(abilityInfo.moduleName, hapPath);
1301         HILOG_DEBUG("%{public}s hap path is %{public}s", abilityInfo.moduleName.c_str(), hapPath.c_str());
1302     }
1303 
1304     HILOG_INFO("%{public}s moduleInfos size: %{public}zu", __func__, bundleInfo.applicationInfo.moduleInfos.size());
1305     for (const auto &item : bundleInfo.applicationInfo.moduleInfos) {
1306         HILOG_INFO("%{public}s moduleInfos,  moduleName: %{public}s, moduleSourceDir: %{public}s", __func__,
1307             item.moduleName.c_str(), item.moduleSourceDir.c_str());
1308         if (formInfo.moduleName == item.moduleName) {
1309             itemInfo.AddHapSourceDirs(item.moduleSourceDir);
1310         }
1311     }
1312     return ERR_OK;
1313 }
1314 
1315 /**
1316  * @brief set next refresh time.
1317  * @param formId The id of the form.
1318  * @param nextTime next refresh time.
1319  * @return Returns ERR_OK on success, others on failure.
1320  */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)1321 int FormMgrAdapter::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
1322 {
1323     HILOG_INFO("%{public}s begin here, formId:%{public}" PRId64 ",nextTime:%{public}" PRId64 "",
1324         __func__, formId, nextTime);
1325     if (formId <= 0) {
1326         HILOG_ERROR("%{public}s formId is invalid", __func__);
1327         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1328     }
1329 
1330     std::string bundleName;
1331     auto ret = FormBmsHelper::GetInstance().GetCallerBundleName(bundleName);
1332     if (ret != ERR_OK) {
1333         HILOG_ERROR("%{public}s failed to get BundleName", __func__);
1334         return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
1335     }
1336     int32_t callingUid = IPCSkeleton::GetCallingUid();
1337     int32_t userId = GetCurrentUserId(callingUid);
1338     HILOG_INFO("%{public}s, userId:%{public}d, callingUid:%{public}d.", __func__, userId, callingUid);
1339 
1340     // get IBundleMgr
1341     int32_t bundleUid = FormBmsHelper::GetInstance().GetUidByBundleName(bundleName, userId);
1342     if (bundleUid != callingUid) {
1343         HILOG_ERROR("%{public}s error, permission denied, the form is not your own.", __func__);
1344         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1345     }
1346 
1347     FormRecord formRecord;
1348     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1349     if (!FormDataMgr::GetInstance().GetFormRecord(matchedFormId, formRecord)) {
1350         HILOG_ERROR("%{public}s, not found in form record.", __func__);
1351         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1352     }
1353 
1354     bool isSelfFormId = (userId == formRecord.userId) || checkFormHostHasSaUid(formRecord);
1355     if (!isSelfFormId) {
1356         HILOG_ERROR("%{public}s, not self form:%{public}" PRId64 "", __func__, formId);
1357         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1358     }
1359 
1360     // check bundleName
1361     if (bundleName != formRecord.bundleName) {
1362         HILOG_ERROR("%{public}s, not match bundleName:%{public}s", __func__, bundleName.c_str());
1363         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1364     }
1365 
1366     return SetNextRefreshTimeLocked(matchedFormId, nextTime, userId);
1367 }
1368 
CheckPublishForm(Want & want)1369 ErrCode FormMgrAdapter::CheckPublishForm(Want &want)
1370 {
1371     std::string bundleName;
1372     if (!GetBundleName(bundleName)) {
1373         HILOG_ERROR("%{public}s failed to get BundleName", __func__);
1374         return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
1375     }
1376     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
1377     if (iBundleMgr == nullptr) {
1378         HILOG_ERROR("%{public}s fail, failed to get IBundleMgr.", __func__);
1379         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
1380     }
1381     if (!CheckIsSystemAppByBundleName(iBundleMgr, bundleName)) {
1382         HILOG_ERROR("Only system app can request publish form.");
1383         return ERR_APPEXECFWK_FORM_PERMISSION_DENY;
1384     }
1385 
1386     if (bundleName != want.GetElement().GetBundleName()) {
1387         HILOG_WARN("The bundleName in want does not match the bundleName of current calling user.");
1388         want.SetBundle(bundleName);
1389     }
1390 
1391     std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1392     if (moduleName.empty()) {
1393         HILOG_ERROR("%{public}s error, moduleName is empty.", __func__);
1394         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1395     }
1396 
1397     bool isTemporary = want.GetBoolParam(AppExecFwk::Constants::PARAM_FORM_TEMPORARY_KEY, false);
1398     if (isTemporary) {
1399         HILOG_WARN("The published form should not be temp.");
1400         want.SetParam(AppExecFwk::Constants::PARAM_FORM_TEMPORARY_KEY, false);
1401     }
1402 
1403     std::string abilityName = want.GetElement().GetAbilityName();
1404     std::string formName = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_NAME_KEY);
1405     std::vector<FormInfo> formInfos {};
1406     ErrCode errCode = FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos);
1407     if (errCode != ERR_OK) {
1408         HILOG_ERROR("%{public}s error, failed to get forms info.", __func__);
1409         return errCode;
1410     }
1411     for (auto &formInfo: formInfos) {
1412         int32_t dimensionId = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 0);
1413         if ((formInfo.abilityName == abilityName) && (formInfo.name == formName) &&
1414             (IsDimensionValid(formInfo, dimensionId))) {
1415             want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, dimensionId);
1416             return ERR_OK;
1417         }
1418     }
1419     HILOG_ERROR("failed to find match form info.");
1420     return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1421 }
1422 
QueryPublishFormToHost(Want & want)1423 ErrCode FormMgrAdapter::QueryPublishFormToHost(Want &want)
1424 {
1425     /* Query the highest priority ability or extension ability for publishing form */
1426     AppExecFwk::AbilityInfo abilityInfo;
1427     AppExecFwk::ExtensionAbilityInfo extensionAbilityInfo;
1428     int callingUid = IPCSkeleton::GetCallingUid();
1429     int32_t userId = GetCurrentUserId(callingUid);
1430     if (!FormBmsHelper::GetInstance().GetAbilityInfoByAction(
1431         Constants::FORM_PUBLISH_ACTION, userId, abilityInfo, extensionAbilityInfo)) {
1432         HILOG_ERROR("Failed to ImplicitQueryInfoByPriority for publishing form");
1433         return ERR_APPEXECFWK_FORM_GET_HOST_FAILED;
1434     }
1435 
1436     if (abilityInfo.name.empty() && extensionAbilityInfo.name.empty()) {
1437         HILOG_ERROR("Query highest priority ability failed, no form host ability found.");
1438         return ERR_APPEXECFWK_FORM_GET_HOST_FAILED;
1439     }
1440 
1441     if (!abilityInfo.name.empty()) {
1442         /* highest priority ability */
1443         HILOG_INFO("Query highest priority ability success. bundleName: %{public}s, ability:%{public}s",
1444             abilityInfo.bundleName.c_str(), abilityInfo.name.c_str());
1445         want.SetParam(Constants::PARAM_BUNDLE_NAME_KEY, abilityInfo.bundleName);
1446         want.SetParam(Constants::PARAM_ABILITY_NAME_KEY, abilityInfo.name);
1447     } else {
1448         /* highest priority extension ability */
1449         HILOG_INFO("Query highest priority extension ability success. bundleName: %{public}s, ability:%{public}s",
1450             extensionAbilityInfo.bundleName.c_str(), extensionAbilityInfo.name.c_str());
1451         want.SetParam(Constants::PARAM_BUNDLE_NAME_KEY, extensionAbilityInfo.bundleName);
1452         want.SetParam(Constants::PARAM_ABILITY_NAME_KEY, extensionAbilityInfo.name);
1453     }
1454     want.SetParam(Constants::PARAM_FORM_USER_ID, userId);
1455     want.SetAction(Constants::FORM_PUBLISH_ACTION);
1456     return ERR_OK;
1457 }
1458 
RequestPublishFormToHost(Want & want)1459 ErrCode FormMgrAdapter::RequestPublishFormToHost(Want &want)
1460 {
1461     Want wantToHost;
1462     ElementName elementName = want.GetElement();
1463     wantToHost.SetParam(Constants::PARAM_BUNDLE_NAME_KEY, elementName.GetBundleName());
1464     wantToHost.SetParam(Constants::PARAM_ABILITY_NAME_KEY, elementName.GetAbilityName());
1465     std::string strFormId = want.GetStringParam(Constants::PARAM_FORM_IDENTITY_KEY);
1466     wantToHost.SetParam(Constants::PARAM_FORM_IDENTITY_KEY, strFormId);
1467     std::string formName = want.GetStringParam(Constants::PARAM_FORM_NAME_KEY);
1468     wantToHost.SetParam(Constants::PARAM_FORM_NAME_KEY, formName);
1469     std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1470     wantToHost.SetParam(Constants::PARAM_MODULE_NAME_KEY, moduleName);
1471     int32_t dimensionId = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 0);
1472     wantToHost.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, dimensionId);
1473     bool tempFormFlag = want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false);
1474     wantToHost.SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, tempFormFlag);
1475     std::string bundleName = want.GetStringParam(Constants::PARAM_BUNDLE_NAME_KEY);
1476     std::string abilityName = want.GetStringParam(Constants::PARAM_ABILITY_NAME_KEY);
1477     wantToHost.SetElementName(bundleName, abilityName);
1478     int32_t userId = want.GetIntParam(Constants::PARAM_FORM_USER_ID, -1);
1479     wantToHost.SetAction(Constants::FORM_PUBLISH_ACTION);
1480 
1481     return FormAmsHelper::GetInstance().StartAbility(wantToHost, userId);
1482 }
1483 
RequestPublishForm(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId)1484 ErrCode FormMgrAdapter::RequestPublishForm(Want &want, bool withFormBindingData,
1485     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId)
1486 {
1487     HILOG_INFO("%{public}s called.", __func__);
1488     ErrCode errCode = CheckPublishForm(want);
1489     if (errCode != ERR_OK) {
1490         return errCode;
1491     }
1492 
1493     errCode = QueryPublishFormToHost(want);
1494     if (errCode != ERR_OK) {
1495         return errCode;
1496     }
1497 
1498     // generate formId
1499     formId = FormDataMgr::GetInstance().GenerateFormId();
1500     if (formId < 0) {
1501         HILOG_ERROR("%{public}s fail, generateFormId no invalid formId", __func__);
1502         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1503     }
1504     HILOG_DEBUG("formId:%{public}" PRId64 "", formId);
1505     std::string strFormId = std::to_string(formId);
1506     want.SetParam(Constants::PARAM_FORM_IDENTITY_KEY, strFormId);
1507 
1508     if (withFormBindingData) {
1509         errCode = FormDataMgr::GetInstance().AddRequestPublishFormInfo(formId, want, formBindingData);
1510     } else {
1511         std::unique_ptr<FormProviderData> noFormBindingData = nullptr;
1512         errCode = FormDataMgr::GetInstance().AddRequestPublishFormInfo(formId, want, noFormBindingData);
1513     }
1514     if (errCode != ERR_OK) {
1515         return errCode;
1516     }
1517 
1518     errCode = RequestPublishFormToHost(want);
1519     if (errCode != ERR_OK) {
1520         FormDataMgr::GetInstance().RemoveRequestPublishFormInfo(formId);
1521     }
1522     return errCode;
1523 }
1524 
CheckAddRequestPublishForm(const Want & want,const Want & formProviderWant)1525 ErrCode FormMgrAdapter::CheckAddRequestPublishForm(const Want &want, const Want &formProviderWant)
1526 {
1527     std::string bundleName = want.GetElement().GetBundleName();
1528     std::string bundleNameProvider = formProviderWant.GetElement().GetBundleName();
1529     if (bundleNameProvider != bundleName) {
1530         HILOG_ERROR("The bundleName is not match.");
1531         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1532     }
1533 
1534     std::string moduleName = want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1535     std::string moduleNameProvider = formProviderWant.GetStringParam(Constants::PARAM_MODULE_NAME_KEY);
1536     if (moduleNameProvider != moduleName) {
1537         HILOG_ERROR("The moduleName is not match.");
1538         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1539     }
1540 
1541     std::string abilityName = want.GetElement().GetAbilityName();
1542     std::string abilityNameProvider = formProviderWant.GetElement().GetAbilityName();
1543     if (abilityNameProvider != abilityName) {
1544         HILOG_ERROR("The abilityName is not match.");
1545         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1546     }
1547 
1548     std::string formName = want.GetStringParam(Constants::PARAM_FORM_NAME_KEY);
1549     std::string formNameProvider = formProviderWant.GetStringParam(Constants::PARAM_FORM_NAME_KEY);
1550     if (formNameProvider != formName) {
1551         HILOG_ERROR("The formName is not match.");
1552         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1553     }
1554 
1555     int32_t dimensionId = want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 0);
1556     int32_t dimensionIdProvider = formProviderWant.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, 0);
1557     if (dimensionIdProvider != dimensionId) {
1558         HILOG_ERROR("The dimensionId is not match.");
1559         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1560     }
1561 
1562     bool isTemporary = want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false);
1563     bool isTemporaryProvider = formProviderWant.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false);
1564     if (isTemporaryProvider != isTemporary) {
1565         HILOG_ERROR("The temporary is not match.");
1566         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1567     }
1568 
1569     int32_t callingUid = IPCSkeleton::GetCallingUid();
1570     ErrCode errCode = ERR_OK;
1571     if (isTemporary) {
1572         errCode = FormDataMgr::GetInstance().CheckTempEnoughForm();
1573     } else {
1574         int32_t currentUserId = GetCurrentUserId(callingUid);
1575         errCode = FormDataMgr::GetInstance().CheckEnoughForm(callingUid, currentUserId);
1576     }
1577     if (errCode != ERR_OK) {
1578         HILOG_ERROR("%{public}s fail, too much forms in system", __func__);
1579         return errCode;
1580     }
1581     return ERR_OK;
1582 }
1583 
AddRequestPublishForm(const FormItemInfo & formItemInfo,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formJsInfo)1584 ErrCode FormMgrAdapter::AddRequestPublishForm(const FormItemInfo &formItemInfo, const Want &want,
1585     const sptr<IRemoteObject> &callerToken, FormJsInfo &formJsInfo)
1586 {
1587     HILOG_INFO("Add request publish form.");
1588     Want formProviderWant;
1589     std::unique_ptr<FormProviderData> formProviderData = nullptr;
1590     auto formId = formItemInfo.GetFormId();
1591     ErrCode errCode = FormDataMgr::GetInstance().GetRequestPublishFormInfo(formId, formProviderWant, formProviderData);
1592     if (errCode != ERR_OK) {
1593         HILOG_ERROR("Failed to get request publish form");
1594         return errCode;
1595     }
1596 
1597     errCode = CheckAddRequestPublishForm(want, formProviderWant);
1598     if (errCode != ERR_OK) {
1599         return errCode;
1600     }
1601 
1602     int32_t callingUid = IPCSkeleton::GetCallingUid();
1603     if (!FormDataMgr::GetInstance().AllotFormHostRecord(formItemInfo, callerToken, formId, callingUid)) {
1604         HILOG_ERROR("%{public}s fail, AllotFormHostRecord failed when no matched formRecord", __func__);
1605         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1606     }
1607 
1608     // get current userId
1609     int32_t currentUserId = GetCurrentUserId(callingUid);
1610     // allot form record
1611     FormRecord formRecord = FormDataMgr::GetInstance().AllotFormRecord(formItemInfo, callingUid, currentUserId);
1612     if (formProviderData != nullptr) {
1613         formRecord.formProviderInfo.SetFormData(*formProviderData);
1614     }
1615     FormRenderMgr::GetInstance().RenderForm(formRecord, want.GetParams()); // render for arkTs form
1616 
1617     // create form info for js
1618     FormDataMgr::GetInstance().CreateFormJsInfo(formId, formRecord, formJsInfo);
1619     if (formProviderData != nullptr) {
1620         formJsInfo.formData = formProviderData->GetDataString();
1621         formJsInfo.formProviderData = *formProviderData;
1622         if (formProviderData->NeedCache()) {
1623             HILOG_INFO("%{public}s, data is less than 1k, cache data.", __func__);
1624             FormCacheMgr::GetInstance().AddData(formId, formJsInfo.formData);
1625         }
1626     }
1627     // storage info
1628     if (!formItemInfo.IsTemporaryForm()) {
1629         if (ErrCode errorCode = FormDbCache::GetInstance().UpdateDBRecord(formId, formRecord);
1630             errorCode != ERR_OK) {
1631             HILOG_ERROR("%{public}s fail, UpdateDBRecord failed", __func__);
1632             return errorCode;
1633         }
1634     }
1635 
1636     // start update timer
1637     return AddFormTimer(formRecord);
1638 }
1639 
1640 /**
1641  * @brief get bundleName.
1642  * @param bundleName for output.
1643  * @return Returns true on success, others on failure.
1644  */
GetBundleName(std::string & bundleName)1645 bool FormMgrAdapter::GetBundleName(std::string &bundleName)
1646 {
1647     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
1648     if (iBundleMgr == nullptr) {
1649         HILOG_ERROR("%{public}s, failed to get IBundleMgr.", __func__);
1650         return false;
1651     }
1652 
1653     int uid = IPCSkeleton::GetCallingUid();
1654     if (!IN_PROCESS_CALL(iBundleMgr->CheckIsSystemAppByUid(uid))) {
1655         HILOG_ERROR("%{public}s fail, form is not system app. uid:%{public}d", __func__, uid);
1656         return false;
1657     }
1658 
1659     bool result = IN_PROCESS_CALL(iBundleMgr->GetBundleNameForUid(uid, bundleName));
1660     if (!result || bundleName.empty()) {
1661         HILOG_ERROR("%{public}s failed, cannot get bundle name by uid:%{public}d", __func__, uid);
1662         return false;
1663     }
1664     return true;
1665 }
1666 
1667 /**
1668  * @brief set next refresh time locked.
1669  * @param formId The form's id.
1670  * @param nextTime next refresh time.
1671  * @param userId User ID.
1672  * @return Returns ERR_OK on success, others on failure.
1673  */
SetNextRefreshTimeLocked(const int64_t formId,const int64_t nextTime,const int32_t userId)1674 int FormMgrAdapter::SetNextRefreshTimeLocked(const int64_t formId, const int64_t nextTime, const int32_t userId)
1675 {
1676     HILOG_ERROR("SetNextRefreshTimeLocked.");
1677     int32_t timerRefreshedCount = FormTimerMgr::GetInstance().GetRefreshCount(formId);
1678     if (timerRefreshedCount >= Constants::LIMIT_COUNT) {
1679         HILOG_ERROR("%{public}s, already refresh times:%{public}d", __func__, timerRefreshedCount);
1680         FormTimerMgr::GetInstance().MarkRemind(formId);
1681         return ERR_APPEXECFWK_FORM_MAX_REFRESH;
1682     }
1683 
1684     if (!FormTimerMgr::GetInstance().SetNextRefreshTime(formId, nextTime * Constants::SEC_PER_MIN, userId)) {
1685         HILOG_ERROR("%{public}s failed", __func__);
1686         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1687     }
1688 
1689     return ERR_OK;
1690 }
1691 
1692 /**
1693  * @brief set next refresh time locked.
1694  * @param formId The form's id.
1695  * @param bundleName Provider ability bundleName.
1696  * @return Returns true or false.
1697  */
IsUpdateValid(const int64_t formId,const std::string & bundleName)1698 bool FormMgrAdapter::IsUpdateValid(const int64_t formId, const std::string &bundleName)
1699 {
1700     if (formId <= 0 || bundleName.empty()) {
1701         return false;
1702     }
1703     return true;
1704 }
1705 
1706 /**
1707  * @brief enable update form.
1708  * @param formIDs The id of the forms.
1709  * @param callerToken Caller ability token.
1710  * @return Returns ERR_OK on success, others on failure.
1711  */
EnableUpdateForm(const std::vector<int64_t> formIDs,const sptr<IRemoteObject> & callerToken)1712 int FormMgrAdapter::EnableUpdateForm(const std::vector<int64_t> formIDs, const sptr<IRemoteObject> &callerToken)
1713 {
1714     HILOG_INFO("enableUpdateForm");
1715     return HandleUpdateFormFlag(formIDs, callerToken, true, false);
1716 }
1717 
1718 /**
1719  * @brief disable update form.
1720  * @param formIDs The id of the forms.
1721  * @param callerToken Caller ability token.
1722  * @return Returns ERR_OK on success, others on failure.
1723  */
DisableUpdateForm(const std::vector<int64_t> formIDs,const sptr<IRemoteObject> & callerToken)1724 int FormMgrAdapter::DisableUpdateForm(const std::vector<int64_t> formIDs, const sptr<IRemoteObject> &callerToken)
1725 {
1726     HILOG_INFO("disableUpdateForm");
1727     return HandleUpdateFormFlag(formIDs, callerToken, false, false);
1728 }
1729 
1730 /**
1731  * @brief Process js message event.
1732  * @param formId Indicates the unique id of form.
1733  * @param want information passed to supplier.
1734  * @param callerToken Caller ability token.
1735  * @return Returns true if execute success, false otherwise.
1736  */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)1737 int FormMgrAdapter::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
1738 {
1739     HILOG_INFO("%{public}s called.", __func__);
1740     if (formId <= 0) {
1741         HILOG_ERROR("%{public}s form formId is invalid", __func__);
1742         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1743     }
1744 
1745     if (callerToken == nullptr) {
1746         HILOG_ERROR("%{public}s failed, callerToken can not be NULL", __func__);
1747         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1748     }
1749 
1750     if (!want.HasParameter(Constants::PARAM_MESSAGE_KEY)) {
1751         HILOG_ERROR("%{public}s failed, message info is not exist", __func__);
1752         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1753     }
1754 
1755     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1756     FormRecord record;
1757     bool bGetRecord = FormDataMgr::GetInstance().GetFormRecord(matchedFormId, record);
1758     if (!bGetRecord) {
1759         HILOG_ERROR("%{public}s fail, not exist such form:%{public}" PRId64 "", __func__, matchedFormId);
1760         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1761     }
1762 
1763     FormHostRecord formHostRecord;
1764     bool isHostExist = FormDataMgr::GetInstance().GetMatchedHostClient(callerToken, formHostRecord);
1765     if (!isHostExist) {
1766         HILOG_ERROR("%{public}s failed, cannot find target client.", __func__);
1767         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1768     }
1769 
1770     if (!formHostRecord.Contains(matchedFormId)) {
1771         HILOG_ERROR("%{public}s failed, form is not self-owned.", __func__);
1772         return ERR_APPEXECFWK_FORM_OPERATION_NOT_SELF;
1773     }
1774 
1775     auto errCode = FormProviderMgr::GetInstance().MessageEvent(matchedFormId, record, want);
1776     if (errCode != ERR_OK) {
1777         return errCode;
1778     }
1779     HILOG_INFO("%{public}s, find target client.", __func__);
1780 
1781 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
1782     if (!FormDataMgr::GetInstance().ExistTempForm(matchedFormId)) {
1783         int callingUid = IPCSkeleton::GetCallingUid();
1784         int32_t userId = GetCurrentUserId(callingUid);
1785         DeviceUsageStats::BundleActiveEvent event(record.bundleName, record.moduleName, record.formName,
1786             record.specification, record.formId, DeviceUsageStats::BundleActiveEvent::FORM_IS_CLICKED);
1787         DeviceUsageStats::BundleActiveClient::GetInstance().ReportEvent(event, userId);
1788     }
1789 #endif
1790     return ERR_OK;
1791 }
1792 
1793 /**
1794  * @brief Process js router event.
1795  * @param formId Indicates the unique id of form.
1796  * @param want the want of the ability to start.
1797  * @param callerToken Caller ability token.
1798  * @return Returns true if execute success, false otherwise.
1799  */
RouterEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)1800 int FormMgrAdapter::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
1801 {
1802     HILOG_INFO("%{public}s called.", __func__);
1803     if (formId <= 0) {
1804         HILOG_ERROR("%{public}s form formId or bundleName is invalid", __func__);
1805         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1806     }
1807 
1808     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1809     FormRecord record;
1810     bool bGetRecord = FormDataMgr::GetInstance().GetFormRecord(matchedFormId, record);
1811     if (!bGetRecord) {
1812         HILOG_ERROR("%{public}s fail, not exist such form:%{public}" PRId64 "", __func__, matchedFormId);
1813         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1814     }
1815 
1816     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
1817     if (iBundleMgr == nullptr) {
1818         HILOG_ERROR("%{public}s fail, failed to get IBundleMgr.", __func__);
1819         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
1820     }
1821 
1822     if (record.bundleName != want.GetBundle()) {
1823         if (!CheckIsSystemAppByBundleName(iBundleMgr, record.bundleName)) {
1824             HILOG_WARN("Only system apps can launch the ability of the other apps.");
1825             want.SetBundle(record.bundleName);
1826         }
1827     }
1828 
1829     want.SetParam(Constants::PARAM_FORM_ID, formId);
1830     want.SetParam(Constants::PARAM_FORM_IDENTITY_KEY, formId);
1831     int32_t result = FormAmsHelper::GetInstance().GetAbilityManager()->StartAbility(want, callerToken);
1832     if (result != ERR_OK && result != START_ABILITY_WAITING) {
1833         HILOG_ERROR("Failed to StartAbility, result: %{public}d.", result);
1834         return result;
1835     }
1836 
1837 #ifdef DEVICE_USAGE_STATISTICS_ENABLE
1838     if (!FormDataMgr::GetInstance().ExistTempForm(matchedFormId)) {
1839         int32_t callingUid = IPCSkeleton::GetCallingUid();
1840         int32_t userId = GetCurrentUserId(callingUid);
1841         DeviceUsageStats::BundleActiveEvent event(record.bundleName, record.moduleName, record.formName,
1842             record.specification, record.formId, DeviceUsageStats::BundleActiveEvent::FORM_IS_CLICKED);
1843         DeviceUsageStats::BundleActiveClient::GetInstance().ReportEvent(event, userId);
1844     }
1845 #endif
1846     return ERR_OK;
1847 }
1848 
1849 /**
1850  * @brief Process background router event.
1851  * @param formId Indicates the unique id of form.
1852  * @param want the want of the ability to start.
1853  * @param callerToken Caller ability token.
1854  * @return Returns true if execute success, false otherwise.
1855  */
BackgroundEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)1856 int FormMgrAdapter::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
1857 {
1858     HILOG_INFO("%{public}s called.", __func__);
1859     if (formId <= 0) {
1860         HILOG_ERROR("%{public}s form formId or bundleName is invalid", __func__);
1861         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1862     }
1863 
1864     int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
1865     FormRecord record;
1866     bool bGetRecord = FormDataMgr::GetInstance().GetFormRecord(matchedFormId, record);
1867     if (!bGetRecord) {
1868         HILOG_ERROR("%{public}s fail, not exist such form:%{public}" PRId64 "", __func__, matchedFormId);
1869         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
1870     }
1871 
1872     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
1873     if (iBundleMgr == nullptr) {
1874         HILOG_ERROR("%{public}s fail, failed to get IBundleMgr.", __func__);
1875         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
1876     }
1877 
1878     want.SetBundle(record.bundleName);
1879     if (!CheckKeepBackgroundRunningPermission(iBundleMgr, record.bundleName)) {
1880         HILOG_ERROR("The app does not have permission for keeping background running.");
1881         return ERR_APPEXECFWK_FORM_PERMISSION_DENY;
1882     }
1883 
1884     sptr<IAbilityConnection> formBackgroundConnection = new (std::nothrow) FormBackgroundConnection(formId,
1885          record.bundleName, record.abilityName);
1886     if (formBackgroundConnection == nullptr) {
1887         HILOG_ERROR("formBackgroundConnection is null.");
1888         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
1889     }
1890 
1891     want.SetParam(Constants::PARAM_FORM_ID, formId);
1892     want.SetParam(Constants::PARAM_FORM_IDENTITY_KEY, formId);
1893     int32_t result = IN_PROCESS_CALL(FormAmsHelper::GetInstance().GetAbilityManager()->StartAbilityByCall(want,
1894         formBackgroundConnection, callerToken));
1895     if (result != ERR_OK) {
1896         HILOG_ERROR("Failed to StartAbilityByCall, result: %{public}d.", result);
1897         return result;
1898     }
1899     return ERR_OK;
1900 }
1901 
HandleUpdateFormFlag(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool flag,bool isOnlyEnableUpdate)1902 ErrCode FormMgrAdapter::HandleUpdateFormFlag(const std::vector<int64_t> &formIds,
1903     const sptr<IRemoteObject> &callerToken, bool flag, bool isOnlyEnableUpdate)
1904 {
1905     HILOG_INFO("%{public}s called.", __func__);
1906     if (formIds.empty() || callerToken == nullptr) {
1907         HILOG_ERROR("%{public}s, invalid param", __func__);
1908         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1909     }
1910     std::vector<int64_t> refreshForms;
1911     int errCode = FormDataMgr::GetInstance().UpdateHostFormFlag(formIds, callerToken,
1912         flag, isOnlyEnableUpdate, refreshForms);
1913     if (errCode == ERR_OK && !refreshForms.empty()) {
1914         for (const int64_t id : refreshForms) {
1915             HILOG_INFO("%{public}s, formRecord need refresh: %{public}" PRId64 "", __func__, id);
1916             Want want;
1917             int callingUid = IPCSkeleton::GetCallingUid();
1918             int32_t userId = GetCurrentUserId(callingUid);
1919             want.SetParam(Constants::PARAM_FORM_USER_ID, userId);
1920             FormProviderMgr::GetInstance().RefreshForm(id, want, false);
1921         }
1922     }
1923     return errCode;
1924 }
1925 
1926 /**
1927  * @brief check form cached.
1928  * @param record Form information.
1929  * @return Returns true on cached, false on not.
1930  */
IsFormCached(const FormRecord record)1931 bool FormMgrAdapter::IsFormCached(const FormRecord record)
1932 {
1933     if (record.versionUpgrade) {
1934         return false;
1935     }
1936     return true;
1937 }
1938 
AcquireProviderFormInfo(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)1939 void FormMgrAdapter::AcquireProviderFormInfo(const int64_t formId, const Want &want,
1940     const sptr<IRemoteObject> &remoteObject)
1941 {
1942     HILOG_INFO("%{public}s called.", __func__);
1943     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
1944     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
1945     if (formProviderProxy == nullptr) {
1946         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
1947         HILOG_ERROR("%{public}s fail, Failed to get formProviderProxy", __func__);
1948         return;
1949     }
1950     FormRecord formRecord;
1951     FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
1952     FormJsInfo formJsInfo;
1953     FormDataMgr::GetInstance().CreateFormJsInfo(formId, formRecord, formJsInfo);
1954     int error = formProviderProxy->AcquireProviderFormInfo(formJsInfo, want, FormSupplyCallback::GetInstance());
1955     if (error != ERR_OK) {
1956         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
1957         HILOG_ERROR("%{public}s fail, Failed to get acquire provider form info", __func__);
1958     }
1959 }
1960 
1961 /**
1962  * @brief Notify form provider for delete form.
1963  *
1964  * @param formId The Id of the from.
1965  * @param want The want of the form.
1966  * @param remoteObject Form provider proxy object.
1967  */
NotifyFormDelete(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)1968 void FormMgrAdapter::NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
1969 {
1970     HILOG_INFO("%{public}s called.", __func__);
1971     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
1972     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
1973     if (formProviderProxy == nullptr) {
1974         HILOG_ERROR("%{public}s fail, Failed to get formProviderProxy", __func__);
1975         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
1976         return;
1977     }
1978     int error = formProviderProxy->NotifyFormDelete(formId, want, FormSupplyCallback::GetInstance());
1979     if (error != ERR_OK) {
1980         HILOG_ERROR("%{public}s fail, Failed to NotifyFormDelete", __func__);
1981         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
1982     }
1983 }
1984 
1985 /**
1986  * @brief Create eventMaps for event notify.
1987  *
1988  * @param matchedFormId The Id of the form
1989  * @param formRecord Form storage information
1990  * @param eventMaps eventMaps for event notify
1991  * @return Returns true on success, false on failure.
1992  */
CreateHandleEventMap(const int64_t matchedFormId,const FormRecord & formRecord,std::map<std::string,std::vector<int64_t>> & eventMaps)1993 bool FormMgrAdapter::CreateHandleEventMap(const int64_t matchedFormId, const FormRecord &formRecord,
1994     std::map<std::string, std::vector<int64_t>> &eventMaps)
1995 {
1996     if (!formRecord.formVisibleNotify) {
1997         HILOG_WARN("%{public}s fail, the config 'formVisibleNotify' is false, formId:%{public}" PRId64 ".",
1998             __func__, matchedFormId);
1999         return false;
2000     }
2001 
2002     std::string providerKey = formRecord.bundleName + Constants::NAME_DELIMITER + formRecord.abilityName;
2003     auto iter = eventMaps.find(providerKey);
2004     if (iter == eventMaps.end()) {
2005         std::vector<int64_t> formEventsByProvider {matchedFormId};
2006         eventMaps.insert(std::make_pair(providerKey, formEventsByProvider));
2007     } else {
2008         iter->second.emplace_back(matchedFormId);
2009     }
2010     return true;
2011 }
2012 /**
2013  * @brief Update provider info to host
2014  *
2015  * @param matchedFormId The Id of the form
2016  * @param callerToken Caller ability token.
2017  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
2018  * @param formRecord Form storage information
2019  * @return Returns true on success, false on failure.
2020  */
UpdateProviderInfoToHost(const int64_t matchedFormId,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType,FormRecord & formRecord)2021 bool FormMgrAdapter::UpdateProviderInfoToHost(const int64_t matchedFormId, const sptr<IRemoteObject> &callerToken,
2022     const int32_t formVisibleType, FormRecord &formRecord)
2023 {
2024     if (!FormDataMgr::GetInstance().GetFormRecord(matchedFormId, formRecord)) {
2025         HILOG_WARN("%{public}s fail, not exist such form, formId:%{public}" PRId64 ".", __func__, matchedFormId);
2026         return false;
2027     }
2028 
2029     FormHostRecord formHostRecord;
2030     bool hasFormHostRecord = FormDataMgr::GetInstance().GetMatchedHostClient(callerToken, formHostRecord);
2031     if (!(hasFormHostRecord && formHostRecord.Contains(matchedFormId))) {
2032         HILOG_WARN("%{public}s fail, form is not belong to self, formId:%{public}" PRId64 ".", __func__, matchedFormId);
2033         return false;
2034     }
2035 
2036     formRecord.formVisibleNotifyState = formVisibleType;
2037     if (!FormDataMgr::GetInstance().UpdateFormRecord(matchedFormId, formRecord)) {
2038         HILOG_WARN("%{public}s fail, set formVisibleNotifyState error, formId:%{public}" PRId64 ".",
2039         __func__, matchedFormId);
2040         return false;
2041     }
2042 
2043     // If the form need refresh flag is true and form visibleType is FORM_VISIBLE, refresh the form host.
2044     if (formRecord.needRefresh && formVisibleType == Constants::FORM_VISIBLE) {
2045         if (formRecord.isTimerRefresh) {
2046             Want want;
2047             int32_t userId = FormUtil::GetCurrentAccountId();
2048             want.SetParam(Constants::KEY_IS_TIMER, true);
2049             want.SetParam(Constants::KEY_TIMER_REFRESH, true);
2050             want.SetParam(Constants::PARAM_FORM_USER_ID, userId);
2051             FormProviderMgr::GetInstance().RefreshForm(formRecord.formId, want, false);
2052         } else {
2053             std::string cacheData;
2054             // If the form has business cache, refresh the form host.
2055             if (FormCacheMgr::GetInstance().GetData(matchedFormId, cacheData)) {
2056                 formRecord.formProviderInfo.SetFormDataString(cacheData);
2057                 formHostRecord.OnUpdate(matchedFormId, formRecord);
2058             }
2059         }
2060     }
2061     return true;
2062 }
2063 /**
2064  * @brief If the form provider is system app and the config item 'formVisibleNotify' is true,
2065  *        notify the form provider that the current form is visible.
2066  * @param iBundleMgr BundleManagerProxy
2067  * @param bundleName BundleName
2068  * @return Returns true if the form provider is system app, false if not.
2069  */
CheckIsSystemAppByBundleName(const sptr<IBundleMgr> & iBundleMgr,const std::string & bundleName)2070 bool FormMgrAdapter::CheckIsSystemAppByBundleName(const sptr<IBundleMgr> &iBundleMgr, const std::string &bundleName)
2071 {
2072     BundleInfo bundleInfo;
2073     if (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT,
2074         bundleInfo, FormUtil::GetCurrentAccountId()))) {
2075         HILOG_DEBUG("%{public}s, get bundle uid success", __func__);
2076         if (!IN_PROCESS_CALL(iBundleMgr->CheckIsSystemAppByUid(bundleInfo.uid))) {
2077             HILOG_WARN("%{public}s fail, form provider is not system app, bundleName: %{public}s",
2078                 __func__, bundleName.c_str());
2079             return false;
2080         }
2081     } else {
2082         HILOG_WARN("%{public}s fail, can not get bundleInfo's uid", __func__);
2083         return false;
2084     }
2085 
2086     return true;
2087 }
2088 
2089 /**
2090  * @brief if the ability have permission for keeping background running is true,
2091  * @param iBundleMgr BundleManagerProxy
2092  * @param bundleName BundleName
2093  * @return Returns true if the ability have permission for keeping background running, false if not.
2094  */
CheckKeepBackgroundRunningPermission(const sptr<IBundleMgr> & iBundleMgr,const std::string & bundleName)2095 bool FormMgrAdapter::CheckKeepBackgroundRunningPermission(const sptr<IBundleMgr> &iBundleMgr, const std::string &bundleName)
2096 {
2097     BundleInfo bundleInfo;
2098     if (FormBmsHelper::GetInstance().GetBundleInfoWithPermission(bundleName,
2099         FormUtil::GetCurrentAccountId(), bundleInfo)) {
2100         HILOG_DEBUG("%{public}s, get bundleInfo success", __func__);
2101         auto item = find(bundleInfo.reqPermissions.begin(), bundleInfo.reqPermissions.end(), Constants::PERMISSION_KEEP_BACKGROUND_RUNNING);
2102         if (item == bundleInfo.reqPermissions.end()) {
2103             return false;
2104         }
2105     } else {
2106         HILOG_WARN("%{public}s fail, can not get bundleInfo's uid", __func__);
2107         return false;
2108     }
2109 
2110     return true;
2111 }
2112 /**
2113  * @brief Get current user ID.
2114  * @param callingUid calling Uid.
2115  * @return Returns user ID.
2116  */
GetCurrentUserId(const int callingUid)2117 int32_t FormMgrAdapter::GetCurrentUserId(const int callingUid)
2118 {
2119     // get current userId
2120     int32_t userId = callingUid / CALLING_UID_TRANSFORM_DIVISOR;
2121     return userId;
2122 }
2123 
2124 /**
2125  * @brief Delete the invalid forms.
2126  * @param formIds Indicates the ID of the valid forms.
2127  * @param callerToken Caller ability token.
2128  * @param numFormsDeleted Returns the number of the deleted forms.
2129  * @return Returns ERR_OK on success, others on failure.
2130  */
DeleteInvalidForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,int32_t & numFormsDeleted)2131 int FormMgrAdapter::DeleteInvalidForms(const std::vector<int64_t> &formIds,
2132     const sptr<IRemoteObject> &callerToken, int32_t &numFormsDeleted)
2133 {
2134     HILOG_INFO("%{public}s called.", __func__);
2135     if (callerToken == nullptr) {
2136         HILOG_ERROR("%{public}s, callerToken is nullptr", __func__);
2137         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2138     }
2139 
2140     std::set<int64_t> matchedFormIds {};
2141     for (int64_t formId : formIds) {
2142         int64_t matchedFormId = FormDataMgr::GetInstance().FindMatchedFormId(formId);
2143         matchedFormIds.emplace(matchedFormId);
2144         HILOG_DEBUG("valid formId, matchedFormIds: %{public}" PRId64 "", formId);
2145     }
2146     int32_t callingUid = IPCSkeleton::GetCallingUid();
2147     std::map<int64_t, bool> removedFormsMap {};
2148     int32_t userId = GetCurrentUserId(callingUid);
2149 
2150     // delete invalid DB form record
2151     FormDbCache::GetInstance().DeleteInvalidDBForms(userId, callingUid, matchedFormIds, removedFormsMap);
2152     // delete invalid temp form record
2153     FormDataMgr::GetInstance().DeleteInvalidTempForms(userId, callingUid, matchedFormIds, removedFormsMap);
2154 
2155     if (!removedFormsMap.empty()) {
2156         FormDataMgr::GetInstance().ClearHostDataByInvalidForms(callingUid, removedFormsMap);
2157         // delete forms timer
2158         for (const auto &removedForm : removedFormsMap) {
2159             if (removedForm.second) {
2160                 FormTimerMgr::GetInstance().RemoveFormTimer(removedForm.first);
2161             }
2162         }
2163     }
2164 
2165     std::string bundleName;
2166     if (GetBundleName(bundleName)) {
2167         // delete invalid publish form data
2168         FormDataMgr::GetInstance().DeleteInvalidPublishForms(userId, bundleName, matchedFormIds);
2169     }
2170 
2171     numFormsDeleted = static_cast<int32_t>(removedFormsMap.size());
2172     HILOG_INFO("%{public}s done, %{public}d forms deleted.", __func__, numFormsDeleted);
2173     return ERR_OK;
2174 }
2175 
2176 /**
2177  * @brief AcquireFormState want check.
2178  * @param bundleName The bundle name of the form.
2179  * @param abilityName The ability name of the form.
2180  * @param want The want of the form.
2181  * @param provider the provider info.
2182  * @return Returns ERR_OK on success, others on failure.
2183  */
AcquireFormStateCheck(const std::string & bundleName,const std::string & abilityName,const Want & want,std::string & provider)2184 ErrCode FormMgrAdapter::AcquireFormStateCheck(const std::string &bundleName,
2185     const std::string &abilityName, const Want &want, std::string &provider)
2186 {
2187     if (bundleName.empty() || abilityName.empty()) {
2188         HILOG_ERROR("%{public}s error, bundleName or abilityName is empty.", __func__);
2189         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2190     }
2191 
2192     std::string moduleName = want.GetStringParam(AppExecFwk::Constants::PARAM_MODULE_NAME_KEY);
2193     std::string formName = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_NAME_KEY);
2194     int32_t dimensionId = want.GetIntParam(AppExecFwk::Constants::PARAM_FORM_DIMENSION_KEY, 1);
2195 
2196     if (moduleName.empty() || formName.empty()) {
2197         HILOG_ERROR("%{public}s error, moduleName or formName is empty.", __func__);
2198         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2199     }
2200 
2201     std::vector<FormInfo> formInfos {};
2202     ErrCode errCode = FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos);
2203     if (errCode != ERR_OK) {
2204         HILOG_ERROR("%{public}s error, failed to get forms info.", __func__);
2205         return errCode;
2206     }
2207 
2208     bool found = false;
2209     for (auto &formInfo : formInfos) {
2210         if ((formInfo.abilityName == abilityName) && (formInfo.name == formName) &&
2211             (IsDimensionValid(formInfo, dimensionId))) {
2212             found = true;
2213             HILOG_INFO("%{public}s form info found.", __func__);
2214             break;
2215         }
2216     }
2217     if (!found) {
2218         HILOG_INFO("failed to find match form info.");
2219         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2220     }
2221 
2222     int32_t callingUid = IPCSkeleton::GetCallingUid();
2223     const std::string doubleColon = "::";
2224     provider.append(bundleName).append(doubleColon).append(abilityName).append(doubleColon)
2225         .append(moduleName).append(doubleColon).append(formName).append(doubleColon)
2226         .append(std::to_string(dimensionId)).append(doubleColon).append(std::to_string(callingUid));
2227     return ERR_OK;
2228 }
2229 
2230 /**
2231  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
2232  * @param want Indicates a set of parameters to be transparently passed to the form provider.
2233  * @param callerToken Caller ability token.
2234  * @param stateInfo Returns the form's state info of the specify.
2235  * @return Returns ERR_OK on success, others on failure.
2236  */
AcquireFormState(const Want & want,const sptr<IRemoteObject> & callerToken,FormStateInfo & stateInfo)2237 int FormMgrAdapter::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken,
2238                                      FormStateInfo &stateInfo)
2239 {
2240     if (callerToken == nullptr) {
2241         HILOG_ERROR("%{public}s, callerToken is nullptr", __func__);
2242         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2243     }
2244     std::string bundleName = want.GetElement().GetBundleName();
2245     std::string abilityName = want.GetElement().GetAbilityName();
2246 
2247     std::string provider;
2248     ErrCode errCode = AcquireFormStateCheck(bundleName, abilityName, want, provider);
2249     if (errCode != ERR_OK) {
2250         return errCode;
2251     }
2252 
2253     int32_t callingUid = IPCSkeleton::GetCallingUid();
2254     FormItemInfo info;
2255     FormDataMgr::GetInstance().CreateFormStateRecord(provider, info, callerToken, callingUid);
2256 
2257     HILOG_DEBUG("bundleName:%{public}s, abilityName:%{public}s", bundleName.c_str(), abilityName.c_str());
2258     sptr<IAbilityConnection> connection =
2259         new (std::nothrow) FormAcquireStateConnection(bundleName, abilityName, want, provider);
2260     if (connection == nullptr) {
2261         HILOG_ERROR("failed to create FormAcquireStateConnection.");
2262         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2263     }
2264     Want targetWant;
2265     targetWant.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
2266     targetWant.SetElementName(bundleName, abilityName);
2267     ErrCode errorCode = FormAmsHelper::GetInstance().ConnectServiceAbility(targetWant, connection);
2268     if (errorCode != ERR_OK) {
2269         HILOG_ERROR("%{public}s, ConnectServiceAbility failed.", __func__);
2270         return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED;
2271     }
2272     stateInfo.state = FormState::DEFAULT;
2273     return ERR_OK;
2274 }
2275 
2276 /**
2277  * @brief Notify the form is visible or not.
2278  * @param formIds Indicates the ID of the forms.
2279  * @param isVisible Visible or not.
2280  * @param callerToken Host client.
2281  * @return Returns ERR_OK on success, others on failure.
2282  */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)2283 int FormMgrAdapter::NotifyFormsVisible(const std::vector<int64_t> &formIds,
2284     bool isVisible, const sptr<IRemoteObject> &callerToken)
2285 {
2286     HILOG_INFO("%{public}s, isVisible: %{public}d.", __func__, isVisible);
2287     return FormDataMgr::GetInstance().NotifyFormsVisible(formIds, isVisible, callerToken);
2288 }
2289 
2290 /**
2291  * @brief Notify the form is enable to be updated or not.
2292  * @param formIds Indicates the ID of the forms.
2293  * @param isEnableUpdate enable update or not.
2294  * @param callerToken Host client.
2295  * @return Returns ERR_OK on success, others on failure.
2296  */
NotifyFormsEnableUpdate(const std::vector<int64_t> & formIds,bool isEnableUpdate,const sptr<IRemoteObject> & callerToken)2297 int FormMgrAdapter::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds,
2298     bool isEnableUpdate, const sptr<IRemoteObject> &callerToken)
2299 {
2300     HILOG_INFO("%{public}s, isEnableUpdate: %{public}d.", __func__, isEnableUpdate);
2301     return HandleUpdateFormFlag(formIds, callerToken, isEnableUpdate, true);
2302 }
2303 
2304 /**
2305  * @brief Get All FormsInfo.
2306  * @param formInfos Return the forms' information of all forms provided.
2307  * @return Returns ERR_OK on success, others on failure.
2308  */
GetAllFormsInfo(std::vector<FormInfo> & formInfos)2309 int FormMgrAdapter::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
2310 {
2311     return FormInfoMgr::GetInstance().GetAllFormsInfo(formInfos);
2312 }
2313 
2314 /**
2315  * @brief Get forms info by bundle name .
2316  * @param bundleName Application name.
2317  * @param formInfos Return the forms' information of the specify application name.
2318  * @return Returns ERR_OK on success, others on failure.
2319  */
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos)2320 int FormMgrAdapter::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos)
2321 {
2322     return FormInfoMgr::GetInstance().GetFormsInfoByBundle(bundleName, formInfos);
2323 }
2324 
2325 /**
2326  * @brief Get forms info by bundle name and module name.
2327  * @param bundleName bundle name.
2328  * @param moduleName Module name of hap.
2329  * @param formInfos Return the forms' information of the specify bundle name and module name.
2330  * @return Returns ERR_OK on success, others on failure.
2331  */
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos)2332 int FormMgrAdapter::GetFormsInfoByModule(const std::string &bundleName,
2333     const std::string &moduleName, std::vector<FormInfo> &formInfos)
2334 {
2335     return FormInfoMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos);
2336 }
2337 
IsRequestPublishFormSupported()2338 bool FormMgrAdapter::IsRequestPublishFormSupported()
2339 {
2340     /* Query the highest priority ability or extension ability for publishing form */
2341     AppExecFwk::AbilityInfo abilityInfo;
2342     AppExecFwk::ExtensionAbilityInfo extensionAbilityInfo;
2343     int callingUid = IPCSkeleton::GetCallingUid();
2344     int32_t userId = GetCurrentUserId(callingUid);
2345     if (!FormBmsHelper::GetInstance().GetAbilityInfoByAction(
2346         Constants::FORM_PUBLISH_ACTION, userId, abilityInfo, extensionAbilityInfo)) {
2347         HILOG_ERROR("Failed to ImplicitQueryInfoByPriority for publishing form");
2348         return false;
2349     }
2350 
2351     if (abilityInfo.name.empty() && extensionAbilityInfo.name.empty()) {
2352         HILOG_ERROR("Query highest priority ability failed, no form host ability found.");
2353         return false;
2354     }
2355     return true;
2356 }
2357 
2358 /**
2359  * @brief check if the form host is system app
2360  * @param formRecord Form storage information
2361  * @return Returns true if the form host is system app, false if not.
2362  */
checkFormHostHasSaUid(const FormRecord & formRecord)2363 bool FormMgrAdapter::checkFormHostHasSaUid(const FormRecord &formRecord)
2364 {
2365     return std::find(formRecord.formUserUids.begin(), formRecord.formUserUids.end(),
2366         SYSTEM_UID) != formRecord.formUserUids.end();
2367 }
2368 } // namespace AppExecFwk
2369 } // namespace OHOS
2370