1 /*
2 * Copyright (c) 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 #define LOG_TAG "Uds"
16
17 #include "uds.h"
18 #include "logger.h"
19 #include "utd_common.h"
20 #include "unified_meta.h"
21 #include "unified_record.h"
22 #include "udmf_capi_common.h"
23 #include "udmf_meta.h"
24 #include "udmf_err_code.h"
25 #include "pixel_map.h"
26 #include "pixelmap_native_impl.h"
27
28 using namespace OHOS::UDMF;
29
GetUdsStrValue(UdsObject * pThis,NdkStructId ndkStructId,const char * paramName)30 static const char* GetUdsStrValue(UdsObject* pThis, NdkStructId ndkStructId, const char* paramName)
31 {
32 if (IsInvalidUdsObjectPtr(pThis, ndkStructId)) {
33 return nullptr;
34 }
35 auto value = pThis->GetUdsValue<std::string>(paramName);
36 return value == nullptr ? nullptr : value->c_str();
37 }
38
GetUdsUint8Value(UdsObject * pThis,const char * paramName,const char * paramNameLen,unsigned char ** data,unsigned int * len)39 static int GetUdsUint8Value(UdsObject* pThis, const char* paramName, const char* paramNameLen, unsigned char** data,
40 unsigned int* len)
41 {
42 auto value = pThis->GetUdsValue<std::vector<uint8_t>>(paramName);
43 if (value == nullptr) {
44 return UDMF_ERR;
45 }
46 auto lengthPtr = pThis->GetUdsValue<int>(paramNameLen);
47 int length;
48 if (lengthPtr == nullptr) {
49 if (value->size() > MAX_GENERAL_ENTRY_SIZE) {
50 LOG_ERROR(UDMF_CAPI, "length invalid. value'size = %{public}zu", value->size());
51 return UDMF_ERR;
52 }
53 length = static_cast<int>(value->size());
54 } else {
55 length = *lengthPtr;
56 }
57 if (length < 0 || length > MAX_GENERAL_ENTRY_SIZE) {
58 LOG_ERROR(UDMF_CAPI, "length invalid! length'size = %{public}d", length);
59 return UDMF_ERR;
60 }
61 *data = value->data();
62 *len = length;
63 return UDMF_E_OK;
64 }
65
IsInvalidUdsObjectPtr(const UdsObject * pThis,int cid)66 bool IsInvalidUdsObjectPtr(const UdsObject *pThis, int cid)
67 {
68 return pThis == nullptr || pThis->cid != cid || pThis->obj == nullptr;
69 }
70
IsInvalidUdsObjectByType(const UdsObject * pThis,const UDType & type)71 bool IsInvalidUdsObjectByType(const UdsObject* pThis, const UDType& type)
72 {
73 switch (type) {
74 case PLAIN_TEXT:
75 return IsInvalidUdsObjectPtr(pThis, UDS_PLAIN_TEXT_STRUCT_ID);
76 case HYPERLINK:
77 return IsInvalidUdsObjectPtr(pThis, UDS_HYPERLINK_STRUCT_ID);
78 case HTML:
79 return IsInvalidUdsObjectPtr(pThis, UDS_HTML_STRUCT_ID);
80 case SYSTEM_DEFINED_APP_ITEM:
81 return IsInvalidUdsObjectPtr(pThis, UDS_APP_ITEM_STRUCT_ID);
82 case FILE_URI:
83 return IsInvalidUdsObjectPtr(pThis, UDS_FILE_URI_STRUCT_ID);
84 case SYSTEM_DEFINED_PIXEL_MAP:
85 return IsInvalidUdsObjectPtr(pThis, UDS_PIXEL_MAP_STRUCT_ID);
86 default:
87 return false;
88 }
89 }
90
UdsObject(const int cid)91 UdsObject::UdsObject(const int cid) : cid(cid) {}
92
OH_UdsPlainText()93 OH_UdsPlainText::OH_UdsPlainText() : UdsObject(NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {}
94
OH_UdsHyperlink()95 OH_UdsHyperlink::OH_UdsHyperlink() : UdsObject(NdkStructId::UDS_HYPERLINK_STRUCT_ID) {}
96
OH_UdsHtml()97 OH_UdsHtml::OH_UdsHtml() : UdsObject(NdkStructId::UDS_HTML_STRUCT_ID) {}
98
OH_UdsAppItem()99 OH_UdsAppItem::OH_UdsAppItem() : UdsObject(NdkStructId::UDS_APP_ITEM_STRUCT_ID) {}
100
OH_UdsFileUri()101 OH_UdsFileUri::OH_UdsFileUri() : UdsObject(NdkStructId::UDS_FILE_URI_STRUCT_ID) {}
102
OH_UdsPixelMap()103 OH_UdsPixelMap::OH_UdsPixelMap() : UdsObject(NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {}
104
OH_UdsArrayBuffer()105 OH_UdsArrayBuffer::OH_UdsArrayBuffer() : UdsObject(NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) {}
106
OH_UdsContentForm()107 OH_UdsContentForm::OH_UdsContentForm() : UdsObject(NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) {}
108
HasObjectKey(const char * paramName)109 template <typename T> bool UdsObject::HasObjectKey(const char* paramName)
110 {
111 auto it = obj->value_.find(paramName);
112 if (it == obj->value_.end() || !std::holds_alternative<T>(it->second)) {
113 LOG_ERROR(UDMF_CAPI, "Don't have property %{public}s.", paramName);
114 return false;
115 }
116 return true;
117 }
118
119 template<typename T>
GetUdsValue(const char * paramName)120 T* UdsObject::GetUdsValue(const char* paramName)
121 {
122 if (!HasObjectKey<T>(paramName)) {
123 return nullptr;
124 }
125 return std::get_if<T>(&(obj->value_[paramName]));
126 }
127
128 template<typename T>
SetUdsValue(const char * paramName,const T pramValue)129 int UdsObject::SetUdsValue(const char* paramName, const T pramValue)
130 {
131 if (!HasObjectKey<T>(paramName)) {
132 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
133 }
134 std::lock_guard<std::mutex> lock(mutex);
135 obj->value_[paramName] = pramValue;
136 return Udmf_ErrCode::UDMF_E_OK;
137 }
138
OH_UdsPlainText_Create()139 OH_UdsPlainText* OH_UdsPlainText_Create()
140 {
141 OH_UdsPlainText* plainText = new (std::nothrow) OH_UdsPlainText();
142 if (plainText == nullptr) {
143 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
144 return nullptr;
145 }
146 plainText->obj = std::make_shared<Object>();
147 plainText->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_PLAIN_TEXT;
148 plainText->obj->value_[CONTENT] = "";
149 plainText->obj->value_[ABSTRACT] = "";
150 return plainText;
151 }
152
OH_UdsPlainText_Destroy(OH_UdsPlainText * pThis)153 void OH_UdsPlainText_Destroy(OH_UdsPlainText* pThis)
154 {
155 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID) {
156 delete pThis;
157 }
158 }
159
OH_UdsPlainText_GetType(OH_UdsPlainText * pThis)160 const char* OH_UdsPlainText_GetType(OH_UdsPlainText* pThis)
161 {
162 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, UNIFORM_DATA_TYPE);
163 }
164
OH_UdsPlainText_GetContent(OH_UdsPlainText * pThis)165 const char* OH_UdsPlainText_GetContent(OH_UdsPlainText* pThis)
166 {
167 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, CONTENT);
168 }
169
OH_UdsPlainText_GetAbstract(OH_UdsPlainText * pThis)170 const char* OH_UdsPlainText_GetAbstract(OH_UdsPlainText* pThis)
171 {
172 return GetUdsStrValue(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID, ABSTRACT);
173 }
174
OH_UdsPlainText_SetContent(OH_UdsPlainText * pThis,const char * content)175 int OH_UdsPlainText_SetContent(OH_UdsPlainText* pThis, const char* content)
176 {
177 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
178 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
179 }
180 return pThis->SetUdsValue<std::string>(CONTENT, content);
181 }
182
OH_UdsPlainText_SetAbstract(OH_UdsPlainText * pThis,const char * abstract)183 int OH_UdsPlainText_SetAbstract(OH_UdsPlainText* pThis, const char* abstract)
184 {
185 if (abstract == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PLAIN_TEXT_STRUCT_ID)) {
186 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
187 }
188 return pThis->SetUdsValue<std::string>(ABSTRACT, abstract);
189 }
190
OH_UdsHyperlink_Create()191 OH_UdsHyperlink* OH_UdsHyperlink_Create()
192 {
193 OH_UdsHyperlink* hyperlink = new (std::nothrow) OH_UdsHyperlink();
194 if (hyperlink == nullptr) {
195 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
196 return nullptr;
197 }
198 hyperlink->obj = std::make_shared<Object>();
199 hyperlink->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HYPERLINK;
200 hyperlink->obj->value_[URL] = "";
201 hyperlink->obj->value_[DESCRIPTION] = "";
202 return hyperlink;
203 }
204
OH_UdsHyperlink_Destroy(OH_UdsHyperlink * pThis)205 void OH_UdsHyperlink_Destroy(OH_UdsHyperlink* pThis)
206 {
207 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HYPERLINK_STRUCT_ID) {
208 delete pThis;
209 }
210 }
211
OH_UdsHyperlink_GetType(OH_UdsHyperlink * pThis)212 const char* OH_UdsHyperlink_GetType(OH_UdsHyperlink* pThis)
213 {
214 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, UNIFORM_DATA_TYPE);
215 }
216
OH_UdsHyperlink_GetUrl(OH_UdsHyperlink * pThis)217 const char* OH_UdsHyperlink_GetUrl(OH_UdsHyperlink* pThis)
218 {
219 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, URL);
220 }
221
OH_UdsHyperlink_GetDescription(OH_UdsHyperlink * pThis)222 const char* OH_UdsHyperlink_GetDescription(OH_UdsHyperlink* pThis)
223 {
224 return GetUdsStrValue(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID, DESCRIPTION);
225 }
226
OH_UdsHyperlink_SetUrl(OH_UdsHyperlink * pThis,const char * url)227 int OH_UdsHyperlink_SetUrl(OH_UdsHyperlink* pThis, const char* url)
228 {
229 if (url == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
230 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
231 }
232 return pThis->SetUdsValue<std::string>(URL, url);
233 }
234
OH_UdsHyperlink_SetDescription(OH_UdsHyperlink * pThis,const char * description)235 int OH_UdsHyperlink_SetDescription(OH_UdsHyperlink* pThis, const char* description)
236 {
237 if (description == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HYPERLINK_STRUCT_ID)) {
238 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
239 }
240 return pThis->SetUdsValue<std::string>(DESCRIPTION, description);
241 }
242
OH_UdsHtml_Create()243 OH_UdsHtml* OH_UdsHtml_Create()
244 {
245 OH_UdsHtml* html = new (std::nothrow) OH_UdsHtml();
246 if (html == nullptr) {
247 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
248 return nullptr;
249 }
250 html->obj = std::make_shared<Object>();
251 html->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_HTML;
252 html->obj->value_[HTML_CONTENT] = "";
253 html->obj->value_[PLAIN_CONTENT] = "";
254 return html;
255 }
256
OH_UdsHtml_Destroy(OH_UdsHtml * pThis)257 void OH_UdsHtml_Destroy(OH_UdsHtml* pThis)
258 {
259 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_HTML_STRUCT_ID) {
260 delete pThis;
261 }
262 }
263
OH_UdsHtml_GetType(OH_UdsHtml * pThis)264 const char* OH_UdsHtml_GetType(OH_UdsHtml* pThis)
265 {
266 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, UNIFORM_DATA_TYPE);
267 }
268
OH_UdsHtml_GetContent(OH_UdsHtml * pThis)269 const char* OH_UdsHtml_GetContent(OH_UdsHtml* pThis)
270 {
271 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, HTML_CONTENT);
272 }
273
OH_UdsHtml_GetPlainContent(OH_UdsHtml * pThis)274 const char* OH_UdsHtml_GetPlainContent(OH_UdsHtml* pThis)
275 {
276 return GetUdsStrValue(pThis, NdkStructId::UDS_HTML_STRUCT_ID, PLAIN_CONTENT);
277 }
278
OH_UdsHtml_SetContent(OH_UdsHtml * pThis,const char * content)279 int OH_UdsHtml_SetContent(OH_UdsHtml* pThis, const char* content)
280 {
281 if (content == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
282 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
283 }
284 return pThis->SetUdsValue<std::string>(HTML_CONTENT, content);
285 }
286
OH_UdsHtml_SetPlainContent(OH_UdsHtml * pThis,const char * plainContent)287 int OH_UdsHtml_SetPlainContent(OH_UdsHtml* pThis, const char* plainContent)
288 {
289 if (plainContent == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_HTML_STRUCT_ID)) {
290 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
291 }
292 return pThis->SetUdsValue<std::string>(PLAIN_CONTENT, plainContent);
293 }
294
OH_UdsAppItem_Create()295 OH_UdsAppItem* OH_UdsAppItem_Create()
296 {
297 OH_UdsAppItem* appItem = new (std::nothrow) OH_UdsAppItem();
298 if (appItem == nullptr) {
299 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
300 return nullptr;
301 }
302 appItem->obj = std::make_shared<Object>();
303 appItem->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_APP_ITEM;
304 appItem->obj->value_[APP_ID] = "";
305 appItem->obj->value_[APP_NAME] = "";
306 appItem->obj->value_[APP_ICON_ID] = "";
307 appItem->obj->value_[APP_LABEL_ID] = "";
308 appItem->obj->value_[BUNDLE_NAME] = "";
309 appItem->obj->value_[ABILITY_NAME] = "";
310 return appItem;
311 }
312
OH_UdsAppItem_Destroy(OH_UdsAppItem * pThis)313 void OH_UdsAppItem_Destroy(OH_UdsAppItem* pThis)
314 {
315 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_APP_ITEM_STRUCT_ID) {
316 delete pThis;
317 }
318 }
319
OH_UdsAppItem_GetType(OH_UdsAppItem * pThis)320 const char* OH_UdsAppItem_GetType(OH_UdsAppItem* pThis)
321 {
322 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, UNIFORM_DATA_TYPE);
323 }
324
OH_UdsAppItem_GetId(OH_UdsAppItem * pThis)325 const char* OH_UdsAppItem_GetId(OH_UdsAppItem* pThis)
326 {
327 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ID);
328 }
329
OH_UdsAppItem_GetName(OH_UdsAppItem * pThis)330 const char* OH_UdsAppItem_GetName(OH_UdsAppItem* pThis)
331 {
332 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_NAME);
333 }
334
OH_UdsAppItem_GetIconId(OH_UdsAppItem * pThis)335 const char* OH_UdsAppItem_GetIconId(OH_UdsAppItem* pThis)
336 {
337 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_ICON_ID);
338 }
339
OH_UdsAppItem_GetLabelId(OH_UdsAppItem * pThis)340 const char* OH_UdsAppItem_GetLabelId(OH_UdsAppItem* pThis)
341 {
342 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, APP_LABEL_ID);
343 }
344
OH_UdsAppItem_GetBundleName(OH_UdsAppItem * pThis)345 const char* OH_UdsAppItem_GetBundleName(OH_UdsAppItem* pThis)
346 {
347 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, BUNDLE_NAME);
348 }
349
OH_UdsAppItem_GetAbilityName(OH_UdsAppItem * pThis)350 const char* OH_UdsAppItem_GetAbilityName(OH_UdsAppItem* pThis)
351 {
352 return GetUdsStrValue(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID, ABILITY_NAME);
353 }
354
OH_UdsAppItem_SetId(OH_UdsAppItem * pThis,const char * appId)355 int OH_UdsAppItem_SetId(OH_UdsAppItem* pThis, const char* appId)
356 {
357 if (appId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
358 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
359 }
360 return pThis->SetUdsValue<std::string>(APP_ID, appId);
361 }
362
OH_UdsAppItem_SetName(OH_UdsAppItem * pThis,const char * appName)363 int OH_UdsAppItem_SetName(OH_UdsAppItem* pThis, const char* appName)
364 {
365 if (appName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
366 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
367 }
368 return pThis->SetUdsValue<std::string>(APP_NAME, appName);
369 }
370
OH_UdsAppItem_SetIconId(OH_UdsAppItem * pThis,const char * appIconId)371 int OH_UdsAppItem_SetIconId(OH_UdsAppItem* pThis, const char* appIconId)
372 {
373 if (appIconId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
374 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
375 }
376 return pThis->SetUdsValue<std::string>(APP_ICON_ID, appIconId);
377 }
378
OH_UdsAppItem_SetLabelId(OH_UdsAppItem * pThis,const char * appLabelId)379 int OH_UdsAppItem_SetLabelId(OH_UdsAppItem* pThis, const char* appLabelId)
380 {
381 if (appLabelId == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
382 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
383 }
384 return pThis->SetUdsValue<std::string>(APP_LABEL_ID, appLabelId);
385 }
386
OH_UdsAppItem_SetBundleName(OH_UdsAppItem * pThis,const char * bundleName)387 int OH_UdsAppItem_SetBundleName(OH_UdsAppItem* pThis, const char* bundleName)
388 {
389 if (bundleName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
390 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
391 }
392 return pThis->SetUdsValue<std::string>(BUNDLE_NAME, bundleName);
393 }
394
OH_UdsAppItem_SetAbilityName(OH_UdsAppItem * pThis,const char * abilityName)395 int OH_UdsAppItem_SetAbilityName(OH_UdsAppItem* pThis, const char* abilityName)
396 {
397 if (abilityName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_APP_ITEM_STRUCT_ID)) {
398 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
399 }
400 return pThis->SetUdsValue<std::string>(ABILITY_NAME, abilityName);
401 }
402
OH_UdsFileUri_Create()403 OH_UdsFileUri* OH_UdsFileUri_Create()
404 {
405 OH_UdsFileUri* fileUri = new (std::nothrow) OH_UdsFileUri();
406 if (fileUri == nullptr) {
407 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
408 return nullptr;
409 }
410 fileUri->obj = std::make_shared<Object>();
411 fileUri->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_GENERAL_FILE_URI;
412 fileUri->obj->value_[FILE_URI_PARAM] = "";
413 fileUri->obj->value_[FILE_TYPE] = "";
414 return fileUri;
415 }
416
OH_UdsFileUri_Destroy(OH_UdsFileUri * pThis)417 void OH_UdsFileUri_Destroy(OH_UdsFileUri* pThis)
418 {
419 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_FILE_URI_STRUCT_ID) {
420 delete pThis;
421 }
422 }
423
OH_UdsFileUri_GetType(OH_UdsFileUri * pThis)424 const char* OH_UdsFileUri_GetType(OH_UdsFileUri* pThis)
425 {
426 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, UNIFORM_DATA_TYPE);
427 }
428
OH_UdsFileUri_GetFileUri(OH_UdsFileUri * pThis)429 const char* OH_UdsFileUri_GetFileUri(OH_UdsFileUri* pThis)
430 {
431 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_URI_PARAM);
432 }
433
OH_UdsFileUri_GetFileType(OH_UdsFileUri * pThis)434 const char* OH_UdsFileUri_GetFileType(OH_UdsFileUri* pThis)
435 {
436 return GetUdsStrValue(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID, FILE_TYPE);
437 }
438
OH_UdsFileUri_SetFileUri(OH_UdsFileUri * pThis,const char * fileUri)439 int OH_UdsFileUri_SetFileUri(OH_UdsFileUri* pThis, const char* fileUri)
440 {
441 if (fileUri == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
442 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
443 }
444 return pThis->SetUdsValue<std::string>(FILE_URI_PARAM, fileUri);
445 }
446
OH_UdsFileUri_SetFileType(OH_UdsFileUri * pThis,const char * fileType)447 int OH_UdsFileUri_SetFileType(OH_UdsFileUri* pThis, const char* fileType)
448 {
449 if (fileType == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_FILE_URI_STRUCT_ID)) {
450 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
451 }
452 return pThis->SetUdsValue<std::string>(FILE_TYPE, fileType);
453 }
454
OH_UdsPixelMap_Create()455 OH_UdsPixelMap* OH_UdsPixelMap_Create()
456 {
457 OH_UdsPixelMap* pixelMap = new (std::nothrow) OH_UdsPixelMap();
458 if (pixelMap == nullptr) {
459 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
460 return nullptr;
461 }
462 pixelMap->obj = std::make_shared<Object>();
463 pixelMap->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_OPENHARMONY_PIXEL_MAP;
464 pixelMap->obj->value_[PIXEL_MAP] = std::make_shared<OHOS::Media::PixelMap>();
465 return pixelMap;
466 }
467
OH_UdsPixelMap_Destroy(OH_UdsPixelMap * pThis)468 void OH_UdsPixelMap_Destroy(OH_UdsPixelMap* pThis)
469 {
470 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_PIXEL_MAP_STRUCT_ID) {
471 delete pThis;
472 }
473 }
474
OH_UdsPixelMap_GetType(OH_UdsPixelMap * pThis)475 const char* OH_UdsPixelMap_GetType(OH_UdsPixelMap* pThis)
476 {
477 return GetUdsStrValue(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID, UNIFORM_DATA_TYPE);
478 }
479
OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)480 void OH_UdsPixelMap_GetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
481 {
482 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
483 return;
484 }
485 auto pixelMap = pThis->GetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP);
486 if (pixelMap != nullptr) {
487 *pixelmapNative = OH_PixelmapNative(*pixelMap);
488 }
489 }
490
OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap * pThis,OH_PixelmapNative * pixelmapNative)491 int OH_UdsPixelMap_SetPixelMap(OH_UdsPixelMap* pThis, OH_PixelmapNative* pixelmapNative)
492 {
493 if (pixelmapNative == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_PIXEL_MAP_STRUCT_ID)) {
494 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
495 }
496 return pThis->SetUdsValue<std::shared_ptr<OHOS::Media::PixelMap>>(PIXEL_MAP, pixelmapNative->GetInnerPixelmap());
497 }
498
OH_UdsArrayBuffer_Create()499 OH_UdsArrayBuffer* OH_UdsArrayBuffer_Create()
500 {
501 auto *buffer = new (std::nothrow) OH_UdsArrayBuffer();
502 if (buffer == nullptr) {
503 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
504 return nullptr;
505 }
506 buffer->obj = std::make_shared<Object>();
507 buffer->obj->value_[UNIFORM_DATA_TYPE] = "";
508 buffer->obj->value_[ARRAY_BUFFER] = std::vector<uint8_t>();
509 buffer->obj->value_[ARRAY_BUFFER_LENGTH] = 0;
510 return buffer;
511 }
512
OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer * buffer)513 int OH_UdsArrayBuffer_Destroy(OH_UdsArrayBuffer* buffer)
514 {
515 if (IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
516 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
517 return UDMF_E_INVALID_PARAM;
518 }
519 delete buffer;
520 return UDMF_E_OK;
521 }
522
OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer * buffer,unsigned char * data,unsigned int len)523 int OH_UdsArrayBuffer_SetData(OH_UdsArrayBuffer* buffer, unsigned char* data, unsigned int len)
524 {
525 if (data == nullptr || len == 0 || IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID) ||
526 len > MAX_GENERAL_ENTRY_SIZE) {
527 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
528 return UDMF_E_INVALID_PARAM;
529 }
530 std::vector<uint8_t> arrayBuffer(data, data + len);
531 int ret = buffer->SetUdsValue<std::vector<uint8_t>>(ARRAY_BUFFER, arrayBuffer);
532 if (ret != UDMF_E_OK) {
533 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
534 return ret;
535 }
536 ret = buffer->SetUdsValue<int>(ARRAY_BUFFER_LENGTH, static_cast<int>(len));
537 return ret;
538 }
539
OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer * buffer,unsigned char ** data,unsigned int * len)540 int OH_UdsArrayBuffer_GetData(OH_UdsArrayBuffer* buffer, unsigned char** data, unsigned int* len)
541 {
542 if (IsInvalidUdsObjectPtr(buffer, NdkStructId::UDS_ARRAY_BUFFER_STRUCT_ID)) {
543 return UDMF_E_INVALID_PARAM;
544 }
545 return GetUdsUint8Value(buffer, ARRAY_BUFFER, ARRAY_BUFFER_LENGTH, data, len);
546 }
547
OH_UdsContentForm_Create()548 OH_UdsContentForm* OH_UdsContentForm_Create()
549 {
550 auto contentForm = new (std::nothrow) OH_UdsContentForm();
551 if (contentForm == nullptr) {
552 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory.");
553 return nullptr;
554 }
555 contentForm->obj = std::make_shared<Object>();
556 contentForm->obj->value_[UNIFORM_DATA_TYPE] = UDMF_METE_GENERAL_CONTENT_FORM;
557 contentForm->obj->value_[THUMB_DATA] = std::vector<uint8_t>();
558 contentForm->obj->value_[THUMB_DATA_LENGTH] = 0;
559 contentForm->obj->value_[DESCRIPTION] = "";
560 contentForm->obj->value_[TITLE] = "";
561 contentForm->obj->value_[APP_ICON] = std::vector<uint8_t>();
562 contentForm->obj->value_[APP_ICON_LENGTH] = 0;
563 contentForm->obj->value_[APP_NAME] = "";
564 contentForm->obj->value_[LINK_URL] = "";
565 return contentForm;
566 }
567
OH_UdsContentForm_Destroy(OH_UdsContentForm * pThis)568 void OH_UdsContentForm_Destroy(OH_UdsContentForm* pThis)
569 {
570 if (pThis != nullptr && pThis->cid == NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) {
571 delete pThis;
572 }
573 }
574
OH_UdsContentForm_GetType(OH_UdsContentForm * pThis)575 const char* OH_UdsContentForm_GetType(OH_UdsContentForm* pThis)
576 {
577 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, UNIFORM_DATA_TYPE);
578 }
579
OH_UdsContentForm_GetThumbData(OH_UdsContentForm * pThis,unsigned char ** thumbData,unsigned int * len)580 int OH_UdsContentForm_GetThumbData(OH_UdsContentForm* pThis, unsigned char** thumbData, unsigned int* len)
581 {
582 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
583 return UDMF_E_INVALID_PARAM;
584 }
585 return GetUdsUint8Value(pThis, THUMB_DATA, THUMB_DATA_LENGTH, thumbData, len);
586 }
587
OH_UdsContentForm_GetDescription(OH_UdsContentForm * pThis)588 const char* OH_UdsContentForm_GetDescription(OH_UdsContentForm* pThis)
589 {
590 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, DESCRIPTION);
591 }
592
OH_UdsContentForm_GetTitle(OH_UdsContentForm * pThis)593 const char* OH_UdsContentForm_GetTitle(OH_UdsContentForm* pThis)
594 {
595 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, TITLE);
596 }
597
OH_UdsContentForm_GetAppIcon(OH_UdsContentForm * pThis,unsigned char ** appIcon,unsigned int * len)598 int OH_UdsContentForm_GetAppIcon(OH_UdsContentForm* pThis, unsigned char** appIcon, unsigned int* len)
599 {
600 if (IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
601 return UDMF_E_INVALID_PARAM;
602 }
603 return GetUdsUint8Value(pThis, APP_ICON, APP_ICON_LENGTH, appIcon, len);
604 }
605
OH_UdsContentForm_GetAppName(OH_UdsContentForm * pThis)606 const char* OH_UdsContentForm_GetAppName(OH_UdsContentForm* pThis)
607 {
608 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, APP_NAME);
609 }
610
OH_UdsContentForm_GetLinkUri(OH_UdsContentForm * pThis)611 const char* OH_UdsContentForm_GetLinkUri(OH_UdsContentForm* pThis)
612 {
613 return GetUdsStrValue(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID, LINK_URL);
614 }
615
OH_UdsContentForm_SetThumbData(OH_UdsContentForm * pThis,const unsigned char * thumbData,unsigned int len)616 int OH_UdsContentForm_SetThumbData(OH_UdsContentForm* pThis, const unsigned char* thumbData, unsigned int len)
617 {
618 if (thumbData == nullptr || len == 0 || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) ||
619 len > MAX_GENERAL_ENTRY_SIZE) {
620 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
621 return UDMF_E_INVALID_PARAM;
622 }
623 std::vector<uint8_t> data(thumbData, thumbData + len);
624 int ret = pThis->SetUdsValue<std::vector<uint8_t>>(THUMB_DATA, data);
625 if (ret != UDMF_E_OK) {
626 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
627 return ret;
628 }
629 ret = pThis->SetUdsValue<int>(THUMB_DATA_LENGTH, static_cast<int>(len));
630 return ret;
631 }
632
OH_UdsContentForm_SetDescription(OH_UdsContentForm * pThis,const char * description)633 int OH_UdsContentForm_SetDescription(OH_UdsContentForm* pThis, const char* description)
634 {
635 if (description == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
636 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
637 }
638 return pThis->SetUdsValue<std::string>(DESCRIPTION, description);
639 }
640
OH_UdsContentForm_SetTitle(OH_UdsContentForm * pThis,const char * title)641 int OH_UdsContentForm_SetTitle(OH_UdsContentForm* pThis, const char* title)
642 {
643 if (title == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
644 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
645 }
646 return pThis->SetUdsValue<std::string>(TITLE, title);
647 }
648
OH_UdsContentForm_SetAppIcon(OH_UdsContentForm * pThis,const unsigned char * appIcon,unsigned int len)649 int OH_UdsContentForm_SetAppIcon(OH_UdsContentForm* pThis, const unsigned char* appIcon, unsigned int len)
650 {
651 if (appIcon == nullptr || len == 0 || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID) ||
652 len > MAX_GENERAL_ENTRY_SIZE) {
653 LOG_ERROR(UDMF_CAPI, "Param is invalid.");
654 return UDMF_E_INVALID_PARAM;
655 }
656 std::vector<uint8_t> data(appIcon, appIcon + len);
657 int ret = pThis->SetUdsValue<std::vector<uint8_t>>(APP_ICON, data);
658 if (ret != UDMF_E_OK) {
659 LOG_ERROR(UDMF_CAPI, "Failed to apply for memory. ret: %{public}d", ret);
660 return ret;
661 }
662 ret = pThis->SetUdsValue<int>(APP_ICON_LENGTH, static_cast<int>(len));
663 return ret;
664 }
665
OH_UdsContentForm_SetAppName(OH_UdsContentForm * pThis,const char * appName)666 int OH_UdsContentForm_SetAppName(OH_UdsContentForm* pThis, const char* appName)
667 {
668 if (appName == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
669 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
670 }
671 return pThis->SetUdsValue<std::string>(APP_NAME, appName);
672 }
673
OH_UdsContentForm_SetLinkUri(OH_UdsContentForm * pThis,const char * linkUri)674 int OH_UdsContentForm_SetLinkUri(OH_UdsContentForm* pThis, const char* linkUri)
675 {
676 if (linkUri == nullptr || IsInvalidUdsObjectPtr(pThis, NdkStructId::UDS_CONTENT_FORM_STRUCT_ID)) {
677 return Udmf_ErrCode::UDMF_E_INVALID_PARAM;
678 }
679 return pThis->SetUdsValue<std::string>(LINK_URL, linkUri);
680 }