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