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 #ifndef OHOS_FORM_FWK_FORM_TASK_MGR_H
17 #define OHOS_FORM_FWK_FORM_TASK_MGR_H
18 
19 #include <singleton.h>
20 #include <vector>
21 
22 #include "form_event_handler.h"
23 #include "form_js_info.h"
24 #include "form_record.h"
25 #include "form_serial_queue.h"
26 #include "form_state_info.h"
27 #include "iremote_object.h"
28 #include "running_form_info.h"
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 using Want = OHOS::AAFwk::Want;
34 using WantParams = OHOS::AAFwk::WantParams;
35 enum class TaskType : int64_t {
36     RECYCLE_FORM,
37 };
38 namespace {
39 constexpr int32_t FORM_TASK_DELAY_TIME = 20; // ms
40 constexpr int32_t FORM_FRS_DIED_TASK_DELAY_TIME = 100; // ms
41 }
42 /**
43  * @class FormTaskMgr
44  * form task manager.
45  */
46 class FormTaskMgr final : public DelayedRefSingleton<FormTaskMgr> {
47     DECLARE_DELAYED_REF_SINGLETON(FormTaskMgr)
48 
49 public:
50     DISALLOW_COPY_AND_MOVE(FormTaskMgr);
51 
52     /**
53      * @brief SetSerialQueue.
54      * @param serialQueue serial queue
55      */
SetSerialQueue(const std::shared_ptr<FormSerialQueue> &serialQueue)56     inline void SetSerialQueue(const std::shared_ptr<FormSerialQueue> &serialQueue)
57     {
58         serialQueue_ = serialQueue;
59     }
60 
61     /**
62      * @brief Acquire form data from form provider(task).
63      * @param formId The Id of the form.
64      * @param want The want of the request.
65      * @param remoteObject Form provider proxy object.
66      */
67     void PostAcquireTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
68 
69     /**
70      * @brief Acquire share form data from form provider(task).
71      * @param formId The Id of the form.
72      * @param remoteDeviceId The device ID to share.
73      * @param want The want of the request.
74      * @param remoteObject Form provider proxy object.
75      */
76     void PostShareAcquireTask(int64_t formId, const std::string &remoteDeviceId, const Want &want,
77         const sptr<IRemoteObject> &remoteObject);
78 
79     /**
80      * @brief Delete form data from form provider(task).
81      * @param formId The Id of the form.
82      * @param want The want of the request.
83      * @param remoteObject Form provider proxy object.
84      */
85     void PostDeleteTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
86    /**
87      * @brief Notify provider batch delete.
88      * @param formIds The Id list.
89      * @param want The want of the request.
90      * @param remoteObject Form provider proxy object.
91      */
92     void PostProviderBatchDeleteTask(std::set<int64_t> &formIds, const Want &want,
93         const sptr<IRemoteObject> &remoteObject);
94     /**
95      * @brief Refresh form data from form provider(task).
96      *
97      * @param formId The Id of the form.
98      * @param want The want of the form.
99      * @param remoteObject Form provider proxy object.
100      * @return none.
101      */
102     void PostRefreshTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
103 
104     /**
105      * @brief Cast temp form data from form provider(task).
106      *
107      * @param formId The Id of the form.
108      * @param want The want of the request.
109      * @param remoteObject Form provider proxy object.
110      * @return none.
111      */
112     void PostCastTempTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
113 
114     /**
115      * @brief Post form data to form host(task) when acquire form.
116      * @param formId The Id of the form.
117      * @param record form record.
118      * @param remoteObject Form provider proxy object.
119      */
120     void PostAcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject);
121 
122     /**
123      * @brief Post form data to form host(task) when update form.
124      * @param formId The Id of the form.
125      * @param record form record.
126      * @param remoteObject Form provider proxy object.
127      */
128     void PostUpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject);
129 
130     /**
131      * @brief Handel form host died(task).
132      * @param remoteHost Form host proxy object.
133      */
134     void PostHostDiedTask(const sptr<IRemoteObject> &remoteHost);
135 
136     /**
137      * @brief Post event notify to form provider.
138      *
139      * @param formEvent The vector of form ids.
140      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
141      * @param want The want of the form.
142      * @param remoteObject The form provider proxy object.
143      */
144     void PostEventNotifyTask(const std::vector<int64_t> &formEvent, const int32_t formVisibleType, const Want &want,
145         const sptr<IRemoteObject> &remoteObject);
146 
147     /**
148      * @brief Post message event to form provider.
149      * @param formId The Id of the from.
150      * @param message Event message.
151      * @param want The want of the request.
152      * @param remoteObject Form provider proxy object.
153      */
154     void PostFormEventTask(const int64_t formId, const std::string &message, const Want &want,
155         const sptr<IRemoteObject> &remoteObject);
156 
157     /**
158     * @brief Post acquire state to form provider.
159     * @param wantArg The want of onAcquireFormState.
160     * @param provider The provider info.
161     * @param want The want of the request.
162     * @param remoteObject Form provider proxy object.
163     */
164     void PostAcquireStateTask(const Want &wantArg, const std::string &provider, const Want &want,
165                               const sptr <IRemoteObject> &remoteObject);
166 
167     /**
168     * @brief Post acquire data to form provider.
169     * @param formId The Id of the from.
170     * @param want The want of the request.
171     * @param remoteObject Form provider proxy object.
172     */
173     void PostAcquireDataTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
174 
175      /**
176      * @brief Post uninstall message to form host(task).
177      * @param formIds The Id list of the forms.
178      * @param remoteObject Form provider proxy object.
179      */
180     void PostUninstallTaskToHost(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject);
181 
182     /**
183     * @brief Post acquire form state message to form host(task).
184     * @param state The form state.
185     * @param want The want of onAcquireFormState.
186     * @param remoteObject Form provider proxy object.
187     */
188     void PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want,
189                                     const sptr<IRemoteObject> &remoteObject);
190 
191     /**
192     * @brief Post acquire form data message to form host(task).
193     * @param wantParams Indicates the data information acquired by the form.
194     * @param requestCode Indicates the requested id.
195     * @param remoteObject Form provider proxy object.
196     */
197     void PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams, int64_t requestCode,
198                                     const sptr<IRemoteObject> &remoteObject);
199 
200     /**
201     * @brief Post form share error code to form host(task).
202     * @param formShareRequestCode The request code for this share.
203     * @param result The error code of this share.
204     */
205     void PostFormShareSendResponse(int64_t formShareRequestCode, int32_t result);
206 
207     void PostRenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject);
208 
209     void PostStopRenderingForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject);
210 
211     void PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
212         const sptr<IRemoteObject> &remoteObject);
213 
214     void PostAddTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
215         const RunningFormInfo &runningFormInfo);
216 
217     void PostRouterProxyToHost(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want);
218 
219     void PostRemoveTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
220         const RunningFormInfo &runningFormInfo);
221 
222     void PostReleaseRenderer(
223         int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject);
224 
225     void PostOnUnlock(const sptr<IRemoteObject> &remoteObject);
226 
227     /**
228     * @brief Post Form visible/invisible notify.
229     * @param formIds  the Ids of forms need to notify.
230     * @param formInstanceMaps formInstances for visibleNotify.
231     * @param eventMaps eventMaps for event notify.
232     * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
233     * @param visibleNotifyDelay delay time.
234     */
235     void PostVisibleNotify(const std::vector<int64_t> &formIds,
236         std::map<std::string, std::vector<FormInstance>> &formInstanceMaps,
237         std::map<std::string, std::vector<int64_t>> &eventMaps,
238         const int32_t formVisibleType, int32_t visibleNotifyDelay);
239 
240     /**
241     * @brief Post form click event.
242     * @param bundleName BundleName of the form host
243     * @param formEventType form event type.
244     * @param remoteObject thr remote observer.
245     * @param runningFormInfo Current form data.
246     */
247     void PostFormClickEventToHost(
248         const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject,
249         const RunningFormInfo &runningFormInfo);
250 
251     /**
252      * @brief Post recycle forms.
253      * @param formIds the Ids of forms to be recycled.
254      * @param want The want of the request.
255      * @param remoteObjectOfHost Form host proxy object.
256      * @param remoteObjectOfRender Form render proxy object.
257      */
258     void PostRecycleForms(const std::vector<int64_t> &formIds, const Want &want,
259         const sptr<IRemoteObject> &remoteObjectOfHost, const sptr<IRemoteObject> &remoteObjectOfRender);
260 
261     /**
262      * @brief Post recover form.
263      * @param formIds the Id of form to be recovered.
264      * @param want The want of the request.
265      * @param remoteObject Form render proxy object.
266      */
267     void PostRecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject);
268 
269     /**
270      * @brief Cancel delay task.
271      * @param eventMsg Delay Task.
272      */
273     void CancelDelayTask(const std::pair<int64_t, int64_t> &eventMsg);
274 
275     /**
276     * @brief Post BatchRefresh forms.
277     * @param formRefreshType batch refresh forms type.
278     */
279     void PostBatchRefreshForms(const int32_t formRefreshType);
280 
281     /**
282      * @brief Post enable form task.
283      * @param formIds formIds The Id list of the forms.
284      * @param enable True or false.
285      * @param remoteObject Form host proxy object.
286      */
287     void PostEnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
288         const sptr<IRemoteObject> &remoteObject);
289 
290     /**
291      * @brief Post task to excute.
292      * @param func Task function.
293      * @param delayMs Task excute delay time(ms).
294      */
295     void PostTask(const std::function<void()> &func, uint64_t delayMs = FORM_TASK_DELAY_TIME);
296 
297     /**
298      * @brief Post re-add form task to form host when FormRenderService is died.
299      * @param remoteObject Form host proxy object.
300      */
301     void PostFrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject);
302 
303 private:
304     /**
305      * @brief Acquire form data from form provider.
306      * @param formId The Id of the from.
307      * @param want The want of the request.
308      * @param remoteObject Form provider proxy object.
309      */
310     void AcquireProviderFormInfo(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
311 
312     /**
313      * @brief Acquire share form data from form provider(task).
314      * @param formId The Id of the form.
315      * @param remoteDeviceId The device ID to share.
316      * @param want The want of the request.
317      * @param remoteObject Form provider proxy object.
318      */
319     void AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId, const Want &want,
320         const sptr<IRemoteObject> &remoteObject);
321 
322     /**
323      * @brief Notify form provider for delete form.
324      * @param formId The Id of the from.
325      * @param want The want of the form.
326      * @param remoteObject Form provider proxy object.
327      */
328     void NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
329 
330     /**
331      * @brief Notify form provider for updating form.
332      * @param formId The Id of the from.
333      * @param want The want of the form.
334      * @param remoteObject Form provider proxy object.
335      */
336     void NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
337 
338     /**
339      * @brief Event notify to form provider.
340      *
341      * @param formEvents The vector of form ids.
342      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
343      * @param want The want of the form.
344      * @param remoteObject The form provider proxy object.
345      */
346     void EventNotify(const std::vector<int64_t> &formEvents, const int32_t formVisibleType, const Want &want,
347         const sptr<IRemoteObject> &remoteObject);
348 
349     /**
350      * @brief Notify form provider for cast temp form.
351      *
352      * @param formId The Id of the from.
353      * @param want The want of the form.
354      * @param remoteObject Form provider proxy object.
355      * @return none.
356      */
357     void NotifyCastTemp(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
358     /**
359      * @brief Post form data to form host when acquire form..
360      * @param formId The Id of the form.
361      * @param record form record.
362      * @param remoteObject Form provider proxy object.
363      */
364     void AcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject);
365 
366     /**
367      * @brief Post form data to form host when update form.
368      * @param formId The Id of the form.
369      * @param record form record.
370      * @param remoteObject Form provider proxy object.
371      */
372     void UpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject);
373 
374     /**
375      * @brief Handle form host died.
376      * @param remoteHost Form host proxy object.
377      */
378     void HostDied(const sptr<IRemoteObject> &remoteHost);
379 
380     /**
381      * @brief Post provider batch delete.
382      * @param formIds The Id list.
383      * @param want The want of the request.
384      * @param remoteObject Form provider proxy object.
385      */
386     void ProviderBatchDelete(std::set<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObject);
387     /**
388      * @brief Fire message event to form provider.
389      * @param formId The Id of the from.
390      * @param message Event message.
391      * @param want The want of the request.
392      * @param remoteObject Form provider proxy object.
393      */
394     void FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
395         const sptr<IRemoteObject> &remoteObject);
396 
397     /**
398      * @brief Acquire form state to form provider.
399      * @param wantArg The want of onAcquireFormState.
400      * @param provider The provider info.
401      * @param want The want of the request.
402      * @param remoteObject Form provider proxy object.
403      */
404     void AcquireState(const Want &wantArg, const std::string &provider, const Want &want,
405                       const sptr <IRemoteObject> &remoteObject);
406     /**
407      * @brief Handle acquire data.
408      * @param wantParams Indicates the data information acquired by the form.
409      * @param requestCode Indicates the requested id.
410      * @param remoteObject Form provider proxy object.
411      */
412     void AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode,
413                             const sptr<IRemoteObject> &remoteObject);
414 
415     /**
416      * @brief Acquire form data to form provider.
417      * @param formId The Id of the form.
418      * @param want The want of the request.
419      * @param remoteObject Form provider proxy object.
420      */
421     void AcquireFormData(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject);
422 
423     /**
424      * @brief Handle uninstall message.
425      * @param formIds The Id list of the forms.
426      * @param remoteObject Form provider proxy object.
427      */
428     void FormUninstall(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject);
429 
430     /**
431      * @brief Handle acquire state.
432      * @param state the form state.
433      * @param want The want of onAcquireFormState.
434      * @param remoteObject Form provider proxy object.
435      */
436     void AcquireStateBack(AppExecFwk::FormState state, const AAFwk::Want &want,
437                           const sptr <IRemoteObject> &remoteObject);
438 
439     /**
440      * @brief Create form data for form host.
441      * @param formId The Id of the form.
442      * @param record Form record.
443      * @return Form data.
444      */
445     FormJsInfo CreateFormJsInfo(const int64_t formId, const FormRecord &record);
446 
447     /**
448     * @brief Post form share error code to form host(task).
449     * @param formShareRequestCode The request code for this share.
450     * @param result The error code of this share.
451     */
452     void FormShareSendResponse(int64_t formShareRequestCode, int32_t result);
453 
454     /**
455     * @brief Post form share error code to form host(task).
456     * @param formShareRequestCode The request code for this share.
457     * @param result The error code of this share.
458     */
459     void RenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject);
460 
461     void StopRenderingForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject);
462 
463     void ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
464         const sptr<IRemoteObject> &remoteObject);
465 
466     void FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
467         const RunningFormInfo &runningFormInfo);
468 
469     void FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
470         const RunningFormInfo &runningFormInfo);
471 
472     void ReleaseRenderer(
473         int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject);
474 
475     void OnUnlock(const sptr<IRemoteObject> &remoteObject);
476 
477     void RemoveConnection(int32_t connectId);
478 
479     /**
480      * @brief Form router event proxy.
481      * @param formId The id of the form.
482      * @param remoteObject Form router proxy manager object.
483      * @param want The want of the form for router event.
484      */
485     void FormRouterEventProxy(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want);
486 
487     /**
488     * @brief Form visible/invisible notify.
489     * @param formIds  the Ids of forms need to notify.
490     * @param formInstanceMaps formInstances for visibleNotify.
491     * @param eventMaps eventMaps for event notify.
492     * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
493     */
494     void NotifyVisible(const std::vector<int64_t> &formIds,
495         std::map<std::string, std::vector<FormInstance>> formInstanceMaps,
496         std::map<std::string, std::vector<int64_t>> eventMaps, const int32_t formVisibleType);
497 
498     /**
499      * @brief Handle recycle form message.
500      * @param formId The Id of form to be recycled.
501      * @param remoteObjectOfHost Form host proxy object.
502      * @param remoteObjectOfRender Form render proxy object.
503      */
504     void RecycleForm(const int64_t &formId, const sptr<IRemoteObject> &remoteObjectOfHost,
505         const sptr<IRemoteObject> &remoteObjectOfRender);
506 
507     /**
508      * @brief Handle recover form message.
509      * @param formId The Id of form to be recovered.
510      * @param want The want of the request.
511      * @param remoteObject Form render proxy object.
512      */
513     void RecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject);
514 
515     /**
516     * @brief Notify remote observer form click event.
517     * @param bundleName BundleName of the form host
518     * @param formEventType  form event type.
519     * @param remoteObject thr remote observer.
520     * @param runningFormInfo Current form data.
521     */
522     void FormClickEvent(const std::string &bundleName, const std::string &formEventType,
523         const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo);
524 
525     /**
526      * @brief Enable form or disable form.
527      * @param formIds formIds The Id list of the forms.
528      * @param enable True or false.
529      * @param remoteObject Form host proxy object.
530      */
531     void EnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
532         const sptr<IRemoteObject> &remoteObject);
533 
534     /**
535      * @brief Post re-add form task to form host when FormRenderService is died.
536      * @param remoteObject Form host proxy object.
537      */
538     void FrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject);
539 
540 private:
541     std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr;
542 };
543 }  // namespace AppExecFwk
544 }  // namespace OHOS
545 #endif // OHOS_FORM_FWK_FORM_TASK_MGR_H
546