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 "Udmf"
16 #include "udmf.h"
17 #include "udmf_err_code.h"
18 #include "data_provider_impl.h"
19 #include "udmf_client.h"
20 #include "securec.h"
21 #include "udmf_capi_common.h"
22 #include "int_wrapper.h"
23 #include "string_wrapper.h"
24 #include "unified_meta.h"
25 #include "udmf_meta.h"
26 #include "logger.h"
27 #include "plain_text.h"
28 #include "link.h"
29 #include "html.h"
30 #include "system_defined_appitem.h"
31 #include "application_defined_record.h"
32 #include "system_defined_pixelmap.h"
33 #include "file.h"
34 #include "audio.h"
35 #include "folder.h"
36 #include "image.h"
37 #include "video.h"
38 #include "utd.h"
39 #include "utd_client.h"
40
41 using namespace OHOS::UDMF;
42
43 static constexpr uint64_t MAX_RECORDS_COUNT = 4 * 1024 * 1024;
44 static constexpr uint64_t MAX_KEY_STRING_LEN = 1 * 1024 * 1024;
45 static const std::map<std::string, UDType> FILE_TYPES = {
46 { UDMF_META_GENERAL_FILE, UDType::FILE },
47 { UDMF_META_AUDIO, UDType::AUDIO },
48 { UDMF_META_FOLDER, UDType::FOLDER },
49 { UDMF_META_IMAGE, UDType::IMAGE },
50 { UDMF_META_VIDEO, UDType::VIDEO }
51 };
52 static const std::set<std::string> FILE_SUB_TYPES = {
53 "general.image", "general.video", "general.audio", "general.folder" };
54
DestroyStringArray(char ** & bufArray,unsigned int & count)55 static void DestroyStringArray(char**& bufArray, unsigned int& count)
56 {
57 if (bufArray == nullptr) {
58 return;
59 }
60 for (unsigned int i = 0; i < count; i++) {
61 if (bufArray[i] != nullptr) {
62 delete[] bufArray[i];
63 bufArray[i] = nullptr;
64 }
65 }
66 delete[] bufArray;
67 bufArray = nullptr;
68 count = 0;
69 }
70
DestroyUnifiedRecordArray(OH_UdmfRecord ** & records,unsigned int & count)71 static void DestroyUnifiedRecordArray(OH_UdmfRecord**& records, unsigned int& count)
72 {
73 if (records == nullptr) {
74 return;
75 }
76 for (unsigned int i = 0; i < count; i++) {
77 if (records[i] != nullptr) {
78 delete records[i];
79 records[i] = nullptr;
80 }
81 }
82 delete[] records;
83 records = nullptr;
84 count = 0;
85 }
86
StrVectorToTypesArray(const std::vector<std::string> & strVector)87 static char** StrVectorToTypesArray(const std::vector<std::string>& strVector)
88 {
89 unsigned int vectorSize = strVector.size();
90 if (vectorSize == 0 || vectorSize > MAX_RECORDS_COUNT) {
91 return nullptr;
92 }
93 char** typesArray = new (std::nothrow) char* [vectorSize];
94 if (typesArray == nullptr) {
95 LOG_ERROR(UDMF_CAPI, "create types array failed!, vectorSize: %{public}d, MAX_RECORDS_COUNT: %{public}" PRIu64,
96 vectorSize, MAX_RECORDS_COUNT);
97 return nullptr;
98 }
99 for (unsigned int i = 0; i < vectorSize; ++i) {
100 unsigned int strLen = strVector[i].length() + 1;
101 if (strLen > MAX_KEY_STRING_LEN) {
102 LOG_INFO(UDMF_CAPI, "string exceeds maximum length, length is %{public}d", strLen);
103 DestroyStringArray(typesArray, vectorSize);
104 return nullptr;
105 }
106 typesArray[i] = new (std::nothrow) char[strLen];
107 if (typesArray[i] == nullptr ||
108 (strcpy_s(typesArray[i], strLen, strVector[i].c_str())) != EOK) {
109 LOG_ERROR(UDMF_CAPI, "string copy failed");
110 DestroyStringArray(typesArray, vectorSize);
111 return nullptr;
112 }
113 }
114 return typesArray;
115 }
116
CreateUnifiedDataRecordsArray(OH_UdmfData * unifiedData,std::vector<std::shared_ptr<UnifiedRecord>> & records)117 static OH_UdmfRecord** CreateUnifiedDataRecordsArray(OH_UdmfData* unifiedData,
118 std::vector<std::shared_ptr<UnifiedRecord>>& records)
119 {
120 unsigned int size = static_cast<unsigned int>(records.size());
121 if (unifiedData == nullptr || size == 0 || size > MAX_RECORDS_COUNT) {
122 return nullptr;
123 }
124 OH_UdmfRecord** result = new (std::nothrow) OH_UdmfRecord* [size] { nullptr };
125 if (result == nullptr) {
126 return nullptr;
127 }
128 for (unsigned int i = 0; i < size; i++) {
129 result[i] = new (std::nothrow) OH_UdmfRecord;
130 if (result[i] == nullptr) {
131 DestroyUnifiedRecordArray(result, size);
132 return nullptr;
133 }
134 result[i]->record_ = records[i];
135 }
136 unifiedData->records = result;
137 unifiedData->recordsCount = size;
138 return unifiedData->records;
139 }
140
IsUnifiedDataValid(OH_UdmfData * data)141 static bool IsUnifiedDataValid(OH_UdmfData* data)
142 {
143 return data != nullptr && data->unifiedData_ != nullptr &&
144 data->cid == NdkStructId::UDMF_UNIFIED_DATA_STRUCT_ID;
145 }
146
IsUnifiedRecordValid(OH_UdmfRecord * record)147 static bool IsUnifiedRecordValid(OH_UdmfRecord* record)
148 {
149 return record != nullptr && record->record_ != nullptr &&
150 record->cid == NdkStructId::UDMF_UNIFIED_RECORD_STRUCT_ID;
151 }
152
IsUnifiedPropertiesValid(OH_UdmfProperty * properties)153 static bool IsUnifiedPropertiesValid(OH_UdmfProperty* properties)
154 {
155 return properties != nullptr && properties->properties_ != nullptr &&
156 properties->cid == NdkStructId::UDMF_UNIFIED_DATA_PROPERTIES_ID;
157 }
158
AddFileUriTypeIfContains(std::vector<std::string> & types)159 static void AddFileUriTypeIfContains(std::vector<std::string>& types)
160 {
161 if (std::find(types.begin(), types.end(), UDMF_META_GENERAL_FILE_URI) != types.end()) {
162 return;
163 }
164 for (auto type : types) {
165 if (FILE_TYPES.find(type) != FILE_TYPES.end()) {
166 types.push_back(UDMF_META_GENERAL_FILE_URI);
167 break;
168 }
169 }
170 return;
171 }
172
OH_UdmfData_Create()173 OH_UdmfData* OH_UdmfData_Create()
174 {
175 OH_UdmfData* data = new (std::nothrow) OH_UdmfData;
176 if (data == nullptr) {
177 return nullptr;
178 }
179 data->unifiedData_ = std::make_shared<UnifiedData>();
180 return data;
181 }
182
OH_UdmfData_Destroy(OH_UdmfData * data)183 void OH_UdmfData_Destroy(OH_UdmfData* data)
184 {
185 if (data == nullptr) {
186 return;
187 }
188 DestroyStringArray(data->typesArray, data->typesCount);
189 DestroyUnifiedRecordArray(data->records, data->recordsCount);
190 delete data;
191 }
192
OH_UdmfData_AddRecord(OH_UdmfData * unifiedData,OH_UdmfRecord * record)193 int OH_UdmfData_AddRecord(OH_UdmfData* unifiedData, OH_UdmfRecord* record)
194 {
195 if (!IsUnifiedDataValid(unifiedData) || !IsUnifiedRecordValid(record)) {
196 return UDMF_E_INVALID_PARAM;
197 }
198 unifiedData->unifiedData_->AddRecord(record->record_);
199 return UDMF_E_OK;
200 }
201
OH_UdmfData_HasType(OH_UdmfData * unifiedData,const char * type)202 bool OH_UdmfData_HasType(OH_UdmfData* unifiedData, const char* type)
203 {
204 return IsUnifiedDataValid(unifiedData) && type != nullptr && unifiedData->unifiedData_->HasTypeInEntries(type);
205 }
206
OH_UdmfData_GetTypes(OH_UdmfData * unifiedData,unsigned int * count)207 char** OH_UdmfData_GetTypes(OH_UdmfData* unifiedData, unsigned int* count)
208 {
209 if (!IsUnifiedDataValid(unifiedData) || count == nullptr) {
210 return nullptr;
211 }
212 std::lock_guard<std::mutex> lock(unifiedData->mutex);
213 if (unifiedData->typesArray != nullptr) {
214 LOG_DEBUG(UDMF_CAPI, "return cache value");
215 *count = unifiedData->typesCount;
216 return unifiedData->typesArray;
217 }
218 std::vector<std::string> typeLabels = unifiedData->unifiedData_->GetEntriesTypes();
219 AddFileUriTypeIfContains(typeLabels);
220 unifiedData->typesArray = StrVectorToTypesArray(typeLabels);
221 unifiedData->typesArray == nullptr ? unifiedData->typesCount = 0 : unifiedData->typesCount = typeLabels.size();
222 *count = unifiedData->typesCount;
223 return unifiedData->typesArray;
224 }
225
OH_UdmfRecord_GetTypes(OH_UdmfRecord * record,unsigned int * count)226 char** OH_UdmfRecord_GetTypes(OH_UdmfRecord* record, unsigned int* count)
227 {
228 if (!IsUnifiedRecordValid(record) || count == nullptr) {
229 return nullptr;
230 }
231 std::lock_guard<std::mutex> lock(record->mutex);
232 if (record->typesArray != nullptr) {
233 LOG_DEBUG(UDMF_CAPI, "return cache value");
234 *count = record->typesCount;
235 return record->typesArray;
236 }
237 auto types = record->record_->GetUtdIdsWithAddFileType();
238 std::vector<std::string> typeLabels {types.begin(), types.end()};
239 AddFileUriTypeIfContains(typeLabels);
240 record->typesArray = StrVectorToTypesArray(typeLabels);
241 record->typesArray == nullptr ? record->typesCount = 0 : record->typesCount = typeLabels.size();
242 *count = record->typesCount;
243 return record->typesArray;
244 }
245
OH_UdmfData_GetRecords(OH_UdmfData * unifiedData,unsigned int * count)246 OH_UdmfRecord** OH_UdmfData_GetRecords(OH_UdmfData* unifiedData, unsigned int* count)
247 {
248 if (!IsUnifiedDataValid(unifiedData) || count == nullptr) {
249 return nullptr;
250 }
251 std::lock_guard<std::mutex> lock(unifiedData->mutex);
252 if (unifiedData->records != nullptr) {
253 LOG_DEBUG(UDMF_CAPI, "return cache value");
254 *count = unifiedData->recordsCount;
255 return unifiedData->records;
256 }
257 std::vector<std::shared_ptr<UnifiedRecord>> records = unifiedData->unifiedData_->GetRecords();
258 CreateUnifiedDataRecordsArray(unifiedData, records);
259 *count = unifiedData->recordsCount;
260 return unifiedData->records;
261 }
262
GetFirstPlainText(OH_UdmfRecord ** records,unsigned int recordCount,OH_UdsPlainText * plainText)263 static int GetFirstPlainText(OH_UdmfRecord **records, unsigned int recordCount, OH_UdsPlainText* plainText)
264 {
265 int ret = UDMF_ERR;
266 if (records == nullptr || recordCount == 0) {
267 return ret;
268 }
269 for (unsigned int i = 0; i < recordCount; i++) {
270 const char *type = OH_UdsPlainText_GetType(plainText);
271 if (type == nullptr || !records[i]->record_->HasType(type)) {
272 continue;
273 }
274 ret = OH_UdmfRecord_GetPlainText(records[i], plainText);
275 if (ret == UDMF_E_OK) {
276 return ret;
277 }
278 }
279 return ret;
280 }
281
GetFirstHtml(OH_UdmfRecord ** records,unsigned int recordCount,OH_UdsHtml * html)282 static int GetFirstHtml(OH_UdmfRecord **records, unsigned int recordCount, OH_UdsHtml* html)
283 {
284 int ret = UDMF_ERR;
285 if (records == nullptr || recordCount == 0) {
286 return ret;
287 }
288 for (unsigned int i = 0; i < recordCount; i++) {
289 const char *type = OH_UdsHtml_GetType(html);
290 if (type == nullptr || !records[i]->record_->HasType(type)) {
291 continue;
292 }
293 ret = OH_UdmfRecord_GetHtml(records[i], html);
294 if (ret == UDMF_E_OK) {
295 return ret;
296 }
297 }
298 return ret;
299 }
300
OH_UdmfData_GetPrimaryPlainText(OH_UdmfData * data,OH_UdsPlainText * plainText)301 int OH_UdmfData_GetPrimaryPlainText(OH_UdmfData* data, OH_UdsPlainText* plainText)
302 {
303 if (!IsUnifiedDataValid(data) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
304 return UDMF_E_INVALID_PARAM;
305 }
306 std::lock_guard<std::mutex> lock(data->mutex);
307 if (data->records == nullptr) {
308 LOG_DEBUG(UDMF_CAPI, "no cache value");
309 std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
310 CreateUnifiedDataRecordsArray(data, records);
311 }
312
313 return GetFirstPlainText(data->records, data->recordsCount, plainText);
314 }
315
OH_UdmfData_GetPrimaryHtml(OH_UdmfData * data,OH_UdsHtml * html)316 int OH_UdmfData_GetPrimaryHtml(OH_UdmfData* data, OH_UdsHtml* html)
317 {
318 if (!IsUnifiedDataValid(data) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
319 return UDMF_E_INVALID_PARAM;
320 }
321 std::lock_guard<std::mutex> lock(data->mutex);
322 if (data->records == nullptr) {
323 LOG_DEBUG(UDMF_CAPI, "no cache value");
324 std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
325 CreateUnifiedDataRecordsArray(data, records);
326 }
327
328 return GetFirstHtml(data->records, data->recordsCount, html);
329 }
330
OH_UdmfData_GetRecordCount(OH_UdmfData * data)331 int OH_UdmfData_GetRecordCount(OH_UdmfData *data)
332 {
333 if (!IsUnifiedDataValid(data)) {
334 return 0;
335 }
336 std::lock_guard<std::mutex> lock(data->mutex);
337 if (data->records == nullptr) {
338 LOG_DEBUG(UDMF_CAPI, "no cache value");
339 std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
340 CreateUnifiedDataRecordsArray(data, records);
341 }
342 return static_cast<int>(data->recordsCount);
343 }
344
OH_UdmfData_GetRecord(OH_UdmfData * data,unsigned int index)345 OH_UdmfRecord* OH_UdmfData_GetRecord(OH_UdmfData* data, unsigned int index)
346 {
347 if (!IsUnifiedDataValid(data)) {
348 return nullptr;
349 }
350 std::lock_guard<std::mutex> lock(data->mutex);
351 if (data->records == nullptr) {
352 LOG_DEBUG(UDMF_CAPI, "no cache value");
353 std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
354 CreateUnifiedDataRecordsArray(data, records);
355 }
356 if (index >= data->recordsCount || data->records == nullptr) {
357 return nullptr;
358 }
359 return data->records[index];
360 }
361
OH_UdmfData_IsLocal(OH_UdmfData * data)362 bool OH_UdmfData_IsLocal(OH_UdmfData* data)
363 {
364 if (!IsUnifiedDataValid(data) || data->unifiedData_->GetProperties() == nullptr) {
365 return true;
366 }
367 bool isRemote = data->unifiedData_->GetProperties()->isRemote;
368 return !isRemote;
369 }
370
OH_Udmf_GetUnifiedData(const char * key,Udmf_Intention intention,OH_UdmfData * data)371 int OH_Udmf_GetUnifiedData(const char* key, Udmf_Intention intention, OH_UdmfData* data)
372 {
373 if (!IsUnifiedDataValid(data) || key == nullptr) {
374 return UDMF_E_INVALID_PARAM;
375 }
376 Intention queryOptIntent;
377 switch (intention) {
378 case UDMF_INTENTION_DRAG:
379 queryOptIntent = Intention::UD_INTENTION_DRAG;
380 break;
381 default:
382 return UDMF_E_INVALID_PARAM;
383 }
384 QueryOption query = {.key = std::string(key), .intention = queryOptIntent};
385 if (UdmfClient::GetInstance().GetData(query, *(data->unifiedData_)) != E_OK) {
386 LOG_ERROR(UDMF_CAPI, "get data error");
387 return UDMF_ERR;
388 }
389 return UDMF_E_OK;
390 }
391
OH_Udmf_SetUnifiedData(Udmf_Intention intention,OH_UdmfData * unifiedData,char * key,unsigned int keyLen)392 int OH_Udmf_SetUnifiedData(Udmf_Intention intention, OH_UdmfData* unifiedData, char* key, unsigned int keyLen)
393 {
394 if (!IsUnifiedDataValid(unifiedData) || key == nullptr || keyLen < UDMF_KEY_BUFFER_LEN) {
395 return UDMF_E_INVALID_PARAM;
396 }
397 enum Intention customOptIntent;
398 switch (intention) {
399 case UDMF_INTENTION_DRAG:
400 customOptIntent = Intention::UD_INTENTION_DRAG;
401 break;
402 default:
403 return UDMF_E_INVALID_PARAM;
404 }
405 CustomOption option = {.intention = customOptIntent};
406 std::string keyStr;
407 if ((UdmfClient::GetInstance().SetData(option, *(unifiedData->unifiedData_), keyStr)) != E_OK) {
408 LOG_ERROR(UDMF_CAPI, "set data error");
409 return UDMF_ERR;
410 }
411 if (strcpy_s(key, keyLen, keyStr.c_str()) != EOK) {
412 LOG_ERROR(UDMF_CAPI, "string copy failed");
413 return UDMF_ERR;
414 }
415 return UDMF_E_OK;
416 }
417
OH_UdmfRecord_Create()418 OH_UdmfRecord* OH_UdmfRecord_Create()
419 {
420 OH_UdmfRecord* record = new (std::nothrow) OH_UdmfRecord;
421 if (record == nullptr) {
422 return nullptr;
423 }
424 record->record_ = std::make_shared<UnifiedRecord>();
425 return record;
426 }
427
OH_UdmfRecord_Destroy(OH_UdmfRecord * record)428 void OH_UdmfRecord_Destroy(OH_UdmfRecord* record)
429 {
430 if (record == nullptr) {
431 return;
432 }
433 if (record->recordData != nullptr) {
434 delete[] record->recordData;
435 record->recordData = nullptr;
436 }
437 DestroyStringArray(record->typesArray, record->typesCount);
438 delete record;
439 }
440
OH_UdmfRecord_AddGeneralEntry(OH_UdmfRecord * record,const char * typeId,const unsigned char * entry,unsigned int count)441 int OH_UdmfRecord_AddGeneralEntry(OH_UdmfRecord* record, const char* typeId,
442 const unsigned char* entry, unsigned int count)
443 {
444 if (!IsUnifiedRecordValid(record) || typeId == nullptr || entry == nullptr || count == 0 ||
445 count > MAX_GENERAL_ENTRY_SIZE || strlen(typeId) > MAX_KEY_STRING_LEN) {
446 return UDMF_E_INVALID_PARAM;
447 }
448 std::vector<uint8_t> recordValue(entry, entry + count);
449 auto obj = std::make_shared<Object>();
450 obj->value_[UNIFORM_DATA_TYPE] = typeId;
451 obj->value_[ARRAY_BUFFER] = recordValue;
452 obj->value_[ARRAY_BUFFER_LENGTH] = static_cast<int>(recordValue.size());
453 if (record->record_->GetType() == UD_BUTT) {
454 record->record_ = std::make_shared<ApplicationDefinedRecord>(APPLICATION_DEFINED_RECORD, obj);
455 record->record_->SetUtdId(typeId);
456 } else {
457 record->record_->AddEntry(typeId, obj);
458 }
459 record->recordDataLen = count;
460 return UDMF_E_OK;
461 }
462
GetValueFromUint8Array(OH_UdmfRecord * record,const char * typeId,ValueType value)463 static int GetValueFromUint8Array(OH_UdmfRecord *record, const char *typeId, ValueType value)
464 {
465 if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
466 LOG_ERROR(UDMF_CAPI, "valueType is not object sptr!");
467 return UDMF_ERR;
468 }
469 auto obj = std::get<std::shared_ptr<Object>>(value);
470 if (obj == nullptr) {
471 LOG_ERROR(UDMF_CAPI, "object is nullptr!");
472 return UDMF_ERR;
473 }
474 std::vector<uint8_t> recordValue;
475 int recordDataLen;
476 obj->GetValue(ARRAY_BUFFER, recordValue);
477 obj->GetValue(ARRAY_BUFFER_LENGTH, recordDataLen);
478 record->recordDataLen = recordValue.size();
479 if (record->recordDataLen > MAX_GENERAL_ENTRY_SIZE) {
480 LOG_INFO(UDMF_CAPI, "data size exceeds maximum size");
481 return UDMF_ERR;
482 }
483 record->recordData = new (std::nothrow) unsigned char[record->recordDataLen];
484 if (record->recordData == nullptr) {
485 return UDMF_ERR;
486 }
487 auto err = memcpy_s(record->recordData, record->recordDataLen, recordValue.data(), record->recordDataLen);
488 if (err != EOK) {
489 LOG_ERROR(UDMF_CAPI, "memcpy error! type:%{public}s", typeId);
490 return UDMF_ERR;
491 }
492 record->lastType = const_cast<char*>(typeId);
493 return UDMF_E_OK;
494 }
495
OH_UdmfRecord_GetGeneralEntry(OH_UdmfRecord * record,const char * typeId,unsigned char ** entry,unsigned int * count)496 int OH_UdmfRecord_GetGeneralEntry(OH_UdmfRecord* record, const char* typeId, unsigned char** entry, unsigned int* count)
497 {
498 if (!IsUnifiedRecordValid(record) || typeId == nullptr || entry == nullptr || count == nullptr) {
499 return UDMF_E_INVALID_PARAM;
500 }
501 std::lock_guard<std::mutex> lock(record->mutex);
502 if (!record->record_->HasType(typeId)) {
503 LOG_ERROR(UDMF_CAPI, "no type:%{public}s", typeId);
504 return UDMF_E_INVALID_PARAM;
505 }
506 if (record->lastType == typeId && record->recordData != nullptr) {
507 LOG_DEBUG(UDMF_CAPI, "return cache value");
508 *entry = record->recordData;
509 *count = record->recordDataLen;
510 return UDMF_E_OK;
511 }
512 record->record_->InitObject();
513 auto value = record->record_->GetEntry(typeId);
514 if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
515 LOG_ERROR(UDMF_CAPI, "Not contains right data type.");
516 return UDMF_ERR;
517 }
518 auto result = GetValueFromUint8Array(record, typeId, value);
519 if (result != UDMF_E_OK) {
520 LOG_ERROR(UDMF_CAPI, "Get value from valueType failed. typeId: %{public}s, result: %{public}d", typeId, result);
521 return result;
522 }
523 *count = record->recordDataLen;
524 *entry = record->recordData;
525 return UDMF_E_OK;
526 }
527
528 template<typename T>
AddUds(OH_UdmfRecord * record,UdsObject * udsObject,UDType type)529 void AddUds(OH_UdmfRecord* record, UdsObject* udsObject, UDType type)
530 {
531 if (record->record_->GetType() == UD_BUTT) {
532 record->record_ = std::make_shared<T>(type, udsObject->obj);
533 } else {
534 record->record_->AddEntry(UtdUtils::GetUtdIdFromUtdEnum(type), udsObject->obj);
535 }
536 }
537
OH_UdmfRecord_AddPlainText(OH_UdmfRecord * record,OH_UdsPlainText * plainText)538 int OH_UdmfRecord_AddPlainText(OH_UdmfRecord* record, OH_UdsPlainText* plainText)
539 {
540 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
541 return UDMF_E_INVALID_PARAM;
542 }
543 AddUds<PlainText>(record, plainText, PLAIN_TEXT);
544 return UDMF_E_OK;
545 }
546
OH_UdmfRecord_AddHyperlink(OH_UdmfRecord * record,OH_UdsHyperlink * hyperlink)547 int OH_UdmfRecord_AddHyperlink(OH_UdmfRecord* record, OH_UdsHyperlink* hyperlink)
548 {
549 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(hyperlink, UDS_HYPERLINK_STRUCT_ID)) {
550 return UDMF_E_INVALID_PARAM;
551 }
552 AddUds<Link>(record, hyperlink, HYPERLINK);
553 return UDMF_E_OK;
554 }
555
OH_UdmfRecord_AddHtml(OH_UdmfRecord * record,OH_UdsHtml * html)556 int OH_UdmfRecord_AddHtml(OH_UdmfRecord* record, OH_UdsHtml* html)
557 {
558 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
559 return UDMF_E_INVALID_PARAM;
560 }
561 AddUds<Html>(record, html, HTML);
562 return UDMF_E_OK;
563 }
564
OH_UdmfRecord_AddAppItem(OH_UdmfRecord * record,OH_UdsAppItem * appItem)565 int OH_UdmfRecord_AddAppItem(OH_UdmfRecord* record, OH_UdsAppItem* appItem)
566 {
567 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(appItem, UDS_APP_ITEM_STRUCT_ID)) {
568 return UDMF_E_INVALID_PARAM;
569 }
570 AddUds<SystemDefinedAppItem>(record, appItem, SYSTEM_DEFINED_APP_ITEM);
571 return UDMF_E_OK;
572 }
573
OH_UdmfRecord_AddFileUri(OH_UdmfRecord * record,OH_UdsFileUri * fileUri)574 int OH_UdmfRecord_AddFileUri(OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
575 {
576 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(fileUri, UDS_FILE_URI_STRUCT_ID)) {
577 return UDMF_E_INVALID_PARAM;
578 }
579 std::string* fileType = std::get_if<std::string>(&(fileUri->obj->value_[FILE_TYPE]));
580 if (fileType == nullptr) {
581 return UDMF_ERR;
582 }
583 std::map<UDType, std::function<void(OH_UdmfRecord*, OH_UdsFileUri*)>> addFileUriFuncs = {
584 {UDType::FILE, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
585 { AddUds<File>(record, fileUri, UDType::FILE); }},
586 {UDType::IMAGE, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
587 { AddUds<Image>(record, fileUri, UDType::IMAGE); }},
588 {UDType::VIDEO, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
589 { AddUds<Video>(record, fileUri, UDType::VIDEO); }},
590 {UDType::AUDIO, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
591 { AddUds<Audio>(record, fileUri, UDType::AUDIO); }},
592 {UDType::FOLDER, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
593 { AddUds<Folder>(record, fileUri, UDType::FOLDER); }},
594 };
595 int32_t utdId = UDType::FILE;
596 std::shared_ptr<TypeDescriptor> descriptor;
597 UtdClient::GetInstance().GetTypeDescriptor(*fileType, descriptor);
598 if (descriptor != nullptr) {
599 bool isFileType = false;
600 for (const auto &fileSub : FILE_SUB_TYPES) {
601 descriptor->BelongsTo(fileSub, isFileType);
602 if (isFileType) {
603 utdId = static_cast<UDType>(UtdUtils::GetUtdEnumFromUtdId(*fileType));
604 break;
605 }
606 }
607 }
608 addFileUriFuncs[static_cast<UDType>(utdId)](record, fileUri);
609 return UDMF_E_OK;
610 }
611
OH_UdmfRecord_AddPixelMap(OH_UdmfRecord * record,OH_UdsPixelMap * pixelMap)612 int OH_UdmfRecord_AddPixelMap(OH_UdmfRecord* record, OH_UdsPixelMap* pixelMap)
613 {
614 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(pixelMap, UDS_PIXEL_MAP_STRUCT_ID)) {
615 return UDMF_E_INVALID_PARAM;
616 }
617 AddUds<SystemDefinedPixelMap>(record, pixelMap, UDType::SYSTEM_DEFINED_PIXEL_MAP);
618 return UDMF_E_OK;
619 }
620
GetUds(OH_UdmfRecord * record,UdsObject * udsObject,UDType type)621 int GetUds(OH_UdmfRecord* record, UdsObject* udsObject, UDType type)
622 {
623 record->record_->InitObject();
624 auto value = record->record_->GetEntry(UtdUtils::GetUtdIdFromUtdEnum(type));
625 if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
626 return UDMF_ERR;
627 }
628 udsObject->obj = std::get<std::shared_ptr<Object>>(value);
629 return UDMF_E_OK;
630 }
631
OH_UdmfRecord_AddArrayBuffer(OH_UdmfRecord * record,const char * type,OH_UdsArrayBuffer * buffer)632 int OH_UdmfRecord_AddArrayBuffer(OH_UdmfRecord* record, const char* type, OH_UdsArrayBuffer* buffer)
633 {
634 if (!IsUnifiedRecordValid(record) || type == nullptr || strlen(type) > MAX_KEY_STRING_LEN ||
635 IsInvalidUdsObjectPtr(buffer, UDS_ARRAY_BUFFER_STRUCT_ID)) {
636 return UDMF_E_INVALID_PARAM;
637 }
638 unsigned char *entry;
639 unsigned int size;
640 int ret = OH_UdsArrayBuffer_GetData(buffer, &entry, &size);
641 if (ret != UDMF_E_OK) {
642 return UDMF_E_INVALID_PARAM;
643 }
644 buffer->obj->value_[UNIFORM_DATA_TYPE] = type;
645 return OH_UdmfRecord_AddGeneralEntry(record, type, entry, size);
646 }
647
OH_UdmfRecord_AddContentForm(OH_UdmfRecord * record,OH_UdsContentForm * contentForm)648 int OH_UdmfRecord_AddContentForm(OH_UdmfRecord* record, OH_UdsContentForm* contentForm)
649 {
650 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(contentForm, UDS_CONTENT_FORM_STRUCT_ID)) {
651 return UDMF_E_INVALID_PARAM;
652 }
653 AddUds<UnifiedRecord>(record, contentForm, UDType::CONTENT_FORM);
654 return UDMF_E_OK;
655 }
656
OH_UdmfRecord_GetPlainText(OH_UdmfRecord * record,OH_UdsPlainText * plainText)657 int OH_UdmfRecord_GetPlainText(OH_UdmfRecord* record, OH_UdsPlainText* plainText)
658 {
659 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
660 return UDMF_E_INVALID_PARAM;
661 }
662 return GetUds(record, plainText, PLAIN_TEXT);
663 }
664
OH_UdmfRecord_GetHyperlink(OH_UdmfRecord * record,OH_UdsHyperlink * hyperlink)665 int OH_UdmfRecord_GetHyperlink(OH_UdmfRecord* record, OH_UdsHyperlink* hyperlink)
666 {
667 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(hyperlink, UDS_HYPERLINK_STRUCT_ID)) {
668 return UDMF_E_INVALID_PARAM;
669 }
670 return GetUds(record, hyperlink, HYPERLINK);
671 }
672
OH_UdmfRecord_GetHtml(OH_UdmfRecord * record,OH_UdsHtml * html)673 int OH_UdmfRecord_GetHtml(OH_UdmfRecord* record, OH_UdsHtml* html)
674 {
675 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
676 return UDMF_E_INVALID_PARAM;
677 }
678 return GetUds(record, html, HTML);
679 }
680
OH_UdmfRecord_GetAppItem(OH_UdmfRecord * record,OH_UdsAppItem * appItem)681 int OH_UdmfRecord_GetAppItem(OH_UdmfRecord* record, OH_UdsAppItem* appItem)
682 {
683 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(appItem, UDS_APP_ITEM_STRUCT_ID)) {
684 return UDMF_E_INVALID_PARAM;
685 }
686 return GetUds(record, appItem, SYSTEM_DEFINED_APP_ITEM);
687 }
688
OH_UdmfRecord_GetFileUri(OH_UdmfRecord * record,OH_UdsFileUri * fileUri)689 int OH_UdmfRecord_GetFileUri(OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
690 {
691 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(fileUri, UDS_FILE_URI_STRUCT_ID)) {
692 return UDMF_E_INVALID_PARAM;
693 }
694 if (GetUds(record, fileUri, UDType::FILE_URI) == UDMF_E_OK) {
695 return UDMF_E_OK;
696 }
697 for (auto fileType : FILE_TYPES) {
698 int ret = GetUds(record, fileUri, fileType.second);
699 if (ret == UDMF_E_OK) {
700 fileUri->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_GENERAL_FILE_URI;
701 fileUri->obj->value_[FILE_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(fileType.second);
702 return UDMF_E_OK;
703 }
704 }
705 LOG_ERROR(UDMF_CAPI, "could't find file uri");
706 return UDMF_E_INVALID_PARAM;
707 }
708
OH_UdmfRecord_GetPixelMap(OH_UdmfRecord * record,OH_UdsPixelMap * pixelMap)709 int OH_UdmfRecord_GetPixelMap(OH_UdmfRecord* record, OH_UdsPixelMap* pixelMap)
710 {
711 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(pixelMap, UDS_PIXEL_MAP_STRUCT_ID)) {
712 return UDMF_E_INVALID_PARAM;
713 }
714 return GetUds(record, pixelMap, UDType::SYSTEM_DEFINED_PIXEL_MAP);
715 }
716
OH_UdmfRecord_GetArrayBuffer(OH_UdmfRecord * record,const char * type,OH_UdsArrayBuffer * buffer)717 int OH_UdmfRecord_GetArrayBuffer(OH_UdmfRecord* record, const char* type, OH_UdsArrayBuffer* buffer)
718 {
719 unsigned int size = 0;
720 unsigned char *entry;
721 int ret = OH_UdmfRecord_GetGeneralEntry(record, type, &entry, &size);
722 if (ret != UDMF_E_OK) {
723 LOG_ERROR(UDMF_CAPI, "OH_UdmfRecord_GetGeneralEntry ret: %{public}d.", ret);
724 return ret;
725 }
726 return OH_UdsArrayBuffer_SetData(buffer, entry, size);
727 }
728
OH_UdmfRecord_GetContentForm(OH_UdmfRecord * record,OH_UdsContentForm * contentForm)729 int OH_UdmfRecord_GetContentForm(OH_UdmfRecord* record, OH_UdsContentForm* contentForm)
730 {
731 if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(contentForm, UDS_CONTENT_FORM_STRUCT_ID)) {
732 return UDMF_E_INVALID_PARAM;
733 }
734 return GetUds(record, contentForm, UDType::CONTENT_FORM);
735 }
736
OH_UdmfProperty_Create(OH_UdmfData * data)737 OH_UdmfProperty* OH_UdmfProperty_Create(OH_UdmfData* data)
738 {
739 if (!IsUnifiedDataValid(data)) {
740 return nullptr;
741 }
742 OH_UdmfProperty* properties = new (std::nothrow) OH_UdmfProperty;
743 if (properties == nullptr) {
744 return nullptr;
745 }
746 properties->properties_ = data->unifiedData_->GetProperties();
747 return properties;
748 }
749
OH_UdmfProperty_Destroy(OH_UdmfProperty * properties)750 void OH_UdmfProperty_Destroy(OH_UdmfProperty* properties)
751 {
752 if (properties != nullptr) {
753 delete properties;
754 }
755 }
756
OH_UdmfProperty_GetTag(OH_UdmfProperty * properties)757 const char* OH_UdmfProperty_GetTag(OH_UdmfProperty* properties)
758 {
759 if (!IsUnifiedPropertiesValid(properties)) {
760 return nullptr;
761 }
762 return properties->properties_->tag.c_str();
763 }
764
OH_UdmfProperty_GetTimestamp(OH_UdmfProperty * properties)765 int64_t OH_UdmfProperty_GetTimestamp(OH_UdmfProperty* properties)
766 {
767 if (!IsUnifiedPropertiesValid(properties)) {
768 return -1;
769 }
770 return properties->properties_->timestamp;
771 }
772
OH_UdmfProperty_GetShareOption(OH_UdmfProperty * properties)773 Udmf_ShareOption OH_UdmfProperty_GetShareOption(OH_UdmfProperty* properties)
774 {
775 if (!IsUnifiedPropertiesValid(properties)) {
776 return Udmf_ShareOption::SHARE_OPTIONS_INVALID;
777 }
778 switch (properties->properties_->shareOptions) {
779 case ShareOptions::IN_APP:
780 return Udmf_ShareOption::SHARE_OPTIONS_IN_APP;
781 case ShareOptions::CROSS_APP:
782 case ShareOptions::CROSS_DEVICE:
783 return Udmf_ShareOption::SHARE_OPTIONS_CROSS_APP;
784 default:
785 return Udmf_ShareOption::SHARE_OPTIONS_INVALID;
786 }
787 }
788
OH_UdmfProperty_GetExtrasIntParam(OH_UdmfProperty * properties,const char * key,const int defaultValue)789 int OH_UdmfProperty_GetExtrasIntParam(OH_UdmfProperty* properties, const char* key, const int defaultValue)
790 {
791 return (IsUnifiedPropertiesValid(properties) && key != nullptr) ?
792 properties->properties_->extras.GetIntParam(key, defaultValue) : defaultValue;
793 }
794
OH_UdmfProperty_GetExtrasStringParam(OH_UdmfProperty * properties,const char * key)795 const char* OH_UdmfProperty_GetExtrasStringParam(OH_UdmfProperty* properties, const char* key)
796 {
797 if (!IsUnifiedPropertiesValid(properties) || key == nullptr) {
798 return nullptr;
799 }
800 properties->extraStr = properties->properties_->extras.GetStringParam(key);
801 return properties->extraStr.c_str();
802 }
803
OH_UdmfProperty_SetTag(OH_UdmfProperty * properties,const char * tag)804 int OH_UdmfProperty_SetTag(OH_UdmfProperty* properties, const char* tag)
805 {
806 if (!IsUnifiedPropertiesValid(properties) || tag == nullptr) {
807 return UDMF_E_INVALID_PARAM;
808 }
809 properties->properties_->tag = tag;
810 return UDMF_E_OK;
811 }
812
OH_UdmfProperty_SetShareOption(OH_UdmfProperty * properties,Udmf_ShareOption option)813 int OH_UdmfProperty_SetShareOption(OH_UdmfProperty* properties, Udmf_ShareOption option)
814 {
815 if (!IsUnifiedPropertiesValid(properties)) {
816 return UDMF_E_INVALID_PARAM;
817 }
818 std::lock_guard<std::mutex> lock(properties->mutex);
819 switch (option) {
820 case Udmf_ShareOption::SHARE_OPTIONS_IN_APP:
821 properties->properties_->shareOptions = ShareOptions::IN_APP;
822 break;
823 case Udmf_ShareOption::SHARE_OPTIONS_CROSS_APP:
824 properties->properties_->shareOptions = ShareOptions::CROSS_APP;
825 break;
826 default:
827 return UDMF_E_INVALID_PARAM;
828 }
829 return UDMF_E_OK;
830 }
831
OH_UdmfProperty_SetExtrasIntParam(OH_UdmfProperty * properties,const char * key,int param)832 int OH_UdmfProperty_SetExtrasIntParam(OH_UdmfProperty* properties, const char* key, int param)
833 {
834 if (!IsUnifiedPropertiesValid(properties) || key == nullptr) {
835 return UDMF_E_INVALID_PARAM;
836 }
837 std::lock_guard<std::mutex> lock(properties->mutex);
838 properties->properties_->extras.SetParam(key, OHOS::AAFwk::Integer::Box(param));
839 return UDMF_E_OK;
840 }
841
OH_UdmfProperty_SetExtrasStringParam(OH_UdmfProperty * properties,const char * key,const char * param)842 int OH_UdmfProperty_SetExtrasStringParam(OH_UdmfProperty* properties, const char* key, const char* param)
843 {
844 if (!IsUnifiedPropertiesValid(properties) || key == nullptr || param == nullptr) {
845 return UDMF_E_INVALID_PARAM;
846 }
847 std::lock_guard<std::mutex> lock(properties->mutex);
848 properties->properties_->extras.SetParam(key, OHOS::AAFwk::String::Box(param));
849 return UDMF_E_OK;
850 }
851
OH_UdmfRecordProvider_Create()852 OH_UdmfRecordProvider* OH_UdmfRecordProvider_Create()
853 {
854 OH_UdmfRecordProvider* provider = new (std::nothrow) OH_UdmfRecordProvider();
855 if (provider == nullptr) {
856 LOG_ERROR(UDMF_CAPI, "allocate OH_UdmfRecordProvider memory fail");
857 }
858 return provider;
859 }
860
OH_UdmfRecordProvider_Destroy(OH_UdmfRecordProvider * provider)861 int OH_UdmfRecordProvider_Destroy(OH_UdmfRecordProvider* provider)
862 {
863 if (provider == nullptr) {
864 return UDMF_E_INVALID_PARAM;
865 }
866 if (provider->context != nullptr && provider->finalize != nullptr) {
867 (provider->finalize)(provider->context);
868 LOG_INFO(UDMF_CAPI, "free context finished");
869 }
870 delete provider;
871 return UDMF_E_OK;
872 }
873
OH_UdmfRecordProvider_SetData(OH_UdmfRecordProvider * provider,void * context,const OH_UdmfRecordProvider_GetData callback,const UdmfData_Finalize finalize)874 int OH_UdmfRecordProvider_SetData(OH_UdmfRecordProvider* provider, void* context,
875 const OH_UdmfRecordProvider_GetData callback, const UdmfData_Finalize finalize)
876 {
877 if (provider == nullptr || callback == nullptr) {
878 return UDMF_E_INVALID_PARAM;
879 }
880 provider->callback = callback;
881 if (context != nullptr && finalize == nullptr) {
882 LOG_ERROR(UDMF_CAPI, "finalize function is null when context not null");
883 return UDMF_E_INVALID_PARAM;
884 }
885 provider->context = context;
886 provider->finalize = finalize;
887 return UDMF_E_OK;
888 }
889
OH_UdmfRecord_SetProvider(OH_UdmfRecord * record,const char * const * types,unsigned int count,OH_UdmfRecordProvider * provider)890 int OH_UdmfRecord_SetProvider(OH_UdmfRecord* record, const char* const* types, unsigned int count,
891 OH_UdmfRecordProvider* provider)
892 {
893 if (!IsUnifiedRecordValid(record) || types == nullptr || count == 0 || provider == nullptr) {
894 return UDMF_E_INVALID_PARAM;
895 }
896 std::shared_ptr<DataProviderImpl> providerBox = std::make_shared<DataProviderImpl>();
897 providerBox->SetInnerProvider(provider);
898 std::vector<std::string> udTypes;
899 std::set<std::string> udTypeSet;
900 for (unsigned int i = 0; i < count; ++i) {
901 if (types[i] == nullptr) {
902 LOG_ERROR(UDMF_CAPI, "The type with index %{public}d is empty", i);
903 continue;
904 }
905 if (udTypeSet.count(types[i]) == 0) {
906 udTypeSet.emplace(types[i]);
907 udTypes.emplace_back(types[i]);
908 }
909 }
910 record->record_->SetEntryGetter(udTypes, providerBox);
911 return UDMF_E_OK;
912 }
913
OH_UdmfProgressInfo_GetProgress(OH_Udmf_ProgressInfo * progressInfo)914 int OH_UdmfProgressInfo_GetProgress(OH_Udmf_ProgressInfo* progressInfo)
915 {
916 return progressInfo->progress;
917 }
918
OH_UdmfProgressInfo_GetStatus(OH_Udmf_ProgressInfo * progressInfo)919 int OH_UdmfProgressInfo_GetStatus(OH_Udmf_ProgressInfo* progressInfo)
920 {
921 return progressInfo->status;
922 }
923
OH_UdmfGetDataParams_Create()924 OH_UdmfGetDataParams* OH_UdmfGetDataParams_Create()
925 {
926 OH_UdmfGetDataParams *params = new (std::nothrow) OH_UdmfGetDataParams();
927 if (params == nullptr) {
928 LOG_ERROR(UDMF_CAPI, "allocate OH_UdmfGetDataParams memory fail");
929 return nullptr;
930 }
931 return params;
932 }
933
OH_UdmfGetDataParams_Destroy(OH_UdmfGetDataParams * pThis)934 void OH_UdmfGetDataParams_Destroy(OH_UdmfGetDataParams* pThis)
935 {
936 if (pThis == nullptr) {
937 return;
938 }
939 delete pThis;
940 }
941
OH_UdmfGetDataParams_SetDestUri(OH_UdmfGetDataParams * params,const char * destUri)942 void OH_UdmfGetDataParams_SetDestUri(OH_UdmfGetDataParams* params, const char* destUri)
943 {
944 if (destUri == nullptr) {
945 return;
946 }
947 params->destUri = std::string(destUri);
948 }
949
OH_UdmfGetDataParams_SetFileConflictOptions(OH_UdmfGetDataParams * params,const Udmf_FileConflictOptions options)950 void OH_UdmfGetDataParams_SetFileConflictOptions(OH_UdmfGetDataParams* params, const Udmf_FileConflictOptions options)
951 {
952 if (params == nullptr) {
953 return;
954 }
955 params->fileConflictOptions = options;
956 }
957
OH_UdmfGetDataParams_SetProgressIndicator(OH_UdmfGetDataParams * params,const Udmf_ProgressIndicator progressIndicator)958 void OH_UdmfGetDataParams_SetProgressIndicator(OH_UdmfGetDataParams* params,
959 const Udmf_ProgressIndicator progressIndicator)
960 {
961 if (params == nullptr) {
962 return;
963 }
964 params->progressIndicator = progressIndicator;
965 }
966
OH_UdmfGetDataParams_SetDataProgressListener(OH_UdmfGetDataParams * params,const OH_Udmf_DataProgressListener dataProgressListener)967 void OH_UdmfGetDataParams_SetDataProgressListener(OH_UdmfGetDataParams* params,
968 const OH_Udmf_DataProgressListener dataProgressListener)
969 {
970 if (params == nullptr) {
971 return;
972 }
973 params->dataProgressListener = dataProgressListener;
974 }