• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_OHOS_FORM_MGR_H
17 #define FOUNDATION_APPEXECFWK_OHOS_FORM_MGR_H
18 
19 #include <mutex>
20 #include <singleton.h>
21 
22 #include "form_callback_interface.h"
23 #include "form_constants.h"
24 #include "form_death_callback.h"
25 #include "form_info.h"
26 #include "form_js_info.h"
27 #include "form_mgr_interface.h"
28 #include "form_provider_data.h"
29 #include "form_state_info.h"
30 #include "iremote_object.h"
31 #include "want.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 using OHOS::AAFwk::Want;
36 
37 static volatile int recoverStatus_ = Constants::NOT_IN_RECOVERY;
38 
39 /**
40  * @class FormMgr
41  * FormMgr is used to access form manager services.
42  */
43 class FormMgr final : public DelayedRefSingleton<FormMgr> {
44     DECLARE_DELAYED_REF_SINGLETON(FormMgr)
45 public:
46     DISALLOW_COPY_AND_MOVE(FormMgr);
47     /**
48      * @brief Get the error message by error code.
49      * @param errorCode the error code return form fms.
50      * @return Returns the error message detail.
51      */
52     std::string GetErrorMsg(int errorCode);
53 
54     /**
55      * @brief Add form with want, send want to form manager service.
56      * @param formId The Id of the forms to add.
57      * @param want The want of the form to add.
58      * @param callerToken Caller ability token.
59      * @param formInfo Form info.
60      * @return Returns ERR_OK on success, others on failure.
61      */
62     int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken,
63         FormJsInfo &formInfo);
64 
65     /**
66      * @brief Delete forms with formIds, send formIds to form manager service.
67      * @param formId The Id of the forms to delete.
68      * @param callerToken Caller ability token.
69      * @return Returns ERR_OK on success, others on failure.
70      */
71     int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
72 
73     /**
74      * @brief Release forms with formIds, send formIds to form manager service.
75      * @param formId The Id of the forms to release.
76      * @param callerToken Caller ability token.
77      * @param delCache Delete Cache or not.
78      * @return Returns ERR_OK on success, others on failure.
79      */
80     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache);
81 
82     /**
83      * @brief Update form with formId, send formId to form manager service.
84      * @param formId The Id of the form to update.
85      * @param formBindingData Form binding data.
86      * @return Returns ERR_OK on success, others on failure.
87      */
88     int UpdateForm(const int64_t formId, const FormProviderData &formBindingData);
89 
90     /**
91      * @brief Notify the form service that the form user's lifecycle is updated.
92      *
93      * This should be called when form user request form.
94      *
95      * @param formId Indicates the unique id of form.
96      * @param callerToken Indicates the callback remote object of specified form user.
97      * @param want information passed to supplier.
98      * @return Returns true if execute success, false otherwise.
99      */
100     int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want);
101 
102     /**
103      * @brief Form visible/invisible notify, send formIds to form manager service.
104      * @param formIds The Id list of the forms to notify.
105      * @param callerToken Caller ability token.
106      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
107      * @return Returns ERR_OK on success, others on failure.
108      */
109     int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
110     const int32_t formVisibleType);
111 
112     /**
113      * @brief temp form to normal form.
114      * @param formId The Id of the form.
115      * @param callerToken Caller ability token.
116      * @return None.
117      */
118     int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken);
119 
120     /**
121      * @brief Dump all of form storage infos.
122      * @param formInfos All of form storage infos.
123      * @return Returns ERR_OK on success, others on failure.
124      */
125     int DumpStorageFormInfos(std::string &formInfos);
126     /**
127      * @brief Dump form info by a bundle name.
128      * @param bundleName The bundle name of form provider.
129      * @param formInfos Form infos.
130      * @return Returns ERR_OK on success, others on failure.
131      */
132     int DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos);
133     /**
134      * @brief Dump form info by a bundle name.
135      * @param formId The id of the form.
136      * @param formInfo Form info.
137      * @return Returns ERR_OK on success, others on failure.
138      */
139     int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo);
140     /**
141      * @brief Dump form timer by form id.
142      * @param formId The id of the form.
143      * @param formInfo Form timer.
144      * @return Returns ERR_OK on success, others on failure.
145      */
146     int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService);
147     /**
148      * @brief Process js message event.
149      * @param formId Indicates the unique id of form.
150      * @param want information passed to supplier.
151      * @param callerToken Caller ability token.
152      * @return Returns true if execute success, false otherwise.
153      */
154     int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken);
155 
156     /**
157      * @brief Process js router event.
158      * @param formId Indicates the unique id of form.
159      * @param want the want of the ability to start.
160      * @return Returns true if execute success, false otherwise.
161      */
162     int RouterEvent(const int64_t formId, Want &want);
163 
164     /**
165      * @brief Get fms recoverStatus.
166      *
167      * @return The current recover status.
168      */
169     static int GetRecoverStatus();
170 
171     /**
172      * @brief Register death callback.
173      *
174      * @param formDeathCallback The death callback.
175      */
176     void RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
177 
178     /**
179      * @brief UnRegister death callback.
180      *
181      * @param formDeathCallback The death callback.
182      */
183     void UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
184 
185     /**
186      * @brief Set the next refresh time
187      *
188      * @param formId The id of the form.
189      * @param nextTime Next refresh time
190      * @return Returns ERR_OK on success, others on failure.
191      */
192     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime);
193 
194     /**
195      * @brief Lifecycle Update.
196      * @param formIds The id of the forms.
197      * @param callerToken Host client.
198      * @param updateType Next refresh time.
199      * @return Returns ERR_OK on success, others on failure.
200      */
201     int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
202                         const int32_t updateType);
203 
204     /**
205      * @brief Set fms recoverStatus.
206      *
207      * @param recoverStatus The recover status.
208      */
209     static void SetRecoverStatus(int recoverStatus);
210 
211     /**
212      * @brief Set form mgr service for test.
213      */
214     void SetFormMgrService(sptr<IFormMgr> formMgrService);
215 
216     /**
217      * @brief Get death recipient.
218      * @return deathRecipient_.
219      */
220     sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() const;
221 
222     /**
223      * @brief Check whether the specified death callback is registered in form mgr.
224      * @param formDeathCallback The specified death callback for checking.
225      * @return Return true on success, false on failure.
226      */
227     bool CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback);
228 
229     /**
230      * @brief Get the error message content.
231      *
232      * @param errCode Error code.
233      * @return Message content.
234      */
235     std::string GetErrorMessage(int errCode);
236 
237     /**
238      * @brief  Add forms to storage for st .
239      * @param Want The Want of the form to add.
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     int DistributedDataAddForm(const Want &want);
243 
244     /**
245      * @brief  Delete form form storage for st.
246      * @param formId The formId of the form to delete.
247      * @return Returns ERR_OK on success, others on failure.
248      */
249     int DistributedDataDeleteForm(const std::string &formId);
250 
251     /**
252      * @brief Delete the invalid forms.
253      * @param formIds Indicates the ID of the valid forms.
254      * @param callerToken Host client.
255      * @param numFormsDeleted Returns the number of the deleted forms.
256      * @return Returns ERR_OK on success, others on failure.
257      */
258     int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
259                            int32_t &numFormsDeleted);
260 
261     /**
262      * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
263      * @param want Indicates a set of parameters to be transparently passed to the form provider.
264      * @param callerToken Host client.
265      * @param stateInfo Returns the form's state info of the specify.
266      * @return Returns ERR_OK on success, others on failure.
267      */
268     int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo);
269 
270     /**
271      * @brief Notify the form is visible or not.
272      * @param formIds Indicates the ID of the forms.
273      * @param isVisible Visible or not.
274      * @param callerToken Host client.
275      * @return Returns ERR_OK on success, others on failure.
276      */
277     int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, const sptr<IRemoteObject> &callerToken);
278 
279     /**
280      * @brief Notify the form is enable to be updated or not.
281      * @param formIds Indicates the ID of the forms.
282      * @param isEnableUpdate enable update or not.
283      * @param callerToken Host client.
284      * @return Returns ERR_OK on success, others on failure.
285      */
286     int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
287                                 const sptr<IRemoteObject> &callerToken);
288 
289     /**
290      * @brief Get All FormsInfo.
291      * @param formInfos Return the forms' information of all forms provided.
292      * @return Returns ERR_OK on success, others on failure.
293      */
294     int GetAllFormsInfo(std::vector<FormInfo> &formInfos);
295 
296     /**
297      * @brief Get forms info by bundle name .
298      * @param bundleName Application name.
299      * @param formInfos Return the forms' information of the specify application name.
300      * @return Returns ERR_OK on success, others on failure.
301      */
302     int GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos);
303 
304     /**
305      * @brief Get forms info by bundle name and module name.
306      * @param bundleName bundle name.
307      * @param moduleName Module name of hap.
308      * @param formInfos Return the forms' information of the specify bundle name and module name.
309      * @return Returns ERR_OK on success, others on failure.
310      */
311     int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos);
312 
313     /**
314      * @brief Update action string for router event.
315      * @param formId Indicates the unique id of form.
316      * @param action Indicates the origin action string.
317      * @return Returns ERR_OK on success, others on failure.
318      */
319     int UpdateRouterAction(const int64_t formId, std::string &action);
320 private:
321     /**
322      * @brief Connect form manager service.
323      * @return Returns ERR_OK on success, others on failure.
324      */
325     ErrCode Connect();
326 
327     /**
328     * @brief Reconnect form manager service once per 1000 milliseconds,
329     *        until the connection succeeds or reaching the max retry times.
330     * @return Returns true if execute success, false otherwise.
331     */
332     bool Reconnect();
333     /**
334      * @brief Reset proxy.
335      * @param remote remote object.
336      */
337     void ResetProxy(const wptr<IRemoteObject> &remote);
338 
339 public:
340     friend class FormMgrDeathRecipient;
341 
342 private:
343     /**
344      * @class FormMgrDeathRecipient
345      * FormMgrDeathRecipient notices IRemoteBroker died.
346      */
347     class FormMgrDeathRecipient : public IRemoteObject::DeathRecipient {
348     public:
349         FormMgrDeathRecipient() = default;
350         ~FormMgrDeathRecipient() = default;
351 
352         /**
353          * @brief Notices IRemoteBroker died.
354          * @param remote remote object.
355          */
356         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
357     private:
358         DISALLOW_COPY_AND_MOVE(FormMgrDeathRecipient);
359     };
360 
361     std::mutex connectMutex_;
362     sptr<IFormMgr> remoteProxy_;
363 
364     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
365 
366     // True: need to get a new fms remote object,
367     // False: no need to get a new fms remote object.
368     volatile bool resetFlag_ = false;
369 
370     std::vector<std::shared_ptr<FormCallbackInterface>> formDeathCallbacks_;
371 };
372 }  // namespace AppExecFwk
373 }  // namespace OHOS
374 #endif  // FOUNDATION_APPEXECFWK_OHOS_FORM_MGR_H