1 /*
2 * Copyright (c) 2025 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 "cj_form_provider.h"
17
18 #include <cinttypes>
19 #include <vector>
20
21 #include "cj_form_util.h"
22 #include "fms_log_wrapper.h"
23 #include "form_constants.h"
24 #include "form_mgr.h"
25 #include "form_mgr_errors.h"
26 #include "ipc_skeleton.h"
27 #include "napi_form_util.h"
28 #include "running_form_info.h"
29 #include "runtime.h"
30 #include "tokenid_kit.h"
31
32 namespace OHOS {
33 namespace AbilityRuntime {
34 using namespace OHOS;
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37
38 const size_t MAX_SIZE = 1024;
39
OnGetFormsInfo(const CFormFilter & cfilter,CArrCFormInfo * cArrformInfo)40 int32_t OnGetFormsInfo(const CFormFilter& cfilter, CArrCFormInfo* cArrformInfo)
41 {
42 FormInfoFilter formInfoFilter;
43 formInfoFilter.moduleName = std::string(cfilter.moduleName);
44 std::vector<FormInfo> formInfos;
45 auto ret = FormMgr::GetInstance().GetFormsInfo(formInfoFilter, formInfos);
46 if (ret != ERR_OK) {
47 return CreateErrorByInternalErrorCode(ret);
48 }
49 if (formInfos.size() > MAX_SIZE) {
50 return ERR_FORM_EXTERNAL_FORM_NUM_EXCEEDS_UPPER_BOUND;
51 }
52 CFormInfo* head = static_cast<CFormInfo*>(malloc(sizeof(CFormInfo) * formInfos.size()));
53 if (head == nullptr) {
54 return ERR_FORM_EXTERNAL_KERNEL_MALLOC_ERROR;
55 }
56 uint32_t index = 0;
57 for (const auto& formInfo : formInfos) {
58 head[index] = ConvertFormInfo2CFormInfo(formInfo);
59 index++;
60 }
61 cArrformInfo->head = head;
62 cArrformInfo->size = static_cast<int64_t>(formInfos.size());
63 return ret;
64 }
65
OnSetFormNextRefreshTime(char * cFormId,int32_t time)66 int32_t OnSetFormNextRefreshTime(char* cFormId, int32_t time)
67 {
68 HILOG_DEBUG("call");
69 if (cFormId == nullptr) {
70 HILOG_ERROR("input param is nullptr");
71 return ERR_FORM_EXTERNAL_PARAM_INVALID;
72 }
73 int64_t formId = 0;
74 std::string strFormId = std::string(cFormId);
75 if (!ConvertStringToInt64(strFormId, formId)) {
76 HILOG_ERROR("convert form string failed");
77 return ERR_FORM_EXTERNAL_PARAM_INVALID;
78 }
79 int32_t ret = FormMgr::GetInstance().SetNextRefreshTime(formId, time);
80 if (ret != ERR_OK) {
81 HILOG_ERROR("SetNextRefreshTime failed");
82 return CreateErrorByInternalErrorCode(ret);
83 }
84 return ret;
85 }
86
OnUpdateForm(char * cFormId,const CFormBindingData & cFormBindingData)87 int32_t OnUpdateForm(char* cFormId, const CFormBindingData& cFormBindingData)
88 {
89 HILOG_DEBUG("call");
90 if (cFormId == nullptr) {
91 HILOG_ERROR("input param is nullptr");
92 return ERR_FORM_EXTERNAL_PARAM_INVALID;
93 }
94 int64_t formId = 0;
95 std::string strFormId = std::string(cFormId);
96 if (!ConvertStringToInt64(strFormId, formId)) {
97 HILOG_ERROR("convert form string failed");
98 return ERR_FORM_EXTERNAL_PARAM_INVALID;
99 }
100 auto formProviderData = std::make_shared<OHOS::AppExecFwk::FormProviderData>();
101 std::string formDataStr = std::string(cFormBindingData.data);
102 formProviderData->SetDataString(formDataStr);
103 formProviderData->ParseImagesData();
104
105 std::vector<AppExecFwk::FormDataProxy> formDataProxies;
106 CArrProxyData nativeProxies = cFormBindingData.cArrProxyData;
107 ConvertFromDataProxies(nativeProxies, formDataProxies);
108 int32_t ret = FormMgr::GetInstance().UpdateForm(formId, *formProviderData, formDataProxies);
109 if (ret != ERR_OK) {
110 return CreateErrorByInternalErrorCode(ret);
111 }
112 return ret;
113 }
114
FreeCArrCFormInfo(CArrCFormInfo * cArrformInfo)115 void FreeCArrCFormInfo(CArrCFormInfo* cArrformInfo)
116 {
117 if (cArrformInfo == nullptr || cArrformInfo->head == nullptr) {
118 return;
119 }
120 for (int64_t i = 0; i < cArrformInfo->size; i++) {
121 FreeCFormInfo((cArrformInfo->head)[i]);
122 }
123 free(cArrformInfo->head);
124 cArrformInfo->head = nullptr;
125 }
126
127 extern "C" {
FFIFormProviderGetFormsInfo(CFormFilter cfilter,CArrCFormInfo * cArrformInfo)128 CJ_EXPORT int32_t FFIFormProviderGetFormsInfo(CFormFilter cfilter, CArrCFormInfo* cArrformInfo)
129 {
130 if (cfilter.moduleName == nullptr || cArrformInfo == nullptr) {
131 HILOG_ERROR("input param is nullptr");
132 return ERR_FORM_EXTERNAL_PARAM_INVALID;
133 }
134 return OnGetFormsInfo(cfilter, cArrformInfo);
135 }
136
FFIFormProviderFreeCArrCFormInfo(CArrCFormInfo * cArrformInfo)137 CJ_EXPORT void FFIFormProviderFreeCArrCFormInfo(CArrCFormInfo* cArrformInfo)
138 {
139 if (cArrformInfo == nullptr) {
140 HILOG_ERROR("input param is nullptr");
141 return;
142 }
143 FreeCArrCFormInfo(cArrformInfo);
144 }
145
FFIFormProviderSetFormNextRefreshTime(char * cFormId,int32_t time)146 CJ_EXPORT int32_t FFIFormProviderSetFormNextRefreshTime(char* cFormId, int32_t time)
147 {
148 if (cFormId == nullptr) {
149 HILOG_ERROR("input param is nullptr");
150 return ERR_FORM_EXTERNAL_PARAM_INVALID;
151 }
152 return OnSetFormNextRefreshTime(cFormId, time);
153 }
154
FFIFormProviderUpdateForm(char * cFormId,CFormBindingData cFormBindingData)155 CJ_EXPORT int32_t FFIFormProviderUpdateForm(char* cFormId, CFormBindingData cFormBindingData)
156 {
157 if (cFormId == nullptr) {
158 HILOG_ERROR("input param is nullptr");
159 return ERR_FORM_EXTERNAL_PARAM_INVALID;
160 }
161 return OnUpdateForm(cFormId, cFormBindingData);
162 }
163
164 } // extern "C"
165
166 } // namespace AbilityRuntime
167 } // namespace OHOS