• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "udmf_impl.h"
17 
18 #include "data_load_params_napi.h"
19 #include "data_params_conversion.h"
20 #include "html.h"
21 #include "image.h"
22 #include "link.h"
23 #include "summary_napi.h"
24 #include "system_defined_form.h"
25 #include "system_defined_pixelmap.h"
26 #include "plain_text.h"
27 #include "application_defined_record.h"
28 #include "get_data_params_napi.h"
29 #include "udmf_async_client.h"
30 #include "unified_data_napi.h"
31 #include "unified_meta.h"
32 #include "unified_types.h"
33 #include "utd_client.h"
34 #include "video.h"
35 #include "frameworks/bridge/common/utils/engine_helper.h"
36 #include "frameworks/bridge/common/utils/utils.h"
37 
38 #include "ndk_data_conversion.h"
39 
40 namespace OHOS::Ace {
GetInstance()41 UdmfClient* UdmfClient::GetInstance()
42 {
43     static UdmfClientImpl instance;
44     return &instance;
45 }
46 
CreateUnifiedData()47 RefPtr<UnifiedData> UdmfClientImpl::CreateUnifiedData()
48 {
49     return AceType::DynamicCast<UnifiedData>(AceType::MakeRefPtr<UnifiedDataImpl>());
50 }
51 
TransformUnifiedData(napi_value napiValue)52 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedData(napi_value napiValue)
53 {
54     auto engine = EngineHelper::GetCurrentEngine();
55     CHECK_NULL_RETURN(engine, nullptr);
56     NativeEngine* nativeEngine = engine->GetNativeEngine();
57     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
58     void* native = nullptr;
59     napi_unwrap(env, napiValue, &native);
60     auto* unifiedData = reinterpret_cast<UDMF::UnifiedDataNapi*>(native);
61     CHECK_NULL_RETURN(unifiedData, nullptr);
62     CHECK_NULL_RETURN(unifiedData->value_, nullptr);
63     auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
64     udData->SetUnifiedData(unifiedData->value_);
65     return udData;
66 }
67 
TransformDataLoadParams(napi_env env,napi_value napiValue)68 RefPtr<DataLoadParams> UdmfClientImpl::TransformDataLoadParams(napi_env env, napi_value napiValue)
69 {
70     UDMF::DataLoadParams dataLoadParams;
71     if (UDMF::DataLoadParamsNapi::Convert2NativeValue(env, napiValue, dataLoadParams)) {
72         auto udDataLoadParams = AceType::MakeRefPtr<DataLoadParamsImpl>();
73         CHECK_NULL_RETURN(udDataLoadParams, nullptr);
74         udDataLoadParams->SetDataLoadParams(std::make_shared<UDMF::DataLoadParams>(dataLoadParams));
75         return udDataLoadParams;
76     }
77     TAG_LOGI(AceLogTag::ACE_DRAG, "dataLoadParamsNapi convert2NativeValue failed");
78     return nullptr;
79 }
80 
TransformUdmfUnifiedData(RefPtr<UnifiedData> & UnifiedData)81 napi_value UdmfClientImpl::TransformUdmfUnifiedData(RefPtr<UnifiedData>& UnifiedData)
82 {
83     auto engine = EngineHelper::GetCurrentEngine();
84     CHECK_NULL_RETURN(engine, nullptr);
85     NativeEngine* nativeEngine = engine->GetNativeEngine();
86     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
87     auto unifiedDataImpl = AceType::DynamicCast<UnifiedDataImpl>(UnifiedData);
88     CHECK_NULL_RETURN(unifiedDataImpl, nullptr);
89     auto unifiedData = unifiedDataImpl->GetUnifiedData();
90     CHECK_NULL_RETURN(unifiedData, nullptr);
91     napi_value dataVal = nullptr;
92     UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, dataVal);
93     CHECK_NULL_RETURN(dataVal, nullptr);
94     return dataVal;
95 }
96 
TransformUnifiedDataPtr(RefPtr<UnifiedData> & unifiedDataImpl)97 void* UdmfClientImpl::TransformUnifiedDataPtr(RefPtr<UnifiedData>& unifiedDataImpl)
98 {
99     CHECK_NULL_RETURN(unifiedDataImpl, nullptr);
100     std::shared_ptr<UDMF::UnifiedData> unifiedData =
101         AceType::DynamicCast<UnifiedDataImpl>(unifiedDataImpl)->GetUnifiedData();
102     CHECK_NULL_RETURN(unifiedData, nullptr);
103     return unifiedData.get();
104 }
105 
TransformUnifiedDataForNative(void * rawData)106 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedDataForNative(void* rawData)
107 {
108     CHECK_NULL_RETURN(rawData, nullptr);
109     auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
110     auto udmfData = static_cast<OH_UdmfData*>(rawData);
111     CHECK_NULL_RETURN(udmfData, nullptr);
112     auto unifiedData = std::make_shared<UDMF::UnifiedData>();
113     auto status = OHOS::UDMF::NdkDataConversion::GetNativeUnifiedData(udmfData, unifiedData);
114     if (status) {
115         return nullptr;
116     }
117 
118     udData->SetUnifiedData(unifiedData);
119     return udData;
120 }
121 
TransformDataLoadParamsForNative(void * rawData)122 RefPtr<DataLoadParams> UdmfClientImpl::TransformDataLoadParamsForNative(void* rawData)
123 {
124     CHECK_NULL_RETURN(rawData, nullptr);
125     auto udDataLoadParams = AceType::MakeRefPtr<DataLoadParamsImpl>();
126     auto udmfDataLoadParams = static_cast<OH_UdmfDataLoadParams*>(rawData);
127     CHECK_NULL_RETURN(udmfDataLoadParams, nullptr);
128     UDMF::DataLoadParams dataLoadParams;
129     auto status = OHOS::UDMF::DataParamsConversion::GetDataLoaderParams(*udmfDataLoadParams, dataLoadParams);
130     if (status) {
131         return nullptr;
132     }
133     udDataLoadParams->SetDataLoadParams(std::make_shared<UDMF::DataLoadParams>(dataLoadParams));
134     return udDataLoadParams;
135 }
136 
TransformSummary(std::map<std::string,int64_t> & summary)137 napi_value UdmfClientImpl::TransformSummary(std::map<std::string, int64_t>& summary)
138 {
139     auto engine = EngineHelper::GetCurrentEngine();
140     CHECK_NULL_RETURN(engine, nullptr);
141     NativeEngine* nativeEngine = engine->GetNativeEngine();
142     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
143     std::shared_ptr<UDMF::Summary> udmfSummary = std::make_shared<UDMF::Summary>();
144     CHECK_NULL_RETURN(udmfSummary, nullptr);
145     udmfSummary->totalSize = 0;
146     for (auto element : summary) {
147         udmfSummary->totalSize += element.second;
148     }
149     udmfSummary->summary = std::move(summary);
150     napi_value dataVal = nullptr;
151     UDMF::SummaryNapi::NewInstance(env, udmfSummary, dataVal);
152     CHECK_NULL_RETURN(dataVal, nullptr);
153     return dataVal;
154 }
155 
SetData(const RefPtr<UnifiedData> & unifiedData,std::string & key)156 int32_t UdmfClientImpl::SetData(const RefPtr<UnifiedData>& unifiedData, std::string& key)
157 {
158     auto& client = UDMF::UdmfClient::GetInstance();
159     UDMF::CustomOption udCustomOption;
160     udCustomOption.intention = UDMF::Intention::UD_INTENTION_DRAG;
161     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
162     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
163     int32_t ret = client.SetData(udCustomOption, *udData->GetUnifiedData(), key);
164     return ret;
165 }
166 
SetDelayInfo(RefPtr<DataLoadParams> dataLoadParams,std::string & key)167 int32_t UdmfClientImpl::SetDelayInfo(RefPtr<DataLoadParams> dataLoadParams, std::string& key)
168 {
169     CHECK_NULL_RETURN(dataLoadParams, UDMF::E_ERROR);
170     auto& client = UDMF::UdmfClient::GetInstance();
171     auto udDataLoadParams = AceType::DynamicCast<DataLoadParamsImpl>(dataLoadParams);
172     CHECK_NULL_RETURN(udDataLoadParams, UDMF::E_ERROR);
173     CHECK_NULL_RETURN(udDataLoadParams->GetDataLoadParams(), UDMF::E_ERROR);
174     int32_t ret = client.SetDelayInfo(*udDataLoadParams->GetDataLoadParams(), key);
175     return ret;
176 }
177 
GetData(const RefPtr<UnifiedData> & unifiedData,const std::string & key)178 int32_t UdmfClientImpl::GetData(const RefPtr<UnifiedData>& unifiedData, const std::string& key)
179 {
180     auto& client = UDMF::UdmfClient::GetInstance();
181     UDMF::QueryOption queryOption;
182     queryOption.key = key;
183     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
184     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
185     int ret = client.GetData(queryOption, *udData->GetUnifiedData());
186     return ret;
187 }
188 
StartAsyncDataRetrieval(napi_env env,napi_value napiValue,const std::string & key)189 int32_t UdmfClientImpl::StartAsyncDataRetrieval(napi_env env, napi_value napiValue, const std::string& key)
190 {
191     UDMF::GetDataParams getDataParams;
192     getDataParams.query.key = key;
193     getDataParams.query.intention = UDMF::Intention::UD_INTENTION_DRAG;
194     auto status = UDMF::GetDataParamsNapi::Convert2NativeValue(env, napiValue, getDataParams, key);
195     if (!status) {
196         return -1;
197     }
198     return static_cast<int32_t>(UDMF::UdmfAsyncClient::GetInstance().StartAsyncDataRetrieval(getDataParams));
199 }
200 
Cancel(const std::string & key)201 int32_t UdmfClientImpl::Cancel(const std::string& key)
202 {
203     return static_cast<int32_t>(UDMF::UdmfAsyncClient::GetInstance().Cancel(key));
204 }
205 
GetSummary(std::string & key,DragSummaryInfo & dragSummaryInfo)206 int32_t UdmfClientImpl::GetSummary(std::string& key, DragSummaryInfo& dragSummaryInfo)
207 {
208     auto& client = UDMF::UdmfClient::GetInstance();
209     UDMF::Summary summary;
210     UDMF::QueryOption queryOption;
211     queryOption.key = key;
212     int32_t ret = client.GetSummary(queryOption, summary);
213     if (ret != 0) {
214         return ret;
215     }
216     dragSummaryInfo.summary = summary.summary;
217     dragSummaryInfo.detailedSummary = summary.specificSummary;
218     dragSummaryInfo.summaryFormat = summary.summaryFormat;
219     dragSummaryInfo.version = summary.version;
220     dragSummaryInfo.totalSize = summary.totalSize;
221     return ret;
222 }
223 
GetRemoteStatus(std::string & key)224 bool UdmfClientImpl::GetRemoteStatus(std::string& key)
225 {
226     auto& client = UDMF::UdmfClient::GetInstance();
227     bool isRemoteData = false;
228     UDMF::QueryOption queryOption;
229     queryOption.key = key;
230     int32_t ret = client.IsRemoteData(queryOption, isRemoteData);
231     if (ret != 0) {
232         // if ret is not 0, udmf client has not been sync, so return true to use remote getData.
233         return true;
234     }
235     return isRemoteData;
236 }
237 
GetSize()238 int64_t UnifiedDataImpl::GetSize()
239 {
240     CHECK_NULL_RETURN(unifiedData_, 0);
241     return unifiedData_->GetRecords().size();
242 }
243 
GetUnifiedData()244 std::shared_ptr<UDMF::UnifiedData> UnifiedDataImpl::GetUnifiedData()
245 {
246     if (unifiedData_ == nullptr) {
247         unifiedData_ = std::make_shared<UDMF::UnifiedData>();
248     }
249     return unifiedData_;
250 }
251 
SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)252 void UnifiedDataImpl::SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)
253 {
254     unifiedData_ = unifiedData;
255 }
256 
GetRecordCount()257 uint32_t DataLoadParamsImpl::GetRecordCount()
258 {
259     CHECK_NULL_RETURN(dataLoadParams_, 0);
260     return dataLoadParams_->dataLoadInfo.recordCount;
261 }
262 
GetDataLoadParams() const263 std::shared_ptr<UDMF::DataLoadParams> DataLoadParamsImpl::GetDataLoadParams() const
264 {
265     return dataLoadParams_;
266 }
267 
SetDataLoadParams(const std::shared_ptr<UDMF::DataLoadParams> & dataLoadParams)268 void DataLoadParamsImpl::SetDataLoadParams(const std::shared_ptr<UDMF::DataLoadParams>& dataLoadParams)
269 {
270     dataLoadParams_ = dataLoadParams;
271 }
272 
AddFormRecord(const RefPtr<UnifiedData> & unifiedData,int32_t formId,const RequestFormInfo & cardInfo)273 void UdmfClientImpl::AddFormRecord(
274     const RefPtr<UnifiedData>& unifiedData, int32_t formId, const RequestFormInfo& cardInfo)
275 {
276     auto formRecord = std::make_shared<UDMF::SystemDefinedForm>();
277     formRecord->SetFormId(formId);
278     formRecord->SetFormName(cardInfo.cardName);
279     formRecord->SetBundleName(cardInfo.bundleName);
280     formRecord->SetAbilityName(cardInfo.abilityName);
281     formRecord->SetModule(cardInfo.moduleName);
282     formRecord->SetType(UDMF::UDType::SYSTEM_DEFINED_FORM);
283 
284     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
285     CHECK_NULL_VOID(udData);
286     CHECK_NULL_VOID(udData->GetUnifiedData());
287     udData->GetUnifiedData()->AddRecord(formRecord);
288 }
289 
AddLinkRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & url,const std::string & description)290 void UdmfClientImpl::AddLinkRecord(
291     const RefPtr<UnifiedData>& unifiedData, const std::string& url, const std::string& description)
292 {
293     auto record = std::make_shared<UDMF::Link>(url, description);
294 
295     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
296     CHECK_NULL_VOID(udData);
297     CHECK_NULL_VOID(udData->GetUnifiedData());
298     udData->GetUnifiedData()->AddRecord(record);
299 }
300 
GetLinkRecord(const RefPtr<UnifiedData> & unifiedData,std::string & url,std::string & description)301 void UdmfClientImpl::GetLinkRecord(
302     const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
303 {
304     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
305     CHECK_NULL_VOID(udData);
306     CHECK_NULL_VOID(udData->GetUnifiedData());
307     auto records = udData->GetUnifiedData()->GetRecords();
308     for (auto record : records) {
309         UDMF::UDType type = record->GetType();
310         if (type == UDMF::UDType::HYPERLINK) {
311             UDMF::Link* link = reinterpret_cast<UDMF::Link*>(record.get());
312             url = link->GetUrl();
313             description = link->GetDescription();
314             return;
315         }
316     }
317 }
318 
AddHtmlRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & htmlContent,const std::string & plainContent)319 void UdmfClientImpl::AddHtmlRecord(
320     const RefPtr<UnifiedData>& unifiedData, const std::string& htmlContent, const std::string& plainContent)
321 {
322     auto htmlRecord = std::make_shared<UDMF::Html>(htmlContent, plainContent);
323 
324     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
325     CHECK_NULL_VOID(udData);
326     CHECK_NULL_VOID(udData->GetUnifiedData());
327     if (!plainContent.empty() || !htmlContent.empty()) {
328         udData->GetUnifiedData()->AddRecord(htmlRecord);
329     }
330 }
331 
GetHtmlRecord(const RefPtr<UnifiedData> & unifiedData,std::string & htmlContent,std::string & plainContent)332 void UdmfClientImpl::GetHtmlRecord(
333     const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
334 {
335     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
336     CHECK_NULL_VOID(udData);
337     CHECK_NULL_VOID(udData->GetUnifiedData());
338     auto records = udData->GetUnifiedData()->GetRecords();
339     for (auto record : records) {
340         UDMF::UDType type = record->GetType();
341         if (type == UDMF::UDType::HTML) {
342             UDMF::Html* html = reinterpret_cast<UDMF::Html*>(record.get());
343             plainContent = html->GetPlainContent();
344             htmlContent = html->GetHtmlContent();
345             return;
346         }
347     }
348 }
349 
AddPixelMapRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<uint8_t> & data,PixelMapRecordDetails & details)350 void UdmfClientImpl::AddPixelMapRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data,
351     PixelMapRecordDetails& details)
352 {
353     auto record = std::make_shared<UDMF::SystemDefinedPixelMap>(data);
354     UDMF::UDDetails uDetails = {
355         { "width", details.width },
356         { "height", details.height },
357         { "pixel-format", static_cast<int32_t>(details.pixelFormat) },
358         { "alpha-type", static_cast<int32_t>(details.alphaType) } };
359     record->SetDetails(uDetails);
360     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
361     CHECK_NULL_VOID(udData);
362     CHECK_NULL_VOID(udData->GetUnifiedData());
363     udData->GetUnifiedData()->AddRecord(record);
364 }
365 
AddImageRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & uri)366 void UdmfClientImpl::AddImageRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& uri)
367 {
368     auto record = std::make_shared<UDMF::Image>(uri);
369 
370     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
371     CHECK_NULL_VOID(udData);
372     CHECK_NULL_VOID(udData->GetUnifiedData());
373     udData->GetUnifiedData()->AddRecord(record);
374 }
375 
AddPlainTextRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & selectedStr)376 void UdmfClientImpl::AddPlainTextRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& selectedStr)
377 {
378     auto record = std::make_shared<UDMF::PlainText>(selectedStr, "");
379 
380     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
381     CHECK_NULL_VOID(udData);
382     CHECK_NULL_VOID(udData->GetUnifiedData());
383     udData->GetUnifiedData()->AddRecord(record);
384 }
385 
GetSinglePlainTextRecord(const RefPtr<UnifiedData> & unifiedData)386 std::string UdmfClientImpl::GetSinglePlainTextRecord(const RefPtr<UnifiedData>& unifiedData)
387 {
388     std::string str = "";
389     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
390     CHECK_NULL_RETURN(udData, str);
391     CHECK_NULL_RETURN(udData->GetUnifiedData(), str);
392     auto records = udData->GetUnifiedData()->GetRecords();
393     if (records.size() >= 1 && records[0]->GetType() == UDMF::UDType::PLAIN_TEXT) {
394         UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(records[0].get());
395         str = plainText->GetContent();
396     }
397     return str;
398 }
399 
AddFileUriRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)400 bool UdmfClientImpl::AddFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
401 {
402     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
403     CHECK_NULL_RETURN(udData, false);
404     CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
405 
406     for (std::string u : uri) {
407         std::vector<std::string> types;
408         std::string belongsToType = "general.image";
409         char pointChar = '.';
410         size_t pos = u.rfind(pointChar);
411         std::string filenameExtension;
412         if (pos != std::string::npos) {
413             filenameExtension = u.substr(pos);
414             LOGI("DragDrop event AddFileUriRecord, filename extension is %{public}s", filenameExtension.c_str());
415         }
416         auto status = UDMF::UtdClient::GetInstance().GetUniformDataTypesByFilenameExtension(
417             filenameExtension, types, belongsToType);
418         if (status == UDMF::Status::E_OK && types.size() > 0) {
419             LOGI("DragDrop event AddFileUriRecord, extension type is %{public}s", types[0].c_str());
420             std::shared_ptr<UDMF::Object> obj = std::make_shared<UDMF::Object>();
421             obj->value_[UDMF::UNIFORM_DATA_TYPE] = "general.file-uri";
422             obj->value_[UDMF::FILE_URI_PARAM] = u;
423             obj->value_[UDMF::FILE_TYPE] = types[0];
424             auto record = std::make_shared<UDMF::UnifiedRecord>(UDMF::UDType::FILE_URI, obj);
425             udData->GetUnifiedData()->AddRecord(record);
426         }
427     }
428 
429     return true;
430 }
431 
GetFileUriRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)432 bool UdmfClientImpl::GetFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
433 {
434     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
435     CHECK_NULL_RETURN(udData, false);
436     CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
437     auto records = udData->GetUnifiedData()->GetRecords();
438 
439     for (auto record : records) {
440         UDMF::UDType type = record->GetType();
441         if (type == UDMF::UDType::IMAGE || type == UDMF::UDType::AUDIO ||
442             type == UDMF::UDType::VIDEO || type == UDMF::UDType::FILE) {
443             UDMF::File* file = reinterpret_cast<UDMF::File*>(record.get());
444             if (file) {
445                 uri.emplace_back(file->GetUri());
446                 LOGI("DragDrop event GetFileUri, uri:%{public}s", file->GetUri().c_str());
447             } else {
448                 LOGE("DragDrop event GetFileUri file is null");
449             }
450         }
451     }
452     return true;
453 }
454 
GetPlainTextRecords(const RefPtr<UnifiedData> & unifiedData)455 std::vector<std::string> UdmfClientImpl::GetPlainTextRecords(const RefPtr<UnifiedData>& unifiedData)
456 {
457     std::vector<std::string> textRecords;
458     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
459     CHECK_NULL_RETURN(udData, textRecords);
460     CHECK_NULL_RETURN(udData->GetUnifiedData(), textRecords);
461     auto records = udData->GetUnifiedData()->GetRecords();
462     for (const auto& record : records) {
463         UDMF::UDType type = record->GetType();
464         if (type == UDMF::UDType::PLAIN_TEXT) {
465             UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(record.get());
466             std::string str = plainText->GetContent();
467             textRecords.emplace_back(str);
468         }
469     }
470     return textRecords;
471 }
472 
GetVideoRecordUri(const RefPtr<UnifiedData> & unifiedData,std::string & uri)473 int32_t UdmfClientImpl::GetVideoRecordUri(const RefPtr<UnifiedData>& unifiedData, std::string& uri)
474 {
475     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
476     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
477     CHECK_NULL_RETURN(udData->GetUnifiedData(), UDMF::E_ERROR);
478     auto records = udData->GetUnifiedData()->GetRecords();
479     if (records.size() == 0) {
480         return UDMF::E_ERROR;
481     }
482     auto video = static_cast<UDMF::Video*>(records[0].get());
483     uri = video->GetUri();
484     return 0;
485 }
486 
GetErrorInfo(int32_t errorCode)487 std::pair<int32_t, std::string> UdmfClientImpl::GetErrorInfo(int32_t errorCode)
488 {
489     switch (errorCode) {
490         case UDMF::E_NOT_FOUND:
491             return { ERROR_CODE_DRAG_DATA_NOT_FOUND,
492                 "GetData failed, data not found. Possible causes: 1.The data is too large and has not been "
493                 "synchronized yet; 2.No permission to access data." };
494         default:
495             return { ERROR_CODE_DRAG_DATA_ERROR,
496                 "GetData failed, data error. Possible cause: Data synchronization failed." };
497     }
498 }
499 
AddSpanStringRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<uint8_t> & data)500 void UdmfClientImpl::AddSpanStringRecord(
501     const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data)
502 {
503     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
504     CHECK_NULL_VOID(udData);
505     CHECK_NULL_VOID(udData->GetUnifiedData());
506     auto record = std::make_shared<UDMF::ApplicationDefinedRecord>("OPENHARMONY_STYLED_STRING_UDMF", data);
507     udData->GetUnifiedData()->AddRecord(record);
508 }
509 
GetSpanStringRecord(const RefPtr<UnifiedData> & unifiedData)510 std::vector<uint8_t> UdmfClientImpl::GetSpanStringRecord(const RefPtr<UnifiedData>& unifiedData)
511 {
512     std::vector<uint8_t> arr;
513     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
514     CHECK_NULL_RETURN(udData, arr);
515     CHECK_NULL_RETURN(udData->GetUnifiedData(), arr);
516     auto records = udData->GetUnifiedData()->GetRecords();
517     for (auto record: records) {
518         UDMF::UDType type = record->GetType();
519         if (type == UDMF::UDType::APPLICATION_DEFINED_RECORD) {
520             UDMF::ApplicationDefinedRecord* app = reinterpret_cast<UDMF::ApplicationDefinedRecord*>(record.get());
521             if (app->GetApplicationDefinedType() == "OPENHARMONY_STYLED_STRING_UDMF") {
522                 arr = app->GetRawData();
523                 return arr;
524             }
525         }
526     }
527     return arr;
528 }
529 
SetTagProperty(const RefPtr<UnifiedData> & unifiedData,const std::string & tag)530 void UdmfClientImpl::SetTagProperty(const RefPtr<UnifiedData>& unifiedData, const std::string& tag)
531 {
532     auto properties = std::make_shared<UDMF::UnifiedDataProperties>();
533     properties->tag = tag;
534     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
535     CHECK_NULL_VOID(udData);
536     CHECK_NULL_VOID(udData->GetUnifiedData());
537     udData->GetUnifiedData()->SetProperties(properties);
538 }
539 
GetPlainTextEntry(const RefPtr<UnifiedData> & unifiedData)540 std::string UdmfClientImpl::GetPlainTextEntry(const RefPtr<UnifiedData>& unifiedData)
541 {
542     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
543     CHECK_NULL_RETURN(udData, std::string());
544     CHECK_NULL_RETURN(udData->GetUnifiedData(), std::string());
545     auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
546     CHECK_NULL_RETURN(udData->GetUnifiedData()->HasType(utdId), std::string());
547     std::string plainTextSum;
548     auto records = udData->GetUnifiedData()->GetRecords();
549     for (const auto& record : records) {
550         std::string plainText;
551         auto value = record->GetEntry(utdId);
552         if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
553             auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
554             object->GetValue(UDMF::CONTENT, plainText);
555         } else if (std::holds_alternative<std::string>(value)) {
556             plainText = std::get<std::string>(value);
557         }
558         plainTextSum += plainText;
559     }
560 
561     if (plainTextSum.empty()) {
562         std::vector<std::string> plains = GetPlainTextRecords(unifiedData);
563         if (!plains.empty()) {
564             plainTextSum = plains[0];
565         }
566     }
567     return plainTextSum;
568 }
569 
GetHtmlEntry(const RefPtr<UnifiedData> & unifiedData,std::string & htmlContent,std::string & plainContent)570 void UdmfClientImpl::GetHtmlEntry(
571     const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
572 {
573     CHECK_NULL_VOID(htmlContent.empty() && plainContent.empty());
574     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
575     CHECK_NULL_VOID(udData);
576     CHECK_NULL_VOID(udData->GetUnifiedData());
577     auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HTML);
578     CHECK_NULL_VOID(udData->GetUnifiedData()->HasType(utdId));
579     auto records = udData->GetUnifiedData()->GetRecords();
580     for (auto record : records) {
581         std::string htmlText;
582         std::string plainText;
583         auto value = record->GetEntry(utdId);
584         if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
585             auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
586             object->GetValue(UDMF::HTML_CONTENT, htmlText);
587             object->GetValue(UDMF::PLAIN_CONTENT, plainText);
588         }
589         htmlContent += htmlText;
590         plainContent += plainText;
591     }
592 
593     if (htmlContent.empty() && plainContent.empty()) {
594         GetHtmlRecord(unifiedData, htmlContent, plainContent);
595     }
596 }
597 
GetLinkEntry(const RefPtr<UnifiedData> & unifiedData,std::string & url,std::string & description)598 void UdmfClientImpl::GetLinkEntry(const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
599 {
600     CHECK_NULL_VOID(url.empty() && description.empty());
601     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
602     CHECK_NULL_VOID(udData);
603     CHECK_NULL_VOID(udData->GetUnifiedData());
604     auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HYPERLINK);
605     CHECK_NULL_VOID(udData->GetUnifiedData()->HasType(utdId));
606     auto records = udData->GetUnifiedData()->GetRecords();
607     for (auto record : records) {
608         std::string currentUrl;
609         std::string currentDes;
610         auto value = record->GetEntry(utdId);
611         if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
612             auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
613             object->GetValue(UDMF::URL, currentUrl);
614             object->GetValue(UDMF::DESCRIPTION, currentDes);
615         }
616         url += currentUrl;
617         description += currentDes;
618     }
619 
620     if (url.empty() && description.empty()) {
621         GetLinkRecord(unifiedData, url, description);
622     }
623 }
624 
GetFileUriEntry(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)625 bool UdmfClientImpl::GetFileUriEntry(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
626 {
627     CHECK_NULL_RETURN(uri.empty(), false);
628     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
629     CHECK_NULL_RETURN(udData, false);
630     CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
631     auto records = udData->GetUnifiedData()->GetRecords();
632     auto typeList = { UDMF::UDType::IMAGE, UDMF::UDType::AUDIO, UDMF::UDType::VIDEO, UDMF::UDType::FILE,
633         UDMF::UDType::FILE_URI };
634     for (auto record : records) {
635         std::string currentUri;
636         for (auto type : typeList) {
637             auto value = record->GetEntry(UDMF::UtdUtils::GetUtdIdFromUtdEnum(type));
638             if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
639                 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
640                 std::string currentEntryUri;
641                 object->GetValue(UDMF::ORI_URI, currentEntryUri);
642                 uri.emplace_back(currentEntryUri);
643             }
644         }
645     }
646 
647     if (uri.empty()) {
648         GetFileUriRecord(unifiedData, uri);
649     }
650     return true;
651 }
652 
GetSpanStringEntry(const RefPtr<UnifiedData> & unifiedData)653 std::vector<uint8_t> UdmfClientImpl::GetSpanStringEntry(const RefPtr<UnifiedData>& unifiedData)
654 {
655     std::vector<uint8_t> arr;
656     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
657     CHECK_NULL_RETURN(udData, arr);
658     CHECK_NULL_RETURN(udData->GetUnifiedData(), arr);
659     auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD);
660     CHECK_NULL_RETURN(udData->GetUnifiedData()->HasType(utdId), GetSpanStringRecord(unifiedData));
661     auto records = udData->GetUnifiedData()->GetRecords();
662     for (auto record : records) {
663         auto value = record->GetEntry(utdId);
664         if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
665             auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
666             UDMF::ApplicationDefinedRecord* app = reinterpret_cast<UDMF::ApplicationDefinedRecord*>(object.get());
667             if (app && app->GetApplicationDefinedType() == "OPENHARMONY_STYLED_STRING_UDMF") {
668                 arr = app->GetRawData();
669                 return arr;
670             }
671         }
672     }
673     return GetSpanStringRecord(unifiedData);
674 }
675 
IsAppropriateType(DragSummaryInfo & dragSummaryInfo,const std::set<std::string> & allowTypes)676 bool UdmfClientImpl::IsAppropriateType(DragSummaryInfo& dragSummaryInfo, const std::set<std::string>& allowTypes)
677 {
678     UDMF::Summary summary;
679     summary.summary = dragSummaryInfo.summary;
680     summary.specificSummary = dragSummaryInfo.detailedSummary;
681     summary.summaryFormat = dragSummaryInfo.summaryFormat;
682     summary.version = dragSummaryInfo.version;
683     summary.totalSize = dragSummaryInfo.totalSize;
684     auto& client = UDMF::UdmfClient::GetInstance();
685     std::vector<std::string> allowTypesArr(allowTypes.begin(), allowTypes.end());
686     return client.IsAppropriateType(summary, allowTypesArr);
687 }
688 
689 #if defined(ACE_STATIC)
TransformUnifiedDataFromANI(void * rawData)690 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedDataFromANI(void* rawData)
691 {
692     CHECK_NULL_RETURN(rawData, nullptr);
693     auto unifiedDataPtr = reinterpret_cast<UDMF::UnifiedData*>(rawData);
694     std::shared_ptr<UDMF::UnifiedData> unifiedData(unifiedDataPtr);
695     auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
696     udData->SetUnifiedData(unifiedData);
697     return udData;
698 }
699 
TransformSummaryANI(std::map<std::string,int64_t> & summary,void * summaryPtr)700 void UdmfClientImpl::TransformSummaryANI(std::map<std::string, int64_t>& summary, void* summaryPtr)
701 {
702     auto udmfSummary = reinterpret_cast<UDMF::Summary*>(summaryPtr);
703     CHECK_NULL_VOID(udmfSummary);
704     udmfSummary->totalSize = 0;
705     for (auto element : summary) {
706         udmfSummary->totalSize += element.second;
707     }
708     udmfSummary->summary = std::move(summary);
709 }
710 #endif
711 } // namespace OHOS::Ace
712