• 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 #include "form_mgr.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_caller_mgr.h"
21 #include "form_errors.h"
22 #include "form_mgr_errors.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 {
31 
FormMgr()32 FormMgr::FormMgr()
33 {
34     HILOG_DEBUG("called.");
35 }
36 
~FormMgr()37 FormMgr::~FormMgr()
38 {
39     FMS_CALL_INFO_ENTER;
40     if (remoteProxy_ != nullptr) {
41         auto remoteObject = remoteProxy_->AsObject();
42         if (remoteObject != nullptr) {
43             remoteObject->RemoveDeathRecipient(deathRecipient_);
44         }
45     }
46 }
47 
48 /**
49  * @brief Get the error message by error code.
50  * @param errorCode the error code return form fms.
51  * @return Returns the error message detail.
52  */
GetErrorMsg(int errorCode)53 std::string FormMgr::GetErrorMsg(int errorCode)
54 {
55     return "unknown error";
56 }
57 
58 /**
59  * @brief Add form with want, send want to form manager service.
60  * @param formId The Id of the forms to add.
61  * @param want The want of the form to add.
62  * @param callerToken Caller ability token.
63  * @param formInfo Form info.
64  * @return Returns ERR_OK on success, others on failure.
65  */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)66 int FormMgr::AddForm(
67     const int64_t formId,
68     const Want &want,
69     const sptr<IRemoteObject> &callerToken,
70     FormJsInfo &formInfo)
71 {
72     HILOG_INFO("formId is %{public}" PRId64, formId);
73     int errCode = Connect();
74     if (errCode != ERR_OK) {
75         return errCode;
76     }
77     return remoteProxy_->AddForm(formId, want, callerToken, formInfo);
78 }
79 
80 /**
81  * @brief Delete forms with formIds, send formIds to form manager service.
82  * @param formId The Id of the forms to delete.
83  * @param callerToken Caller ability token.
84  * @return Returns ERR_OK on success, others on failure.
85  */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)86 int FormMgr::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
87 {
88     HILOG_INFO("formId is %{public}" PRId64, formId);
89     // check fms recover status
90     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
91         HILOG_ERROR("delete form failed, form is in recover status, can't do action on form.");
92         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
93     }
94     // check formId
95     if (formId <= 0) {
96         HILOG_ERROR("delete form failed, the passed in formId can't be negative or zero.");
97         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
98     }
99 
100     int errCode = Connect();
101     if (errCode != ERR_OK) {
102         return errCode;
103     }
104     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
105     return remoteProxy_->DeleteForm(formId, callerToken);
106 }
107 
108 /**
109  * @brief Stop rendering form.
110  * @param formId The Id of the forms to delete.
111  * @param compId The compId of the forms to delete.
112  * @return Returns ERR_OK on success, others on failure.
113 */
StopRenderingForm(const int64_t formId,const std::string & compId)114 int FormMgr::StopRenderingForm(const int64_t formId, const std::string &compId)
115 {
116     HILOG_INFO("formId is %{public}" PRId64, formId);
117     // check fms recover status
118     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
119         HILOG_ERROR("form is in recover status, can't do action on form.");
120         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
121     }
122     // check formId
123     if (formId <= 0 || compId.empty()) {
124         HILOG_ERROR("the formId is invalid or compId is empty.");
125         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
126     }
127 
128     int errCode = Connect();
129     if (errCode != ERR_OK) {
130         return errCode;
131     }
132 
133     return remoteProxy_->StopRenderingForm(formId, compId);
134 }
135 
136 /**
137  * @brief Release forms with formIds, send formIds to form manager service.
138  * @param formId The Id of the forms to release.
139  * @param callerToken Caller ability token.
140  * @param delCache Delete Cache or not.
141  * @return Returns ERR_OK on success, others on failure.
142  */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)143 int FormMgr::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
144 {
145     HILOG_INFO("formId is %{public}" PRId64 ", delCache is %{public}d", formId, delCache);
146     // check fms recover status
147     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
148         HILOG_ERROR("form is in recover status, can't do action on form.");
149         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
150     }
151     // check formId
152     if (formId <= 0) {
153         HILOG_ERROR("the passed in formId can't be negative or zero.");
154         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
155     }
156 
157     int errCode = Connect();
158     if (errCode != ERR_OK) {
159         return errCode;
160     }
161     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
162     return remoteProxy_->ReleaseForm(formId, callerToken, delCache);
163 }
164 
165 /**
166  * @brief Update form with formId, send formId to form manager service.
167  * @param formId The Id of the form to update.
168  * @param formBindingData Form binding data.
169  * @return Returns ERR_OK on success, others on failure.
170  */
UpdateForm(const int64_t formId,const FormProviderData & formBindingData,const std::vector<FormDataProxy> & formDataProxies)171 int FormMgr::UpdateForm(const int64_t formId, const FormProviderData &formBindingData,
172     const std::vector<FormDataProxy> &formDataProxies)
173 {
174     HILOG_DEBUG("called.");
175     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
176         HILOG_ERROR("UpdateForm failed, form is in recover status, can't do action on form.");
177         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
178     }
179 
180     if (formId <= 0) {
181         HILOG_ERROR(" UpdateForm failed, the passed in formId can't be negative or zero.");
182         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
183     }
184 
185     // check formBindingData
186     if (formBindingData.GetDataString().empty() && formDataProxies.empty()) {
187         HILOG_ERROR("UpdateForm failed, the formProviderData is null.");
188         return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY;
189     }
190 
191     int errCode = Connect();
192     if (errCode != ERR_OK) {
193         return errCode;
194     }
195 
196     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
197     if (hostCaller != nullptr) {
198         hostCaller->UpdateForm(formId, formBindingData);
199     } else {
200         std::vector<std::shared_ptr<FormProviderCaller>> formProviderCallers;
201         FormCallerMgr::GetInstance().GetFormProviderCaller(formId, formProviderCallers);
202         for (const auto &formProviderCaller : formProviderCallers) {
203             formProviderCaller->UpdateForm(formId, formBindingData);
204         }
205     }
206     if (formDataProxies.empty()) {
207         return remoteProxy_->UpdateForm(formId, formBindingData);
208     }
209     return remoteProxy_->UpdateProxyForm(formId, formBindingData, formDataProxies);
210 }
211 
212 /**
213  * @brief Release renderer.
214  * @param formId The Id of the forms to release.
215  * @param compId The compId of the forms to release.
216  * @return Returns ERR_OK on success, others on failure.
217 */
ReleaseRenderer(const int64_t formId,const std::string & compId)218 int FormMgr::ReleaseRenderer(const int64_t formId, const std::string &compId)
219 {
220     HILOG_INFO("%{public}s called.", __func__);
221     // check fms recover status
222     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
223         HILOG_ERROR("form is in recover status, can't do action on form.");
224         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
225     }
226     // check formId and compId
227     if (formId <= 0 || compId.empty()) {
228         HILOG_ERROR("the formId is invalid or compId is empty.");
229         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
230     }
231 
232     int errCode = Connect();
233     if (errCode != ERR_OK) {
234         return errCode;
235     }
236 
237     return remoteProxy_->ReleaseRenderer(formId, compId);
238 }
239 
240 /**
241  * @brief Notify the form service that the form user's lifecycle is updated.
242  *
243  * This should be called when form user request form.
244  *
245  * @param formId Indicates the unique id of form.
246  * @param callerToken Indicates the callback remote object of specified form user.
247  * @param want information passed to supplier.
248  * @return Returns true if execute success, false otherwise.
249  */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)250 int FormMgr::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
251 {
252     HILOG_INFO("formId is %{public}" PRId64, formId);
253     if (formId <= 0) {
254         HILOG_ERROR("The passed formid is invalid. Its value must be larger than 0.");
255         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
256     }
257     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
258         HILOG_ERROR("form is in recover status, can't do action on form.");
259         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
260     }
261     int errCode = Connect();
262     if (errCode != ERR_OK) {
263         return errCode;
264     }
265     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
266     if (hostCaller != nullptr) {
267         HILOG_DEBUG("request form by host caller");
268         return hostCaller->RequestForm(formId, callerToken, want);
269     }
270     ErrCode resultCode = remoteProxy_->RequestForm(formId, callerToken, want);
271     if (resultCode != ERR_OK) {
272         HILOG_ERROR("failed to notify the form service that the form user's lifecycle is updated, error "
273             "code is %{public}d.", resultCode);
274     }
275     return resultCode;
276 }
277 
278 /**
279  * @brief Form visible/invisible notify, send formIds to form manager service.
280  * @param formIds The Id list of the forms to notify.
281  * @param callerToken Caller ability token.
282  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
283  * @return Returns ERR_OK on success, others on failure.
284  */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)285 int FormMgr::NotifyWhetherVisibleForms(
286     const std::vector<int64_t> &formIds,
287     const sptr<IRemoteObject> &callerToken,
288     const int32_t formVisibleType)
289 {
290     HILOG_DEBUG("formVisibleType is %{public}d", formVisibleType);
291 
292     if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) {
293         HILOG_ERROR("formIds is empty or exceed 32.");
294         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
295     }
296 
297     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
298         HILOG_ERROR("form is in recover status, can't do action on form.");
299         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
300     }
301 
302     int errCode = Connect();
303     if (errCode != ERR_OK) {
304         return errCode;
305     }
306     // IPC entry
307     ErrCode resultCode = remoteProxy_->NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
308     if (resultCode != ERR_OK) {
309         HILOG_ERROR("internal error occurs, error code:%{public}d.", resultCode);
310     }
311     return resultCode;
312 }
313 
314 /**
315  * @brief temp form to normal form.
316  * @param formId The Id of the form.
317  * @param callerToken Caller ability token.
318  * @return None.
319  */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)320 int FormMgr::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
321 {
322     HILOG_INFO("formId is %{public}" PRId64, formId);
323     if (formId <= 0) {
324         HILOG_ERROR("passing in form id can't be negative.");
325         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
326     }
327 
328     int errCode = Connect();
329     if (errCode != ERR_OK) {
330         return errCode;
331     }
332     return remoteProxy_->CastTempForm(formId, callerToken);
333 }
334 
335 /**
336  * @brief Dump all of form storage infos.
337  * @param formInfos All of form storage infos.
338  * @return Returns ERR_OK on success, others on failure.
339  */
DumpStorageFormInfos(std::string & formInfos)340 int FormMgr::DumpStorageFormInfos(std::string &formInfos)
341 {
342     HILOG_DEBUG("called.");
343     int errCode = Connect();
344     if (errCode != ERR_OK) {
345         return errCode;
346     }
347     return remoteProxy_->DumpStorageFormInfos(formInfos);
348 }
349 /**
350  * @brief Dump form info by a bundle name.
351  * @param bundleName The bundle name of form provider.
352  * @param formInfos Form infos.
353  * @return Returns ERR_OK on success, others on failure.
354  */
DumpFormInfoByBundleName(const std::string bundleName,std::string & formInfos)355 int FormMgr::DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)
356 {
357     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
358     int errCode = Connect();
359     if (errCode != ERR_OK) {
360         return errCode;
361     }
362     return remoteProxy_->DumpFormInfoByBundleName(bundleName, formInfos);
363 }
364 /**
365  * @brief Dump form info by a bundle name.
366  * @param formId The id of the form.
367  * @param formInfo Form info.
368  * @return Returns ERR_OK on success, others on failure.
369  */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo)370 int FormMgr::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)
371 {
372     HILOG_INFO("formId is %{public}" PRId64, formId);
373     int errCode = Connect();
374     if (errCode != ERR_OK) {
375         return errCode;
376     }
377     return remoteProxy_->DumpFormInfoByFormId(formId, formInfo);
378 }
379 /**
380  * @brief Dump form timer by form id.
381  * @param formId The id of the form.
382  * @param formInfo Form timer.
383  * @return Returns ERR_OK on success, others on failure.
384  */
DumpFormTimerByFormId(const std::int64_t formId,std::string & isTimingService)385 int FormMgr::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)
386 {
387     HILOG_INFO("%{public}s called.", __func__);
388     int errCode = Connect();
389     if (errCode != ERR_OK) {
390         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
391         return errCode;
392     }
393     return remoteProxy_->DumpFormTimerByFormId(formId, isTimingService);
394 }
395 /**
396  * @brief Process js message event.
397  * @param formId Indicates the unique id of form.
398  * @param want information passed to supplier.
399  * @param callerToken Caller ability token.
400  * @return Returns true if execute success, false otherwise.
401  */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)402 int FormMgr::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
403 {
404     HILOG_INFO("%{public}s called.", __func__);
405     int errCode = Connect();
406     if (errCode != ERR_OK) {
407         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
408         return errCode;
409     }
410     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
411     if (hostCaller != nullptr) {
412         HILOG_DEBUG("send message by host caller");
413         return hostCaller->MessageEvent(formId, want, callerToken);
414     }
415     return remoteProxy_->MessageEvent(formId, want, callerToken);
416 }
417 
418 /**
419  * @brief Process js router event.
420  * @param formId Indicates the unique id of form.
421  * @param want the want of the ability to start.
422  * @param callerToken Caller ability token.
423  * @return Returns true if execute success, false otherwise.
424  */
RouterEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)425 int FormMgr::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
426 {
427     HILOG_INFO("%{public}s called.", __func__);
428     int errCode = Connect();
429     if (errCode != ERR_OK) {
430         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
431         return errCode;
432     }
433     return remoteProxy_->RouterEvent(formId, want, callerToken);
434 }
435 
436 /**
437  * @brief Process Background event.
438  * @param formId Indicates the unique id of form.
439  * @param want the want of the ability to start.
440  * @param callerToken Caller ability token.
441  * @return Returns true if execute success, false otherwise.
442  */
BackgroundEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)443 int FormMgr::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
444 {
445     HILOG_INFO("%{public}s called.", __func__);
446     int errCode = Connect();
447     if (errCode != ERR_OK) {
448         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
449         return errCode;
450     }
451     return remoteProxy_->BackgroundEvent(formId, want, callerToken);
452 }
453 
454 /**
455  * @brief Set next refresh time.
456  * @param formId The id of the form.
457  * @param nextTime Next refresh time.
458  * @return Returns ERR_OK on success, others on failure.
459  */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)460 int FormMgr::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
461 {
462     HILOG_INFO("%{public}s called.", __func__);
463 
464     if (nextTime < (Constants::MIN_NEXT_TIME / Constants::SEC_PER_MIN)) {
465         HILOG_ERROR("next time less than 5 mins");
466         return ERR_APPEXECFWK_FORM_INVALID_REFRESH_TIME;
467     }
468 
469     if (GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
470         HILOG_ERROR("%{public}s, formManager is in recovering", __func__);
471         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
472     }
473 
474     int errCode = Connect();
475     if (errCode != ERR_OK) {
476         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
477         return errCode;
478     }
479     return remoteProxy_->SetNextRefreshTime(formId, nextTime);
480 }
481 
RequestPublishForm(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)482 ErrCode FormMgr::RequestPublishForm(Want &want, bool withFormBindingData,
483     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
484     const std::vector<FormDataProxy> &formDataProxies)
485 {
486     HILOG_INFO("%{public}s called.", __func__);
487     ErrCode errCode = Connect();
488     if (errCode != ERR_OK) {
489         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
490         return errCode;
491     }
492     if (formDataProxies.empty()) {
493         return remoteProxy_->RequestPublishForm(want, withFormBindingData, formBindingData, formId);
494     }
495     return remoteProxy_->RequestPublishProxyForm(want, withFormBindingData, formBindingData, formId, formDataProxies);
496 }
497 
LifecycleUpdate(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool updateType)498 int FormMgr::LifecycleUpdate(
499     const std::vector<int64_t> &formIds,
500     const sptr<IRemoteObject> &callerToken,
501     bool updateType)
502 {
503     HILOG_INFO("%{public}s called.", __func__);
504 
505     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
506         HILOG_ERROR("%{public}s error, form is in recover status, can't do action on form.", __func__);
507         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
508     }
509 
510     int errCode = Connect();
511     if (errCode != ERR_OK) {
512         HILOG_ERROR("%{public}s failed errCode:%{public}d.", __func__, errCode);
513         return errCode;
514     }
515     return remoteProxy_->LifecycleUpdate(formIds, callerToken, updateType);
516 }
517 /**
518  * @brief Get fms recoverStatus.
519  *
520  * @return The current recover status.
521  */
GetRecoverStatus()522 int FormMgr::GetRecoverStatus()
523 {
524     HILOG_DEBUG("get recover status");
525     return recoverStatus_;
526 }
527 
528 /**
529  * @brief Set fms recoverStatus.
530  *
531  * @param recoverStatus The recover status.
532  */
SetRecoverStatus(int recoverStatus)533 void FormMgr::SetRecoverStatus(int recoverStatus)
534 {
535     HILOG_INFO("%{public}s called.", __func__);
536     recoverStatus_ = recoverStatus;
537 }
538 
539 /**
540  * @brief Get the error message content.
541  *
542  * @param errCode Error code.
543  * @return Message content.
544  */
GetErrorMessage(int errCode)545 std::string FormMgr::GetErrorMessage(int errCode)
546 {
547     HILOG_INFO("%{public}s called.", __func__);
548     return FormErrors::GetInstance().GetErrorMessage(errCode);
549 }
550 
551 /**
552  * @brief Register death callback.
553  *
554  * @param deathCallback Death callback.
555  */
RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)556 void FormMgr::RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
557 {
558     HILOG_INFO("%{public}s called.", __func__);
559     if (formDeathCallback == nullptr) {
560         HILOG_ERROR("%{public}s error, form death callback is nullptr.", __func__);
561         return;
562     }
563     formDeathCallbacks_.emplace_back(formDeathCallback);
564 }
565 
566 /**
567  * @brief UnRegister death callback.
568  *
569  * @param deathCallback Death callback.
570  */
UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)571 void FormMgr::UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
572 {
573     HILOG_INFO("%{public}s called.", __func__);
574     if (formDeathCallback == nullptr) {
575         HILOG_ERROR("%{public}s error, form death callback is nullptr.", __func__);
576         return;
577     }
578 
579     // Remove the specified death callback in the vector of death callback
580     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
581     if (iter != formDeathCallbacks_.end()) {
582         formDeathCallbacks_.erase(iter);
583     }
584     HILOG_INFO("%{public}s end.", __func__);
585 }
586 
587 /**
588  * @brief Get death recipient.
589  * @return deathRecipient_.
590  */
GetDeathRecipient() const591 sptr<IRemoteObject::DeathRecipient> FormMgr::GetDeathRecipient() const
592 {
593     return deathRecipient_;
594 }
595 
596 /**
597  * @brief Check whether the specified death callback is registered in form mgr.
598  * @param formDeathCallback The specified death callback for checking.
599  * @return Return true on success, false on failure.
600  */
CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)601 bool FormMgr::CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
602 {
603     HILOG_INFO("%{public}s called.", __func__);
604     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
605     if (iter != formDeathCallbacks_.end()) {
606         return true;
607     }
608     return false;
609 }
610 
611 /**
612  * @brief Notices IRemoteBroker died.
613  * @param remote remote object.
614  */
OnRemoteDied(const wptr<IRemoteObject> & remote)615 void FormMgr::FormMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
616 {
617     HILOG_INFO("%{public}s called.", __func__);
618     if (remote == nullptr) {
619         HILOG_ERROR("%{public}s failed, remote is nullptr.", __func__);
620         return;
621     }
622 
623     if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
624         HILOG_WARN("%{public}s, fms in recovering.", __func__);
625         return;
626     }
627     // Reset proxy
628     FormMgr::GetInstance().ResetProxy(remote);
629 
630     if (!FormMgr::GetInstance().Reconnect()) {
631         HILOG_ERROR("%{public}s, form mgr service died, try to reconnect to fms failed.", __func__);
632         FormMgr::GetInstance().SetRecoverStatus(Constants::RECOVER_FAIL);
633         return;
634     }
635 
636     // refresh form host.
637     for (auto &deathCallback : FormMgr::GetInstance().formDeathCallbacks_) {
638         deathCallback->OnDeathReceived();
639     }
640     FormMgr::GetInstance().SetRecoverStatus(Constants::NOT_IN_RECOVERY);
641 }
642 
643 /**
644  * @brief Reconnect form manager service once per 1000 milliseconds,
645  *        until the connection succeeds or reaching the max retry times.
646  * @return Returns true if execute success, false otherwise.
647  */
Reconnect()648 bool FormMgr::Reconnect()
649 {
650     HILOG_DEBUG("called.");
651     for (int i = 0; i < Constants::MAX_RETRY_TIME; i++) {
652         // Sleep 1000 milliseconds before reconnect.
653         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::SLEEP_TIME));
654 
655         // try to connect fms
656         if (Connect() != ERR_OK) {
657             HILOG_ERROR("%{public}s, get fms proxy fail, try again.", __func__);
658             continue;
659         }
660 
661         HILOG_INFO("%{public}s, get fms proxy success.", __func__);
662         return true;
663     }
664 
665     return false;
666 }
667 
668 /**
669  * @brief Connect form manager service.
670  * @return Returns ERR_OK on success, others on failure.
671  */
Connect()672 ErrCode FormMgr::Connect()
673 {
674     std::lock_guard<std::mutex> lock(connectMutex_);
675     if (remoteProxy_ != nullptr && !resetFlag_) {
676         return ERR_OK;
677     }
678 
679     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
680     if (systemManager == nullptr) {
681         HILOG_ERROR("fail to get registry");
682         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
683     }
684     sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
685     if (remoteObject == nullptr) {
686         HILOG_ERROR("fail to connect FormMgrService");
687         return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
688     }
689     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
690     if (deathRecipient_ == nullptr) {
691         HILOG_ERROR("Failed to create FormMgrDeathRecipient!");
692         return ERR_APPEXECFWK_FORM_COMMON_CODE;
693     }
694     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
695         HILOG_ERROR("failed to add death recipient to FormMgrService.");
696         return ERR_APPEXECFWK_FORM_COMMON_CODE;
697     }
698 
699     remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
700     if (remoteProxy_ == nullptr) {
701         HILOG_ERROR("remoteProxy_ is nullptr");
702         return ERR_APPEXECFWK_FORM_COMMON_CODE;
703     }
704     HILOG_DEBUG("Connecting FormMgrService success.");
705     return ERR_OK;
706 }
707 
708 /**
709  * @brief Reset proxy.
710  * @param remote remote object.
711  */
ResetProxy(const wptr<IRemoteObject> & remote)712 void FormMgr::ResetProxy(const wptr<IRemoteObject> &remote)
713 {
714     HILOG_DEBUG("called.");
715     std::lock_guard<std::mutex> lock(connectMutex_);
716     if (remoteProxy_ == nullptr) {
717         HILOG_ERROR("failed, remote proxy is nullptr.");
718         return;
719     }
720 
721     // set formMgr's recover status to IN_RECOVERING.
722     recoverStatus_ = Constants::IN_RECOVERING;
723 
724     // remove the death recipient
725     auto serviceRemote = remoteProxy_->AsObject();
726     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
727         serviceRemote->RemoveDeathRecipient(deathRecipient_);
728     }
729     // clearn the remote proxy
730     remoteProxy_ = nullptr;
731 }
732 
733 /**
734  * @brief Set form mgr service for test.
735  */
SetFormMgrService(sptr<IFormMgr> formMgrService)736 void FormMgr::SetFormMgrService(sptr<IFormMgr> formMgrService)
737 {
738     HILOG_DEBUG("called.");
739     std::lock_guard<std::mutex> lock(connectMutex_);
740     remoteProxy_ = formMgrService;
741 }
742 
743 /**
744  * @brief Delete the invalid forms.
745  * @param formIds Indicates the ID of the valid forms.
746  * @param callerToken Host client.
747  * @param numFormsDeleted Returns the number of the deleted forms.
748  * @return Returns ERR_OK on success, others on failure.
749  */
DeleteInvalidForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,int32_t & numFormsDeleted)750 int FormMgr::DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
751                                 int32_t &numFormsDeleted)
752 {
753     HILOG_DEBUG("called.");
754     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
755         HILOG_ERROR("form is in recover status, can't do action on form.");
756         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
757     }
758 
759     int errCode = Connect();
760     if (errCode != ERR_OK) {
761         return errCode;
762     }
763     int resultCode = remoteProxy_->DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
764     if (resultCode != ERR_OK) {
765         HILOG_ERROR("failed to DeleteInvalidForms, error code is %{public}d.", resultCode);
766     }
767     return resultCode;
768 }
769 
770 /**
771  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
772  * @param want Indicates a set of parameters to be transparently passed to the form provider.
773  * @param callerToken Host client.
774  * @param stateInfo Returns the form's state info of the specify.
775  * @return Returns ERR_OK on success, others on failure.
776  */
AcquireFormState(const Want & want,const sptr<IRemoteObject> & callerToken,FormStateInfo & stateInfo)777 int FormMgr::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)
778 {
779     HILOG_DEBUG("called.");
780     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
781         HILOG_ERROR("form is in recover status, can't do action on form.");
782         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
783     }
784 
785     int errCode = Connect();
786     if (errCode != ERR_OK) {
787         return errCode;
788     }
789     int resultCode = remoteProxy_->AcquireFormState(want, callerToken, stateInfo);
790     if (resultCode != ERR_OK) {
791         HILOG_ERROR("failed to AcquireFormState, error code is %{public}d.", resultCode);
792     }
793     return resultCode;
794 }
795 
796 /**
797  * @brief Notify the form is visible or not.
798  * @param formIds Indicates the ID of the forms.
799  * @param isVisible Visible or not.
800  * @param callerToken Host client.
801  * @return Returns ERR_OK on success, others on failure.
802  */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)803 int FormMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
804                                 const sptr<IRemoteObject> &callerToken)
805 {
806     HILOG_DEBUG("called.");
807     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
808         HILOG_ERROR("form is in recover status, can't do action on form.");
809         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
810     }
811 
812     int errCode = Connect();
813     if (errCode != ERR_OK) {
814         return errCode;
815     }
816 
817     int resultCode = remoteProxy_->NotifyFormsVisible(formIds, isVisible, callerToken);
818     if (resultCode != ERR_OK) {
819         HILOG_ERROR("failed to NotifyFormsVisible, error code is %{public}d.", resultCode);
820     }
821     return resultCode;
822 }
823 
NotifyFormsPrivacyProtected(const std::vector<int64_t> & formIds,bool isProtected,const sptr<IRemoteObject> & callerToken)824 int FormMgr::NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
825                                          const sptr<IRemoteObject> &callerToken)
826 {
827     HILOG_DEBUG("called.");
828     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
829         HILOG_ERROR("form is in recover status, can't do action on form.");
830         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
831     }
832 
833     int errCode = Connect();
834     if (errCode != ERR_OK) {
835         return errCode;
836     }
837 
838     int resultCode = remoteProxy_->NotifyFormsPrivacyProtected(formIds, isProtected, callerToken);
839     if (resultCode != ERR_OK) {
840         HILOG_ERROR("failed to NotifyFormsPrivacyProtected, error code is %{public}d.", resultCode);
841     }
842     return resultCode;
843 }
844 
845 /**
846  * @brief Notify the form is enable to be updated or not.
847  * @param formIds Indicates the ID of the forms.
848  * @param isEnableUpdate enable update or not.
849  * @param callerToken Host client.
850  * @return Returns ERR_OK on success, others on failure.
851  */
NotifyFormsEnableUpdate(const std::vector<int64_t> & formIds,bool isEnableUpdate,const sptr<IRemoteObject> & callerToken)852 int FormMgr::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
853                                      const sptr<IRemoteObject> &callerToken)
854 {
855     HILOG_DEBUG("called.");
856     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
857         HILOG_ERROR("form is in recover status, can't do action on form.");
858         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
859     }
860 
861     int errCode = Connect();
862     if (errCode != ERR_OK) {
863         return errCode;
864     }
865 
866     int resultCode = remoteProxy_->NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
867     if (resultCode != ERR_OK) {
868         HILOG_ERROR("failed to NotifyFormsEnableUpdate, error code is %{public}d.", resultCode);
869     }
870     return resultCode;
871 }
872 
873 /**
874  * @brief Get All FormsInfo.
875  * @param formInfos Return the forms' information of all forms provided.
876  * @return Returns ERR_OK on success, others on failure.
877  */
GetAllFormsInfo(std::vector<FormInfo> & formInfos)878 int FormMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
879 {
880     HILOG_DEBUG("called.");
881     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
882         HILOG_ERROR("form is in recover status, can't do action on form.");
883         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
884     }
885 
886     int errCode = Connect();
887     if (errCode != ERR_OK) {
888         return errCode;
889     }
890     int resultCode = remoteProxy_->GetAllFormsInfo(formInfos);
891     if (resultCode != ERR_OK) {
892         HILOG_ERROR("failed to GetAllFormsInfo, error code is %{public}d.", resultCode);
893     }
894     return resultCode;
895 }
896 
897 /**
898  * @brief Get forms info by bundle name .
899  * @param bundleName Application name.
900  * @param formInfos Return the forms' information of the specify application name.
901  * @return Returns ERR_OK on success, others on failure.
902  */
GetFormsInfoByApp(std::string & bundleName,std::vector<FormInfo> & formInfos)903 int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)
904 {
905     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
906     if (bundleName.empty()) {
907         HILOG_WARN("Failed to Get forms info, because empty bundle name");
908         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
909     }
910 
911     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
912         HILOG_ERROR("form is in recover status, can't do action on form.");
913         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
914     }
915 
916     int errCode = Connect();
917     if (errCode != ERR_OK) {
918         return errCode;
919     }
920 
921     int resultCode = remoteProxy_->GetFormsInfoByApp(bundleName, formInfos);
922     if (resultCode != ERR_OK) {
923         HILOG_ERROR("failed to GetFormsInfoByApp, error code is %{public}d.", resultCode);
924     }
925     return resultCode;
926 }
927 /**
928  * @brief Get forms info by bundle name and module name.
929  * @param bundleName bundle name.
930  * @param moduleName Module name of hap.
931  * @param formInfos Return the forms' information of the specify bundle name and module name.
932  * @return Returns ERR_OK on success, others on failure.
933  */
GetFormsInfoByModule(std::string & bundleName,std::string & moduleName,std::vector<FormInfo> & formInfos)934 int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName,
935     std::vector<FormInfo> &formInfos)
936 {
937     HILOG_DEBUG("bundleName is %{public}s, moduleName is %{public}s", bundleName.c_str(), moduleName.c_str());
938     if (bundleName.empty()) {
939         HILOG_WARN("Failed to Get forms info, because empty bundleName");
940         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
941     }
942 
943     if (moduleName.empty()) {
944         HILOG_WARN("Failed to Get forms info, because empty moduleName");
945         return ERR_APPEXECFWK_FORM_INVALID_MODULENAME;
946     }
947 
948     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
949         HILOG_ERROR("form is in recover status, can't do action on form.");
950         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
951     }
952 
953     int errCode = Connect();
954     if (errCode != ERR_OK) {
955         return errCode;
956     }
957 
958     int resultCode = remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos);
959     if (resultCode != ERR_OK) {
960         HILOG_ERROR("failed to GetFormsInfoByApp, error code is %{public}d.", resultCode);
961     }
962     return resultCode;
963 }
GetFormsInfo(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)964 int32_t FormMgr::GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
965 {
966     HILOG_DEBUG("called.");
967     int errCode = Connect();
968     if (errCode != ERR_OK) {
969         return errCode;
970     }
971     return remoteProxy_->GetFormsInfo(filter, formInfos);
972 }
973 
IsRequestPublishFormSupported()974 bool FormMgr::IsRequestPublishFormSupported()
975 {
976     HILOG_DEBUG("called.");
977     int errCode = Connect();
978     if (errCode != ERR_OK) {
979         return false;
980     }
981     return remoteProxy_->IsRequestPublishFormSupported();
982 }
983 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken)984 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
985 {
986     HILOG_DEBUG("called.");
987     int32_t errCode = Connect();
988     if (errCode != ERR_OK) {
989         return errCode;
990     }
991     return remoteProxy_->StartAbility(want, callerToken);
992 }
993 
ShareForm(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & callerToken,int64_t requestCode)994 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId,
995     const sptr<IRemoteObject> &callerToken, int64_t requestCode)
996 {
997     HILOG_INFO("formId is %{public}" PRId64, formId);
998     int32_t errCode = Connect();
999     if (errCode != ERR_OK) {
1000         return errCode;
1001     }
1002     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
1003 }
1004 
AcquireFormData(int64_t formId,int64_t requestCode,const sptr<IRemoteObject> & callerToken,AAFwk::WantParams & formData)1005 int32_t FormMgr::AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
1006     AAFwk::WantParams &formData)
1007 {
1008     HILOG_INFO("formId is %{public}" PRId64, formId);
1009     int32_t errCode = Connect();
1010     if (errCode != ERR_OK) {
1011         return errCode;
1012     }
1013     return remoteProxy_->AcquireFormData(formId, requestCode, callerToken, formData);
1014 }
1015 
CheckFMSReady()1016 bool FormMgr::CheckFMSReady()
1017 {
1018     HILOG_DEBUG("called.");
1019     int32_t errCode = Connect();
1020     if (errCode != ERR_OK) {
1021         return false;
1022     }
1023     bool resultCode = remoteProxy_->CheckFMSReady();
1024     if (resultCode == false) {
1025         HILOG_ERROR("CheckFMSReady failed.");
1026     }
1027     return resultCode;
1028 }
1029 
RegisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1030 int32_t FormMgr::RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1031 {
1032     HILOG_DEBUG("called");
1033     int32_t errCode = Connect();
1034     if (errCode != ERR_OK) {
1035         HILOG_ERROR("register publish form failed, errCode:%{public}d.", errCode);
1036         return errCode;
1037     }
1038     return remoteProxy_->RegisterPublishFormInterceptor(interceptorCallback);
1039 }
1040 
UnregisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1041 int32_t FormMgr::UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1042 {
1043     HILOG_DEBUG("called");
1044     int32_t errCode = Connect();
1045     if (errCode != ERR_OK) {
1046         return errCode;
1047     }
1048     return remoteProxy_->UnregisterPublishFormInterceptor(interceptorCallback);
1049 }
1050 
GetExternalError(int32_t innerErrorCode,int32_t & externalErrorCode,std::string & errorMsg)1051 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1052 {
1053     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1054     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1055     HILOG_DEBUG("innerErrorCode: %{public}d, externalErrorCode: %{public}d, errorMsg: %{public}s",
1056         innerErrorCode, externalErrorCode, errorMsg.c_str());
1057 }
1058 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1059 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1060 {
1061     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1062 }
1063 
GetRunningFormInfos(bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1064 ErrCode FormMgr::GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1065 {
1066     HILOG_INFO("isUnusedIncluded is %{public}d", isUnusedIncluded);
1067     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1068         HILOG_ERROR("error, form is in recover status, can't do action on form.");
1069         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1070     }
1071 
1072     ErrCode errCode = Connect();
1073     if (errCode != ERR_OK) {
1074         return errCode;
1075     }
1076 
1077     ErrCode resultCode = remoteProxy_->GetRunningFormInfos(isUnusedIncluded, runningFormInfos);
1078     if (resultCode != ERR_OK) {
1079         HILOG_ERROR("error, failed to GetRunningFormInfos, error code is %{public}d.", resultCode);
1080     }
1081     return resultCode;
1082 }
1083 
GetRunningFormInfosByBundleName(const std::string & bundleName,bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1084 ErrCode FormMgr::GetRunningFormInfosByBundleName(
1085     const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1086 {
1087     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1088     if (bundleName.empty()) {
1089         HILOG_WARN("Failed to Get running form infos, because empty bundleName");
1090         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1091     }
1092     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1093         HILOG_ERROR("form is in recover status, can't do action on form.");
1094         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1095     }
1096 
1097     ErrCode errCode = Connect();
1098     if (errCode != ERR_OK) {
1099         return errCode;
1100     }
1101 
1102     ErrCode resultCode = remoteProxy_->GetRunningFormInfosByBundleName(
1103         bundleName, isUnusedIncluded, runningFormInfos);
1104     if (resultCode != ERR_OK) {
1105         HILOG_ERROR("failed to GetRunningFormInfosByBundleName, error code is %{public}d.", resultCode);
1106     }
1107     return resultCode;
1108 }
1109 
RegisterFormAddObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1110 ErrCode FormMgr::RegisterFormAddObserverByBundle(const std::string bundleName,
1111     const sptr<IRemoteObject> &callerToken)
1112 {
1113     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1114     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1115         HILOG_ERROR("error, form is in recover status, can't do action on form.");
1116         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1117     }
1118     int errCode = Connect();
1119     if (errCode != ERR_OK) {
1120         return errCode;
1121     }
1122     return remoteProxy_->RegisterFormAddObserverByBundle(bundleName, callerToken);
1123 }
1124 
RegisterFormRemoveObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1125 ErrCode FormMgr::RegisterFormRemoveObserverByBundle(const std::string bundleName,
1126     const sptr<IRemoteObject> &callerToken)
1127 {
1128     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1129     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1130         HILOG_ERROR("error, form is in recover status, can't do action on form.");
1131         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1132     }
1133     int errCode = Connect();
1134     if (errCode != ERR_OK) {
1135         return errCode;
1136     }
1137     return remoteProxy_->RegisterFormRemoveObserverByBundle(bundleName, callerToken);
1138 }
1139 
GetFormsCount(bool isTempFormFlag,int32_t & formCount)1140 int32_t FormMgr::GetFormsCount(bool isTempFormFlag, int32_t &formCount)
1141 {
1142     HILOG_INFO("isTempFormFlag is %{public}d", isTempFormFlag);
1143 
1144     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1145         HILOG_ERROR("form is in recover status, can't do action on form.");
1146         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1147     }
1148 
1149     int32_t errCode = Connect();
1150     if (errCode != ERR_OK) {
1151         return errCode;
1152     }
1153     int32_t resultCode = remoteProxy_->GetFormsCount(isTempFormFlag, formCount);
1154     if (resultCode != ERR_OK) {
1155         HILOG_ERROR("failed to GetFormsCount, error code is %{public}d.", resultCode);
1156     }
1157     return resultCode;
1158 }
1159 
GetHostFormsCount(std::string & bundleName,int32_t & formCount)1160 int32_t FormMgr::GetHostFormsCount(std::string &bundleName, int32_t &formCount)
1161 {
1162     HILOG_DEBUG("bundleName is %{public}s.", bundleName.c_str());
1163 
1164     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1165         HILOG_ERROR("form is in recover status, can't do action on form.");
1166         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1167     }
1168 
1169     if (bundleName.empty()) {
1170         HILOG_WARN("Failed to Get host forms count, because empty bundleName");
1171         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1172     }
1173 
1174     int32_t errCode = Connect();
1175     if (errCode != ERR_OK) {
1176         return errCode;
1177     }
1178     int32_t resultCode = remoteProxy_->GetHostFormsCount(bundleName, formCount);
1179     if (resultCode != ERR_OK) {
1180         HILOG_ERROR("failed to GetFormsCount, error code is %{public}d.", resultCode);
1181     }
1182     return resultCode;
1183 }
1184 
GetFormInstancesByFilter(const FormInstancesFilter & formInstancesFilter,std::vector<FormInstance> & formInstances)1185 ErrCode FormMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1186     std::vector<FormInstance> &formInstances)
1187 {
1188     HILOG_DEBUG("called.");
1189     auto errCode = Connect();
1190     if (errCode != ERR_OK) {
1191         return errCode;
1192     }
1193     return remoteProxy_->GetFormInstancesByFilter(formInstancesFilter, formInstances);
1194 }
1195 
GetFormInstanceById(const int64_t formId,FormInstance & formInstance)1196 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
1197 {
1198     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1199     auto errCode = Connect();
1200     if (errCode != ERR_OK) {
1201         return errCode;
1202     }
1203     return remoteProxy_->GetFormInstanceById(formId, formInstance);
1204 }
1205 
GetFormInstanceById(const int64_t formId,bool isUnusedIncluded,FormInstance & formInstance)1206 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)
1207 {
1208     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1209     auto errCode = Connect();
1210     if (errCode != ERR_OK) {
1211         return errCode;
1212     }
1213     if (remoteProxy_ == nullptr) {
1214         HILOG_ERROR("remoteProxy_ is nullptr.");
1215         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1216     }
1217     return remoteProxy_->GetFormInstanceById(formId, isUnusedIncluded, formInstance);
1218 }
1219 
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1220 ErrCode FormMgr::RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1221 {
1222     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1223     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1224         HILOG_ERROR("error, form is in recover status, can't do action on form.");
1225         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1226     }
1227     auto errCode = Connect();
1228     if (errCode != ERR_OK) {
1229         return errCode;
1230     }
1231     return remoteProxy_->RegisterAddObserver(bundleName, callerToken);
1232 }
1233 
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1234 ErrCode FormMgr::RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1235 {
1236     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1237     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1238         HILOG_ERROR("error, form is in recover status, can't do action on form.");
1239         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1240     }
1241     auto errCode = Connect();
1242     if (errCode != ERR_OK) {
1243         return errCode;
1244     }
1245     return remoteProxy_->RegisterRemoveObserver(bundleName, callerToken);
1246 }
1247 
RegisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1248 ErrCode FormMgr::RegisterClickEventObserver(
1249     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1250 {
1251     HILOG_DEBUG("called.");
1252     if (observer == nullptr) {
1253         HILOG_ERROR("Caller token parameteris empty.");
1254         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1255     }
1256     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1257         HILOG_ERROR("Error, form is in recover status, can't do action on form.");
1258         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1259     }
1260     int errCode = Connect();
1261     if (errCode != ERR_OK) {
1262         return errCode;
1263     }
1264     return remoteProxy_->RegisterClickEventObserver(bundleName, formEventType, observer);
1265 }
1266 
UnregisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1267 ErrCode FormMgr::UnregisterClickEventObserver(
1268     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1269 {
1270     HILOG_DEBUG("called.");
1271     if (observer == nullptr) {
1272         HILOG_ERROR("Caller token parameteris empty.");
1273         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1274     }
1275     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1276         HILOG_ERROR("Error, form is in recover status, can't do action on form.");
1277         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1278     }
1279     int errCode = Connect();
1280     if (errCode != ERR_OK) {
1281         return errCode;
1282     }
1283     return remoteProxy_->UnregisterClickEventObserver(bundleName, formEventType, observer);
1284 }
1285 
RegisterFormRouterProxy(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)1286 int FormMgr::RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
1287 {
1288     HILOG_DEBUG("called.");
1289     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1290         HILOG_ERROR("Error, form is in recover status, can't do action on form.");
1291         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1292     }
1293     auto errCode = Connect();
1294     if (errCode != ERR_OK) {
1295         return errCode;
1296     }
1297     return remoteProxy_->RegisterFormRouterProxy(formIds, callerToken);
1298 }
1299 
UnregisterFormRouterProxy(const std::vector<int64_t> & formIds)1300 int FormMgr::UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)
1301 {
1302     HILOG_DEBUG("called.");
1303     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1304         HILOG_ERROR("Error, form is in recover status, can't do action on form.");
1305         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1306     }
1307     auto errCode = Connect();
1308     if (errCode != ERR_OK) {
1309         return errCode;
1310     }
1311     return remoteProxy_->UnregisterFormRouterProxy(formIds);
1312 }
1313 
SetFormsRecyclable(const std::vector<int64_t> & formIds)1314 int32_t FormMgr::SetFormsRecyclable(const std::vector<int64_t> &formIds)
1315 {
1316     HILOG_DEBUG("called.");
1317     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1318         HILOG_ERROR("form is in recover status, can't do action on form.");
1319         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1320     }
1321     if (formIds.empty()) {
1322         HILOG_ERROR("formIds is empty.");
1323         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1324     }
1325     auto errCode = Connect();
1326     if (errCode != ERR_OK) {
1327         HILOG_ERROR("errCode: %{public}d.", errCode);
1328         return errCode;
1329     }
1330     return remoteProxy_->SetFormsRecyclable(formIds);
1331 }
1332 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want)1333 int32_t FormMgr::RecycleForms(const std::vector<int64_t> &formIds, const Want &want)
1334 {
1335     HILOG_DEBUG("called.");
1336     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1337         HILOG_ERROR("form is in recover status, can't do action on form.");
1338         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1339     }
1340     if (formIds.empty()) {
1341         HILOG_ERROR("formIds is empty.");
1342         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1343     }
1344     auto errCode = Connect();
1345     if (errCode != ERR_OK) {
1346         HILOG_ERROR("errCode: %{public}d.", errCode);
1347         return errCode;
1348     }
1349     return remoteProxy_->RecycleForms(formIds, want);
1350 }
1351 
RecoverForms(const std::vector<int64_t> & formIds,const Want & want)1352 int32_t FormMgr::RecoverForms(const std::vector<int64_t> &formIds, const Want &want)
1353 {
1354     HILOG_DEBUG("called.");
1355     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1356         HILOG_ERROR("form is in recover status, can't do action on form.");
1357         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1358     }
1359     if (formIds.empty()) {
1360         HILOG_ERROR("formIds is empty.");
1361         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1362     }
1363     auto errCode = Connect();
1364     if (errCode != ERR_OK) {
1365         HILOG_ERROR("errCode: %{public}d.", errCode);
1366         return errCode;
1367     }
1368     return remoteProxy_->RecoverForms(formIds, want);
1369 }
1370 }  // namespace AppExecFwk
1371 }  // namespace OHOS
1372