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_util.h"
17
18 #include <cinttypes>
19 #include <vector>
20
21 #include "fms_log_wrapper.h"
22 #include "form_constants.h"
23 #include "form_mgr.h"
24 #include "form_mgr_errors.h"
25 #include "ipc_skeleton.h"
26 #include "running_form_info.h"
27 #include "runtime.h"
28 #include "tokenid_kit.h"
29
30 namespace OHOS {
31 namespace AbilityRuntime {
32 using namespace OHOS;
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35
CreateErrorByInternalErrorCode(int32_t internalErrorCode)36 int32_t CreateErrorByInternalErrorCode(int32_t internalErrorCode)
37 {
38 int32_t externalErrorCode = 0;
39 std::string externalErrorMessage;
40 FormMgr::GetInstance().GetExternalError(internalErrorCode, externalErrorCode, externalErrorMessage);
41 return externalErrorCode;
42 }
43
CreateCStringFromString(const std::string & source)44 char* CreateCStringFromString(const std::string& source)
45 {
46 if (source.size() == 0) {
47 return nullptr;
48 }
49 size_t length = source.size() + 1;
50 auto res = static_cast<char*>(malloc(length));
51 if (res == nullptr) {
52 HILOG_ERROR("null res");
53 return nullptr;
54 }
55 if (strcpy_s(res, length, source.c_str()) != 0) {
56 free(res);
57 HILOG_ERROR("Strcpy failed");
58 return nullptr;
59 }
60 return res;
61 }
62
GetFormType(const FormInfo & formInfo)63 int32_t GetFormType(const FormInfo& formInfo)
64 {
65 if (formInfo.uiSyntax == FormType::ETS) {
66 return static_cast<int32_t>(FormType::ETS);
67 }
68 return static_cast<int32_t>(formInfo.type);
69 }
70
ConvertArrI32(const std::vector<int32_t> & infos)71 CArrI32 ConvertArrI32(const std::vector<int32_t>& infos)
72 {
73 CArrI32 ret = { .head = nullptr, .size = 0 };
74 int32_t* head = static_cast<int32_t*>(malloc(sizeof(int32_t) * infos.size()));
75 if (head == nullptr) {
76 return ret;
77 }
78 uint32_t index = 0;
79 for (const auto& info : infos) {
80 head[index] = info;
81 index++;
82 }
83 ret.head = head;
84 ret.size = static_cast<int64_t>(infos.size());
85 return ret;
86 }
87
ConvertArrUI32(const std::vector<uint32_t> & infos)88 CArrUI32 ConvertArrUI32(const std::vector<uint32_t>& infos)
89 {
90 CArrUI32 ret = { .head = nullptr, .size = 0 };
91 uint32_t* head = static_cast<uint32_t*>(malloc(sizeof(uint32_t) * infos.size()));
92 if (head == nullptr) {
93 return ret;
94 }
95 uint32_t index = 0;
96 for (const auto& info : infos) {
97 head[index] = info;
98 index++;
99 }
100 ret.head = head;
101 ret.size = static_cast<int64_t>(infos.size());
102 return ret;
103 }
104
ConvertArrFormCustomizeData(const std::vector<FormCustomizeData> & infos)105 CArrCFormCustomizeData ConvertArrFormCustomizeData(const std::vector<FormCustomizeData>& infos)
106 {
107 CArrCFormCustomizeData ret = { .head = nullptr, .size = 0 };
108 CFormCustomizeData* head = static_cast<CFormCustomizeData*>(malloc(sizeof(CFormCustomizeData) * infos.size()));
109 if (head == nullptr) {
110 return ret;
111 }
112 uint32_t index = 0;
113 for (const auto& info : infos) {
114 head[index].name = CreateCStringFromString(info.name);
115 head[index].value = CreateCStringFromString(info.value);
116 index++;
117 }
118 ret.head = head;
119 ret.size = static_cast<int64_t>(infos.size());
120 return ret;
121 }
122
FreeCArrFormCustomizeData(CArrCFormCustomizeData & infos)123 void FreeCArrFormCustomizeData(CArrCFormCustomizeData& infos)
124 {
125 if (infos.head == nullptr) {
126 return;
127 }
128 for (int64_t i = 0; i < infos.size; i++) {
129 free(infos.head[i].name);
130 infos.head[i].name = nullptr;
131 free(infos.head[i].value);
132 infos.head[i].value = nullptr;
133 }
134 free(infos.head);
135 infos.head = nullptr;
136 }
137
FreeCFormInfo(CFormInfo & formInfo)138 void FreeCFormInfo(CFormInfo& formInfo)
139 {
140 free(formInfo.bundleName);
141 formInfo.bundleName = nullptr;
142 free(formInfo.moduleName);
143 formInfo.moduleName = nullptr;
144 free(formInfo.abilityName);
145 formInfo.abilityName = nullptr;
146 free(formInfo.name);
147 formInfo.name = nullptr;
148 free(formInfo.displayName);
149 formInfo.displayName = nullptr;
150 free(formInfo.description);
151 formInfo.description = nullptr;
152 free(formInfo.jsComponentName);
153 formInfo.jsComponentName = nullptr;
154 free(formInfo.scheduledUpdateTime);
155 formInfo.scheduledUpdateTime = nullptr;
156 free(formInfo.formConfigAbility);
157 formInfo.formConfigAbility = nullptr;
158 free(formInfo.supportDimensions.head);
159 formInfo.supportDimensions.head = nullptr;
160 FreeCArrFormCustomizeData(formInfo.customizeData);
161 free(formInfo.supportedShapes.head);
162 formInfo.supportedShapes.head = nullptr;
163 free(formInfo.previewImages.head);
164 formInfo.previewImages.head = nullptr;
165 }
166
ConvertFormInfo2CFormInfo(const FormInfo & formInfo)167 CFormInfo ConvertFormInfo2CFormInfo(const FormInfo& formInfo)
168 {
169 CFormInfo ret = {};
170 ret.bundleName = CreateCStringFromString(formInfo.bundleName);
171 ret.moduleName = CreateCStringFromString(formInfo.moduleName);
172 ret.abilityName = CreateCStringFromString(formInfo.abilityName);
173 ret.name = CreateCStringFromString(formInfo.name);
174 ret.displayName = CreateCStringFromString(formInfo.displayName);
175 ret.displayNameId = formInfo.displayNameId;
176 ret.description = CreateCStringFromString(formInfo.description);
177 ret.descriptionId = formInfo.descriptionId;
178 ret.type = GetFormType(formInfo);
179 ret.jsComponentName = CreateCStringFromString(formInfo.jsComponentName);
180 ret.colorMode = static_cast<int32_t>(formInfo.colorMode);
181 ret.isDefault = formInfo.defaultFlag;
182 ret.updateEnabled = formInfo.updateEnabled;
183 ret.formVisibleNotify = formInfo.formVisibleNotify;
184 ret.scheduledUpdateTime = CreateCStringFromString(formInfo.scheduledUpdateTime);
185 ret.formConfigAbility = CreateCStringFromString(formInfo.formConfigAbility);
186 ret.updateDuration = formInfo.updateDuration;
187 ret.defaultDimension = formInfo.defaultDimension;
188 ret.supportDimensions = ConvertArrI32(formInfo.supportDimensions);
189 ret.customizeData = ConvertArrFormCustomizeData(formInfo.customizeDatas);
190 ret.isDynamic = formInfo.isDynamic;
191 ret.transparencyEnabled = formInfo.transparencyEnabled;
192 ret.supportedShapes = ConvertArrI32(formInfo.supportShapes);
193 ret.previewImages = ConvertArrUI32(formInfo.formPreviewImages);
194 ret.enableBlurBackground = formInfo.enableBlurBackground;
195 ret.renderingMode = static_cast<int32_t>(formInfo.renderingMode);
196 return ret;
197 }
198
ConvertFromDataProxies(const CArrProxyData & cArrProxyData,std::vector<FormDataProxy> & formDataProxies)199 bool ConvertFromDataProxies(const CArrProxyData& cArrProxyData, std::vector<FormDataProxy>& formDataProxies)
200 {
201 if (cArrProxyData.head == nullptr) {
202 HILOG_ERROR("null head");
203 return false;
204 }
205 int32_t len = cArrProxyData.size;
206 for (int32_t i = 0; i < len; i++) {
207 FormDataProxy formDataProxy("", "");
208 CProxyData element = cArrProxyData.head[i];
209 if (!ConvertFormDataProxy(element, formDataProxy)) {
210 HILOG_ERROR("GetElement [%{public}u] error", i);
211 continue;
212 }
213 formDataProxies.push_back(formDataProxy);
214 }
215 return true;
216 }
217
ConvertFormDataProxy(const CProxyData & cProxyData,FormDataProxy & formDataProxy)218 bool ConvertFormDataProxy(const CProxyData& cProxyData, FormDataProxy& formDataProxy)
219 {
220 if (cProxyData.key == nullptr || cProxyData.subscribeId == nullptr) {
221 return false;
222 }
223 formDataProxy.key = std::string(cProxyData.key);
224 formDataProxy.subscribeId = std::string(cProxyData.subscribeId);
225 return true;
226 }
227
228 } // namespace AbilityRuntime
229 } // namespace OHOS