• 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_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_interface.h"
25 #include "form_mgr_adapter.h"
26 #include "form_mgr_errors.h"
27 #include "form_provider_interface.h"
28 #include "form_render_interface.h"
29 #include "form_serial_queue.h"
30 #include "form_share_mgr.h"
31 #include "form_supply_callback.h"
32 #include "js_form_state_observer_interface.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 namespace {
37 constexpr int32_t FORM_TASK_DELAY_TIME = 20; // ms
38 } // namespace
FormTaskMgr()39 FormTaskMgr::FormTaskMgr() {}
~FormTaskMgr()40 FormTaskMgr::~FormTaskMgr() {}
41 /**
42  * @brief Acquire form data from form provider(task).
43  * @param formId The Id of the form.
44  * @param want The want of the request.
45  * @param remoteObject Form provider proxy object.
46  */
PostAcquireTask(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)47 void FormTaskMgr::PostAcquireTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
48 {
49     if (serialQueue_ == nullptr) {
50         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate", __func__);
51         return;
52     }
53     auto acquireProviderFormInfoFunc = [formId, want, remoteObject]() {
54         FormTaskMgr::GetInstance().AcquireProviderFormInfo(formId, want, remoteObject);
55     };
56     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireProviderFormInfoFunc);
57 }
58 
PostShareAcquireTask(int64_t formId,const std::string & remoteDeviceId,const Want & want,const sptr<IRemoteObject> & remoteObject)59 void FormTaskMgr::PostShareAcquireTask(int64_t formId, const std::string &remoteDeviceId, const Want &want,
60     const sptr<IRemoteObject> &remoteObject)
61 {
62     if (serialQueue_ == nullptr) {
63         HILOG_ERROR("serialQueue_ is nullptr");
64         int64_t requestCode = static_cast<int64_t>(want.GetLongParam(Constants::FORM_SHARE_REQUEST_CODE, 0));
65         PostFormShareSendResponse(requestCode, ERR_APPEXECFWK_FORM_COMMON_CODE);
66         return;
67     }
68 
69     auto acquireShareProviderFormInfoFunc = [formId, remoteDeviceId, want, remoteObject]() {
70         FormTaskMgr::GetInstance().AcquireShareFormData(formId, remoteDeviceId, want, remoteObject);
71     };
72     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireShareProviderFormInfoFunc);
73 }
74 /**
75  * @brief Delete form data from form provider(task).
76  * @param formId The Id of the form.
77  * @param want The want of the request.
78  * @param remoteObject Form provider proxy object.
79  */
PostDeleteTask(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)80 void FormTaskMgr::PostDeleteTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
81 {
82     if (serialQueue_ == nullptr) {
83         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate", __func__);
84         return;
85     }
86     auto notifyFormDeleteFunc = [formId, want, remoteObject]() {
87         FormTaskMgr::GetInstance().NotifyFormDelete(formId, want, remoteObject);
88     };
89     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyFormDeleteFunc);
90 }
91 
92 /**
93  * @brief Refresh form data from form provider(task).
94  *
95  * @param formId The Id of the form.
96  * @param want The want of the form.
97  * @param remoteObject Form provider proxy object.
98  * @return none.
99  */
PostRefreshTask(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)100 void FormTaskMgr::PostRefreshTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
101 {
102     if (serialQueue_ == nullptr) {
103         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
104         return;
105     }
106     auto notifyFormUpdateFunc = [formId, want, remoteObject]() {
107         FormTaskMgr::GetInstance().NotifyFormUpdate(formId, want, remoteObject);
108     };
109     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyFormUpdateFunc);
110 }
111 
112 /**
113  * @brief Cast temp form data from form provider(task).
114  *
115  * @param formId The Id of the form.
116  * @param want The want of the form.
117  * @param remoteObject Form provider proxy object.
118  * @return none.
119  */
PostCastTempTask(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)120 void FormTaskMgr::PostCastTempTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
121 {
122     if (serialQueue_ == nullptr) {
123         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate", __func__);
124         return;
125     }
126     auto notifyCastTempFunc = [formId, want, remoteObject]() {
127         FormTaskMgr::GetInstance().NotifyCastTemp(formId, want, remoteObject);
128     };
129     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, notifyCastTempFunc);
130 }
131 
132 /**
133  * @brief Post form data to form host(task) when acquire form.
134  * @param formId The Id of the form.
135  * @param callingUid Calling uid.
136  * @param info Form configure info.
137  * @param wantParams WantParams of the request.
138  * @param remoteObject Form provider proxy object.
139  */
PostAcquireTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)140 void FormTaskMgr::PostAcquireTaskToHost(const int64_t formId, const FormRecord &record,
141     const sptr<IRemoteObject> &remoteObject)
142 {
143     if (serialQueue_ == nullptr) {
144         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate", __func__);
145         return;
146     }
147     auto acquireTaskToHostFunc = [formId, record, remoteObject]() {
148         FormTaskMgr::GetInstance().AcquireTaskToHost(formId, record, remoteObject);
149     };
150     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
151 }
152 
PostAcquireDataTaskToHost(const AAFwk::WantParams & wantParams,int64_t requestCode,const sptr<IRemoteObject> & remoteObject)153 void FormTaskMgr::PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams,
154     int64_t requestCode, const sptr<IRemoteObject> &remoteObject)
155 {
156     if (serialQueue_ == nullptr) {
157         HILOG_ERROR("failed, serialQueue_ invalidate");
158         return;
159     }
160     auto acquireTaskToHostFunc = [wantParams, requestCode, remoteObject]() {
161         FormTaskMgr::GetInstance().AcquireFormDataBack(wantParams, requestCode, remoteObject);
162     };
163     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
164 }
165 
166 /**
167  * @brief Post form data to form host(task) when update form.
168  * @param formId The Id of the form.
169  * @param callingUid Calling uid.
170  * @param info Form configure info.
171  * @param wantParams WantParams of the request.
172  * @param remoteObject Form provider proxy object.
173  */
PostUpdateTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)174 void FormTaskMgr::PostUpdateTaskToHost(const int64_t formId, const FormRecord &record,
175     const sptr<IRemoteObject> &remoteObject)
176 {
177     HILOG_INFO("%{public}s called.", __func__);
178 
179     if (serialQueue_ == nullptr) {
180         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
181         return;
182     }
183 
184     HILOG_DEBUG("%{public}s, post the task of updateTaskToHostFunc.", __func__);
185     auto updateTaskToHostFunc = [formId, record, remoteObject]() {
186         FormTaskMgr::GetInstance().UpdateTaskToHost(formId, record, remoteObject);
187     };
188     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, updateTaskToHostFunc);
189 }
190 
191 /**
192  * @brief Acquire form data from form provider.
193  * @param formId The Id of the form.
194  * @param info Form configure info.
195  * @param wantParams WantParams of the request.
196  * @param remoteObject Form provider proxy object.
197  */
198 /**
199  * @brief Handle form host died(task).
200  * @param remoteHost Form host proxy object.
201  */
PostHostDiedTask(const sptr<IRemoteObject> & remoteHost)202 void FormTaskMgr::PostHostDiedTask(const sptr<IRemoteObject> &remoteHost)
203 {
204     if (serialQueue_ == nullptr) {
205         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate", __func__);
206         return;
207     }
208     auto postTaskFunc = [remoteHost]() {
209         FormTaskMgr::GetInstance().HostDied(remoteHost);
210     };
211     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, postTaskFunc);
212 }
213 
214 /**
215  * @brief Post event notify to form provider.
216  *
217  * @param formEvent The vector of form ids.
218  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
219  * @param want The want of the form.
220  * @param remoteObject The form provider proxy object.
221  * @return none.
222  */
PostEventNotifyTask(const std::vector<int64_t> & formEvent,const int32_t formVisibleType,const Want & want,const sptr<IRemoteObject> & remoteObject)223 void FormTaskMgr::PostEventNotifyTask(const std::vector<int64_t> &formEvent, const int32_t formVisibleType,
224     const Want &want, const sptr<IRemoteObject> &remoteObject)
225 {
226     if (serialQueue_ == nullptr) {
227         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
228         return;
229     }
230     auto eventNotifyFunc = [formEvent, formVisibleType, want, remoteObject]() {
231         FormTaskMgr::GetInstance().EventNotify(formEvent, formVisibleType, want, remoteObject);
232     };
233     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, eventNotifyFunc);
234 }
235 /**
236  * @brief Post provider batch delete.
237  * @param formIds The Id list.
238  * @param want The want of the request.
239  * @param remoteObject Form provider proxy object.
240  */
PostProviderBatchDeleteTask(std::set<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & remoteObject)241 void FormTaskMgr::PostProviderBatchDeleteTask(std::set<int64_t> &formIds, const Want &want,
242     const sptr<IRemoteObject> &remoteObject)
243 {
244     if (serialQueue_ == nullptr) {
245         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
246         return;
247     }
248     auto batchDeleteFunc = [&formIds, want, remoteObject]() {
249         FormTaskMgr::GetInstance().ProviderBatchDelete(formIds, want, remoteObject);
250     };
251     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, batchDeleteFunc);
252 }
253 /**
254  * @brief Post message event to form provider.
255  * @param formId The Id of the from.
256  * @param message Event message.
257  * @param want The want of the request.
258  * @param remoteObject Form provider proxy object.
259  */
PostFormEventTask(const int64_t formId,const std::string & message,const Want & want,const sptr<IRemoteObject> & remoteObject)260 void FormTaskMgr::PostFormEventTask(const int64_t formId, const std::string &message,
261     const Want &want, const sptr<IRemoteObject> &remoteObject)
262 {
263     if (serialQueue_ == nullptr) {
264         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
265         return;
266     }
267     auto formEventFunc = [formId, message, want, remoteObject]() {
268         FormTaskMgr::GetInstance().FireFormEvent(formId, message, want, remoteObject);
269     };
270     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, formEventFunc);
271 }
272 
273 /**
274 * @brief Post acquire state to form provider.
275 * @param wantArg The want of onAcquireFormState.
276 * @param provider The provider info.
277 * @param want The want of the request.
278 * @param remoteObject Form provider proxy object.
279 */
PostAcquireStateTask(const Want & wantArg,const std::string & provider,const Want & want,const sptr<IRemoteObject> & remoteObject)280 void FormTaskMgr::PostAcquireStateTask(const Want &wantArg, const std::string &provider, const Want &want,
281                                        const sptr<IRemoteObject> &remoteObject)
282 {
283     if (serialQueue_ == nullptr) {
284         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
285         return;
286     }
287     auto acquireStateFunc = [wantArg, provider, want, remoteObject]() {
288         FormTaskMgr::GetInstance().AcquireState(wantArg, provider, want, remoteObject);
289     };
290     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireStateFunc);
291 }
292 
293 /**
294 * @brief Post acquire data to form provider.
295 * @param formId The Id of the from.
296 * @param want The want of the request.
297 * @param remoteObject Form provider proxy object.
298 */
PostAcquireDataTask(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)299 void FormTaskMgr::PostAcquireDataTask(const int64_t formId, const Want &want,
300                                       const sptr<IRemoteObject> &remoteObject)
301 {
302     if (serialQueue_ == nullptr) {
303         HILOG_ERROR("fail, serialQueue_ invalidate.");
304         return;
305     }
306     auto acquireDataFunc = [formId, want, remoteObject]() {
307         FormTaskMgr::GetInstance().AcquireFormData(formId, want, remoteObject);
308     };
309     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireDataFunc);
310 }
311 
312 /**
313  * @brief Post uninstall message to form host(task).
314  * @param formIds The Id list of the forms.
315  * @param remoteObject Form provider proxy object.
316  */
PostUninstallTaskToHost(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & remoteObject)317 void FormTaskMgr::PostUninstallTaskToHost(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject)
318 {
319     HILOG_INFO("%{public}s start", __func__);
320     if (serialQueue_ == nullptr) {
321         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
322         return;
323     }
324     auto uninstallFunc = [formIds, remoteObject]() {
325         FormTaskMgr::GetInstance().FormUninstall(formIds, remoteObject);
326     };
327     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, uninstallFunc);
328     HILOG_INFO("%{public}s end", __func__);
329 }
330 
331 /**
332 * @brief Post acquire form state message to form host(task).
333 * @param state The form state.
334 * @param want The want of onAcquireFormState.
335 * @param remoteObject Form provider proxy object.
336 */
PostAcquireStateTaskToHost(AppExecFwk::FormState state,const AAFwk::Want & want,const sptr<IRemoteObject> & remoteObject)337 void FormTaskMgr::PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want,
338                                              const sptr<IRemoteObject> &remoteObject)
339 {
340     HILOG_INFO("%{public}s start", __func__);
341     if (serialQueue_ == nullptr) {
342         HILOG_ERROR("%{public}s fail, serialQueue_ invalidate.", __func__);
343         return;
344     }
345     auto acquireStateFunc = [state, want, remoteObject]() {
346         FormTaskMgr::GetInstance().AcquireStateBack(state, want, remoteObject);
347     };
348     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, acquireStateFunc);
349     HILOG_INFO("%{public}s end", __func__);
350 }
351 
PostFormShareSendResponse(int64_t formShareRequestCode,int32_t result)352 void FormTaskMgr::PostFormShareSendResponse(int64_t formShareRequestCode, int32_t result)
353 {
354     HILOG_INFO("%{public}s start", __func__);
355     if (serialQueue_ == nullptr) {
356         HILOG_ERROR("serialQueue_ is nullptr.");
357         return;
358     }
359 
360     auto formShareSendResponseFunc = [formShareRequestCode, result]() {
361         FormTaskMgr::GetInstance().FormShareSendResponse(formShareRequestCode, result);
362     };
363     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, formShareSendResponseFunc);
364     HILOG_INFO("%{public}s end", __func__);
365 }
366 
PostAddTaskToHost(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)367 void FormTaskMgr::PostAddTaskToHost(const std::string bundleName,
368     const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
369 {
370     HILOG_DEBUG("start");
371     if (serialQueue_ == nullptr) {
372         HILOG_ERROR("fail, serialQueue_ invalidate.");
373         return;
374     }
375     auto addFunc = [bundleName, remoteObject, runningFormInfo]() {
376         FormTaskMgr::GetInstance().FormAdd(bundleName, remoteObject, runningFormInfo);
377     };
378     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, addFunc);
379     HILOG_DEBUG("end");
380 }
381 
PostRemoveTaskToHost(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)382 void FormTaskMgr::PostRemoveTaskToHost(const std::string bundleName,
383     const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
384 {
385     HILOG_DEBUG("start");
386     if (serialQueue_ == nullptr) {
387         HILOG_ERROR("fail, serialQueue_ invalidate.");
388         return;
389     }
390     auto removeFunc = [bundleName, remoteObject, runningFormInfo]() {
391         FormTaskMgr::GetInstance().FormRemove(bundleName, remoteObject, runningFormInfo);
392     };
393     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, removeFunc);
394     HILOG_DEBUG("end");
395 }
396 
FormAdd(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)397 void FormTaskMgr::FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
398     const RunningFormInfo &runningFormInfo)
399 {
400     HILOG_DEBUG("start");
401     sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
402         iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
403     if (remoteJsFormStateObserver == nullptr) {
404         HILOG_ERROR("fail, Failed to get js form state observer proxy.");
405         return;
406     }
407     remoteJsFormStateObserver->OnAddForm(bundleName, runningFormInfo);
408     HILOG_DEBUG("end");
409 }
410 
FormRemove(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)411 void FormTaskMgr::FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
412     const RunningFormInfo &runningFormInfo)
413 {
414     HILOG_DEBUG("start");
415     sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
416         iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
417     if (remoteJsFormStateObserver == nullptr) {
418         HILOG_ERROR("fail, Failed to get js form state observer proxy.");
419         return;
420     }
421     remoteJsFormStateObserver->OnRemoveForm(bundleName, runningFormInfo);
422     HILOG_DEBUG("end");
423 }
424 
AcquireProviderFormInfo(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)425 void FormTaskMgr::AcquireProviderFormInfo(const int64_t formId, const Want &want,
426     const sptr<IRemoteObject> &remoteObject)
427 {
428     FormMgrAdapter::GetInstance().AcquireProviderFormInfo(formId, want, remoteObject);
429 }
430 
AcquireShareFormData(int64_t formId,const std::string & remoteDeviceId,const Want & want,const sptr<IRemoteObject> & remoteObject)431 void FormTaskMgr::AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId,
432     const Want &want, const sptr<IRemoteObject> &remoteObject)
433 {
434     DelayedSingleton<FormShareMgr>::GetInstance()->AcquireShareFormData(formId, remoteDeviceId, want, remoteObject);
435 }
436 /**
437  * @brief Notify form provider for delete form.
438  *
439  * @param formId The Id of the from.
440  * @param want The want of the form.
441  * @param remoteObject Form provider proxy object.
442  * @return none.
443  */
NotifyFormDelete(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)444 void FormTaskMgr::NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
445 {
446     FormMgrAdapter::GetInstance().NotifyFormDelete(formId, want, remoteObject);
447 }
448 
449 /**
450  * @brief Notify form provider for updating form.
451  *
452  * @param formId The Id of the from.
453  * @param want The want of the form.
454  * @param remoteObject Form provider proxy object.
455  * @return none.
456  */
NotifyFormUpdate(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)457 void FormTaskMgr::NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
458 {
459     HILOG_INFO("%{public}s called.", __func__);
460 
461     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
462     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
463     if (formProviderProxy == nullptr) {
464         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
465         HILOG_ERROR("%{public}s fail, failed to get formProviderProxy", __func__);
466         return;
467     }
468     int error = formProviderProxy->NotifyFormUpdate(formId, want, FormSupplyCallback::GetInstance());
469     if (error != ERR_OK) {
470         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
471         HILOG_ERROR("%{public}s fail, Failed to notify form update.", __func__);
472     }
473 }
474 
475 /**
476  * @brief Event notify to form provider.
477  *
478  * @param formEvents The vector of form ids.
479  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
480  * @param want The want of the form.
481  * @param remoteObject The form provider proxy object.
482  * @return none.
483  */
EventNotify(const std::vector<int64_t> & formEvents,const int32_t formVisibleType,const Want & want,const sptr<IRemoteObject> & remoteObject)484 void FormTaskMgr::EventNotify(const std::vector<int64_t> &formEvents, const int32_t formVisibleType,
485     const Want &want, const sptr<IRemoteObject> &remoteObject)
486 {
487     HILOG_INFO("%{public}s called.", __func__);
488 
489     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
490     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
491     if (formProviderProxy == nullptr) {
492         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
493         HILOG_ERROR("%{public}s fail, failed to get formProviderProxy", __func__);
494         return;
495     }
496 
497     int error = formProviderProxy->EventNotify(formEvents, formVisibleType, want, FormSupplyCallback::GetInstance());
498     if (error != ERR_OK) {
499         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
500         HILOG_ERROR("%{public}s fail, Failed to send event notify.", __func__);
501     }
502 }
503 
504 /**
505  * @brief Notify form provider for cast temp form.
506  *
507  * @param formId The Id of the from.
508  * @param want The want of the form.
509  * @param remoteObject Form provider proxy object.
510  * @return none.
511  */
NotifyCastTemp(const int64_t formId,const Want & want,const sptr<IRemoteObject> & remoteObject)512 void FormTaskMgr::NotifyCastTemp(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject)
513 {
514     HILOG_INFO("%{public}s called.", __func__);
515 
516     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
517     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
518     if (formProviderProxy == nullptr) {
519         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
520         HILOG_ERROR("%{public}s fail, failed to get formProviderProxy", __func__);
521         return;
522     }
523 
524     int error = formProviderProxy->NotifyFormCastTempForm(formId, want, FormSupplyCallback::GetInstance());
525     if (error != ERR_OK) {
526         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
527         HILOG_ERROR("%{public}s fail, Failed to get acquire provider form info", __func__);
528     }
529 }
530 
531 /**
532  * @brief Post form data to form host when acquire form.
533  * @param formId The Id of the form.
534  * @param callingUid Calling uid.
535  * @param info Form configure info.
536  * @param wantParams WantParams of the request.
537  * @param remoteObject Form provider proxy object.
538  */
AcquireTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)539 void FormTaskMgr::AcquireTaskToHost(const int64_t formId, const FormRecord &record,
540     const sptr<IRemoteObject> &remoteObject)
541 {
542     HILOG_INFO("FormTaskMgr AcquireTaskToHost, formId:%{public}" PRId64 "", formId);
543 
544     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
545     if (remoteFormHost == nullptr) {
546         HILOG_ERROR("%{public}s fail, Failed to get form host proxy", __func__);
547         return;
548     }
549 
550     HILOG_DEBUG("FormTaskMgr remoteFormHost OnAcquired");
551     remoteFormHost->OnAcquired(CreateFormJsInfo(formId, record), nullptr);
552 }
553 
554 /**
555  * @brief Post form data to form host when update form.
556  * @param formId The Id of the form.
557  * @param callingUid Calling uid.
558  * @param info Form configure info.
559  * @param wantParams WantParams of the request.
560  * @param remoteObject Form provider proxy object.
561  */
UpdateTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)562 void FormTaskMgr::UpdateTaskToHost(const int64_t formId, const FormRecord &record,
563     const sptr<IRemoteObject> &remoteObject)
564 {
565     HILOG_INFO("%{public}s start.", __func__);
566 
567     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
568     if (remoteFormHost == nullptr) {
569         HILOG_ERROR("%{public}s fail, Failed to get form host proxy.", __func__);
570         return;
571     }
572 
573     HILOG_DEBUG("%{public}s, FormTaskMgr remoteFormHost OnUpdate.", __func__);
574     remoteFormHost->OnUpdate(CreateFormJsInfo(formId, record));
575 
576     HILOG_INFO("%{public}s end.", __func__);
577 }
578 
579 /**
580  * @brief Handle form host died.
581  * @param remoteHost Form host proxy object.
582  */
HostDied(const sptr<IRemoteObject> & remoteHost)583 void FormTaskMgr::HostDied(const sptr<IRemoteObject> &remoteHost)
584 {
585     HILOG_INFO("%{public}s, remote client died event", __func__);
586     if (remoteHost == nullptr) {
587         HILOG_INFO("%{public}s, remote client died, invalid param", __func__);
588         return;
589     }
590     FormDataMgr::GetInstance().HandleHostDied(remoteHost);
591     FormSupplyCallback::GetInstance()->HandleHostDied(remoteHost);
592 }
593 /**
594  * @brief Post provider batch delete.
595  * @param formIds The Id list.
596  * @param want The want of the request.
597  * @param remoteObject Form provider proxy object.
598  */
ProviderBatchDelete(std::set<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & remoteObject)599 void FormTaskMgr::ProviderBatchDelete(std::set<int64_t> &formIds, const Want &want,
600     const sptr<IRemoteObject> &remoteObject)
601 {
602     HILOG_INFO("%{public}s called.", __func__);
603     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
604     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
605     if (formProviderProxy == nullptr) {
606         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
607         HILOG_ERROR("%{public}s fail, Failed to get formProviderProxy", __func__);
608         return;
609     }
610     std::vector<int64_t> vFormIds;
611     vFormIds.assign(formIds.begin(), formIds.end());
612     int error = formProviderProxy->NotifyFormsDelete(vFormIds, want, FormSupplyCallback::GetInstance());
613     if (error != ERR_OK) {
614         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
615         HILOG_ERROR("%{public}s failed", __func__);
616     }
617 }
618 /**
619  * @brief Fire message event to form provider.
620  * @param formId The Id of the from.
621  * @param message Event message.
622  * @param want The want of the request.
623  * @param remoteObject Form provider proxy object.
624  */
FireFormEvent(const int64_t formId,const std::string & message,const Want & want,const sptr<IRemoteObject> & remoteObject)625 void FormTaskMgr::FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
626     const sptr<IRemoteObject> &remoteObject)
627 {
628     HILOG_INFO("%{public}s start", __func__);
629     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
630     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
631     if (formProviderProxy == nullptr) {
632         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
633         HILOG_ERROR("%{public}s, Failed to get formProviderProxy", __func__);
634         return;
635     }
636 
637     int error = formProviderProxy->FireFormEvent(formId, message, want, FormSupplyCallback::GetInstance());
638     if (error != ERR_OK) {
639         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
640         HILOG_ERROR("%{public}s, Failed to fire message event to form provider", __func__);
641     }
642     HILOG_INFO("%{public}s end", __func__);
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("%{public}s start", __func__);
656     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
657     sptr<IFormProvider> formProviderProxy = iface_cast<IFormProvider>(remoteObject);
658     if (formProviderProxy == nullptr) {
659     FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
660         HILOG_ERROR("%{public}s, Failed to get formProviderProxy", __func__);
661         return;
662     }
663 
664     int error = formProviderProxy->AcquireState(wantArg, provider, want, FormSupplyCallback::GetInstance());
665     if (error != ERR_OK) {
666         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
667         HILOG_ERROR("%{public}s, Failed to acquire form state to form provider", __func__);
668     }
669     HILOG_INFO("%{public}s end", __func__);
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         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
686         HILOG_ERROR("Failed to get formProviderProxy");
687         return;
688     }
689 
690     int error = formProviderProxy->AcquireFormData(formId, FormSupplyCallback::GetInstance(), requestCode);
691     if (error != ERR_OK) {
692         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
693         HILOG_ERROR("Failed to acquire form state to form provider");
694     }
695     FormSupplyCallback::GetInstance()->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("%{public}s start", __func__);
708     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
709     if (remoteFormHost == nullptr) {
710         HILOG_ERROR("%{public}s fail, Failed to get form host proxy.", __func__);
711         return;
712     }
713 
714     remoteFormHost->OnUninstall(formIds);
715 
716     HILOG_INFO("%{public}s end", __func__);
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("%{public}s start", __func__);
729     sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
730     if (remoteFormHost == nullptr) {
731         HILOG_ERROR("%{public}s fail, Failed to get form host proxy.", __func__);
732         return;
733     }
734 
735     remoteFormHost->OnAcquireState(state, want);
736 
737     HILOG_INFO("%{public}s end", __func__);
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("fail, Failed to get form host proxy.");
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_INFO("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     HILOG_DEBUG("%{public}s end, jsPath: %{private}s, data: %{public}s", __func__,
786         form.jsFormCodePath.c_str(), form.formData.c_str());
787     return form;
788 }
789 
FormShareSendResponse(int64_t formShareRequestCode,int32_t result)790 void FormTaskMgr::FormShareSendResponse(int64_t formShareRequestCode, int32_t result)
791 {
792     DelayedSingleton<FormShareMgr>::GetInstance()->SendResponse(formShareRequestCode, result);
793 }
794 
PostRenderForm(const FormRecord & formRecord,const Want & want,const sptr<IRemoteObject> & remoteObject)795 void FormTaskMgr::PostRenderForm(const FormRecord &formRecord, const Want &want,
796     const sptr<IRemoteObject> &remoteObject)
797 {
798     HILOG_INFO("PostRenderForm");
799     if (serialQueue_ == nullptr) {
800         HILOG_ERROR("serialQueue_ is nullptr.");
801         return;
802     }
803 
804     auto renderForm = [formRecord, want, remoteObject]() {
805         FormTaskMgr::GetInstance().RenderForm(formRecord, want, remoteObject);
806     };
807     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, renderForm);
808     HILOG_INFO("%{public}s end", __func__);
809 }
810 
RenderForm(const FormRecord & formRecord,const Want & want,const sptr<IRemoteObject> & remoteObject)811 void FormTaskMgr::RenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
812 {
813     HILOG_INFO("render form");
814     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
815     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
816     if (remoteFormRender == nullptr) {
817         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
818         HILOG_ERROR("%{public}s fail, Failed to get form render proxy.", __func__);
819         return;
820     }
821 
822     FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
823     int32_t error = remoteFormRender->RenderForm(formJsInfo, want, FormSupplyCallback::GetInstance());
824     if (error != ERR_OK) {
825         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
826         HILOG_ERROR("%{public}s fail, Failed to add form renderer", __func__);
827         return;
828     }
829 
830     HILOG_INFO("%{public}s end", __func__);
831 }
832 
PostStopRenderingForm(const FormRecord & formRecord,const Want & want,const sptr<IRemoteObject> & remoteObject)833 void FormTaskMgr::PostStopRenderingForm(
834     const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
835 {
836     HILOG_INFO("PostStopRenderingForm");
837     if (serialQueue_ == nullptr) {
838         HILOG_ERROR("serialQueue_ is nullptr.");
839         return;
840     }
841 
842     auto deleterenderForm = [formRecord, want, remoteObject]() {
843         FormTaskMgr::GetInstance().StopRenderingForm(formRecord, want, remoteObject);
844     };
845     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, deleterenderForm);
846     HILOG_INFO("%{public}s end", __func__);
847 }
848 
StopRenderingForm(const FormRecord & formRecord,const Want & want,const sptr<IRemoteObject> & remoteObject)849 void FormTaskMgr::StopRenderingForm(
850     const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject)
851 {
852     HILOG_INFO("%{public}s begin", __func__);
853     auto connectId = want.GetIntParam(Constants::FORM_CONNECT_ID, 0);
854     sptr<IFormRender> remoteFormDeleteRender = iface_cast<IFormRender>(remoteObject);
855     if (remoteFormDeleteRender == nullptr) {
856         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
857         HILOG_ERROR("%{public}s fail, Failed to get form render proxy.", __func__);
858         return;
859     }
860 
861     FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
862     int32_t error = remoteFormDeleteRender->StopRenderingForm(formJsInfo, want, FormSupplyCallback::GetInstance());
863     if (error != ERR_OK) {
864         FormSupplyCallback::GetInstance()->RemoveConnection(connectId);
865         HILOG_ERROR("%{public}s fail, Failed to add form renderer", __func__);
866         return;
867     }
868 
869     HILOG_INFO("%{public}s end", __func__);
870 }
871 
PostReleaseRenderer(int64_t formId,const std::string & compId,const std::string & uid,const sptr<IRemoteObject> & remoteObject)872 void FormTaskMgr::PostReleaseRenderer(
873     int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)
874 {
875     HILOG_INFO("%{public}s begin", __func__);
876     if (serialQueue_ == nullptr) {
877         HILOG_ERROR("serialQueue_ is nullptr.");
878         return;
879     }
880 
881     auto deleterenderForm = [formId, compId, uid, remoteObject]() {
882         FormTaskMgr::GetInstance().ReleaseRenderer(formId, compId, uid, remoteObject);
883     };
884     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, deleterenderForm);
885     HILOG_INFO("%{public}s end", __func__);
886 }
887 
ReleaseRenderer(int64_t formId,const std::string & compId,const std::string & uid,const sptr<IRemoteObject> & remoteObject)888 void FormTaskMgr::ReleaseRenderer(
889     int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject)
890 {
891     HILOG_INFO("%{public}s begin", __func__);
892     sptr<IFormRender> remoteFormDeleteRender = iface_cast<IFormRender>(remoteObject);
893     if (remoteFormDeleteRender == nullptr) {
894         HILOG_ERROR("%{public}s fail, Failed to get form render proxy.", __func__);
895         return;
896     }
897 
898     int32_t error = remoteFormDeleteRender->ReleaseRenderer(formId, compId, uid);
899     if (error != ERR_OK) {
900         HILOG_ERROR("%{public}s fail, Failed to release form renderer", __func__);
901         return;
902     }
903     HILOG_INFO("%{public}s end", __func__);
904 }
905 
ReloadForm(const std::vector<FormRecord> && formRecords,const Want & want,const sptr<IRemoteObject> & remoteObject)906 void FormTaskMgr::ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
907     const sptr<IRemoteObject> &remoteObject)
908 {
909     HILOG_INFO("%{public}s begin", __func__);
910     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
911     if (remoteFormRender == nullptr) {
912         HILOG_ERROR("%{public}s fail, Failed to get form render proxy.", __func__);
913         return;
914     }
915 
916     std::vector<FormJsInfo> formJsInfos;
917     for (const auto &formRecord : formRecords) {
918         FormJsInfo formJsInfo = CreateFormJsInfo(formRecord.formId, formRecord);
919         formJsInfos.emplace_back(formJsInfo);
920     }
921 
922     int32_t error = remoteFormRender->ReloadForm(std::move(formJsInfos), want);
923     if (error != ERR_OK) {
924         HILOG_ERROR("%{public}s fail, Failed to reload form.", __func__);
925         return;
926     }
927     HILOG_INFO("%{public}s end", __func__);
928 }
929 
PostReloadForm(const std::vector<FormRecord> && formRecords,const Want & want,const sptr<IRemoteObject> & remoteObject)930 void FormTaskMgr::PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
931     const sptr<IRemoteObject> &remoteObject)
932 {
933     HILOG_INFO("%{public}s begin", __func__);
934     if (serialQueue_ == nullptr) {
935         HILOG_ERROR("serialQueue_ is nullptr.");
936         return;
937     }
938     auto reloadForm = [forms = std::forward<decltype(formRecords)>(formRecords), want, remoteObject]() {
939         FormTaskMgr::GetInstance().ReloadForm(std::move(forms), want, remoteObject);
940     };
941     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, reloadForm);
942     HILOG_INFO("%{public}s end", __func__);
943 }
944 
OnUnlock(const sptr<IRemoteObject> & remoteObject)945 void FormTaskMgr::OnUnlock(const sptr<IRemoteObject> &remoteObject)
946 {
947     HILOG_DEBUG("begin");
948     sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
949     if (remoteFormRender == nullptr) {
950         HILOG_ERROR("%{public}s fail, Failed to get form render proxy.", __func__);
951         return;
952     }
953     int32_t error = remoteFormRender->OnUnlock();
954     if (error != ERR_OK) {
955         HILOG_ERROR("%{public}s fail", __func__);
956         return;
957     }
958     HILOG_DEBUG("end");
959 }
960 
PostOnUnlock(const sptr<IRemoteObject> & remoteObject)961 void FormTaskMgr::PostOnUnlock(const sptr<IRemoteObject> &remoteObject)
962 {
963     HILOG_DEBUG("called");
964     if (serialQueue_ == nullptr) {
965         HILOG_ERROR("serialQueue_ is nullptr.");
966         return;
967     }
968     auto task = [remoteObject]() {
969         FormTaskMgr::GetInstance().OnUnlock(remoteObject);
970     };
971     serialQueue_->ScheduleTask(FORM_TASK_DELAY_TIME, task);
972     HILOG_DEBUG("end");
973 }
974 } // namespace AppExecFwk
975 } // namespace OHOS
976