• 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 #include "form_mgr.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "form_caller_mgr.h"
20 #include "form_errors.h"
21 #include "form_mgr_errors.h"
22 #include "hilog_wrapper.h"
23 #include "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
FormMgr()31 FormMgr::FormMgr() {}
~FormMgr()32 FormMgr::~FormMgr()
33 {
34     if (remoteProxy_ != nullptr) {
35         auto remoteObject = remoteProxy_->AsObject();
36         if (remoteObject != nullptr) {
37             remoteObject->RemoveDeathRecipient(deathRecipient_);
38         }
39     }
40 }
41 
42 /**
43  * @brief Get the error message by error code.
44  * @param errorCode the error code return form fms.
45  * @return Returns the error message detail.
46  */
GetErrorMsg(int errorCode)47 std::string FormMgr::GetErrorMsg(int errorCode)
48 {
49     return "unknown error";
50 }
51 
52 /**
53  * @brief Add form with want, send want to form manager service.
54  * @param formId The Id of the forms to add.
55  * @param want The want of the form to add.
56  * @param callerToken Caller ability token.
57  * @param formInfo Form info.
58  * @return Returns ERR_OK on success, others on failure.
59  */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)60 int FormMgr::AddForm(
61     const int64_t formId,
62     const Want &want,
63     const sptr<IRemoteObject> &callerToken,
64     FormJsInfo &formInfo)
65 {
66     HILOG_INFO("%{public}s called.", __func__);
67     int errCode = Connect();
68     if (errCode != ERR_OK) {
69         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
70         return errCode;
71     }
72     return remoteProxy_->AddForm(formId, want, callerToken, formInfo);
73 }
74 
75 /**
76  * @brief Delete forms with formIds, send formIds to form manager service.
77  * @param formId The Id of the forms to delete.
78  * @param callerToken Caller ability token.
79  * @return Returns ERR_OK on success, others on failure.
80  */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)81 int FormMgr::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
82 {
83     HILOG_INFO("%{public}s called.", __func__);
84     // check fms recover status
85     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
86         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
87         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
88     }
89     // check formId
90     if (formId <= 0) {
91         HILOG_ERROR("%{public}s error, the passed in formId can't be negative or zero.", __func__);
92         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
93     }
94 
95     int errCode = Connect();
96     if (errCode != ERR_OK) {
97         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
98         return errCode;
99     }
100     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
101     return remoteProxy_->DeleteForm(formId, callerToken);
102 }
103 
104 /**
105  * @brief Stop rendering form.
106  * @param formId The Id of the forms to delete.
107  * @param compId The compId of the forms to delete.
108  * @return Returns ERR_OK on success, others on failure.
109 */
StopRenderingForm(const int64_t formId,const std::string & compId)110 int FormMgr::StopRenderingForm(const int64_t formId, const std::string &compId)
111 {
112     HILOG_INFO("%{public}s called.", __func__);
113     // check fms recover status
114     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
115         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
116         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
117     }
118     // check formId
119     if (formId <= 0 || compId.empty()) {
120         HILOG_ERROR("%{public}s error, the formId is invalid or compId is empty.", __func__);
121         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
122     }
123 
124     int errCode = Connect();
125     if (errCode != ERR_OK) {
126         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
127         return errCode;
128     }
129 
130     return remoteProxy_->StopRenderingForm(formId, compId);
131 }
132 
133 /**
134  * @brief Release forms with formIds, send formIds to form manager service.
135  * @param formId The Id of the forms to release.
136  * @param callerToken Caller ability token.
137  * @param delCache Delete Cache or not.
138  * @return Returns ERR_OK on success, others on failure.
139  */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)140 int FormMgr::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
141 {
142     HILOG_INFO("%{public}s called.", __func__);
143     // check fms recover status
144     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
145         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
146         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
147     }
148     // check formId
149     if (formId <= 0) {
150         HILOG_ERROR("%{public}s error, the passed in formId can't be negative or zero.", __func__);
151         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
152     }
153 
154     int errCode = Connect();
155     if (errCode != ERR_OK) {
156         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
157         return errCode;
158     }
159     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
160     return remoteProxy_->ReleaseForm(formId, callerToken, delCache);
161 }
162 
163 /**
164  * @brief Update form with formId, send formId to form manager service.
165  * @param formId The Id of the form to update.
166  * @param formBindingData Form binding data.
167  * @return Returns ERR_OK on success, others on failure.
168  */
UpdateForm(const int64_t formId,const FormProviderData & formBindingData)169 int FormMgr::UpdateForm(const int64_t formId, const FormProviderData &formBindingData)
170 {
171     HILOG_INFO("%{public}s called.", __func__);
172 
173     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
174         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
175         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
176     }
177 
178     if (formId <= 0) {
179         HILOG_ERROR("%{public}s error, the passed in formId can't be negative or zero.", __func__);
180         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
181     }
182 
183     // check formBindingData
184     if (formBindingData.GetDataString().empty()) {
185         HILOG_ERROR("%{public}s error, the formProviderData is null.", __func__);
186         return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY;
187     }
188 
189     int errCode = Connect();
190     if (errCode != ERR_OK) {
191         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
192         return errCode;
193     }
194     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
195     if (hostCaller != nullptr) {
196         hostCaller->UpdateForm(formId, formBindingData);
197     } else {
198         std::vector<std::shared_ptr<FormProviderCaller>> formProviderCallers;
199         FormCallerMgr::GetInstance().GetFormProviderCaller(formId, formProviderCallers);
200         for (const auto &formProviderCaller : formProviderCallers) {
201             formProviderCaller->UpdateForm(formId, formBindingData);
202         }
203     }
204     return remoteProxy_->UpdateForm(formId, formBindingData);
205 }
206 
207 /**
208  * @brief Notify the form service that the form user's lifecycle is updated.
209  *
210  * This should be called when form user request form.
211  *
212  * @param formId Indicates the unique id of form.
213  * @param callerToken Indicates the callback remote object of specified form user.
214  * @param want information passed to supplier.
215  * @return Returns true if execute success, false otherwise.
216  */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)217 int FormMgr::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
218 {
219     HILOG_INFO("%{public}s called.", __func__);
220     if (formId <= 0) {
221         HILOG_ERROR("%{public}s error, The passed formid is invalid. Its value must be larger than 0.", __func__);
222         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
223     }
224     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
225         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
226         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
227     }
228     int errCode = Connect();
229     if (errCode != ERR_OK) {
230         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
231         return errCode;
232     }
233     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
234     if (hostCaller != nullptr) {
235         HILOG_DEBUG("request form by host caller");
236         return hostCaller->RequestForm(formId, callerToken, want);
237     }
238     ErrCode resultCode = remoteProxy_->RequestForm(formId, callerToken, want);
239     if (resultCode != ERR_OK) {
240         HILOG_ERROR(
241             "%{public}s error, failed to notify the form service that the form user's lifecycle is updated, error "
242             "code is %{public}d.",
243             __func__,
244             resultCode);
245     }
246     return resultCode;
247 }
248 
249 /**
250  * @brief Form visible/invisible notify, send formIds to form manager service.
251  * @param formIds The Id list of the forms to notify.
252  * @param callerToken Caller ability token.
253  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
254  * @return Returns ERR_OK on success, others on failure.
255  */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)256 int FormMgr::NotifyWhetherVisibleForms(
257     const std::vector<int64_t> &formIds,
258     const sptr<IRemoteObject> &callerToken,
259     const int32_t formVisibleType)
260 {
261     HILOG_INFO("%{public}s called.", __func__);
262 
263     if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) {
264         HILOG_ERROR("%{public}s, formIds is empty or exceed 32.", __func__);
265         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
266     }
267 
268     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
269         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
270         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
271     }
272 
273     int errCode = Connect();
274     if (errCode != ERR_OK) {
275         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
276         return errCode;
277     }
278     // IPC entry
279     ErrCode resultCode = remoteProxy_->NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
280     if (resultCode != ERR_OK) {
281         HILOG_ERROR("%{public}s error, internal error occurs, error code:%{public}d.", __func__, resultCode);
282     }
283     return resultCode;
284 }
285 
286 /**
287  * @brief temp form to normal form.
288  * @param formId The Id of the form.
289  * @param callerToken Caller ability token.
290  * @return None.
291  */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)292 int FormMgr::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
293 {
294     HILOG_INFO("%{public}s called.", __func__);
295     if (formId <= 0) {
296         HILOG_ERROR("%{public}s error, passing in form id can't be negative.", __func__);
297         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
298     }
299 
300     int errCode = Connect();
301     if (errCode != ERR_OK) {
302         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
303         return errCode;
304     }
305     return remoteProxy_->CastTempForm(formId, callerToken);
306 }
307 
308 /**
309  * @brief Dump all of form storage infos.
310  * @param formInfos All of form storage infos.
311  * @return Returns ERR_OK on success, others on failure.
312  */
DumpStorageFormInfos(std::string & formInfos)313 int FormMgr::DumpStorageFormInfos(std::string &formInfos)
314 {
315     HILOG_INFO("%{public}s called.", __func__);
316     int errCode = Connect();
317     if (errCode != ERR_OK) {
318         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
319         return errCode;
320     }
321     return remoteProxy_->DumpStorageFormInfos(formInfos);
322 }
323 /**
324  * @brief Dump form info by a bundle name.
325  * @param bundleName The bundle name of form provider.
326  * @param formInfos Form infos.
327  * @return Returns ERR_OK on success, others on failure.
328  */
DumpFormInfoByBundleName(const std::string bundleName,std::string & formInfos)329 int FormMgr::DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)
330 {
331     HILOG_INFO("%{public}s called.", __func__);
332     int errCode = Connect();
333     if (errCode != ERR_OK) {
334         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
335         return errCode;
336     }
337     return remoteProxy_->DumpFormInfoByBundleName(bundleName, formInfos);
338 }
339 /**
340  * @brief Dump form info by a bundle name.
341  * @param formId The id of the form.
342  * @param formInfo Form info.
343  * @return Returns ERR_OK on success, others on failure.
344  */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo)345 int FormMgr::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)
346 {
347     HILOG_INFO("%{public}s called.", __func__);
348     int errCode = Connect();
349     if (errCode != ERR_OK) {
350         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
351         return errCode;
352     }
353     return remoteProxy_->DumpFormInfoByFormId(formId, formInfo);
354 }
355 /**
356  * @brief Dump form timer by form id.
357  * @param formId The id of the form.
358  * @param formInfo Form timer.
359  * @return Returns ERR_OK on success, others on failure.
360  */
DumpFormTimerByFormId(const std::int64_t formId,std::string & isTimingService)361 int FormMgr::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)
362 {
363     HILOG_INFO("%{public}s called.", __func__);
364     int errCode = Connect();
365     if (errCode != ERR_OK) {
366         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
367         return errCode;
368     }
369     return remoteProxy_->DumpFormTimerByFormId(formId, isTimingService);
370 }
371 /**
372  * @brief Process js message event.
373  * @param formId Indicates the unique id of form.
374  * @param want information passed to supplier.
375  * @param callerToken Caller ability token.
376  * @return Returns true if execute success, false otherwise.
377  */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)378 int FormMgr::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
379 {
380     HILOG_INFO("%{public}s called.", __func__);
381     int errCode = Connect();
382     if (errCode != ERR_OK) {
383         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
384         return errCode;
385     }
386     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
387     if (hostCaller != nullptr) {
388         HILOG_DEBUG("send message by host caller");
389         return hostCaller->MessageEvent(formId, want, callerToken);
390     }
391     return remoteProxy_->MessageEvent(formId, want, callerToken);
392 }
393 
394 /**
395  * @brief Process js router event.
396  * @param formId Indicates the unique id of form.
397  * @param want the want of the ability to start.
398  * @param callerToken Caller ability token.
399  * @return Returns true if execute success, false otherwise.
400  */
RouterEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)401 int FormMgr::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
402 {
403     HILOG_INFO("%{public}s called.", __func__);
404     int errCode = Connect();
405     if (errCode != ERR_OK) {
406         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
407         return errCode;
408     }
409     return remoteProxy_->RouterEvent(formId, want, callerToken);
410 }
411 
412 /**
413  * @brief Process Background event.
414  * @param formId Indicates the unique id of form.
415  * @param want the want of the ability to start.
416  * @param callerToken Caller ability token.
417  * @return Returns true if execute success, false otherwise.
418  */
BackgroundEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)419 int FormMgr::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
420 {
421     HILOG_INFO("%{public}s called.", __func__);
422     int errCode = Connect();
423     if (errCode != ERR_OK) {
424         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
425         return errCode;
426     }
427     return remoteProxy_->BackgroundEvent(formId, want, callerToken);
428 }
429 
430 /**
431  * @brief Set next refresh time.
432  * @param formId The id of the form.
433  * @param nextTime Next refresh time.
434  * @return Returns ERR_OK on success, others on failure.
435  */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)436 int FormMgr::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
437 {
438     HILOG_INFO("%{public}s called.", __func__);
439 
440     if (nextTime < (Constants::MIN_NEXT_TIME / Constants::SEC_PER_MIN)) {
441         HILOG_ERROR("next time less than 5 mins");
442         return ERR_APPEXECFWK_FORM_INVALID_REFRESH_TIME;
443     }
444 
445     if (GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
446         HILOG_ERROR("%{public}s, formManager is in recovering", __func__);
447         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
448     }
449 
450     int errCode = Connect();
451     if (errCode != ERR_OK) {
452         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
453         return errCode;
454     }
455     return remoteProxy_->SetNextRefreshTime(formId, nextTime);
456 }
457 
RequestPublishForm(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId)458 ErrCode FormMgr::RequestPublishForm(Want &want, bool withFormBindingData,
459                                     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId)
460 {
461     HILOG_INFO("%{public}s called.", __func__);
462     ErrCode errCode = Connect();
463     if (errCode != ERR_OK) {
464         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
465         return errCode;
466     }
467     return remoteProxy_->RequestPublishForm(want, withFormBindingData, formBindingData, formId);
468 }
469 
LifecycleUpdate(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool updateType)470 int FormMgr::LifecycleUpdate(
471     const std::vector<int64_t> &formIds,
472     const sptr<IRemoteObject> &callerToken,
473     bool updateType)
474 {
475     HILOG_INFO("%{public}s called.", __func__);
476 
477     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
478         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
479         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
480     }
481 
482     int errCode = Connect();
483     if (errCode != ERR_OK) {
484         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
485         return errCode;
486     }
487     return remoteProxy_->LifecycleUpdate(formIds, callerToken, updateType);
488 }
489 /**
490  * @brief Get fms recoverStatus.
491  *
492  * @return The current recover status.
493  */
GetRecoverStatus()494 int FormMgr::GetRecoverStatus()
495 {
496     HILOG_INFO("%{public}s called.", __func__);
497     return recoverStatus_;
498 }
499 
500 /**
501  * @brief Set fms recoverStatus.
502  *
503  * @param recoverStatus The recover status.
504  */
SetRecoverStatus(int recoverStatus)505 void FormMgr::SetRecoverStatus(int recoverStatus)
506 {
507     HILOG_INFO("%{public}s called.", __func__);
508     recoverStatus_ = recoverStatus;
509 }
510 
511 /**
512  * @brief Get the error message content.
513  *
514  * @param errCode Error code.
515  * @return Message content.
516  */
GetErrorMessage(int errCode)517 std::string FormMgr::GetErrorMessage(int errCode)
518 {
519     HILOG_INFO("%{public}s called.", __func__);
520     return FormErrors::GetInstance().GetErrorMessage(errCode);
521 }
522 
523 /**
524  * @brief Register death callback.
525  *
526  * @param deathCallback Death callback.
527  */
RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)528 void FormMgr::RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
529 {
530     HILOG_INFO("%{public}s called.", __func__);
531     if (formDeathCallback == nullptr) {
532         HILOG_ERROR("%{public}s error, form death callback is nullptr.", __func__);
533         return;
534     }
535     formDeathCallbacks_.emplace_back(formDeathCallback);
536 }
537 
538 /**
539  * @brief UnRegister death callback.
540  *
541  * @param deathCallback Death callback.
542  */
UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)543 void FormMgr::UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
544 {
545     HILOG_INFO("%{public}s called.", __func__);
546     if (formDeathCallback == nullptr) {
547         HILOG_ERROR("%{public}s error, form death callback is nullptr.", __func__);
548         return;
549     }
550 
551     // Remove the specified death callback in the vector of death callback
552     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
553     if (iter != formDeathCallbacks_.end()) {
554         formDeathCallbacks_.erase(iter);
555     }
556     HILOG_INFO("%{public}s end.", __func__);
557 }
558 
559 /**
560  * @brief Get death recipient.
561  * @return deathRecipient_.
562  */
GetDeathRecipient() const563 sptr<IRemoteObject::DeathRecipient> FormMgr::GetDeathRecipient() const
564 {
565     return deathRecipient_;
566 }
567 
568 /**
569  * @brief Check whether the specified death callback is registered in form mgr.
570  * @param formDeathCallback The specified death callback for checking.
571  * @return Return true on success, false on failure.
572  */
CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)573 bool FormMgr::CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
574 {
575     HILOG_INFO("%{public}s called.", __func__);
576     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
577     if (iter != formDeathCallbacks_.end()) {
578         return true;
579     }
580     return false;
581 }
582 
583 /**
584  * @brief Notices IRemoteBroker died.
585  * @param remote remote object.
586  */
OnRemoteDied(const wptr<IRemoteObject> & remote)587 void FormMgr::FormMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
588 {
589     HILOG_INFO("%{public}s called.", __func__);
590     if (remote == nullptr) {
591         HILOG_ERROR("%{public}s failed, remote is nullptr.", __func__);
592         return;
593     }
594 
595     if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
596         HILOG_WARN("%{public}s, fms in recovering.", __func__);
597         return;
598     }
599     // Reset proxy
600     FormMgr::GetInstance().ResetProxy(remote);
601 
602     if (!FormMgr::GetInstance().Reconnect()) {
603         HILOG_ERROR("%{public}s, form mgr service died, try to reconnect to fms failed.", __func__);
604         FormMgr::GetInstance().SetRecoverStatus(Constants::RECOVER_FAIL);
605         return;
606     }
607 
608     // refresh form host.
609     for (auto &deathCallback : FormMgr::GetInstance().formDeathCallbacks_) {
610         deathCallback->OnDeathReceived();
611     }
612     FormMgr::GetInstance().SetRecoverStatus(Constants::NOT_IN_RECOVERY);
613 }
614 
615 /**
616  * @brief Reconnect form manager service once per 1000 milliseconds,
617  *        until the connection succeeds or reaching the max retry times.
618  * @return Returns true if execute success, false otherwise.
619  */
Reconnect()620 bool FormMgr::Reconnect()
621 {
622     HILOG_INFO("%{public}s called.", __func__);
623     for (int i = 0; i < Constants::MAX_RETRY_TIME; i++) {
624         // Sleep 1000 milliseconds before reconnect.
625         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::SLEEP_TIME));
626 
627         // try to connect fms
628         if (Connect() != ERR_OK) {
629             HILOG_ERROR("%{public}s, get fms proxy fail, try again.", __func__);
630             continue;
631         }
632 
633         HILOG_INFO("%{public}s, get fms proxy success.", __func__);
634         return true;
635     }
636 
637     return false;
638 }
639 
640 /**
641  * @brief Connect form manager service.
642  * @return Returns ERR_OK on success, others on failure.
643  */
Connect()644 ErrCode FormMgr::Connect()
645 {
646     std::lock_guard<std::mutex> lock(connectMutex_);
647     if (remoteProxy_ != nullptr && !resetFlag_) {
648         return ERR_OK;
649     }
650 
651     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
652     if (systemManager == nullptr) {
653         HILOG_ERROR("%{private}s fail to get registry", __func__);
654         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
655     }
656     sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
657     if (remoteObject == nullptr) {
658         HILOG_ERROR("%{private}s fail to connect FormMgrService", __func__);
659         return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
660     }
661     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
662     if (deathRecipient_ == nullptr) {
663         HILOG_ERROR("%{public}s Failed to create FormMgrDeathRecipient!", __func__);
664         return ERR_APPEXECFWK_FORM_COMMON_CODE;
665     }
666     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
667         HILOG_ERROR("%{public}s Add death recipient to FormMgrService failed.", __func__);
668         return ERR_APPEXECFWK_FORM_COMMON_CODE;
669     }
670 
671     remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
672     HILOG_DEBUG("%{public}s Connecting FormMgrService success.", __func__);
673     return ERR_OK;
674 }
675 
676 /**
677  * @brief Reset proxy.
678  * @param remote remote object.
679  */
ResetProxy(const wptr<IRemoteObject> & remote)680 void FormMgr::ResetProxy(const wptr<IRemoteObject> &remote)
681 {
682     HILOG_INFO("%{public}s called.", __func__);
683     std::lock_guard<std::mutex> lock(connectMutex_);
684     if (remoteProxy_ == nullptr) {
685         HILOG_ERROR("%{public}s failed, remote proxy is nullptr.", __func__);
686         return;
687     }
688 
689     // set formMgr's recover status to IN_RECOVERING.
690     recoverStatus_ = Constants::IN_RECOVERING;
691 
692     // remove the death recipient
693     auto serviceRemote = remoteProxy_->AsObject();
694     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
695         serviceRemote->RemoveDeathRecipient(deathRecipient_);
696     }
697     // clearn the remote proxy
698     remoteProxy_ = nullptr;
699 }
700 
701 /**
702  * @brief Set form mgr service for test.
703  */
SetFormMgrService(sptr<IFormMgr> formMgrService)704 void FormMgr::SetFormMgrService(sptr<IFormMgr> formMgrService)
705 {
706     HILOG_INFO("%{public}s called.", __func__);
707     remoteProxy_ = formMgrService;
708 }
709 
710 /**
711  * @brief Delete the invalid forms.
712  * @param formIds Indicates the ID of the valid forms.
713  * @param callerToken Host client.
714  * @param numFormsDeleted Returns the number of the deleted forms.
715  * @return Returns ERR_OK on success, others on failure.
716  */
DeleteInvalidForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,int32_t & numFormsDeleted)717 int FormMgr::DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
718                                 int32_t &numFormsDeleted)
719 {
720     HILOG_INFO("%{public}s start.", __func__);
721 
722     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
723         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
724         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
725     }
726 
727     int errCode = Connect();
728     if (errCode != ERR_OK) {
729         HILOG_ERROR("%{public}s failed, errCode: %{public}d.", __func__, errCode);
730         return errCode;
731     }
732     int resultCode = remoteProxy_->DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
733     if (resultCode != ERR_OK) {
734         HILOG_ERROR("%{public}s error, failed to DeleteInvalidForms, error code is %{public}d.", __func__, resultCode);
735     }
736     return resultCode;
737 }
738 
739 /**
740  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
741  * @param want Indicates a set of parameters to be transparently passed to the form provider.
742  * @param callerToken Host client.
743  * @param stateInfo Returns the form's state info of the specify.
744  * @return Returns ERR_OK on success, others on failure.
745  */
AcquireFormState(const Want & want,const sptr<IRemoteObject> & callerToken,FormStateInfo & stateInfo)746 int FormMgr::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)
747 {
748     HILOG_INFO("%{public}s start.", __func__);
749 
750     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
751         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
752         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
753     }
754 
755     int errCode = Connect();
756     if (errCode != ERR_OK) {
757         HILOG_ERROR("%{public}s failed, errCode: %{public}d.", __func__, errCode);
758         return errCode;
759     }
760     int resultCode = remoteProxy_->AcquireFormState(want, callerToken, stateInfo);
761     if (resultCode != ERR_OK) {
762         HILOG_ERROR("%{public}s error, failed to AcquireFormState, error code is %{public}d.", __func__, resultCode);
763     }
764     return resultCode;
765 }
766 
767 /**
768  * @brief Notify the form is visible or not.
769  * @param formIds Indicates the ID of the forms.
770  * @param isVisible Visible or not.
771  * @param callerToken Host client.
772  * @return Returns ERR_OK on success, others on failure.
773  */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)774 int FormMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
775                                 const sptr<IRemoteObject> &callerToken)
776 {
777     HILOG_INFO("%{public}s start.", __func__);
778 
779     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
780         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
781         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
782     }
783 
784     int errCode = Connect();
785     if (errCode != ERR_OK) {
786         HILOG_ERROR("%{public}s failed, errCode: %{public}d.", __func__, errCode);
787         return errCode;
788     }
789 
790     int resultCode = remoteProxy_->NotifyFormsVisible(formIds, isVisible, callerToken);
791     if (resultCode != ERR_OK) {
792         HILOG_ERROR("%{public}s error, failed to NotifyFormsVisible, error code is %{public}d.", __func__, resultCode);
793     }
794     return resultCode;
795 }
796 
NotifyFormsPrivacyProtected(const std::vector<int64_t> & formIds,bool isProtected,const sptr<IRemoteObject> & callerToken)797 int FormMgr::NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
798                                          const sptr<IRemoteObject> &callerToken)
799 {
800     HILOG_INFO("%{public}s start.", __func__);
801 
802     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
803         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
804         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
805     }
806 
807     int errCode = Connect();
808     if (errCode != ERR_OK) {
809         HILOG_ERROR("%{public}s failed, errCode: %{public}d.", __func__, errCode);
810         return errCode;
811     }
812 
813     int resultCode = remoteProxy_->NotifyFormsPrivacyProtected(formIds, isProtected, callerToken);
814     if (resultCode != ERR_OK) {
815         HILOG_ERROR("%{public}s error, failed to NotifyFormsPrivacyProtected, error code is %{public}d.", __func__,
816             resultCode);
817     }
818     return resultCode;
819 }
820 
821 /**
822  * @brief Notify the form is enable to be updated or not.
823  * @param formIds Indicates the ID of the forms.
824  * @param isEnableUpdate enable update or not.
825  * @param callerToken Host client.
826  * @return Returns ERR_OK on success, others on failure.
827  */
NotifyFormsEnableUpdate(const std::vector<int64_t> & formIds,bool isEnableUpdate,const sptr<IRemoteObject> & callerToken)828 int FormMgr::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
829                                      const sptr<IRemoteObject> &callerToken)
830 {
831     HILOG_INFO("%{public}s start.", __func__);
832 
833     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
834         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
835         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
836     }
837 
838     int errCode = Connect();
839     if (errCode != ERR_OK) {
840         HILOG_ERROR("%{public}s failed, errCode: %{public}d.", __func__, errCode);
841         return errCode;
842     }
843 
844     int resultCode = remoteProxy_->NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
845     if (resultCode != ERR_OK) {
846         HILOG_ERROR("%{public}s error, failed to NotifyFormsEnableUpdate, error code is %{public}d.", __func__,
847             resultCode);
848     }
849     return resultCode;
850 }
851 
852 /**
853  * @brief Get All FormsInfo.
854  * @param formInfos Return the forms' information of all forms provided.
855  * @return Returns ERR_OK on success, others on failure.
856  */
GetAllFormsInfo(std::vector<FormInfo> & formInfos)857 int FormMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
858 {
859     HILOG_INFO("%{public}s start.", __func__);
860 
861     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
862         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
863         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
864     }
865 
866     int errCode = Connect();
867     if (errCode != ERR_OK) {
868         HILOG_INFO("%{public}s failed, errCode: %{public}d.", __func__, errCode);
869         return errCode;
870     }
871     int resultCode = remoteProxy_->GetAllFormsInfo(formInfos);
872     if (resultCode != ERR_OK) {
873         HILOG_ERROR("%{public}s error, failed to GetAllFormsInfo, error code is %{public}d.", __func__,
874             resultCode);
875     }
876     return resultCode;
877 }
878 
879 /**
880  * @brief Get forms info by bundle name .
881  * @param bundleName Application name.
882  * @param formInfos Return the forms' information of the specify application name.
883  * @return Returns ERR_OK on success, others on failure.
884  */
GetFormsInfoByApp(std::string & bundleName,std::vector<FormInfo> & formInfos)885 int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)
886 {
887     HILOG_INFO("%{public}s start.", __func__);
888 
889     if (bundleName.empty()) {
890         HILOG_WARN("Failed to Get forms info, because empty bundle name");
891         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
892     }
893 
894     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
895         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
896         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
897     }
898 
899     int errCode = Connect();
900     if (errCode != ERR_OK) {
901         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
902         return errCode;
903     }
904 
905     int resultCode = remoteProxy_->GetFormsInfoByApp(bundleName, formInfos);
906     if (resultCode != ERR_OK) {
907         HILOG_ERROR("%{public}s error, failed to GetFormsInfoByApp, error code is %{public}d.", __func__,
908             resultCode);
909     }
910     return resultCode;
911 }
912 /**
913  * @brief Get forms info by bundle name and module name.
914  * @param bundleName bundle name.
915  * @param moduleName Module name of hap.
916  * @param formInfos Return the forms' information of the specify bundle name and module name.
917  * @return Returns ERR_OK on success, others on failure.
918  */
GetFormsInfoByModule(std::string & bundleName,std::string & moduleName,std::vector<FormInfo> & formInfos)919 int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos)
920 {
921     HILOG_INFO("%{public}s start.", __func__);
922     if (bundleName.empty()) {
923         HILOG_WARN("Failed to Get forms info, because empty bundleName");
924         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
925     }
926 
927     if (moduleName.empty()) {
928         HILOG_WARN("Failed to Get forms info, because empty moduleName");
929         return ERR_APPEXECFWK_FORM_INVALID_MODULENAME;
930     }
931 
932     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
933         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
934         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
935     }
936 
937     int errCode = Connect();
938     if (errCode != ERR_OK) {
939         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
940         return errCode;
941     }
942 
943     int resultCode = remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos);
944     if (resultCode != ERR_OK) {
945         HILOG_ERROR("%{public}s error, failed to GetFormsInfoByApp, error code is %{public}d.", __func__, resultCode);
946     }
947     return resultCode;
948 }
GetFormsInfo(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)949 int32_t FormMgr::GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
950 {
951     HILOG_INFO("%{public}s starts.", __func__);
952     int errCode = Connect();
953     if (errCode != ERR_OK) {
954         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
955         return errCode;
956     }
957     return remoteProxy_->GetFormsInfo(filter, formInfos);
958 }
959 
IsRequestPublishFormSupported()960 bool FormMgr::IsRequestPublishFormSupported()
961 {
962     HILOG_INFO("%{public}s starts.", __func__);
963     int errCode = Connect();
964     if (errCode != ERR_OK) {
965         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
966         return false;
967     }
968     return remoteProxy_->IsRequestPublishFormSupported();
969 }
970 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken)971 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
972 {
973     HILOG_INFO("%{public}s starts.", __func__);
974     int32_t errCode = Connect();
975     if (errCode != ERR_OK) {
976         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
977         return errCode;
978     }
979     return remoteProxy_->StartAbility(want, callerToken);
980 }
981 
ShareForm(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & callerToken,int64_t requestCode)982 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId, const sptr<IRemoteObject> &callerToken,
983     int64_t requestCode)
984 {
985     HILOG_DEBUG("%{public}s called.", __func__);
986     int32_t errCode = Connect();
987     if (errCode != ERR_OK) {
988         HILOG_ERROR("share form failed, errCode:%{public}d.", errCode);
989         return errCode;
990     }
991     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
992 }
993 
CheckFMSReady()994 bool FormMgr::CheckFMSReady()
995 {
996     HILOG_INFO("%{public}s called.", __func__);
997 
998     sptr<ISystemAbilityManager> systemAbilityManager =
999         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1000     auto remoteObject = systemAbilityManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
1001     if (remoteObject == nullptr) {
1002         HILOG_INFO("%{public}s, form manager service is not ready.", __func__);
1003         return false;
1004     }
1005 
1006     return true;
1007 }
1008 
GetExternalError(int32_t innerErrorCode,int32_t & externalErrorCode,std::string & errorMsg)1009 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1010 {
1011     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1012     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1013     HILOG_DEBUG("innerErrorCode: %{public}d, externalErrorCode: %{public}d, errorMsg: %{public}s",
1014         innerErrorCode, externalErrorCode, errorMsg.c_str());
1015 }
1016 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1017 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1018 {
1019     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1020 }
1021 }  // namespace AppExecFwk
1022 }  // namespace OHOS
1023