• 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 <memory>
19 #include <unordered_map>
20 
21 #include "html.h"
22 #include "image.h"
23 #include "link.h"
24 #include "summary_napi.h"
25 #include "system_defined_form.h"
26 #include "system_defined_pixelmap.h"
27 #include "text.h"
28 #include "plain_text.h"
29 #include "udmf_client.h"
30 #include "unified_data.h"
31 #include "unified_data_napi.h"
32 #include "unified_types.h"
33 #include "video.h"
34 #include "native_engine/native_engine.h"
35 #include "frameworks/bridge/common/utils/engine_helper.h"
36 #include "frameworks/bridge/common/utils/utils.h"
37 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
38 #include "js_native_api_types.h"
39 
40 #include "base/utils/utils.h"
41 #include "core/common/udmf/unified_data.h"
42 namespace OHOS::Ace {
GetInstance()43 UdmfClient* UdmfClient::GetInstance()
44 {
45     static UdmfClientImpl instance;
46     return &instance;
47 }
48 
CreateUnifiedData()49 RefPtr<UnifiedData> UdmfClientImpl::CreateUnifiedData()
50 {
51     return AceType::DynamicCast<UnifiedData>(AceType::MakeRefPtr<UnifiedDataImpl>());
52 }
53 
TransformUnifiedData(NativeValue * nativeValue)54 RefPtr<UnifiedData> UdmfClientImpl::TransformUnifiedData(NativeValue* nativeValue)
55 {
56     auto engine = EngineHelper::GetCurrentEngine();
57     CHECK_NULL_RETURN(engine, nullptr);
58     NativeEngine* nativeEngine = engine->GetNativeEngine();
59     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
60     void* native = nullptr;
61     napi_unwrap(env, reinterpret_cast<napi_value>(nativeValue), &native);
62     auto* unifiedData = reinterpret_cast<UDMF::UnifiedDataNapi*>(native);
63     CHECK_NULL_RETURN(unifiedData, nullptr);
64     CHECK_NULL_RETURN(unifiedData->value_, nullptr);
65     auto udData = AceType::MakeRefPtr<UnifiedDataImpl>();
66     udData->SetUnifiedData(unifiedData->value_);
67     return udData;
68 }
69 
TransformUdmfUnifiedData(RefPtr<UnifiedData> & UnifiedData)70 NativeValue* UdmfClientImpl::TransformUdmfUnifiedData(RefPtr<UnifiedData>& UnifiedData)
71 {
72     auto engine = EngineHelper::GetCurrentEngine();
73     CHECK_NULL_RETURN(engine, nullptr);
74     NativeEngine* nativeEngine = engine->GetNativeEngine();
75     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
76     auto unifiedData = AceType::DynamicCast<UnifiedDataImpl>(UnifiedData)->GetUnifiedData();
77     CHECK_NULL_RETURN(unifiedData, nullptr);
78     napi_value dataVal = nullptr;
79     UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, dataVal);
80     CHECK_NULL_RETURN(dataVal, nullptr);
81     return reinterpret_cast<NativeValue*>(dataVal);
82 }
83 
TransformSummary(std::map<std::string,int64_t> & summary)84 NativeValue* UdmfClientImpl::TransformSummary(std::map<std::string, int64_t>& summary)
85 {
86     auto engine = EngineHelper::GetCurrentEngine();
87     CHECK_NULL_RETURN(engine, nullptr);
88     NativeEngine* nativeEngine = engine->GetNativeEngine();
89     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
90     std::shared_ptr<UDMF::Summary> udmfSummary = std::make_shared<UDMF::Summary>();
91     CHECK_NULL_RETURN(udmfSummary, nullptr);
92     udmfSummary->totalSize = 0;
93     for (auto element : summary) {
94         udmfSummary->totalSize += element.second;
95     }
96     udmfSummary->summary = std::move(summary);
97     napi_value dataVal = nullptr;
98     UDMF::SummaryNapi::NewInstance(env, udmfSummary, dataVal);
99     CHECK_NULL_RETURN(dataVal, nullptr);
100     return reinterpret_cast<NativeValue*>(dataVal);
101 }
102 
SetData(const RefPtr<UnifiedData> & unifiedData,std::string & key)103 int32_t UdmfClientImpl::SetData(const RefPtr<UnifiedData>& unifiedData, std::string& key)
104 {
105     auto client = UDMF::UdmfClient::GetInstance();
106     UDMF::CustomOption udCustomOption;
107     udCustomOption.intention = UDMF::Intention::UD_INTENTION_DRAG;
108     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
109     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
110     int32_t ret = client.SetData(udCustomOption, *udData->GetUnifiedData(), key);
111     return ret;
112 }
113 
GetData(const RefPtr<UnifiedData> & unifiedData,const std::string & key)114 int32_t UdmfClientImpl::GetData(const RefPtr<UnifiedData>& unifiedData, const std::string& key)
115 {
116     auto client = UDMF::UdmfClient::GetInstance();
117     UDMF::QueryOption queryOption;
118     queryOption.key = key;
119     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
120     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
121     int ret = client.GetData(queryOption, *udData->GetUnifiedData());
122     return ret;
123 }
124 
GetSummary(std::string & key,std::map<std::string,int64_t> & summaryMap)125 int32_t UdmfClientImpl::GetSummary(std::string& key, std::map<std::string, int64_t>& summaryMap)
126 {
127     auto client = UDMF::UdmfClient::GetInstance();
128     UDMF::Summary summary;
129     UDMF::QueryOption queryOption;
130     queryOption.key = key;
131     int32_t ret = client.GetSummary(queryOption, summary);
132     summaryMap = summary.summary;
133     return ret;
134 }
135 
GetSize()136 int64_t UnifiedDataImpl::GetSize()
137 {
138     CHECK_NULL_RETURN(unifiedData_, 0);
139     return unifiedData_->GetRecords().size();
140 }
141 
GetUnifiedData()142 std::shared_ptr<UDMF::UnifiedData> UnifiedDataImpl::GetUnifiedData()
143 {
144     if (unifiedData_ == nullptr) {
145         unifiedData_ = std::make_shared<UDMF::UnifiedData>();
146     }
147     return unifiedData_;
148 }
149 
SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)150 void UnifiedDataImpl::SetUnifiedData(std::shared_ptr<UDMF::UnifiedData> unifiedData)
151 {
152     unifiedData_ = unifiedData;
153 }
154 
AddFormRecord(const RefPtr<UnifiedData> & unifiedData,int32_t formId,const RequestFormInfo & cardInfo)155 void UdmfClientImpl::AddFormRecord(
156     const RefPtr<UnifiedData>& unifiedData, int32_t formId, const RequestFormInfo& cardInfo)
157 {
158     auto formRecord = std::make_shared<UDMF::SystemDefinedForm>();
159     formRecord->SetFormId(formId);
160     formRecord->SetFormName(cardInfo.cardName);
161     formRecord->SetBundleName(cardInfo.bundleName);
162     formRecord->SetAbilityName(cardInfo.abilityName);
163     formRecord->SetModule(cardInfo.moduleName);
164     formRecord->SetType(UDMF::UDType::SYSTEM_DEFINED_FORM);
165 
166     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
167     CHECK_NULL_VOID(udData);
168     CHECK_NULL_VOID(udData->GetUnifiedData());
169     udData->GetUnifiedData()->AddRecord(formRecord);
170 }
171 
AddLinkRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & url,const std::string & description)172 void UdmfClientImpl::AddLinkRecord(
173     const RefPtr<UnifiedData>& unifiedData, const std::string& url, const std::string& description)
174 {
175     auto record = std::make_shared<UDMF::Link>(url, description);
176 
177     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
178     CHECK_NULL_VOID(udData);
179     CHECK_NULL_VOID(udData->GetUnifiedData());
180     udData->GetUnifiedData()->AddRecord(record);
181 }
182 
GetLinkRecord(const RefPtr<UnifiedData> & unifiedData,std::string & url,std::string & description)183 void UdmfClientImpl::GetLinkRecord(
184     const RefPtr<UnifiedData>& unifiedData, std::string& url, std::string& description)
185 {
186     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
187     CHECK_NULL_VOID(udData);
188     CHECK_NULL_VOID(udData->GetUnifiedData());
189     auto records = udData->GetUnifiedData()->GetRecords();
190     for (auto record : records) {
191         UDMF::UDType type = record->GetType();
192         if (type == UDMF::UDType::HYPERLINK) {
193             UDMF::Link* link = reinterpret_cast<UDMF::Link*>(record.get());
194             url = link->GetUrl();
195             description = link->GetDescription();
196             return;
197         }
198     }
199 }
200 
AddHtmlRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & htmlContent,const std::string & plainContent)201 void UdmfClientImpl::AddHtmlRecord(
202     const RefPtr<UnifiedData>& unifiedData, const std::string& htmlContent, const std::string& plainContent)
203 {
204     auto htmlRecord = std::make_shared<UDMF::Html>(htmlContent, plainContent);
205 
206     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
207     CHECK_NULL_VOID(udData);
208     CHECK_NULL_VOID(udData->GetUnifiedData());
209     if (!plainContent.empty() || !htmlContent.empty()) {
210         udData->GetUnifiedData()->AddRecord(htmlRecord);
211     }
212 }
213 
GetHtmlRecord(const RefPtr<UnifiedData> & unifiedData,std::string & htmlContent,std::string & plainContent)214 void UdmfClientImpl::GetHtmlRecord(
215     const RefPtr<UnifiedData>& unifiedData, std::string& htmlContent, std::string& plainContent)
216 {
217     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
218     CHECK_NULL_VOID(udData);
219     CHECK_NULL_VOID(udData->GetUnifiedData());
220     auto records = udData->GetUnifiedData()->GetRecords();
221     for (auto record : records) {
222         UDMF::UDType type = record->GetType();
223         if (type == UDMF::UDType::HTML) {
224             UDMF::Html* html = reinterpret_cast<UDMF::Html*>(record.get());
225             plainContent = html->GetPlainContent();
226             htmlContent = html->GetHtmlContent();
227             return;
228         }
229     }
230 }
231 
AddPixelMapRecord(const RefPtr<UnifiedData> & unifiedData,std::vector<uint8_t> & data)232 void UdmfClientImpl::AddPixelMapRecord(const RefPtr<UnifiedData>& unifiedData, std::vector<uint8_t>& data)
233 {
234     auto record = std::make_shared<UDMF::SystemDefinedPixelMap>(data);
235 
236     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
237     CHECK_NULL_VOID(udData);
238     CHECK_NULL_VOID(udData->GetUnifiedData());
239     udData->GetUnifiedData()->AddRecord(record);
240 }
241 
AddImageRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & uri)242 void UdmfClientImpl::AddImageRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& uri)
243 {
244     auto record = std::make_shared<UDMF::Image>(uri);
245 
246     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
247     CHECK_NULL_VOID(udData);
248     CHECK_NULL_VOID(udData->GetUnifiedData());
249     udData->GetUnifiedData()->AddRecord(record);
250 }
251 
AddPlainTextRecord(const RefPtr<UnifiedData> & unifiedData,const std::string & selectedStr)252 void UdmfClientImpl::AddPlainTextRecord(const RefPtr<UnifiedData>& unifiedData, const std::string& selectedStr)
253 {
254     auto record = std::make_shared<UDMF::PlainText>(selectedStr, "");
255 
256     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
257     CHECK_NULL_VOID(udData);
258     CHECK_NULL_VOID(udData->GetUnifiedData());
259     udData->GetUnifiedData()->AddRecord(record);
260 }
261 
GetSinglePlainTextRecord(const RefPtr<UnifiedData> & unifiedData)262 std::string UdmfClientImpl::GetSinglePlainTextRecord(const RefPtr<UnifiedData>& unifiedData)
263 {
264     std::string str = "";
265     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
266     CHECK_NULL_RETURN(udData, str);
267     CHECK_NULL_RETURN(udData->GetUnifiedData(), str);
268     auto records = udData->GetUnifiedData()->GetRecords();
269     if (records.size() >= 1 && records[0]->GetType() == UDMF::UDType::PLAIN_TEXT) {
270         UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(records[0].get());
271         str = plainText->GetContent();
272     }
273     return str;
274 }
275 
GetPlainTextRecords(const RefPtr<UnifiedData> & unifiedData)276 std::vector<std::string> UdmfClientImpl::GetPlainTextRecords(const RefPtr<UnifiedData>& unifiedData)
277 {
278     std::vector<std::string> textRecords;
279     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
280     CHECK_NULL_RETURN(udData, textRecords);
281     CHECK_NULL_RETURN(udData->GetUnifiedData(), textRecords);
282     auto records = udData->GetUnifiedData()->GetRecords();
283     for (const auto& record : records) {
284         UDMF::UDType type = record->GetType();
285         if (type == UDMF::UDType::PLAIN_TEXT) {
286             UDMF::PlainText* plainText = reinterpret_cast<UDMF::PlainText*>(record.get());
287             std::string str = plainText->GetContent();
288             textRecords.emplace_back(str);
289         }
290     }
291     return textRecords;
292 }
293 
GetVideoRecordUri(const RefPtr<UnifiedData> & unifiedData,std::string & uri)294 int32_t UdmfClientImpl::GetVideoRecordUri(const RefPtr<UnifiedData>& unifiedData, std::string& uri)
295 {
296     auto udData = AceType::DynamicCast<UnifiedDataImpl>(unifiedData);
297     CHECK_NULL_RETURN(udData, UDMF::E_ERROR);
298     CHECK_NULL_RETURN(udData->GetUnifiedData(), UDMF::E_ERROR);
299     auto records = udData->GetUnifiedData()->GetRecords();
300     if (records.size() == 0) {
301         return UDMF::E_ERROR;
302     }
303     auto video = static_cast<UDMF::Video*>(records[0].get());
304     uri = video->GetUri();
305     return 0;
306 }
307 
GetErrorInfo(int32_t errorCode)308 std::pair<int32_t, std::string> UdmfClientImpl::GetErrorInfo(int32_t errorCode)
309 {
310     switch (errorCode) {
311         case UDMF::E_NOT_FOUND:
312             return { Framework::ERROR_CODE_DRAG_DATA_NOT_FOUND, "GetData failed, data not found." };
313         default:
314             return { Framework::ERROR_CODE_DRAG_DATA_ERROR, "GetData failed, data error." };
315     }
316 }
317 } // namespace OHOS::Ace
318