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