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     {
861         std::shared_lock<std::shared_mutex> lock(connectMutex_);
862         if (remoteProxy_ != nullptr && !resetFlag_) {
863             return ERR_OK;
864         }
865     }
866     {
867         std::lock_guard<std::shared_mutex> lock(connectMutex_);
868         sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
869         if (systemManager == nullptr) {
870             HILOG_ERROR("get registry failed");
871             return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
872         }
873         sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
874         if (remoteObject == nullptr) {
875             HILOG_ERROR("connect FormMgrService failed");
876             return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
877         }
878         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
879         if (deathRecipient_ == nullptr) {
880             HILOG_ERROR("null deathRecipient_");
881             return ERR_APPEXECFWK_FORM_COMMON_CODE;
882         }
883         if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
884             HILOG_ERROR("fail add death recipient to FormMgrService");
885             return ERR_APPEXECFWK_FORM_COMMON_CODE;
886         }
887 
888         remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
889         if (remoteProxy_ == nullptr) {
890             HILOG_ERROR("null remoteProxy_");
891             return ERR_APPEXECFWK_FORM_COMMON_CODE;
892         }
893         HILOG_DEBUG("Connecting FormMgrService success");
894         return ERR_OK;
895     }
896 }
897 
898 /**
899  * @brief Reset proxy.
900  * @param remote remote object.
901  */
ResetProxy(const wptr<IRemoteObject> & remote)902 void FormMgr::ResetProxy(const wptr<IRemoteObject> &remote)
903 {
904     HILOG_DEBUG("call");
905     std::lock_guard<std::shared_mutex> lock(connectMutex_);
906     if (remoteProxy_ == nullptr) {
907         HILOG_ERROR("null remoteProxy_");
908         return;
909     }
910 
911     // set formMgr's recover status to IN_RECOVERING.
912     recoverStatus_ = Constants::IN_RECOVERING;
913 
914     // remove the death recipient
915     auto serviceRemote = remoteProxy_->AsObject();
916     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
917         serviceRemote->RemoveDeathRecipient(deathRecipient_);
918     }
919     // clearn the remote proxy
920     remoteProxy_ = nullptr;
921 }
922 
923 /**
924  * @brief Set form mgr service for test.
925  */
SetFormMgrService(sptr<IFormMgr> formMgrService)926 void FormMgr::SetFormMgrService(sptr<IFormMgr> formMgrService)
927 {
928     HILOG_DEBUG("call");
929     std::lock_guard<std::shared_mutex> lock(connectMutex_);
930     remoteProxy_ = formMgrService;
931 }
932 
933 /**
934  * @brief Delete the invalid forms.
935  * @param formIds Indicates the ID of the valid forms.
936  * @param callerToken Host client.
937  * @param numFormsDeleted Returns the number of the deleted forms.
938  * @return Returns ERR_OK on success, others on failure.
939  */
DeleteInvalidForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,int32_t & numFormsDeleted)940 int FormMgr::DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
941                                 int32_t &numFormsDeleted)
942 {
943     HILOG_DEBUG("call");
944     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
945         HILOG_ERROR("form is in recover status, can't do action on form");
946         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
947     }
948 
949     int errCode = Connect();
950     if (errCode != ERR_OK) {
951         return errCode;
952     }
953     std::shared_lock<std::shared_mutex> lock(connectMutex_);
954     if (remoteProxy_ == nullptr) {
955         HILOG_ERROR("null remoteProxy_");
956         return ERR_APPEXECFWK_FORM_COMMON_CODE;
957     }
958     int resultCode = remoteProxy_->DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
959     if (resultCode != ERR_OK) {
960         HILOG_ERROR("fail DeleteInvalidForms,errCode %{public}d", resultCode);
961     }
962     return resultCode;
963 }
964 
965 /**
966  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
967  * @param want Indicates a set of parameters to be transparently passed to the form provider.
968  * @param callerToken Host client.
969  * @param stateInfo Returns the form's state info of the specify.
970  * @return Returns ERR_OK on success, others on failure.
971  */
AcquireFormState(const Want & want,const sptr<IRemoteObject> & callerToken,FormStateInfo & stateInfo)972 int FormMgr::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)
973 {
974     HILOG_DEBUG("call");
975     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
976         HILOG_ERROR("form is in recover status, can't do action on form");
977         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
978     }
979 
980     int errCode = Connect();
981     if (errCode != ERR_OK) {
982         return errCode;
983     }
984     std::shared_lock<std::shared_mutex> lock(connectMutex_);
985     if (remoteProxy_ == nullptr) {
986         HILOG_ERROR("null remoteProxy_");
987         return ERR_APPEXECFWK_FORM_COMMON_CODE;
988     }
989     int resultCode = remoteProxy_->AcquireFormState(want, callerToken, stateInfo);
990     if (resultCode != ERR_OK) {
991         HILOG_ERROR("fail AcquireFormState,errCode %{public}d", resultCode);
992     }
993     return resultCode;
994 }
995 
996 /**
997  * @brief Notify the form is visible or not.
998  * @param formIds Indicates the ID of the forms.
999  * @param isVisible Visible or not.
1000  * @param callerToken Host client.
1001  * @return Returns ERR_OK on success, others on failure.
1002  */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)1003 int FormMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
1004                                 const sptr<IRemoteObject> &callerToken)
1005 {
1006     HILOG_DEBUG("call");
1007     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1008         HILOG_ERROR("form is in recover status, can't do action on form");
1009         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1010     }
1011 
1012     int errCode = Connect();
1013     if (errCode != ERR_OK) {
1014         return errCode;
1015     }
1016     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1017     if (remoteProxy_ == nullptr) {
1018         HILOG_ERROR("null remoteProxy_");
1019         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1020     }
1021     int resultCode = remoteProxy_->NotifyFormsVisible(formIds, isVisible, callerToken);
1022     if (resultCode != ERR_OK) {
1023         HILOG_ERROR("fail NotifyFormsVisible,errCode %{public}d", resultCode);
1024     }
1025     return resultCode;
1026 }
1027 
NotifyFormsPrivacyProtected(const std::vector<int64_t> & formIds,bool isProtected,const sptr<IRemoteObject> & callerToken)1028 int FormMgr::NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
1029                                          const sptr<IRemoteObject> &callerToken)
1030 {
1031     HILOG_DEBUG("call");
1032     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1033         HILOG_ERROR("form is in recover status, can't do action on form");
1034         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1035     }
1036 
1037     int errCode = Connect();
1038     if (errCode != ERR_OK) {
1039         return errCode;
1040     }
1041     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1042     if (remoteProxy_ == nullptr) {
1043         HILOG_ERROR("null remoteProxy_");
1044         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1045     }
1046     int resultCode = remoteProxy_->NotifyFormsPrivacyProtected(formIds, isProtected, callerToken);
1047     if (resultCode != ERR_OK) {
1048         HILOG_ERROR("fail NotifyFormsPrivacyProtected,errCode %{public}d", resultCode);
1049     }
1050     return resultCode;
1051 }
1052 
1053 /**
1054  * @brief Notify the form is enable to be updated or not.
1055  * @param formIds Indicates the ID of the forms.
1056  * @param isEnableUpdate enable update or not.
1057  * @param callerToken Host client.
1058  * @return Returns ERR_OK on success, others on failure.
1059  */
NotifyFormsEnableUpdate(const std::vector<int64_t> & formIds,bool isEnableUpdate,const sptr<IRemoteObject> & callerToken)1060 int FormMgr::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
1061                                      const sptr<IRemoteObject> &callerToken)
1062 {
1063     HILOG_DEBUG("call");
1064     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1065         HILOG_ERROR("form is in recover status, can't do action on form");
1066         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1067     }
1068 
1069     int errCode = Connect();
1070     if (errCode != ERR_OK) {
1071         return errCode;
1072     }
1073     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1074     if (remoteProxy_ == nullptr) {
1075         HILOG_ERROR("null remoteProxy_");
1076         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1077     }
1078     int resultCode = remoteProxy_->NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
1079     if (resultCode != ERR_OK) {
1080         HILOG_ERROR("fail NotifyFormsEnableUpdate,errCode %{public}d", resultCode);
1081     }
1082     return resultCode;
1083 }
1084 
1085 /**
1086  * @brief Get All FormsInfo.
1087  * @param formInfos Return the forms' information of all forms provided.
1088  * @return Returns ERR_OK on success, others on failure.
1089  */
GetAllFormsInfo(std::vector<FormInfo> & formInfos)1090 int FormMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
1091 {
1092     HILOG_DEBUG("call");
1093     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1094         HILOG_ERROR("form is in recover status, can't do action on form");
1095         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1096     }
1097 
1098     int errCode = Connect();
1099     if (errCode != ERR_OK) {
1100         return errCode;
1101     }
1102     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1103     if (remoteProxy_ == nullptr) {
1104         HILOG_ERROR("null remoteProxy_");
1105         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1106     }
1107     int resultCode = remoteProxy_->GetAllFormsInfo(formInfos);
1108     if (resultCode != ERR_OK) {
1109         HILOG_ERROR("fail GetAllFormsInfo,errCode %{public}d", resultCode);
1110     }
1111     return resultCode;
1112 }
1113 
1114 /**
1115  * @brief Get forms info by bundle name .
1116  * @param bundleName Application name.
1117  * @param formInfos Return the forms' information of the specify application name.
1118  * @return Returns ERR_OK on success, others on failure.
1119  */
GetFormsInfoByApp(std::string & bundleName,std::vector<FormInfo> & formInfos)1120 int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)
1121 {
1122     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1123     if (bundleName.empty()) {
1124         HILOG_WARN("fail Get forms info,because empty bundle name");
1125         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1126     }
1127 
1128     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1129         HILOG_ERROR("form is in recover status, can't do action on form");
1130         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1131     }
1132 
1133     int errCode = Connect();
1134     if (errCode != ERR_OK) {
1135         return errCode;
1136     }
1137 
1138     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1139     if (remoteProxy_ == nullptr) {
1140         HILOG_ERROR("null remoteProxy_");
1141         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1142     }
1143     int resultCode = remoteProxy_->GetFormsInfoByApp(bundleName, formInfos);
1144     if (resultCode != ERR_OK) {
1145         HILOG_ERROR("fail GetFormsInfoByApp,errCode %{public}d", resultCode);
1146     }
1147     return resultCode;
1148 }
1149 /**
1150  * @brief Get forms info by bundle name and module name.
1151  * @param bundleName bundle name.
1152  * @param moduleName Module name of hap.
1153  * @param formInfos Return the forms' information of the specify bundle name and module name.
1154  * @return Returns ERR_OK on success, others on failure.
1155  */
GetFormsInfoByModule(std::string & bundleName,std::string & moduleName,std::vector<FormInfo> & formInfos)1156 int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName,
1157     std::vector<FormInfo> &formInfos)
1158 {
1159     HILOG_DEBUG("bundleName is %{public}s, moduleName is %{public}s", bundleName.c_str(), moduleName.c_str());
1160     if (bundleName.empty()) {
1161         HILOG_WARN("fail Get forms info,because empty bundleName");
1162         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1163     }
1164 
1165     if (moduleName.empty()) {
1166         HILOG_WARN("fail Get forms info,because empty moduleName");
1167         return ERR_APPEXECFWK_FORM_INVALID_MODULENAME;
1168     }
1169 
1170     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1171         HILOG_ERROR("form is in recover status, can't do action on form");
1172         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1173     }
1174 
1175     int errCode = Connect();
1176     if (errCode != ERR_OK) {
1177         return errCode;
1178     }
1179 
1180     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1181     if (remoteProxy_ == nullptr) {
1182         HILOG_ERROR("null remoteProxy_");
1183         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1184     }
1185     int resultCode = remoteProxy_->GetFormsInfoByModule(bundleName, moduleName, formInfos);
1186     if (resultCode != ERR_OK) {
1187         HILOG_ERROR("fail GetFormsInfoByModule,errCode %{public}d", resultCode);
1188     }
1189     return resultCode;
1190 }
1191 
GetFormsInfoByFilter(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)1192 int FormMgr::GetFormsInfoByFilter(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
1193 {
1194     HILOG_DEBUG("call");
1195     int errCode = Connect();
1196     if (errCode != ERR_OK) {
1197         return errCode;
1198     }
1199     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1200     if (remoteProxy_ == nullptr) {
1201         HILOG_ERROR("null remoteProxy_");
1202         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1203     }
1204     int resultCode = remoteProxy_->GetFormsInfoByFilter(filter, formInfos);
1205     if (resultCode != ERR_OK) {
1206         HILOG_ERROR("fail GetFormsInfoByFilter,errCode %{public}d", resultCode);
1207     }
1208     return resultCode;
1209 }
1210 
GetFormsInfo(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)1211 int32_t FormMgr::GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
1212 {
1213     HILOG_DEBUG("call");
1214     int errCode = Connect();
1215     if (errCode != ERR_OK) {
1216         return errCode;
1217     }
1218     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1219     if (remoteProxy_ == nullptr) {
1220         HILOG_ERROR("null remoteProxy_");
1221         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1222     }
1223     return remoteProxy_->GetFormsInfo(filter, formInfos);
1224 }
1225 
IsRequestPublishFormSupported()1226 bool FormMgr::IsRequestPublishFormSupported()
1227 {
1228     HILOG_DEBUG("call");
1229     int errCode = Connect();
1230     if (errCode != ERR_OK) {
1231         return false;
1232     }
1233     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1234     if (remoteProxy_ == nullptr) {
1235         HILOG_ERROR("null remoteProxy_");
1236         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1237     }
1238     return remoteProxy_->IsRequestPublishFormSupported();
1239 }
1240 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken)1241 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
1242 {
1243     HILOG_DEBUG("call");
1244     int32_t errCode = Connect();
1245     if (errCode != ERR_OK) {
1246         return errCode;
1247     }
1248     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1249     if (remoteProxy_ == nullptr) {
1250         HILOG_ERROR("null remoteProxy_");
1251         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1252     }
1253     return remoteProxy_->StartAbility(want, callerToken);
1254 }
1255 
ShareForm(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & callerToken,int64_t requestCode)1256 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId,
1257     const sptr<IRemoteObject> &callerToken, int64_t requestCode)
1258 {
1259     HILOG_INFO("formId:%{public}" PRId64, formId);
1260     int32_t errCode = Connect();
1261     if (errCode != ERR_OK) {
1262         return errCode;
1263     }
1264     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1265     if (remoteProxy_ == nullptr) {
1266         HILOG_ERROR("null remoteProxy_");
1267         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1268     }
1269     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
1270 }
1271 
AcquireFormData(int64_t formId,int64_t requestCode,const sptr<IRemoteObject> & callerToken,AAFwk::WantParams & formData)1272 int32_t FormMgr::AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
1273     AAFwk::WantParams &formData)
1274 {
1275     HILOG_INFO("formId:%{public}" PRId64, formId);
1276     int32_t errCode = Connect();
1277     if (errCode != ERR_OK) {
1278         return errCode;
1279     }
1280     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1281     if (remoteProxy_ == nullptr) {
1282         HILOG_ERROR("null remoteProxy_");
1283         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1284     }
1285     return remoteProxy_->AcquireFormData(formId, requestCode, callerToken, formData);
1286 }
1287 
CheckFMSReady()1288 bool FormMgr::CheckFMSReady()
1289 {
1290     HILOG_DEBUG("call");
1291     int32_t errCode = Connect();
1292     if (errCode != ERR_OK) {
1293         return false;
1294     }
1295     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1296     if (remoteProxy_ == nullptr) {
1297         HILOG_ERROR("null remoteProxy_");
1298         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1299     }
1300     bool resultCode = remoteProxy_->CheckFMSReady();
1301     if (resultCode == false) {
1302         HILOG_ERROR("CheckFMSReady failed");
1303     }
1304     return resultCode;
1305 }
1306 
IsSystemAppForm(const std::string & bundleName)1307 bool FormMgr::IsSystemAppForm(const std::string &bundleName)
1308 {
1309     HILOG_DEBUG("check %{public}s is system form", bundleName.c_str());
1310     ErrCode errCode = Connect();
1311     if (errCode != ERR_OK) {
1312         HILOG_ERROR("errCode:%{public}d", errCode);
1313         return false;
1314     }
1315     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1316     if (remoteProxy_ == nullptr) {
1317         HILOG_ERROR("null remoteProxy_");
1318         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1319     }
1320     return remoteProxy_->IsSystemAppForm(bundleName);
1321 }
1322 
RegisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1323 int32_t FormMgr::RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1324 {
1325     HILOG_DEBUG("call");
1326     int32_t errCode = Connect();
1327     if (errCode != ERR_OK) {
1328         HILOG_ERROR("register publish form failed, errCode:%{public}d", errCode);
1329         return errCode;
1330     }
1331     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1332     if (remoteProxy_ == nullptr) {
1333         HILOG_ERROR("null remoteProxy_");
1334         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1335     }
1336     return remoteProxy_->RegisterPublishFormInterceptor(interceptorCallback);
1337 }
1338 
UnregisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1339 int32_t FormMgr::UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1340 {
1341     HILOG_DEBUG("call");
1342     int32_t errCode = Connect();
1343     if (errCode != ERR_OK) {
1344         return errCode;
1345     }
1346     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1347     if (remoteProxy_ == nullptr) {
1348         HILOG_ERROR("null remoteProxy_");
1349         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1350     }
1351     return remoteProxy_->UnregisterPublishFormInterceptor(interceptorCallback);
1352 }
1353 
GetExternalError(int32_t innerErrorCode,int32_t & externalErrorCode,std::string & errorMsg)1354 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1355 {
1356     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1357     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1358     HILOG_DEBUG("innerErrorCode:%{public}d, externalErrorCode:%{public}d, errorMsg:%{public}s",
1359         innerErrorCode, externalErrorCode, errorMsg.c_str());
1360 }
1361 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1362 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1363 {
1364     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1365 }
1366 
GetRunningFormInfos(bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1367 ErrCode FormMgr::GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1368 {
1369     HILOG_INFO("isUnusedIncluded is %{public}d", isUnusedIncluded);
1370     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1371         HILOG_ERROR("form is in recover status,can't do action on form");
1372         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1373     }
1374 
1375     ErrCode errCode = Connect();
1376     if (errCode != ERR_OK) {
1377         return errCode;
1378     }
1379     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1380     if (remoteProxy_ == nullptr) {
1381         HILOG_ERROR("null remoteProxy_");
1382         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1383     }
1384     ErrCode resultCode = remoteProxy_->GetRunningFormInfos(isUnusedIncluded, runningFormInfos);
1385     if (resultCode != ERR_OK) {
1386         HILOG_ERROR("fail GetRunningFormInfos,errCode %{public}d", resultCode);
1387     }
1388     return resultCode;
1389 }
1390 
GetRunningFormInfosByBundleName(const std::string & bundleName,bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1391 ErrCode FormMgr::GetRunningFormInfosByBundleName(
1392     const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1393 {
1394     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1395     if (bundleName.empty()) {
1396         HILOG_WARN("fail Get running form infos,because empty bundleName");
1397         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1398     }
1399     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1400         HILOG_ERROR("form is in recover status, can't do action on form");
1401         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1402     }
1403 
1404     ErrCode errCode = Connect();
1405     if (errCode != ERR_OK) {
1406         return errCode;
1407     }
1408 
1409     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1410     if (remoteProxy_ == nullptr) {
1411         HILOG_ERROR("null remoteProxy_");
1412         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1413     }
1414     ErrCode resultCode = remoteProxy_->GetRunningFormInfosByBundleName(
1415         bundleName, isUnusedIncluded, runningFormInfos);
1416     if (resultCode != ERR_OK) {
1417         HILOG_ERROR("fail GetRunningFormInfosByBundleName,errCode %{public}d", resultCode);
1418     }
1419     return resultCode;
1420 }
1421 
RegisterFormAddObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1422 ErrCode FormMgr::RegisterFormAddObserverByBundle(const std::string bundleName,
1423     const sptr<IRemoteObject> &callerToken)
1424 {
1425     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1426     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1427         HILOG_ERROR("form is in recover status,can't do action on form");
1428         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1429     }
1430     int errCode = Connect();
1431     if (errCode != ERR_OK) {
1432         return errCode;
1433     }
1434     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1435     if (remoteProxy_ == nullptr) {
1436         HILOG_ERROR("null remoteProxy_");
1437         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1438     }
1439     return remoteProxy_->RegisterFormAddObserverByBundle(bundleName, callerToken);
1440 }
1441 
RegisterFormRemoveObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1442 ErrCode FormMgr::RegisterFormRemoveObserverByBundle(const std::string bundleName,
1443     const sptr<IRemoteObject> &callerToken)
1444 {
1445     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1446     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1447         HILOG_ERROR("form is in recover status,can't do action on form");
1448         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1449     }
1450     int errCode = Connect();
1451     if (errCode != ERR_OK) {
1452         return errCode;
1453     }
1454     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1455     if (remoteProxy_ == nullptr) {
1456         HILOG_ERROR("null remoteProxy_");
1457         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1458     }
1459     return remoteProxy_->RegisterFormRemoveObserverByBundle(bundleName, callerToken);
1460 }
1461 
GetFormsCount(bool isTempFormFlag,int32_t & formCount)1462 int32_t FormMgr::GetFormsCount(bool isTempFormFlag, int32_t &formCount)
1463 {
1464     HILOG_INFO("isTempFormFlag is %{public}d", isTempFormFlag);
1465 
1466     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1467         HILOG_ERROR("form is in recover status, can't do action on form");
1468         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1469     }
1470 
1471     int32_t errCode = Connect();
1472     if (errCode != ERR_OK) {
1473         return errCode;
1474     }
1475     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1476     if (remoteProxy_ == nullptr) {
1477         HILOG_ERROR("null remoteProxy_");
1478         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1479     }
1480     int32_t resultCode = remoteProxy_->GetFormsCount(isTempFormFlag, formCount);
1481     if (resultCode != ERR_OK) {
1482         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1483     }
1484     return resultCode;
1485 }
1486 
GetHostFormsCount(std::string & bundleName,int32_t & formCount)1487 int32_t FormMgr::GetHostFormsCount(std::string &bundleName, int32_t &formCount)
1488 {
1489     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1490 
1491     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1492         HILOG_ERROR("form is in recover status, can't do action on form");
1493         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1494     }
1495 
1496     if (bundleName.empty()) {
1497         HILOG_WARN("fail Get host forms count,because empty bundleName");
1498         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1499     }
1500 
1501     int32_t errCode = Connect();
1502     if (errCode != ERR_OK) {
1503         return errCode;
1504     }
1505     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1506     if (remoteProxy_ == nullptr) {
1507         HILOG_ERROR("null remoteProxy_");
1508         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1509     }
1510     int32_t resultCode = remoteProxy_->GetHostFormsCount(bundleName, formCount);
1511     if (resultCode != ERR_OK) {
1512         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1513     }
1514     return resultCode;
1515 }
1516 
GetFormInstancesByFilter(const FormInstancesFilter & formInstancesFilter,std::vector<FormInstance> & formInstances)1517 ErrCode FormMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1518     std::vector<FormInstance> &formInstances)
1519 {
1520     HILOG_DEBUG("call");
1521     auto errCode = Connect();
1522     if (errCode != ERR_OK) {
1523         return errCode;
1524     }
1525     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1526     if (remoteProxy_ == nullptr) {
1527         HILOG_ERROR("null remoteProxy_");
1528         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1529     }
1530     return remoteProxy_->GetFormInstancesByFilter(formInstancesFilter, formInstances);
1531 }
1532 
GetFormInstanceById(const int64_t formId,FormInstance & formInstance)1533 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
1534 {
1535     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1536     auto errCode = Connect();
1537     if (errCode != ERR_OK) {
1538         return errCode;
1539     }
1540     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1541     if (remoteProxy_ == nullptr) {
1542         HILOG_ERROR("null remoteProxy_");
1543         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1544     }
1545     return remoteProxy_->GetFormInstanceById(formId, formInstance);
1546 }
1547 
GetFormInstanceById(const int64_t formId,bool isUnusedIncluded,FormInstance & formInstance)1548 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)
1549 {
1550     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1551     auto errCode = Connect();
1552     if (errCode != ERR_OK) {
1553         return errCode;
1554     }
1555     if (remoteProxy_ == nullptr) {
1556         HILOG_ERROR("null remoteProxy_");
1557         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1558     }
1559     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1560     if (remoteProxy_ == nullptr) {
1561         HILOG_ERROR("null remoteProxy_");
1562         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1563     }
1564     return remoteProxy_->GetFormInstanceById(formId, isUnusedIncluded, formInstance);
1565 }
1566 
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1567 ErrCode FormMgr::RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1568 {
1569     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1570     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1571         HILOG_ERROR("form is in recover status,can't do action on form");
1572         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1573     }
1574     auto errCode = Connect();
1575     if (errCode != ERR_OK) {
1576         return errCode;
1577     }
1578     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1579     if (remoteProxy_ == nullptr) {
1580         HILOG_ERROR("null remoteProxy_");
1581         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1582     }
1583     return remoteProxy_->RegisterAddObserver(bundleName, callerToken);
1584 }
1585 
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1586 ErrCode FormMgr::RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1587 {
1588     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1589     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1590         HILOG_ERROR("form is in recover status,can't do action on form");
1591         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1592     }
1593     auto errCode = Connect();
1594     if (errCode != ERR_OK) {
1595         return errCode;
1596     }
1597     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1598     if (remoteProxy_ == nullptr) {
1599         HILOG_ERROR("null remoteProxy_");
1600         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1601     }
1602     return remoteProxy_->RegisterRemoveObserver(bundleName, callerToken);
1603 }
1604 
RegisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1605 ErrCode FormMgr::RegisterClickEventObserver(
1606     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1607 {
1608     HILOG_DEBUG("call");
1609     if (observer == nullptr) {
1610         HILOG_ERROR("empty callerTokenParameter");
1611         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1612     }
1613     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1614         HILOG_ERROR("form is in recover status,can't do action on form");
1615         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1616     }
1617     int errCode = Connect();
1618     if (errCode != ERR_OK) {
1619         HILOG_ERROR("errCode:%{public}d", errCode);
1620         return errCode;
1621     }
1622     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1623     if (remoteProxy_ == nullptr) {
1624         HILOG_ERROR("null remoteProxy_");
1625         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1626     }
1627     return remoteProxy_->RegisterClickEventObserver(bundleName, formEventType, observer);
1628 }
1629 
UnregisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1630 ErrCode FormMgr::UnregisterClickEventObserver(
1631     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1632 {
1633     HILOG_DEBUG("call");
1634     if (observer == nullptr) {
1635         HILOG_ERROR("empty callerTokenParameter");
1636         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1637     }
1638     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1639         HILOG_ERROR("form is in recover status,can't do action on form");
1640         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1641     }
1642     int errCode = Connect();
1643     if (errCode != ERR_OK) {
1644         HILOG_ERROR("errCode:%{public}d", errCode);
1645         return errCode;
1646     }
1647     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1648     if (remoteProxy_ == nullptr) {
1649         HILOG_ERROR("null remoteProxy_");
1650         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1651     }
1652     return remoteProxy_->UnregisterClickEventObserver(bundleName, formEventType, observer);
1653 }
1654 
RegisterFormRouterProxy(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)1655 int FormMgr::RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
1656 {
1657     HILOG_DEBUG("call");
1658     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1659         HILOG_ERROR("form is in recover status, can't do action on form");
1660         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1661     }
1662     auto errCode = Connect();
1663     if (errCode != ERR_OK) {
1664         HILOG_ERROR("errCode:%{public}d", errCode);
1665         return errCode;
1666     }
1667     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1668     if (remoteProxy_ == nullptr) {
1669         HILOG_ERROR("null remoteProxy_");
1670         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1671     }
1672     return remoteProxy_->RegisterFormRouterProxy(formIds, callerToken);
1673 }
1674 
UnregisterFormRouterProxy(const std::vector<int64_t> & formIds)1675 int FormMgr::UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)
1676 {
1677     HILOG_DEBUG("call");
1678     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1679         HILOG_ERROR("form is in recover status,can't do action on form");
1680         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1681     }
1682     auto errCode = Connect();
1683     if (errCode != ERR_OK) {
1684         HILOG_ERROR("errCode:%{public}d", errCode);
1685         return errCode;
1686     }
1687     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1688     if (remoteProxy_ == nullptr) {
1689         HILOG_ERROR("null remoteProxy_");
1690         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1691     }
1692     return remoteProxy_->UnregisterFormRouterProxy(formIds);
1693 }
1694 
SetFormsRecyclable(const std::vector<int64_t> & formIds)1695 int32_t FormMgr::SetFormsRecyclable(const std::vector<int64_t> &formIds)
1696 {
1697     HILOG_DEBUG("call");
1698     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1699         HILOG_ERROR("form is in recover status, can't do action on form");
1700         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1701     }
1702     if (formIds.empty()) {
1703         HILOG_ERROR("empty formIds");
1704         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1705     }
1706     auto errCode = Connect();
1707     if (errCode != ERR_OK) {
1708         HILOG_ERROR("errCode:%{public}d", errCode);
1709         return errCode;
1710     }
1711     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1712     if (remoteProxy_ == nullptr) {
1713         HILOG_ERROR("null remoteProxy_");
1714         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1715     }
1716     return remoteProxy_->SetFormsRecyclable(formIds);
1717 }
1718 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want)1719 int32_t FormMgr::RecycleForms(const std::vector<int64_t> &formIds, const Want &want)
1720 {
1721     HILOG_DEBUG("call");
1722     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1723         HILOG_ERROR("form is in recover status, can't do action on form");
1724         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1725     }
1726     if (formIds.empty()) {
1727         HILOG_ERROR("empty formIds");
1728         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1729     }
1730     auto errCode = Connect();
1731     if (errCode != ERR_OK) {
1732         HILOG_ERROR("errCode:%{public}d", errCode);
1733         return errCode;
1734     }
1735     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1736     if (remoteProxy_ == nullptr) {
1737         HILOG_ERROR("null remoteProxy_");
1738         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1739     }
1740     return remoteProxy_->RecycleForms(formIds, want);
1741 }
1742 
RecoverForms(const std::vector<int64_t> & formIds,const Want & want)1743 int32_t FormMgr::RecoverForms(const std::vector<int64_t> &formIds, const Want &want)
1744 {
1745     HILOG_DEBUG("call");
1746     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1747         HILOG_ERROR("form is in recover status, can't do action on form");
1748         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1749     }
1750     if (formIds.empty()) {
1751         HILOG_ERROR("empty formIds");
1752         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1753     }
1754     auto errCode = Connect();
1755     if (errCode != ERR_OK) {
1756         HILOG_ERROR("errCode:%{public}d", errCode);
1757         return errCode;
1758     }
1759     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1760     if (remoteProxy_ == nullptr) {
1761         HILOG_ERROR("null remoteProxy_");
1762         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1763     }
1764     return remoteProxy_->RecoverForms(formIds, want);
1765 }
1766 
UpdateFormLocation(const int64_t & formId,const int32_t & formLocation)1767 ErrCode FormMgr::UpdateFormLocation(const int64_t &formId, const int32_t &formLocation)
1768 {
1769     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1770         HILOG_ERROR("form is in recover status, can't do action on form");
1771         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1772     }
1773 
1774     ErrCode errCode = Connect();
1775     if (errCode != ERR_OK) {
1776         return errCode;
1777     }
1778     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1779     if (remoteProxy_ == nullptr) {
1780         HILOG_ERROR("null remoteProxy_");
1781         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1782     }
1783     ErrCode resultCode = remoteProxy_->UpdateFormLocation(formId, formLocation);
1784     if (resultCode != ERR_OK) {
1785         HILOG_ERROR("fail UpdateFormLocation,errCode %{public}d", resultCode);
1786     }
1787     return resultCode;
1788 }
1789 
RequestPublishFormWithSnapshot(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)1790 ErrCode FormMgr::RequestPublishFormWithSnapshot(Want &want, bool withFormBindingData,
1791     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
1792     const std::vector<FormDataProxy> &formDataProxies)
1793 {
1794     HILOG_INFO("call");
1795     ErrCode errCode = Connect();
1796     if (errCode != ERR_OK) {
1797         HILOG_ERROR("errCode:%{public}d", errCode);
1798         return errCode;
1799     }
1800     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1801     if (remoteProxy_ == nullptr) {
1802         HILOG_ERROR("null remoteProxy_");
1803         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1804     }
1805     return remoteProxy_->RequestPublishFormWithSnapshot(want, withFormBindingData, formBindingData, formId);
1806 }
1807 
BatchRefreshForms(const int32_t formRefreshType)1808 int32_t FormMgr::BatchRefreshForms(const int32_t formRefreshType)
1809 {
1810     HILOG_INFO("call");
1811     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1812         HILOG_ERROR("BatchRefreshForms failed, form is in recover status, can't do action on form");
1813         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1814     }
1815 
1816     if (formRefreshType < Constants::REFRESH_ALL_FORM || formRefreshType > Constants::REFRESH_ATOMIC_FORM) {
1817         HILOG_ERROR("BatchRefreshForms failed, invalid formRefreshType %{public}d", formRefreshType);
1818         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1819     }
1820 
1821     int errCode = Connect();
1822     if (errCode != ERR_OK) {
1823         HILOG_ERROR("errCode:%{public}d", errCode);
1824         return errCode;
1825     }
1826     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1827     if (remoteProxy_ == nullptr) {
1828         HILOG_ERROR("null remoteProxy_");
1829         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1830     }
1831     return remoteProxy_->BatchRefreshForms(formRefreshType);
1832 }
1833 
EnableForms(const std::string bundleName,const bool enable)1834 int32_t FormMgr::EnableForms(const std::string bundleName, const bool enable)
1835 {
1836     if (bundleName.empty()) {
1837         HILOG_ERROR("empty bundleName");
1838         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1839     }
1840     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1841         HILOG_ERROR("form is in recover status, can't do action on form");
1842         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1843     }
1844 
1845     ErrCode errCode = Connect();
1846     if (errCode != ERR_OK) {
1847         return errCode;
1848     }
1849 
1850     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1851     if (remoteProxy_ == nullptr) {
1852         HILOG_ERROR("null remoteProxy_");
1853         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1854     }
1855     ErrCode resultCode = remoteProxy_->EnableForms(bundleName, enable);
1856     if (resultCode != ERR_OK) {
1857         HILOG_ERROR("fail EnableForms,errCode %{public}d", resultCode);
1858     }
1859     return resultCode;
1860 }
1861 
IsFormBundleForbidden(const std::string & bundleName)1862 bool FormMgr::IsFormBundleForbidden(const std::string &bundleName)
1863 {
1864     ErrCode errCode = Connect();
1865     if (errCode != ERR_OK) {
1866         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1867         return false;
1868     }
1869 
1870     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1871     if (remoteProxy_ == nullptr) {
1872         HILOG_ERROR("null remoteProxy_");
1873         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1874     }
1875     return remoteProxy_->IsFormBundleForbidden(bundleName);
1876 }
1877 
UpdateFormSize(const int64_t formId,float width,float height,float borderWidth)1878 ErrCode FormMgr::UpdateFormSize(const int64_t formId, float width, float height, float borderWidth)
1879 {
1880     ErrCode errCode = Connect();
1881     if (errCode != ERR_OK) {
1882         return errCode;
1883     }
1884     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1885     if (remoteProxy_ == nullptr) {
1886         HILOG_ERROR("null remoteProxy_");
1887         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1888     }
1889     ErrCode resultCode = remoteProxy_->UpdateFormSize(formId, width, height, borderWidth);
1890     if (resultCode != ERR_OK) {
1891         HILOG_ERROR("fail UpdateFormSize,errCode %{public}d", resultCode);
1892     }
1893     return resultCode;
1894 }
1895 }  // namespace AppExecFwk
1896 }  // namespace OHOS
1897