• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "application_defined_record.h"
18 #include "audio.h"
19 #include "data_provider_impl.h"
20 #include "file.h"
21 #include "folder.h"
22 #include "html.h"
23 #include "image.h"
24 #include "int_wrapper.h"
25 #include "link.h"
26 #include "logger.h"
27 #include "ndk_data_conversion.h"
28 #include "plain_text.h"
29 #include "securec.h"
30 #include "string_wrapper.h"
31 #include "system_defined_appitem.h"
32 #include "system_defined_pixelmap.h"
33 #include "udmf_capi_common.h"
34 #include "udmf_client.h"
35 #include "udmf_err_code.h"
36 #include "udmf_meta.h"
37 #include "unified_meta.h"
38 #include "utd.h"
39 #include "utd_client.h"
40 #include "video.h"
41 
42 using namespace OHOS::UDMF;
43 
44 static constexpr uint64_t MAX_RECORDS_COUNT = 4 * 1024 * 1024;
45 static constexpr uint64_t MAX_KEY_STRING_LEN = 1 * 1024 * 1024;
46 static constexpr uint64_t MAX_TYPES_COUNT = 10 * 1024;
47 static const std::map<std::string, UDType> FILE_TYPES = {
48     { UDMF_META_GENERAL_FILE, UDType::FILE },
49     { UDMF_META_AUDIO, UDType::AUDIO },
50     { UDMF_META_FOLDER, UDType::FOLDER },
51     { UDMF_META_IMAGE, UDType::IMAGE },
52     { UDMF_META_VIDEO, UDType::VIDEO }
53 };
54 
55 static const std::map<Udmf_Intention, Intention> VAILD_INTENTIONS = {
56     { UDMF_INTENTION_DRAG, Intention::UD_INTENTION_DRAG },
57     { UDMF_INTENTION_SYSTEM_SHARE, Intention::UD_INTENTION_SYSTEM_SHARE },
58     { UDMF_INTENTION_PICKER, Intention::UD_INTENTION_PICKER },
59     { UDMF_INTENTION_MENU, Intention::UD_INTENTION_MENU }
60 };
61 
DestroyUnifiedRecordArray(OH_UdmfRecord ** & records,unsigned int & count)62 static void DestroyUnifiedRecordArray(OH_UdmfRecord**& records, unsigned int& count)
63 {
64     if (records == nullptr) {
65         return;
66     }
67     for (unsigned int i = 0; i < count; i++) {
68         if (records[i] != nullptr) {
69             if (records[i]->recordData != nullptr) {
70                 delete records[i]->recordData;
71                 records[i]->recordData = nullptr;
72             }
73             NdkDataConversion::DestroyStringArray(records[i]->typesArray, records[i]->typesCount);
74             delete records[i];
75             records[i] = nullptr;
76         }
77     }
78     delete[] records;
79     records = nullptr;
80     count = 0;
81 }
82 
CreateUnifiedDataRecordsArray(OH_UdmfData * unifiedData,std::vector<std::shared_ptr<UnifiedRecord>> & records)83 static OH_UdmfRecord** CreateUnifiedDataRecordsArray(OH_UdmfData* unifiedData,
84                                                      std::vector<std::shared_ptr<UnifiedRecord>>& records)
85 {
86     unsigned int size = static_cast<unsigned int>(records.size());
87     if (unifiedData == nullptr || size == 0 || size > MAX_RECORDS_COUNT) {
88         return nullptr;
89     }
90     OH_UdmfRecord** result = new (std::nothrow) OH_UdmfRecord* [size] { nullptr };
91     if (result == nullptr) {
92         return nullptr;
93     }
94     for (unsigned int i = 0; i < size; i++) {
95         result[i] = new (std::nothrow) OH_UdmfRecord;
96         if (result[i] == nullptr) {
97             DestroyUnifiedRecordArray(result, size);
98             return nullptr;
99         }
100         result[i]->record_ = records[i];
101     }
102     unifiedData->records = result;
103     unifiedData->recordsCount = size;
104     return unifiedData->records;
105 }
106 
IsUnifiedDataValid(OH_UdmfData * data)107 static bool IsUnifiedDataValid(OH_UdmfData* data)
108 {
109     return data != nullptr && data->unifiedData_ != nullptr &&
110            data->cid == NdkStructId::UDMF_UNIFIED_DATA_STRUCT_ID;
111 }
112 
IsUnifiedRecordValid(OH_UdmfRecord * record)113 static bool IsUnifiedRecordValid(OH_UdmfRecord* record)
114 {
115     return record != nullptr && record->record_ != nullptr &&
116            record->cid == NdkStructId::UDMF_UNIFIED_RECORD_STRUCT_ID;
117 }
118 
IsUnifiedPropertiesValid(OH_UdmfProperty * properties)119 static bool IsUnifiedPropertiesValid(OH_UdmfProperty* properties)
120 {
121     return properties != nullptr && properties->properties_ != nullptr &&
122            properties->cid == NdkStructId::UDMF_UNIFIED_DATA_PROPERTIES_ID;
123 }
124 
AddFileUriTypeIfContains(std::vector<std::string> & types)125 static void AddFileUriTypeIfContains(std::vector<std::string>& types)
126 {
127     if (std::find(types.begin(), types.end(), UDMF_META_GENERAL_FILE_URI) != types.end()) {
128         return;
129     }
130     for (auto type : types) {
131         if (FILE_TYPES.find(type) != FILE_TYPES.end()) {
132             types.push_back(UDMF_META_GENERAL_FILE_URI);
133             break;
134         }
135     }
136     return;
137 }
138 
OH_UdmfData_Create()139 OH_UdmfData* OH_UdmfData_Create()
140 {
141     OH_UdmfData* data = new (std::nothrow) OH_UdmfData;
142     if (data == nullptr) {
143         return nullptr;
144     }
145     data->unifiedData_ = std::make_shared<UnifiedData>();
146     return data;
147 }
148 
OH_UdmfData_Destroy(OH_UdmfData * data)149 void OH_UdmfData_Destroy(OH_UdmfData* data)
150 {
151     if (data == nullptr) {
152         return;
153     }
154     {
155         std::lock_guard<std::mutex> lock(data->mutex);
156         NdkDataConversion::DestroyStringArray(data->typesArray, data->typesCount);
157         DestroyUnifiedRecordArray(data->records, data->recordsCount);
158     }
159     delete data;
160 }
161 
OH_UdmfData_AddRecord(OH_UdmfData * unifiedData,OH_UdmfRecord * record)162 int OH_UdmfData_AddRecord(OH_UdmfData* unifiedData, OH_UdmfRecord* record)
163 {
164     if (!IsUnifiedDataValid(unifiedData) || !IsUnifiedRecordValid(record)) {
165         return UDMF_E_INVALID_PARAM;
166     }
167     unifiedData->unifiedData_->AddRecord(record->record_);
168     return UDMF_E_OK;
169 }
170 
OH_UdmfData_HasType(OH_UdmfData * unifiedData,const char * type)171 bool OH_UdmfData_HasType(OH_UdmfData* unifiedData, const char* type)
172 {
173     return IsUnifiedDataValid(unifiedData) && type != nullptr && unifiedData->unifiedData_->HasTypeInEntries(type);
174 }
175 
OH_UdmfData_GetTypes(OH_UdmfData * unifiedData,unsigned int * count)176 char** OH_UdmfData_GetTypes(OH_UdmfData* unifiedData, unsigned int* count)
177 {
178     if (!IsUnifiedDataValid(unifiedData) || count == nullptr) {
179         return nullptr;
180     }
181     std::lock_guard<std::mutex> lock(unifiedData->mutex);
182     if (unifiedData->typesArray != nullptr) {
183         LOG_DEBUG(UDMF_CAPI, "return cache value");
184         *count = unifiedData->typesCount;
185         return unifiedData->typesArray;
186     }
187     std::vector<std::string> typeLabels = unifiedData->unifiedData_->GetEntriesTypes();
188     AddFileUriTypeIfContains(typeLabels);
189     unifiedData->typesArray = NdkDataConversion::StrVectorToTypesArray(typeLabels);
190     unifiedData->typesArray == nullptr ? unifiedData->typesCount = 0 : unifiedData->typesCount = typeLabels.size();
191     *count = unifiedData->typesCount;
192     return unifiedData->typesArray;
193 }
194 
OH_UdmfRecord_GetTypes(OH_UdmfRecord * record,unsigned int * count)195 char** OH_UdmfRecord_GetTypes(OH_UdmfRecord* record, unsigned int* count)
196 {
197     if (!IsUnifiedRecordValid(record) || count == nullptr) {
198         return nullptr;
199     }
200     std::lock_guard<std::mutex> lock(record->mutex);
201     if (record->typesArray != nullptr) {
202         LOG_DEBUG(UDMF_CAPI, "return cache value");
203         *count = record->typesCount;
204         return record->typesArray;
205     }
206     auto types = record->record_->GetUtdIdsWithAddFileType(false);
207     std::vector<std::string> typeLabels {types.begin(), types.end()};
208     AddFileUriTypeIfContains(typeLabels);
209     record->typesArray = NdkDataConversion::StrVectorToTypesArray(typeLabels);
210     record->typesArray == nullptr ? record->typesCount = 0 : record->typesCount = typeLabels.size();
211     *count = record->typesCount;
212     return record->typesArray;
213 }
214 
OH_UdmfData_GetRecords(OH_UdmfData * unifiedData,unsigned int * count)215 OH_UdmfRecord** OH_UdmfData_GetRecords(OH_UdmfData* unifiedData, unsigned int* count)
216 {
217     if (!IsUnifiedDataValid(unifiedData) || count == nullptr) {
218         return nullptr;
219     }
220     std::lock_guard<std::mutex> lock(unifiedData->mutex);
221     if (unifiedData->records != nullptr) {
222         LOG_DEBUG(UDMF_CAPI, "return cache value");
223         *count = unifiedData->recordsCount;
224         return unifiedData->records;
225     }
226     std::vector<std::shared_ptr<UnifiedRecord>> records = unifiedData->unifiedData_->GetRecords();
227     CreateUnifiedDataRecordsArray(unifiedData, records);
228     *count = unifiedData->recordsCount;
229     return unifiedData->records;
230 }
231 
GetFirstPlainText(OH_UdmfRecord ** records,unsigned int recordCount,OH_UdsPlainText * plainText)232 static int GetFirstPlainText(OH_UdmfRecord **records, unsigned int recordCount, OH_UdsPlainText* plainText)
233 {
234     int ret = UDMF_ERR;
235     if (records == nullptr || recordCount == 0) {
236         return ret;
237     }
238     for (unsigned int i = 0; i < recordCount; i++) {
239         const char *type = OH_UdsPlainText_GetType(plainText);
240         if (type == nullptr || !records[i]->record_->HasType(type)) {
241             continue;
242         }
243         ret = OH_UdmfRecord_GetPlainText(records[i], plainText);
244         if (ret == UDMF_E_OK) {
245             return ret;
246         }
247     }
248     return ret;
249 }
250 
GetFirstHtml(OH_UdmfRecord ** records,unsigned int recordCount,OH_UdsHtml * html)251 static int GetFirstHtml(OH_UdmfRecord **records, unsigned int recordCount, OH_UdsHtml* html)
252 {
253     int ret = UDMF_ERR;
254     if (records == nullptr || recordCount == 0) {
255         return ret;
256     }
257     for (unsigned int i = 0; i < recordCount; i++) {
258         const char *type = OH_UdsHtml_GetType(html);
259         if (type == nullptr || !records[i]->record_->HasType(type)) {
260             continue;
261         }
262         ret = OH_UdmfRecord_GetHtml(records[i], html);
263         if (ret == UDMF_E_OK) {
264             return ret;
265         }
266     }
267     return ret;
268 }
269 
OH_UdmfData_GetPrimaryPlainText(OH_UdmfData * data,OH_UdsPlainText * plainText)270 int OH_UdmfData_GetPrimaryPlainText(OH_UdmfData* data, OH_UdsPlainText* plainText)
271 {
272     if (!IsUnifiedDataValid(data) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
273         return UDMF_E_INVALID_PARAM;
274     }
275     std::lock_guard<std::mutex> lock(data->mutex);
276     if (data->records == nullptr) {
277         LOG_DEBUG(UDMF_CAPI, "no cache value");
278         std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
279         CreateUnifiedDataRecordsArray(data, records);
280     }
281 
282     return GetFirstPlainText(data->records, data->recordsCount, plainText);
283 }
284 
OH_UdmfData_GetPrimaryHtml(OH_UdmfData * data,OH_UdsHtml * html)285 int OH_UdmfData_GetPrimaryHtml(OH_UdmfData* data, OH_UdsHtml* html)
286 {
287     if (!IsUnifiedDataValid(data) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
288         return UDMF_E_INVALID_PARAM;
289     }
290     std::lock_guard<std::mutex> lock(data->mutex);
291     if (data->records == nullptr) {
292         LOG_DEBUG(UDMF_CAPI, "no cache value");
293         std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
294         CreateUnifiedDataRecordsArray(data, records);
295     }
296 
297     return GetFirstHtml(data->records, data->recordsCount, html);
298 }
299 
OH_UdmfData_GetRecordCount(OH_UdmfData * data)300 int OH_UdmfData_GetRecordCount(OH_UdmfData *data)
301 {
302     if (!IsUnifiedDataValid(data)) {
303         return 0;
304     }
305     std::lock_guard<std::mutex> lock(data->mutex);
306     if (data->records == nullptr) {
307         LOG_DEBUG(UDMF_CAPI, "no cache value");
308         std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
309         CreateUnifiedDataRecordsArray(data, records);
310     }
311     return static_cast<int>(data->recordsCount);
312 }
313 
OH_UdmfData_GetRecord(OH_UdmfData * data,unsigned int index)314 OH_UdmfRecord* OH_UdmfData_GetRecord(OH_UdmfData* data, unsigned int index)
315 {
316     if (!IsUnifiedDataValid(data)) {
317         return nullptr;
318     }
319     std::lock_guard<std::mutex> lock(data->mutex);
320     if (data->records == nullptr) {
321         LOG_DEBUG(UDMF_CAPI, "no cache value");
322         std::vector<std::shared_ptr<UnifiedRecord>> records = data->unifiedData_->GetRecords();
323         CreateUnifiedDataRecordsArray(data, records);
324     }
325     if (index >= data->recordsCount || data->records == nullptr) {
326         return nullptr;
327     }
328     return data->records[index];
329 }
330 
OH_UdmfData_IsLocal(OH_UdmfData * data)331 bool OH_UdmfData_IsLocal(OH_UdmfData* data)
332 {
333     if (!IsUnifiedDataValid(data) || data->unifiedData_->GetProperties() == nullptr) {
334         return true;
335     }
336     bool isRemote = data->unifiedData_->GetProperties()->isRemote;
337     return !isRemote;
338 }
339 
OH_UdmfOptions_Create()340 OH_UdmfOptions* OH_UdmfOptions_Create()
341 {
342     OH_UdmfOptions* options = new (std::nothrow) OH_UdmfOptions;
343     if (options == nullptr) {
344         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions create error");
345         return nullptr;
346     }
347     return options;
348 }
349 
OH_UdmfOptions_Destroy(OH_UdmfOptions * pThis)350 void OH_UdmfOptions_Destroy(OH_UdmfOptions* pThis)
351 {
352     if (pThis != nullptr) {
353         delete pThis;
354         pThis = nullptr;
355     }
356 }
357 
OH_UdmfOptions_GetKey(OH_UdmfOptions * pThis)358 const char* OH_UdmfOptions_GetKey(OH_UdmfOptions* pThis)
359 {
360     if (pThis == nullptr) {
361         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions get key error, invaild params!");
362         return nullptr;
363     }
364     return pThis->key.c_str();
365 }
366 
OH_UdmfOptions_SetKey(OH_UdmfOptions * pThis,const char * key)367 int OH_UdmfOptions_SetKey(OH_UdmfOptions* pThis, const char* key)
368 {
369     if (pThis == nullptr || key == nullptr) {
370         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions set key error, invaild params!");
371         return UDMF_E_INVALID_PARAM;
372     }
373     std::string keyStr(key);
374     if (keyStr.length() > UDMF_KEY_BUFFER_LEN) {
375         LOG_ERROR(UDMF_CAPI, "key length exceeds maximum length, length is %{public}zu", keyStr.length());
376         return UDMF_E_INVALID_PARAM;
377     }
378     pThis->key = keyStr;
379     return UDMF_E_OK;
380 }
381 
OH_UdmfOptions_GetIntention(OH_UdmfOptions * pThis)382 Udmf_Intention OH_UdmfOptions_GetIntention(OH_UdmfOptions* pThis)
383 {
384     if (pThis == nullptr) {
385         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions get intention error, invaild params!");
386         return Udmf_Intention {};
387     }
388     return pThis->intention;
389 }
390 
OH_UdmfOptions_SetIntention(OH_UdmfOptions * pThis,Udmf_Intention intention)391 int OH_UdmfOptions_SetIntention(OH_UdmfOptions* pThis, Udmf_Intention intention)
392 {
393     if (pThis == nullptr) {
394         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions set intention error, invaild params!");
395         return UDMF_E_INVALID_PARAM;
396     }
397 
398     pThis->intention = intention;
399     return UDMF_E_OK;
400 }
401 
OH_UdmfOptions_Reset(OH_UdmfOptions * pThis)402 int OH_UdmfOptions_Reset(OH_UdmfOptions* pThis)
403 {
404     if (pThis == nullptr) {
405         LOG_ERROR(UDMF_CAPI, "The OH_UdmfOptions reset error, invaild params!");
406         return UDMF_E_INVALID_PARAM;
407     }
408     pThis->key.clear();
409     pThis->intention = Udmf_Intention {};
410     return UDMF_E_OK;
411 }
412 
OH_UdmfOptions_GetVisibility(OH_UdmfOptions * pThis)413 Udmf_Visibility OH_UdmfOptions_GetVisibility(OH_UdmfOptions* pThis)
414 {
415     if (pThis == nullptr) {
416         LOG_ERROR(UDMF_CAPI, "The Udmf_Visibility get visibility error, invaild params!");
417         return Udmf_Visibility::UDMF_ALL;
418     }
419     return pThis->visibility;
420 }
421 
OH_UdmfOptions_SetVisibility(OH_UdmfOptions * pThis,Udmf_Visibility visibility)422 int OH_UdmfOptions_SetVisibility(OH_UdmfOptions* pThis, Udmf_Visibility visibility)
423 {
424     if (pThis == nullptr) {
425         LOG_ERROR(UDMF_CAPI, "The Udmf_Visibility set visibility error, invaild params!");
426         return UDMF_E_INVALID_PARAM;
427     }
428     pThis->visibility = visibility;
429     return UDMF_E_OK;
430 }
431 
OH_Udmf_GetUnifiedData(const char * key,Udmf_Intention intention,OH_UdmfData * data)432 int OH_Udmf_GetUnifiedData(const char* key, Udmf_Intention intention, OH_UdmfData* data)
433 {
434     if (!IsUnifiedDataValid(data) || key == nullptr) {
435         return UDMF_E_INVALID_PARAM;
436     }
437     Intention queryOptIntent;
438     auto it = VAILD_INTENTIONS.find(intention);
439     if (it != VAILD_INTENTIONS.end()) {
440         queryOptIntent = it->second;
441     } else {
442         return UDMF_E_INVALID_PARAM;
443     }
444     QueryOption query = {.key = std::string(key), .intention = queryOptIntent};
445     if (Intention::UD_INTENTION_DRAG == queryOptIntent) {
446         if (UdmfClient::GetInstance().GetData(query, *(data->unifiedData_)) != E_OK) {
447             LOG_ERROR(UDMF_CAPI, "get data error");
448             return UDMF_ERR;
449         }
450         return UDMF_E_OK;
451     }
452     if (UnifiedDataUtils::IsFileMangerIntention(UD_INTENTION_MAP.at(queryOptIntent))) {
453         std::vector<UnifiedData> unifiedDataSet;
454         auto ret = UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet);
455         if (ret != E_OK) {
456             LOG_ERROR(UDMF_CAPI, "get batchdata error:%{public}d", ret);
457             return UDMF_ERR;
458         }
459         if (unifiedDataSet.empty()) {
460             return UDMF_E_OK;
461         } else {
462             data->unifiedData_ = std::make_shared<OHOS::UDMF::UnifiedData>(unifiedDataSet[0]);
463         }
464     }
465     return UDMF_E_OK;
466 }
467 
OH_Udmf_GetUnifiedDataByOptions(OH_UdmfOptions * options,OH_UdmfData ** dataArray,unsigned int * dataSize)468 int OH_Udmf_GetUnifiedDataByOptions(OH_UdmfOptions* options, OH_UdmfData** dataArray, unsigned int* dataSize)
469 {
470     if (options == nullptr || dataSize == nullptr || dataArray == nullptr) {
471         LOG_ERROR(UDMF_CAPI, "The input param error");
472         return UDMF_E_INVALID_PARAM;
473     }
474     Intention queryOptIntent;
475     switch (options->intention) {
476         case UDMF_INTENTION_DATA_HUB:
477             queryOptIntent = Intention::UD_INTENTION_DATA_HUB;
478             break;
479         case UDMF_INTENTION_SYSTEM_SHARE:
480             queryOptIntent = Intention::UD_INTENTION_SYSTEM_SHARE;
481             break;
482         case UDMF_INTENTION_PICKER:
483             queryOptIntent = Intention::UD_INTENTION_PICKER;
484             break;
485         case UDMF_INTENTION_MENU:
486             queryOptIntent = Intention::UD_INTENTION_MENU;
487             break;
488         default:
489             LOG_ERROR(UDMF_CAPI, "Intention error, it's not one of the above enumerations");
490             return UDMF_E_INVALID_PARAM;
491     }
492     QueryOption query = {.key = std::string(options->key), .intention = queryOptIntent};
493 
494     std::vector<UnifiedData> unifiedData;
495     int32_t ret = UdmfClient::GetInstance().GetBatchData(query, unifiedData);
496     if (ret != E_OK) {
497         LOG_ERROR(UDMF_CAPI, "Get batch data error,ret = %{public}d", ret);
498         return UDMF_ERR;
499     }
500     *dataSize = unifiedData.size();
501     if (*dataSize == 0) {
502         return E_OK;
503     }
504     *dataArray = new (std::nothrow) OH_UdmfData[*dataSize];
505     if (*dataArray == nullptr) {
506         LOG_ERROR(UDMF_CAPI, "New dataArray error");
507         return UDMF_ERR;
508     }
509     for (unsigned int i = 0; i < *dataSize; i++) {
510         (*dataArray)[i].unifiedData_ = std::make_shared<UnifiedData>(unifiedData[i]);
511     }
512     return UDMF_E_OK;
513 }
514 
OH_Udmf_SetUnifiedData(Udmf_Intention intention,OH_UdmfData * unifiedData,char * key,unsigned int keyLen)515 int OH_Udmf_SetUnifiedData(Udmf_Intention intention, OH_UdmfData* unifiedData, char* key, unsigned int keyLen)
516 {
517     if (!IsUnifiedDataValid(unifiedData) || key == nullptr || keyLen < UDMF_KEY_BUFFER_LEN) {
518         return UDMF_E_INVALID_PARAM;
519     }
520     Intention customOptIntent;
521     auto it = VAILD_INTENTIONS.find(intention);
522     if (it != VAILD_INTENTIONS.end()) {
523         customOptIntent = it->second;
524     } else {
525         LOG_ERROR(UDMF_CAPI, "The intention is invalid");
526         return UDMF_E_INVALID_PARAM;
527     }
528     CustomOption option = {.intention = customOptIntent};
529     std::string keyStr;
530     if ((UdmfClient::GetInstance().SetData(option, *(unifiedData->unifiedData_), keyStr)) != E_OK) {
531         LOG_ERROR(UDMF_CAPI, "Set data error");
532         return UDMF_ERR;
533     }
534     if (strcpy_s(key, keyLen, keyStr.c_str()) != EOK) {
535         LOG_ERROR(UDMF_CAPI, "String copy failed");
536         return UDMF_ERR;
537     }
538     return UDMF_E_OK;
539 }
540 
OH_Udmf_SetUnifiedDataByOptions(OH_UdmfOptions * options,OH_UdmfData * unifiedData,char * key,unsigned int keyLen)541 int OH_Udmf_SetUnifiedDataByOptions(OH_UdmfOptions* options, OH_UdmfData* unifiedData, char* key, unsigned int keyLen)
542 {
543     if (!IsUnifiedDataValid(unifiedData) || key == nullptr || keyLen < UDMF_KEY_BUFFER_LEN || options == nullptr) {
544         LOG_ERROR(UDMF_CAPI, "The set param invalid");
545         return UDMF_E_INVALID_PARAM;
546     }
547     Intention customOptIntent;
548     switch (options->intention) {
549         case UDMF_INTENTION_DATA_HUB:
550             customOptIntent = Intention::UD_INTENTION_DATA_HUB;
551             break;
552         case UDMF_INTENTION_SYSTEM_SHARE:
553             customOptIntent = Intention::UD_INTENTION_SYSTEM_SHARE;
554             break;
555         case UDMF_INTENTION_PICKER:
556             customOptIntent = Intention::UD_INTENTION_PICKER;
557             break;
558         case UDMF_INTENTION_MENU:
559             customOptIntent = Intention::UD_INTENTION_MENU;
560             break;
561         default:
562             LOG_ERROR(UDMF_CAPI, "Intention error, it's not one of the above enumerations");
563             return UDMF_E_INVALID_PARAM;
564     }
565     Visibility customOptVisibility;
566     switch (options->visibility) {
567         case UDMF_ALL:
568             customOptVisibility = Visibility::VISIBILITY_ALL;
569             break;
570         case UDMF_OWN_PROCESS:
571             customOptVisibility = Visibility::VISIBILITY_OWN_PROCESS;
572             break;
573         default:
574             LOG_ERROR(UDMF_CAPI, "Invalid visibility value: %{public}d", options->visibility);
575             return UDMF_E_INVALID_PARAM;
576     }
577     CustomOption option = {.intention = customOptIntent, .visibility = customOptVisibility};
578     std::string keyStr;
579     int32_t ret = UdmfClient::GetInstance().SetData(option, *(unifiedData->unifiedData_), keyStr);
580     if (ret != E_OK) {
581         LOG_ERROR(UDMF_CAPI, "Set data error,ret = %{public}d", ret);
582         return UDMF_ERR;
583     }
584     if (strcpy_s(key, keyLen, keyStr.c_str()) != E_OK) {
585         LOG_ERROR(UDMF_CAPI, "String copy failed");
586         return UDMF_ERR;
587     }
588     return UDMF_E_OK;
589 }
590 
OH_Udmf_UpdateUnifiedData(OH_UdmfOptions * options,OH_UdmfData * unifiedData)591 int OH_Udmf_UpdateUnifiedData(OH_UdmfOptions* options, OH_UdmfData* unifiedData)
592 {
593     if (!IsUnifiedDataValid(unifiedData) || options == nullptr || options->key.empty()) {
594         LOG_ERROR(UDMF_CAPI, "The update error, lnvaild params!");
595         return UDMF_E_INVALID_PARAM;
596     }
597     if (options->intention != UDMF_INTENTION_DATA_HUB) {
598         LOG_ERROR(UDMF_CAPI, "Intention error, Intention is %{public}d", options->intention);
599         return UDMF_E_INVALID_PARAM;
600     }
601     QueryOption query = {.key = options->key};
602     int32_t ret = UdmfClient::GetInstance().UpdateData(query, *(unifiedData->unifiedData_));
603     if (ret != E_OK) {
604         LOG_ERROR(UDMF_CAPI, "Updata error,ret = %{public}d", ret);
605         return UDMF_ERR;
606     }
607     return UDMF_E_OK;
608 }
609 
OH_Udmf_DeleteUnifiedData(OH_UdmfOptions * options,OH_UdmfData ** dataArray,unsigned int * dataSize)610 int OH_Udmf_DeleteUnifiedData(OH_UdmfOptions* options, OH_UdmfData** dataArray, unsigned int* dataSize)
611 {
612     if (options == nullptr || dataSize == nullptr || dataArray == nullptr) {
613         LOG_ERROR(UDMF_CAPI, "The delete param invalid");
614         return UDMF_E_INVALID_PARAM;
615     }
616     Intention queryOptIntent;
617     switch (options->intention) {
618         case UDMF_INTENTION_DATA_HUB:
619             queryOptIntent = Intention::UD_INTENTION_DATA_HUB;
620             break;
621         case UDMF_INTENTION_SYSTEM_SHARE:
622             queryOptIntent = Intention::UD_INTENTION_SYSTEM_SHARE;
623             break;
624         case UDMF_INTENTION_PICKER:
625             queryOptIntent = Intention::UD_INTENTION_PICKER;
626             break;
627         case UDMF_INTENTION_MENU:
628             queryOptIntent = Intention::UD_INTENTION_MENU;
629             break;
630         default:
631             LOG_ERROR(UDMF_CAPI, "Intention error, it's not one of the above enumerations");
632             return UDMF_E_INVALID_PARAM;
633     }
634     QueryOption query = {.key = options->key, .intention = queryOptIntent};
635     std::vector<UnifiedData> unifiedData;
636     int32_t ret = UdmfClient::GetInstance().DeleteData(query, unifiedData);
637     if (ret != E_OK) {
638         LOG_ERROR(UDMF_CAPI, "Delete data error,key is %{public}s, ret = %{public}d",
639             query.key.c_str(), ret);
640         return UDMF_ERR;
641     }
642     *dataSize = unifiedData.size();
643     *dataArray = new (std::nothrow) OH_UdmfData[*dataSize];
644     if (*dataArray == nullptr) {
645         LOG_ERROR(UDMF_CAPI, "New dataArray error");
646         return UDMF_ERR;
647     }
648     for (unsigned int i = 0; i < *dataSize; i++) {
649         (*dataArray)[i].unifiedData_ = std::make_shared<UnifiedData>(unifiedData[i]);
650     }
651     return UDMF_E_OK;
652 }
653 
OH_Udmf_DestroyDataArray(OH_UdmfData ** dataArray,unsigned int dataSize)654 void OH_Udmf_DestroyDataArray(OH_UdmfData** dataArray, unsigned int dataSize)
655 {
656     if (dataArray == nullptr || *dataArray == nullptr) {
657         LOG_ERROR(UDMF_CAPI, "The DestroyDataArray error, invaild params!");
658         return;
659     }
660     for (unsigned int i = 0; i < dataSize; ++i) {
661         NdkDataConversion::DestroyStringArray((*dataArray)[i].typesArray, (*dataArray)[i].typesCount);
662         DestroyUnifiedRecordArray((*dataArray)[i].records, (*dataArray)[i].recordsCount);
663     }
664     delete[] *dataArray;
665     *dataArray = nullptr;
666 }
667 
OH_UdmfRecord_Create()668 OH_UdmfRecord* OH_UdmfRecord_Create()
669 {
670     OH_UdmfRecord* record = new (std::nothrow) OH_UdmfRecord;
671     if (record == nullptr) {
672         return nullptr;
673     }
674     record->record_ = std::make_shared<UnifiedRecord>();
675     return record;
676 }
677 
OH_UdmfRecord_Destroy(OH_UdmfRecord * record)678 void OH_UdmfRecord_Destroy(OH_UdmfRecord* record)
679 {
680     if (record == nullptr) {
681         return;
682     }
683     {
684         std::lock_guard<std::mutex> lock(record->mutex);
685         if (record->recordData != nullptr) {
686             delete[] record->recordData;
687             record->recordData = nullptr;
688         }
689         NdkDataConversion::DestroyStringArray(record->typesArray, record->typesCount);
690     }
691     delete record;
692 }
693 
OH_UdmfRecord_AddGeneralEntry(OH_UdmfRecord * record,const char * typeId,const unsigned char * entry,unsigned int count)694 int OH_UdmfRecord_AddGeneralEntry(OH_UdmfRecord* record, const char* typeId,
695                                   const unsigned char* entry, unsigned int count)
696 {
697     if (!IsUnifiedRecordValid(record) || typeId == nullptr || entry == nullptr || count == 0 ||
698         count > MAX_GENERAL_ENTRY_SIZE || strlen(typeId) > MAX_KEY_STRING_LEN) {
699         return UDMF_E_INVALID_PARAM;
700     }
701     std::vector<uint8_t> recordValue(entry, entry + count);
702     auto obj = std::make_shared<Object>();
703     obj->value_[UNIFORM_DATA_TYPE] = typeId;
704     obj->value_[ARRAY_BUFFER] = recordValue;
705     obj->value_[ARRAY_BUFFER_LENGTH] = static_cast<int>(recordValue.size());
706     if (record->record_->GetType() == UD_BUTT) {
707         record->record_ = std::make_shared<ApplicationDefinedRecord>(APPLICATION_DEFINED_RECORD, obj);
708         record->record_->SetUtdId(typeId);
709     } else {
710         record->record_->AddEntry(typeId, obj);
711     }
712     record->recordDataLen = count;
713     return UDMF_E_OK;
714 }
715 
GetValueFromUint8Array(OH_UdmfRecord * record,const char * typeId,ValueType value)716 static int GetValueFromUint8Array(OH_UdmfRecord *record, const char *typeId, ValueType value)
717 {
718     if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
719         LOG_ERROR(UDMF_CAPI, "valueType is not object sptr!");
720         return UDMF_ERR;
721     }
722     auto obj = std::get<std::shared_ptr<Object>>(value);
723     if (obj == nullptr) {
724         LOG_ERROR(UDMF_CAPI, "object is nullptr!");
725         return UDMF_ERR;
726     }
727     std::vector<uint8_t> recordValue;
728     int recordDataLen;
729     obj->GetValue(ARRAY_BUFFER, recordValue);
730     obj->GetValue(ARRAY_BUFFER_LENGTH, recordDataLen);
731     record->recordDataLen = recordValue.size();
732     if (record->recordDataLen > MAX_GENERAL_ENTRY_SIZE) {
733         LOG_INFO(UDMF_CAPI, "data size exceeds maximum size");
734         return UDMF_ERR;
735     }
736     record->recordData = new (std::nothrow) unsigned char[record->recordDataLen];
737     if (record->recordData == nullptr) {
738         return UDMF_ERR;
739     }
740     auto err = memcpy_s(record->recordData, record->recordDataLen, recordValue.data(), record->recordDataLen);
741     if (err != EOK) {
742         LOG_ERROR(UDMF_CAPI, "memcpy error!");
743         return UDMF_ERR;
744     }
745     record->lastType = const_cast<char*>(typeId);
746     return UDMF_E_OK;
747 }
748 
OH_UdmfRecord_GetGeneralEntry(OH_UdmfRecord * record,const char * typeId,unsigned char ** entry,unsigned int * count)749 int OH_UdmfRecord_GetGeneralEntry(OH_UdmfRecord* record, const char* typeId, unsigned char** entry, unsigned int* count)
750 {
751     if (!IsUnifiedRecordValid(record) || typeId == nullptr || entry == nullptr || count == nullptr) {
752         return UDMF_E_INVALID_PARAM;
753     }
754     std::lock_guard<std::mutex> lock(record->mutex);
755     if (!record->record_->HasType(typeId)) {
756         LOG_ERROR(UDMF_CAPI, "not has typeId");
757         return UDMF_E_INVALID_PARAM;
758     }
759     if (record->lastType == typeId && record->recordData != nullptr) {
760         LOG_DEBUG(UDMF_CAPI, "return cache value");
761         *entry = record->recordData;
762         *count = record->recordDataLen;
763         return UDMF_E_OK;
764     }
765     record->record_->InitObject();
766     auto value = record->record_->GetEntry(typeId);
767     if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
768         LOG_ERROR(UDMF_CAPI, "Not contains right data type.");
769         return UDMF_ERR;
770     }
771     auto result = GetValueFromUint8Array(record, typeId, value);
772     if (result != UDMF_E_OK) {
773         LOG_ERROR(UDMF_CAPI, "Get value from valueType failed. result: %{public}d", result);
774         return result;
775     }
776     *count = record->recordDataLen;
777     *entry = record->recordData;
778     return UDMF_E_OK;
779 }
780 
781 template<typename T>
AddUds(OH_UdmfRecord * record,UdsObject * udsObject,UDType type)782 void AddUds(OH_UdmfRecord* record, UdsObject* udsObject, UDType type)
783 {
784     if (record->record_->GetType() == UD_BUTT) {
785         record->record_ = std::make_shared<T>(type, udsObject->obj);
786     } else {
787         record->record_->AddEntry(UtdUtils::GetUtdIdFromUtdEnum(type), udsObject->obj);
788     }
789 }
790 
OH_UdmfRecord_AddPlainText(OH_UdmfRecord * record,OH_UdsPlainText * plainText)791 int OH_UdmfRecord_AddPlainText(OH_UdmfRecord* record, OH_UdsPlainText* plainText)
792 {
793     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
794         return UDMF_E_INVALID_PARAM;
795     }
796     AddUds<PlainText>(record, plainText, PLAIN_TEXT);
797     return UDMF_E_OK;
798 }
799 
OH_UdmfRecord_AddHyperlink(OH_UdmfRecord * record,OH_UdsHyperlink * hyperlink)800 int OH_UdmfRecord_AddHyperlink(OH_UdmfRecord* record, OH_UdsHyperlink* hyperlink)
801 {
802     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(hyperlink, UDS_HYPERLINK_STRUCT_ID)) {
803         return UDMF_E_INVALID_PARAM;
804     }
805     AddUds<Link>(record, hyperlink, HYPERLINK);
806     return UDMF_E_OK;
807 }
808 
OH_UdmfRecord_AddHtml(OH_UdmfRecord * record,OH_UdsHtml * html)809 int OH_UdmfRecord_AddHtml(OH_UdmfRecord* record, OH_UdsHtml* html)
810 {
811     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
812         return UDMF_E_INVALID_PARAM;
813     }
814     AddUds<Html>(record, html, HTML);
815     return UDMF_E_OK;
816 }
817 
OH_UdmfRecord_AddAppItem(OH_UdmfRecord * record,OH_UdsAppItem * appItem)818 int OH_UdmfRecord_AddAppItem(OH_UdmfRecord* record, OH_UdsAppItem* appItem)
819 {
820     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(appItem, UDS_APP_ITEM_STRUCT_ID)) {
821         return UDMF_E_INVALID_PARAM;
822     }
823     AddUds<SystemDefinedAppItem>(record, appItem, SYSTEM_DEFINED_APP_ITEM);
824     return UDMF_E_OK;
825 }
826 
OH_UdmfRecord_AddFileUri(OH_UdmfRecord * record,OH_UdsFileUri * fileUri)827 int OH_UdmfRecord_AddFileUri(OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
828 {
829     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(fileUri, UDS_FILE_URI_STRUCT_ID)) {
830         return UDMF_E_INVALID_PARAM;
831     }
832     std::string* fileType = std::get_if<std::string>(&(fileUri->obj->value_[FILE_TYPE]));
833     if (fileType == nullptr) {
834         return UDMF_ERR;
835     }
836     std::map<UDType, std::function<void(OH_UdmfRecord*, OH_UdsFileUri*)>> addFileUriFuncs = {
837         {UDType::FILE, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
838             { AddUds<File>(record, fileUri, UDType::FILE); }},
839         {UDType::IMAGE, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
840             { AddUds<Image>(record, fileUri, UDType::IMAGE); }},
841         {UDType::VIDEO, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
842             {  AddUds<Video>(record, fileUri, UDType::VIDEO); }},
843         {UDType::AUDIO, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
844             {  AddUds<Audio>(record, fileUri, UDType::AUDIO); }},
845         {UDType::FOLDER, [](OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
846             {  AddUds<Folder>(record, fileUri, UDType::FOLDER); }},
847     };
848     int32_t utdId = UDType::FILE;
849     std::string subFileType = UnifiedDataUtils::GetBelongsToFileType(*fileType);
850     if (!subFileType.empty()) {
851         utdId = static_cast<UDType>(UtdUtils::GetUtdEnumFromUtdId(subFileType));
852     }
853     addFileUriFuncs[static_cast<UDType>(utdId)](record, fileUri);
854     return UDMF_E_OK;
855 }
856 
OH_UdmfRecord_AddPixelMap(OH_UdmfRecord * record,OH_UdsPixelMap * pixelMap)857 int OH_UdmfRecord_AddPixelMap(OH_UdmfRecord* record, OH_UdsPixelMap* pixelMap)
858 {
859     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(pixelMap, UDS_PIXEL_MAP_STRUCT_ID)) {
860         return UDMF_E_INVALID_PARAM;
861     }
862     AddUds<SystemDefinedPixelMap>(record, pixelMap, UDType::SYSTEM_DEFINED_PIXEL_MAP);
863     return UDMF_E_OK;
864 }
865 
GetUds(OH_UdmfRecord * record,UdsObject * udsObject,UDType type)866 int GetUds(OH_UdmfRecord* record, UdsObject* udsObject, UDType type)
867 {
868     record->record_->InitObject();
869     auto value = record->record_->GetEntry(UtdUtils::GetUtdIdFromUtdEnum(type));
870     if (!std::holds_alternative<std::shared_ptr<Object>>(value)) {
871         return UDMF_ERR;
872     }
873     udsObject->obj = std::get<std::shared_ptr<Object>>(value);
874     return UDMF_E_OK;
875 }
876 
OH_UdmfRecord_AddArrayBuffer(OH_UdmfRecord * record,const char * type,OH_UdsArrayBuffer * buffer)877 int OH_UdmfRecord_AddArrayBuffer(OH_UdmfRecord* record, const char* type, OH_UdsArrayBuffer* buffer)
878 {
879     if (!IsUnifiedRecordValid(record) || type == nullptr || strlen(type) > MAX_KEY_STRING_LEN ||
880         IsInvalidUdsObjectPtr(buffer, UDS_ARRAY_BUFFER_STRUCT_ID)) {
881         return UDMF_E_INVALID_PARAM;
882     }
883     unsigned char *entry;
884     unsigned int size;
885     int ret = OH_UdsArrayBuffer_GetData(buffer, &entry, &size);
886     if (ret != UDMF_E_OK) {
887         return UDMF_E_INVALID_PARAM;
888     }
889     buffer->obj->value_[UNIFORM_DATA_TYPE] = type;
890     return OH_UdmfRecord_AddGeneralEntry(record, type, entry, size);
891 }
892 
OH_UdmfRecord_AddContentForm(OH_UdmfRecord * record,OH_UdsContentForm * contentForm)893 int OH_UdmfRecord_AddContentForm(OH_UdmfRecord* record, OH_UdsContentForm* contentForm)
894 {
895     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(contentForm, UDS_CONTENT_FORM_STRUCT_ID)) {
896         return UDMF_E_INVALID_PARAM;
897     }
898     AddUds<UnifiedRecord>(record, contentForm, UDType::CONTENT_FORM);
899     return UDMF_E_OK;
900 }
901 
OH_UdmfRecord_GetPlainText(OH_UdmfRecord * record,OH_UdsPlainText * plainText)902 int OH_UdmfRecord_GetPlainText(OH_UdmfRecord* record, OH_UdsPlainText* plainText)
903 {
904     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(plainText, UDS_PLAIN_TEXT_STRUCT_ID)) {
905         return UDMF_E_INVALID_PARAM;
906     }
907     return GetUds(record, plainText, PLAIN_TEXT);
908 }
909 
OH_UdmfRecord_GetHyperlink(OH_UdmfRecord * record,OH_UdsHyperlink * hyperlink)910 int OH_UdmfRecord_GetHyperlink(OH_UdmfRecord* record, OH_UdsHyperlink* hyperlink)
911 {
912     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(hyperlink, UDS_HYPERLINK_STRUCT_ID)) {
913         return UDMF_E_INVALID_PARAM;
914     }
915     return GetUds(record, hyperlink, HYPERLINK);
916 }
917 
OH_UdmfRecord_GetHtml(OH_UdmfRecord * record,OH_UdsHtml * html)918 int OH_UdmfRecord_GetHtml(OH_UdmfRecord* record, OH_UdsHtml* html)
919 {
920     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(html, UDS_HTML_STRUCT_ID)) {
921         return UDMF_E_INVALID_PARAM;
922     }
923     return GetUds(record, html, HTML);
924 }
925 
OH_UdmfRecord_GetAppItem(OH_UdmfRecord * record,OH_UdsAppItem * appItem)926 int OH_UdmfRecord_GetAppItem(OH_UdmfRecord* record, OH_UdsAppItem* appItem)
927 {
928     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(appItem, UDS_APP_ITEM_STRUCT_ID)) {
929         return UDMF_E_INVALID_PARAM;
930     }
931     return GetUds(record, appItem, SYSTEM_DEFINED_APP_ITEM);
932 }
933 
OH_UdmfRecord_GetFileUri(OH_UdmfRecord * record,OH_UdsFileUri * fileUri)934 int OH_UdmfRecord_GetFileUri(OH_UdmfRecord* record, OH_UdsFileUri* fileUri)
935 {
936     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(fileUri, UDS_FILE_URI_STRUCT_ID)) {
937         return UDMF_E_INVALID_PARAM;
938     }
939     if (GetUds(record, fileUri, UDType::FILE_URI) == UDMF_E_OK) {
940         return UDMF_E_OK;
941     }
942     for (auto fileType : FILE_TYPES) {
943         int ret = GetUds(record, fileUri, fileType.second);
944         if (ret == UDMF_E_OK) {
945             fileUri->obj->value_[UNIFORM_DATA_TYPE] = UDMF_META_GENERAL_FILE_URI;
946             fileUri->obj->value_[FILE_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(fileType.second);
947             return UDMF_E_OK;
948         }
949     }
950     LOG_ERROR(UDMF_CAPI, "could't find file uri");
951     return UDMF_E_INVALID_PARAM;
952 }
953 
OH_UdmfRecord_GetPixelMap(OH_UdmfRecord * record,OH_UdsPixelMap * pixelMap)954 int OH_UdmfRecord_GetPixelMap(OH_UdmfRecord* record, OH_UdsPixelMap* pixelMap)
955 {
956     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(pixelMap, UDS_PIXEL_MAP_STRUCT_ID)) {
957         return UDMF_E_INVALID_PARAM;
958     }
959     return GetUds(record, pixelMap, UDType::SYSTEM_DEFINED_PIXEL_MAP);
960 }
961 
OH_UdmfRecord_GetArrayBuffer(OH_UdmfRecord * record,const char * type,OH_UdsArrayBuffer * buffer)962 int OH_UdmfRecord_GetArrayBuffer(OH_UdmfRecord* record, const char* type, OH_UdsArrayBuffer* buffer)
963 {
964     unsigned int size = 0;
965     unsigned char *entry;
966     int ret = OH_UdmfRecord_GetGeneralEntry(record, type, &entry, &size);
967     if (ret != UDMF_E_OK) {
968         LOG_ERROR(UDMF_CAPI, "OH_UdmfRecord_GetGeneralEntry ret: %{public}d.", ret);
969         return ret;
970     }
971     return OH_UdsArrayBuffer_SetData(buffer, entry, size);
972 }
973 
OH_UdmfRecord_GetContentForm(OH_UdmfRecord * record,OH_UdsContentForm * contentForm)974 int OH_UdmfRecord_GetContentForm(OH_UdmfRecord* record, OH_UdsContentForm* contentForm)
975 {
976     if (!IsUnifiedRecordValid(record) || IsInvalidUdsObjectPtr(contentForm, UDS_CONTENT_FORM_STRUCT_ID)) {
977         return UDMF_E_INVALID_PARAM;
978     }
979     return GetUds(record, contentForm, UDType::CONTENT_FORM);
980 }
981 
OH_UdmfProperty_Create(OH_UdmfData * data)982 OH_UdmfProperty* OH_UdmfProperty_Create(OH_UdmfData* data)
983 {
984     if (!IsUnifiedDataValid(data)) {
985         return nullptr;
986     }
987     OH_UdmfProperty* properties = new (std::nothrow) OH_UdmfProperty;
988     if (properties == nullptr) {
989         return nullptr;
990     }
991     properties->properties_ = data->unifiedData_->GetProperties();
992     return properties;
993 }
994 
OH_UdmfProperty_Destroy(OH_UdmfProperty * properties)995 void OH_UdmfProperty_Destroy(OH_UdmfProperty* properties)
996 {
997     if (properties != nullptr) {
998         delete properties;
999     }
1000 }
1001 
OH_UdmfProperty_GetTag(OH_UdmfProperty * properties)1002 const char* OH_UdmfProperty_GetTag(OH_UdmfProperty* properties)
1003 {
1004     if (!IsUnifiedPropertiesValid(properties)) {
1005         return nullptr;
1006     }
1007     return properties->properties_->tag.c_str();
1008 }
1009 
OH_UdmfProperty_GetTimestamp(OH_UdmfProperty * properties)1010 int64_t OH_UdmfProperty_GetTimestamp(OH_UdmfProperty* properties)
1011 {
1012     if (!IsUnifiedPropertiesValid(properties)) {
1013         return -1;
1014     }
1015     return properties->properties_->timestamp;
1016 }
1017 
OH_UdmfProperty_GetShareOption(OH_UdmfProperty * properties)1018 Udmf_ShareOption OH_UdmfProperty_GetShareOption(OH_UdmfProperty* properties)
1019 {
1020     if (!IsUnifiedPropertiesValid(properties)) {
1021         return Udmf_ShareOption::SHARE_OPTIONS_INVALID;
1022     }
1023     switch (properties->properties_->shareOptions) {
1024         case ShareOptions::IN_APP:
1025             return Udmf_ShareOption::SHARE_OPTIONS_IN_APP;
1026         case ShareOptions::CROSS_APP:
1027         case ShareOptions::CROSS_DEVICE:
1028             return Udmf_ShareOption::SHARE_OPTIONS_CROSS_APP;
1029         default:
1030             return Udmf_ShareOption::SHARE_OPTIONS_INVALID;
1031     }
1032 }
1033 
OH_UdmfProperty_GetExtrasIntParam(OH_UdmfProperty * properties,const char * key,const int defaultValue)1034 int OH_UdmfProperty_GetExtrasIntParam(OH_UdmfProperty* properties, const char* key, const int defaultValue)
1035 {
1036     return (IsUnifiedPropertiesValid(properties) && key != nullptr) ?
1037            properties->properties_->extras.GetIntParam(key, defaultValue) : defaultValue;
1038 }
1039 
OH_UdmfProperty_GetExtrasStringParam(OH_UdmfProperty * properties,const char * key)1040 const char* OH_UdmfProperty_GetExtrasStringParam(OH_UdmfProperty* properties, const char* key)
1041 {
1042     if (!IsUnifiedPropertiesValid(properties) || key == nullptr) {
1043         return nullptr;
1044     }
1045     properties->extraStr = properties->properties_->extras.GetStringParam(key);
1046     return properties->extraStr.c_str();
1047 }
1048 
OH_UdmfProperty_SetTag(OH_UdmfProperty * properties,const char * tag)1049 int OH_UdmfProperty_SetTag(OH_UdmfProperty* properties, const char* tag)
1050 {
1051     if (!IsUnifiedPropertiesValid(properties) || tag == nullptr) {
1052         return UDMF_E_INVALID_PARAM;
1053     }
1054     properties->properties_->tag = tag;
1055     return UDMF_E_OK;
1056 }
1057 
OH_UdmfProperty_SetShareOption(OH_UdmfProperty * properties,Udmf_ShareOption option)1058 int OH_UdmfProperty_SetShareOption(OH_UdmfProperty* properties, Udmf_ShareOption option)
1059 {
1060     if (!IsUnifiedPropertiesValid(properties)) {
1061         return UDMF_E_INVALID_PARAM;
1062     }
1063     std::lock_guard<std::mutex> lock(properties->mutex);
1064     switch (option) {
1065         case Udmf_ShareOption::SHARE_OPTIONS_IN_APP:
1066             properties->properties_->shareOptions = ShareOptions::IN_APP;
1067             break;
1068         case Udmf_ShareOption::SHARE_OPTIONS_CROSS_APP:
1069             properties->properties_->shareOptions = ShareOptions::CROSS_APP;
1070             break;
1071         default:
1072             return UDMF_E_INVALID_PARAM;
1073     }
1074     return UDMF_E_OK;
1075 }
1076 
OH_UdmfProperty_SetExtrasIntParam(OH_UdmfProperty * properties,const char * key,int param)1077 int OH_UdmfProperty_SetExtrasIntParam(OH_UdmfProperty* properties, const char* key, int param)
1078 {
1079     if (!IsUnifiedPropertiesValid(properties) || key == nullptr) {
1080         return UDMF_E_INVALID_PARAM;
1081     }
1082     std::lock_guard<std::mutex> lock(properties->mutex);
1083     properties->properties_->extras.SetParam(key, OHOS::AAFwk::Integer::Box(param));
1084     return UDMF_E_OK;
1085 }
1086 
OH_UdmfProperty_SetExtrasStringParam(OH_UdmfProperty * properties,const char * key,const char * param)1087 int OH_UdmfProperty_SetExtrasStringParam(OH_UdmfProperty* properties, const char* key, const char* param)
1088 {
1089     if (!IsUnifiedPropertiesValid(properties) || key == nullptr || param == nullptr) {
1090         return UDMF_E_INVALID_PARAM;
1091     }
1092     std::lock_guard<std::mutex> lock(properties->mutex);
1093     properties->properties_->extras.SetParam(key, OHOS::AAFwk::String::Box(param));
1094     return UDMF_E_OK;
1095 }
1096 
OH_UdmfRecordProvider_Create()1097 OH_UdmfRecordProvider* OH_UdmfRecordProvider_Create()
1098 {
1099     OH_UdmfRecordProvider* provider = new (std::nothrow) OH_UdmfRecordProvider();
1100     if (provider == nullptr) {
1101         LOG_ERROR(UDMF_CAPI, "allocate OH_UdmfRecordProvider memory fail");
1102     }
1103     return provider;
1104 }
1105 
OH_UdmfRecordProvider_Destroy(OH_UdmfRecordProvider * provider)1106 int OH_UdmfRecordProvider_Destroy(OH_UdmfRecordProvider* provider)
1107 {
1108     if (provider == nullptr) {
1109         return UDMF_E_INVALID_PARAM;
1110     }
1111     if (provider->context != nullptr && provider->finalize != nullptr) {
1112         (provider->finalize)(provider->context);
1113         LOG_INFO(UDMF_CAPI, "free context finished");
1114     }
1115     delete provider;
1116     return UDMF_E_OK;
1117 }
1118 
OH_UdmfRecordProvider_SetData(OH_UdmfRecordProvider * provider,void * context,const OH_UdmfRecordProvider_GetData callback,const UdmfData_Finalize finalize)1119 int OH_UdmfRecordProvider_SetData(OH_UdmfRecordProvider* provider, void* context,
1120     const OH_UdmfRecordProvider_GetData callback, const UdmfData_Finalize finalize)
1121 {
1122     if (provider == nullptr || callback == nullptr) {
1123         return UDMF_E_INVALID_PARAM;
1124     }
1125     provider->callback = callback;
1126     if (context != nullptr && finalize == nullptr) {
1127         LOG_ERROR(UDMF_CAPI, "finalize function is null when context not null");
1128         return UDMF_E_INVALID_PARAM;
1129     }
1130     provider->context = context;
1131     provider->finalize = finalize;
1132     return UDMF_E_OK;
1133 }
1134 
OH_UdmfRecord_SetProvider(OH_UdmfRecord * record,const char * const * types,unsigned int count,OH_UdmfRecordProvider * provider)1135 int OH_UdmfRecord_SetProvider(OH_UdmfRecord* record, const char* const* types, unsigned int count,
1136     OH_UdmfRecordProvider* provider)
1137 {
1138     if (!IsUnifiedRecordValid(record) || types == nullptr || count == 0
1139         || count > MAX_TYPES_COUNT || provider == nullptr) {
1140         return UDMF_E_INVALID_PARAM;
1141     }
1142     std::shared_ptr<DataProviderImpl> providerBox = std::make_shared<DataProviderImpl>();
1143     providerBox->SetInnerProvider(provider);
1144     std::vector<std::string> udTypes;
1145     std::set<std::string> udTypeSet;
1146     for (unsigned int i = 0; i < count; ++i) {
1147         if (types[i] == nullptr) {
1148             LOG_ERROR(UDMF_CAPI, "The type with index %{public}d is empty", i);
1149             continue;
1150         }
1151         if (udTypeSet.count(types[i]) == 0) {
1152             udTypeSet.emplace(types[i]);
1153             udTypes.emplace_back(types[i]);
1154         }
1155     }
1156     record->record_->SetEntryGetter(udTypes, providerBox);
1157     return UDMF_E_OK;
1158 }
1159 
OH_UdmfProgressInfo_GetProgress(OH_Udmf_ProgressInfo * progressInfo)1160 int OH_UdmfProgressInfo_GetProgress(OH_Udmf_ProgressInfo* progressInfo)
1161 {
1162     return progressInfo->progress;
1163 }
1164 
OH_UdmfProgressInfo_GetStatus(OH_Udmf_ProgressInfo * progressInfo)1165 int OH_UdmfProgressInfo_GetStatus(OH_Udmf_ProgressInfo* progressInfo)
1166 {
1167     return progressInfo->status;
1168 }
1169 
OH_UdmfGetDataParams_Create()1170 OH_UdmfGetDataParams* OH_UdmfGetDataParams_Create()
1171 {
1172     OH_UdmfGetDataParams *params =  new (std::nothrow) OH_UdmfGetDataParams();
1173     if (params == nullptr) {
1174         LOG_ERROR(UDMF_CAPI, "allocate OH_UdmfGetDataParams memory fail");
1175         return nullptr;
1176     }
1177     return params;
1178 }
1179 
OH_UdmfGetDataParams_Destroy(OH_UdmfGetDataParams * pThis)1180 void OH_UdmfGetDataParams_Destroy(OH_UdmfGetDataParams* pThis)
1181 {
1182     if (pThis == nullptr) {
1183         return;
1184     }
1185     delete pThis;
1186 }
1187 
OH_UdmfGetDataParams_SetDestUri(OH_UdmfGetDataParams * params,const char * destUri)1188 void OH_UdmfGetDataParams_SetDestUri(OH_UdmfGetDataParams* params, const char* destUri)
1189 {
1190     if (destUri == nullptr) {
1191         return;
1192     }
1193     params->destUri = std::string(destUri);
1194 }
1195 
OH_UdmfGetDataParams_SetFileConflictOptions(OH_UdmfGetDataParams * params,const Udmf_FileConflictOptions options)1196 void OH_UdmfGetDataParams_SetFileConflictOptions(OH_UdmfGetDataParams* params, const Udmf_FileConflictOptions options)
1197 {
1198     if (params == nullptr) {
1199         return;
1200     }
1201     params->fileConflictOptions = options;
1202 }
1203 
OH_UdmfGetDataParams_SetProgressIndicator(OH_UdmfGetDataParams * params,const Udmf_ProgressIndicator progressIndicator)1204 void OH_UdmfGetDataParams_SetProgressIndicator(OH_UdmfGetDataParams* params,
1205     const Udmf_ProgressIndicator progressIndicator)
1206 {
1207     if (params == nullptr) {
1208         return;
1209     }
1210     params->progressIndicator = progressIndicator;
1211 }
1212 
OH_UdmfGetDataParams_SetDataProgressListener(OH_UdmfGetDataParams * params,const OH_Udmf_DataProgressListener dataProgressListener)1213 void OH_UdmfGetDataParams_SetDataProgressListener(OH_UdmfGetDataParams* params,
1214     const OH_Udmf_DataProgressListener dataProgressListener)
1215 {
1216     if (params == nullptr) {
1217         return;
1218     }
1219     params->dataProgressListener = dataProgressListener;
1220 }
1221 
OH_UdmfGetDataParams_SetAcceptableInfo(OH_UdmfGetDataParams * params,OH_UdmfDataLoadInfo * acceptableInfo)1222 void OH_UdmfGetDataParams_SetAcceptableInfo(OH_UdmfGetDataParams* params, OH_UdmfDataLoadInfo* acceptableInfo)
1223 {
1224     if (params == nullptr || acceptableInfo == nullptr) {
1225         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1226         return;
1227     }
1228     params->acceptableInfo = *acceptableInfo;
1229 }
1230 
OH_UdmfDataLoadParams_Create()1231 OH_UdmfDataLoadParams* OH_UdmfDataLoadParams_Create()
1232 {
1233     OH_UdmfDataLoadParams *params = new (std::nothrow) OH_UdmfDataLoadParams;
1234     if (params == nullptr) {
1235         LOG_ERROR(UDMF_CAPI, "Allocate OH_UdmfDataLoadParams memory failed.");
1236         return nullptr;
1237     }
1238     return params;
1239 }
1240 
OH_UdmfDataLoadParams_Destroy(OH_UdmfDataLoadParams * pThis)1241 void OH_UdmfDataLoadParams_Destroy(OH_UdmfDataLoadParams* pThis)
1242 {
1243     if (pThis == nullptr) {
1244         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1245         return;
1246     }
1247     delete pThis;
1248 }
1249 
OH_UdmfDataLoadParams_SetLoadHandler(OH_UdmfDataLoadParams * params,const OH_Udmf_DataLoadHandler dataLoadHandler)1250 void OH_UdmfDataLoadParams_SetLoadHandler(OH_UdmfDataLoadParams* params, const OH_Udmf_DataLoadHandler dataLoadHandler)
1251 {
1252     if (params == nullptr) {
1253         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1254         return;
1255     }
1256     params->dataLoadHandler = dataLoadHandler;
1257 }
1258 
OH_UdmfDataLoadParams_SetDataLoadInfo(OH_UdmfDataLoadParams * params,OH_UdmfDataLoadInfo * dataLoadInfo)1259 void OH_UdmfDataLoadParams_SetDataLoadInfo(OH_UdmfDataLoadParams* params, OH_UdmfDataLoadInfo* dataLoadInfo)
1260 {
1261     if (params == nullptr || dataLoadInfo == nullptr) {
1262         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1263         return;
1264     }
1265     params->dataLoadInfo = *dataLoadInfo;
1266 }
1267 
OH_UdmfDataLoadInfo_Create()1268 OH_UdmfDataLoadInfo* OH_UdmfDataLoadInfo_Create()
1269 {
1270     OH_UdmfDataLoadInfo *params = new (std::nothrow) OH_UdmfDataLoadInfo;
1271     if (params == nullptr) {
1272         LOG_ERROR(UDMF_CAPI, "Allocate OH_UdmfDataLoadInfo memory failed.");
1273         return nullptr;
1274     }
1275     return params;
1276 }
1277 
OH_UdmfDataLoadInfo_Destroy(OH_UdmfDataLoadInfo * dataLoadInfo)1278 void OH_UdmfDataLoadInfo_Destroy(OH_UdmfDataLoadInfo* dataLoadInfo)
1279 {
1280     if (dataLoadInfo == nullptr) {
1281         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1282         return;
1283     }
1284     NdkDataConversion::DestroyStringArray(dataLoadInfo->typesArray, dataLoadInfo->typesCount);
1285     delete dataLoadInfo;
1286 }
1287 
OH_UdmfDataLoadInfo_GetTypes(OH_UdmfDataLoadInfo * dataLoadInfo,unsigned int * count)1288 char** OH_UdmfDataLoadInfo_GetTypes(OH_UdmfDataLoadInfo* dataLoadInfo, unsigned int* count)
1289 {
1290     if (dataLoadInfo == nullptr || count == nullptr) {
1291         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1292         return nullptr;
1293     }
1294     *count = dataLoadInfo->typesCount;
1295     return dataLoadInfo->typesArray;
1296 }
1297 
OH_UdmfDataLoadInfo_SetType(OH_UdmfDataLoadInfo * dataLoadInfo,const char * type)1298 void OH_UdmfDataLoadInfo_SetType(OH_UdmfDataLoadInfo* dataLoadInfo, const char* type)
1299 {
1300     if (dataLoadInfo == nullptr || type == nullptr) {
1301         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1302         return;
1303     }
1304     std::set<std::string> types;
1305     for (unsigned int i = 0; i < dataLoadInfo->typesCount; ++i) {
1306         types.insert(dataLoadInfo->typesArray[i]);
1307     }
1308     types.insert(type);
1309     std::vector<std::string> typeLabels {types.begin(), types.end()};
1310     dataLoadInfo->typesArray = NdkDataConversion::StrVectorToTypesArray(typeLabels);
1311     dataLoadInfo->typesCount = typeLabels.size();
1312 }
1313 
OH_UdmfDataLoadInfo_GetRecordCount(OH_UdmfDataLoadInfo * dataLoadInfo)1314 int OH_UdmfDataLoadInfo_GetRecordCount(OH_UdmfDataLoadInfo* dataLoadInfo)
1315 {
1316     if (dataLoadInfo == nullptr) {
1317         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1318         return 0;
1319     }
1320     return static_cast<int>(dataLoadInfo->recordsCount);
1321 }
1322 
OH_UdmfDataLoadInfo_SetRecordCount(OH_UdmfDataLoadInfo * dataLoadInfo,unsigned int recordCount)1323 void OH_UdmfDataLoadInfo_SetRecordCount(OH_UdmfDataLoadInfo* dataLoadInfo, unsigned int recordCount)
1324 {
1325     if (dataLoadInfo == nullptr) {
1326         LOG_ERROR(UDMF_CAPI, "Parameter error.");
1327         return;
1328     }
1329     dataLoadInfo->recordsCount = recordCount;
1330 }