• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "running_form_info.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "string_ex.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 
FormMgr()33 FormMgr::FormMgr()
34 {
35     HILOG_DEBUG("call");
36 }
37 
~FormMgr()38 FormMgr::~FormMgr()
39 {
40     HILOG_INFO("call");
41     if (remoteProxy_ != nullptr) {
42         auto remoteObject = remoteProxy_->AsObject();
43         if (remoteObject != nullptr) {
44             remoteObject->RemoveDeathRecipient(deathRecipient_);
45         }
46     }
47 }
48 
49 /**
50  * @brief Get the error message by error code.
51  * @param errorCode the error code return form fms.
52  * @return Returns the error message detail.
53  */
GetErrorMsg(int errorCode)54 std::string FormMgr::GetErrorMsg(int errorCode)
55 {
56     return "unknown error";
57 }
58 
59 /**
60  * @brief Add form with want, send want to form manager service.
61  * @param formId The Id of the forms to add.
62  * @param want The want of the form to add.
63  * @param callerToken Caller ability token.
64  * @param formInfo Form info.
65  * @return Returns ERR_OK on success, others on failure.
66  */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)67 int FormMgr::AddForm(
68     const int64_t formId,
69     const Want &want,
70     const sptr<IRemoteObject> &callerToken,
71     FormJsInfo &formInfo)
72 {
73     HILOG_INFO("formId:%{public}" PRId64, formId);
74     int errCode = Connect();
75     if (errCode != ERR_OK) {
76         return errCode;
77     }
78     // Perform read lock control. Do not assign a value to remoteProxy_ in subsequent operations.
79     std::shared_lock<std::shared_mutex> lock(connectMutex_);
80 
81     // To prevent the obtained value of remoteProxy_ from being null,
82     // the system checks whether the value of remoteProxy_ is null.
83     if (remoteProxy_ == nullptr) {
84         HILOG_ERROR("null remoteProxy_");
85         return ERR_APPEXECFWK_FORM_COMMON_CODE;
86     }
87     return remoteProxy_->AddForm(formId, want, callerToken, formInfo);
88 }
89 
90 /**
91  * @brief Add form with want, send want to form manager service.
92  * @param want The want of the form to add.
93  * @param runningFormInfo Running form info.
94  * @return Returns ERR_OK on success, others on failure.
95  */
CreateForm(const Want & want,RunningFormInfo & runningFormInfo)96 int FormMgr::CreateForm(const Want &want, RunningFormInfo &runningFormInfo)
97 {
98     HILOG_INFO("call");
99     int resultCode = Connect();
100     if (resultCode != ERR_OK) {
101         HILOG_ERROR("Connect failed errCode:%{public}d", resultCode);
102         return resultCode;
103     }
104     std::shared_lock<std::shared_mutex> lock(connectMutex_);
105     if (remoteProxy_ == nullptr) {
106         HILOG_ERROR("null remoteProxy_");
107         return ERR_APPEXECFWK_FORM_COMMON_CODE;
108     }
109     resultCode = remoteProxy_->CreateForm(want, runningFormInfo);
110     if (resultCode != ERR_OK) {
111         HILOG_ERROR("createForm failed,errorCode is %{public}d", resultCode);
112     }
113     HILOG_INFO("formId:%{public}s", std::to_string(runningFormInfo.formId).c_str());
114     return resultCode;
115 }
116 
117 /**
118  * @brief Delete forms with formIds, send formIds to form manager service.
119  * @param formId The Id of the forms to delete.
120  * @param callerToken Caller ability token.
121  * @return Returns ERR_OK on success, others on failure.
122  */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)123 int FormMgr::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
124 {
125     HILOG_INFO("formId:%{public}" PRId64, formId);
126     // check fms recover status
127     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
128         HILOG_ERROR("delete form failed,form in recover status,can't do action on form");
129         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
130     }
131     // check formId
132     if (formId <= 0) {
133         HILOG_ERROR("delete form failed,the passed in formId can't be negative or zero");
134         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
135     }
136 
137     int errCode = Connect();
138     if (errCode != ERR_OK) {
139         return errCode;
140     }
141     std::shared_lock<std::shared_mutex> lock(connectMutex_);
142     if (remoteProxy_ == nullptr) {
143         HILOG_ERROR("null remoteProxy_");
144         return ERR_APPEXECFWK_FORM_COMMON_CODE;
145     }
146     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
147     return remoteProxy_->DeleteForm(formId, callerToken);
148 }
149 
150 /**
151  * @brief Stop rendering form.
152  * @param formId The Id of the forms to delete.
153  * @param compId The compId of the forms to delete.
154  * @return Returns ERR_OK on success, others on failure.
155 */
StopRenderingForm(const int64_t formId,const std::string & compId)156 int FormMgr::StopRenderingForm(const int64_t formId, const std::string &compId)
157 {
158     HILOG_INFO("formId:%{public}" PRId64, formId);
159     // check fms recover status
160     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
161         HILOG_ERROR("form is in recover status, can't do action on form");
162         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
163     }
164     // check formId
165     if (formId <= 0 || compId.empty()) {
166         HILOG_ERROR("invalid formId or empty compId");
167         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
168     }
169 
170     int errCode = Connect();
171     if (errCode != ERR_OK) {
172         return errCode;
173     }
174     std::shared_lock<std::shared_mutex> lock(connectMutex_);
175     if (remoteProxy_ == nullptr) {
176         HILOG_ERROR("null remoteProxy_");
177         return ERR_APPEXECFWK_FORM_COMMON_CODE;
178     }
179     return remoteProxy_->StopRenderingForm(formId, compId);
180 }
181 
182 /**
183  * @brief Release forms with formIds, send formIds to form manager service.
184  * @param formId The Id of the forms to release.
185  * @param callerToken Caller ability token.
186  * @param delCache Delete Cache or not.
187  * @return Returns ERR_OK on success, others on failure.
188  */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)189 int FormMgr::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
190 {
191     HILOG_INFO("formId:%{public}" PRId64 ", delCache:%{public}d", formId, delCache);
192     // check fms recover status
193     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
194         HILOG_ERROR("form is in recover status, can't do action on form");
195         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
196     }
197     // check formId
198     if (formId <= 0) {
199         HILOG_ERROR("the passed in formId can't be negative or zero");
200         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
201     }
202 
203     int errCode = Connect();
204     if (errCode != ERR_OK) {
205         return errCode;
206     }
207     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
208     std::shared_lock<std::shared_mutex> lock(connectMutex_);
209     if (remoteProxy_ == nullptr) {
210         HILOG_ERROR("null remoteProxy_");
211         return ERR_APPEXECFWK_FORM_COMMON_CODE;
212     }
213     return remoteProxy_->ReleaseForm(formId, callerToken, delCache);
214 }
215 
216 /**
217  * @brief Update form with formId, send formId to form manager service.
218  * @param formId The Id of the form to update.
219  * @param formBindingData Form binding data.
220  * @return Returns ERR_OK on success, others on failure.
221  */
UpdateForm(const int64_t formId,const FormProviderData & formBindingData,const std::vector<FormDataProxy> & formDataProxies)222 int FormMgr::UpdateForm(const int64_t formId, const FormProviderData &formBindingData,
223     const std::vector<FormDataProxy> &formDataProxies)
224 {
225     HILOG_DEBUG("call");
226     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
227         HILOG_ERROR("UpdateForm failed, form is in recover status, can't do action on form");
228         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
229     }
230 
231     if (formId <= 0) {
232         HILOG_ERROR(" UpdateForm failed, the passed in formId can't be negative or zero");
233         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
234     }
235 
236     // check formBindingData
237     if (formBindingData.GetDataString().empty() && formDataProxies.empty()) {
238         HILOG_ERROR("UpdateForm failed,null formProviderData");
239         return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY;
240     }
241 
242     int errCode = Connect();
243     if (errCode != ERR_OK) {
244         return errCode;
245     }
246 
247     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
248     if (hostCaller != nullptr) {
249         hostCaller->UpdateForm(formId, formBindingData);
250     } else {
251         std::vector<std::shared_ptr<FormProviderCaller>> formProviderCallers;
252         FormCallerMgr::GetInstance().GetFormProviderCaller(formId, formProviderCallers);
253         for (const auto &formProviderCaller : formProviderCallers) {
254             formProviderCaller->UpdateForm(formId, formBindingData);
255         }
256     }
257     std::shared_lock<std::shared_mutex> lock(connectMutex_);
258     if (remoteProxy_ == nullptr) {
259         HILOG_ERROR("null remoteProxy_");
260         return ERR_APPEXECFWK_FORM_COMMON_CODE;
261     }
262     if (formDataProxies.empty()) {
263         return remoteProxy_->UpdateForm(formId, formBindingData);
264     }
265     return remoteProxy_->UpdateProxyForm(formId, formBindingData, formDataProxies);
266 }
267 
268 /**
269  * @brief Release renderer.
270  * @param formId The Id of the forms to release.
271  * @param compId The compId of the forms to release.
272  * @return Returns ERR_OK on success, others on failure.
273 */
ReleaseRenderer(const int64_t formId,const std::string & compId)274 int FormMgr::ReleaseRenderer(const int64_t formId, const std::string &compId)
275 {
276     HILOG_INFO("formId:%{public}" PRId64, formId);
277     // check fms recover status
278     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
279         HILOG_ERROR("form is in recover status, can't do action on form");
280         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
281     }
282     // check formId and compId
283     if (formId <= 0 || compId.empty()) {
284         HILOG_ERROR("invalid formId or empty compId");
285         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
286     }
287 
288     int errCode = Connect();
289     if (errCode != ERR_OK) {
290         return errCode;
291     }
292     std::shared_lock<std::shared_mutex> lock(connectMutex_);
293     if (remoteProxy_ == nullptr) {
294         HILOG_ERROR("null remoteProxy_");
295         return ERR_APPEXECFWK_FORM_COMMON_CODE;
296     }
297     return remoteProxy_->ReleaseRenderer(formId, compId);
298 }
299 
300 /**
301  * @brief Notify the form service that the form user's lifecycle is updated.
302  *
303  * This should be called when form user request form.
304  *
305  * @param formId Indicates the unique id of form.
306  * @param callerToken Indicates the callback remote object of specified form user.
307  * @param want information passed to supplier.
308  * @return Returns true if execute success, false otherwise.
309  */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)310 int FormMgr::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
311 {
312     HILOG_INFO("formId:%{public}" PRId64, formId);
313     if (formId <= 0) {
314         HILOG_ERROR("invalid formId");
315         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
316     }
317     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
318         HILOG_ERROR("form is in recover status, can't do action on form");
319         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
320     }
321     int errCode = Connect();
322     if (errCode != ERR_OK) {
323         return errCode;
324     }
325     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
326     if (hostCaller != nullptr) {
327         HILOG_INFO("request by host caller");
328         return hostCaller->RequestForm(formId, callerToken, want);
329     }
330     std::shared_lock<std::shared_mutex> lock(connectMutex_);
331     if (remoteProxy_ == nullptr) {
332         HILOG_ERROR("null remoteProxy_");
333         return ERR_APPEXECFWK_FORM_COMMON_CODE;
334     }
335     ErrCode resultCode = remoteProxy_->RequestForm(formId, callerToken, want);
336     if (resultCode != ERR_OK) {
337         HILOG_ERROR("fail notify the form service that the form user's lifecycle is updated"
338             "code is %{public}d", resultCode);
339     }
340     return resultCode;
341 }
342 
343 /**
344  * @brief Form visible/invisible notify, send formIds to form manager service.
345  * @param formIds The Id list of the forms to notify.
346  * @param callerToken Caller ability token.
347  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
348  * @return Returns ERR_OK on success, others on failure.
349  */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)350 int FormMgr::NotifyWhetherVisibleForms(
351     const std::vector<int64_t> &formIds,
352     const sptr<IRemoteObject> &callerToken,
353     const int32_t formVisibleType)
354 {
355     HILOG_DEBUG("formVisibleType is %{public}d", formVisibleType);
356 
357     if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) {
358         HILOG_ERROR("empty formIds or exceed 32");
359         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
360     }
361 
362     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
363         HILOG_ERROR("form is in recover status, can't do action on form");
364         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
365     }
366 
367     int errCode = Connect();
368     if (errCode != ERR_OK) {
369         return errCode;
370     }
371     std::shared_lock<std::shared_mutex> lock(connectMutex_);
372     if (remoteProxy_ == nullptr) {
373         HILOG_ERROR("null remoteProxy_");
374         return ERR_APPEXECFWK_FORM_COMMON_CODE;
375     }
376     // IPC entry
377     ErrCode resultCode = remoteProxy_->NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
378     if (resultCode != ERR_OK) {
379         HILOG_ERROR("internal error occurs,errCode:%{public}d", resultCode);
380     }
381     return resultCode;
382 }
383 
384 /**
385  * @brief Query whether has visible form by tokenId.
386  * @param tokenId Unique identification of application.
387  * @return Returns true if has visible form, false otherwise.
388  */
HasFormVisible(const uint32_t tokenId)389 bool FormMgr::HasFormVisible(const uint32_t tokenId)
390 {
391     HILOG_DEBUG("call");
392     int errCode = Connect();
393     if (errCode != ERR_OK) {
394         HILOG_ERROR("errCode:%{public}d", errCode);
395         return false;
396     }
397     std::shared_lock<std::shared_mutex> lock(connectMutex_);
398     if (remoteProxy_ == nullptr) {
399         HILOG_ERROR("null remoteProxy_");
400         return false;
401     }
402     return remoteProxy_->HasFormVisible(tokenId);
403 }
404 
405 /**
406  * @brief temp form to normal form.
407  * @param formId The Id of the form.
408  * @param callerToken Caller ability token.
409  * @return None.
410  */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)411 int FormMgr::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
412 {
413     HILOG_INFO("formId:%{public}" PRId64, formId);
414     if (formId <= 0) {
415         HILOG_ERROR("passing in form id can't be negative");
416         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
417     }
418 
419     int errCode = Connect();
420     if (errCode != ERR_OK) {
421         return errCode;
422     }
423     std::shared_lock<std::shared_mutex> lock(connectMutex_);
424     if (remoteProxy_ == nullptr) {
425         HILOG_ERROR("null remoteProxy_");
426         return ERR_APPEXECFWK_FORM_COMMON_CODE;
427     }
428     return remoteProxy_->CastTempForm(formId, callerToken);
429 }
430 
431 /**
432  * @brief Dump all of form storage infos.
433  * @param formInfos All of form storage infos.
434  * @return Returns ERR_OK on success, others on failure.
435  */
DumpStorageFormInfos(std::string & formInfos)436 int FormMgr::DumpStorageFormInfos(std::string &formInfos)
437 {
438     HILOG_DEBUG("call");
439     int errCode = Connect();
440     if (errCode != ERR_OK) {
441         return errCode;
442     }
443     std::shared_lock<std::shared_mutex> lock(connectMutex_);
444     if (remoteProxy_ == nullptr) {
445         HILOG_ERROR("null remoteProxy_");
446         return ERR_APPEXECFWK_FORM_COMMON_CODE;
447     }
448     return remoteProxy_->DumpStorageFormInfos(formInfos);
449 }
450 /**
451  * @brief Dump form info by a bundle name.
452  * @param bundleName The bundle name of form provider.
453  * @param formInfos Form infos.
454  * @return Returns ERR_OK on success, others on failure.
455  */
DumpFormInfoByBundleName(const std::string bundleName,std::string & formInfos)456 int FormMgr::DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)
457 {
458     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
459     int errCode = Connect();
460     if (errCode != ERR_OK) {
461         return errCode;
462     }
463     std::shared_lock<std::shared_mutex> lock(connectMutex_);
464     if (remoteProxy_ == nullptr) {
465         HILOG_ERROR("null remoteProxy_");
466         return ERR_APPEXECFWK_FORM_COMMON_CODE;
467     }
468     return remoteProxy_->DumpFormInfoByBundleName(bundleName, formInfos);
469 }
470 /**
471  * @brief Dump form info by a bundle name.
472  * @param formId The id of the form.
473  * @param formInfo Form info.
474  * @return Returns ERR_OK on success, others on failure.
475  */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo)476 int FormMgr::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)
477 {
478     HILOG_INFO("formId:%{public}" PRId64, formId);
479     int errCode = Connect();
480     if (errCode != ERR_OK) {
481         return errCode;
482     }
483     std::shared_lock<std::shared_mutex> lock(connectMutex_);
484     if (remoteProxy_ == nullptr) {
485         HILOG_ERROR("null remoteProxy_");
486         return ERR_APPEXECFWK_FORM_COMMON_CODE;
487     }
488     return remoteProxy_->DumpFormInfoByFormId(formId, formInfo);
489 }
490 /**
491  * @brief Dump form timer by form id.
492  * @param formId The id of the form.
493  * @param formInfo Form timer.
494  * @return Returns ERR_OK on success, others on failure.
495  */
DumpFormTimerByFormId(const std::int64_t formId,std::string & isTimingService)496 int FormMgr::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)
497 {
498     HILOG_INFO("call");
499     int errCode = Connect();
500     if (errCode != ERR_OK) {
501         HILOG_ERROR("errCode:%{public}d", errCode);
502         return errCode;
503     }
504     std::shared_lock<std::shared_mutex> lock(connectMutex_);
505     if (remoteProxy_ == nullptr) {
506         HILOG_ERROR("null remoteProxy_");
507         return ERR_APPEXECFWK_FORM_COMMON_CODE;
508     }
509     return remoteProxy_->DumpFormTimerByFormId(formId, isTimingService);
510 }
511 /**
512  * @brief Process js message event.
513  * @param formId Indicates the unique id of form.
514  * @param want information passed to supplier.
515  * @param callerToken Caller ability token.
516  * @return Returns true if execute success, false otherwise.
517  */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)518 int FormMgr::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
519 {
520     HILOG_INFO("call");
521     int errCode = Connect();
522     if (errCode != ERR_OK) {
523         HILOG_ERROR("errCode:%{public}d", errCode);
524         return errCode;
525     }
526     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
527     if (hostCaller != nullptr) {
528         HILOG_DEBUG("send message by host caller");
529         return hostCaller->MessageEvent(formId, want, callerToken);
530     }
531     std::shared_lock<std::shared_mutex> lock(connectMutex_);
532     if (remoteProxy_ == nullptr) {
533         HILOG_ERROR("null remoteProxy_");
534         return ERR_APPEXECFWK_FORM_COMMON_CODE;
535     }
536     return remoteProxy_->MessageEvent(formId, want, callerToken);
537 }
538 
539 /**
540  * @brief Process js router event.
541  * @param formId Indicates the unique id of form.
542  * @param want the want of the ability to start.
543  * @param callerToken Caller ability token.
544  * @return Returns true if execute success, false otherwise.
545  */
RouterEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)546 int FormMgr::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
547 {
548     HILOG_INFO("call");
549     int errCode = Connect();
550     if (errCode != ERR_OK) {
551         HILOG_ERROR("errCode:%{public}d", errCode);
552         return errCode;
553     }
554     std::shared_lock<std::shared_mutex> lock(connectMutex_);
555     if (remoteProxy_ == nullptr) {
556         HILOG_ERROR("null remoteProxy_");
557         return ERR_APPEXECFWK_FORM_COMMON_CODE;
558     }
559     return remoteProxy_->RouterEvent(formId, want, callerToken);
560 }
561 
562 /**
563  * @brief Process Background event.
564  * @param formId Indicates the unique id of form.
565  * @param want the want of the ability to start.
566  * @param callerToken Caller ability token.
567  * @return Returns true if execute success, false otherwise.
568  */
BackgroundEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)569 int FormMgr::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
570 {
571     HILOG_INFO("call");
572     int errCode = Connect();
573     if (errCode != ERR_OK) {
574         HILOG_ERROR("errCode:%{public}d", errCode);
575         return errCode;
576     }
577     std::shared_lock<std::shared_mutex> lock(connectMutex_);
578     if (remoteProxy_ == nullptr) {
579         HILOG_ERROR("null remoteProxy_");
580         return ERR_APPEXECFWK_FORM_COMMON_CODE;
581     }
582     return remoteProxy_->BackgroundEvent(formId, want, callerToken);
583 }
584 
585 /**
586  * @brief Set next refresh time.
587  * @param formId The id of the form.
588  * @param nextTime Next refresh time.
589  * @return Returns ERR_OK on success, others on failure.
590  */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)591 int FormMgr::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
592 {
593     HILOG_INFO("call");
594 
595     if (GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
596         HILOG_ERROR("formManager is in recovering");
597         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
598     }
599 
600     int errCode = Connect();
601     if (errCode != ERR_OK) {
602         HILOG_ERROR("errCode:%{public}d", errCode);
603         return errCode;
604     }
605     std::shared_lock<std::shared_mutex> lock(connectMutex_);
606     if (remoteProxy_ == nullptr) {
607         HILOG_ERROR("null remoteProxy_");
608         return ERR_APPEXECFWK_FORM_COMMON_CODE;
609     }
610     return remoteProxy_->SetNextRefreshTime(formId, nextTime);
611 }
612 
RequestPublishForm(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)613 ErrCode FormMgr::RequestPublishForm(Want &want, bool withFormBindingData,
614     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
615     const std::vector<FormDataProxy> &formDataProxies)
616 {
617     HILOG_INFO("call");
618     ErrCode errCode = Connect();
619     if (errCode != ERR_OK) {
620         HILOG_ERROR("errCode:%{public}d", errCode);
621         return errCode;
622     }
623     std::shared_lock<std::shared_mutex> lock(connectMutex_);
624     if (remoteProxy_ == nullptr) {
625         HILOG_ERROR("null remoteProxy_");
626         return ERR_APPEXECFWK_FORM_COMMON_CODE;
627     }
628     if (formDataProxies.empty()) {
629         return remoteProxy_->RequestPublishForm(want, withFormBindingData, formBindingData, formId);
630     }
631     return remoteProxy_->RequestPublishProxyForm(want, withFormBindingData, formBindingData, formId, formDataProxies);
632 }
633 
634 
SetPublishFormResult(const int64_t formId,Constants::PublishFormResult & errorCodeInfo)635 ErrCode FormMgr::SetPublishFormResult(const int64_t formId, Constants::PublishFormResult &errorCodeInfo)
636 {
637     HILOG_INFO("call");
638     if (formId <= 0) {
639         HILOG_ERROR("errCode:%{public}." PRId64, formId);
640         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
641     }
642     ErrCode errCode = Connect();
643     if (errCode != ERR_OK) {
644         HILOG_ERROR("errCode:%{public}d", errCode);
645         return errCode;
646     }
647     std::shared_lock<std::shared_mutex> lock(connectMutex_);
648     if (remoteProxy_ == nullptr) {
649         HILOG_ERROR("null remoteProxy_");
650         return ERR_APPEXECFWK_FORM_COMMON_CODE;
651     }
652     return remoteProxy_->SetPublishFormResult(formId, errorCodeInfo);
653 }
654 
AcquireAddFormResult(const int64_t formId)655 ErrCode FormMgr::AcquireAddFormResult(const int64_t formId)
656 {
657     HILOG_INFO("call");
658     if (formId <= 0) {
659         HILOG_ERROR("errCode:%{public}" PRId64, formId);
660         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
661     }
662     ErrCode errCode = Connect();
663     if (errCode != ERR_OK) {
664         HILOG_ERROR("errCode:%{public}d", errCode);
665         return errCode;
666     }
667     std::shared_lock<std::shared_mutex> lock(connectMutex_);
668     if (remoteProxy_ == nullptr) {
669         HILOG_ERROR("null remoteProxy_");
670         return ERR_APPEXECFWK_FORM_COMMON_CODE;
671     }
672     return remoteProxy_->AcquireAddFormResult(formId);
673 }
674 
LifecycleUpdate(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool updateType)675 int FormMgr::LifecycleUpdate(
676     const std::vector<int64_t> &formIds,
677     const sptr<IRemoteObject> &callerToken,
678     bool updateType)
679 {
680     HILOG_INFO("call");
681 
682     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
683         HILOG_ERROR("form is in recover status, can't do action on form");
684         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
685     }
686 
687     int errCode = Connect();
688     if (errCode != ERR_OK) {
689         HILOG_ERROR("errCode:%{public}d", errCode);
690         return errCode;
691     }
692     std::shared_lock<std::shared_mutex> lock(connectMutex_);
693     if (remoteProxy_ == nullptr) {
694         HILOG_ERROR("null remoteProxy_");
695         return ERR_APPEXECFWK_FORM_COMMON_CODE;
696     }
697     return remoteProxy_->LifecycleUpdate(formIds, callerToken, updateType);
698 }
699 /**
700  * @brief Get fms recoverStatus.
701  *
702  * @return The current recover status.
703  */
GetRecoverStatus()704 int FormMgr::GetRecoverStatus()
705 {
706     HILOG_DEBUG("get recover status");
707     return recoverStatus_;
708 }
709 
710 /**
711  * @brief Set fms recoverStatus.
712  *
713  * @param recoverStatus The recover status.
714  */
SetRecoverStatus(int recoverStatus)715 void FormMgr::SetRecoverStatus(int recoverStatus)
716 {
717     HILOG_INFO("call");
718     recoverStatus_ = recoverStatus;
719 }
720 
721 /**
722  * @brief Get the error message content.
723  *
724  * @param errCode Error code.
725  * @return Message content.
726  */
GetErrorMessage(int errCode)727 std::string FormMgr::GetErrorMessage(int errCode)
728 {
729     HILOG_INFO("call");
730     return FormErrors::GetInstance().GetErrorMessage(errCode);
731 }
732 
733 /**
734  * @brief Register death callback.
735  *
736  * @param deathCallback Death callback.
737  */
RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)738 void FormMgr::RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
739 {
740     HILOG_INFO("call");
741     if (formDeathCallback == nullptr) {
742         HILOG_ERROR("null formDeathCallback");
743         return;
744     }
745     formDeathCallbacks_.emplace_back(formDeathCallback);
746 }
747 
748 /**
749  * @brief UnRegister death callback.
750  *
751  * @param deathCallback Death callback.
752  */
UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)753 void FormMgr::UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
754 {
755     HILOG_INFO("call");
756     if (formDeathCallback == nullptr) {
757         HILOG_ERROR("null formDeathCallback");
758         return;
759     }
760 
761     // Remove the specified death callback in the vector of death callback
762     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
763     if (iter != formDeathCallbacks_.end()) {
764         formDeathCallbacks_.erase(iter);
765     }
766     HILOG_INFO("end");
767 }
768 
769 /**
770  * @brief Get death recipient.
771  * @return deathRecipient_.
772  */
GetDeathRecipient() const773 sptr<IRemoteObject::DeathRecipient> FormMgr::GetDeathRecipient() const
774 {
775     return deathRecipient_;
776 }
777 
778 /**
779  * @brief Check whether the specified death callback is registered in form mgr.
780  * @param formDeathCallback The specified death callback for checking.
781  * @return Return true on success, false on failure.
782  */
CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)783 bool FormMgr::CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
784 {
785     HILOG_INFO("call");
786     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
787     if (iter != formDeathCallbacks_.end()) {
788         return true;
789     }
790     return false;
791 }
792 
793 /**
794  * @brief Notices IRemoteBroker died.
795  * @param remote remote object.
796  */
OnRemoteDied(const wptr<IRemoteObject> & remote)797 void FormMgr::FormMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
798 {
799     HILOG_INFO("call");
800     if (remote == nullptr) {
801         HILOG_ERROR("null remote");
802         return;
803     }
804 
805     if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
806         HILOG_WARN("fms in recovering");
807         return;
808     }
809     // Reset proxy
810     FormMgr::GetInstance().ResetProxy(remote);
811 
812     if (!FormMgr::GetInstance().Reconnect()) {
813         HILOG_ERROR("form mgr service died,try to reconnect to fms failed");
814         FormMgr::GetInstance().SetRecoverStatus(Constants::RECOVER_FAIL);
815         return;
816     }
817 
818     // refresh form host.
819     for (auto &deathCallback : FormMgr::GetInstance().formDeathCallbacks_) {
820         deathCallback->OnDeathReceived();
821     }
822     FormMgr::GetInstance().SetRecoverStatus(Constants::NOT_IN_RECOVERY);
823 }
824 
825 /**
826  * @brief Reconnect form manager service once per 1000 milliseconds,
827  *        until the connection succeeds or reaching the max retry times.
828  * @return Returns true if execute success, false otherwise.
829  */
Reconnect()830 bool FormMgr::Reconnect()
831 {
832     HILOG_DEBUG("call");
833     for (int i = 0; i < Constants::MAX_RETRY_TIME; i++) {
834         // Sleep 1000 milliseconds before reconnect.
835         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::SLEEP_TIME));
836 
837         // try to connect fms
838         if (Connect() != ERR_OK) {
839             HILOG_ERROR("get fms proxy fail,try again");
840             continue;
841         }
842 
843         HILOG_INFO("success");
844         return true;
845     }
846 
847     return false;
848 }
849 
850 /**
851  * @brief Connect form manager service.
852  * @return Returns ERR_OK on success, others on failure.
853  */
Connect()854 ErrCode FormMgr::Connect()
855 {
856     {
857         std::shared_lock<std::shared_mutex> lock(connectMutex_);
858         if (remoteProxy_ != nullptr && !resetFlag_) {
859             return ERR_OK;
860         }
861     }
862     {
863         std::lock_guard<std::shared_mutex> lock(connectMutex_);
864         sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
865         if (systemManager == nullptr) {
866             HILOG_ERROR("get registry failed");
867             return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
868         }
869         sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
870         if (remoteObject == nullptr) {
871             HILOG_ERROR("connect FormMgrService failed");
872             return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
873         }
874         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
875         if (deathRecipient_ == nullptr) {
876             HILOG_ERROR("null deathRecipient_");
877             return ERR_APPEXECFWK_FORM_COMMON_CODE;
878         }
879         if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
880             HILOG_ERROR("fail add death recipient to FormMgrService");
881             return ERR_APPEXECFWK_FORM_COMMON_CODE;
882         }
883 
884         remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
885         if (remoteProxy_ == nullptr) {
886             HILOG_ERROR("null remoteProxy_");
887             return ERR_APPEXECFWK_FORM_COMMON_CODE;
888         }
889         HILOG_DEBUG("Connecting FormMgrService success");
890         return ERR_OK;
891     }
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_INFO("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 
GetPublishedFormInfoById(const int64_t formId,RunningFormInfo & formInfo)1222 int32_t FormMgr::GetPublishedFormInfoById(const int64_t formId, RunningFormInfo &formInfo)
1223 {
1224     HILOG_DEBUG("call");
1225     int errCode = Connect();
1226     if (errCode != ERR_OK) {
1227         return errCode;
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_->GetPublishedFormInfoById(formId, formInfo);
1235 }
1236 
GetPublishedFormInfos(std::vector<RunningFormInfo> & formInfos)1237 int32_t FormMgr::GetPublishedFormInfos(std::vector<RunningFormInfo> &formInfos)
1238 {
1239     HILOG_DEBUG("call");
1240     int 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_->GetPublishedFormInfos(formInfos);
1250 }
1251 
IsRequestPublishFormSupported()1252 bool FormMgr::IsRequestPublishFormSupported()
1253 {
1254     HILOG_DEBUG("call");
1255     int errCode = Connect();
1256     if (errCode != ERR_OK) {
1257         return false;
1258     }
1259     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1260     if (remoteProxy_ == nullptr) {
1261         HILOG_ERROR("null remoteProxy_");
1262         return false;
1263     }
1264     return remoteProxy_->IsRequestPublishFormSupported();
1265 }
1266 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken)1267 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
1268 {
1269     HILOG_DEBUG("call");
1270     int32_t errCode = Connect();
1271     if (errCode != ERR_OK) {
1272         return errCode;
1273     }
1274     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1275     if (remoteProxy_ == nullptr) {
1276         HILOG_ERROR("null remoteProxy_");
1277         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1278     }
1279     return remoteProxy_->StartAbility(want, callerToken);
1280 }
1281 
StartAbilityByFms(const Want & want)1282 int32_t FormMgr::StartAbilityByFms(const Want &want)
1283 {
1284     HILOG_DEBUG("call");
1285     int32_t errCode = Connect();
1286     if (errCode != ERR_OK) {
1287         return errCode;
1288     }
1289     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1290     if (remoteProxy_ == nullptr) {
1291         HILOG_ERROR("null remoteProxy_");
1292         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1293     }
1294     return remoteProxy_->StartAbilityByFms(want);
1295 }
1296 
ShareForm(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & callerToken,int64_t requestCode)1297 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId,
1298     const sptr<IRemoteObject> &callerToken, int64_t requestCode)
1299 {
1300     HILOG_INFO("formId:%{public}" PRId64, formId);
1301     int32_t errCode = Connect();
1302     if (errCode != ERR_OK) {
1303         return errCode;
1304     }
1305     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1306     if (remoteProxy_ == nullptr) {
1307         HILOG_ERROR("null remoteProxy_");
1308         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1309     }
1310     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
1311 }
1312 
AcquireFormData(int64_t formId,int64_t requestCode,const sptr<IRemoteObject> & callerToken,AAFwk::WantParams & formData)1313 int32_t FormMgr::AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
1314     AAFwk::WantParams &formData)
1315 {
1316     HILOG_INFO("formId:%{public}" PRId64, formId);
1317     int32_t errCode = Connect();
1318     if (errCode != ERR_OK) {
1319         return errCode;
1320     }
1321     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1322     if (remoteProxy_ == nullptr) {
1323         HILOG_ERROR("null remoteProxy_");
1324         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1325     }
1326     return remoteProxy_->AcquireFormData(formId, requestCode, callerToken, formData);
1327 }
1328 
CheckFMSReady()1329 bool FormMgr::CheckFMSReady()
1330 {
1331     HILOG_DEBUG("call");
1332     int32_t errCode = Connect();
1333     if (errCode != ERR_OK) {
1334         return false;
1335     }
1336     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1337     if (remoteProxy_ == nullptr) {
1338         HILOG_ERROR("null remoteProxy_");
1339         return false;
1340     }
1341     bool resultCode = remoteProxy_->CheckFMSReady();
1342     if (resultCode == false) {
1343         HILOG_ERROR("CheckFMSReady failed");
1344     }
1345     return resultCode;
1346 }
1347 
IsSystemAppForm(const std::string & bundleName)1348 bool FormMgr::IsSystemAppForm(const std::string &bundleName)
1349 {
1350     HILOG_DEBUG("check %{public}s is system form", bundleName.c_str());
1351     ErrCode errCode = Connect();
1352     if (errCode != ERR_OK) {
1353         HILOG_ERROR("errCode:%{public}d", errCode);
1354         return false;
1355     }
1356     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1357     if (remoteProxy_ == nullptr) {
1358         HILOG_ERROR("null remoteProxy_");
1359         return false;
1360     }
1361     return remoteProxy_->IsSystemAppForm(bundleName);
1362 }
1363 
RegisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1364 int32_t FormMgr::RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1365 {
1366     HILOG_DEBUG("call");
1367     int32_t errCode = Connect();
1368     if (errCode != ERR_OK) {
1369         HILOG_ERROR("register publish form failed, errCode:%{public}d", errCode);
1370         return errCode;
1371     }
1372     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1373     if (remoteProxy_ == nullptr) {
1374         HILOG_ERROR("null remoteProxy_");
1375         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1376     }
1377     return remoteProxy_->RegisterPublishFormInterceptor(interceptorCallback);
1378 }
1379 
UnregisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1380 int32_t FormMgr::UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1381 {
1382     HILOG_DEBUG("call");
1383     int32_t errCode = Connect();
1384     if (errCode != ERR_OK) {
1385         return errCode;
1386     }
1387     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1388     if (remoteProxy_ == nullptr) {
1389         HILOG_ERROR("null remoteProxy_");
1390         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1391     }
1392     return remoteProxy_->UnregisterPublishFormInterceptor(interceptorCallback);
1393 }
1394 
GetExternalError(int32_t innerErrorCode,int32_t & externalErrorCode,std::string & errorMsg)1395 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1396 {
1397     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1398     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1399     HILOG_DEBUG("innerErrorCode:%{public}d, externalErrorCode:%{public}d, errorMsg:%{public}s",
1400         innerErrorCode, externalErrorCode, errorMsg.c_str());
1401 }
1402 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1403 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1404 {
1405     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1406 }
1407 
GetRunningFormInfos(bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1408 ErrCode FormMgr::GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1409 {
1410     HILOG_INFO("isUnusedIncluded is %{public}d", isUnusedIncluded);
1411     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1412         HILOG_ERROR("form is in recover status,can't do action on form");
1413         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1414     }
1415 
1416     ErrCode errCode = Connect();
1417     if (errCode != ERR_OK) {
1418         return errCode;
1419     }
1420     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1421     if (remoteProxy_ == nullptr) {
1422         HILOG_ERROR("null remoteProxy_");
1423         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1424     }
1425     ErrCode resultCode = remoteProxy_->GetRunningFormInfos(isUnusedIncluded, runningFormInfos);
1426     if (resultCode != ERR_OK) {
1427         HILOG_ERROR("fail GetRunningFormInfos,errCode %{public}d", resultCode);
1428     }
1429     return resultCode;
1430 }
1431 
GetRunningFormInfosByBundleName(const std::string & bundleName,bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1432 ErrCode FormMgr::GetRunningFormInfosByBundleName(
1433     const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1434 {
1435     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1436     if (bundleName.empty()) {
1437         HILOG_WARN("fail Get running form infos,because empty bundleName");
1438         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1439     }
1440     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1441         HILOG_ERROR("form is in recover status, can't do action on form");
1442         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1443     }
1444 
1445     ErrCode errCode = Connect();
1446     if (errCode != ERR_OK) {
1447         return errCode;
1448     }
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     ErrCode resultCode = remoteProxy_->GetRunningFormInfosByBundleName(
1456         bundleName, isUnusedIncluded, runningFormInfos);
1457     if (resultCode != ERR_OK) {
1458         HILOG_ERROR("fail GetRunningFormInfosByBundleName,errCode %{public}d", resultCode);
1459     }
1460     return resultCode;
1461 }
1462 
RegisterFormAddObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1463 ErrCode FormMgr::RegisterFormAddObserverByBundle(const std::string bundleName,
1464     const sptr<IRemoteObject> &callerToken)
1465 {
1466     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1467     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1468         HILOG_ERROR("form is in recover status,can't do action on form");
1469         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1470     }
1471     int 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     return remoteProxy_->RegisterFormAddObserverByBundle(bundleName, callerToken);
1481 }
1482 
RegisterFormRemoveObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1483 ErrCode FormMgr::RegisterFormRemoveObserverByBundle(const std::string bundleName,
1484     const sptr<IRemoteObject> &callerToken)
1485 {
1486     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1487     if (FormMgr::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     int errCode = Connect();
1492     if (errCode != ERR_OK) {
1493         return errCode;
1494     }
1495     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1496     if (remoteProxy_ == nullptr) {
1497         HILOG_ERROR("null remoteProxy_");
1498         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1499     }
1500     return remoteProxy_->RegisterFormRemoveObserverByBundle(bundleName, callerToken);
1501 }
1502 
GetFormsCount(bool isTempFormFlag,int32_t & formCount)1503 int32_t FormMgr::GetFormsCount(bool isTempFormFlag, int32_t &formCount)
1504 {
1505     HILOG_INFO("isTempFormFlag is %{public}d", isTempFormFlag);
1506 
1507     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1508         HILOG_ERROR("form is in recover status, can't do action on form");
1509         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1510     }
1511 
1512     int32_t errCode = Connect();
1513     if (errCode != ERR_OK) {
1514         return errCode;
1515     }
1516     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1517     if (remoteProxy_ == nullptr) {
1518         HILOG_ERROR("null remoteProxy_");
1519         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1520     }
1521     int32_t resultCode = remoteProxy_->GetFormsCount(isTempFormFlag, formCount);
1522     if (resultCode != ERR_OK) {
1523         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1524     }
1525     return resultCode;
1526 }
1527 
GetHostFormsCount(std::string & bundleName,int32_t & formCount)1528 int32_t FormMgr::GetHostFormsCount(std::string &bundleName, int32_t &formCount)
1529 {
1530     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1531 
1532     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1533         HILOG_ERROR("form is in recover status, can't do action on form");
1534         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1535     }
1536 
1537     if (bundleName.empty()) {
1538         HILOG_WARN("fail Get host forms count,because empty bundleName");
1539         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1540     }
1541 
1542     int32_t errCode = Connect();
1543     if (errCode != ERR_OK) {
1544         return errCode;
1545     }
1546     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1547     if (remoteProxy_ == nullptr) {
1548         HILOG_ERROR("null remoteProxy_");
1549         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1550     }
1551     int32_t resultCode = remoteProxy_->GetHostFormsCount(bundleName, formCount);
1552     if (resultCode != ERR_OK) {
1553         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1554     }
1555     return resultCode;
1556 }
1557 
GetFormInstancesByFilter(const FormInstancesFilter & formInstancesFilter,std::vector<FormInstance> & formInstances)1558 ErrCode FormMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1559     std::vector<FormInstance> &formInstances)
1560 {
1561     HILOG_DEBUG("call");
1562     auto errCode = Connect();
1563     if (errCode != ERR_OK) {
1564         return errCode;
1565     }
1566     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1567     if (remoteProxy_ == nullptr) {
1568         HILOG_ERROR("null remoteProxy_");
1569         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1570     }
1571     return remoteProxy_->GetFormInstancesByFilter(formInstancesFilter, formInstances);
1572 }
1573 
GetFormInstanceById(const int64_t formId,FormInstance & formInstance)1574 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
1575 {
1576     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1577     auto errCode = Connect();
1578     if (errCode != ERR_OK) {
1579         return errCode;
1580     }
1581     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1582     if (remoteProxy_ == nullptr) {
1583         HILOG_ERROR("null remoteProxy_");
1584         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1585     }
1586     return remoteProxy_->GetFormInstanceById(formId, formInstance);
1587 }
1588 
GetFormInstanceById(const int64_t formId,bool isUnusedIncluded,FormInstance & formInstance)1589 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)
1590 {
1591     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1592     auto errCode = Connect();
1593     if (errCode != ERR_OK) {
1594         return errCode;
1595     }
1596     if (remoteProxy_ == nullptr) {
1597         HILOG_ERROR("null remoteProxy_");
1598         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1599     }
1600     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1601     if (remoteProxy_ == nullptr) {
1602         HILOG_ERROR("null remoteProxy_");
1603         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1604     }
1605     return remoteProxy_->GetFormInstanceById(formId, isUnusedIncluded, formInstance);
1606 }
1607 
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1608 ErrCode FormMgr::RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1609 {
1610     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1611     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1612         HILOG_ERROR("form is in recover status,can't do action on form");
1613         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1614     }
1615     auto errCode = Connect();
1616     if (errCode != ERR_OK) {
1617         return errCode;
1618     }
1619     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1620     if (remoteProxy_ == nullptr) {
1621         HILOG_ERROR("null remoteProxy_");
1622         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1623     }
1624     return remoteProxy_->RegisterAddObserver(bundleName, callerToken);
1625 }
1626 
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1627 ErrCode FormMgr::RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1628 {
1629     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1630     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1631         HILOG_ERROR("form is in recover status,can't do action on form");
1632         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1633     }
1634     auto errCode = Connect();
1635     if (errCode != ERR_OK) {
1636         return errCode;
1637     }
1638     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1639     if (remoteProxy_ == nullptr) {
1640         HILOG_ERROR("null remoteProxy_");
1641         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1642     }
1643     return remoteProxy_->RegisterRemoveObserver(bundleName, callerToken);
1644 }
1645 
RegisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1646 ErrCode FormMgr::RegisterClickEventObserver(
1647     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1648 {
1649     HILOG_DEBUG("call");
1650     if (observer == nullptr) {
1651         HILOG_ERROR("empty callerTokenParameter");
1652         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1653     }
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     int 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_->RegisterClickEventObserver(bundleName, formEventType, observer);
1669 }
1670 
UnregisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1671 ErrCode FormMgr::UnregisterClickEventObserver(
1672     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1673 {
1674     HILOG_DEBUG("call");
1675     if (observer == nullptr) {
1676         HILOG_ERROR("empty callerTokenParameter");
1677         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1678     }
1679     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1680         HILOG_ERROR("form is in recover status,can't do action on form");
1681         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1682     }
1683     int errCode = Connect();
1684     if (errCode != ERR_OK) {
1685         HILOG_ERROR("errCode:%{public}d", errCode);
1686         return errCode;
1687     }
1688     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1689     if (remoteProxy_ == nullptr) {
1690         HILOG_ERROR("null remoteProxy_");
1691         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1692     }
1693     return remoteProxy_->UnregisterClickEventObserver(bundleName, formEventType, observer);
1694 }
1695 
RegisterFormRouterProxy(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)1696 int FormMgr::RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
1697 {
1698 #ifndef WATCH_API_DISABLE
1699     HILOG_DEBUG("call");
1700     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1701         HILOG_ERROR("form is in recover status, can't do action on form");
1702         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1703     }
1704     auto errCode = Connect();
1705     if (errCode != ERR_OK) {
1706         HILOG_ERROR("errCode:%{public}d", errCode);
1707         return errCode;
1708     }
1709     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1710     if (remoteProxy_ == nullptr) {
1711         HILOG_ERROR("null remoteProxy_");
1712         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1713     }
1714     return remoteProxy_->RegisterFormRouterProxy(formIds, callerToken);
1715 #else
1716     return ERR_OK;
1717 #endif
1718 }
1719 
UnregisterFormRouterProxy(const std::vector<int64_t> & formIds)1720 int FormMgr::UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)
1721 {
1722     HILOG_DEBUG("call");
1723     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1724         HILOG_ERROR("form is in recover status,can't do action on form");
1725         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1726     }
1727     auto errCode = Connect();
1728     if (errCode != ERR_OK) {
1729         HILOG_ERROR("errCode:%{public}d", errCode);
1730         return errCode;
1731     }
1732     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1733     if (remoteProxy_ == nullptr) {
1734         HILOG_ERROR("null remoteProxy_");
1735         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1736     }
1737     return remoteProxy_->UnregisterFormRouterProxy(formIds);
1738 }
1739 
SetFormsRecyclable(const std::vector<int64_t> & formIds)1740 int32_t FormMgr::SetFormsRecyclable(const std::vector<int64_t> &formIds)
1741 {
1742     HILOG_DEBUG("call");
1743     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1744         HILOG_ERROR("form is in recover status, can't do action on form");
1745         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1746     }
1747     if (formIds.empty()) {
1748         HILOG_ERROR("empty formIds");
1749         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1750     }
1751     auto errCode = Connect();
1752     if (errCode != ERR_OK) {
1753         HILOG_ERROR("errCode:%{public}d", errCode);
1754         return errCode;
1755     }
1756     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1757     if (remoteProxy_ == nullptr) {
1758         HILOG_ERROR("null remoteProxy_");
1759         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1760     }
1761     return remoteProxy_->SetFormsRecyclable(formIds);
1762 }
1763 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want)1764 int32_t FormMgr::RecycleForms(const std::vector<int64_t> &formIds, const Want &want)
1765 {
1766     HILOG_DEBUG("call");
1767     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1768         HILOG_ERROR("form is in recover status, can't do action on form");
1769         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1770     }
1771     if (formIds.empty()) {
1772         HILOG_ERROR("empty formIds");
1773         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1774     }
1775     auto errCode = Connect();
1776     if (errCode != ERR_OK) {
1777         HILOG_ERROR("errCode:%{public}d", errCode);
1778         return errCode;
1779     }
1780     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1781     if (remoteProxy_ == nullptr) {
1782         HILOG_ERROR("null remoteProxy_");
1783         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1784     }
1785     return remoteProxy_->RecycleForms(formIds, want);
1786 }
1787 
RecoverForms(const std::vector<int64_t> & formIds,const Want & want)1788 int32_t FormMgr::RecoverForms(const std::vector<int64_t> &formIds, const Want &want)
1789 {
1790     HILOG_DEBUG("call");
1791     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1792         HILOG_ERROR("form is in recover status, can't do action on form");
1793         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1794     }
1795     if (formIds.empty()) {
1796         HILOG_ERROR("empty formIds");
1797         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1798     }
1799     auto errCode = Connect();
1800     if (errCode != ERR_OK) {
1801         HILOG_ERROR("errCode:%{public}d", errCode);
1802         return errCode;
1803     }
1804     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1805     if (remoteProxy_ == nullptr) {
1806         HILOG_ERROR("null remoteProxy_");
1807         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1808     }
1809     return remoteProxy_->RecoverForms(formIds, want);
1810 }
1811 
UpdateFormLocation(const int64_t & formId,const int32_t & formLocation)1812 ErrCode FormMgr::UpdateFormLocation(const int64_t &formId, const int32_t &formLocation)
1813 {
1814     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1815         HILOG_ERROR("form is in recover status, can't do action on form");
1816         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1817     }
1818 
1819     ErrCode errCode = Connect();
1820     if (errCode != ERR_OK) {
1821         return errCode;
1822     }
1823     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1824     if (remoteProxy_ == nullptr) {
1825         HILOG_ERROR("null remoteProxy_");
1826         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1827     }
1828     ErrCode resultCode = remoteProxy_->UpdateFormLocation(formId, formLocation);
1829     if (resultCode != ERR_OK) {
1830         HILOG_ERROR("fail UpdateFormLocation,errCode %{public}d", resultCode);
1831     }
1832     return resultCode;
1833 }
1834 
RequestPublishFormWithSnapshot(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)1835 ErrCode FormMgr::RequestPublishFormWithSnapshot(Want &want, bool withFormBindingData,
1836     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
1837     const std::vector<FormDataProxy> &formDataProxies)
1838 {
1839     HILOG_INFO("call");
1840     ErrCode errCode = Connect();
1841     if (errCode != ERR_OK) {
1842         HILOG_ERROR("errCode:%{public}d", errCode);
1843         return errCode;
1844     }
1845     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1846     if (remoteProxy_ == nullptr) {
1847         HILOG_ERROR("null remoteProxy_");
1848         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1849     }
1850     return remoteProxy_->RequestPublishFormWithSnapshot(want, withFormBindingData, formBindingData, formId);
1851 }
1852 
BatchRefreshForms(const int32_t formRefreshType)1853 int32_t FormMgr::BatchRefreshForms(const int32_t formRefreshType)
1854 {
1855     HILOG_INFO("call");
1856     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1857         HILOG_ERROR("BatchRefreshForms failed, form is in recover status, can't do action on form");
1858         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1859     }
1860 
1861     if (formRefreshType < Constants::REFRESH_ALL_FORM || formRefreshType > Constants::REFRESH_ATOMIC_FORM) {
1862         HILOG_ERROR("BatchRefreshForms failed, invalid formRefreshType %{public}d", formRefreshType);
1863         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1864     }
1865 
1866     int errCode = Connect();
1867     if (errCode != ERR_OK) {
1868         HILOG_ERROR("errCode:%{public}d", errCode);
1869         return errCode;
1870     }
1871     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1872     if (remoteProxy_ == nullptr) {
1873         HILOG_ERROR("null remoteProxy_");
1874         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1875     }
1876     return remoteProxy_->BatchRefreshForms(formRefreshType);
1877 }
1878 
EnableForms(const std::string bundleName,const bool enable)1879 int32_t FormMgr::EnableForms(const std::string bundleName, const bool enable)
1880 {
1881     if (bundleName.empty()) {
1882         HILOG_ERROR("empty bundleName");
1883         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1884     }
1885     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1886         HILOG_ERROR("form is in recover status, can't do action on form");
1887         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1888     }
1889 
1890     ErrCode errCode = Connect();
1891     if (errCode != ERR_OK) {
1892         return errCode;
1893     }
1894 
1895     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1896     if (remoteProxy_ == nullptr) {
1897         HILOG_ERROR("null remoteProxy_");
1898         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1899     }
1900     ErrCode resultCode = remoteProxy_->EnableForms(bundleName, enable);
1901     if (resultCode != ERR_OK) {
1902         HILOG_ERROR("fail EnableForms,errCode %{public}d", resultCode);
1903     }
1904     return resultCode;
1905 }
1906 
IsFormBundleForbidden(const std::string & bundleName)1907 bool FormMgr::IsFormBundleForbidden(const std::string &bundleName)
1908 {
1909     ErrCode errCode = Connect();
1910     if (errCode != ERR_OK) {
1911         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1912         return false;
1913     }
1914 
1915     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1916     if (remoteProxy_ == nullptr) {
1917         HILOG_ERROR("null remoteProxy_");
1918         return false;
1919     }
1920     return remoteProxy_->IsFormBundleForbidden(bundleName);
1921 }
1922 
IsFormBundleProtected(const std::string & bundleName,int64_t formId)1923 bool FormMgr::IsFormBundleProtected(const std::string &bundleName, int64_t formId)
1924 {
1925     ErrCode errCode = Connect();
1926     if (errCode != ERR_OK) {
1927         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1928         return false;
1929     }
1930 
1931     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1932     if (remoteProxy_ == nullptr) {
1933         HILOG_ERROR("null remoteProxy_");
1934         return false;
1935     }
1936     return remoteProxy_->IsFormBundleProtected(bundleName, formId);
1937 }
1938 
IsFormBundleExempt(int64_t formId)1939 bool FormMgr::IsFormBundleExempt(int64_t formId)
1940 {
1941     ErrCode errCode = Connect();
1942     if (errCode != ERR_OK) {
1943         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1944         return false;
1945     }
1946 
1947     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1948     if (remoteProxy_ == nullptr) {
1949         HILOG_ERROR("null remoteProxy_");
1950         return false;
1951     }
1952     return remoteProxy_->IsFormBundleExempt(formId);
1953 }
1954 
UpdateFormSize(const int64_t formId,float width,float height,float borderWidth)1955 ErrCode FormMgr::UpdateFormSize(const int64_t formId, float width, float height, float borderWidth)
1956 {
1957     ErrCode errCode = Connect();
1958     if (errCode != ERR_OK) {
1959         return errCode;
1960     }
1961     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1962     if (remoteProxy_ == nullptr) {
1963         HILOG_ERROR("null remoteProxy_");
1964         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1965     }
1966     ErrCode resultCode = remoteProxy_->UpdateFormSize(formId, width, height, borderWidth);
1967     if (resultCode != ERR_OK) {
1968         HILOG_ERROR("fail UpdateFormSize,errCode %{public}d", resultCode);
1969     }
1970     return resultCode;
1971 }
1972 
NotifyFormLocked(const int64_t & formId,bool isLocked)1973 int32_t FormMgr::NotifyFormLocked(const int64_t &formId, bool isLocked)
1974 {
1975     HILOG_DEBUG("call");
1976     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1977         HILOG_ERROR("form is in recover status, can't do action on form");
1978         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1979     }
1980 
1981     int errCode = Connect();
1982     if (errCode != ERR_OK) {
1983         return errCode;
1984     }
1985     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1986     if (remoteProxy_ == nullptr) {
1987         HILOG_ERROR("null remoteProxy_");
1988         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1989     }
1990     int resultCode = remoteProxy_->NotifyFormLocked(formId, isLocked);
1991     if (resultCode != ERR_OK) {
1992         HILOG_ERROR("fail NotifyFormLocked, errCode %{public}d", resultCode);
1993     }
1994     return resultCode;
1995 }
1996 
OpenFormEditAbility(const std::string & abilityName,const int64_t & formId,bool isMainPage)1997 ErrCode FormMgr::OpenFormEditAbility(const std::string &abilityName, const int64_t &formId, bool isMainPage)
1998 {
1999     HILOG_INFO("abilityName: %{public}s, formId: %{public}" PRId64 ", isMainPage: %{public}s",
2000         abilityName.c_str(), formId, isMainPage ? "true" : "false");
2001     ErrCode resultCode = Connect();
2002     if (resultCode != ERR_OK) {
2003         return resultCode;
2004     }
2005     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2006     if (remoteProxy_ == nullptr) {
2007         HILOG_ERROR("null remoteProxy_");
2008         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2009     }
2010     resultCode = remoteProxy_->OpenFormEditAbility(abilityName, formId, isMainPage);
2011     if (resultCode != ERR_OK) {
2012         HILOG_ERROR("fail OpenFormEditAbility,errCode %{public}d", resultCode);
2013     }
2014     return resultCode;
2015 }
2016 }  // namespace AppExecFwk
2017 }  // namespace OHOS
2018