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_task_mgr.h"
17 
18 #include <cinttypes>
19 #include <utility>
20 
21 #include "fms_log_wrapper.h"
22 #include "form_constants.h"
23 #include "form_data_mgr.h"
24 #include "form_host_delegate_proxy.h"
25 #include "form_host_interface.h"
26 #include "form_mgr_adapter.h"
27 #include "form_mgr_errors.h"
28 #include "form_provider_interface.h"
29 #include "form_render_interface.h"
30 #include "form_serial_queue.h"
31 #include "form_share_mgr.h"
32 #include "form_supply_callback.h"
33 #include "js_form_state_observer_interface.h"
34 #include "form_info_rdb_storage_mgr.h"
35 #include "form_util.h"
36 #include "form_record_report.h"
37 
38 namespace OHOS {
39 namespace AppExecFwk { // namespace
FormTaskMgr()40 FormTaskMgr::FormTaskMgr() {}
~FormTaskMgr()41 FormTaskMgr::~FormTaskMgr() {}
42 /**
43  * @brief Acquire form data from form provider(task).
44  * @param formId The Id of the form.
45  * @param want The want of the request.
46  * @param remoteObject Form provider proxy object.
47  */
PostAcquireTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)48 void FormTaskMgr::PostAcquireTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
49 {
50     if (serialQueue_ == nullptr) {
51         HILOG_ERROR("null serialQueue_");
52         return;
53     }
54     auto acquireProviderFormInfoFunc = [formId, want, remoteObject]() {
55         FormTaskMgr::GetInstance().AcquireProviderFormInfo(formId, want, remoteObject);
56     };
57     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireProviderFormInfoFunc);
58 }
59 
PostShareAcquireTask(int64_t formId, const std::string &remoteDeviceId, const Want &want, const sptr<IRemoteObject> &remoteObject)60 void FormTaskMgr::PostShareAcquireTask(int64_t formId, const std::string &remoteDeviceId, const Want &want,
61     const sptr<IRemoteObject> &remoteObject)
62 {
63     if (serialQueue_ == nullptr) {
64         HILOG_ERROR("null serialQueue_");
65         int64_t requestCode = static_cast<int64_t>(want.GetLongParam(Constants::FORM_SHARE_REQUEST_CODE, 0));
66         PostFormShareSendResponse(requestCode, ERR_APPEXECFWK_FORM_COMMON_CODE);
67         return;
68     }
69 
70     auto acquireShareProviderFormInfoFunc = [formId, remoteDeviceId, want, remoteObject]() {
71         FormTaskMgr::GetInstance().AcquireShareFormData(formId, remoteDeviceId, want, remoteObject);
72     };
73     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireShareProviderFormInfoFunc);
74 }
75 /**
76  * @brief Delete form data from form provider(task).
77  * @param formId The Id of the form.
78  * @param want The want of the request.
79  * @param remoteObject Form provider proxy object.
80  */
PostDeleteTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)81 void FormTaskMgr::PostDeleteTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
82 {
83     if (serialQueue_ == nullptr) {
84         HILOG_ERROR("null serialQueue_");
85         return;
86     }
87     auto notifyFormDeleteFunc = [formId, want, remoteObject]() {
88         FormTaskMgr::GetInstance().NotifyFormDelete(formId, want, remoteObject);
89     };
90     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyFormDeleteFunc);
91 }
92 
93 /**
94  * @brief Refresh form data from form provider(task).
95  *
96  * @param formId The Id of the form.
97  * @param want The want of the form.
98  * @param remoteObject Form provider proxy object.
99  * @return none.
100  */
PostRefreshTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)101 void FormTaskMgr::PostRefreshTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
102 {
103     if (serialQueue_ == nullptr) {
104         HILOG_ERROR("null serialQueue_");
105         return;
106     }
107     auto notifyFormUpdateFunc = [formId, want, remoteObject]() {
108         FormTaskMgr::GetInstance().NotifyFormUpdate(formId, want, remoteObject);
109     };
110     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyFormUpdateFunc);
111 }
112 
113 /**
114  * @brief Cast temp form data from form provider(task).
115  *
116  * @param formId The Id of the form.
117  * @param want The want of the form.
118  * @param remoteObject Form provider proxy object.
119  * @return none.
120  */
PostCastTempTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)121 void FormTaskMgr::PostCastTempTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
122 {
123     if (serialQueue_ == nullptr) {
124         HILOG_ERROR("null serialQueue_");
125         return;
126     }
127     auto notifyCastTempFunc = [formId, want, remoteObject]() {
128         FormTaskMgr::GetInstance().NotifyCastTemp(formId, want, remoteObject);
129     };
130     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyCastTempFunc);
131 }
132 
133 /**
134  * @brief Post form data to form host(task) when acquire form.
135  * @param formId The Id of the form.
136  * @param callingUid Calling uid.
137  * @param info Form configure info.
138  * @param wantParams WantParams of the request.
139  * @param remoteObject Form provider proxy object.
140  */
PostAcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject)141 void FormTaskMgr::PostAcquireTaskToHost(const int64_t formId, const FormRecord &record,
142     const sptr<IRemoteObject> &remoteObject)
143 {
144     if (serialQueue_ == nullptr) {
145         HILOG_ERROR("null serialQueue_");
146         return;
147     }
148     auto acquireTaskToHostFunc = [formId, record, remoteObject]() {
149         FormTaskMgr::GetInstance().AcquireTaskToHost(formId, record, remoteObject);
150     };
151     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
152 }
153 
PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams, int64_t requestCode, const sptr<IRemoteObject> &remoteObject)154 void FormTaskMgr::PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams,
155     int64_t requestCode, const sptr<IRemoteObject> &remoteObject)
156 {
157     if (serialQueue_ == nullptr) {
158         HILOG_ERROR("serialQueue_ invalidate");
159         return;
160     }
161     auto acquireTaskToHostFunc = [wantParams, requestCode, remoteObject]() {
162         FormTaskMgr::GetInstance().AcquireFormDataBack(wantParams, requestCode, remoteObject);
163     };
164     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
165 }
166 
167 /**
168  * @brief Post form data to form host(task) when update form.
169  * @param formId The Id of the form.
170  * @param callingUid Calling uid.
171  * @param info Form configure info.
172  * @param wantParams WantParams of the request.
173  * @param remoteObject Form provider proxy object.
174  */
PostUpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject)175 void FormTaskMgr::PostUpdateTaskToHost(const int64_t formId, const FormRecord &record,
176     const sptr<IRemoteObject> &remoteObject)
177 {
178     HILOG_INFO("call");
179 
180     if (serialQueue_ == nullptr) {
181         HILOG_ERROR("serialQueue_ invalidate");
182         return;
183     }
184 
185     HILOG_DEBUG("post the task of updateTaskToHostFunc");
186     auto updateTaskToHostFunc = [formId, record, remoteObject]() {
187         FormTaskMgr::GetInstance().UpdateTaskToHost(formId, record, remoteObject);
188     };
189     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, updateTaskToHostFunc);
190 }
191 
192 /**
193  * @brief Acquire form data from form provider.
194  * @param formId The Id of the form.
195  * @param info Form configure info.
196  * @param wantParams WantParams of the request.
197  * @param remoteObject Form provider proxy object.
198  */
199 /**
200  * @brief Handle form host died(task).
201  * @param remoteHost Form host proxy object.
202  */
PostHostDiedTask(const sptr<IRemoteObject> &remoteHost)203 void FormTaskMgr::PostHostDiedTask(const sptr<IRemoteObject> &remoteHost)
204 {
205     if (serialQueue_ == nullptr) {
206         HILOG_ERROR("null serialQueue_");
207         return;
208     }
209     auto postTaskFunc = [remoteHost]() {
210         FormTaskMgr::GetInstance().HostDied(remoteHost);
211     };
212     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, postTaskFunc);
213 }
214 
215 /**
216  * @brief Post event notify to form provider.
217  *
218  * @param formEvent The vector of form ids.
219  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
220  * @param want The want of the form.
221  * @param remoteObject The form provider proxy object.
222  * @return none.
223  */
PostEventNotifyTask(const std::vector<int64_t> &formEvent, const int32_t formVisibleType, const Want &want, const sptr<IRemoteObject> &remoteObject)224 void FormTaskMgr::PostEventNotifyTask(const std::vector<int64_t> &formEvent, const int32_t formVisibleType,
225     const Want &want, const sptr<IRemoteObject> &remoteObject)
226 {
227     if (serialQueue_ == nullptr) {
228         HILOG_ERROR("null serialQueue_");
229         return;
230     }
231     auto eventNotifyFunc = [formEvent, formVisibleType, want, remoteObject]() {
232         FormTaskMgr::GetInstance().EventNotify(formEvent, formVisibleType, want, remoteObject);
233     };
234     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, eventNotifyFunc);
235 }
236 /**
237  * @brief Post provider batch delete.
238  * @param formIds The Id list.
239  * @param want The want of the request.
240  * @param remoteObject Form provider proxy object.
241  */
PostProviderBatchDeleteTask(std::set<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObject)242 void FormTaskMgr::PostProviderBatchDeleteTask(std::set<int64_t> &formIds, const Want &want,
243     const sptr<IRemoteObject> &remoteObject)
244 {
245     if (serialQueue_ == nullptr) {
246         HILOG_ERROR("null serialQueue_");
247         return;
248     }
249     auto batchDeleteFunc = [&formIds, want, remoteObject]() {
250         FormTaskMgr::GetInstance().ProviderBatchDelete(formIds, want, remoteObject);
251     };
252     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, batchDeleteFunc);
253 }
254 /**
255  * @brief Post message event to form provider.
256  * @param formId The Id of the from.
257  * @param message Event message.
258  * @param want The want of the request.
259  * @param remoteObject Form provider proxy object.
260  */
PostFormEventTask(const int64_t formId, const std::string &message, const Want &want, const sptr<IRemoteObject> &remoteObject)261 void FormTaskMgr::PostFormEventTask(const int64_t formId, const std::string &message,
262     const Want &want, const sptr<IRemoteObject> &remoteObject)
263 {
264     if (serialQueue_ == nullptr) {
265         HILOG_ERROR("null serialQueue_");
266         return;
267     }
268     auto formEventFunc = [formId, message, want, remoteObject]() {
269         FormTaskMgr::GetInstance().FireFormEvent(formId, message, want, remoteObject);
270     };
271     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, formEventFunc);
272 }
273 
274 /**
275 * @brief Post acquire state to form provider.
276 * @param wantArg The want of onAcquireFormState.
277 * @param provider The provider info.
278 * @param want The want of the request.
279 * @param remoteObject Form provider proxy object.
280 */
PostAcquireStateTask(const Want &wantArg, const std::string &provider, const Want &want, const sptr<IRemoteObject> &remoteObject)281 void FormTaskMgr::PostAcquireStateTask(const Want &wantArg, const std::string &provider, const Want &want,
282                                        const sptr<IRemoteObject> &remoteObject)
283 {
284     if (serialQueue_ == nullptr) {
285         HILOG_ERROR("null serialQueue_");
286         return;
287     }
288     auto acquireStateFunc = [wantArg, provider, want, remoteObject]() {
289         FormTaskMgr::GetInstance().AcquireState(wantArg, provider, want, remoteObject);
290     };
291     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireStateFunc);
292 }
293 
294 /**
295 * @brief Post acquire data to form provider.
296 * @param formId The Id of the from.
297 * @param want The want of the request.
298 * @param remoteObject Form provider proxy object.
299 */
PostAcquireDataTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)300 void FormTaskMgr::PostAcquireDataTask(const int64_t formId, const Want &want,
301                                       const sptr<IRemoteObject> &remoteObject)
302 {
303     if (serialQueue_ == nullptr) {
304         HILOG_ERROR("serialQueue_ invalidate");
305         return;
306     }
307     auto acquireDataFunc = [formId, want, remoteObject]() {
308         FormTaskMgr::GetInstance().AcquireFormData(formId, want, remoteObject);
309     };
310     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireDataFunc);
311 }
312 
313 /**
314  * @brief Post uninstall message to form host(task).
315  * @param formIds The Id list of the forms.
316  * @param remoteObject Form provider proxy object.
317  */
PostUninstallTaskToHost(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject)318 void FormTaskMgr::PostUninstallTaskToHost(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject)
319 {
320     HILOG_INFO("start");
321     if (serialQueue_ == nullptr) {
322         HILOG_ERROR("null serialQueue_");
323         return;
324     }
325     auto uninstallFunc = [formIds, remoteObject]() {
326         FormTaskMgr::GetInstance().FormUninstall(formIds, remoteObject);
327     };
328     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, uninstallFunc);
329     HILOG_INFO("end");
330 }
331 
332 /**
333 * @brief Post acquire form state message to form host(task).
334 * @param state The form state.
335 * @param want The want of onAcquireFormState.
336 * @param remoteObject Form provider proxy object.
337 */
PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want, const sptr<IRemoteObject> &remoteObject)338 void FormTaskMgr::PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want,
339                                              const sptr<IRemoteObject> &remoteObject)
340 {
341     HILOG_INFO("start");
342     if (serialQueue_ == nullptr) {
343         HILOG_ERROR("null serialQueue_");
344         return;
345     }
346     auto acquireStateFunc = [state, want, remoteObject]() {
347         FormTaskMgr::GetInstance().AcquireStateBack(state, want, remoteObject);
348     };
349     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireStateFunc);
350     HILOG_INFO("end");
351 }
352 
PostFormShareSendResponse(int64_t formShareRequestCode, int32_t result)353 void FormTaskMgr::PostFormShareSendResponse(int64_t formShareRequestCode, int32_t result)
354 {
355     HILOG_INFO("start");
356     if (serialQueue_ == nullptr) {
357         HILOG_ERROR("null serialQueue_");
358         return;
359     }
360 
361     auto formShareSendResponseFunc = [formShareRequestCode, result]() {
362         FormTaskMgr::GetInstance().FormShareSendResponse(formShareRequestCode, result);
363     };
364     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, formShareSendResponseFunc);
365     HILOG_INFO("end");
366 }
367 
PostAddTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)368 void FormTaskMgr::PostAddTaskToHost(const std::string bundleName,
369     const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
370 {
371     HILOG_DEBUG("start");
372     if (serialQueue_ == nullptr) {
373         HILOG_ERROR("serialQueue_ invalidate");
374         return;
375     }
376     auto addFunc = [bundleName, remoteObject, runningFormInfo]() {
377         FormTaskMgr::GetInstance().FormAdd(bundleName, remoteObject, runningFormInfo);
378     };
379     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, addFunc);
380     HILOG_DEBUG("end");
381 }
382 
PostRemoveTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)383 void FormTaskMgr::PostRemoveTaskToHost(const std::string bundleName,
384     const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
385 {
386     HILOG_DEBUG("start");
387     if (serialQueue_ == nullptr) {
388         HILOG_ERROR("serialQueue_ invalidate");
389         return;
390     }
391     auto removeFunc = [bundleName, remoteObject, runningFormInfo]() {
392         FormTaskMgr::GetInstance().FormRemove(bundleName, remoteObject, runningFormInfo);
393     };
394     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, removeFunc);
395     HILOG_DEBUG("end");
396 }
397 
FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)398 void FormTaskMgr::FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
399     const RunningFormInfo &runningFormInfo)
400 {
401     HILOG_DEBUG("start");
402     sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
403         iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
404     if (remoteJsFormStateObserver == nullptr) {
405         HILOG_ERROR("get jsFormStateObserverProxy failed");
406         return;
407     }
408     remoteJsFormStateObserver->OnAddForm(bundleName, runningFormInfo);
409     HILOG_DEBUG("end");
410 }
411 
FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)412 void FormTaskMgr::FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
413     const RunningFormInfo &runningFormInfo)
414 {
415     HILOG_DEBUG("start");
416     sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
417         iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
418     if (remoteJsFormStateObserver == nullptr) {
419         HILOG_ERROR("get jsFormStateObserverProxy failed");
420         return;
421     }
422     remoteJsFormStateObserver->OnRemoveForm(bundleName, runningFormInfo);
423     HILOG_DEBUG("end");
424 }
425 
AcquireProviderFormInfo(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)426 void FormTaskMgr::AcquireProviderFormInfo(const int64_t formId, const Want &want,
427     const sptr<IRemoteObject> &remoteObject)
428 {
429     FormMgrAdapter::GetInstance().AcquireProviderFormInfo(formId, want, remoteObject);
430 }
431 
AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId, const Want &want, const sptr<IRemoteObject> &remoteObject)432 void FormTaskMgr::AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId,
433     const Want &want, const sptr<IRemoteObject> &remoteObject)
434 {
435     DelayedSingleton<FormShareMgr>::GetInstance()->AcquireShareFormData(formId, remoteDeviceId, want, remoteObject);
436 }
437 /**
438  * @brief Notify form provider for delete form.
439  *
440  * @param formId The Id of the from.
441  * @param want The want of the form.
442  * @param remoteObject Form provider proxy object.
443  * @return none.
444  */
NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)445 void FormTaskMgr::NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
446 {
447     FormMgrAdapter::GetInstance().NotifyFormDelete(formId, want, remoteObject);
448 }
449 
450 /**
451  * @brief Notify form provider for updating form.
452  *
453  * @param formId The Id of the from.
454  * @param want The want of the form.
455  * @param remoteObject Form provider proxy object.
456  * @return none.
457  */
NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)458 void FormTaskMgr::NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
459 {
460     HILOG_INFO("call");
461 
462     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
463     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
464     if (formProviderProxy == nullptr) {
465         RemoveConnection(connectId);
466         HILOG_ERROR("get formProviderProxy failed");
467         return;
468     }
469     int error = formProviderProxy->NotifyFormUpdate(formId, want, FormSupplyCallback::GetInstance());
470     if (error != ERR_OK) {
471         RemoveConnection(connectId);
472         HILOG_ERROR("fail notify form update");
473     }
474 }
475 
476 /**
477  * @brief Event notify to form provider.
478  *
479  * @param formEvents The vector of form ids.
480  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
481  * @param want The want of the form.
482  * @param remoteObject The form provider proxy object.
483  * @return none.
484  */
EventNotify(const std::vector<int64_t> &formEvents, const int32_t formVisibleType, const Want &want, const sptr<IRemoteObject> &remoteObject)485 void FormTaskMgr::EventNotify(const std::vector<int64_t> &formEvents, const int32_t formVisibleType,
486     const Want &want, const sptr<IRemoteObject> &remoteObject)
487 {
488     HILOG_INFO("call");
489 
490     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
491     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
492     if (formProviderProxy == nullptr) {
493         RemoveConnection(connectId);
494         HILOG_ERROR("get formProviderProxy failed");
495         return;
496     }
497 
498     int error = formProviderProxy->EventNotify(formEvents, formVisibleType, want, FormSupplyCallback::GetInstance());
499     if (error != ERR_OK) {
500         RemoveConnection(connectId);
501         HILOG_ERROR("fail send event notify");
502     }
503 }
504 
505 /**
506  * @brief Notify form provider for cast temp form.
507  *
508  * @param formId The Id of the from.
509  * @param want The want of the form.
510  * @param remoteObject Form provider proxy object.
511  * @return none.
512  */
NotifyCastTemp(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)513 void FormTaskMgr::NotifyCastTemp(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
514 {
515     HILOG_INFO("call");
516 
517     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
518     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
519     if (formProviderProxy == nullptr) {
520         RemoveConnection(connectId);
521         HILOG_ERROR("get formProviderProxy failed");
522         return;
523     }
524 
525     int error = formProviderProxy->NotifyFormCastTempForm(formId, want, FormSupplyCallback::GetInstance());
526     if (error != ERR_OK) {
527         RemoveConnection(connectId);
528         HILOG_ERROR("acquire providerFormInfo failed");
529     }
530 }
531 
532 /**
533  * @brief Post form data to form host when acquire form.
534  * @param formId The Id of the form.
535  * @param callingUid Calling uid.
536  * @param info Form configure info.
537  * @param wantParams WantParams of the request.
538  * @param remoteObject Form provider proxy object.
539  */
AcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject)540 void FormTaskMgr::AcquireTaskToHost(const int64_t formId, const FormRecord &record,
541     const sptr<IRemoteObject> &remoteObject)
542 {
543     HILOG_INFO("formId:%{public}" PRId64, formId);
544 
545     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
546     if (remoteFormHost == nullptr) {
547         HILOG_ERROR("get formHostProxy failed");
548         return;
549     }
550 
551     HILOG_DEBUG("FormTaskMgr remoteFormHost OnAcquired");
552     remoteFormHost->OnAcquired(CreateFormJsInfo(formId, record), nullptr);
553 }
554 
555 /**
556  * @brief Post form data to form host when update form.
557  * @param formId The Id of the form.
558  * @param callingUid Calling uid.
559  * @param info Form configure info.
560  * @param wantParams WantParams of the request.
561  * @param remoteObject Form provider proxy object.
562  */
UpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject)563 void FormTaskMgr::UpdateTaskToHost(const int64_t formId, const FormRecord &record,
564     const sptr<IRemoteObject> &remoteObject)
565 {
566     HILOG_INFO("start");
567 
568     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
569     if (remoteFormHost == nullptr) {
570         HILOG_ERROR("get formHostProxy failed");
571         return;
572     }
573 
574     HILOG_DEBUG("FormTaskMgr remoteFormHost OnUpdate");
575     remoteFormHost->OnUpdate(CreateFormJsInfo(formId, record));
576 
577     HILOG_INFO("end");
578 }
579 
580 /**
581  * @brief Handle form host died.
582  * @param remoteHost Form host proxy object.
583  */
HostDied(const sptr<IRemoteObject> &remoteHost)584 void FormTaskMgr::HostDied(const sptr<IRemoteObject> &remoteHost)
585 {
586     HILOG_INFO("remote client died event");
587     if (remoteHost == nullptr) {
588         HILOG_INFO("remote client died, invalid param");
589         return;
590     }
591     FormDataMgr::GetInstance().HandleHostDied(remoteHost);
592     FormSupplyCallback::GetInstance()->HandleHostDied(remoteHost);
593 }
594 /**
595  * @brief Post provider batch delete.
596  * @param formIds The Id list.
597  * @param want The want of the request.
598  * @param remoteObject Form provider proxy object.
599  */
ProviderBatchDelete(std::set<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObject)600 void FormTaskMgr::ProviderBatchDelete(std::set<int64_t> &formIds, const Want &want,
601     const sptr<IRemoteObject> &remoteObject)
602 {
603     HILOG_INFO("start");
604     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
605     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
606     if (formProviderProxy == nullptr) {
607         RemoveConnection(connectId);
608         HILOG_ERROR("get formProviderProxy failed");
609         return;
610     }
611     std::vector<int64_t> vFormIds;
612     vFormIds.assign(formIds.begin(), formIds.end());
613     int error = formProviderProxy->NotifyFormsDelete(vFormIds, want, FormSupplyCallback::GetInstance());
614     if (error != ERR_OK) {
615         RemoveConnection(connectId);
616         HILOG_ERROR("fail");
617     }
618 }
619 /**
620  * @brief Fire message event to form provider.
621  * @param formId The Id of the from.
622  * @param message Event message.
623  * @param want The want of the request.
624  * @param remoteObject Form provider proxy object.
625  */
FireFormEvent(const int64_t formId, const std::string &message, const Want &want, const sptr<IRemoteObject> &remoteObject)626 void FormTaskMgr::FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
627     const sptr<IRemoteObject> &remoteObject)
628 {
629     HILOG_INFO("start");
630     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
631     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
632     if (formProviderProxy == nullptr) {
633         RemoveConnection(connectId);
634         HILOG_ERROR("get formProviderProxy failed");
635         return;
636     }
637 
638     int error = formProviderProxy->FireFormEvent(formId, message, want, FormSupplyCallback::GetInstance());
639     if (error != ERR_OK) {
640         RemoveConnection(connectId);
641         HILOG_ERROR("fire messageEvent to formProvider failed");
642     }
643 }
644 
645 /**
646  * @brief Acquire form state to form provider.
647  * @param wantArg The want of onAcquireFormState.
648  * @param provider The provider info.
649  * @param want The want of the request.
650  * @param remoteObject Form provider proxy object.
651  */
AcquireState(const Want &wantArg, const std::string &provider, const Want &want, const sptr<IRemoteObject> &remoteObject)652 void FormTaskMgr::AcquireState(const Want &wantArg, const std::string &provider, const Want &want,
653                                const sptr<IRemoteObject> &remoteObject)
654 {
655     HILOG_INFO("start");
656     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
657     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
658     if (formProviderProxy == nullptr) {
659         RemoveConnection(connectId);
660         HILOG_ERROR("get formProviderProxy failed");
661         return;
662     }
663 
664     int error = formProviderProxy->AcquireState(wantArg, provider, want, FormSupplyCallback::GetInstance());
665     if (error != ERR_OK) {
666         RemoveConnection(connectId);
667         HILOG_ERROR("acquire formState failed");
668     }
669     HILOG_INFO("end");
670 }
671 
672 /**
673  * @brief Acquire form data to form provider.
674  * @param formId The Id of the form.
675  * @param want The want of the request.
676  * @param remoteObject Form provider proxy object.
677  */
AcquireFormData(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)678 void FormTaskMgr::AcquireFormData(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
679 {
680     HILOG_INFO("start");
681     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
682     int64_t requestCode = static_cast<int64_t>(want.GetLongParam(Constants::FORM_ACQUIRE_DATA_REQUEST_CODE, 0));
683     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
684     if (formProviderProxy == nullptr) {
685         RemoveConnection(connectId);
686         HILOG_ERROR("null formProviderProxy");
687         return;
688     }
689 
690     int error = formProviderProxy->AcquireFormData(formId, FormSupplyCallback::GetInstance(), requestCode);
691     if (error != ERR_OK) {
692         RemoveConnection(connectId);
693         HILOG_ERROR("fail acquire formStateToFormProvider");
694     }
695     RemoveConnection(connectId);
696     HILOG_INFO("end");
697 }
698 
699 /**
700  * @brief Handle uninstall message.
701  * @param formIds The Id list of the forms.
702  * @param remoteObject Form provider proxy object.
703  */
FormUninstall(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject)704 void FormTaskMgr::FormUninstall(const std::vector<int64_t> &formIds,
705     const sptr<IRemoteObject> &remoteObject)
706 {
707     HILOG_INFO("start");
708     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
709     if (remoteFormHost == nullptr) {
710         HILOG_ERROR("get formHostProxy failed");
711         return;
712     }
713 
714     remoteFormHost->OnUninstall(formIds);
715 
716     HILOG_INFO("end");
717 }
718 
719 /**
720  * @brief Handle acquire state.
721  * @param state the form state.
722  * @param want The want of onAcquireFormState.
723  * @param remoteObject Form provider proxy object.
724  */
AcquireStateBack(AppExecFwk::FormState state, const AAFwk::Want &want, const sptr<IRemoteObject> &remoteObject)725 void FormTaskMgr::AcquireStateBack(AppExecFwk::FormState state, const AAFwk::Want &want,
726                                    const sptr<IRemoteObject> &remoteObject)
727 {
728     HILOG_INFO("start");
729     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
730     if (remoteFormHost == nullptr) {
731         HILOG_ERROR("get formHostProxy failed");
732         return;
733     }
734 
735     remoteFormHost->OnAcquireState(state, want);
736 
737     HILOG_INFO("end");
738 }
739 
740 /**
741  * @brief Handle acquire data.
742 * @param wantParams Indicates the data information acquired by the form.
743 * @param requestCode Indicates the requested id.
744 * @param remoteObject Form provider proxy object.
745 */
AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode, const sptr<IRemoteObject> &remoteObject)746 void FormTaskMgr::AcquireFormDataBack(const AAFwk::WantParams &wantParams,
747     int64_t requestCode, const sptr<IRemoteObject> &remoteObject)
748 {
749     HILOG_INFO("start");
750     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
751     if (remoteFormHost == nullptr) {
752         HILOG_ERROR("get formHostProxy failed");
753         return;
754     }
755 
756     remoteFormHost->OnAcquireDataResponse(wantParams, requestCode);
757 
758     HILOG_INFO("end");
759 }
760 
761 /**
762  * @brief Create form data for form host.
763  * @param formId The Id of the form.
764  * @param record Form record.
765  * @return Form data.
766  */
CreateFormJsInfo(const int64_t formId, const FormRecord &record)767 FormJsInfo FormTaskMgr::CreateFormJsInfo(const int64_t formId, const FormRecord &record)
768 {
769     HILOG_DEBUG("create formJsInfo");
770     FormJsInfo form;
771     form.formId = formId;
772     form.bundleName = record.bundleName;
773     form.abilityName = record.abilityName;
774     form.formName = record.formName;
775     form.moduleName = record.moduleName;
776     form.formTempFlag = record.formTempFlag;
777     form.jsFormCodePath = record.jsFormCodePath;
778     form.formData = record.formProviderInfo.GetFormDataString();
779     form.formProviderData = record.formProviderInfo.GetFormData();
780     form.formSrc = record.formSrc;
781     form.formWindow = record.formWindow;
782     form.type = record.type;
783     form.uiSyntax = record.uiSyntax;
784     form.isDynamic = record.isDynamic;
785     form.transparencyEnabled = record.transparencyEnabled;
786     form.modulePkgNameMap = record.modulePkgNameMap;
787     HILOG_DEBUG("jsPath: %{private}s, data: %{private}s",
788         form.jsFormCodePath.c_str(), form.formData.c_str());
789     return form;
790 }
791 
FormShareSendResponse(int64_t formShareRequestCode, int32_t result)792 void FormTaskMgr::FormShareSendResponse(int64_t formShareRequestCode, int32_t result)
793 {
794     auto formShareMgr = DelayedSingleton<FormShareMgr>::GetInstance();
795     if (formShareMgr == nullptr) {
796         HILOG_ERROR("null formShareMgr");
797         return;
798     }
799     formShareMgr->SendResponse(formShareRequestCode, result);
800 }
801 
PostRenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)802 void FormTaskMgr::PostRenderForm(const FormRecord &formRecord, const Want &want,
803     const sptr<IRemoteObject> &remoteObject)
804 {
805     HILOG_DEBUG("PostRenderForm");
806     if (serialQueue_ == nullptr) {
807         HILOG_ERROR("null serialQueue_");
808         return;
809     }
810 
811     auto renderForm = [formRecord, want, remoteObject]() {
812         FormTaskMgr::GetInstance().RenderForm(formRecord, want, remoteObject);
813     };
814     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, renderForm);
815     HILOG_DEBUG("end");
816 }
817 
RenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)818 void FormTaskMgr::RenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
819 {
820     HILOG_DEBUG("render form");
821     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
822     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
823     if (remoteFormRender == nullptr) {
824         RemoveConnection(connectId);
825         HILOG_ERROR("get formRenderProxy failed");
826         return;
827     }
828 
829     FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
830     int32_t error = remoteFormRender->RenderForm(formJsInfo, want, FormSupplyCallback::GetInstance());
831     FormRecordReport::GetInstance().IncreaseUpdateTimes(formRecord.formId, HiSysEventPointType::TYPE_DAILY_REFRESH);
832     if (!formRecord.isVisible) {
833         FormRecordReport::GetInstance().IncreaseUpdateTimes(formRecord.formId,
834             HiSysEventPointType::TYPE_INVISIBLE_UPDATE);
835     }
836     if (error != ERR_OK) {
837         RemoveConnection(connectId);
838         HILOG_ERROR("fail add form renderer");
839         return;
840     }
841 
842     HILOG_DEBUG("end");
843 }
844 
PostStopRenderingForm( const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)845 void FormTaskMgr::PostStopRenderingForm(
846     const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
847 {
848     HILOG_INFO("call");
849     if (serialQueue_ == nullptr) {
850         HILOG_ERROR("null serialQueue_");
851         return;
852     }
853 
854     auto deleterenderForm = [formRecord, want, remoteObject]() {
855         FormTaskMgr::GetInstance().StopRenderingForm(formRecord, want, remoteObject);
856     };
857     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, deleterenderForm);
858 }
859 
StopRenderingForm( const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)860 void FormTaskMgr::StopRenderingForm(
861     const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
862 {
863     HILOG_INFO("begin");
864     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
865     sptr<IFormRender> remoteFormDeleteRender = iface_cast<IFormRender>(remoteObject);
866     if (remoteFormDeleteRender == nullptr) {
867         RemoveConnection(connectId);
868         HILOG_ERROR("get formRenderProxy failed");
869         return;
870     }
871 
872     FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
873     int32_t error = remoteFormDeleteRender->StopRenderingForm(formJsInfo, want, FormSupplyCallback::GetInstance());
874     if (error != ERR_OK) {
875         RemoveConnection(connectId);
876         HILOG_ERROR("fail add form renderer");
877         return;
878     }
879 
880     HILOG_INFO("end");
881 }
882 
PostReleaseRenderer( int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)883 void FormTaskMgr::PostReleaseRenderer(
884     int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)
885 {
886     HILOG_INFO("begin");
887     if (serialQueue_ == nullptr) {
888         HILOG_ERROR("null serialQueue_");
889         return;
890     }
891 
892     auto deleterenderForm = [formId, compId, uid, remoteObject]() {
893         FormTaskMgr::GetInstance().ReleaseRenderer(formId, compId, uid, remoteObject);
894     };
895     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, deleterenderForm);
896     HILOG_INFO("end");
897 }
898 
ReleaseRenderer( int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)899 void FormTaskMgr::ReleaseRenderer(
900     int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)
901 {
902     HILOG_INFO("begin");
903     sptr<IFormRender> remoteFormDeleteRender = iface_cast<IFormRender>(remoteObject);
904     if (remoteFormDeleteRender == nullptr) {
905         HILOG_ERROR("get formRenderProxy failed");
906         return;
907     }
908 
909     int32_t error = remoteFormDeleteRender->ReleaseRenderer(formId, compId, uid);
910     if (error != ERR_OK) {
911         HILOG_ERROR("fail release form renderer");
912         return;
913     }
914     HILOG_INFO("end");
915 }
916 
ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want, const sptr<IRemoteObject> &remoteObject)917 void FormTaskMgr::ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
918     const sptr<IRemoteObject> &remoteObject)
919 {
920     HILOG_INFO("begin");
921     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
922     if (remoteFormRender == nullptr) {
923         HILOG_ERROR("get formRenderProxy failed");
924         return;
925     }
926 
927     std::vector<FormJsInfo> formJsInfos;
928     for (const auto &formRecord : formRecords) {
929         FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
930         formJsInfos.emplace_back(formJsInfo);
931     }
932 
933     int32_t error = remoteFormRender->ReloadForm(std::move(formJsInfos), want);
934     if (error != ERR_OK) {
935         HILOG_ERROR("fail reload form");
936         return;
937     }
938     HILOG_INFO("end");
939 }
940 
PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want, const sptr<IRemoteObject> &remoteObject)941 void FormTaskMgr::PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
942     const sptr<IRemoteObject> &remoteObject)
943 {
944     HILOG_INFO("begin");
945     if (serialQueue_ == nullptr) {
946         HILOG_ERROR("null serialQueue_");
947         return;
948     }
949     auto reloadForm = [forms = std::forward<decltype(formRecords)>(formRecords), want, remoteObject]() {
950         FormTaskMgr::GetInstance().ReloadForm(std::move(forms), want, remoteObject);
951     };
952     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, reloadForm);
953     HILOG_INFO("end");
954 }
955 
OnUnlock(const sptr<IRemoteObject> &remoteObject)956 void FormTaskMgr::OnUnlock(const sptr<IRemoteObject> &remoteObject)
957 {
958     HILOG_DEBUG("begin");
959     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
960     if (remoteFormRender == nullptr) {
961         HILOG_ERROR("get formRenderProxy failed");
962         return;
963     }
964     int32_t error = remoteFormRender->OnUnlock();
965     if (error != ERR_OK) {
966         HILOG_ERROR("fail");
967         return;
968     }
969     HILOG_DEBUG("end");
970 }
971 
PostOnUnlock(const sptr<IRemoteObject> &remoteObject)972 void FormTaskMgr::PostOnUnlock(const sptr<IRemoteObject> &remoteObject)
973 {
974     HILOG_DEBUG("call");
975     if (serialQueue_ == nullptr) {
976         HILOG_ERROR("null serialQueue_");
977         return;
978     }
979     auto task = [remoteObject]() {
980         FormTaskMgr::GetInstance().OnUnlock(remoteObject);
981     };
982     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, task);
983     HILOG_DEBUG("end");
984 }
985 
RemoveConnection(int32_t connectId)986 void FormTaskMgr::RemoveConnection(int32_t connectId)
987 {
988     auto formSupplyCallback = FormSupplyCallback::GetInstance();
989     if (formSupplyCallback == nullptr) {
990         HILOG_ERROR("null formSupplyCallback");
991         return;
992     }
993     formSupplyCallback->RemoveConnection(connectId);
994 }
995 
996 /**
997  * @brief want data from form router event(task).
998  * @param formId The id of the form.
999  * @param remoteObject Form router proxy manager object.
1000  * @param want The want of the form for router event.
1001  */
PostRouterProxyToHost(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want)1002 void FormTaskMgr::PostRouterProxyToHost(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want)
1003 {
1004     if (serialQueue_ == nullptr) {
1005         HILOG_ERROR("serialQueue_ invalidate");
1006         return;
1007     }
1008 
1009     auto routerProxyFunc = [formId, want, remoteObject]() {
1010         FormTaskMgr::GetInstance().FormRouterEventProxy(formId, remoteObject, want);
1011     };
1012     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, routerProxyFunc);
1013 }
1014 
1015 /**
1016  * @brief Form router event proxy.
1017  * @param formId The id of the form.
1018  * @param remoteObject Form router proxy manager object.
1019  * @param want The want of the form for router event.
1020  */
FormRouterEventProxy(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want)1021 void FormTaskMgr::FormRouterEventProxy(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want)
1022 {
1023     if (remoteObject == nullptr) {
1024         HILOG_ERROR("Fail,null remoteObject");
1025         return;
1026     }
1027 
1028     sptr<IFormHostDelegate> remoteFormHostDelegateProxy = iface_cast<IFormHostDelegate>(remoteObject);
1029     if (remoteFormHostDelegateProxy == nullptr) {
1030         HILOG_ERROR("Fail,null remoteFormHostDelegateProxy");
1031         return;
1032     }
1033     remoteFormHostDelegateProxy->RouterEvent(formId, want);
1034 }
1035 
1036 /**
1037  * @brief Post Form visible/invisible notify.
1038  * @param formIds  the Ids of forms need to notify.
1039  * @param formInstanceMaps formInstances for visibleNotify.
1040  * @param eventMaps eventMaps for event notify.
1041  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
1042  * @param visibleNotifyDelay delay time.
1043  */
PostVisibleNotify(const std::vector<int64_t> &formIds, std::map<std::string, std::vector<FormInstance>> &formInstanceMaps, std::map<std::string, std::vector<int64_t>> &eventMaps, const int32_t formVisibleType, int32_t visibleNotifyDelay)1044 void FormTaskMgr::PostVisibleNotify(const std::vector<int64_t> &formIds,
1045     std::map<std::string, std::vector<FormInstance>> &formInstanceMaps,
1046     std::map<std::string, std::vector<int64_t>> &eventMaps,
1047     const int32_t formVisibleType, int32_t visibleNotifyDelay)
1048 {
1049     HILOG_DEBUG("call");
1050     if (serialQueue_ == nullptr) {
1051         HILOG_ERROR("null serialQueue_");
1052         FormTaskMgr::GetInstance().NotifyVisible(formIds, formInstanceMaps, eventMaps, formVisibleType);
1053         return;
1054     }
1055     auto task = [formIds, formInstanceMaps, eventMaps, formVisibleType]() {
1056         FormTaskMgr::GetInstance().NotifyVisible(formIds, formInstanceMaps, eventMaps, formVisibleType);
1057     };
1058     serialQueue_->ScheduleTask(visibleNotifyDelay, task);
1059     HILOG_DEBUG("end");
1060 }
1061 
1062 /**
1063 * @brief Form visible/invisible notify.
1064 * @param formIds  the Ids of forms need to notify.
1065 * @param formInstanceMaps formInstances for visibleNotify.
1066 * @param eventMaps eventMaps for event notify.
1067 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
1068 */
NotifyVisible(const std::vector<int64_t> &formIds, std::map<std::string, std::vector<FormInstance>> formInstanceMaps, std::map<std::string, std::vector<int64_t>> eventMaps, const int32_t formVisibleType)1069 void FormTaskMgr::NotifyVisible(const std::vector<int64_t> &formIds,
1070     std::map<std::string, std::vector<FormInstance>> formInstanceMaps,
1071     std::map<std::string, std::vector<int64_t>> eventMaps, const int32_t formVisibleType)
1072 {
1073     FormMgrAdapter::GetInstance().HandlerNotifyWhetherVisibleForms(formIds,
1074         formInstanceMaps, eventMaps, formVisibleType);
1075 }
1076 
1077 /**
1078  * @brief Post recycle forms.
1079  * @param formIds the Ids of forms to be recycled.
1080  * @param want The want of the request.
1081  * @param remoteObjectOfHost Form host proxy object.
1082  * @param remoteObjectOfRender Form render proxy object.
1083  */
PostRecycleForms(const std::vector<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObjectOfHost, const sptr<IRemoteObject> &remoteObjectOfRender)1084 void FormTaskMgr::PostRecycleForms(const std::vector<int64_t> &formIds, const Want &want,
1085     const sptr<IRemoteObject> &remoteObjectOfHost, const sptr<IRemoteObject> &remoteObjectOfRender)
1086 {
1087     HILOG_DEBUG("start");
1088     if (serialQueue_ == nullptr) {
1089         HILOG_ERROR("null serialQueue_");
1090         return;
1091     }
1092 
1093     auto delayTime = want.GetIntParam(Constants::FORM_DELAY_TIME_OF_RECYCLE, FORM_TASK_DELAY_TIME);
1094     for (const int64_t &formId : formIds) {
1095         auto recycleForm = [formId, remoteObjectOfHost, remoteObjectOfRender]() {
1096             FormTaskMgr::GetInstance().RecycleForm(formId, remoteObjectOfHost, remoteObjectOfRender);
1097         };
1098         serialQueue_->ScheduleDelayTask(
1099             std::make_pair((int64_t)TaskType::RECYCLE_FORM, formId), delayTime, recycleForm);
1100     }
1101     HILOG_DEBUG("end");
1102 }
1103 
1104 /**
1105  * @brief Handle recycle form message.
1106  * @param formId The Id of form to be recycled.
1107  * @param remoteObjectOfHost Form host proxy object.
1108  * @param remoteObjectOfRender Form render proxy object.
1109  */
RecycleForm(const int64_t &formId, const sptr<IRemoteObject> &remoteObjectOfHost, const sptr<IRemoteObject> &remoteObjectOfRender)1110 void FormTaskMgr::RecycleForm(const int64_t &formId, const sptr<IRemoteObject> &remoteObjectOfHost,
1111     const sptr<IRemoteObject> &remoteObjectOfRender)
1112 {
1113     HILOG_DEBUG("start");
1114 
1115     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObjectOfRender);
1116     if (remoteFormRender == nullptr) {
1117         HILOG_ERROR("fail get form render proxy, formId is %{public}" PRId64, formId);
1118         return;
1119     }
1120 
1121     FormRecord formRecord;
1122     if (!FormDataMgr::GetInstance().GetFormRecord(formId, formRecord)) {
1123         HILOG_ERROR("form %{public}" PRId64 " not exist", formId);
1124         return;
1125     }
1126     if (formRecord.recycleStatus != RecycleStatus::RECYCLABLE) {
1127         HILOG_ERROR("form %{public}" PRId64 " not RECYCLABLE", formId);
1128         return;
1129     }
1130 
1131     Want want;
1132     want.SetParam(Constants::FORM_SUPPLY_UID, std::to_string(formRecord.providerUserId) + formRecord.bundleName);
1133     want.SetParam(Constants::PARAM_FORM_HOST_TOKEN, remoteObjectOfHost);
1134     int32_t error = remoteFormRender->RecycleForm(formId, want);
1135     if (error != ERR_OK) {
1136         HILOG_ERROR("fail");
1137         return;
1138     }
1139 }
1140 
1141 /**
1142  * @brief Post recover forms.
1143  * @param formIds the Ids of forms to be recycled.
1144  * @param want The want of the request.
1145  * @param remoteObject Form render proxy object.
1146  */
PostRecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject)1147 void FormTaskMgr::PostRecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject)
1148 {
1149     HILOG_DEBUG("start");
1150     if (serialQueue_ == nullptr) {
1151         HILOG_ERROR("null serialQueue_");
1152         return;
1153     }
1154 
1155     auto recoverForm = [record, want, remoteObject]() {
1156         FormTaskMgr::GetInstance().RecoverForm(record, want, remoteObject);
1157     };
1158     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, recoverForm);
1159     HILOG_DEBUG("end");
1160 }
1161 
1162 /**
1163  * @brief Handle recover form message.
1164  * @param formId The Id of form to be recovered.
1165  * @param want The want of the request.
1166  * @param remoteObject Form render proxy object.
1167  */
RecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject)1168 void FormTaskMgr::RecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject)
1169 {
1170     HILOG_DEBUG("start");
1171     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
1172     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
1173     if (remoteFormRender == nullptr) {
1174         RemoveConnection(connectId);
1175         HILOG_ERROR("get formRenderProxy failed");
1176         return;
1177     }
1178 
1179     FormJsInfo formJsInfo = CreateFormJsInfo(record.formId, record);
1180     int32_t error = remoteFormRender->RecoverForm(formJsInfo, want);
1181     if (error != ERR_OK) {
1182         RemoveConnection(connectId);
1183         HILOG_ERROR("fail recover form");
1184         return;
1185     }
1186 
1187     HILOG_DEBUG("end");
1188 }
1189 /**
1190  * @brief Cancel delay task.
1191  * @param eventMsg Delay Task.
1192  */
CancelDelayTask(const std::pair<int64_t, int64_t> &eventMsg)1193 void FormTaskMgr::CancelDelayTask(const std::pair<int64_t, int64_t> &eventMsg)
1194 {
1195     HILOG_DEBUG("cancel delay task: <%{public}" PRId64",%{public}" PRId64">.",
1196         eventMsg.first, eventMsg.second);
1197     if (serialQueue_ == nullptr) {
1198         HILOG_ERROR("null serialQueue_");
1199         return;
1200     }
1201 
1202     serialQueue_->CancelDelayTask(eventMsg);
1203     HILOG_DEBUG("end");
1204 }
1205 
PostFormClickEventToHost( const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)1206 void FormTaskMgr::PostFormClickEventToHost(
1207     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject,
1208     const RunningFormInfo &runningFormInfo)
1209 {
1210     HILOG_DEBUG("call");
1211     if (serialQueue_ == nullptr) {
1212         HILOG_ERROR("serialQueue_ invalidate");
1213         return;
1214     }
1215     auto task = [bundleName, formEventType, remoteObject, runningFormInfo]() {
1216         if (remoteObject == nullptr) {
1217             HILOG_ERROR("null remoteObject");
1218             return;
1219         }
1220         FormTaskMgr::GetInstance().FormClickEvent(bundleName, formEventType, remoteObject, runningFormInfo);
1221     };
1222     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, task);
1223 }
1224 
FormClickEvent(const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)1225 void FormTaskMgr::FormClickEvent(const std::string &bundleName, const std::string &formEventType,
1226     const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
1227 {
1228     HILOG_DEBUG("call");
1229     sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
1230         iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
1231     if (remoteJsFormStateObserver == nullptr) {
1232         HILOG_ERROR("fail get js form state observer proxy");
1233         return;
1234     }
1235 
1236     remoteJsFormStateObserver->OnFormClickEvent(bundleName, formEventType, runningFormInfo);
1237 }
1238 
PostBatchRefreshForms(const int32_t formRefreshType)1239 void FormTaskMgr::PostBatchRefreshForms(const int32_t formRefreshType)
1240 {
1241     HILOG_DEBUG("start");
1242     if (serialQueue_ == nullptr) {
1243         HILOG_ERROR("null serialQueue_");
1244         return;
1245     }
1246 
1247     auto batchRefreshForms = [formRefreshType]() {
1248         return FormMgrAdapter::GetInstance().BatchRefreshForms(formRefreshType);
1249     };
1250     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, batchRefreshForms);
1251     HILOG_DEBUG("end");
1252 }
1253 
PostEnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable, const sptr<IRemoteObject> &remoteObject)1254 void FormTaskMgr::PostEnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
1255     const sptr<IRemoteObject> &remoteObject)
1256 {
1257     HILOG_DEBUG("call");
1258     if (serialQueue_ == nullptr) {
1259         HILOG_ERROR("serialQueue_ invalidate");
1260         return;
1261     }
1262 
1263     auto enableFormsTaskToHostFunc = [formIds, enable, remoteObject]() {
1264         FormTaskMgr::GetInstance().EnableFormsTaskToHost(formIds, enable, remoteObject);
1265     };
1266     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, enableFormsTaskToHostFunc);
1267 }
1268 
EnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable, const sptr<IRemoteObject> &remoteObject)1269 void FormTaskMgr::EnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
1270     const sptr<IRemoteObject> &remoteObject)
1271 {
1272     HILOG_DEBUG("start");
1273     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
1274     if (remoteFormHost == nullptr) {
1275         HILOG_ERROR("get formHostProxy failed");
1276         return;
1277     }
1278 
1279     remoteFormHost->OnEnableForm(formIds, enable);
1280     HILOG_DEBUG("end");
1281 }
1282 
PostTask(const std::function<void()> &func, uint64_t delayMs)1283 void FormTaskMgr::PostTask(const std::function<void()> &func, uint64_t delayMs)
1284 {
1285     if (!func) {
1286         HILOG_ERROR("Invalid input function");
1287         return;
1288     }
1289 
1290     if (serialQueue_ == nullptr) {
1291         HILOG_ERROR("Invalid serialQueue_");
1292         return;
1293     }
1294 
1295     serialQueue_->ScheduleTask(delayMs, func);
1296 }
1297 
PostFrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)1298 void FormTaskMgr::PostFrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)
1299 {
1300     if (serialQueue_ == nullptr) {
1301         HILOG_ERROR("serialQueue_ invalidate");
1302         return;
1303     }
1304     auto task = [remoteObject]() {
1305         FormTaskMgr::GetInstance().FrsDiedTaskToHost(remoteObject);
1306     };
1307     serialQueue_->ScheduleTask(FORM_FRS_DIED_TASK_DELAY_TIME, task);
1308 }
1309 
FrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)1310 void FormTaskMgr::FrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)
1311 {
1312     HILOG_INFO("call");
1313 
1314     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
1315     if (remoteFormHost == nullptr) {
1316         HILOG_ERROR("get formHostProxy failed");
1317         return;
1318     }
1319 
1320     remoteFormHost->OnError(ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED, "FormRenderService is dead.");
1321 }
1322 } // namespace AppExecFwk
1323 } // namespace OHOS
1324