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