1 /*
2  * Copyright (c) 2021-2024 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("call");
35 }
36 
~FormMgr()37 FormMgr::~FormMgr()
38 {
39     HILOG_INFO("call");
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:%{public}" PRId64, formId);
73     int errCode = Connect();
74     if (errCode != ERR_OK) {
75         return errCode;
76     }
77     // Perform read lock control. Do not assign a value to remoteProxy_ in subsequent operations.
78     std::shared_lock<std::shared_mutex> lock(connectMutex_);
79 
80     // To prevent the obtained value of remoteProxy_ from being null,
81     // the system checks whether the value of remoteProxy_ is null.
82     if (remoteProxy_ == nullptr) {
83         HILOG_ERROR("null remoteProxy_");
84         return ERR_APPEXECFWK_FORM_COMMON_CODE;
85     }
86     return remoteProxy_->AddForm(formId, want, callerToken, formInfo);
87 }
88 
89 /**
90  * @brief Add form with want, send want to form manager service.
91  * @param want The want of the form to add.
92  * @param runningFormInfo Running form info.
93  * @return Returns ERR_OK on success, others on failure.
94  */
CreateForm(const Want &want, RunningFormInfo &runningFormInfo)95 int FormMgr::CreateForm(const Want &want, RunningFormInfo &runningFormInfo)
96 {
97     HILOG_INFO("call");
98     int resultCode = Connect();
99     if (resultCode != ERR_OK) {
100         HILOG_ERROR("Connect failed errCode:%{public}d", resultCode);
101         return resultCode;
102     }
103     std::shared_lock<std::shared_mutex> lock(connectMutex_);
104     if (remoteProxy_ == nullptr) {
105         HILOG_ERROR("null remoteProxy_");
106         return ERR_APPEXECFWK_FORM_COMMON_CODE;
107     }
108     resultCode = remoteProxy_->CreateForm(want, runningFormInfo);
109     if (resultCode != ERR_OK) {
110         HILOG_ERROR("createForm failed,errorCode is %{public}d", resultCode);
111     }
112     HILOG_INFO("formId:%{public}s", std::to_string(runningFormInfo.formId).c_str());
113     return resultCode;
114 }
115 
116 /**
117  * @brief Delete forms with formIds, send formIds to form manager service.
118  * @param formId The Id of the forms to delete.
119  * @param callerToken Caller ability token.
120  * @return Returns ERR_OK on success, others on failure.
121  */
DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)122 int FormMgr::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
123 {
124     HILOG_INFO("formId:%{public}" PRId64, formId);
125     // check fms recover status
126     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
127         HILOG_ERROR("delete form failed,form in recover status,can't do action on form");
128         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
129     }
130     // check formId
131     if (formId <= 0) {
132         HILOG_ERROR("delete form failed,the passed in formId can't be negative or zero");
133         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
134     }
135 
136     int errCode = Connect();
137     if (errCode != ERR_OK) {
138         return errCode;
139     }
140     std::shared_lock<std::shared_mutex> lock(connectMutex_);
141     if (remoteProxy_ == nullptr) {
142         HILOG_ERROR("null remoteProxy_");
143         return ERR_APPEXECFWK_FORM_COMMON_CODE;
144     }
145     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
146     return remoteProxy_->DeleteForm(formId, callerToken);
147 }
148 
149 /**
150  * @brief Stop rendering form.
151  * @param formId The Id of the forms to delete.
152  * @param compId The compId of the forms to delete.
153  * @return Returns ERR_OK on success, others on failure.
154 */
StopRenderingForm(const int64_t formId, const std::string &compId)155 int FormMgr::StopRenderingForm(const int64_t formId, const std::string &compId)
156 {
157     HILOG_INFO("formId:%{public}" PRId64, formId);
158     // check fms recover status
159     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
160         HILOG_ERROR("form is in recover status, can't do action on form");
161         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
162     }
163     // check formId
164     if (formId <= 0 || compId.empty()) {
165         HILOG_ERROR("invalid formId or empty compId");
166         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
167     }
168 
169     int errCode = Connect();
170     if (errCode != ERR_OK) {
171         return errCode;
172     }
173     std::shared_lock<std::shared_mutex> lock(connectMutex_);
174     if (remoteProxy_ == nullptr) {
175         HILOG_ERROR("null remoteProxy_");
176         return ERR_APPEXECFWK_FORM_COMMON_CODE;
177     }
178     return remoteProxy_->StopRenderingForm(formId, compId);
179 }
180 
181 /**
182  * @brief Release forms with formIds, send formIds to form manager service.
183  * @param formId The Id of the forms to release.
184  * @param callerToken Caller ability token.
185  * @param delCache Delete Cache or not.
186  * @return Returns ERR_OK on success, others on failure.
187  */
ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)188 int FormMgr::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
189 {
190     HILOG_INFO("formId:%{public}" PRId64 ", delCache:%{public}d", formId, delCache);
191     // check fms recover status
192     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
193         HILOG_ERROR("form is in recover status, can't do action on form");
194         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
195     }
196     // check formId
197     if (formId <= 0) {
198         HILOG_ERROR("the passed in formId can't be negative or zero");
199         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
200     }
201 
202     int errCode = Connect();
203     if (errCode != ERR_OK) {
204         return errCode;
205     }
206     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
207     std::shared_lock<std::shared_mutex> lock(connectMutex_);
208     if (remoteProxy_ == nullptr) {
209         HILOG_ERROR("null remoteProxy_");
210         return ERR_APPEXECFWK_FORM_COMMON_CODE;
211     }
212     return remoteProxy_->ReleaseForm(formId, callerToken, delCache);
213 }
214 
215 /**
216  * @brief Update form with formId, send formId to form manager service.
217  * @param formId The Id of the form to update.
218  * @param formBindingData Form binding data.
219  * @return Returns ERR_OK on success, others on failure.
220  */
UpdateForm(const int64_t formId, const FormProviderData &formBindingData, const std::vector<FormDataProxy> &formDataProxies)221 int FormMgr::UpdateForm(const int64_t formId, const FormProviderData &formBindingData,
222     const std::vector<FormDataProxy> &formDataProxies)
223 {
224     HILOG_DEBUG("call");
225     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
226         HILOG_ERROR("UpdateForm failed, form is in recover status, can't do action on form");
227         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
228     }
229 
230     if (formId <= 0) {
231         HILOG_ERROR(" UpdateForm failed, the passed in formId can't be negative or zero");
232         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
233     }
234 
235     // check formBindingData
236     if (formBindingData.GetDataString().empty() && formDataProxies.empty()) {
237         HILOG_ERROR("UpdateForm failed,null formProviderData");
238         return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY;
239     }
240 
241     int errCode = Connect();
242     if (errCode != ERR_OK) {
243         return errCode;
244     }
245 
246     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
247     if (hostCaller != nullptr) {
248         hostCaller->UpdateForm(formId, formBindingData);
249     } else {
250         std::vector<std::shared_ptr<FormProviderCaller>> formProviderCallers;
251         FormCallerMgr::GetInstance().GetFormProviderCaller(formId, formProviderCallers);
252         for (const auto &formProviderCaller : formProviderCallers) {
253             formProviderCaller->UpdateForm(formId, formBindingData);
254         }
255     }
256     std::shared_lock<std::shared_mutex> lock(connectMutex_);
257     if (remoteProxy_ == nullptr) {
258         HILOG_ERROR("null remoteProxy_");
259         return ERR_APPEXECFWK_FORM_COMMON_CODE;
260     }
261     if (formDataProxies.empty()) {
262         return remoteProxy_->UpdateForm(formId, formBindingData);
263     }
264     return remoteProxy_->UpdateProxyForm(formId, formBindingData, formDataProxies);
265 }
266 
267 /**
268  * @brief Release renderer.
269  * @param formId The Id of the forms to release.
270  * @param compId The compId of the forms to release.
271  * @return Returns ERR_OK on success, others on failure.
272 */
ReleaseRenderer(const int64_t formId, const std::string &compId)273 int FormMgr::ReleaseRenderer(const int64_t formId, const std::string &compId)
274 {
275     HILOG_INFO("formId:%{public}" PRId64, formId);
276     // check fms recover status
277     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
278         HILOG_ERROR("form is in recover status, can't do action on form");
279         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
280     }
281     // check formId and compId
282     if (formId <= 0 || compId.empty()) {
283         HILOG_ERROR("invalid formId or empty compId");
284         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
285     }
286 
287     int errCode = Connect();
288     if (errCode != ERR_OK) {
289         return errCode;
290     }
291     std::shared_lock<std::shared_mutex> lock(connectMutex_);
292     if (remoteProxy_ == nullptr) {
293         HILOG_ERROR("null remoteProxy_");
294         return ERR_APPEXECFWK_FORM_COMMON_CODE;
295     }
296     return remoteProxy_->ReleaseRenderer(formId, compId);
297 }
298 
299 /**
300  * @brief Notify the form service that the form user's lifecycle is updated.
301  *
302  * This should be called when form user request form.
303  *
304  * @param formId Indicates the unique id of form.
305  * @param callerToken Indicates the callback remote object of specified form user.
306  * @param want information passed to supplier.
307  * @return Returns true if execute success, false otherwise.
308  */
RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)309 int FormMgr::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
310 {
311     HILOG_INFO("formId:%{public}" PRId64, formId);
312     if (formId <= 0) {
313         HILOG_ERROR("invalid formId");
314         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
315     }
316     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
317         HILOG_ERROR("form is in recover status, can't do action on form");
318         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
319     }
320     int errCode = Connect();
321     if (errCode != ERR_OK) {
322         return errCode;
323     }
324     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
325     if (hostCaller != nullptr) {
326         HILOG_INFO("request by host caller");
327         return hostCaller->RequestForm(formId, callerToken, want);
328     }
329     std::shared_lock<std::shared_mutex> lock(connectMutex_);
330     if (remoteProxy_ == nullptr) {
331         HILOG_ERROR("null remoteProxy_");
332         return ERR_APPEXECFWK_FORM_COMMON_CODE;
333     }
334     ErrCode resultCode = remoteProxy_->RequestForm(formId, callerToken, want);
335     if (resultCode != ERR_OK) {
336         HILOG_ERROR("fail notify the form service that the form user's lifecycle is updated"
337             "code is %{public}d", resultCode);
338     }
339     return resultCode;
340 }
341 
342 /**
343  * @brief Form visible/invisible notify, send formIds to form manager service.
344  * @param formIds The Id list of the forms to notify.
345  * @param callerToken Caller ability token.
346  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
347  * @return Returns ERR_OK on success, others on failure.
348  */
NotifyWhetherVisibleForms( const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, const int32_t formVisibleType)349 int FormMgr::NotifyWhetherVisibleForms(
350     const std::vector<int64_t> &formIds,
351     const sptr<IRemoteObject> &callerToken,
352     const int32_t formVisibleType)
353 {
354     HILOG_DEBUG("formVisibleType is %{public}d", formVisibleType);
355 
356     if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) {
357         HILOG_ERROR("empty formIds or exceed 32");
358         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
359     }
360 
361     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
362         HILOG_ERROR("form is in recover status, can't do action on form");
363         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
364     }
365 
366     int errCode = Connect();
367     if (errCode != ERR_OK) {
368         return errCode;
369     }
370     std::shared_lock<std::shared_mutex> lock(connectMutex_);
371     if (remoteProxy_ == nullptr) {
372         HILOG_ERROR("null remoteProxy_");
373         return ERR_APPEXECFWK_FORM_COMMON_CODE;
374     }
375     // IPC entry
376     ErrCode resultCode = remoteProxy_->NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
377     if (resultCode != ERR_OK) {
378         HILOG_ERROR("internal error occurs,errCode:%{public}d", resultCode);
379     }
380     return resultCode;
381 }
382 
383 /**
384  * @brief Query whether has visible form by tokenId.
385  * @param tokenId Unique identification of application.
386  * @return Returns true if has visible form, false otherwise.
387  */
HasFormVisible(const uint32_t tokenId)388 bool FormMgr::HasFormVisible(const uint32_t tokenId)
389 {
390     HILOG_DEBUG("call");
391     int errCode = Connect();
392     if (errCode != ERR_OK) {
393         HILOG_ERROR("errCode:%{public}d", errCode);
394         return false;
395     }
396     std::shared_lock<std::shared_mutex> lock(connectMutex_);
397     if (remoteProxy_ == nullptr) {
398         HILOG_ERROR("null remoteProxy_");
399         return ERR_APPEXECFWK_FORM_COMMON_CODE;
400     }
401     return remoteProxy_->HasFormVisible(tokenId);
402 }
403 
404 /**
405  * @brief temp form to normal form.
406  * @param formId The Id of the form.
407  * @param callerToken Caller ability token.
408  * @return None.
409  */
CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)410 int FormMgr::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
411 {
412     HILOG_INFO("formId:%{public}" PRId64, formId);
413     if (formId <= 0) {
414         HILOG_ERROR("passing in form id can't be negative");
415         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
416     }
417 
418     int errCode = Connect();
419     if (errCode != ERR_OK) {
420         return errCode;
421     }
422     std::shared_lock<std::shared_mutex> lock(connectMutex_);
423     if (remoteProxy_ == nullptr) {
424         HILOG_ERROR("null remoteProxy_");
425         return ERR_APPEXECFWK_FORM_COMMON_CODE;
426     }
427     return remoteProxy_->CastTempForm(formId, callerToken);
428 }
429 
430 /**
431  * @brief Dump all of form storage infos.
432  * @param formInfos All of form storage infos.
433  * @return Returns ERR_OK on success, others on failure.
434  */
DumpStorageFormInfos(std::string &formInfos)435 int FormMgr::DumpStorageFormInfos(std::string &formInfos)
436 {
437     HILOG_DEBUG("call");
438     int errCode = Connect();
439     if (errCode != ERR_OK) {
440         return errCode;
441     }
442     std::shared_lock<std::shared_mutex> lock(connectMutex_);
443     if (remoteProxy_ == nullptr) {
444         HILOG_ERROR("null remoteProxy_");
445         return ERR_APPEXECFWK_FORM_COMMON_CODE;
446     }
447     return remoteProxy_->DumpStorageFormInfos(formInfos);
448 }
449 /**
450  * @brief Dump form info by a bundle name.
451  * @param bundleName The bundle name of form provider.
452  * @param formInfos Form infos.
453  * @return Returns ERR_OK on success, others on failure.
454  */
DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)455 int FormMgr::DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)
456 {
457     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
458     int errCode = Connect();
459     if (errCode != ERR_OK) {
460         return errCode;
461     }
462     std::shared_lock<std::shared_mutex> lock(connectMutex_);
463     if (remoteProxy_ == nullptr) {
464         HILOG_ERROR("null remoteProxy_");
465         return ERR_APPEXECFWK_FORM_COMMON_CODE;
466     }
467     return remoteProxy_->DumpFormInfoByBundleName(bundleName, formInfos);
468 }
469 /**
470  * @brief Dump form info by a bundle name.
471  * @param formId The id of the form.
472  * @param formInfo Form info.
473  * @return Returns ERR_OK on success, others on failure.
474  */
DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)475 int FormMgr::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)
476 {
477     HILOG_INFO("formId:%{public}" PRId64, formId);
478     int errCode = Connect();
479     if (errCode != ERR_OK) {
480         return errCode;
481     }
482     std::shared_lock<std::shared_mutex> lock(connectMutex_);
483     if (remoteProxy_ == nullptr) {
484         HILOG_ERROR("null remoteProxy_");
485         return ERR_APPEXECFWK_FORM_COMMON_CODE;
486     }
487     return remoteProxy_->DumpFormInfoByFormId(formId, formInfo);
488 }
489 /**
490  * @brief Dump form timer by form id.
491  * @param formId The id of the form.
492  * @param formInfo Form timer.
493  * @return Returns ERR_OK on success, others on failure.
494  */
DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)495 int FormMgr::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)
496 {
497     HILOG_INFO("call");
498     int errCode = Connect();
499     if (errCode != ERR_OK) {
500         HILOG_ERROR("errCode:%{public}d", errCode);
501         return errCode;
502     }
503     std::shared_lock<std::shared_mutex> lock(connectMutex_);
504     if (remoteProxy_ == nullptr) {
505         HILOG_ERROR("null remoteProxy_");
506         return ERR_APPEXECFWK_FORM_COMMON_CODE;
507     }
508     return remoteProxy_->DumpFormTimerByFormId(formId, isTimingService);
509 }
510 /**
511  * @brief Process js message event.
512  * @param formId Indicates the unique id of form.
513  * @param want information passed to supplier.
514  * @param callerToken Caller ability token.
515  * @return Returns true if execute success, false otherwise.
516  */
MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)517 int FormMgr::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
518 {
519     HILOG_INFO("call");
520     int errCode = Connect();
521     if (errCode != ERR_OK) {
522         HILOG_ERROR("errCode:%{public}d", errCode);
523         return errCode;
524     }
525     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
526     if (hostCaller != nullptr) {
527         HILOG_DEBUG("send message by host caller");
528         return hostCaller->MessageEvent(formId, want, callerToken);
529     }
530     std::shared_lock<std::shared_mutex> lock(connectMutex_);
531     if (remoteProxy_ == nullptr) {
532         HILOG_ERROR("null remoteProxy_");
533         return ERR_APPEXECFWK_FORM_COMMON_CODE;
534     }
535     return remoteProxy_->MessageEvent(formId, want, callerToken);
536 }
537 
538 /**
539  * @brief Process js router event.
540  * @param formId Indicates the unique id of form.
541  * @param want the want of the ability to start.
542  * @param callerToken Caller ability token.
543  * @return Returns true if execute success, false otherwise.
544  */
RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)545 int FormMgr::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
546 {
547     HILOG_INFO("call");
548     int errCode = Connect();
549     if (errCode != ERR_OK) {
550         HILOG_ERROR("errCode:%{public}d", errCode);
551         return errCode;
552     }
553     std::shared_lock<std::shared_mutex> lock(connectMutex_);
554     if (remoteProxy_ == nullptr) {
555         HILOG_ERROR("null remoteProxy_");
556         return ERR_APPEXECFWK_FORM_COMMON_CODE;
557     }
558     return remoteProxy_->RouterEvent(formId, want, callerToken);
559 }
560 
561 /**
562  * @brief Process Background event.
563  * @param formId Indicates the unique id of form.
564  * @param want the want of the ability to start.
565  * @param callerToken Caller ability token.
566  * @return Returns true if execute success, false otherwise.
567  */
BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)568 int FormMgr::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
569 {
570     HILOG_INFO("call");
571     int errCode = Connect();
572     if (errCode != ERR_OK) {
573         HILOG_ERROR("errCode:%{public}d", errCode);
574         return errCode;
575     }
576     std::shared_lock<std::shared_mutex> lock(connectMutex_);
577     if (remoteProxy_ == nullptr) {
578         HILOG_ERROR("null remoteProxy_");
579         return ERR_APPEXECFWK_FORM_COMMON_CODE;
580     }
581     return remoteProxy_->BackgroundEvent(formId, want, callerToken);
582 }
583 
584 /**
585  * @brief Set next refresh time.
586  * @param formId The id of the form.
587  * @param nextTime Next refresh time.
588  * @return Returns ERR_OK on success, others on failure.
589  */
SetNextRefreshTime(const int64_t formId, const int64_t nextTime)590 int FormMgr::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
591 {
592     HILOG_INFO("call");
593 
594     if (nextTime < (Constants::MIN_NEXT_TIME / Constants::SEC_PER_MIN)) {
595         HILOG_ERROR("next time less than 5 mins");
596         return ERR_APPEXECFWK_FORM_INVALID_REFRESH_TIME;
597     }
598 
599     if (GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
600         HILOG_ERROR("formManager is in recovering");
601         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
602     }
603 
604     int errCode = Connect();
605     if (errCode != ERR_OK) {
606         HILOG_ERROR("errCode:%{public}d", errCode);
607         return errCode;
608     }
609     std::shared_lock<std::shared_mutex> lock(connectMutex_);
610     if (remoteProxy_ == nullptr) {
611         HILOG_ERROR("null remoteProxy_");
612         return ERR_APPEXECFWK_FORM_COMMON_CODE;
613     }
614     return remoteProxy_->SetNextRefreshTime(formId, nextTime);
615 }
616 
RequestPublishForm(Want &want, bool withFormBindingData, std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId, const std::vector<FormDataProxy> &formDataProxies)617 ErrCode FormMgr::RequestPublishForm(Want &want, bool withFormBindingData,
618     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
619     const std::vector<FormDataProxy> &formDataProxies)
620 {
621     HILOG_INFO("call");
622     ErrCode errCode = Connect();
623     if (errCode != ERR_OK) {
624         HILOG_ERROR("errCode:%{public}d", errCode);
625         return errCode;
626     }
627     std::shared_lock<std::shared_mutex> lock(connectMutex_);
628     if (remoteProxy_ == nullptr) {
629         HILOG_ERROR("null remoteProxy_");
630         return ERR_APPEXECFWK_FORM_COMMON_CODE;
631     }
632     if (formDataProxies.empty()) {
633         return remoteProxy_->RequestPublishForm(want, withFormBindingData, formBindingData, formId);
634     }
635     return remoteProxy_->RequestPublishProxyForm(want, withFormBindingData, formBindingData, formId, formDataProxies);
636 }
637 
638 
SetPublishFormResult(const int64_t formId, Constants::PublishFormResult &errorCodeInfo)639 ErrCode FormMgr::SetPublishFormResult(const int64_t formId, Constants::PublishFormResult &errorCodeInfo)
640 {
641     HILOG_INFO("call");
642     if (formId <= 0) {
643         HILOG_ERROR("errCode:%{public}." PRId64, formId);
644         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
645     }
646     ErrCode errCode = Connect();
647     if (errCode != ERR_OK) {
648         HILOG_ERROR("errCode:%{public}d", errCode);
649         return errCode;
650     }
651     std::shared_lock<std::shared_mutex> lock(connectMutex_);
652     if (remoteProxy_ == nullptr) {
653         HILOG_ERROR("null remoteProxy_");
654         return ERR_APPEXECFWK_FORM_COMMON_CODE;
655     }
656     return remoteProxy_->SetPublishFormResult(formId, errorCodeInfo);
657 }
658 
AcquireAddFormResult(const int64_t formId)659 ErrCode FormMgr::AcquireAddFormResult(const int64_t formId)
660 {
661     HILOG_INFO("call");
662     if (formId <= 0) {
663         HILOG_ERROR("errCode:%{public}" PRId64, formId);
664         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
665     }
666     ErrCode errCode = Connect();
667     if (errCode != ERR_OK) {
668         HILOG_ERROR("errCode:%{public}d", errCode);
669         return errCode;
670     }
671     std::shared_lock<std::shared_mutex> lock(connectMutex_);
672     if (remoteProxy_ == nullptr) {
673         HILOG_ERROR("null remoteProxy_");
674         return ERR_APPEXECFWK_FORM_COMMON_CODE;
675     }
676     return remoteProxy_->AcquireAddFormResult(formId);
677 }
678 
LifecycleUpdate( const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, bool updateType)679 int FormMgr::LifecycleUpdate(
680     const std::vector<int64_t> &formIds,
681     const sptr<IRemoteObject> &callerToken,
682     bool updateType)
683 {
684     HILOG_INFO("call");
685 
686     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
687         HILOG_ERROR("form is in recover status, can't do action on form");
688         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
689     }
690 
691     int errCode = Connect();
692     if (errCode != ERR_OK) {
693         HILOG_ERROR("errCode:%{public}d", errCode);
694         return errCode;
695     }
696     std::shared_lock<std::shared_mutex> lock(connectMutex_);
697     if (remoteProxy_ == nullptr) {
698         HILOG_ERROR("null remoteProxy_");
699         return ERR_APPEXECFWK_FORM_COMMON_CODE;
700     }
701     return remoteProxy_->LifecycleUpdate(formIds, callerToken, updateType);
702 }
703 /**
704  * @brief Get fms recoverStatus.
705  *
706  * @return The current recover status.
707  */
GetRecoverStatus()708 int FormMgr::GetRecoverStatus()
709 {
710     HILOG_DEBUG("get recover status");
711     return recoverStatus_;
712 }
713 
714 /**
715  * @brief Set fms recoverStatus.
716  *
717  * @param recoverStatus The recover status.
718  */
SetRecoverStatus(int recoverStatus)719 void FormMgr::SetRecoverStatus(int recoverStatus)
720 {
721     HILOG_INFO("call");
722     recoverStatus_ = recoverStatus;
723 }
724 
725 /**
726  * @brief Get the error message content.
727  *
728  * @param errCode Error code.
729  * @return Message content.
730  */
GetErrorMessage(int errCode)731 std::string FormMgr::GetErrorMessage(int errCode)
732 {
733     HILOG_INFO("call");
734     return FormErrors::GetInstance().GetErrorMessage(errCode);
735 }
736 
737 /**
738  * @brief Register death callback.
739  *
740  * @param deathCallback Death callback.
741  */
RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)742 void FormMgr::RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
743 {
744     HILOG_INFO("call");
745     if (formDeathCallback == nullptr) {
746         HILOG_ERROR("null formDeathCallback");
747         return;
748     }
749     formDeathCallbacks_.emplace_back(formDeathCallback);
750 }
751 
752 /**
753  * @brief UnRegister death callback.
754  *
755  * @param deathCallback Death callback.
756  */
UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)757 void FormMgr::UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
758 {
759     HILOG_INFO("call");
760     if (formDeathCallback == nullptr) {
761         HILOG_ERROR("null formDeathCallback");
762         return;
763     }
764 
765     // Remove the specified death callback in the vector of death callback
766     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
767     if (iter != formDeathCallbacks_.end()) {
768         formDeathCallbacks_.erase(iter);
769     }
770     HILOG_INFO("end");
771 }
772 
773 /**
774  * @brief Get death recipient.
775  * @return deathRecipient_.
776  */
GetDeathRecipient() const777 sptr<IRemoteObject::DeathRecipient> FormMgr::GetDeathRecipient() const
778 {
779     return deathRecipient_;
780 }
781 
782 /**
783  * @brief Check whether the specified death callback is registered in form mgr.
784  * @param formDeathCallback The specified death callback for checking.
785  * @return Return true on success, false on failure.
786  */
CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)787 bool FormMgr::CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
788 {
789     HILOG_INFO("call");
790     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
791     if (iter != formDeathCallbacks_.end()) {
792         return true;
793     }
794     return false;
795 }
796 
797 /**
798  * @brief Notices IRemoteBroker died.
799  * @param remote remote object.
800  */
OnRemoteDied(const wptr<IRemoteObject> &remote)801 void FormMgr::FormMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
802 {
803     HILOG_INFO("call");
804     if (remote == nullptr) {
805         HILOG_ERROR("null remote");
806         return;
807     }
808 
809     if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
810         HILOG_WARN("fms in recovering");
811         return;
812     }
813     // Reset proxy
814     FormMgr::GetInstance().ResetProxy(remote);
815 
816     if (!FormMgr::GetInstance().Reconnect()) {
817         HILOG_ERROR("form mgr service died,try to reconnect to fms failed");
818         FormMgr::GetInstance().SetRecoverStatus(Constants::RECOVER_FAIL);
819         return;
820     }
821 
822     // refresh form host.
823     for (auto &deathCallback : FormMgr::GetInstance().formDeathCallbacks_) {
824         deathCallback->OnDeathReceived();
825     }
826     FormMgr::GetInstance().SetRecoverStatus(Constants::NOT_IN_RECOVERY);
827 }
828 
829 /**
830  * @brief Reconnect form manager service once per 1000 milliseconds,
831  *        until the connection succeeds or reaching the max retry times.
832  * @return Returns true if execute success, false otherwise.
833  */
Reconnect()834 bool FormMgr::Reconnect()
835 {
836     HILOG_DEBUG("call");
837     for (int i = 0; i < Constants::MAX_RETRY_TIME; i++) {
838         // Sleep 1000 milliseconds before reconnect.
839         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::SLEEP_TIME));
840 
841         // try to connect fms
842         if (Connect() != ERR_OK) {
843             HILOG_ERROR("get fms proxy fail,try again");
844             continue;
845         }
846 
847         HILOG_INFO("success");
848         return true;
849     }
850 
851     return false;
852 }
853 
854 /**
855  * @brief Connect form manager service.
856  * @return Returns ERR_OK on success, others on failure.
857  */
Connect()858 ErrCode FormMgr::Connect()
859 {
860     std::lock_guard<std::shared_mutex> lock(connectMutex_);
861     if (remoteProxy_ != nullptr && !resetFlag_) {
862         return ERR_OK;
863     }
864 
865     sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
866     if (systemManager == nullptr) {
867         HILOG_ERROR("get registry failed");
868         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
869     }
870     sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
871     if (remoteObject == nullptr) {
872         HILOG_ERROR("connect FormMgrService failed");
873         return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
874     }
875     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
876     if (deathRecipient_ == nullptr) {
877         HILOG_ERROR("null deathRecipient_");
878         return ERR_APPEXECFWK_FORM_COMMON_CODE;
879     }
880     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
881         HILOG_ERROR("fail add death recipient to FormMgrService");
882         return ERR_APPEXECFWK_FORM_COMMON_CODE;
883     }
884 
885     remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
886     if (remoteProxy_ == nullptr) {
887         HILOG_ERROR("null remoteProxy_");
888         return ERR_APPEXECFWK_FORM_COMMON_CODE;
889     }
890     HILOG_DEBUG("Connecting FormMgrService success");
891     return ERR_OK;
892 }
893 
894 /**
895  * @brief Reset proxy.
896  * @param remote remote object.
897  */
ResetProxy(const wptr<IRemoteObject> &remote)898 void FormMgr::ResetProxy(const wptr<IRemoteObject> &remote)
899 {
900     HILOG_DEBUG("call");
901     std::lock_guard<std::shared_mutex> lock(connectMutex_);
902     if (remoteProxy_ == nullptr) {
903         HILOG_ERROR("null remoteProxy_");
904         return;
905     }
906 
907     // set formMgr's recover status to IN_RECOVERING.
908     recoverStatus_ = Constants::IN_RECOVERING;
909 
910     // remove the death recipient
911     auto serviceRemote = remoteProxy_->AsObject();
912     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
913         serviceRemote->RemoveDeathRecipient(deathRecipient_);
914     }
915     // clearn the remote proxy
916     remoteProxy_ = nullptr;
917 }
918 
919 /**
920  * @brief Set form mgr service for test.
921  */
SetFormMgrService(sptr<IFormMgr> formMgrService)922 void FormMgr::SetFormMgrService(sptr<IFormMgr> formMgrService)
923 {
924     HILOG_DEBUG("call");
925     std::lock_guard<std::shared_mutex> lock(connectMutex_);
926     remoteProxy_ = formMgrService;
927 }
928 
929 /**
930  * @brief Delete the invalid forms.
931  * @param formIds Indicates the ID of the valid forms.
932  * @param callerToken Host client.
933  * @param numFormsDeleted Returns the number of the deleted forms.
934  * @return Returns ERR_OK on success, others on failure.
935  */
DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, int32_t &numFormsDeleted)936 int FormMgr::DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
937                                 int32_t &numFormsDeleted)
938 {
939     HILOG_DEBUG("call");
940     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
941         HILOG_ERROR("form is in recover status, can't do action on form");
942         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
943     }
944 
945     int errCode = Connect();
946     if (errCode != ERR_OK) {
947         return errCode;
948     }
949     std::shared_lock<std::shared_mutex> lock(connectMutex_);
950     if (remoteProxy_ == nullptr) {
951         HILOG_ERROR("null remoteProxy_");
952         return ERR_APPEXECFWK_FORM_COMMON_CODE;
953     }
954     int resultCode = remoteProxy_->DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
955     if (resultCode != ERR_OK) {
956         HILOG_ERROR("fail DeleteInvalidForms,errCode %{public}d", resultCode);
957     }
958     return resultCode;
959 }
960 
961 /**
962  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
963  * @param want Indicates a set of parameters to be transparently passed to the form provider.
964  * @param callerToken Host client.
965  * @param stateInfo Returns the form's state info of the specify.
966  * @return Returns ERR_OK on success, others on failure.
967  */
AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)968 int FormMgr::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)
969 {
970     HILOG_DEBUG("call");
971     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
972         HILOG_ERROR("form is in recover status, can't do action on form");
973         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
974     }
975 
976     int errCode = Connect();
977     if (errCode != ERR_OK) {
978         return errCode;
979     }
980     std::shared_lock<std::shared_mutex> lock(connectMutex_);
981     if (remoteProxy_ == nullptr) {
982         HILOG_ERROR("null remoteProxy_");
983         return ERR_APPEXECFWK_FORM_COMMON_CODE;
984     }
985     int resultCode = remoteProxy_->AcquireFormState(want, callerToken, stateInfo);
986     if (resultCode != ERR_OK) {
987         HILOG_ERROR("fail AcquireFormState,errCode %{public}d", resultCode);
988     }
989     return resultCode;
990 }
991 
992 /**
993  * @brief Notify the form is visible or not.
994  * @param formIds Indicates the ID of the forms.
995  * @param isVisible Visible or not.
996  * @param callerToken Host client.
997  * @return Returns ERR_OK on success, others on failure.
998  */
NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, const sptr<IRemoteObject> &callerToken)999 int FormMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
1000                                 const sptr<IRemoteObject> &callerToken)
1001 {
1002     HILOG_DEBUG("call");
1003     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1004         HILOG_ERROR("form is in recover status, can't do action on form");
1005         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1006     }
1007 
1008     int errCode = Connect();
1009     if (errCode != ERR_OK) {
1010         return errCode;
1011     }
1012     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1013     if (remoteProxy_ == nullptr) {
1014         HILOG_ERROR("null remoteProxy_");
1015         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1016     }
1017     int resultCode = remoteProxy_->NotifyFormsVisible(formIds, isVisible, callerToken);
1018     if (resultCode != ERR_OK) {
1019         HILOG_ERROR("fail NotifyFormsVisible,errCode %{public}d", resultCode);
1020     }
1021     return resultCode;
1022 }
1023 
NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected, const sptr<IRemoteObject> &callerToken)1024 int FormMgr::NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
1025                                          const sptr<IRemoteObject> &callerToken)
1026 {
1027     HILOG_DEBUG("call");
1028     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1029         HILOG_ERROR("form is in recover status, can't do action on form");
1030         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1031     }
1032 
1033     int errCode = Connect();
1034     if (errCode != ERR_OK) {
1035         return errCode;
1036     }
1037     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1038     if (remoteProxy_ == nullptr) {
1039         HILOG_ERROR("null remoteProxy_");
1040         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1041     }
1042     int resultCode = remoteProxy_->NotifyFormsPrivacyProtected(formIds, isProtected, callerToken);
1043     if (resultCode != ERR_OK) {
1044         HILOG_ERROR("fail NotifyFormsPrivacyProtected,errCode %{public}d", resultCode);
1045     }
1046     return resultCode;
1047 }
1048 
1049 /**
1050  * @brief Notify the form is enable to be updated or not.
1051  * @param formIds Indicates the ID of the forms.
1052  * @param isEnableUpdate enable update or not.
1053  * @param callerToken Host client.
1054  * @return Returns ERR_OK on success, others on failure.
1055  */
NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate, const sptr<IRemoteObject> &callerToken)1056 int FormMgr::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
1057                                      const sptr<IRemoteObject> &callerToken)
1058 {
1059     HILOG_DEBUG("call");
1060     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1061         HILOG_ERROR("form is in recover status, can't do action on form");
1062         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1063     }
1064 
1065     int errCode = Connect();
1066     if (errCode != ERR_OK) {
1067         return errCode;
1068     }
1069     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1070     if (remoteProxy_ == nullptr) {
1071         HILOG_ERROR("null remoteProxy_");
1072         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1073     }
1074     int resultCode = remoteProxy_->NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
1075     if (resultCode != ERR_OK) {
1076         HILOG_ERROR("fail NotifyFormsEnableUpdate,errCode %{public}d", resultCode);
1077     }
1078     return resultCode;
1079 }
1080 
1081 /**
1082  * @brief Get All FormsInfo.
1083  * @param formInfos Return the forms' information of all forms provided.
1084  * @return Returns ERR_OK on success, others on failure.
1085  */
GetAllFormsInfo(std::vector<FormInfo> &formInfos)1086 int FormMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
1087 {
1088     HILOG_DEBUG("call");
1089     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1090         HILOG_ERROR("form is in recover status, can't do action on form");
1091         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1092     }
1093 
1094     int errCode = Connect();
1095     if (errCode != ERR_OK) {
1096         return errCode;
1097     }
1098     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1099     if (remoteProxy_ == nullptr) {
1100         HILOG_ERROR("null remoteProxy_");
1101         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1102     }
1103     int resultCode = remoteProxy_->GetAllFormsInfo(formInfos);
1104     if (resultCode != ERR_OK) {
1105         HILOG_ERROR("fail GetAllFormsInfo,errCode %{public}d", resultCode);
1106     }
1107     return resultCode;
1108 }
1109 
1110 /**
1111  * @brief Get forms info by bundle name .
1112  * @param bundleName Application name.
1113  * @param formInfos Return the forms' information of the specify application name.
1114  * @return Returns ERR_OK on success, others on failure.
1115  */
GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)1116 int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)
1117 {
1118     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1119     if (bundleName.empty()) {
1120         HILOG_WARN("fail Get forms info,because empty bundle name");
1121         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1122     }
1123 
1124     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1125         HILOG_ERROR("form is in recover status, can't do action on form");
1126         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1127     }
1128 
1129     int errCode = Connect();
1130     if (errCode != ERR_OK) {
1131         return errCode;
1132     }
1133 
1134     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1135     if (remoteProxy_ == nullptr) {
1136         HILOG_ERROR("null remoteProxy_");
1137         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1138     }
1139     int resultCode = remoteProxy_->GetFormsInfoByApp(bundleName, formInfos);
1140     if (resultCode != ERR_OK) {
1141         HILOG_ERROR("fail GetFormsInfoByApp,errCode %{public}d", resultCode);
1142     }
1143     return resultCode;
1144 }
1145 /**
1146  * @brief Get forms info by bundle name and module name.
1147  * @param bundleName bundle name.
1148  * @param moduleName Module name of hap.
1149  * @param formInfos Return the forms' information of the specify bundle name and module name.
1150  * @return Returns ERR_OK on success, others on failure.
1151  */
GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector<FormInfo> &formInfos)1152 int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName,
1153     std::vector<FormInfo> &formInfos)
1154 {
1155     HILOG_DEBUG("bundleName is %{public}s, moduleName is %{public}s", bundleName.c_str(), moduleName.c_str());
1156     if (bundleName.empty()) {
1157         HILOG_WARN("fail Get forms info,because empty bundleName");
1158         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1159     }
1160 
1161     if (moduleName.empty()) {
1162         HILOG_WARN("fail Get forms info,because empty moduleName");
1163         return ERR_APPEXECFWK_FORM_INVALID_MODULENAME;
1164     }
1165 
1166     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1167         HILOG_ERROR("form is in recover status, can't do action on form");
1168         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1169     }
1170 
1171     int errCode = Connect();
1172     if (errCode != ERR_OK) {
1173         return errCode;
1174     }
1175 
1176     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1177     if (remoteProxy_ == nullptr) {
1178         HILOG_ERROR("null remoteProxy_");
1179         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1180     }
1181     int resultCode = remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos);
1182     if (resultCode != ERR_OK) {
1183         HILOG_ERROR("fail GetFormsInfoByModule,errCode %{public}d", resultCode);
1184     }
1185     return resultCode;
1186 }
1187 
GetFormsInfoByFilter(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)1188 int FormMgr::GetFormsInfoByFilter(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
1189 {
1190     HILOG_DEBUG("call");
1191     int errCode = Connect();
1192     if (errCode != ERR_OK) {
1193         return errCode;
1194     }
1195     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1196     if (remoteProxy_ == nullptr) {
1197         HILOG_ERROR("null remoteProxy_");
1198         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1199     }
1200     int resultCode = remoteProxy_->GetFormsInfoByFilter(filter, formInfos);
1201     if (resultCode != ERR_OK) {
1202         HILOG_ERROR("fail GetFormsInfoByFilter,errCode %{public}d", resultCode);
1203     }
1204     return resultCode;
1205 }
1206 
GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)1207 int32_t FormMgr::GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
1208 {
1209     HILOG_DEBUG("call");
1210     int errCode = Connect();
1211     if (errCode != ERR_OK) {
1212         return errCode;
1213     }
1214     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1215     if (remoteProxy_ == nullptr) {
1216         HILOG_ERROR("null remoteProxy_");
1217         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1218     }
1219     return remoteProxy_->GetFormsInfo(filter, formInfos);
1220 }
1221 
IsRequestPublishFormSupported()1222 bool FormMgr::IsRequestPublishFormSupported()
1223 {
1224     HILOG_DEBUG("call");
1225     int errCode = Connect();
1226     if (errCode != ERR_OK) {
1227         return false;
1228     }
1229     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1230     if (remoteProxy_ == nullptr) {
1231         HILOG_ERROR("null remoteProxy_");
1232         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1233     }
1234     return remoteProxy_->IsRequestPublishFormSupported();
1235 }
1236 
StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)1237 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
1238 {
1239     HILOG_DEBUG("call");
1240     int32_t errCode = Connect();
1241     if (errCode != ERR_OK) {
1242         return errCode;
1243     }
1244     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1245     if (remoteProxy_ == nullptr) {
1246         HILOG_ERROR("null remoteProxy_");
1247         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1248     }
1249     return remoteProxy_->StartAbility(want, callerToken);
1250 }
1251 
ShareForm(int64_t formId, const std::string &remoteDeviceId, const sptr<IRemoteObject> &callerToken, int64_t requestCode)1252 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId,
1253     const sptr<IRemoteObject> &callerToken, int64_t requestCode)
1254 {
1255     HILOG_INFO("formId:%{public}" PRId64, formId);
1256     int32_t errCode = Connect();
1257     if (errCode != ERR_OK) {
1258         return errCode;
1259     }
1260     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1261     if (remoteProxy_ == nullptr) {
1262         HILOG_ERROR("null remoteProxy_");
1263         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1264     }
1265     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
1266 }
1267 
AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken, AAFwk::WantParams &formData)1268 int32_t FormMgr::AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
1269     AAFwk::WantParams &formData)
1270 {
1271     HILOG_INFO("formId:%{public}" PRId64, formId);
1272     int32_t errCode = Connect();
1273     if (errCode != ERR_OK) {
1274         return errCode;
1275     }
1276     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1277     if (remoteProxy_ == nullptr) {
1278         HILOG_ERROR("null remoteProxy_");
1279         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1280     }
1281     return remoteProxy_->AcquireFormData(formId, requestCode, callerToken, formData);
1282 }
1283 
CheckFMSReady()1284 bool FormMgr::CheckFMSReady()
1285 {
1286     HILOG_DEBUG("call");
1287     int32_t errCode = Connect();
1288     if (errCode != ERR_OK) {
1289         return false;
1290     }
1291     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1292     if (remoteProxy_ == nullptr) {
1293         HILOG_ERROR("null remoteProxy_");
1294         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1295     }
1296     bool resultCode = remoteProxy_->CheckFMSReady();
1297     if (resultCode == false) {
1298         HILOG_ERROR("CheckFMSReady failed");
1299     }
1300     return resultCode;
1301 }
1302 
IsSystemAppForm(const std::string &bundleName)1303 bool FormMgr::IsSystemAppForm(const std::string &bundleName)
1304 {
1305     HILOG_DEBUG("check %{public}s is system form", bundleName.c_str());
1306     ErrCode errCode = Connect();
1307     if (errCode != ERR_OK) {
1308         HILOG_ERROR("errCode:%{public}d", errCode);
1309         return false;
1310     }
1311     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1312     if (remoteProxy_ == nullptr) {
1313         HILOG_ERROR("null remoteProxy_");
1314         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1315     }
1316     return remoteProxy_->IsSystemAppForm(bundleName);
1317 }
1318 
RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)1319 int32_t FormMgr::RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1320 {
1321     HILOG_DEBUG("call");
1322     int32_t errCode = Connect();
1323     if (errCode != ERR_OK) {
1324         HILOG_ERROR("register publish form failed, errCode:%{public}d", errCode);
1325         return errCode;
1326     }
1327     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1328     if (remoteProxy_ == nullptr) {
1329         HILOG_ERROR("null remoteProxy_");
1330         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1331     }
1332     return remoteProxy_->RegisterPublishFormInterceptor(interceptorCallback);
1333 }
1334 
UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)1335 int32_t FormMgr::UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1336 {
1337     HILOG_DEBUG("call");
1338     int32_t errCode = Connect();
1339     if (errCode != ERR_OK) {
1340         return errCode;
1341     }
1342     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1343     if (remoteProxy_ == nullptr) {
1344         HILOG_ERROR("null remoteProxy_");
1345         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1346     }
1347     return remoteProxy_->UnregisterPublishFormInterceptor(interceptorCallback);
1348 }
1349 
GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)1350 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1351 {
1352     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1353     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1354     HILOG_DEBUG("innerErrorCode:%{public}d, externalErrorCode:%{public}d, errorMsg:%{public}s",
1355         innerErrorCode, externalErrorCode, errorMsg.c_str());
1356 }
1357 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1358 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1359 {
1360     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1361 }
1362 
GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)1363 ErrCode FormMgr::GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1364 {
1365     HILOG_INFO("isUnusedIncluded is %{public}d", isUnusedIncluded);
1366     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1367         HILOG_ERROR("form is in recover status,can't do action on form");
1368         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1369     }
1370 
1371     ErrCode errCode = Connect();
1372     if (errCode != ERR_OK) {
1373         return errCode;
1374     }
1375     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1376     if (remoteProxy_ == nullptr) {
1377         HILOG_ERROR("null remoteProxy_");
1378         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1379     }
1380     ErrCode resultCode = remoteProxy_->GetRunningFormInfos(isUnusedIncluded, runningFormInfos);
1381     if (resultCode != ERR_OK) {
1382         HILOG_ERROR("fail GetRunningFormInfos,errCode %{public}d", resultCode);
1383     }
1384     return resultCode;
1385 }
1386 
GetRunningFormInfosByBundleName( const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)1387 ErrCode FormMgr::GetRunningFormInfosByBundleName(
1388     const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1389 {
1390     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1391     if (bundleName.empty()) {
1392         HILOG_WARN("fail Get running form infos,because empty bundleName");
1393         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1394     }
1395     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1396         HILOG_ERROR("form is in recover status, can't do action on form");
1397         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1398     }
1399 
1400     ErrCode errCode = Connect();
1401     if (errCode != ERR_OK) {
1402         return errCode;
1403     }
1404 
1405     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1406     if (remoteProxy_ == nullptr) {
1407         HILOG_ERROR("null remoteProxy_");
1408         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1409     }
1410     ErrCode resultCode = remoteProxy_->GetRunningFormInfosByBundleName(
1411         bundleName, isUnusedIncluded, runningFormInfos);
1412     if (resultCode != ERR_OK) {
1413         HILOG_ERROR("fail GetRunningFormInfosByBundleName,errCode %{public}d", resultCode);
1414     }
1415     return resultCode;
1416 }
1417 
RegisterFormAddObserverByBundle(const std::string bundleName, const sptr<IRemoteObject> &callerToken)1418 ErrCode FormMgr::RegisterFormAddObserverByBundle(const std::string bundleName,
1419     const sptr<IRemoteObject> &callerToken)
1420 {
1421     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1422     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1423         HILOG_ERROR("form is in recover status,can't do action on form");
1424         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1425     }
1426     int errCode = Connect();
1427     if (errCode != ERR_OK) {
1428         return errCode;
1429     }
1430     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1431     if (remoteProxy_ == nullptr) {
1432         HILOG_ERROR("null remoteProxy_");
1433         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1434     }
1435     return remoteProxy_->RegisterFormAddObserverByBundle(bundleName, callerToken);
1436 }
1437 
RegisterFormRemoveObserverByBundle(const std::string bundleName, const sptr<IRemoteObject> &callerToken)1438 ErrCode FormMgr::RegisterFormRemoveObserverByBundle(const std::string bundleName,
1439     const sptr<IRemoteObject> &callerToken)
1440 {
1441     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1442     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1443         HILOG_ERROR("form is in recover status,can't do action on form");
1444         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1445     }
1446     int errCode = Connect();
1447     if (errCode != ERR_OK) {
1448         return errCode;
1449     }
1450     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1451     if (remoteProxy_ == nullptr) {
1452         HILOG_ERROR("null remoteProxy_");
1453         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1454     }
1455     return remoteProxy_->RegisterFormRemoveObserverByBundle(bundleName, callerToken);
1456 }
1457 
GetFormsCount(bool isTempFormFlag, int32_t &formCount)1458 int32_t FormMgr::GetFormsCount(bool isTempFormFlag, int32_t &formCount)
1459 {
1460     HILOG_INFO("isTempFormFlag is %{public}d", isTempFormFlag);
1461 
1462     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1463         HILOG_ERROR("form is in recover status, can't do action on form");
1464         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1465     }
1466 
1467     int32_t errCode = Connect();
1468     if (errCode != ERR_OK) {
1469         return errCode;
1470     }
1471     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1472     if (remoteProxy_ == nullptr) {
1473         HILOG_ERROR("null remoteProxy_");
1474         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1475     }
1476     int32_t resultCode = remoteProxy_->GetFormsCount(isTempFormFlag, formCount);
1477     if (resultCode != ERR_OK) {
1478         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1479     }
1480     return resultCode;
1481 }
1482 
GetHostFormsCount(std::string &bundleName, int32_t &formCount)1483 int32_t FormMgr::GetHostFormsCount(std::string &bundleName, int32_t &formCount)
1484 {
1485     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1486 
1487     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1488         HILOG_ERROR("form is in recover status, can't do action on form");
1489         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1490     }
1491 
1492     if (bundleName.empty()) {
1493         HILOG_WARN("fail Get host forms count,because empty bundleName");
1494         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1495     }
1496 
1497     int32_t errCode = Connect();
1498     if (errCode != ERR_OK) {
1499         return errCode;
1500     }
1501     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1502     if (remoteProxy_ == nullptr) {
1503         HILOG_ERROR("null remoteProxy_");
1504         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1505     }
1506     int32_t resultCode = remoteProxy_->GetHostFormsCount(bundleName, formCount);
1507     if (resultCode != ERR_OK) {
1508         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1509     }
1510     return resultCode;
1511 }
1512 
GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter, std::vector<FormInstance> &formInstances)1513 ErrCode FormMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1514     std::vector<FormInstance> &formInstances)
1515 {
1516     HILOG_DEBUG("call");
1517     auto errCode = Connect();
1518     if (errCode != ERR_OK) {
1519         return errCode;
1520     }
1521     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1522     if (remoteProxy_ == nullptr) {
1523         HILOG_ERROR("null remoteProxy_");
1524         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1525     }
1526     return remoteProxy_->GetFormInstancesByFilter(formInstancesFilter, formInstances);
1527 }
1528 
GetFormInstanceById(const int64_t formId, FormInstance &formInstance)1529 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
1530 {
1531     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1532     auto errCode = Connect();
1533     if (errCode != ERR_OK) {
1534         return errCode;
1535     }
1536     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1537     if (remoteProxy_ == nullptr) {
1538         HILOG_ERROR("null remoteProxy_");
1539         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1540     }
1541     return remoteProxy_->GetFormInstanceById(formId, formInstance);
1542 }
1543 
GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)1544 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)
1545 {
1546     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1547     auto errCode = Connect();
1548     if (errCode != ERR_OK) {
1549         return errCode;
1550     }
1551     if (remoteProxy_ == nullptr) {
1552         HILOG_ERROR("null remoteProxy_");
1553         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1554     }
1555     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1556     if (remoteProxy_ == nullptr) {
1557         HILOG_ERROR("null remoteProxy_");
1558         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1559     }
1560     return remoteProxy_->GetFormInstanceById(formId, isUnusedIncluded, formInstance);
1561 }
1562 
RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)1563 ErrCode FormMgr::RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1564 {
1565     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1566     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1567         HILOG_ERROR("form is in recover status,can't do action on form");
1568         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1569     }
1570     auto errCode = Connect();
1571     if (errCode != ERR_OK) {
1572         return errCode;
1573     }
1574     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1575     if (remoteProxy_ == nullptr) {
1576         HILOG_ERROR("null remoteProxy_");
1577         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1578     }
1579     return remoteProxy_->RegisterAddObserver(bundleName, callerToken);
1580 }
1581 
RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)1582 ErrCode FormMgr::RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1583 {
1584     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1585     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1586         HILOG_ERROR("form is in recover status,can't do action on form");
1587         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1588     }
1589     auto errCode = Connect();
1590     if (errCode != ERR_OK) {
1591         return errCode;
1592     }
1593     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1594     if (remoteProxy_ == nullptr) {
1595         HILOG_ERROR("null remoteProxy_");
1596         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1597     }
1598     return remoteProxy_->RegisterRemoveObserver(bundleName, callerToken);
1599 }
1600 
RegisterClickEventObserver( const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)1601 ErrCode FormMgr::RegisterClickEventObserver(
1602     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1603 {
1604     HILOG_DEBUG("call");
1605     if (observer == nullptr) {
1606         HILOG_ERROR("empty callerTokenParameter");
1607         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1608     }
1609     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1610         HILOG_ERROR("form is in recover status,can't do action on form");
1611         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1612     }
1613     int errCode = Connect();
1614     if (errCode != ERR_OK) {
1615         HILOG_ERROR("errCode:%{public}d", errCode);
1616         return errCode;
1617     }
1618     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1619     if (remoteProxy_ == nullptr) {
1620         HILOG_ERROR("null remoteProxy_");
1621         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1622     }
1623     return remoteProxy_->RegisterClickEventObserver(bundleName, formEventType, observer);
1624 }
1625 
UnregisterClickEventObserver( const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)1626 ErrCode FormMgr::UnregisterClickEventObserver(
1627     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1628 {
1629     HILOG_DEBUG("call");
1630     if (observer == nullptr) {
1631         HILOG_ERROR("empty callerTokenParameter");
1632         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1633     }
1634     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1635         HILOG_ERROR("form is in recover status,can't do action on form");
1636         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1637     }
1638     int errCode = Connect();
1639     if (errCode != ERR_OK) {
1640         HILOG_ERROR("errCode:%{public}d", errCode);
1641         return errCode;
1642     }
1643     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1644     if (remoteProxy_ == nullptr) {
1645         HILOG_ERROR("null remoteProxy_");
1646         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1647     }
1648     return remoteProxy_->UnregisterClickEventObserver(bundleName, formEventType, observer);
1649 }
1650 
RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)1651 int FormMgr::RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
1652 {
1653     HILOG_DEBUG("call");
1654     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1655         HILOG_ERROR("form is in recover status, can't do action on form");
1656         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1657     }
1658     auto errCode = Connect();
1659     if (errCode != ERR_OK) {
1660         HILOG_ERROR("errCode:%{public}d", errCode);
1661         return errCode;
1662     }
1663     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1664     if (remoteProxy_ == nullptr) {
1665         HILOG_ERROR("null remoteProxy_");
1666         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1667     }
1668     return remoteProxy_->RegisterFormRouterProxy(formIds, callerToken);
1669 }
1670 
UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)1671 int FormMgr::UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)
1672 {
1673     HILOG_DEBUG("call");
1674     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1675         HILOG_ERROR("form is in recover status,can't do action on form");
1676         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1677     }
1678     auto errCode = Connect();
1679     if (errCode != ERR_OK) {
1680         HILOG_ERROR("errCode:%{public}d", errCode);
1681         return errCode;
1682     }
1683     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1684     if (remoteProxy_ == nullptr) {
1685         HILOG_ERROR("null remoteProxy_");
1686         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1687     }
1688     return remoteProxy_->UnregisterFormRouterProxy(formIds);
1689 }
1690 
SetFormsRecyclable(const std::vector<int64_t> &formIds)1691 int32_t FormMgr::SetFormsRecyclable(const std::vector<int64_t> &formIds)
1692 {
1693     HILOG_DEBUG("call");
1694     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1695         HILOG_ERROR("form is in recover status, can't do action on form");
1696         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1697     }
1698     if (formIds.empty()) {
1699         HILOG_ERROR("empty formIds");
1700         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1701     }
1702     auto errCode = Connect();
1703     if (errCode != ERR_OK) {
1704         HILOG_ERROR("errCode:%{public}d", errCode);
1705         return errCode;
1706     }
1707     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1708     if (remoteProxy_ == nullptr) {
1709         HILOG_ERROR("null remoteProxy_");
1710         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1711     }
1712     return remoteProxy_->SetFormsRecyclable(formIds);
1713 }
1714 
RecycleForms(const std::vector<int64_t> &formIds, const Want &want)1715 int32_t FormMgr::RecycleForms(const std::vector<int64_t> &formIds, const Want &want)
1716 {
1717     HILOG_DEBUG("call");
1718     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1719         HILOG_ERROR("form is in recover status, can't do action on form");
1720         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1721     }
1722     if (formIds.empty()) {
1723         HILOG_ERROR("empty formIds");
1724         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1725     }
1726     auto errCode = Connect();
1727     if (errCode != ERR_OK) {
1728         HILOG_ERROR("errCode:%{public}d", errCode);
1729         return errCode;
1730     }
1731     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1732     if (remoteProxy_ == nullptr) {
1733         HILOG_ERROR("null remoteProxy_");
1734         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1735     }
1736     return remoteProxy_->RecycleForms(formIds, want);
1737 }
1738 
RecoverForms(const std::vector<int64_t> &formIds, const Want &want)1739 int32_t FormMgr::RecoverForms(const std::vector<int64_t> &formIds, const Want &want)
1740 {
1741     HILOG_DEBUG("call");
1742     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1743         HILOG_ERROR("form is in recover status, can't do action on form");
1744         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1745     }
1746     if (formIds.empty()) {
1747         HILOG_ERROR("empty formIds");
1748         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1749     }
1750     auto errCode = Connect();
1751     if (errCode != ERR_OK) {
1752         HILOG_ERROR("errCode:%{public}d", errCode);
1753         return errCode;
1754     }
1755     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1756     if (remoteProxy_ == nullptr) {
1757         HILOG_ERROR("null remoteProxy_");
1758         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1759     }
1760     return remoteProxy_->RecoverForms(formIds, want);
1761 }
1762 
UpdateFormLocation(const int64_t &formId, const int32_t &formLocation)1763 ErrCode FormMgr::UpdateFormLocation(const int64_t &formId, const int32_t &formLocation)
1764 {
1765     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1766         HILOG_ERROR("form is in recover status, can't do action on form");
1767         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1768     }
1769 
1770     ErrCode errCode = Connect();
1771     if (errCode != ERR_OK) {
1772         return errCode;
1773     }
1774     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1775     if (remoteProxy_ == nullptr) {
1776         HILOG_ERROR("null remoteProxy_");
1777         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1778     }
1779     ErrCode resultCode = remoteProxy_->UpdateFormLocation(formId, formLocation);
1780     if (resultCode != ERR_OK) {
1781         HILOG_ERROR("fail UpdateFormLocation,errCode %{public}d", resultCode);
1782     }
1783     return resultCode;
1784 }
1785 
RequestPublishFormWithSnapshot(Want &want, bool withFormBindingData, std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId, const std::vector<FormDataProxy> &formDataProxies)1786 ErrCode FormMgr::RequestPublishFormWithSnapshot(Want &want, bool withFormBindingData,
1787     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
1788     const std::vector<FormDataProxy> &formDataProxies)
1789 {
1790     HILOG_INFO("call");
1791     ErrCode errCode = Connect();
1792     if (errCode != ERR_OK) {
1793         HILOG_ERROR("errCode:%{public}d", errCode);
1794         return errCode;
1795     }
1796     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1797     if (remoteProxy_ == nullptr) {
1798         HILOG_ERROR("null remoteProxy_");
1799         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1800     }
1801     return remoteProxy_->RequestPublishFormWithSnapshot(want, withFormBindingData, formBindingData, formId);
1802 }
1803 
BatchRefreshForms(const int32_t formRefreshType)1804 int32_t FormMgr::BatchRefreshForms(const int32_t formRefreshType)
1805 {
1806     HILOG_INFO("call");
1807     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1808         HILOG_ERROR("BatchRefreshForms failed, form is in recover status, can't do action on form");
1809         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1810     }
1811 
1812     if (formRefreshType < Constants::REFRESH_ALL_FORM || formRefreshType > Constants::REFRESH_ATOMIC_FORM) {
1813         HILOG_ERROR("BatchRefreshForms failed, invalid formRefreshType %{public}d", formRefreshType);
1814         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1815     }
1816 
1817     int errCode = Connect();
1818     if (errCode != ERR_OK) {
1819         HILOG_ERROR("errCode:%{public}d", errCode);
1820         return errCode;
1821     }
1822     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1823     if (remoteProxy_ == nullptr) {
1824         HILOG_ERROR("null remoteProxy_");
1825         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1826     }
1827     return remoteProxy_->BatchRefreshForms(formRefreshType);
1828 }
1829 
EnableForms(const std::string bundleName, const bool enable)1830 int32_t FormMgr::EnableForms(const std::string bundleName, const bool enable)
1831 {
1832     if (bundleName.empty()) {
1833         HILOG_ERROR("empty bundleName");
1834         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1835     }
1836     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1837         HILOG_ERROR("form is in recover status, can't do action on form");
1838         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1839     }
1840 
1841     ErrCode errCode = Connect();
1842     if (errCode != ERR_OK) {
1843         return errCode;
1844     }
1845 
1846     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1847     if (remoteProxy_ == nullptr) {
1848         HILOG_ERROR("null remoteProxy_");
1849         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1850     }
1851     ErrCode resultCode = remoteProxy_->EnableForms(bundleName, enable);
1852     if (resultCode != ERR_OK) {
1853         HILOG_ERROR("fail EnableForms,errCode %{public}d", resultCode);
1854     }
1855     return resultCode;
1856 }
1857 
IsFormBundleForbidden(const std::string &bundleName)1858 bool FormMgr::IsFormBundleForbidden(const std::string &bundleName)
1859 {
1860     ErrCode errCode = Connect();
1861     if (errCode != ERR_OK) {
1862         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1863         return false;
1864     }
1865 
1866     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1867     if (remoteProxy_ == nullptr) {
1868         HILOG_ERROR("null remoteProxy_");
1869         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1870     }
1871     return remoteProxy_->IsFormBundleForbidden(bundleName);
1872 }
1873 }  // namespace AppExecFwk
1874 }  // namespace OHOS
1875