• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_MGR_H
17 #define OHOS_FORM_FWK_FORM_MGR_H
18 
19 #include <mutex>
20 #include <singleton.h>
21 #include <thread>
22 
23 #include "form_callback_interface.h"
24 #include "form_constants.h"
25 #include "form_errors.h"
26 #include "form_death_callback.h"
27 #include "form_info.h"
28 #include "form_instance.h"
29 #include "form_instances_filter.h"
30 #include "form_js_info.h"
31 #include "form_mgr_interface.h"
32 #include "form_provider_data.h"
33 #include "form_provider_data_proxy.h"
34 #include "form_state_info.h"
35 #include "iremote_object.h"
36 #include "want.h"
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 using OHOS::AAFwk::Want;
41 
42 static volatile int recoverStatus_ = Constants::NOT_IN_RECOVERY;
43 
44 /**
45  * @class FormMgr
46  * FormMgr is used to access form manager services.
47  */
48 class FormMgr final : public DelayedRefSingleton<FormMgr> {
49     DECLARE_DELAYED_REF_SINGLETON(FormMgr)
50 public:
51     DISALLOW_COPY_AND_MOVE(FormMgr);
52 
53     friend class FormMgrDeathRecipient;
54 
55     /**
56      * @brief Get the error message by error code.
57      * @param errorCode the error code return form fms.
58      * @return Returns the error message detail.
59      */
60     std::string GetErrorMsg(int errorCode);
61 
62     /**
63      * @brief Add form with want, send want to form manager service.
64      * @param formId The Id of the forms to add.
65      * @param want The want of the form to add.
66      * @param callerToken Caller ability token.
67      * @param formInfo Form info.
68      * @return Returns ERR_OK on success, others on failure.
69      */
70     int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken,
71         FormJsInfo &formInfo);
72 
73     /**
74      * @brief Delete forms with formIds, send formIds to form manager service.
75      * @param formId The Id of the forms to delete.
76      * @param callerToken Caller ability token.
77      * @return Returns ERR_OK on success, others on failure.
78      */
79     int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
80 
81     /**
82      * @brief Stop rendering form.
83      * @param formId The Id of the forms to delete.
84      * @param compId The compId of the forms to delete.
85      * @return Returns ERR_OK on success, others on failure.
86      */
87     int StopRenderingForm(const int64_t formId, const std::string &compId);
88 
89     /**
90      * @brief Release forms with formIds, send formIds to form manager service.
91      * @param formId The Id of the forms to release.
92      * @param callerToken Caller ability token.
93      * @param delCache Delete Cache or not.
94      * @return Returns ERR_OK on success, others on failure.
95      */
96     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache);
97 
98     /**
99      * @brief Update form with formId, send formId to form manager service.
100      * @param formId The Id of the form to update.
101      * @param formBindingData Form binding data.
102      * @param std::vector<FormDataProxy> Form proxy vector.
103      * @return Returns ERR_OK on success, others on failure.
104      */
105     int UpdateForm(const int64_t formId, const FormProviderData &formBindingData,
106         const std::vector<FormDataProxy> &formDataProxies = {});
107 
108     /**
109      * @brief Notify the form service that the form user's lifecycle is updated.
110      *
111      * This should be called when form user request form.
112      *
113      * @param formId Indicates the unique id of form.
114      * @param callerToken Indicates the callback remote object of specified form user.
115      * @param want information passed to supplier.
116      * @return Returns ERR_OK on success, others on failure.
117      */
118     int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want);
119 
120     /**
121      * @brief Form visible/invisible notify, send formIds to form manager service.
122      * @param formIds The Id list of the forms to notify.
123      * @param callerToken Caller ability token.
124      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
125      * @return Returns ERR_OK on success, others on failure.
126      */
127     int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
128     const int32_t formVisibleType);
129 
130     /**
131      * @brief Release renderer.
132      * @param formId The Id of the forms to release.
133      * @param compId The compId of the forms to release.
134      * @return Returns ERR_OK on success, others on failure.
135      */
136     int ReleaseRenderer(const int64_t formId, const std::string &compId);
137 
138     /**
139      * @brief temp form to normal form.
140      * @param formId The Id of the form.
141      * @param callerToken Caller ability token.
142      * @return None.
143      */
144     int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
145 
146     /**
147      * @brief Dump all of form storage infos.
148      * @param formInfos All of form storage infos.
149      * @return Returns ERR_OK on success, others on failure.
150      */
151     int DumpStorageFormInfos(std::string &formInfos);
152     /**
153      * @brief Dump form info by a bundle name.
154      * @param bundleName The bundle name of form provider.
155      * @param formInfos Form infos.
156      * @return Returns ERR_OK on success, others on failure.
157      */
158     int DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos);
159     /**
160      * @brief Dump form info by a bundle name.
161      * @param formId The id of the form.
162      * @param formInfo Form info.
163      * @return Returns ERR_OK on success, others on failure.
164      */
165     int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo);
166     /**
167      * @brief Dump form timer by form id.
168      * @param formId The id of the form.
169      * @param formInfo Form timer.
170      * @return Returns ERR_OK on success, others on failure.
171      */
172     int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService);
173     /**
174      * @brief Process js message event.
175      * @param formId Indicates the unique id of form.
176      * @param want information passed to supplier.
177      * @param callerToken Caller ability token.
178      * @return Returns ERR_OK on success, others on failure.
179      */
180     int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken);
181 
182     /**
183      * @brief Process js router event.
184      * @param formId Indicates the unique id of form.
185      * @param want the want of the ability to start.
186      * @param callerToken Caller ability token.
187      * @return Returns ERR_OK on success, others on failure.
188      */
189     int RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken);
190 
191     /**
192      * @brief Process Background event.
193      * @param formId Indicates the unique id of form.
194      * @param want the want of the ability to start.
195      * @param callerToken Caller ability token.
196      * @return Returns true if execute success, false otherwise.
197      */
198     int BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken);
199 
200     /**
201      * @brief Get fms recoverStatus.
202      *
203      * @return The current recover status.
204      */
205     static int GetRecoverStatus();
206 
207     /**
208      * @brief Register death callback.
209      *
210      * @param formDeathCallback The death callback.
211      */
212     void RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
213 
214     /**
215      * @brief UnRegister death callback.
216      *
217      * @param formDeathCallback The death callback.
218      */
219     void UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
220 
221     /**
222      * @brief Set the next refresh time
223      *
224      * @param formId The id of the form.
225      * @param nextTime Next refresh time
226      * @return Returns ERR_OK on success, others on failure.
227      */
228     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime);
229 
230     /**
231      * @brief Request to publish a form to the form host.
232      *
233      * @param want The want of the form to publish.
234      * @param withFormBindingData Indicates whether the formBindingData is carried with.
235      * @param formBindingData Indicates the form data.
236      * @param formId Return the form id to be published.
237      * @return Returns ERR_OK on success, others on failure.
238      */
239     ErrCode RequestPublishForm(Want &want, bool withFormBindingData,
240         std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
241         const std::vector<FormDataProxy> &formDataProxies = {});
242 
243     /**
244      * @brief Lifecycle Update.
245      * @param formIds The id of the forms.
246      * @param callerToken Host client.
247      * @param updateType update type, enable if true and disable if false.
248      * @return Returns ERR_OK on success, others on failure.
249      */
250     int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
251                         bool updateType);
252 
253     /**
254      * @brief Set fms recoverStatus.
255      *
256      * @param recoverStatus The recover status.
257      */
258     static void SetRecoverStatus(int recoverStatus);
259 
260     /**
261      * @brief Set form mgr service for test.
262      */
263     void SetFormMgrService(sptr<IFormMgr> formMgrService);
264 
265     /**
266      * @brief Get death recipient.
267      * @return deathRecipient_.
268      */
269     sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() const;
270 
271     /**
272      * @brief Check whether the specified death callback is registered in form mgr.
273      * @param formDeathCallback The specified death callback for checking.
274      * @return Return true on success, false on failure.
275      */
276     bool CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
277 
278     /**
279      * @brief Get the error message content.
280      *
281      * @param errCode Error code.
282      * @return Message content.
283      */
284     std::string GetErrorMessage(int errCode);
285 
286     /**
287      * @brief Delete the invalid forms.
288      * @param formIds Indicates the ID of the valid forms.
289      * @param callerToken Host client.
290      * @param numFormsDeleted Returns the number of the deleted forms.
291      * @return Returns ERR_OK on success, others on failure.
292      */
293     int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
294                            int32_t &numFormsDeleted);
295 
296     /**
297      * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
298      * @param want Indicates a set of parameters to be transparently passed to the form provider.
299      * @param callerToken Host client.
300      * @param stateInfo Returns the form's state info of the specify.
301      * @return Returns ERR_OK on success, others on failure.
302      */
303     int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo);
304 
305     /**
306      * @brief Notify the form is visible or not.
307      * @param formIds Indicates the ID of the forms.
308      * @param isVisible Visible or not.
309      * @param callerToken Host client.
310      * @return Returns ERR_OK on success, others on failure.
311      */
312     int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, const sptr<IRemoteObject> &callerToken);
313 
314     /**
315      * @brief Notify the form is privacy protected or not.
316      * @param formIds Indicates the ID of the forms.
317      * @param isProtected isProtected or not.
318      * @param callerToken Host client.
319      * @return Returns ERR_OK on success, others on failure.
320      */
321     int NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
322                                     const sptr<IRemoteObject> &callerToken);
323 
324     /**
325      * @brief Notify the form is enable to be updated or not.
326      * @param formIds Indicates the ID of the forms.
327      * @param isEnableUpdate enable update or not.
328      * @param callerToken Host client.
329      * @return Returns ERR_OK on success, others on failure.
330      */
331     int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
332                                 const sptr<IRemoteObject> &callerToken);
333 
334     /**
335      * @brief Get All FormsInfo.
336      * @param formInfos Return the forms' information of all forms provided.
337      * @return Returns ERR_OK on success, others on failure.
338      */
339     int GetAllFormsInfo(std::vector<FormInfo> &formInfos);
340 
341     /**
342      * @brief Get forms info by bundle name .
343      * @param bundleName Application name.
344      * @param formInfos Return the forms' information of the specify application name.
345      * @return Returns ERR_OK on success, others on failure.
346      */
347     int GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos);
348 
349     /**
350      * @brief Get forms info by bundle name and module name.
351      * @param bundleName bundle name.
352      * @param moduleName Module name of hap.
353      * @param formInfos Return the forms' information of the specify bundle name and module name.
354      * @return Returns ERR_OK on success, others on failure.
355      */
356     int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos);
357 
358     /**
359     * @brief This function is called by formProvider and gets forms info by the bundle name of the calling ability.
360     *        The bundle name will be retrieved by form service manager.
361     * @param filter filter that contains attributes that the formInfos have to have.
362     * @param formInfos Return the forms' information of the calling bundle name
363     * @return Returns ERR_OK on success, others on failure.
364     */
365     int32_t GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos);
366 
367     /**
368      * @brief Get all running form infos.
369      * @param runningFormInfos Return the running forms' infos currently.
370      * @return Returns ERR_OK on success, others on failure.
371      */
372     ErrCode GetRunningFormInfos(std::vector<RunningFormInfo> &runningFormInfos);
373 
374     /**
375      * @brief Get the running form infos by bundle name.
376      * @param bundleName Application name.
377      * @param runningFormInfos Return the running forms' infos of the specify application name.
378      * @return Returns ERR_OK on success, others on failure.
379      */
380     ErrCode GetRunningFormInfosByBundleName(const std::string &bundleName,
381         std::vector<RunningFormInfo> &runningFormInfos);
382 
383     /**
384      * @brief Check if the request of publishing a form is supported by the host.
385      * @return Returns true if the request is supported and false otherwise.
386      */
387     bool IsRequestPublishFormSupported();
388 
389     /**
390      * @brief Start an ability. This function can only be called by a form extension of a system app.
391      * @param want includes ability name, parameters and relative info sending to an ability.
392      * @param callerToken token of the ability that initially calls this function.
393      * @return Returns ERR_OK on success, others on failure.
394      */
395     int32_t StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken);
396 
397     /**
398      * @brief Share form with formId and remote device id.
399      * @param formId The Id of the forms to share.
400      * @param remoteDeviceId The Id of the remote revice to share.
401      * @param callerToken Indicates the host client.
402      * @param requestCode The request code of this share form.
403      * @return Returns ERR_OK on success, others on failure.
404      */
405     int32_t ShareForm(int64_t formId, const std::string &remoteDeviceId, const sptr<IRemoteObject> &callerToken,
406         int64_t requestCode);
407 
408     /**
409      * @brief Acquire form data by formId.
410      * @param formId The Id of the form to acquire data.
411      * @param requestCode The request code of this acquire form.
412      * @param callerToken Indicates the host client.
413      * @param formData Return the forms' information of customization
414      * @return Returns ERR_OK on success, others on failure.
415      */
416     int32_t AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
417          AAFwk::WantParams &formData);
418 
419     /**
420      * @brief Check form manager service ready.
421      *
422      * @return Return true if form manager service ready; returns false otherwise.
423      */
424     bool CheckFMSReady();
425 
426     /**
427      * @brief Registers the callback for publish form. The callback is used to process the publish form request
428      * when the system handler is not found.
429      * @param interceptorCallback The injected callback, should implementation IFormPublishInterceptor.
430      * @return Returns ERR_OK on success, others on failure.
431      */
432     int32_t RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback);
433 
434     /**
435      * @brief Unregisters the callback for publish form. The callback is used to process the publish form request
436      * when the system handler is not found.
437      * @param interceptorCallback The injected callback, should implementation IFormPublishInterceptor.
438      * @return Returns ERR_OK on success, others on failure.
439      */
440     int32_t UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback);
441 
442     /**
443      * @brief Get external error from innerErrorCode.
444      * @param innerErrorCode innerErrorCode, get from FMS.
445      * @param externalErrorCode output externalErrorCode.
446      * @param errorMsg output errorMsg.
447      */
448     void GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg);
449 
450     /**
451      * @brief Get external error message by external error code.
452      *
453      * @param externalErrorCode External error code.
454      * @return External error message.
455      */
456     std::string GetErrorMsgByExternalErrorCode(int32_t externalErrorCode);
457 
458     /**
459      * @brief Register form add observer by bundle.
460      * @param bundleName BundleName of the form host
461      * @param callerToken Caller ability token.
462      * @return Returns ERR_OK on success, others on failure.
463      */
464     ErrCode RegisterFormAddObserverByBundle(const std::string bundleName, const sptr<IRemoteObject> &callerToken);
465 
466     /**
467      * @brief Register form remove observer by bundle.
468      * @param bundleName BundleName of the form host
469      * @param callerToken Caller ability token.
470      * @return Returns ERR_OK on success, others on failure.
471      */
472     ErrCode RegisterFormRemoveObserverByBundle(const std::string bundleName, const sptr<IRemoteObject> &callerToken);
473 
474     /**
475     * @brief get forms count.
476     * @param isTempFormFlag Indicates temp form or not.
477     * @param formCount Returns the number of the cast or temp form.
478     * @return Returns ERR_OK on success, others on failure.
479     */
480     int32_t GetFormsCount(bool isTempFormFlag, int32_t &formCount);
481 
482     /**
483     * @brief get host forms count.
484     * @param bundleName Indicates form host bundleName.
485     * @param formCount Returns the number of the host form.
486     * @return Returns ERR_OK on success, others on failure.
487     */
488     int32_t GetHostFormsCount(std::string &bundleName, int32_t &formCount);
489 
490     /**
491     * @brief Get form instances by filter info.
492     * @param formInstancesFilter includes bundleName, moduleName, formName, abilityName to get formInstances.
493     * @param formInstances return formInstances
494     * @return return ERR_OK on get info success, others on failure.
495     */
496     ErrCode GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
497         std::vector<FormInstance> &formInstances);
498 
499    /**
500     * @brief Get form instance by formId.
501     * @param formId formId Indicates the unique id of form.
502     * @param formInstance return formInstance
503     * @return return ERR_OK on get info success, others on failure.
504     */
505     ErrCode GetFormInstanceById(const int64_t formId, FormInstance &formInstance);
506 
507     /**
508      * @brief Get form instance by formId, include form store in DB.
509      * @param formId formId Indicates the unique id of form.
510      * @param isIncludeUnused Indicates whether to include unused form.
511      * @param formInstance return formInstance
512      * @return return ERR_OK on get info success, others on failure.
513      */
514     ErrCode GetFormInstanceById(const int64_t formId, bool isIncludeUnused, FormInstance &formInstance);
515 
516     /**
517      * @brief Register form add observer.
518      * @param bundleName BundleName of the form host
519      * @param callerToken Caller ability token.
520      * @return Returns ERR_OK on success, others on failure.
521      */
522     ErrCode RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken);
523 
524     /**
525      * @brief Register form remove observer.
526      * @param bundleName BundleName of the form host
527      * @param callerToken Caller ability token.
528      * @return Returns ERR_OK on success, others on failure.
529      */
530     ErrCode RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken);
531 
532 private:
533     /**
534      * @brief Connect form manager service.
535      * @return Returns ERR_OK on success, others on failure.
536      */
537     ErrCode Connect();
538 
539     /**
540     * @brief Reconnect form manager service once per 1000 milliseconds,
541     *        until the connection succeeds or reaching the max retry times.
542     * @return Returns true if execute success, false otherwise.
543     */
544     bool Reconnect();
545     /**
546      * @brief Reset proxy.
547      * @param remote remote object.
548      */
549     void ResetProxy(const wptr<IRemoteObject> &remote);
550 
551     /**
552      * @class FormMgrDeathRecipient
553      * FormMgrDeathRecipient notices IRemoteBroker died.
554      */
555     class FormMgrDeathRecipient : public IRemoteObject::DeathRecipient {
556     public:
557         FormMgrDeathRecipient() = default;
558         ~FormMgrDeathRecipient() = default;
559 
560         /**
561          * @brief Notices IRemoteBroker died.
562          * @param remote remote object.
563          */
564         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
565     private:
566         DISALLOW_COPY_AND_MOVE(FormMgrDeathRecipient);
567     };
568 
569     std::mutex connectMutex_;
570     sptr<IFormMgr> remoteProxy_;
571 
572     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
573 
574     // True: need to get a new fms remote object,
575     // False: no need to get a new fms remote object.
576     volatile bool resetFlag_ = false;
577 
578     std::vector<std::shared_ptr<FormCallbackInterface>> formDeathCallbacks_;
579 };
580 }  // namespace AppExecFwk
581 }  // namespace OHOS
582 #endif  // OHOS_FORM_FWK_FORM_MGR_H
583