• 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 #ifdef NO_RUNTIME_EMULATOR
22 #include "form_event_hiappevent.h"
23 #endif
24 #include "form_errors.h"
25 #include "form_mgr_errors.h"
26 #include "running_form_info.h"
27 #include "if_system_ability_manager.h"
28 #include "ipc_skeleton.h"
29 #include "iservice_registry.h"
30 #include "string_ex.h"
31 #include "system_ability_definition.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 
FormMgr()36 FormMgr::FormMgr()
37 {
38     HILOG_DEBUG("call");
39 }
40 
~FormMgr()41 FormMgr::~FormMgr()
42 {
43     HILOG_INFO("call");
44     if (remoteProxy_ != nullptr) {
45         auto remoteObject = remoteProxy_->AsObject();
46         if (remoteObject != nullptr) {
47             remoteObject->RemoveDeathRecipient(deathRecipient_);
48         }
49     }
50 }
51 
52 /**
53  * @brief Get the error message by error code.
54  * @param errorCode the error code return form fms.
55  * @return Returns the error message detail.
56  */
GetErrorMsg(int errorCode)57 std::string FormMgr::GetErrorMsg(int errorCode)
58 {
59     return "unknown error";
60 }
61 
62 /**
63  * @brief Add form with want, send want to form manager service.
64  * @param formId The Id of the forms to add.
65  * @param want The want of the form to add.
66  * @param callerToken Caller ability token.
67  * @param formInfo Form info.
68  * @return Returns ERR_OK on success, others on failure.
69  */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)70 int FormMgr::AddForm(
71     const int64_t formId,
72     const Want &want,
73     const sptr<IRemoteObject> &callerToken,
74     FormJsInfo &formInfo)
75 {
76     HILOG_INFO("formId:%{public}" PRId64, formId);
77     int errCode = Connect();
78     if (errCode != ERR_OK) {
79         return errCode;
80     }
81     // Perform read lock control. Do not assign a value to remoteProxy_ in subsequent operations.
82     std::shared_lock<std::shared_mutex> lock(connectMutex_);
83 
84     // To prevent the obtained value of remoteProxy_ from being null,
85     // the system checks whether the value of remoteProxy_ is null.
86     if (remoteProxy_ == nullptr) {
87         HILOG_ERROR("null remoteProxy_");
88         return ERR_APPEXECFWK_FORM_COMMON_CODE;
89     }
90     return remoteProxy_->AddForm(formId, want, callerToken, formInfo);
91 }
92 
93 /**
94  * @brief Add form with want, send want to form manager service.
95  * @param want The want of the form to add.
96  * @param runningFormInfo Running form info.
97  * @return Returns ERR_OK on success, others on failure.
98  */
CreateForm(const Want & want,RunningFormInfo & runningFormInfo)99 int FormMgr::CreateForm(const Want &want, RunningFormInfo &runningFormInfo)
100 {
101     HILOG_INFO("call");
102     int resultCode = Connect();
103     if (resultCode != ERR_OK) {
104         HILOG_ERROR("Connect failed errCode:%{public}d", resultCode);
105         return resultCode;
106     }
107     std::shared_lock<std::shared_mutex> lock(connectMutex_);
108     if (remoteProxy_ == nullptr) {
109         HILOG_ERROR("null remoteProxy_");
110         return ERR_APPEXECFWK_FORM_COMMON_CODE;
111     }
112     resultCode = remoteProxy_->CreateForm(want, runningFormInfo);
113     if (resultCode != ERR_OK) {
114         HILOG_ERROR("createForm failed,errorCode is %{public}d", resultCode);
115     }
116     HILOG_INFO("formId:%{public}s", std::to_string(runningFormInfo.formId).c_str());
117     return resultCode;
118 }
119 
120 /**
121  * @brief Delete forms with formIds, send formIds to form manager service.
122  * @param formId The Id of the forms to delete.
123  * @param callerToken Caller ability token.
124  * @return Returns ERR_OK on success, others on failure.
125  */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)126 int FormMgr::DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
127 {
128     HILOG_INFO("formId:%{public}" PRId64, formId);
129     // check fms recover status
130     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
131         HILOG_ERROR("delete form failed,form in recover status,can't do action on form");
132         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
133     }
134     // check formId
135     if (formId <= 0) {
136         HILOG_ERROR("delete form failed,the passed in formId can't be negative or zero");
137         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
138     }
139 
140     int errCode = Connect();
141     if (errCode != ERR_OK) {
142         return errCode;
143     }
144     std::shared_lock<std::shared_mutex> lock(connectMutex_);
145     if (remoteProxy_ == nullptr) {
146         HILOG_ERROR("null remoteProxy_");
147         return ERR_APPEXECFWK_FORM_COMMON_CODE;
148     }
149     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
150     return remoteProxy_->DeleteForm(formId, callerToken);
151 }
152 
153 /**
154  * @brief Stop rendering form.
155  * @param formId The Id of the forms to delete.
156  * @param compId The compId of the forms to delete.
157  * @return Returns ERR_OK on success, others on failure.
158 */
StopRenderingForm(const int64_t formId,const std::string & compId)159 int FormMgr::StopRenderingForm(const int64_t formId, const std::string &compId)
160 {
161     HILOG_INFO("formId:%{public}" PRId64, formId);
162     // check fms recover status
163     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
164         HILOG_ERROR("form is in recover status, can't do action on form");
165         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
166     }
167     // check formId
168     if (formId <= 0 || compId.empty()) {
169         HILOG_ERROR("invalid formId or empty compId");
170         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
171     }
172 
173     int errCode = Connect();
174     if (errCode != ERR_OK) {
175         return errCode;
176     }
177     std::shared_lock<std::shared_mutex> lock(connectMutex_);
178     if (remoteProxy_ == nullptr) {
179         HILOG_ERROR("null remoteProxy_");
180         return ERR_APPEXECFWK_FORM_COMMON_CODE;
181     }
182     return remoteProxy_->StopRenderingForm(formId, compId);
183 }
184 
185 /**
186  * @brief Release forms with formIds, send formIds to form manager service.
187  * @param formId The Id of the forms to release.
188  * @param callerToken Caller ability token.
189  * @param delCache Delete Cache or not.
190  * @return Returns ERR_OK on success, others on failure.
191  */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)192 int FormMgr::ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache)
193 {
194     HILOG_INFO("formId:%{public}" PRId64 ", delCache:%{public}d", formId, delCache);
195     // check fms recover status
196     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
197         HILOG_ERROR("form is in recover status, can't do action on form");
198         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
199     }
200     // check formId
201     if (formId <= 0) {
202         HILOG_ERROR("the passed in formId can't be negative or zero");
203         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
204     }
205 
206     int errCode = Connect();
207     if (errCode != ERR_OK) {
208         return errCode;
209     }
210     FormCallerMgr::GetInstance().RemoveFormHostCaller(formId);
211     std::shared_lock<std::shared_mutex> lock(connectMutex_);
212     if (remoteProxy_ == nullptr) {
213         HILOG_ERROR("null remoteProxy_");
214         return ERR_APPEXECFWK_FORM_COMMON_CODE;
215     }
216     return remoteProxy_->ReleaseForm(formId, callerToken, delCache);
217 }
218 
219 /**
220  * @brief Update form with formId, send formId to form manager service.
221  * @param formId The Id of the form to update.
222  * @param formBindingData Form binding data.
223  * @return Returns ERR_OK on success, others on failure.
224  */
UpdateForm(const int64_t formId,const FormProviderData & formBindingData,const std::vector<FormDataProxy> & formDataProxies)225 int FormMgr::UpdateForm(const int64_t formId, const FormProviderData &formBindingData,
226     const std::vector<FormDataProxy> &formDataProxies)
227 {
228     HILOG_DEBUG("call");
229     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
230         HILOG_ERROR("UpdateForm failed, form is in recover status, can't do action on form");
231         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
232     }
233 
234     if (formId <= 0) {
235         HILOG_ERROR(" UpdateForm failed, the passed in formId can't be negative or zero");
236         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
237     }
238 
239     // check formBindingData
240     if (formBindingData.GetDataString().empty() && formDataProxies.empty()) {
241         HILOG_ERROR("UpdateForm failed,null formProviderData");
242         return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY;
243     }
244 
245     int errCode = Connect();
246     if (errCode != ERR_OK) {
247         return errCode;
248     }
249 
250     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
251     if (hostCaller != nullptr) {
252         hostCaller->UpdateForm(formId, formBindingData);
253     } else {
254         std::vector<std::shared_ptr<FormProviderCaller>> formProviderCallers;
255         FormCallerMgr::GetInstance().GetFormProviderCaller(formId, formProviderCallers);
256         for (const auto &formProviderCaller : formProviderCallers) {
257             formProviderCaller->UpdateForm(formId, formBindingData);
258         }
259     }
260     std::shared_lock<std::shared_mutex> lock(connectMutex_);
261     if (remoteProxy_ == nullptr) {
262         HILOG_ERROR("null remoteProxy_");
263         return ERR_APPEXECFWK_FORM_COMMON_CODE;
264     }
265     if (formDataProxies.empty()) {
266         return remoteProxy_->UpdateForm(formId, formBindingData);
267     }
268     return remoteProxy_->UpdateProxyForm(formId, formBindingData, formDataProxies);
269 }
270 
271 /**
272  * @brief Release renderer.
273  * @param formId The Id of the forms to release.
274  * @param compId The compId of the forms to release.
275  * @return Returns ERR_OK on success, others on failure.
276 */
ReleaseRenderer(const int64_t formId,const std::string & compId)277 int FormMgr::ReleaseRenderer(const int64_t formId, const std::string &compId)
278 {
279     HILOG_INFO("formId:%{public}" PRId64, formId);
280     // check fms recover status
281     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
282         HILOG_ERROR("form is in recover status, can't do action on form");
283         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
284     }
285     // check formId and compId
286     if (formId <= 0 || compId.empty()) {
287         HILOG_ERROR("invalid formId or empty compId");
288         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
289     }
290 
291     int errCode = Connect();
292     if (errCode != ERR_OK) {
293         return errCode;
294     }
295     std::shared_lock<std::shared_mutex> lock(connectMutex_);
296     if (remoteProxy_ == nullptr) {
297         HILOG_ERROR("null remoteProxy_");
298         return ERR_APPEXECFWK_FORM_COMMON_CODE;
299     }
300     return remoteProxy_->ReleaseRenderer(formId, compId);
301 }
302 
303 /**
304  * @brief Notify the form service that the form user's lifecycle is updated.
305  *
306  * This should be called when form user request form.
307  *
308  * @param formId Indicates the unique id of form.
309  * @param callerToken Indicates the callback remote object of specified form user.
310  * @param want information passed to supplier.
311  * @return Returns true if execute success, false otherwise.
312  */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)313 int FormMgr::RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want)
314 {
315     HILOG_INFO("formId:%{public}" PRId64, formId);
316     if (formId <= 0) {
317         HILOG_ERROR("invalid formId");
318         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
319     }
320     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
321         HILOG_ERROR("form is in recover status, can't do action on form");
322         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
323     }
324     int errCode = Connect();
325     if (errCode != ERR_OK) {
326         return errCode;
327     }
328     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
329     if (hostCaller != nullptr) {
330         HILOG_INFO("request by host caller");
331         return hostCaller->RequestForm(formId, callerToken, want);
332     }
333     std::shared_lock<std::shared_mutex> lock(connectMutex_);
334     if (remoteProxy_ == nullptr) {
335         HILOG_ERROR("null remoteProxy_");
336         return ERR_APPEXECFWK_FORM_COMMON_CODE;
337     }
338     ErrCode resultCode = remoteProxy_->RequestForm(formId, callerToken, want);
339     if (resultCode != ERR_OK) {
340         HILOG_ERROR("fail notify the form service that the form user's lifecycle is updated"
341             "code is %{public}d", resultCode);
342     }
343     return resultCode;
344 }
345 
346 /**
347  * @brief Form visible/invisible notify, send formIds to form manager service.
348  * @param formIds The Id list of the forms to notify.
349  * @param callerToken Caller ability token.
350  * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
351  * @return Returns ERR_OK on success, others on failure.
352  */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)353 int FormMgr::NotifyWhetherVisibleForms(
354     const std::vector<int64_t> &formIds,
355     const sptr<IRemoteObject> &callerToken,
356     const int32_t formVisibleType)
357 {
358     HILOG_DEBUG("formVisibleType is %{public}d", formVisibleType);
359 
360     if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) {
361         HILOG_ERROR("empty formIds or exceed 32");
362         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
363     }
364 
365     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
366         HILOG_ERROR("form is in recover status, can't do action on form");
367         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
368     }
369 
370     int errCode = Connect();
371     if (errCode != ERR_OK) {
372         return errCode;
373     }
374     std::shared_lock<std::shared_mutex> lock(connectMutex_);
375     if (remoteProxy_ == nullptr) {
376         HILOG_ERROR("null remoteProxy_");
377         return ERR_APPEXECFWK_FORM_COMMON_CODE;
378     }
379     // IPC entry
380     ErrCode resultCode = remoteProxy_->NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType);
381     if (resultCode != ERR_OK) {
382         HILOG_ERROR("internal error occurs,errCode:%{public}d", resultCode);
383     }
384     return resultCode;
385 }
386 
387 /**
388  * @brief Query whether has visible form by tokenId.
389  * @param tokenId Unique identification of application.
390  * @return Returns true if has visible form, false otherwise.
391  */
HasFormVisible(const uint32_t tokenId)392 bool FormMgr::HasFormVisible(const uint32_t tokenId)
393 {
394     HILOG_DEBUG("call");
395     int errCode = Connect();
396     if (errCode != ERR_OK) {
397         HILOG_ERROR("errCode:%{public}d", errCode);
398         return false;
399     }
400     std::shared_lock<std::shared_mutex> lock(connectMutex_);
401     if (remoteProxy_ == nullptr) {
402         HILOG_ERROR("null remoteProxy_");
403         return false;
404     }
405     return remoteProxy_->HasFormVisible(tokenId);
406 }
407 
408 /**
409  * @brief temp form to normal form.
410  * @param formId The Id of the form.
411  * @param callerToken Caller ability token.
412  * @return None.
413  */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)414 int FormMgr::CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken)
415 {
416     HILOG_INFO("formId:%{public}" PRId64, formId);
417     if (formId <= 0) {
418         HILOG_ERROR("passing in form id can't be negative");
419         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
420     }
421 
422     int errCode = Connect();
423     if (errCode != ERR_OK) {
424         return errCode;
425     }
426     std::shared_lock<std::shared_mutex> lock(connectMutex_);
427     if (remoteProxy_ == nullptr) {
428         HILOG_ERROR("null remoteProxy_");
429         return ERR_APPEXECFWK_FORM_COMMON_CODE;
430     }
431     return remoteProxy_->CastTempForm(formId, callerToken);
432 }
433 
434 /**
435  * @brief Dump all of form storage infos.
436  * @param formInfos All of form storage infos.
437  * @return Returns ERR_OK on success, others on failure.
438  */
DumpStorageFormInfos(std::string & formInfos)439 int FormMgr::DumpStorageFormInfos(std::string &formInfos)
440 {
441     HILOG_DEBUG("call");
442     int errCode = Connect();
443     if (errCode != ERR_OK) {
444         return errCode;
445     }
446     std::shared_lock<std::shared_mutex> lock(connectMutex_);
447     if (remoteProxy_ == nullptr) {
448         HILOG_ERROR("null remoteProxy_");
449         return ERR_APPEXECFWK_FORM_COMMON_CODE;
450     }
451     return remoteProxy_->DumpStorageFormInfos(formInfos);
452 }
453 /**
454  * @brief Dump form info by a bundle name.
455  * @param bundleName The bundle name of form provider.
456  * @param formInfos Form infos.
457  * @return Returns ERR_OK on success, others on failure.
458  */
DumpFormInfoByBundleName(const std::string bundleName,std::string & formInfos)459 int FormMgr::DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos)
460 {
461     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
462     int errCode = Connect();
463     if (errCode != ERR_OK) {
464         return errCode;
465     }
466     std::shared_lock<std::shared_mutex> lock(connectMutex_);
467     if (remoteProxy_ == nullptr) {
468         HILOG_ERROR("null remoteProxy_");
469         return ERR_APPEXECFWK_FORM_COMMON_CODE;
470     }
471     return remoteProxy_->DumpFormInfoByBundleName(bundleName, formInfos);
472 }
473 /**
474  * @brief Dump form info by a bundle name.
475  * @param formId The id of the form.
476  * @param formInfo Form info.
477  * @return Returns ERR_OK on success, others on failure.
478  */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo)479 int FormMgr::DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo)
480 {
481     HILOG_INFO("formId:%{public}" PRId64, formId);
482     int errCode = Connect();
483     if (errCode != ERR_OK) {
484         return errCode;
485     }
486     std::shared_lock<std::shared_mutex> lock(connectMutex_);
487     if (remoteProxy_ == nullptr) {
488         HILOG_ERROR("null remoteProxy_");
489         return ERR_APPEXECFWK_FORM_COMMON_CODE;
490     }
491     return remoteProxy_->DumpFormInfoByFormId(formId, formInfo);
492 }
493 /**
494  * @brief Dump form timer by form id.
495  * @param formId The id of the form.
496  * @param formInfo Form timer.
497  * @return Returns ERR_OK on success, others on failure.
498  */
DumpFormTimerByFormId(const std::int64_t formId,std::string & isTimingService)499 int FormMgr::DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService)
500 {
501     HILOG_INFO("call");
502     int errCode = Connect();
503     if (errCode != ERR_OK) {
504         HILOG_ERROR("errCode:%{public}d", errCode);
505         return errCode;
506     }
507     std::shared_lock<std::shared_mutex> lock(connectMutex_);
508     if (remoteProxy_ == nullptr) {
509         HILOG_ERROR("null remoteProxy_");
510         return ERR_APPEXECFWK_FORM_COMMON_CODE;
511     }
512     return remoteProxy_->DumpFormTimerByFormId(formId, isTimingService);
513 }
514 /**
515  * @brief Process js message event.
516  * @param formId Indicates the unique id of form.
517  * @param want information passed to supplier.
518  * @param callerToken Caller ability token.
519  * @return Returns true if execute success, false otherwise.
520  */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)521 int FormMgr::MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken)
522 {
523     HILOG_INFO("call");
524     int errCode = Connect();
525     if (errCode != ERR_OK) {
526         HILOG_ERROR("errCode:%{public}d", errCode);
527         return errCode;
528     }
529     auto hostCaller = FormCallerMgr::GetInstance().GetFormHostCaller(formId);
530     if (hostCaller != nullptr) {
531         HILOG_DEBUG("send message by host caller");
532         return hostCaller->MessageEvent(formId, want, callerToken);
533     }
534     std::shared_lock<std::shared_mutex> lock(connectMutex_);
535     if (remoteProxy_ == nullptr) {
536         HILOG_ERROR("null remoteProxy_");
537         return ERR_APPEXECFWK_FORM_COMMON_CODE;
538     }
539     return remoteProxy_->MessageEvent(formId, want, callerToken);
540 }
541 
542 /**
543  * @brief Process js router event.
544  * @param formId Indicates the unique id of form.
545  * @param want the want of the ability to start.
546  * @param callerToken Caller ability token.
547  * @return Returns true if execute success, false otherwise.
548  */
RouterEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)549 int FormMgr::RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
550 {
551     HILOG_INFO("call");
552     int errCode = Connect();
553     if (errCode != ERR_OK) {
554         HILOG_ERROR("errCode:%{public}d", errCode);
555         return errCode;
556     }
557     std::shared_lock<std::shared_mutex> lock(connectMutex_);
558     if (remoteProxy_ == nullptr) {
559         HILOG_ERROR("null remoteProxy_");
560         return ERR_APPEXECFWK_FORM_COMMON_CODE;
561     }
562     return remoteProxy_->RouterEvent(formId, want, callerToken);
563 }
564 
565 /**
566  * @brief Process Background event.
567  * @param formId Indicates the unique id of form.
568  * @param want the want of the ability to start.
569  * @param callerToken Caller ability token.
570  * @return Returns true if execute success, false otherwise.
571  */
BackgroundEvent(const int64_t formId,Want & want,const sptr<IRemoteObject> & callerToken)572 int FormMgr::BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken)
573 {
574     HILOG_INFO("call");
575     int errCode = Connect();
576     if (errCode != ERR_OK) {
577         HILOG_ERROR("errCode:%{public}d", errCode);
578         return errCode;
579     }
580     std::shared_lock<std::shared_mutex> lock(connectMutex_);
581     if (remoteProxy_ == nullptr) {
582         HILOG_ERROR("null remoteProxy_");
583         return ERR_APPEXECFWK_FORM_COMMON_CODE;
584     }
585     return remoteProxy_->BackgroundEvent(formId, want, callerToken);
586 }
587 
588 /**
589  * @brief Set next refresh time.
590  * @param formId The id of the form.
591  * @param nextTime Next refresh time.
592  * @return Returns ERR_OK on success, others on failure.
593  */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)594 int FormMgr::SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
595 {
596     HILOG_INFO("call, nextTime:%{public}" PRId64 ", formId:%{public}" PRId64, nextTime, formId);
597 
598     if (GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
599         HILOG_ERROR("formManager is in recovering");
600         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
601     }
602 
603     int errCode = Connect();
604     if (errCode != ERR_OK) {
605         HILOG_ERROR("errCode:%{public}d", errCode);
606         return errCode;
607     }
608     std::shared_lock<std::shared_mutex> lock(connectMutex_);
609     if (remoteProxy_ == nullptr) {
610         HILOG_ERROR("null remoteProxy_");
611         return ERR_APPEXECFWK_FORM_COMMON_CODE;
612     }
613     return remoteProxy_->SetNextRefreshTime(formId, nextTime);
614 }
615 
RequestPublishForm(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)616 ErrCode FormMgr::RequestPublishForm(Want &want, bool withFormBindingData,
617     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
618     const std::vector<FormDataProxy> &formDataProxies)
619 {
620     HILOG_INFO("call");
621 #ifdef NO_RUNTIME_EMULATOR
622     int64_t processorId = FormEventHiAppEvent::AddProcessor();
623     HILOG_INFO("Add processor begin.Processor id is %{public}" PRId64, processorId);
624     time_t beginTime = time(nullptr);
625 #endif
626     ErrCode errCode = Connect();
627     if (errCode != ERR_OK) {
628         HILOG_ERROR("errCode:%{public}d", errCode);
629         return errCode;
630     }
631     std::shared_lock<std::shared_mutex> lock(connectMutex_);
632     if (remoteProxy_ == nullptr) {
633         HILOG_ERROR("null remoteProxy_");
634         return ERR_APPEXECFWK_FORM_COMMON_CODE;
635     }
636     if (formDataProxies.empty()) {
637         return remoteProxy_->RequestPublishForm(want, withFormBindingData, formBindingData, formId);
638     }
639     ErrCode ret = remoteProxy_->RequestPublishProxyForm(want, withFormBindingData, formBindingData,
640         formId, formDataProxies);
641 #ifdef NO_RUNTIME_EMULATOR
642     PublishFormData publishFormData = {
643     want.GetElement().GetBundleName(),
644     want.GetElement().GetAbilityName(),
645     want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, -1),
646     want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY),
647     want.GetStringParam(Constants::PARAM_FORM_NAME_KEY)};
648     FormEventHiAppEvent::WriteAppFormEndEvent(ret, beginTime, "RequestPublishForm", publishFormData, processorId);
649 #endif
650     return ret;
651 }
652 
653 
SetPublishFormResult(const int64_t formId,Constants::PublishFormResult & errorCodeInfo)654 ErrCode FormMgr::SetPublishFormResult(const int64_t formId, Constants::PublishFormResult &errorCodeInfo)
655 {
656     HILOG_INFO("call");
657     if (formId <= 0) {
658         HILOG_ERROR("errCode:%{public}." PRId64, formId);
659         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
660     }
661     ErrCode errCode = Connect();
662     if (errCode != ERR_OK) {
663         HILOG_ERROR("errCode:%{public}d", errCode);
664         return errCode;
665     }
666     std::shared_lock<std::shared_mutex> lock(connectMutex_);
667     if (remoteProxy_ == nullptr) {
668         HILOG_ERROR("null remoteProxy_");
669         return ERR_APPEXECFWK_FORM_COMMON_CODE;
670     }
671     return remoteProxy_->SetPublishFormResult(formId, errorCodeInfo);
672 }
673 
AcquireAddFormResult(const int64_t formId)674 ErrCode FormMgr::AcquireAddFormResult(const int64_t formId)
675 {
676     HILOG_INFO("call");
677     if (formId <= 0) {
678         HILOG_ERROR("errCode:%{public}" PRId64, formId);
679         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
680     }
681     ErrCode errCode = Connect();
682     if (errCode != ERR_OK) {
683         HILOG_ERROR("errCode:%{public}d", errCode);
684         return errCode;
685     }
686     std::shared_lock<std::shared_mutex> lock(connectMutex_);
687     if (remoteProxy_ == nullptr) {
688         HILOG_ERROR("null remoteProxy_");
689         return ERR_APPEXECFWK_FORM_COMMON_CODE;
690     }
691     return remoteProxy_->AcquireAddFormResult(formId);
692 }
693 
LifecycleUpdate(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,bool updateType)694 int FormMgr::LifecycleUpdate(
695     const std::vector<int64_t> &formIds,
696     const sptr<IRemoteObject> &callerToken,
697     bool updateType)
698 {
699     HILOG_INFO("call");
700 
701     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
702         HILOG_ERROR("form is in recover status, can't do action on form");
703         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
704     }
705 
706     int errCode = Connect();
707     if (errCode != ERR_OK) {
708         HILOG_ERROR("errCode:%{public}d", errCode);
709         return errCode;
710     }
711     std::shared_lock<std::shared_mutex> lock(connectMutex_);
712     if (remoteProxy_ == nullptr) {
713         HILOG_ERROR("null remoteProxy_");
714         return ERR_APPEXECFWK_FORM_COMMON_CODE;
715     }
716     return remoteProxy_->LifecycleUpdate(formIds, callerToken, updateType);
717 }
718 /**
719  * @brief Get fms recoverStatus.
720  *
721  * @return The current recover status.
722  */
GetRecoverStatus()723 int FormMgr::GetRecoverStatus()
724 {
725     HILOG_DEBUG("get recover status");
726     return recoverStatus_;
727 }
728 
729 /**
730  * @brief Set fms recoverStatus.
731  *
732  * @param recoverStatus The recover status.
733  */
SetRecoverStatus(int recoverStatus)734 void FormMgr::SetRecoverStatus(int recoverStatus)
735 {
736     HILOG_INFO("call");
737     recoverStatus_ = recoverStatus;
738 }
739 
740 /**
741  * @brief Get the error message content.
742  *
743  * @param errCode Error code.
744  * @return Message content.
745  */
GetErrorMessage(int errCode)746 std::string FormMgr::GetErrorMessage(int errCode)
747 {
748     HILOG_INFO("call");
749     return FormErrors::GetInstance().GetErrorMessage(errCode);
750 }
751 
752 /**
753  * @brief Register death callback.
754  *
755  * @param deathCallback Death callback.
756  */
RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)757 void FormMgr::RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
758 {
759     HILOG_INFO("call");
760     if (formDeathCallback == nullptr) {
761         HILOG_ERROR("null formDeathCallback");
762         return;
763     }
764     formDeathCallbacks_.emplace_back(formDeathCallback);
765 }
766 
767 /**
768  * @brief UnRegister death callback.
769  *
770  * @param deathCallback Death callback.
771  */
UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)772 void FormMgr::UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
773 {
774     HILOG_INFO("call");
775     if (formDeathCallback == nullptr) {
776         HILOG_ERROR("null formDeathCallback");
777         return;
778     }
779 
780     // Remove the specified death callback in the vector of death callback
781     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
782     if (iter != formDeathCallbacks_.end()) {
783         formDeathCallbacks_.erase(iter);
784     }
785     HILOG_INFO("end");
786 }
787 
788 /**
789  * @brief Get death recipient.
790  * @return deathRecipient_.
791  */
GetDeathRecipient() const792 sptr<IRemoteObject::DeathRecipient> FormMgr::GetDeathRecipient() const
793 {
794     return deathRecipient_;
795 }
796 
797 /**
798  * @brief Check whether the specified death callback is registered in form mgr.
799  * @param formDeathCallback The specified death callback for checking.
800  * @return Return true on success, false on failure.
801  */
CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> & formDeathCallback)802 bool FormMgr::CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback)
803 {
804     HILOG_INFO("call");
805     auto iter = std::find(formDeathCallbacks_.begin(), formDeathCallbacks_.end(), formDeathCallback);
806     if (iter != formDeathCallbacks_.end()) {
807         return true;
808     }
809     return false;
810 }
811 
812 /**
813  * @brief Notices IRemoteBroker died.
814  * @param remote remote object.
815  */
OnRemoteDied(const wptr<IRemoteObject> & remote)816 void FormMgr::FormMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
817 {
818     HILOG_INFO("call");
819     if (remote == nullptr) {
820         HILOG_ERROR("null remote");
821         return;
822     }
823 
824     if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) {
825         HILOG_WARN("fms in recovering");
826         return;
827     }
828     // Reset proxy
829     FormMgr::GetInstance().ResetProxy(remote);
830 
831     if (!FormMgr::GetInstance().Reconnect()) {
832         HILOG_ERROR("form mgr service died,try to reconnect to fms failed");
833         FormMgr::GetInstance().SetRecoverStatus(Constants::RECOVER_FAIL);
834         return;
835     }
836 
837     // refresh form host.
838     for (auto &deathCallback : FormMgr::GetInstance().formDeathCallbacks_) {
839         deathCallback->OnDeathReceived();
840     }
841     FormMgr::GetInstance().SetRecoverStatus(Constants::NOT_IN_RECOVERY);
842 }
843 
844 /**
845  * @brief Reconnect form manager service once per 1000 milliseconds,
846  *        until the connection succeeds or reaching the max retry times.
847  * @return Returns true if execute success, false otherwise.
848  */
Reconnect()849 bool FormMgr::Reconnect()
850 {
851     HILOG_DEBUG("call");
852     for (int i = 0; i < Constants::MAX_RETRY_TIME; i++) {
853         // Sleep 1000 milliseconds before reconnect.
854         std::this_thread::sleep_for(std::chrono::milliseconds(Constants::SLEEP_TIME));
855 
856         // try to connect fms
857         if (Connect() != ERR_OK) {
858             HILOG_ERROR("get fms proxy fail,try again");
859             continue;
860         }
861 
862         HILOG_INFO("success");
863         return true;
864     }
865 
866     return false;
867 }
868 
869 /**
870  * @brief Connect form manager service.
871  * @return Returns ERR_OK on success, others on failure.
872  */
Connect()873 ErrCode FormMgr::Connect()
874 {
875     {
876         std::shared_lock<std::shared_mutex> lock(connectMutex_);
877         if (remoteProxy_ != nullptr && !resetFlag_) {
878             return ERR_OK;
879         }
880     }
881     {
882         std::lock_guard<std::shared_mutex> lock(connectMutex_);
883         sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
884         if (systemManager == nullptr) {
885             HILOG_ERROR("get registry failed");
886             return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
887         }
888         sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(FORM_MGR_SERVICE_ID);
889         if (remoteObject == nullptr) {
890             HILOG_ERROR("connect FormMgrService failed");
891             return ERR_APPEXECFWK_FORM_GET_FMS_FAILED;
892         }
893         deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) FormMgrDeathRecipient());
894         if (deathRecipient_ == nullptr) {
895             HILOG_ERROR("null deathRecipient_");
896             return ERR_APPEXECFWK_FORM_COMMON_CODE;
897         }
898         if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
899             HILOG_ERROR("fail add death recipient to FormMgrService");
900             return ERR_APPEXECFWK_FORM_COMMON_CODE;
901         }
902 
903         remoteProxy_ = iface_cast<IFormMgr>(remoteObject);
904         if (remoteProxy_ == nullptr) {
905             HILOG_ERROR("null remoteProxy_");
906             return ERR_APPEXECFWK_FORM_COMMON_CODE;
907         }
908         HILOG_DEBUG("Connecting FormMgrService success");
909         return ERR_OK;
910     }
911 }
912 
913 /**
914  * @brief Reset proxy.
915  * @param remote remote object.
916  */
ResetProxy(const wptr<IRemoteObject> & remote)917 void FormMgr::ResetProxy(const wptr<IRemoteObject> &remote)
918 {
919     HILOG_DEBUG("call");
920     std::lock_guard<std::shared_mutex> lock(connectMutex_);
921     if (remoteProxy_ == nullptr) {
922         HILOG_ERROR("null remoteProxy_");
923         return;
924     }
925 
926     // set formMgr's recover status to IN_RECOVERING.
927     recoverStatus_ = Constants::IN_RECOVERING;
928 
929     // remove the death recipient
930     auto serviceRemote = remoteProxy_->AsObject();
931     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
932         serviceRemote->RemoveDeathRecipient(deathRecipient_);
933     }
934     // clearn the remote proxy
935     remoteProxy_ = nullptr;
936 }
937 
938 /**
939  * @brief Set form mgr service for test.
940  */
SetFormMgrService(sptr<IFormMgr> formMgrService)941 void FormMgr::SetFormMgrService(sptr<IFormMgr> formMgrService)
942 {
943     HILOG_DEBUG("call");
944     std::lock_guard<std::shared_mutex> lock(connectMutex_);
945     remoteProxy_ = formMgrService;
946 }
947 
948 /**
949  * @brief Delete the invalid forms.
950  * @param formIds Indicates the ID of the valid forms.
951  * @param callerToken Host client.
952  * @param numFormsDeleted Returns the number of the deleted forms.
953  * @return Returns ERR_OK on success, others on failure.
954  */
DeleteInvalidForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,int32_t & numFormsDeleted)955 int FormMgr::DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
956                                 int32_t &numFormsDeleted)
957 {
958     HILOG_DEBUG("call");
959     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
960         HILOG_ERROR("form is in recover status, can't do action on form");
961         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
962     }
963 
964     int errCode = Connect();
965     if (errCode != ERR_OK) {
966         return errCode;
967     }
968     std::shared_lock<std::shared_mutex> lock(connectMutex_);
969     if (remoteProxy_ == nullptr) {
970         HILOG_ERROR("null remoteProxy_");
971         return ERR_APPEXECFWK_FORM_COMMON_CODE;
972     }
973     int resultCode = remoteProxy_->DeleteInvalidForms(formIds, callerToken, numFormsDeleted);
974     if (resultCode != ERR_OK) {
975         HILOG_ERROR("fail DeleteInvalidForms,errCode %{public}d", resultCode);
976     }
977     return resultCode;
978 }
979 
980 /**
981  * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
982  * @param want Indicates a set of parameters to be transparently passed to the form provider.
983  * @param callerToken Host client.
984  * @param stateInfo Returns the form's state info of the specify.
985  * @return Returns ERR_OK on success, others on failure.
986  */
AcquireFormState(const Want & want,const sptr<IRemoteObject> & callerToken,FormStateInfo & stateInfo)987 int FormMgr::AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo)
988 {
989     HILOG_DEBUG("call");
990     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
991         HILOG_ERROR("form is in recover status, can't do action on form");
992         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
993     }
994 
995     int errCode = Connect();
996     if (errCode != ERR_OK) {
997         return errCode;
998     }
999     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1000     if (remoteProxy_ == nullptr) {
1001         HILOG_ERROR("null remoteProxy_");
1002         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1003     }
1004     int resultCode = remoteProxy_->AcquireFormState(want, callerToken, stateInfo);
1005     if (resultCode != ERR_OK) {
1006         HILOG_ERROR("fail AcquireFormState,errCode %{public}d", resultCode);
1007     }
1008     return resultCode;
1009 }
1010 
1011 /**
1012  * @brief Notify the form is visible or not.
1013  * @param formIds Indicates the ID of the forms.
1014  * @param isVisible Visible or not.
1015  * @param callerToken Host client.
1016  * @return Returns ERR_OK on success, others on failure.
1017  */
NotifyFormsVisible(const std::vector<int64_t> & formIds,bool isVisible,const sptr<IRemoteObject> & callerToken)1018 int FormMgr::NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
1019                                 const sptr<IRemoteObject> &callerToken)
1020 {
1021     HILOG_DEBUG("call");
1022     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1023         HILOG_ERROR("form is in recover status, can't do action on form");
1024         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1025     }
1026 
1027     int errCode = Connect();
1028     if (errCode != ERR_OK) {
1029         return errCode;
1030     }
1031     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1032     if (remoteProxy_ == nullptr) {
1033         HILOG_ERROR("null remoteProxy_");
1034         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1035     }
1036     int resultCode = remoteProxy_->NotifyFormsVisible(formIds, isVisible, callerToken);
1037     if (resultCode != ERR_OK) {
1038         HILOG_ERROR("fail NotifyFormsVisible,errCode %{public}d", resultCode);
1039     }
1040     return resultCode;
1041 }
1042 
NotifyFormsPrivacyProtected(const std::vector<int64_t> & formIds,bool isProtected,const sptr<IRemoteObject> & callerToken)1043 int FormMgr::NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
1044                                          const sptr<IRemoteObject> &callerToken)
1045 {
1046     HILOG_DEBUG("call");
1047     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1048         HILOG_ERROR("form is in recover status, can't do action on form");
1049         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1050     }
1051 
1052     int errCode = Connect();
1053     if (errCode != ERR_OK) {
1054         return errCode;
1055     }
1056     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1057     if (remoteProxy_ == nullptr) {
1058         HILOG_ERROR("null remoteProxy_");
1059         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1060     }
1061     int resultCode = remoteProxy_->NotifyFormsPrivacyProtected(formIds, isProtected, callerToken);
1062     if (resultCode != ERR_OK) {
1063         HILOG_ERROR("fail NotifyFormsPrivacyProtected,errCode %{public}d", resultCode);
1064     }
1065     return resultCode;
1066 }
1067 
1068 /**
1069  * @brief Notify the form is enable to be updated or not.
1070  * @param formIds Indicates the ID of the forms.
1071  * @param isEnableUpdate enable update or not.
1072  * @param callerToken Host client.
1073  * @return Returns ERR_OK on success, others on failure.
1074  */
NotifyFormsEnableUpdate(const std::vector<int64_t> & formIds,bool isEnableUpdate,const sptr<IRemoteObject> & callerToken)1075 int FormMgr::NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
1076                                      const sptr<IRemoteObject> &callerToken)
1077 {
1078     HILOG_DEBUG("call");
1079     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1080         HILOG_ERROR("form is in recover status, can't do action on form");
1081         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1082     }
1083 
1084     int errCode = Connect();
1085     if (errCode != ERR_OK) {
1086         return errCode;
1087     }
1088     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1089     if (remoteProxy_ == nullptr) {
1090         HILOG_ERROR("null remoteProxy_");
1091         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1092     }
1093     int resultCode = remoteProxy_->NotifyFormsEnableUpdate(formIds, isEnableUpdate, callerToken);
1094     if (resultCode != ERR_OK) {
1095         HILOG_ERROR("fail NotifyFormsEnableUpdate,errCode %{public}d", resultCode);
1096     }
1097     return resultCode;
1098 }
1099 
1100 /**
1101  * @brief Get All FormsInfo.
1102  * @param formInfos Return the forms' information of all forms provided.
1103  * @return Returns ERR_OK on success, others on failure.
1104  */
GetAllFormsInfo(std::vector<FormInfo> & formInfos)1105 int FormMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos)
1106 {
1107     HILOG_DEBUG("call");
1108     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1109         HILOG_ERROR("form is in recover status, can't do action on form");
1110         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1111     }
1112 
1113     int errCode = Connect();
1114     if (errCode != ERR_OK) {
1115         return errCode;
1116     }
1117     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1118     if (remoteProxy_ == nullptr) {
1119         HILOG_ERROR("null remoteProxy_");
1120         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1121     }
1122     int resultCode = remoteProxy_->GetAllFormsInfo(formInfos);
1123     if (resultCode != ERR_OK) {
1124         HILOG_ERROR("fail GetAllFormsInfo,errCode %{public}d", resultCode);
1125     }
1126     return resultCode;
1127 }
1128 
1129 /**
1130  * @brief Get forms info by bundle name .
1131  * @param bundleName Application name.
1132  * @param formInfos Return the forms' information of the specify application name.
1133  * @return Returns ERR_OK on success, others on failure.
1134  */
GetFormsInfoByApp(std::string & bundleName,std::vector<FormInfo> & formInfos)1135 int FormMgr::GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos)
1136 {
1137     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1138     if (bundleName.empty()) {
1139         HILOG_WARN("fail Get forms info,because empty bundle name");
1140         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1141     }
1142 
1143     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1144         HILOG_ERROR("form is in recover status, can't do action on form");
1145         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1146     }
1147 
1148     int errCode = Connect();
1149     if (errCode != ERR_OK) {
1150         return errCode;
1151     }
1152 
1153     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1154     if (remoteProxy_ == nullptr) {
1155         HILOG_ERROR("null remoteProxy_");
1156         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1157     }
1158     int resultCode = remoteProxy_->GetFormsInfoByApp(bundleName, formInfos);
1159     if (resultCode != ERR_OK) {
1160         HILOG_ERROR("fail GetFormsInfoByApp,errCode %{public}d", resultCode);
1161     }
1162     return resultCode;
1163 }
1164 /**
1165  * @brief Get forms info by bundle name and module name.
1166  * @param bundleName bundle name.
1167  * @param moduleName Module name of hap.
1168  * @param formInfos Return the forms' information of the specify bundle name and module name.
1169  * @return Returns ERR_OK on success, others on failure.
1170  */
GetFormsInfoByModule(std::string & bundleName,std::string & moduleName,std::vector<FormInfo> & formInfos)1171 int FormMgr::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName,
1172     std::vector<FormInfo> &formInfos)
1173 {
1174     HILOG_INFO("bundleName is %{public}s, moduleName is %{public}s", bundleName.c_str(), moduleName.c_str());
1175     if (bundleName.empty()) {
1176         HILOG_WARN("fail Get forms info,because empty bundleName");
1177         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1178     }
1179 
1180     if (moduleName.empty()) {
1181         HILOG_WARN("fail Get forms info,because empty moduleName");
1182         return ERR_APPEXECFWK_FORM_INVALID_MODULENAME;
1183     }
1184 
1185     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1186         HILOG_ERROR("form is in recover status, can't do action on form");
1187         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1188     }
1189 
1190     int errCode = Connect();
1191     if (errCode != ERR_OK) {
1192         return errCode;
1193     }
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_->GetFormsInfoByModule(bundleName, moduleName, formInfos);
1201     if (resultCode != ERR_OK) {
1202         HILOG_ERROR("fail GetFormsInfoByModule,errCode %{public}d", resultCode);
1203     }
1204     return resultCode;
1205 }
1206 
GetFormsInfoByFilter(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)1207 int FormMgr::GetFormsInfoByFilter(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     int resultCode = remoteProxy_->GetFormsInfoByFilter(filter, formInfos);
1220     if (resultCode != ERR_OK) {
1221         HILOG_ERROR("fail GetFormsInfoByFilter,errCode %{public}d", resultCode);
1222     }
1223     return resultCode;
1224 }
1225 
GetFormsInfo(const FormInfoFilter & filter,std::vector<FormInfo> & formInfos)1226 int32_t FormMgr::GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos)
1227 {
1228     HILOG_DEBUG("call");
1229     int errCode = Connect();
1230     if (errCode != ERR_OK) {
1231         return errCode;
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_->GetFormsInfo(filter, formInfos);
1239 }
1240 
GetPublishedFormInfoById(const int64_t formId,RunningFormInfo & formInfo)1241 int32_t FormMgr::GetPublishedFormInfoById(const int64_t formId, RunningFormInfo &formInfo)
1242 {
1243     HILOG_DEBUG("call");
1244     int 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_->GetPublishedFormInfoById(formId, formInfo);
1254 }
1255 
GetPublishedFormInfos(std::vector<RunningFormInfo> & formInfos)1256 int32_t FormMgr::GetPublishedFormInfos(std::vector<RunningFormInfo> &formInfos)
1257 {
1258     HILOG_DEBUG("call");
1259     int errCode = Connect();
1260     if (errCode != ERR_OK) {
1261         return errCode;
1262     }
1263     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1264     if (remoteProxy_ == nullptr) {
1265         HILOG_ERROR("null remoteProxy_");
1266         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1267     }
1268     return remoteProxy_->GetPublishedFormInfos(formInfos);
1269 }
1270 
IsRequestPublishFormSupported()1271 bool FormMgr::IsRequestPublishFormSupported()
1272 {
1273     HILOG_DEBUG("call");
1274     int errCode = Connect();
1275     if (errCode != ERR_OK) {
1276         return false;
1277     }
1278     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1279     if (remoteProxy_ == nullptr) {
1280         HILOG_ERROR("null remoteProxy_");
1281         return false;
1282     }
1283     return remoteProxy_->IsRequestPublishFormSupported();
1284 }
1285 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken)1286 int32_t FormMgr::StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken)
1287 {
1288     HILOG_DEBUG("call");
1289     int32_t errCode = Connect();
1290     if (errCode != ERR_OK) {
1291         return errCode;
1292     }
1293     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1294     if (remoteProxy_ == nullptr) {
1295         HILOG_ERROR("null remoteProxy_");
1296         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1297     }
1298     return remoteProxy_->StartAbility(want, callerToken);
1299 }
1300 
StartAbilityByFms(const Want & want)1301 int32_t FormMgr::StartAbilityByFms(const Want &want)
1302 {
1303     HILOG_DEBUG("call");
1304     int32_t errCode = Connect();
1305     if (errCode != ERR_OK) {
1306         return errCode;
1307     }
1308     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1309     if (remoteProxy_ == nullptr) {
1310         HILOG_ERROR("null remoteProxy_");
1311         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1312     }
1313     return remoteProxy_->StartAbilityByFms(want);
1314 }
1315 
StartAbilityByCrossBundle(const Want & want)1316 int32_t FormMgr::StartAbilityByCrossBundle(const Want &want)
1317 {
1318     HILOG_DEBUG("call");
1319     int32_t errCode = Connect();
1320     if (errCode != ERR_OK) {
1321         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
1322     }
1323     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1324     if (remoteProxy_ == nullptr) {
1325         HILOG_ERROR("null remoteProxy_");
1326         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
1327     }
1328     auto ret = remoteProxy_->StartAbilityByCrossBundle(want);
1329     if (ret == ERR_APPEXECFWK_FORM_NOT_TRUST) {
1330         return ERR_FORM_EXTERNAL_PERMISSION_DENIED;
1331     } else if (ret == ERR_APPEXECFWK_FORM_GET_HOST_FAILED || ret == ERR_APPEXECFWK_FORM_COMMON_CODE) {
1332         return ERR_APPEXECFWK_FORM_GET_SYSMGR_FAILED;
1333     }
1334     return ret;
1335 }
1336 
ShareForm(int64_t formId,const std::string & remoteDeviceId,const sptr<IRemoteObject> & callerToken,int64_t requestCode)1337 int32_t FormMgr::ShareForm(int64_t formId, const std::string &remoteDeviceId,
1338     const sptr<IRemoteObject> &callerToken, int64_t requestCode)
1339 {
1340     HILOG_INFO("formId:%{public}" PRId64, formId);
1341     int32_t errCode = Connect();
1342     if (errCode != ERR_OK) {
1343         return errCode;
1344     }
1345     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1346     if (remoteProxy_ == nullptr) {
1347         HILOG_ERROR("null remoteProxy_");
1348         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1349     }
1350     return remoteProxy_->ShareForm(formId, remoteDeviceId, callerToken, requestCode);
1351 }
1352 
AcquireFormData(int64_t formId,int64_t requestCode,const sptr<IRemoteObject> & callerToken,AAFwk::WantParams & formData)1353 int32_t FormMgr::AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
1354     AAFwk::WantParams &formData)
1355 {
1356     HILOG_INFO("formId:%{public}" PRId64, formId);
1357     int32_t errCode = Connect();
1358     if (errCode != ERR_OK) {
1359         return errCode;
1360     }
1361     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1362     if (remoteProxy_ == nullptr) {
1363         HILOG_ERROR("null remoteProxy_");
1364         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1365     }
1366     return remoteProxy_->AcquireFormData(formId, requestCode, callerToken, formData);
1367 }
1368 
CheckFMSReady()1369 bool FormMgr::CheckFMSReady()
1370 {
1371     HILOG_DEBUG("call");
1372     int32_t errCode = Connect();
1373     if (errCode != ERR_OK) {
1374         return false;
1375     }
1376     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1377     if (remoteProxy_ == nullptr) {
1378         HILOG_ERROR("null remoteProxy_");
1379         return false;
1380     }
1381     bool resultCode = remoteProxy_->CheckFMSReady();
1382     if (resultCode == false) {
1383         HILOG_ERROR("CheckFMSReady failed");
1384     }
1385     return resultCode;
1386 }
1387 
IsSystemAppForm(const std::string & bundleName)1388 bool FormMgr::IsSystemAppForm(const std::string &bundleName)
1389 {
1390     HILOG_DEBUG("check %{public}s is system form", bundleName.c_str());
1391     ErrCode errCode = Connect();
1392     if (errCode != ERR_OK) {
1393         HILOG_ERROR("errCode:%{public}d", errCode);
1394         return false;
1395     }
1396     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1397     if (remoteProxy_ == nullptr) {
1398         HILOG_ERROR("null remoteProxy_");
1399         return false;
1400     }
1401     return remoteProxy_->IsSystemAppForm(bundleName);
1402 }
1403 
RegisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1404 int32_t FormMgr::RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1405 {
1406     HILOG_DEBUG("call");
1407     int32_t errCode = Connect();
1408     if (errCode != ERR_OK) {
1409         HILOG_ERROR("register publish form failed, errCode:%{public}d", errCode);
1410         return errCode;
1411     }
1412     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1413     if (remoteProxy_ == nullptr) {
1414         HILOG_ERROR("null remoteProxy_");
1415         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1416     }
1417     return remoteProxy_->RegisterPublishFormInterceptor(interceptorCallback);
1418 }
1419 
UnregisterPublishFormInterceptor(const sptr<IRemoteObject> & interceptorCallback)1420 int32_t FormMgr::UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback)
1421 {
1422     HILOG_DEBUG("call");
1423     int32_t errCode = Connect();
1424     if (errCode != ERR_OK) {
1425         return errCode;
1426     }
1427     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1428     if (remoteProxy_ == nullptr) {
1429         HILOG_ERROR("null remoteProxy_");
1430         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1431     }
1432     return remoteProxy_->UnregisterPublishFormInterceptor(interceptorCallback);
1433 }
1434 
GetExternalError(int32_t innerErrorCode,int32_t & externalErrorCode,std::string & errorMsg)1435 void FormMgr::GetExternalError(int32_t innerErrorCode, int32_t &externalErrorCode, std::string &errorMsg)
1436 {
1437     externalErrorCode = FormErrors::GetInstance().QueryExternalErrorCode(innerErrorCode);
1438     errorMsg = FormErrors::GetInstance().QueryExternalErrorMessage(innerErrorCode, externalErrorCode);
1439     HILOG_DEBUG("innerErrorCode:%{public}d, externalErrorCode:%{public}d, errorMsg:%{public}s",
1440         innerErrorCode, externalErrorCode, errorMsg.c_str());
1441 }
1442 
GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)1443 std::string FormMgr::GetErrorMsgByExternalErrorCode(int32_t externalErrorCode)
1444 {
1445     return FormErrors::GetInstance().GetErrorMsgByExternalErrorCode(externalErrorCode);
1446 }
1447 
GetRunningFormInfos(bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1448 ErrCode FormMgr::GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1449 {
1450     HILOG_INFO("isUnusedIncluded is %{public}d", isUnusedIncluded);
1451     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1452         HILOG_ERROR("form is in recover status,can't do action on form");
1453         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1454     }
1455 
1456     ErrCode errCode = Connect();
1457     if (errCode != ERR_OK) {
1458         return errCode;
1459     }
1460     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1461     if (remoteProxy_ == nullptr) {
1462         HILOG_ERROR("null remoteProxy_");
1463         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1464     }
1465     ErrCode resultCode = remoteProxy_->GetRunningFormInfos(isUnusedIncluded, runningFormInfos);
1466     if (resultCode != ERR_OK) {
1467         HILOG_ERROR("fail GetRunningFormInfos,errCode %{public}d", resultCode);
1468     }
1469     return resultCode;
1470 }
1471 
GetRunningFormInfosByBundleName(const std::string & bundleName,bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)1472 ErrCode FormMgr::GetRunningFormInfosByBundleName(
1473     const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
1474 {
1475     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1476     if (bundleName.empty()) {
1477         HILOG_WARN("fail Get running form infos,because empty bundleName");
1478         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1479     }
1480     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1481         HILOG_ERROR("form is in recover status, can't do action on form");
1482         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1483     }
1484 
1485     ErrCode errCode = Connect();
1486     if (errCode != ERR_OK) {
1487         return errCode;
1488     }
1489 
1490     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1491     if (remoteProxy_ == nullptr) {
1492         HILOG_ERROR("null remoteProxy_");
1493         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1494     }
1495     ErrCode resultCode = remoteProxy_->GetRunningFormInfosByBundleName(
1496         bundleName, isUnusedIncluded, runningFormInfos);
1497     if (resultCode != ERR_OK) {
1498         HILOG_ERROR("fail GetRunningFormInfosByBundleName,errCode %{public}d", resultCode);
1499     }
1500     return resultCode;
1501 }
1502 
RegisterFormAddObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1503 ErrCode FormMgr::RegisterFormAddObserverByBundle(const std::string bundleName,
1504     const sptr<IRemoteObject> &callerToken)
1505 {
1506     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1507     if (FormMgr::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     int errCode = Connect();
1512     if (errCode != ERR_OK) {
1513         return errCode;
1514     }
1515     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1516     if (remoteProxy_ == nullptr) {
1517         HILOG_ERROR("null remoteProxy_");
1518         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1519     }
1520     return remoteProxy_->RegisterFormAddObserverByBundle(bundleName, callerToken);
1521 }
1522 
RegisterFormRemoveObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)1523 ErrCode FormMgr::RegisterFormRemoveObserverByBundle(const std::string bundleName,
1524     const sptr<IRemoteObject> &callerToken)
1525 {
1526     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
1527     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1528         HILOG_ERROR("form is in recover status,can't do action on form");
1529         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1530     }
1531     int errCode = Connect();
1532     if (errCode != ERR_OK) {
1533         return errCode;
1534     }
1535     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1536     if (remoteProxy_ == nullptr) {
1537         HILOG_ERROR("null remoteProxy_");
1538         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1539     }
1540     return remoteProxy_->RegisterFormRemoveObserverByBundle(bundleName, callerToken);
1541 }
1542 
GetFormsCount(bool isTempFormFlag,int32_t & formCount)1543 int32_t FormMgr::GetFormsCount(bool isTempFormFlag, int32_t &formCount)
1544 {
1545     HILOG_INFO("isTempFormFlag is %{public}d", isTempFormFlag);
1546 
1547     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1548         HILOG_ERROR("form is in recover status, can't do action on form");
1549         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1550     }
1551 
1552     int32_t errCode = Connect();
1553     if (errCode != ERR_OK) {
1554         return errCode;
1555     }
1556     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1557     if (remoteProxy_ == nullptr) {
1558         HILOG_ERROR("null remoteProxy_");
1559         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1560     }
1561     int32_t resultCode = remoteProxy_->GetFormsCount(isTempFormFlag, formCount);
1562     if (resultCode != ERR_OK) {
1563         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1564     }
1565     return resultCode;
1566 }
1567 
GetHostFormsCount(std::string & bundleName,int32_t & formCount)1568 int32_t FormMgr::GetHostFormsCount(std::string &bundleName, int32_t &formCount)
1569 {
1570     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1571 
1572     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1573         HILOG_ERROR("form is in recover status, can't do action on form");
1574         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1575     }
1576 
1577     if (bundleName.empty()) {
1578         HILOG_WARN("fail Get host forms count,because empty bundleName");
1579         return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME;
1580     }
1581 
1582     int32_t errCode = Connect();
1583     if (errCode != ERR_OK) {
1584         return errCode;
1585     }
1586     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1587     if (remoteProxy_ == nullptr) {
1588         HILOG_ERROR("null remoteProxy_");
1589         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1590     }
1591     int32_t resultCode = remoteProxy_->GetHostFormsCount(bundleName, formCount);
1592     if (resultCode != ERR_OK) {
1593         HILOG_ERROR("fail GetFormsCount,errCode %{public}d", resultCode);
1594     }
1595     return resultCode;
1596 }
1597 
GetFormInstancesByFilter(const FormInstancesFilter & formInstancesFilter,std::vector<FormInstance> & formInstances)1598 ErrCode FormMgr::GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter,
1599     std::vector<FormInstance> &formInstances)
1600 {
1601     HILOG_DEBUG("call");
1602     auto errCode = Connect();
1603     if (errCode != ERR_OK) {
1604         return errCode;
1605     }
1606     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1607     if (remoteProxy_ == nullptr) {
1608         HILOG_ERROR("null remoteProxy_");
1609         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1610     }
1611     return remoteProxy_->GetFormInstancesByFilter(formInstancesFilter, formInstances);
1612 }
1613 
GetFormInstanceById(const int64_t formId,FormInstance & formInstance)1614 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, FormInstance &formInstance)
1615 {
1616     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1617     auto errCode = Connect();
1618     if (errCode != ERR_OK) {
1619         return errCode;
1620     }
1621     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1622     if (remoteProxy_ == nullptr) {
1623         HILOG_ERROR("null remoteProxy_");
1624         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1625     }
1626     return remoteProxy_->GetFormInstanceById(formId, formInstance);
1627 }
1628 
GetFormInstanceById(const int64_t formId,bool isUnusedIncluded,FormInstance & formInstance)1629 ErrCode FormMgr::GetFormInstanceById(const int64_t formId, bool isUnusedIncluded, FormInstance &formInstance)
1630 {
1631     HILOG_DEBUG("formId is %{public}" PRId64, formId);
1632     auto errCode = Connect();
1633     if (errCode != ERR_OK) {
1634         return errCode;
1635     }
1636     if (remoteProxy_ == nullptr) {
1637         HILOG_ERROR("null remoteProxy_");
1638         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1639     }
1640     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1641     if (remoteProxy_ == nullptr) {
1642         HILOG_ERROR("null remoteProxy_");
1643         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1644     }
1645     return remoteProxy_->GetFormInstanceById(formId, isUnusedIncluded, formInstance);
1646 }
1647 
RegisterAddObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1648 ErrCode FormMgr::RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1649 {
1650     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1651     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1652         HILOG_ERROR("form is in recover status,can't do action on form");
1653         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1654     }
1655     auto errCode = Connect();
1656     if (errCode != ERR_OK) {
1657         return errCode;
1658     }
1659     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1660     if (remoteProxy_ == nullptr) {
1661         HILOG_ERROR("null remoteProxy_");
1662         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1663     }
1664     return remoteProxy_->RegisterAddObserver(bundleName, callerToken);
1665 }
1666 
RegisterRemoveObserver(const std::string & bundleName,const sptr<IRemoteObject> & callerToken)1667 ErrCode FormMgr::RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken)
1668 {
1669     HILOG_DEBUG("bundleName is %{public}s", bundleName.c_str());
1670     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1671         HILOG_ERROR("form is in recover status,can't do action on form");
1672         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1673     }
1674     auto errCode = Connect();
1675     if (errCode != ERR_OK) {
1676         return errCode;
1677     }
1678     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1679     if (remoteProxy_ == nullptr) {
1680         HILOG_ERROR("null remoteProxy_");
1681         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1682     }
1683     return remoteProxy_->RegisterRemoveObserver(bundleName, callerToken);
1684 }
1685 
RegisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1686 ErrCode FormMgr::RegisterClickEventObserver(
1687     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1688 {
1689     HILOG_DEBUG("call");
1690     if (observer == nullptr) {
1691         HILOG_ERROR("empty callerTokenParameter");
1692         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1693     }
1694     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1695         HILOG_ERROR("form is in recover status,can't do action on form");
1696         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1697     }
1698     int errCode = Connect();
1699     if (errCode != ERR_OK) {
1700         HILOG_ERROR("errCode:%{public}d", errCode);
1701         return errCode;
1702     }
1703     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1704     if (remoteProxy_ == nullptr) {
1705         HILOG_ERROR("null remoteProxy_");
1706         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1707     }
1708     return remoteProxy_->RegisterClickEventObserver(bundleName, formEventType, observer);
1709 }
1710 
UnregisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)1711 ErrCode FormMgr::UnregisterClickEventObserver(
1712     const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer)
1713 {
1714     HILOG_DEBUG("call");
1715     if (observer == nullptr) {
1716         HILOG_ERROR("empty callerTokenParameter");
1717         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1718     }
1719     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1720         HILOG_ERROR("form is in recover status,can't do action on form");
1721         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1722     }
1723     int errCode = Connect();
1724     if (errCode != ERR_OK) {
1725         HILOG_ERROR("errCode:%{public}d", errCode);
1726         return errCode;
1727     }
1728     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1729     if (remoteProxy_ == nullptr) {
1730         HILOG_ERROR("null remoteProxy_");
1731         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1732     }
1733     return remoteProxy_->UnregisterClickEventObserver(bundleName, formEventType, observer);
1734 }
1735 
RegisterFormRouterProxy(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken)1736 int FormMgr::RegisterFormRouterProxy(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken)
1737 {
1738 #ifndef WATCH_API_DISABLE
1739     HILOG_DEBUG("call");
1740     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1741         HILOG_ERROR("form is in recover status, can't do action on form");
1742         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1743     }
1744     auto errCode = Connect();
1745     if (errCode != ERR_OK) {
1746         HILOG_ERROR("errCode:%{public}d", errCode);
1747         return errCode;
1748     }
1749     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1750     if (remoteProxy_ == nullptr) {
1751         HILOG_ERROR("null remoteProxy_");
1752         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1753     }
1754     return remoteProxy_->RegisterFormRouterProxy(formIds, callerToken);
1755 #else
1756     return ERR_OK;
1757 #endif
1758 }
1759 
UnregisterFormRouterProxy(const std::vector<int64_t> & formIds)1760 int FormMgr::UnregisterFormRouterProxy(const std::vector<int64_t> &formIds)
1761 {
1762     HILOG_DEBUG("call");
1763     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1764         HILOG_ERROR("form is in recover status,can't do action on form");
1765         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1766     }
1767     auto errCode = Connect();
1768     if (errCode != ERR_OK) {
1769         HILOG_ERROR("errCode:%{public}d", errCode);
1770         return errCode;
1771     }
1772     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1773     if (remoteProxy_ == nullptr) {
1774         HILOG_ERROR("null remoteProxy_");
1775         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1776     }
1777     return remoteProxy_->UnregisterFormRouterProxy(formIds);
1778 }
1779 
SetFormsRecyclable(const std::vector<int64_t> & formIds)1780 int32_t FormMgr::SetFormsRecyclable(const std::vector<int64_t> &formIds)
1781 {
1782     HILOG_DEBUG("call");
1783     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1784         HILOG_ERROR("form is in recover status, can't do action on form");
1785         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1786     }
1787     if (formIds.empty()) {
1788         HILOG_ERROR("empty formIds");
1789         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1790     }
1791     auto errCode = Connect();
1792     if (errCode != ERR_OK) {
1793         HILOG_ERROR("errCode:%{public}d", errCode);
1794         return errCode;
1795     }
1796     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1797     if (remoteProxy_ == nullptr) {
1798         HILOG_ERROR("null remoteProxy_");
1799         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1800     }
1801     return remoteProxy_->SetFormsRecyclable(formIds);
1802 }
1803 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want)1804 int32_t FormMgr::RecycleForms(const std::vector<int64_t> &formIds, const Want &want)
1805 {
1806     HILOG_DEBUG("call");
1807     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1808         HILOG_ERROR("form is in recover status, can't do action on form");
1809         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1810     }
1811     if (formIds.empty()) {
1812         HILOG_ERROR("empty formIds");
1813         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1814     }
1815     auto errCode = Connect();
1816     if (errCode != ERR_OK) {
1817         HILOG_ERROR("errCode:%{public}d", errCode);
1818         return errCode;
1819     }
1820     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1821     if (remoteProxy_ == nullptr) {
1822         HILOG_ERROR("null remoteProxy_");
1823         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1824     }
1825     return remoteProxy_->RecycleForms(formIds, want);
1826 }
1827 
RecoverForms(const std::vector<int64_t> & formIds,const Want & want)1828 int32_t FormMgr::RecoverForms(const std::vector<int64_t> &formIds, const Want &want)
1829 {
1830     HILOG_DEBUG("call");
1831     if (GetRecoverStatus() == Constants::IN_RECOVERING) {
1832         HILOG_ERROR("form is in recover status, can't do action on form");
1833         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1834     }
1835     if (formIds.empty()) {
1836         HILOG_ERROR("empty formIds");
1837         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
1838     }
1839     auto errCode = Connect();
1840     if (errCode != ERR_OK) {
1841         HILOG_ERROR("errCode:%{public}d", errCode);
1842         return errCode;
1843     }
1844     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1845     if (remoteProxy_ == nullptr) {
1846         HILOG_ERROR("null remoteProxy_");
1847         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1848     }
1849     return remoteProxy_->RecoverForms(formIds, want);
1850 }
1851 
UpdateFormLocation(const int64_t & formId,const int32_t & formLocation)1852 ErrCode FormMgr::UpdateFormLocation(const int64_t &formId, const int32_t &formLocation)
1853 {
1854     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1855         HILOG_ERROR("form is in recover status, can't do action on form");
1856         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1857     }
1858 
1859     ErrCode errCode = Connect();
1860     if (errCode != ERR_OK) {
1861         return errCode;
1862     }
1863     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1864     if (remoteProxy_ == nullptr) {
1865         HILOG_ERROR("null remoteProxy_");
1866         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1867     }
1868     ErrCode resultCode = remoteProxy_->UpdateFormLocation(formId, formLocation);
1869     if (resultCode != ERR_OK) {
1870         HILOG_ERROR("fail UpdateFormLocation,errCode %{public}d", resultCode);
1871     }
1872     return resultCode;
1873 }
1874 
RequestPublishFormWithSnapshot(Want & want,bool withFormBindingData,std::unique_ptr<FormProviderData> & formBindingData,int64_t & formId,const std::vector<FormDataProxy> & formDataProxies)1875 ErrCode FormMgr::RequestPublishFormWithSnapshot(Want &want, bool withFormBindingData,
1876     std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId,
1877     const std::vector<FormDataProxy> &formDataProxies)
1878 {
1879     HILOG_INFO("call");
1880 #ifdef NO_RUNTIME_EMULATOR
1881     int64_t processorId = FormEventHiAppEvent::AddProcessor();
1882     HILOG_INFO("Add processor begin.Processor id is %{public}" PRId64, processorId);
1883     time_t beginTime = time(nullptr);
1884 #endif
1885     ErrCode errCode = Connect();
1886     if (errCode != ERR_OK) {
1887         HILOG_ERROR("errCode:%{public}d", errCode);
1888         return errCode;
1889     }
1890     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1891     if (remoteProxy_ == nullptr) {
1892         HILOG_ERROR("null remoteProxy_");
1893         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1894     }
1895     ErrCode ret = remoteProxy_->RequestPublishFormWithSnapshot(want, withFormBindingData,
1896         formBindingData, formId);
1897 #ifdef NO_RUNTIME_EMULATOR
1898     PublishFormData publishFormData = {
1899     want.GetElement().GetBundleName(),
1900     want.GetElement().GetAbilityName(),
1901     want.GetIntParam(Constants::PARAM_FORM_DIMENSION_KEY, -1),
1902     want.GetStringParam(Constants::PARAM_MODULE_NAME_KEY),
1903     want.GetStringParam(Constants::PARAM_FORM_NAME_KEY)};
1904     FormEventHiAppEvent::WriteAppFormEndEvent(ret, beginTime, "RequestPublishFormWithSnapshot",
1905         publishFormData, processorId);
1906 #endif
1907     return ret;
1908 }
1909 
BatchRefreshForms(const int32_t formRefreshType)1910 int32_t FormMgr::BatchRefreshForms(const int32_t formRefreshType)
1911 {
1912     HILOG_INFO("call");
1913     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1914         HILOG_ERROR("BatchRefreshForms failed, form is in recover status, can't do action on form");
1915         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1916     }
1917 
1918     if (formRefreshType < Constants::REFRESH_ALL_FORM || formRefreshType > Constants::REFRESH_ATOMIC_FORM) {
1919         HILOG_ERROR("BatchRefreshForms failed, invalid formRefreshType %{public}d", formRefreshType);
1920         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1921     }
1922 
1923     int errCode = Connect();
1924     if (errCode != ERR_OK) {
1925         HILOG_ERROR("errCode:%{public}d", errCode);
1926         return errCode;
1927     }
1928     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1929     if (remoteProxy_ == nullptr) {
1930         HILOG_ERROR("null remoteProxy_");
1931         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1932     }
1933     return remoteProxy_->BatchRefreshForms(formRefreshType);
1934 }
1935 
EnableForms(const std::string bundleName,const bool enable)1936 int32_t FormMgr::EnableForms(const std::string bundleName, const bool enable)
1937 {
1938     if (bundleName.empty()) {
1939         HILOG_ERROR("empty bundleName");
1940         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
1941     }
1942     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
1943         HILOG_ERROR("form is in recover status, can't do action on form");
1944         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
1945     }
1946 
1947     ErrCode errCode = Connect();
1948     if (errCode != ERR_OK) {
1949         return errCode;
1950     }
1951 
1952     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1953     if (remoteProxy_ == nullptr) {
1954         HILOG_ERROR("null remoteProxy_");
1955         return ERR_APPEXECFWK_FORM_COMMON_CODE;
1956     }
1957     ErrCode resultCode = remoteProxy_->EnableForms(bundleName, enable);
1958     if (resultCode != ERR_OK) {
1959         HILOG_ERROR("fail EnableForms,errCode %{public}d", resultCode);
1960     }
1961     return resultCode;
1962 }
1963 
IsFormBundleForbidden(const std::string & bundleName)1964 bool FormMgr::IsFormBundleForbidden(const std::string &bundleName)
1965 {
1966     ErrCode errCode = Connect();
1967     if (errCode != ERR_OK) {
1968         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1969         return false;
1970     }
1971 
1972     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1973     if (remoteProxy_ == nullptr) {
1974         HILOG_ERROR("null remoteProxy_");
1975         return false;
1976     }
1977     return remoteProxy_->IsFormBundleForbidden(bundleName);
1978 }
1979 
IsFormBundleProtected(const std::string & bundleName,int64_t formId)1980 bool FormMgr::IsFormBundleProtected(const std::string &bundleName, int64_t formId)
1981 {
1982     ErrCode errCode = Connect();
1983     if (errCode != ERR_OK) {
1984         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
1985         return false;
1986     }
1987 
1988     std::shared_lock<std::shared_mutex> lock(connectMutex_);
1989     if (remoteProxy_ == nullptr) {
1990         HILOG_ERROR("null remoteProxy_");
1991         return false;
1992     }
1993     return remoteProxy_->IsFormBundleProtected(bundleName, formId);
1994 }
1995 
IsFormBundleExempt(int64_t formId)1996 bool FormMgr::IsFormBundleExempt(int64_t formId)
1997 {
1998     ErrCode errCode = Connect();
1999     if (errCode != ERR_OK) {
2000         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2001         return false;
2002     }
2003 
2004     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2005     if (remoteProxy_ == nullptr) {
2006         HILOG_ERROR("null remoteProxy_");
2007         return false;
2008     }
2009     return remoteProxy_->IsFormBundleExempt(formId);
2010 }
2011 
UpdateFormSize(const int64_t formId,float width,float height,float borderWidth)2012 ErrCode FormMgr::UpdateFormSize(const int64_t formId, float width, float height, float borderWidth)
2013 {
2014     ErrCode errCode = Connect();
2015     if (errCode != ERR_OK) {
2016         return errCode;
2017     }
2018     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2019     if (remoteProxy_ == nullptr) {
2020         HILOG_ERROR("null remoteProxy_");
2021         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2022     }
2023     ErrCode resultCode = remoteProxy_->UpdateFormSize(formId, width, height, borderWidth);
2024     if (resultCode != ERR_OK) {
2025         HILOG_ERROR("fail UpdateFormSize,errCode %{public}d", resultCode);
2026     }
2027     return resultCode;
2028 }
2029 
NotifyFormLocked(const int64_t & formId,bool isLocked)2030 int32_t FormMgr::NotifyFormLocked(const int64_t &formId, bool isLocked)
2031 {
2032     HILOG_DEBUG("call");
2033     if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) {
2034         HILOG_ERROR("form is in recover status, can't do action on form");
2035         return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR;
2036     }
2037 
2038     int errCode = Connect();
2039     if (errCode != ERR_OK) {
2040         return errCode;
2041     }
2042     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2043     if (remoteProxy_ == nullptr) {
2044         HILOG_ERROR("null remoteProxy_");
2045         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2046     }
2047     int resultCode = remoteProxy_->NotifyFormLocked(formId, isLocked);
2048     if (resultCode != ERR_OK) {
2049         HILOG_ERROR("fail NotifyFormLocked, errCode %{public}d", resultCode);
2050     }
2051     return resultCode;
2052 }
2053 
OpenFormEditAbility(const std::string & abilityName,const int64_t & formId,bool isMainPage)2054 ErrCode FormMgr::OpenFormEditAbility(const std::string &abilityName, const int64_t &formId, bool isMainPage)
2055 {
2056     HILOG_INFO("abilityName: %{public}s, formId: %{public}" PRId64 ", isMainPage: %{public}s",
2057         abilityName.c_str(), formId, isMainPage ? "true" : "false");
2058     ErrCode resultCode = Connect();
2059     if (resultCode != ERR_OK) {
2060         return resultCode;
2061     }
2062     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2063     if (remoteProxy_ == nullptr) {
2064         HILOG_ERROR("null remoteProxy_");
2065         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2066     }
2067     resultCode = remoteProxy_->OpenFormEditAbility(abilityName, formId, isMainPage);
2068     if (resultCode != ERR_OK) {
2069         HILOG_ERROR("fail OpenFormEditAbility,errCode %{public}d", resultCode);
2070     }
2071     return resultCode;
2072 }
2073 
RegisterOverflowProxy(const sptr<IRemoteObject> & callerToken)2074 bool FormMgr::RegisterOverflowProxy(const sptr<IRemoteObject> &callerToken)
2075 {
2076     HILOG_INFO("Call");
2077     ErrCode errCode = Connect();
2078     if (errCode != ERR_OK) {
2079         HILOG_ERROR("Connect form mgr service failed, errCode %{public}d", errCode);
2080         return false;
2081     }
2082     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2083     if (remoteProxy_ == nullptr) {
2084         HILOG_ERROR("null remoteProxy_");
2085         return false;
2086     }
2087     return remoteProxy_->RegisterOverflowProxy(callerToken);
2088 }
2089 
UnregisterOverflowProxy()2090 bool FormMgr::UnregisterOverflowProxy()
2091 {
2092     HILOG_INFO("call");
2093     ErrCode errCode = Connect();
2094     if (errCode != ERR_OK) {
2095         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2096         return false;
2097     }
2098     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2099     if (remoteProxy_ == nullptr) {
2100         HILOG_ERROR("null remoteProxy_");
2101         return false;
2102     }
2103     return remoteProxy_->UnregisterOverflowProxy();
2104 }
2105 
RequestOverflow(const int64_t formId,const OverflowInfo & overflowInfo,bool isOverflow)2106 ErrCode FormMgr::RequestOverflow(const int64_t formId, const OverflowInfo &overflowInfo, bool isOverflow)
2107 {
2108     HILOG_INFO("Call");
2109     if (formId <= 0) {
2110         HILOG_ERROR("RequestOverflow failed, formId is invalid");
2111         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
2112     }
2113     if (isOverflow && overflowInfo.duration <= 0) {
2114         HILOG_ERROR("RequestOverflow failed, duration is invalid");
2115         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
2116     }
2117     ErrCode errCode = Connect();
2118     if (errCode != ERR_OK) {
2119         HILOG_ERROR("Connect form mgr service failed, errCode %{public}d", errCode);
2120         return errCode;
2121     }
2122 
2123     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2124     if (remoteProxy_ == nullptr) {
2125         HILOG_ERROR("remoteProxy_ is invalid");
2126         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2127     }
2128 
2129     ErrCode result = remoteProxy_->RequestOverflow(formId, overflowInfo, isOverflow);
2130     HILOG_INFO("RequestOverflow result: %{public}d", result);
2131     return result;
2132 }
2133 
RegisterChangeSceneAnimationStateProxy(const sptr<IRemoteObject> & callerToken)2134 bool FormMgr::RegisterChangeSceneAnimationStateProxy(const sptr<IRemoteObject> &callerToken)
2135 {
2136     HILOG_INFO("call");
2137     ErrCode errCode = Connect();
2138     if (errCode != ERR_OK) {
2139         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2140         return false;
2141     }
2142     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2143     if (remoteProxy_ == nullptr) {
2144         HILOG_ERROR("null remoteProxy_");
2145         return false;
2146     }
2147     return remoteProxy_->RegisterChangeSceneAnimationStateProxy(callerToken);
2148 }
2149 
UnregisterChangeSceneAnimationStateProxy()2150 bool FormMgr::UnregisterChangeSceneAnimationStateProxy()
2151 {
2152     HILOG_INFO("call");
2153     ErrCode errCode = Connect();
2154     if (errCode != ERR_OK) {
2155         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2156         return false;
2157     }
2158     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2159     if (remoteProxy_ == nullptr) {
2160         HILOG_ERROR("null remoteProxy_");
2161         return false;
2162     }
2163     return remoteProxy_->UnregisterChangeSceneAnimationStateProxy();
2164 }
2165 
ChangeSceneAnimationState(const int64_t formId,int32_t state)2166 ErrCode FormMgr::ChangeSceneAnimationState(const int64_t formId, int32_t state)
2167 {
2168     HILOG_INFO("call");
2169     if (formId <= 0) {
2170         HILOG_ERROR("RequestOverflow failed, formId is invalid");
2171         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
2172     }
2173     ErrCode errCode = Connect();
2174     if (errCode != ERR_OK) {
2175         HILOG_ERROR("connect form mgr service failed, errCode %{public}d", errCode);
2176         return errCode;
2177     }
2178 
2179     // check remoteProxy_
2180     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2181     if (remoteProxy_ == nullptr) {
2182         HILOG_ERROR("null remoteProxy_");
2183         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2184     }
2185 
2186     ErrCode result = remoteProxy_->ChangeSceneAnimationState(formId, state);
2187     HILOG_INFO("ChangeSceneAnimationState result: %{public}d", result);
2188     return result;
2189 }
2190 
RegisterGetFormRectProxy(const sptr<IRemoteObject> & callerToken)2191 bool FormMgr::RegisterGetFormRectProxy(const sptr<IRemoteObject> &callerToken)
2192 {
2193     ErrCode errCode = Connect();
2194     if (errCode != ERR_OK) {
2195         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2196         return false;
2197     }
2198     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2199     if (remoteProxy_ == nullptr) {
2200         HILOG_ERROR("null remoteProxy_");
2201         return false;
2202     }
2203     return remoteProxy_->RegisterGetFormRectProxy(callerToken);
2204 }
2205 
UnregisterGetFormRectProxy()2206 bool FormMgr::UnregisterGetFormRectProxy()
2207 {
2208     HILOG_INFO("call");
2209     ErrCode errCode = Connect();
2210     if (errCode != ERR_OK) {
2211         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2212         return false;
2213     }
2214     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2215     if (remoteProxy_ == nullptr) {
2216         HILOG_ERROR("null remoteProxy_");
2217         return false;
2218     }
2219     return remoteProxy_->UnregisterGetFormRectProxy();
2220 }
2221 
GetFormRect(const int64_t formId,Rect & rect)2222 ErrCode FormMgr::GetFormRect(const int64_t formId, Rect &rect)
2223 {
2224     if (formId <= 0) {
2225         HILOG_ERROR("empty formId");
2226         return ERR_APPEXECFWK_FORM_INVALID_FORM_ID;
2227     }
2228     int errCode = Connect();
2229     if (errCode != ERR_OK) {
2230         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2231         return errCode;
2232     }
2233     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2234     if (remoteProxy_ == nullptr) {
2235         HILOG_ERROR("null remoteProxy_");
2236         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2237     }
2238     return remoteProxy_->GetFormRect(formId, rect);
2239 }
2240 
UpdateFormSize(const int64_t formId,const int32_t newDimension,const Rect & newRect)2241 ErrCode FormMgr::UpdateFormSize(const int64_t formId, const int32_t newDimension, const Rect &newRect)
2242 {
2243     HILOG_INFO("UpdateFormSize formid:%{public}" PRId64, formId);
2244     if (formId <= 0) {
2245         HILOG_ERROR("UpdateFormSize failed, formId is invalid");
2246         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
2247     }
2248     ErrCode resultCode = Connect();
2249     if (resultCode != ERR_OK) {
2250         HILOG_ERROR("connect form mgr service failed, errCode %{public}d", resultCode);
2251         return resultCode;
2252     }
2253     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2254     if (remoteProxy_ == nullptr) {
2255         HILOG_ERROR("null remoteProxy_");
2256         return ERR_APPEXECFWK_FORM_COMMON_CODE;
2257     }
2258     resultCode = remoteProxy_->UpdateFormSize(formId, newDimension, newRect);
2259     if (resultCode != ERR_OK) {
2260         HILOG_ERROR("fail UpdateFormSize,errCode %{public}d", resultCode);
2261     }
2262     return resultCode;
2263 }
2264 
RegisterGetLiveFormStatusProxy(const sptr<IRemoteObject> & callerToken)2265 bool FormMgr::RegisterGetLiveFormStatusProxy(const sptr<IRemoteObject> &callerToken)
2266 {
2267     ErrCode errCode = Connect();
2268     if (errCode != ERR_OK) {
2269         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2270         return false;
2271     }
2272     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2273     if (remoteProxy_ == nullptr) {
2274         HILOG_ERROR("null remoteProxy_");
2275         return false;
2276     }
2277     return remoteProxy_->RegisterGetLiveFormStatusProxy(callerToken);
2278 }
2279 
UnregisterGetLiveFormStatusProxy()2280 bool FormMgr::UnregisterGetLiveFormStatusProxy()
2281 {
2282     HILOG_INFO("call");
2283     ErrCode errCode = Connect();
2284     if (errCode != ERR_OK) {
2285         HILOG_ERROR("connect form mgr service failed,errCode %{public}d", errCode);
2286         return false;
2287     }
2288     std::shared_lock<std::shared_mutex> lock(connectMutex_);
2289     if (remoteProxy_ == nullptr) {
2290         HILOG_ERROR("null remoteProxy_");
2291         return false;
2292     }
2293     return remoteProxy_->UnregisterGetLiveFormStatusProxy();
2294 }
2295 }  // namespace AppExecFwk
2296 }  // namespace OHOS
2297