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 "html.h"
19 #include "image.h"
20 #include "link.h"
21 #include "summary_napi.h"
22 #include "system_defined_form.h"
23 #include "system_defined_pixelmap.h"
24 #include "plain_text.h"
25 #include "application_defined_record.h"
26 #include "get_data_params_napi.h"
27 #include "udmf_async_client.h"
28 #include "unified_data_napi.h"
29 #include "video.h"
30 #include "frameworks/bridge/common/utils/engine_helper.h"
31 #include "frameworks/bridge/common/utils/utils.h"
32
33 #include "ndk_data_conversion.h"
34
35 namespace OHOS::Ace {
GetInstance()36 UdmfClient* UdmfClient::GetInstance()
37 {
38 static UdmfClientImpl instance;
39 return &instance;
40 }
41
CreateUnifiedData()42 RefPtr<UnifiedData> UdmfClientImpl::CreateUnifiedData()
43 {
44 return AceType::DynamicCast<UnifiedData>(AceType::MakeRefPtr<UnifiedDataImpl>());
45 }
46
TransformUnifiedData(napi_value napiValue)47 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedData(napi_value napiValue)
48 {
49 auto engine = EngineHelper::GetCurrentEngine();
50 CHECK_NULL_RETURN(engine, nullptr);
51 NativeEngine* nativeEngine = engine->GetNativeEngine();
52 napi_env env = reinterpret_cast<napi_env>(nativeEngine);
53 void* native = nullptr;
54 napi_unwrap(env, napiValue, &native);
55 auto* unifiedData = reinterpret_cast<UDMF::UnifiedDataNapi*>(native);
56 CHECK_NULL_RETURN(unifiedData, nullptr);
57 CHECK_NULL_RETURN(unifiedData->value_, nullptr);
58 auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
59 udData->SetUnifiedData(unifiedData->value_);
60 return udData;
61 }
62
TransformUdmfUnifiedData(RefPtr<UnifiedData> & UnifiedData)63 napi_value UdmfClientImpl::TransformUdmfUnifiedData(RefPtr<UnifiedData>& UnifiedData)
64 {
65 auto engine = EngineHelper::GetCurrentEngine();
66 CHECK_NULL_RETURN(engine, nullptr);
67 NativeEngine* nativeEngine = engine->GetNativeEngine();
68 napi_env env = reinterpret_cast<napi_env>(nativeEngine);
69 auto unifiedDataImpl = AceType::DynamicCast<UnifiedDataImpl>(UnifiedData);
70 CHECK_NULL_RETURN(unifiedDataImpl, nullptr);
71 auto unifiedData = unifiedDataImpl->GetUnifiedData();
72 CHECK_NULL_RETURN(unifiedData, nullptr);
73 napi_value dataVal = nullptr;
74 UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, dataVal);
75 CHECK_NULL_RETURN(dataVal, nullptr);
76 return dataVal;
77 }
78
TransformUnifiedDataPtr(RefPtr<UnifiedData> & unifiedDataImpl)79 void* UdmfClientImpl::TransformUnifiedDataPtr(RefPtr<UnifiedData>& unifiedDataImpl)
80 {
81 CHECK_NULL_RETURN(unifiedDataImpl, nullptr);
82 std::shared_ptr<UDMF::UnifiedData> unifiedData =
83 AceType::DynamicCast<UnifiedDataImpl>(unifiedDataImpl)->GetUnifiedData();
84 CHECK_NULL_RETURN(unifiedData, nullptr);
85 return unifiedData.get();
86 }
87
TransformUnifiedDataForNative(void * rawData)88 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedDataForNative(void* rawData)
89 {
90 CHECK_NULL_RETURN(rawData, nullptr);
91 auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
92 auto udmfData = static_cast<OH_UdmfData*>(rawData);
93 CHECK_NULL_RETURN(udmfData, nullptr);
94 auto unifiedData = std::make_shared<UDMF::UnifiedData>();
95 auto status = OHOS::UDMF::NdkDataConversion::GetNativeUnifiedData(udmfData, unifiedData);
96 if (status) {
97 return nullptr;
98 }
99
100 udData->SetUnifiedData(unifiedData);
101 return udData;
102 }
103
TransformSummary(std::map<std::string,int64_t> & summary)104 napi_value UdmfClientImpl::TransformSummary(std::map<std::string, int64_t>& summary)
105 {
106 auto engine = EngineHelper::GetCurrentEngine();
107 CHECK_NULL_RETURN(engine, nullptr);
108 NativeEngine* nativeEngine = engine->GetNativeEngine();
109 napi_env env = reinterpret_cast<napi_env>(nativeEngine);
110 std::shared_ptr<UDMF::Summary> udmfSummary = std::make_shared<UDMF::Summary>();
111 CHECK_NULL_RETURN(udmfSummary, nullptr);
112 udmfSummary->totalSize = 0;
113 for (auto element : summary) {
114 udmfSummary->totalSize += element.second;
115 }
116 udmfSummary->summary = std::move(summary);
117 napi_value dataVal = nullptr;
118 UDMF::SummaryNapi::NewInstance(env, udmfSummary, dataVal);
119 CHECK_NULL_RETURN(dataVal, nullptr);
120 return dataVal;
121 }
122
SetData(const RefPtr<UnifiedData> & unifiedData,std::string & key)123 int32_t UdmfClientImpl::SetData(const RefPtr<UnifiedData>& unifiedData, std::string& key)
124 {
125 auto& client = UDMF::UdmfClient::GetInstance();
126 UDMF::CustomOption udCustomOption;
127 udCustomOption.intention = UDMF::Intention::UD_INTENTION_DRAG;
128 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
129 CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
130 int32_t ret = client.SetData(udCustomOption, *udData->GetUnifiedData(), key);
131 return ret;
132 }
133
GetData(const RefPtr<UnifiedData> & unifiedData,const std::string & key)134 int32_t UdmfClientImpl::GetData(const RefPtr<UnifiedData>& unifiedData, const std::string& key)
135 {
136 auto& client = UDMF::UdmfClient::GetInstance();
137 UDMF::QueryOption queryOption;
138 queryOption.key = key;
139 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
140 CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
141 int ret = client.GetData(queryOption, *udData->GetUnifiedData());
142 return ret;
143 }
144
StartAsyncDataRetrieval(napi_env env,napi_value napiValue,const std::string & key)145 int32_t UdmfClientImpl::StartAsyncDataRetrieval(napi_env env, napi_value napiValue, const std::string& key)
146 {
147 UDMF::GetDataParams getDataParams;
148 getDataParams.query.key = key;
149 getDataParams.query.intention = UDMF::Intention::UD_INTENTION_DRAG;
150 auto status = UDMF::GetDataParamsNapi::Convert2NativeValue(env, napiValue, getDataParams, key);
151 if (!status) {
152 return -1;
153 }
154 return static_cast<int32_t>(UDMF::UdmfAsyncClient::GetInstance().StartAsyncDataRetrieval(getDataParams));
155 }
156
Cancel(const std::string & key)157 int32_t UdmfClientImpl::Cancel(const std::string& key)
158 {
159 return static_cast<int32_t>(UDMF::UdmfAsyncClient::GetInstance().Cancel(key));
160 }
161
GetSummary(std::string & key,std::map<std::string,int64_t> & summaryMap)162 int32_t UdmfClientImpl::GetSummary(std::string& key, std::map<std::string, int64_t>& summaryMap)
163 {
164 auto& client = UDMF::UdmfClient::GetInstance();
165 UDMF::Summary summary;
166 UDMF::QueryOption queryOption;
167 queryOption.key = key;
168 int32_t ret = client.GetSummary(queryOption, summary);
169 summaryMap = summary.summary;
170 return ret;
171 }
172
GetRemoteStatus(std::string & key)173 bool UdmfClientImpl::GetRemoteStatus(std::string& key)
174 {
175 auto& client = UDMF::UdmfClient::GetInstance();
176 bool isRemoteData = false;
177 UDMF::QueryOption queryOption;
178 queryOption.key = key;
179 int32_t ret = client.IsRemoteData(queryOption, isRemoteData);
180 if (ret != 0) {
181 // if ret is not 0, udmf client has not been sync, so return true to use remote getData.
182 return true;
183 }
184 return isRemoteData;
185 }
186
GetSize()187 int64_t UnifiedDataImpl::GetSize()
188 {
189 CHECK_NULL_RETURN(unifiedData_, 0);
190 return unifiedData_->GetRecords().size();
191 }
192
GetUnifiedData()193 std::shared_ptr<UDMF::UnifiedData> UnifiedDataImpl::GetUnifiedData()
194 {
195 if (unifiedData_ == nullptr) {
196 unifiedData_ = std::make_shared<UDMF::UnifiedData>();
197 }
198 return unifiedData_;
199 }
200
SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)201 void UnifiedDataImpl::SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)
202 {
203 unifiedData_ = unifiedData;
204 }
205
AddFormRecord(const RefPtr<UnifiedData> & unifiedData,int32_t formId,const RequestFormInfo & cardInfo)206 void UdmfClientImpl::AddFormRecord(
207 const RefPtr<UnifiedData>& unifiedData, int32_t formId, const RequestFormInfo& cardInfo)
208 {
209 auto formRecord = std::make_shared<UDMF::SystemDefinedForm>();
210 formRecord->SetFormId(formId);
211 formRecord->SetFormName(cardInfo.cardName);
212 formRecord->SetBundleName(cardInfo.bundleName);
213 formRecord->SetAbilityName(cardInfo.abilityName);
214 formRecord->SetModule(cardInfo.moduleName);
215 formRecord->SetType(UDMF::UDType::SYSTEM_DEFINED_FORM);
216
217 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
218 CHECK_NULL_VOID(udData);
219 CHECK_NULL_VOID(udData->GetUnifiedData());
220 udData->GetUnifiedData()->AddRecord(formRecord);
221 }
222
AddLinkRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & url,const std::string & description)223 void UdmfClientImpl::AddLinkRecord(
224 const RefPtr<UnifiedData>& unifiedData, const std::string& url, const std::string& description)
225 {
226 auto record = std::make_shared<UDMF::Link>(url, description);
227
228 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
229 CHECK_NULL_VOID(udData);
230 CHECK_NULL_VOID(udData->GetUnifiedData());
231 udData->GetUnifiedData()->AddRecord(record);
232 }
233
GetLinkRecord(const RefPtr<UnifiedData> & unifiedData,std::string & url,std::string & description)234 void UdmfClientImpl::GetLinkRecord(
235 const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
236 {
237 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
238 CHECK_NULL_VOID(udData);
239 CHECK_NULL_VOID(udData->GetUnifiedData());
240 auto records = udData->GetUnifiedData()->GetRecords();
241 for (auto record : records) {
242 UDMF::UDType type = record->GetType();
243 if (type == UDMF::UDType::HYPERLINK) {
244 UDMF::Link* link = reinterpret_cast<UDMF::Link*>(record.get());
245 url = link->GetUrl();
246 description = link->GetDescription();
247 return;
248 }
249 }
250 }
251
AddHtmlRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & htmlContent,const std::string & plainContent)252 void UdmfClientImpl::AddHtmlRecord(
253 const RefPtr<UnifiedData>& unifiedData, const std::string& htmlContent, const std::string& plainContent)
254 {
255 auto htmlRecord = std::make_shared<UDMF::Html>(htmlContent, plainContent);
256
257 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
258 CHECK_NULL_VOID(udData);
259 CHECK_NULL_VOID(udData->GetUnifiedData());
260 if (!plainContent.empty() || !htmlContent.empty()) {
261 udData->GetUnifiedData()->AddRecord(htmlRecord);
262 }
263 }
264
GetHtmlRecord(const RefPtr<UnifiedData> & unifiedData,std::string & htmlContent,std::string & plainContent)265 void UdmfClientImpl::GetHtmlRecord(
266 const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
267 {
268 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
269 CHECK_NULL_VOID(udData);
270 CHECK_NULL_VOID(udData->GetUnifiedData());
271 auto records = udData->GetUnifiedData()->GetRecords();
272 for (auto record : records) {
273 UDMF::UDType type = record->GetType();
274 if (type == UDMF::UDType::HTML) {
275 UDMF::Html* html = reinterpret_cast<UDMF::Html*>(record.get());
276 plainContent = html->GetPlainContent();
277 htmlContent = html->GetHtmlContent();
278 return;
279 }
280 }
281 }
282
AddPixelMapRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<uint8_t> & data,PixelMapRecordDetails & details)283 void UdmfClientImpl::AddPixelMapRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data,
284 PixelMapRecordDetails& details)
285 {
286 auto record = std::make_shared<UDMF::SystemDefinedPixelMap>(data);
287 UDMF::UDDetails uDetails = {
288 { "width", details.width },
289 { "height", details.height },
290 { "pixel-format", static_cast<int32_t>(details.pixelFormat) },
291 { "alpha-type", static_cast<int32_t>(details.alphaType) } };
292 record->SetDetails(uDetails);
293 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
294 CHECK_NULL_VOID(udData);
295 CHECK_NULL_VOID(udData->GetUnifiedData());
296 udData->GetUnifiedData()->AddRecord(record);
297 }
298
AddImageRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & uri)299 void UdmfClientImpl::AddImageRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& uri)
300 {
301 auto record = std::make_shared<UDMF::Image>(uri);
302
303 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
304 CHECK_NULL_VOID(udData);
305 CHECK_NULL_VOID(udData->GetUnifiedData());
306 udData->GetUnifiedData()->AddRecord(record);
307 }
308
AddPlainTextRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & selectedStr)309 void UdmfClientImpl::AddPlainTextRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& selectedStr)
310 {
311 auto record = std::make_shared<UDMF::PlainText>(selectedStr, "");
312
313 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
314 CHECK_NULL_VOID(udData);
315 CHECK_NULL_VOID(udData->GetUnifiedData());
316 udData->GetUnifiedData()->AddRecord(record);
317 }
318
GetSinglePlainTextRecord(const RefPtr<UnifiedData> & unifiedData)319 std::string UdmfClientImpl::GetSinglePlainTextRecord(const RefPtr<UnifiedData>& unifiedData)
320 {
321 std::string str = "";
322 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
323 CHECK_NULL_RETURN(udData, str);
324 CHECK_NULL_RETURN(udData->GetUnifiedData(), str);
325 auto records = udData->GetUnifiedData()->GetRecords();
326 if (records.size() >= 1 && records[0]->GetType() == UDMF::UDType::PLAIN_TEXT) {
327 UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(records[0].get());
328 str = plainText->GetContent();
329 }
330 return str;
331 }
332
AddFileUriRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)333 bool UdmfClientImpl::AddFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
334 {
335 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
336 CHECK_NULL_RETURN(udData, false);
337 CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
338
339 for (std::string u : uri) {
340 LOGI("DragDrop event AddFileUriRecord, uri:%{public}s", u.c_str());
341 auto record = std::make_shared<UDMF::Image>(u);
342 udData->GetUnifiedData()->AddRecord(record);
343 }
344
345 return true;
346 }
347
GetFileUriRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)348 bool UdmfClientImpl::GetFileUriRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
349 {
350 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
351 CHECK_NULL_RETURN(udData, false);
352 CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
353 auto records = udData->GetUnifiedData()->GetRecords();
354
355 for (auto record : records) {
356 UDMF::UDType type = record->GetType();
357 if (type == UDMF::UDType::IMAGE || type == UDMF::UDType::AUDIO ||
358 type == UDMF::UDType::VIDEO || type == UDMF::UDType::FILE) {
359 UDMF::File* file = reinterpret_cast<UDMF::File*>(record.get());
360 if (file) {
361 uri.emplace_back(file->GetUri());
362 LOGI("DragDrop event GetFileUri, uri:%{public}s", file->GetUri().c_str());
363 } else {
364 LOGE("DragDrop event GetFileUri file is null");
365 }
366 }
367 }
368 return true;
369 }
370
GetPlainTextRecords(const RefPtr<UnifiedData> & unifiedData)371 std::vector<std::string> UdmfClientImpl::GetPlainTextRecords(const RefPtr<UnifiedData>& unifiedData)
372 {
373 std::vector<std::string> textRecords;
374 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
375 CHECK_NULL_RETURN(udData, textRecords);
376 CHECK_NULL_RETURN(udData->GetUnifiedData(), textRecords);
377 auto records = udData->GetUnifiedData()->GetRecords();
378 for (const auto& record : records) {
379 UDMF::UDType type = record->GetType();
380 if (type == UDMF::UDType::PLAIN_TEXT) {
381 UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(record.get());
382 std::string str = plainText->GetContent();
383 textRecords.emplace_back(str);
384 }
385 }
386 return textRecords;
387 }
388
GetVideoRecordUri(const RefPtr<UnifiedData> & unifiedData,std::string & uri)389 int32_t UdmfClientImpl::GetVideoRecordUri(const RefPtr<UnifiedData>& unifiedData, std::string& uri)
390 {
391 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
392 CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
393 CHECK_NULL_RETURN(udData->GetUnifiedData(), UDMF::E_ERROR);
394 auto records = udData->GetUnifiedData()->GetRecords();
395 if (records.size() == 0) {
396 return UDMF::E_ERROR;
397 }
398 auto video = static_cast<UDMF::Video*>(records[0].get());
399 uri = video->GetUri();
400 return 0;
401 }
402
GetErrorInfo(int32_t errorCode)403 std::pair<int32_t, std::string> UdmfClientImpl::GetErrorInfo(int32_t errorCode)
404 {
405 switch (errorCode) {
406 case UDMF::E_NOT_FOUND:
407 return { ERROR_CODE_DRAG_DATA_NOT_FOUND, "GetData failed, data not found." };
408 default:
409 return { ERROR_CODE_DRAG_DATA_ERROR, "GetData failed, data error." };
410 }
411 }
412
AddSpanStringRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<uint8_t> & data)413 void UdmfClientImpl::AddSpanStringRecord(
414 const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data)
415 {
416 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
417 CHECK_NULL_VOID(udData);
418 CHECK_NULL_VOID(udData->GetUnifiedData());
419 auto record = std::make_shared<UDMF::ApplicationDefinedRecord>("OPENHARMONY_STYLED_STRING_UDMF", data);
420 udData->GetUnifiedData()->AddRecord(record);
421 }
422
GetSpanStringRecord(const RefPtr<UnifiedData> & unifiedData)423 std::vector<uint8_t> UdmfClientImpl::GetSpanStringRecord(const RefPtr<UnifiedData>& unifiedData)
424 {
425 std::vector<uint8_t> arr;
426 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
427 CHECK_NULL_RETURN(udData, arr);
428 CHECK_NULL_RETURN(udData->GetUnifiedData(), arr);
429 auto records = udData->GetUnifiedData()->GetRecords();
430 for (auto record: records) {
431 UDMF::UDType type = record->GetType();
432 if (type == UDMF::UDType::APPLICATION_DEFINED_RECORD) {
433 UDMF::ApplicationDefinedRecord* app = reinterpret_cast<UDMF::ApplicationDefinedRecord*>(record.get());
434 if (app->GetApplicationDefinedType() == "OPENHARMONY_STYLED_STRING_UDMF") {
435 arr = app->GetRawData();
436 return arr;
437 }
438 }
439 }
440 return arr;
441 }
442
SetTagProperty(const RefPtr<UnifiedData> & unifiedData,const std::string & tag)443 void UdmfClientImpl::SetTagProperty(const RefPtr<UnifiedData>& unifiedData, const std::string& tag)
444 {
445 auto properties = std::make_shared<UDMF::UnifiedDataProperties>();
446 properties->tag = tag;
447 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
448 CHECK_NULL_VOID(udData);
449 CHECK_NULL_VOID(udData->GetUnifiedData());
450 udData->GetUnifiedData()->SetProperties(properties);
451 }
452
GetPlainTextEntry(const RefPtr<UnifiedData> & unifiedData)453 std::string UdmfClientImpl::GetPlainTextEntry(const RefPtr<UnifiedData>& unifiedData)
454 {
455 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
456 CHECK_NULL_RETURN(udData, std::string());
457 CHECK_NULL_RETURN(udData->GetUnifiedData(), std::string());
458 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
459 CHECK_NULL_RETURN(udData->GetUnifiedData()->HasType(utdId), std::string());
460 std::string plainTextSum;
461 auto records = udData->GetUnifiedData()->GetRecords();
462 for (const auto& record : records) {
463 std::string plainText;
464 auto value = record->GetEntry(utdId);
465 if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
466 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
467 object->GetValue(UDMF::CONTENT, plainText);
468 } else if (std::holds_alternative<std::string>(value)) {
469 plainText = std::get<std::string>(value);
470 }
471 plainTextSum += plainText;
472 }
473
474 if (plainTextSum.empty()) {
475 std::vector<std::string> plains = GetPlainTextRecords(unifiedData);
476 if (!plains.empty()) {
477 plainTextSum = plains[0];
478 }
479 }
480 return plainTextSum;
481 }
482
GetHtmlEntry(const RefPtr<UnifiedData> & unifiedData,std::string & htmlContent,std::string & plainContent)483 void UdmfClientImpl::GetHtmlEntry(
484 const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
485 {
486 CHECK_NULL_VOID(htmlContent.empty() && plainContent.empty());
487 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
488 CHECK_NULL_VOID(udData);
489 CHECK_NULL_VOID(udData->GetUnifiedData());
490 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HTML);
491 CHECK_NULL_VOID(udData->GetUnifiedData()->HasType(utdId));
492 auto records = udData->GetUnifiedData()->GetRecords();
493 for (auto record : records) {
494 std::string htmlText;
495 std::string plainText;
496 auto value = record->GetEntry(utdId);
497 if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
498 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
499 object->GetValue(UDMF::HTML_CONTENT, htmlText);
500 object->GetValue(UDMF::PLAIN_CONTENT, plainText);
501 }
502 htmlContent += htmlText;
503 plainContent += plainText;
504 }
505
506 if (htmlContent.empty() && plainContent.empty()) {
507 GetHtmlRecord(unifiedData, htmlContent, plainContent);
508 }
509 }
510
GetLinkEntry(const RefPtr<UnifiedData> & unifiedData,std::string & url,std::string & description)511 void UdmfClientImpl::GetLinkEntry(const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
512 {
513 CHECK_NULL_VOID(url.empty() && description.empty());
514 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
515 CHECK_NULL_VOID(udData);
516 CHECK_NULL_VOID(udData->GetUnifiedData());
517 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HYPERLINK);
518 CHECK_NULL_VOID(udData->GetUnifiedData()->HasType(utdId));
519 auto records = udData->GetUnifiedData()->GetRecords();
520 for (auto record : records) {
521 std::string currentUrl;
522 std::string currentDes;
523 auto value = record->GetEntry(utdId);
524 if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
525 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
526 object->GetValue(UDMF::URL, currentUrl);
527 object->GetValue(UDMF::DESCRIPTION, currentDes);
528 }
529 url += currentUrl;
530 description += currentDes;
531 }
532
533 if (url.empty() && description.empty()) {
534 GetLinkRecord(unifiedData, url, description);
535 }
536 }
537
GetFileUriEntry(const RefPtr<UnifiedData> & unifiedData,std::vector<std::string> & uri)538 bool UdmfClientImpl::GetFileUriEntry(const RefPtr<UnifiedData>& unifiedData, std::vector<std::string>& uri)
539 {
540 CHECK_NULL_RETURN(uri.empty(), false);
541 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
542 CHECK_NULL_RETURN(udData, false);
543 CHECK_NULL_RETURN(udData->GetUnifiedData(), false);
544 auto records = udData->GetUnifiedData()->GetRecords();
545 auto typeList = { UDMF::UDType::IMAGE, UDMF::UDType::AUDIO, UDMF::UDType::VIDEO, UDMF::UDType::FILE,
546 UDMF::UDType::FILE_URI };
547 for (auto record : records) {
548 std::string currentUri;
549 for (auto type : typeList) {
550 auto value = record->GetEntry(UDMF::UtdUtils::GetUtdIdFromUtdEnum(type));
551 if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
552 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
553 std::string currentEntryUri;
554 object->GetValue(UDMF::ORI_URI, currentEntryUri);
555 uri.emplace_back(currentEntryUri);
556 }
557 }
558 }
559
560 if (uri.empty()) {
561 GetFileUriRecord(unifiedData, uri);
562 }
563 return true;
564 }
565
GetSpanStringEntry(const RefPtr<UnifiedData> & unifiedData)566 std::vector<uint8_t> UdmfClientImpl::GetSpanStringEntry(const RefPtr<UnifiedData>& unifiedData)
567 {
568 std::vector<uint8_t> arr;
569 auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
570 CHECK_NULL_RETURN(udData, arr);
571 CHECK_NULL_RETURN(udData->GetUnifiedData(), arr);
572 auto utdId = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD);
573 CHECK_NULL_RETURN(udData->GetUnifiedData()->HasType(utdId), GetSpanStringRecord(unifiedData));
574 auto records = udData->GetUnifiedData()->GetRecords();
575 for (auto record : records) {
576 auto value = record->GetEntry(utdId);
577 if (std::holds_alternative<std::shared_ptr<UDMF::Object>>(value)) {
578 auto object = std::get<std::shared_ptr<UDMF::Object>>(value);
579 UDMF::ApplicationDefinedRecord* app = reinterpret_cast<UDMF::ApplicationDefinedRecord*>(object.get());
580 if (app && app->GetApplicationDefinedType() == "OPENHARMONY_STYLED_STRING_UDMF") {
581 arr = app->GetRawData();
582 return arr;
583 }
584 }
585 }
586 return GetSpanStringRecord(unifiedData);
587 }
588 } // namespace OHOS::Ace
589