• 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 #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_js_info.h"
29 #include "form_mgr_interface.h"
30 #include "form_provider_data.h"
31 #include "form_state_info.h"
32 #include "iremote_object.h"
33 #include "want.h"
34 
35 namespace OHOS {
36 namespace AppExecFwk {
37 using OHOS::AAFwk::Want;
38 
39 static volatile int recoverStatus_ = Constants::NOT_IN_RECOVERY;
40 
41 /**
42  * @class FormMgr
43  * FormMgr is used to access form manager services.
44  */
45 class FormMgr final : public DelayedRefSingleton<FormMgr> {
46     DECLARE_DELAYED_REF_SINGLETON(FormMgr)
47 public:
48     DISALLOW_COPY_AND_MOVE(FormMgr);
49 
50     friend class FormMgrDeathRecipient;
51 
52     /**
53      * @brief Get the error message by error code.
54      * @param errorCode the error code return form fms.
55      * @return Returns the error message detail.
56      */
57     std::string GetErrorMsg(int errorCode);
58 
59     /**
60      * @brief Add form with want, send want to form manager service.
61      * @param formId The Id of the forms to add.
62      * @param want The want of the form to add.
63      * @param callerToken Caller ability token.
64      * @param formInfo Form info.
65      * @return Returns ERR_OK on success, others on failure.
66      */
67     int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken,
68         FormJsInfo &formInfo);
69 
70     /**
71      * @brief Delete forms with formIds, send formIds to form manager service.
72      * @param formId The Id of the forms to delete.
73      * @param callerToken Caller ability token.
74      * @return Returns ERR_OK on success, others on failure.
75      */
76     int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
77 
78     /**
79      * @brief Stop rendering form.
80      * @param formId The Id of the forms to delete.
81      * @param compId The compId of the forms to delete.
82      * @return Returns ERR_OK on success, others on failure.
83      */
84     int StopRenderingForm(const int64_t formId, const std::string &compId);
85 
86     /**
87      * @brief Release forms with formIds, send formIds to form manager service.
88      * @param formId The Id of the forms to release.
89      * @param callerToken Caller ability token.
90      * @param delCache Delete Cache or not.
91      * @return Returns ERR_OK on success, others on failure.
92      */
93     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache);
94 
95     /**
96      * @brief Update form with formId, send formId to form manager service.
97      * @param formId The Id of the form to update.
98      * @param formBindingData Form binding data.
99      * @return Returns ERR_OK on success, others on failure.
100      */
101     int UpdateForm(const int64_t formId, const FormProviderData &formBindingData);
102 
103     /**
104      * @brief Notify the form service that the form user's lifecycle is updated.
105      *
106      * This should be called when form user request form.
107      *
108      * @param formId Indicates the unique id of form.
109      * @param callerToken Indicates the callback remote object of specified form user.
110      * @param want information passed to supplier.
111      * @return Returns ERR_OK on success, others on failure.
112      */
113     int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want);
114 
115     /**
116      * @brief Form visible/invisible notify, send formIds to form manager service.
117      * @param formIds The Id list of the forms to notify.
118      * @param callerToken Caller ability token.
119      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
120      * @return Returns ERR_OK on success, others on failure.
121      */
122     int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
123     const int32_t formVisibleType);
124 
125     /**
126      * @brief temp form to normal form.
127      * @param formId The Id of the form.
128      * @param callerToken Caller ability token.
129      * @return None.
130      */
131     int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
132 
133     /**
134      * @brief Dump all of form storage infos.
135      * @param formInfos All of form storage infos.
136      * @return Returns ERR_OK on success, others on failure.
137      */
138     int DumpStorageFormInfos(std::string &formInfos);
139     /**
140      * @brief Dump form info by a bundle name.
141      * @param bundleName The bundle name of form provider.
142      * @param formInfos Form infos.
143      * @return Returns ERR_OK on success, others on failure.
144      */
145     int DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos);
146     /**
147      * @brief Dump form info by a bundle name.
148      * @param formId The id of the form.
149      * @param formInfo Form info.
150      * @return Returns ERR_OK on success, others on failure.
151      */
152     int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo);
153     /**
154      * @brief Dump form timer by form id.
155      * @param formId The id of the form.
156      * @param formInfo Form timer.
157      * @return Returns ERR_OK on success, others on failure.
158      */
159     int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService);
160     /**
161      * @brief Process js message event.
162      * @param formId Indicates the unique id of form.
163      * @param want information passed to supplier.
164      * @param callerToken Caller ability token.
165      * @return Returns ERR_OK on success, others on failure.
166      */
167     int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken);
168 
169     /**
170      * @brief Process js router event.
171      * @param formId Indicates the unique id of form.
172      * @param want the want of the ability to start.
173      * @param callerToken Caller ability token.
174      * @return Returns ERR_OK on success, others on failure.
175      */
176     int RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken);
177 
178     /**
179      * @brief Process Background event.
180      * @param formId Indicates the unique id of form.
181      * @param want the want of the ability to start.
182      * @param callerToken Caller ability token.
183      * @return Returns true if execute success, false otherwise.
184      */
185     int BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken);
186 
187     /**
188      * @brief Get fms recoverStatus.
189      *
190      * @return The current recover status.
191      */
192     static int GetRecoverStatus();
193 
194     /**
195      * @brief Register death callback.
196      *
197      * @param formDeathCallback The death callback.
198      */
199     void RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
200 
201     /**
202      * @brief UnRegister death callback.
203      *
204      * @param formDeathCallback The death callback.
205      */
206     void UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
207 
208     /**
209      * @brief Set the next refresh time
210      *
211      * @param formId The id of the form.
212      * @param nextTime Next refresh time
213      * @return Returns ERR_OK on success, others on failure.
214      */
215     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime);
216 
217     /**
218      * @brief Request to publish a form to the form host.
219      *
220      * @param want The want of the form to publish.
221      * @param withFormBindingData Indicates whether the formBindingData is carried with.
222      * @param formBindingData Indicates the form data.
223      * @param formId Return the form id to be published.
224      * @return Returns ERR_OK on success, others on failure.
225      */
226     ErrCode RequestPublishForm(Want &want, bool withFormBindingData,
227                                std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId);
228 
229     /**
230      * @brief Lifecycle Update.
231      * @param formIds The id of the forms.
232      * @param callerToken Host client.
233      * @param updateType update type, enable if true and disable if false.
234      * @return Returns ERR_OK on success, others on failure.
235      */
236     int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
237                         bool updateType);
238 
239     /**
240      * @brief Set fms recoverStatus.
241      *
242      * @param recoverStatus The recover status.
243      */
244     static void SetRecoverStatus(int recoverStatus);
245 
246     /**
247      * @brief Set form mgr service for test.
248      */
249     void SetFormMgrService(sptr<IFormMgr> formMgrService);
250 
251     /**
252      * @brief Get death recipient.
253      * @return deathRecipient_.
254      */
255     sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() const;
256 
257     /**
258      * @brief Check whether the specified death callback is registered in form mgr.
259      * @param formDeathCallback The specified death callback for checking.
260      * @return Return true on success, false on failure.
261      */
262     bool CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
263 
264     /**
265      * @brief Get the error message content.
266      *
267      * @param errCode Error code.
268      * @return Message content.
269      */
270     std::string GetErrorMessage(int errCode);
271 
272     /**
273      * @brief Delete the invalid forms.
274      * @param formIds Indicates the ID of the valid forms.
275      * @param callerToken Host client.
276      * @param numFormsDeleted Returns the number of the deleted forms.
277      * @return Returns ERR_OK on success, others on failure.
278      */
279     int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
280                            int32_t &numFormsDeleted);
281 
282     /**
283      * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
284      * @param want Indicates a set of parameters to be transparently passed to the form provider.
285      * @param callerToken Host client.
286      * @param stateInfo Returns the form's state info of the specify.
287      * @return Returns ERR_OK on success, others on failure.
288      */
289     int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo);
290 
291     /**
292      * @brief Notify the form is visible or not.
293      * @param formIds Indicates the ID of the forms.
294      * @param isVisible Visible or not.
295      * @param callerToken Host client.
296      * @return Returns ERR_OK on success, others on failure.
297      */
298     int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, const sptr<IRemoteObject> &callerToken);
299 
300     /**
301      * @brief Notify the form is privacy protected or not.
302      * @param formIds Indicates the ID of the forms.
303      * @param isProtected isProtected or not.
304      * @param callerToken Host client.
305      * @return Returns ERR_OK on success, others on failure.
306      */
307     int NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
308                                     const sptr<IRemoteObject> &callerToken);
309 
310     /**
311      * @brief Notify the form is enable to be updated or not.
312      * @param formIds Indicates the ID of the forms.
313      * @param isEnableUpdate enable update or not.
314      * @param callerToken Host client.
315      * @return Returns ERR_OK on success, others on failure.
316      */
317     int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
318                                 const sptr<IRemoteObject> &callerToken);
319 
320     /**
321      * @brief Get All FormsInfo.
322      * @param formInfos Return the forms' information of all forms provided.
323      * @return Returns ERR_OK on success, others on failure.
324      */
325     int GetAllFormsInfo(std::vector<FormInfo> &formInfos);
326 
327     /**
328      * @brief Get forms info by bundle name .
329      * @param bundleName Application name.
330      * @param formInfos Return the forms' information of the specify application name.
331      * @return Returns ERR_OK on success, others on failure.
332      */
333     int GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos);
334 
335     /**
336      * @brief Get forms info by bundle name and module name.
337      * @param bundleName bundle name.
338      * @param moduleName Module name of hap.
339      * @param formInfos Return the forms' information of the specify bundle name and module name.
340      * @return Returns ERR_OK on success, others on failure.
341      */
342     int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos);
343 
344     /**
345     * @brief This function is called by formProvider and gets forms info by the bundle name of the calling ability.
346     *        The bundle name will be retrieved by form service manager.
347     * @param filter filter that contains attributes that the formInfos have to have.
348     * @param formInfos Return the forms' information of the calling bundle name
349     * @return Returns ERR_OK on success, others on failure.
350     */
351     int32_t GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos);
352 
353     /**
354      * @brief Check if the request of publishing a form is supported by the host.
355      * @return Returns true if the request is supported and false otherwise.
356      */
357     bool IsRequestPublishFormSupported();
358 
359     /**
360      * @brief Start an ability. This function can only be called by a form extension of a system app.
361      * @param want includes ability name, parameters and relative info sending to an ability.
362      * @param callerToken token of the ability that initially calls this function.
363      * @return Returns ERR_OK on success, others on failure.
364      */
365     int32_t StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken);
366 
367     /**
368      * @brief Share form with formId and remote device id.
369      * @param formId The Id of the forms to share.
370      * @param remoteDeviceId The Id of the remote revice to share.
371      * @param callerToken Indicates the host client.
372      * @param requestCode The request code of this share form.
373      * @return Returns ERR_OK on success, others on failure.
374      */
375     int32_t ShareForm(int64_t formId, const std::string &remoteDeviceId, const sptr<IRemoteObject> &callerToken,
376         int64_t requestCode);
377 
378     /**
379      * @brief Check form manager service ready.
380      *
381      * @return Return true if form manager service ready; returns false otherwise.
382      */
383     bool CheckFMSReady();
384 
385     /**
386      * @brief Get external error from innerErrorCode.
387      * @param innerErrorCode innerErrorCode, get from FMS.
388      * @param externalErrorCode output externalErrorCode.
389      * @param errorMsg output errorMsg.
390      */
391     void GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg);
392 
393     /**
394      * @brief Get external error message by external error code.
395      *
396      * @param externalErrorCode External error code.
397      * @return External error message.
398      */
399     std::string GetErrorMsgByExternalErrorCode(int32_t externalErrorCode);
400 
401 private:
402     /**
403      * @brief Connect form manager service.
404      * @return Returns ERR_OK on success, others on failure.
405      */
406     ErrCode Connect();
407 
408     /**
409     * @brief Reconnect form manager service once per 1000 milliseconds,
410     *        until the connection succeeds or reaching the max retry times.
411     * @return Returns true if execute success, false otherwise.
412     */
413     bool Reconnect();
414     /**
415      * @brief Reset proxy.
416      * @param remote remote object.
417      */
418     void ResetProxy(const wptr<IRemoteObject> &remote);
419 
420     /**
421      * @class FormMgrDeathRecipient
422      * FormMgrDeathRecipient notices IRemoteBroker died.
423      */
424     class FormMgrDeathRecipient : public IRemoteObject::DeathRecipient {
425     public:
426         FormMgrDeathRecipient() = default;
427         ~FormMgrDeathRecipient() = default;
428 
429         /**
430          * @brief Notices IRemoteBroker died.
431          * @param remote remote object.
432          */
433         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
434     private:
435         DISALLOW_COPY_AND_MOVE(FormMgrDeathRecipient);
436     };
437 
438     std::mutex connectMutex_;
439     sptr<IFormMgr> remoteProxy_;
440 
441     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
442 
443     // True: need to get a new fms remote object,
444     // False: no need to get a new fms remote object.
445     volatile bool resetFlag_ = false;
446 
447     std::vector<std::shared_ptr<FormCallbackInterface>> formDeathCallbacks_;
448 };
449 }  // namespace AppExecFwk
450 }  // namespace OHOS
451 #endif  // OHOS_FORM_FWK_FORM_MGR_H
452