1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci* Copyright (C) 2024 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci* you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci* You may obtain a copy of the License at
6bc03f14fSopenharmony_ci*
7bc03f14fSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci*
9bc03f14fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci* See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci* limitations under the License.
14bc03f14fSopenharmony_ci*/
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include "common/constant.h"
17bc03f14fSopenharmony_ci#include "paste_data_entry.h"
18bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
19bc03f14fSopenharmony_ci#include "pixel_map.h"
20bc03f14fSopenharmony_ci#include "tlv_object.h"
21bc03f14fSopenharmony_cinamespace OHOS {
22bc03f14fSopenharmony_cinamespace MiscServices {
23bc03f14fSopenharmony_ci
24bc03f14fSopenharmony_cienum TAG_CUSTOMDATA : uint16_t {
25bc03f14fSopenharmony_ci    TAG_ITEM_DATA = TAG_BUFF + 1,
26bc03f14fSopenharmony_ci};
27bc03f14fSopenharmony_ci
28bc03f14fSopenharmony_cienum TAG_ENTRY : uint16_t {
29bc03f14fSopenharmony_ci    TAG_ENTRY_UTDID = TAG_BUFF + 1,
30bc03f14fSopenharmony_ci    TAG_ENTRY_MIMETYPE,
31bc03f14fSopenharmony_ci    TAG_ENTRY_VALUE,
32bc03f14fSopenharmony_ci};
33bc03f14fSopenharmony_ci
34bc03f14fSopenharmony_cistd::map<std::string, std::vector<uint8_t>> MineCustomData::GetItemData()
35bc03f14fSopenharmony_ci{
36bc03f14fSopenharmony_ci    return this->itemData_;
37bc03f14fSopenharmony_ci}
38bc03f14fSopenharmony_ci
39bc03f14fSopenharmony_civoid MineCustomData::AddItemData(const std::string &mimeType, const std::vector<uint8_t> &arrayBuffer)
40bc03f14fSopenharmony_ci{
41bc03f14fSopenharmony_ci    itemData_.emplace(mimeType, arrayBuffer);
42bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "itemData_.size = %{public}zu", itemData_.size());
43bc03f14fSopenharmony_ci}
44bc03f14fSopenharmony_ci
45bc03f14fSopenharmony_cibool MineCustomData::Encode(std::vector<std::uint8_t> &buffer)
46bc03f14fSopenharmony_ci{
47bc03f14fSopenharmony_ci    return Write(buffer, TAG_ITEM_DATA, itemData_);
48bc03f14fSopenharmony_ci}
49bc03f14fSopenharmony_ci
50bc03f14fSopenharmony_cibool MineCustomData::Decode(const std::vector<std::uint8_t> &buffer)
51bc03f14fSopenharmony_ci{
52bc03f14fSopenharmony_ci    for (; IsEnough();) {
53bc03f14fSopenharmony_ci        TLVHead head{};
54bc03f14fSopenharmony_ci        bool ret = ReadHead(buffer, head);
55bc03f14fSopenharmony_ci        switch (head.tag) {
56bc03f14fSopenharmony_ci            case TAG_ITEM_DATA:
57bc03f14fSopenharmony_ci                ret = ret && ReadValue(buffer, itemData_, head);
58bc03f14fSopenharmony_ci                break;
59bc03f14fSopenharmony_ci            default:
60bc03f14fSopenharmony_ci                ret = ret && Skip(head.len, buffer.size());
61bc03f14fSopenharmony_ci                break;
62bc03f14fSopenharmony_ci        }
63bc03f14fSopenharmony_ci        if (!ret) {
64bc03f14fSopenharmony_ci            return false;
65bc03f14fSopenharmony_ci        }
66bc03f14fSopenharmony_ci    }
67bc03f14fSopenharmony_ci    return true;
68bc03f14fSopenharmony_ci}
69bc03f14fSopenharmony_ci
70bc03f14fSopenharmony_cisize_t MineCustomData::Count()
71bc03f14fSopenharmony_ci{
72bc03f14fSopenharmony_ci    return TLVObject::Count(itemData_);
73bc03f14fSopenharmony_ci}
74bc03f14fSopenharmony_ci
75bc03f14fSopenharmony_ciPasteDataEntry::PasteDataEntry(const PasteDataEntry &entry)
76bc03f14fSopenharmony_ci    : utdId_(entry.utdId_), mimeType_(entry.mimeType_), value_(entry.value_)
77bc03f14fSopenharmony_ci{
78bc03f14fSopenharmony_ci}
79bc03f14fSopenharmony_ci
80bc03f14fSopenharmony_ciPasteDataEntry &PasteDataEntry::operator=(const PasteDataEntry &entry)
81bc03f14fSopenharmony_ci{
82bc03f14fSopenharmony_ci    if (this == &entry) {
83bc03f14fSopenharmony_ci        return *this;
84bc03f14fSopenharmony_ci    }
85bc03f14fSopenharmony_ci    this->utdId_ = entry.GetUtdId();
86bc03f14fSopenharmony_ci    this->mimeType_ = entry.GetMimeType();
87bc03f14fSopenharmony_ci    this->value_ = entry.GetValue();
88bc03f14fSopenharmony_ci    return *this;
89bc03f14fSopenharmony_ci}
90bc03f14fSopenharmony_ci
91bc03f14fSopenharmony_ciPasteDataEntry::PasteDataEntry(const std::string &utdId, const EntryValue &value) : utdId_(utdId), value_(value)
92bc03f14fSopenharmony_ci{
93bc03f14fSopenharmony_ci    mimeType_ = CommonUtils::Convert2MimeType(utdId_);
94bc03f14fSopenharmony_ci}
95bc03f14fSopenharmony_ci
96bc03f14fSopenharmony_ciPasteDataEntry::PasteDataEntry(const std::string &utdId, const std::string &mimeType, const EntryValue &value)
97bc03f14fSopenharmony_ci    : utdId_(utdId), mimeType_(std::move(mimeType)), value_(std::move(value))
98bc03f14fSopenharmony_ci{
99bc03f14fSopenharmony_ci}
100bc03f14fSopenharmony_ci
101bc03f14fSopenharmony_civoid PasteDataEntry::SetUtdId(const std::string &utdId)
102bc03f14fSopenharmony_ci{
103bc03f14fSopenharmony_ci    utdId_ = utdId;
104bc03f14fSopenharmony_ci}
105bc03f14fSopenharmony_ci
106bc03f14fSopenharmony_cistd::string PasteDataEntry::GetUtdId() const
107bc03f14fSopenharmony_ci{
108bc03f14fSopenharmony_ci    return utdId_;
109bc03f14fSopenharmony_ci}
110bc03f14fSopenharmony_ci
111bc03f14fSopenharmony_civoid PasteDataEntry::SetMimeType(const std::string &mimeType)
112bc03f14fSopenharmony_ci{
113bc03f14fSopenharmony_ci    mimeType_ = mimeType;
114bc03f14fSopenharmony_ci}
115bc03f14fSopenharmony_ci
116bc03f14fSopenharmony_cistd::string PasteDataEntry::GetMimeType() const
117bc03f14fSopenharmony_ci{
118bc03f14fSopenharmony_ci    return mimeType_;
119bc03f14fSopenharmony_ci}
120bc03f14fSopenharmony_ci
121bc03f14fSopenharmony_ciEntryValue PasteDataEntry::GetValue() const
122bc03f14fSopenharmony_ci{
123bc03f14fSopenharmony_ci    return value_;
124bc03f14fSopenharmony_ci}
125bc03f14fSopenharmony_ci
126bc03f14fSopenharmony_civoid PasteDataEntry::SetValue(const EntryValue &value)
127bc03f14fSopenharmony_ci{
128bc03f14fSopenharmony_ci    value_ = value;
129bc03f14fSopenharmony_ci}
130bc03f14fSopenharmony_ci
131bc03f14fSopenharmony_cibool PasteDataEntry::Encode(std::vector<std::uint8_t> &buffer)
132bc03f14fSopenharmony_ci{
133bc03f14fSopenharmony_ci    bool ret = Write(buffer, TAG_ENTRY_UTDID, utdId_);
134bc03f14fSopenharmony_ci    ret = ret && Write(buffer, TAG_ENTRY_MIMETYPE, mimeType_);
135bc03f14fSopenharmony_ci    ret = ret && Write(buffer, TAG_ENTRY_VALUE, value_);
136bc03f14fSopenharmony_ci    return ret;
137bc03f14fSopenharmony_ci}
138bc03f14fSopenharmony_ci
139bc03f14fSopenharmony_cibool PasteDataEntry::Decode(const std::vector<std::uint8_t> &buffer)
140bc03f14fSopenharmony_ci{
141bc03f14fSopenharmony_ci    for (; IsEnough();) {
142bc03f14fSopenharmony_ci        TLVHead head{};
143bc03f14fSopenharmony_ci        bool ret = ReadHead(buffer, head);
144bc03f14fSopenharmony_ci        switch (head.tag) {
145bc03f14fSopenharmony_ci            case TAG_ENTRY_UTDID:
146bc03f14fSopenharmony_ci                ret = ret && ReadValue(buffer, utdId_, head);
147bc03f14fSopenharmony_ci                break;
148bc03f14fSopenharmony_ci            case TAG_ENTRY_MIMETYPE: {
149bc03f14fSopenharmony_ci                ret = ret && ReadValue(buffer, mimeType_, head);
150bc03f14fSopenharmony_ci                break;
151bc03f14fSopenharmony_ci            }
152bc03f14fSopenharmony_ci            case TAG_ENTRY_VALUE: {
153bc03f14fSopenharmony_ci                ret = ret && ReadValue(buffer, value_, head);
154bc03f14fSopenharmony_ci                break;
155bc03f14fSopenharmony_ci            }
156bc03f14fSopenharmony_ci            default:
157bc03f14fSopenharmony_ci                ret = ret && Skip(head.len, buffer.size());
158bc03f14fSopenharmony_ci                break;
159bc03f14fSopenharmony_ci        }
160bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "read value,tag:%{public}u, len:%{public}u, ret:%{public}d",
161bc03f14fSopenharmony_ci            head.tag, head.len, ret);
162bc03f14fSopenharmony_ci        if (!ret) {
163bc03f14fSopenharmony_ci            return false;
164bc03f14fSopenharmony_ci        }
165bc03f14fSopenharmony_ci    }
166bc03f14fSopenharmony_ci    return true;
167bc03f14fSopenharmony_ci}
168bc03f14fSopenharmony_ci
169bc03f14fSopenharmony_cibool PasteDataEntry::Marshalling(std::vector<std::uint8_t> &buffer)
170bc03f14fSopenharmony_ci{
171bc03f14fSopenharmony_ci    Init(buffer);
172bc03f14fSopenharmony_ci    return Encode(buffer);
173bc03f14fSopenharmony_ci}
174bc03f14fSopenharmony_ci
175bc03f14fSopenharmony_cibool PasteDataEntry::Unmarshalling(const std::vector<std::uint8_t> &buffer)
176bc03f14fSopenharmony_ci{
177bc03f14fSopenharmony_ci    total_ = buffer.size();
178bc03f14fSopenharmony_ci    return Decode(buffer);
179bc03f14fSopenharmony_ci}
180bc03f14fSopenharmony_ci
181bc03f14fSopenharmony_cisize_t PasteDataEntry::Count()
182bc03f14fSopenharmony_ci{
183bc03f14fSopenharmony_ci    size_t expectedSize = 0;
184bc03f14fSopenharmony_ci    expectedSize += TLVObject::Count(utdId_);
185bc03f14fSopenharmony_ci    expectedSize += TLVObject::Count(mimeType_);
186bc03f14fSopenharmony_ci    expectedSize += TLVObject::Count(value_);
187bc03f14fSopenharmony_ci    return expectedSize;
188bc03f14fSopenharmony_ci}
189bc03f14fSopenharmony_ci
190bc03f14fSopenharmony_cistd::shared_ptr<std::string> PasteDataEntry::ConvertToPlianText() const
191bc03f14fSopenharmony_ci{
192bc03f14fSopenharmony_ci    std::string res;
193bc03f14fSopenharmony_ci    auto utdId = GetUtdId();
194bc03f14fSopenharmony_ci    auto entry = GetValue();
195bc03f14fSopenharmony_ci    if (std::holds_alternative<std::string>(entry)) {
196bc03f14fSopenharmony_ci        res = std::get<std::string>(entry);
197bc03f14fSopenharmony_ci        return std::make_shared<std::string>(res);
198bc03f14fSopenharmony_ci    }
199bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(entry)) {
200bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no plaintext");
201bc03f14fSopenharmony_ci        return nullptr;
202bc03f14fSopenharmony_ci    }
203bc03f14fSopenharmony_ci    auto object = std::get<std::shared_ptr<Object>>(entry);
204bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::PLAIN_TEXT)) {
205bc03f14fSopenharmony_ci        object->GetValue(UDMF::CONTENT, res);
206bc03f14fSopenharmony_ci    } else {
207bc03f14fSopenharmony_ci        object->GetValue(UDMF::URL, res);
208bc03f14fSopenharmony_ci    }
209bc03f14fSopenharmony_ci    return std::make_shared<std::string>(res);
210bc03f14fSopenharmony_ci}
211bc03f14fSopenharmony_ci
212bc03f14fSopenharmony_cistd::shared_ptr<std::string> PasteDataEntry::ConvertToHtml() const
213bc03f14fSopenharmony_ci{
214bc03f14fSopenharmony_ci    std::string res;
215bc03f14fSopenharmony_ci    if (GetUtdId() != UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HTML)) {
216bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", GetUtdId().c_str());
217bc03f14fSopenharmony_ci        return nullptr;
218bc03f14fSopenharmony_ci    }
219bc03f14fSopenharmony_ci    auto entry = GetValue();
220bc03f14fSopenharmony_ci    if (std::holds_alternative<std::string>(entry)) {
221bc03f14fSopenharmony_ci        res = std::get<std::string>(entry);
222bc03f14fSopenharmony_ci        return std::make_shared<std::string>(res);
223bc03f14fSopenharmony_ci    }
224bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(entry)) {
225bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no html");
226bc03f14fSopenharmony_ci        return nullptr;
227bc03f14fSopenharmony_ci    }
228bc03f14fSopenharmony_ci    auto object = std::get<std::shared_ptr<Object>>(entry);
229bc03f14fSopenharmony_ci    object->GetValue(UDMF::HTML_CONTENT, res);
230bc03f14fSopenharmony_ci    return std::make_shared<std::string>(res);
231bc03f14fSopenharmony_ci}
232bc03f14fSopenharmony_ci
233bc03f14fSopenharmony_cistd::shared_ptr<Uri> PasteDataEntry::ConvertToUri() const
234bc03f14fSopenharmony_ci{
235bc03f14fSopenharmony_ci    std::string res;
236bc03f14fSopenharmony_ci    if (GetUtdId() != UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::FILE_URI)) {
237bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", GetUtdId().c_str());
238bc03f14fSopenharmony_ci        return nullptr;
239bc03f14fSopenharmony_ci    }
240bc03f14fSopenharmony_ci    auto entry = GetValue();
241bc03f14fSopenharmony_ci    if (std::holds_alternative<std::string>(entry)) {
242bc03f14fSopenharmony_ci        res = std::get<std::string>(entry);
243bc03f14fSopenharmony_ci        return std::make_shared<Uri>(Uri(res));
244bc03f14fSopenharmony_ci    }
245bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(entry)) {
246bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no uri");
247bc03f14fSopenharmony_ci        return nullptr;
248bc03f14fSopenharmony_ci    }
249bc03f14fSopenharmony_ci    auto object = std::get<std::shared_ptr<Object>>(entry);
250bc03f14fSopenharmony_ci    object->GetValue(UDMF::FILE_URI_PARAM, res);
251bc03f14fSopenharmony_ci    return std::make_shared<Uri>(Uri(res));
252bc03f14fSopenharmony_ci}
253bc03f14fSopenharmony_ci
254bc03f14fSopenharmony_cistd::shared_ptr<AAFwk::Want> PasteDataEntry::ConvertToWant() const
255bc03f14fSopenharmony_ci{
256bc03f14fSopenharmony_ci    if (GetUtdId() != UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::OPENHARMONY_WANT)) {
257bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", GetUtdId().c_str());
258bc03f14fSopenharmony_ci        return nullptr;
259bc03f14fSopenharmony_ci    }
260bc03f14fSopenharmony_ci    auto entry = GetValue();
261bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<AAFwk::Want>>(entry)) {
262bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no plaintext");
263bc03f14fSopenharmony_ci        return nullptr;
264bc03f14fSopenharmony_ci    }
265bc03f14fSopenharmony_ci    // no uds want
266bc03f14fSopenharmony_ci    return std::get<std::shared_ptr<AAFwk::Want>>(entry);
267bc03f14fSopenharmony_ci}
268bc03f14fSopenharmony_ci
269bc03f14fSopenharmony_cistd::shared_ptr<Media::PixelMap> PasteDataEntry::ConvertToPixelMap() const
270bc03f14fSopenharmony_ci{
271bc03f14fSopenharmony_ci    auto utdId = GetUtdId();
272bc03f14fSopenharmony_ci    if (utdId != UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::SYSTEM_DEFINED_PIXEL_MAP)) {
273bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", utdId.c_str());
274bc03f14fSopenharmony_ci        return nullptr;
275bc03f14fSopenharmony_ci    }
276bc03f14fSopenharmony_ci    auto entry = GetValue();
277bc03f14fSopenharmony_ci    if (std::holds_alternative<std::shared_ptr<Media::PixelMap>>(entry)) {
278bc03f14fSopenharmony_ci        return std::get<std::shared_ptr<Media::PixelMap>>(entry);
279bc03f14fSopenharmony_ci    }
280bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(entry)) {
281bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no pixelmap");
282bc03f14fSopenharmony_ci        return nullptr;
283bc03f14fSopenharmony_ci    }
284bc03f14fSopenharmony_ci    auto object = std::get<std::shared_ptr<Object>>(entry);
285bc03f14fSopenharmony_ci    std::string objecType;
286bc03f14fSopenharmony_ci    if (!object->GetValue(UDMF::UNIFORM_DATA_TYPE, objecType)) {
287bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", utdId.c_str());
288bc03f14fSopenharmony_ci        return nullptr;
289bc03f14fSopenharmony_ci    }
290bc03f14fSopenharmony_ci    if (objecType != UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_PIXEL_MAP)) {
291bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, objecType:%{public}s", objecType.c_str());
292bc03f14fSopenharmony_ci        return nullptr;
293bc03f14fSopenharmony_ci    }
294bc03f14fSopenharmony_ci    auto val = object->value_[UDMF::PIXEL_MAP];
295bc03f14fSopenharmony_ci    if (std::holds_alternative<std::shared_ptr<Media::PixelMap>>(val)) {
296bc03f14fSopenharmony_ci        return std::get<std::shared_ptr<Media::PixelMap>>(val);
297bc03f14fSopenharmony_ci    }
298bc03f14fSopenharmony_ci    PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error, no pixelmap");
299bc03f14fSopenharmony_ci    return nullptr;
300bc03f14fSopenharmony_ci}
301bc03f14fSopenharmony_ci
302bc03f14fSopenharmony_cistd::shared_ptr<MineCustomData> PasteDataEntry::ConvertToCustomData() const
303bc03f14fSopenharmony_ci{
304bc03f14fSopenharmony_ci    auto entry = GetValue();
305bc03f14fSopenharmony_ci    MineCustomData customdata;
306bc03f14fSopenharmony_ci    if (std::holds_alternative<std::vector<uint8_t>>(entry)) {
307bc03f14fSopenharmony_ci        customdata.AddItemData(GetMimeType(), std::get<std::vector<uint8_t>>(entry));
308bc03f14fSopenharmony_ci        return std::make_shared<MineCustomData>(customdata);
309bc03f14fSopenharmony_ci    }
310bc03f14fSopenharmony_ci    if (!std::holds_alternative<std::shared_ptr<Object>>(entry)) {
311bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "value error,  utdId:%{public}s", utdId_.c_str());
312bc03f14fSopenharmony_ci        return nullptr;
313bc03f14fSopenharmony_ci    }
314bc03f14fSopenharmony_ci    auto object = std::get<std::shared_ptr<Object>>(entry);
315bc03f14fSopenharmony_ci    std::string objecType;
316bc03f14fSopenharmony_ci    if (!object->GetValue(UDMF::UNIFORM_DATA_TYPE, objecType)) {
317bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type error, utdId:%{public}s", utdId_.c_str());
318bc03f14fSopenharmony_ci        return nullptr;
319bc03f14fSopenharmony_ci    }
320bc03f14fSopenharmony_ci    if (objecType != GetUtdId()) {
321bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "type diff error, utdId:%{public}s, objecType:%{public}s",
322bc03f14fSopenharmony_ci            utdId_.c_str(), objecType.c_str());
323bc03f14fSopenharmony_ci    }
324bc03f14fSopenharmony_ci    std::vector<uint8_t> recordValue;
325bc03f14fSopenharmony_ci    if (!object->GetValue(UDMF::ARRAY_BUFFER, recordValue)) {
326bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "get value error, utdId:%{public}s", utdId_.c_str());
327bc03f14fSopenharmony_ci        return nullptr;
328bc03f14fSopenharmony_ci    }
329bc03f14fSopenharmony_ci    customdata.AddItemData(utdId_, recordValue);
330bc03f14fSopenharmony_ci    return std::make_shared<MineCustomData>(customdata);
331bc03f14fSopenharmony_ci}
332bc03f14fSopenharmony_ci
333bc03f14fSopenharmony_cibool PasteDataEntry::HasContent(const std::string &utdId) const
334bc03f14fSopenharmony_ci{
335bc03f14fSopenharmony_ci    auto mimeType = CommonUtils::Convert2MimeType(utdId);
336bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_PIXELMAP) {
337bc03f14fSopenharmony_ci        return ConvertToPixelMap() != nullptr;
338bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_HTML) {
339bc03f14fSopenharmony_ci        auto html = ConvertToHtml();
340bc03f14fSopenharmony_ci        return html != nullptr && !html->empty();
341bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_PLAIN) {
342bc03f14fSopenharmony_ci        auto plianText = ConvertToPlianText();
343bc03f14fSopenharmony_ci        return plianText != nullptr && !plianText->empty();
344bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_URI) {
345bc03f14fSopenharmony_ci        auto uri = ConvertToUri();
346bc03f14fSopenharmony_ci        return uri != nullptr && !uri->ToString().empty();
347bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_WANT) {
348bc03f14fSopenharmony_ci        return ConvertToWant() != nullptr;
349bc03f14fSopenharmony_ci    } else {
350bc03f14fSopenharmony_ci        return ConvertToCustomData() != nullptr;
351bc03f14fSopenharmony_ci    }
352bc03f14fSopenharmony_ci}
353bc03f14fSopenharmony_ci
354bc03f14fSopenharmony_cistd::string CommonUtils::Convert(UDType uDType)
355bc03f14fSopenharmony_ci{
356bc03f14fSopenharmony_ci    switch (uDType) {
357bc03f14fSopenharmony_ci        case UDType::PLAIN_TEXT:
358bc03f14fSopenharmony_ci        case UDType::HYPERLINK:
359bc03f14fSopenharmony_ci            return MIMETYPE_TEXT_PLAIN;
360bc03f14fSopenharmony_ci        case UDType::HTML:
361bc03f14fSopenharmony_ci            return MIMETYPE_TEXT_HTML;
362bc03f14fSopenharmony_ci        case UDType::FILE:
363bc03f14fSopenharmony_ci        case UDType::IMAGE:
364bc03f14fSopenharmony_ci        case UDType::VIDEO:
365bc03f14fSopenharmony_ci        case UDType::AUDIO:
366bc03f14fSopenharmony_ci        case UDType::FOLDER:
367bc03f14fSopenharmony_ci        case UDType::FILE_URI:
368bc03f14fSopenharmony_ci            return MIMETYPE_TEXT_URI;
369bc03f14fSopenharmony_ci        case UDType::SYSTEM_DEFINED_PIXEL_MAP:
370bc03f14fSopenharmony_ci            return MIMETYPE_PIXELMAP;
371bc03f14fSopenharmony_ci        case UDType::OPENHARMONY_WANT:
372bc03f14fSopenharmony_ci            return MIMETYPE_TEXT_WANT;
373bc03f14fSopenharmony_ci        default:
374bc03f14fSopenharmony_ci            return UDMF::UtdUtils::GetUtdIdFromUtdEnum(uDType);
375bc03f14fSopenharmony_ci    }
376bc03f14fSopenharmony_ci}
377bc03f14fSopenharmony_ci
378bc03f14fSopenharmony_cistd::string CommonUtils::Convert2MimeType(const std::string &utdId)
379bc03f14fSopenharmony_ci{
380bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::PLAIN_TEXT) ||
381bc03f14fSopenharmony_ci        utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HYPERLINK)) {
382bc03f14fSopenharmony_ci        return MIMETYPE_TEXT_PLAIN;
383bc03f14fSopenharmony_ci    }
384bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HTML)) {
385bc03f14fSopenharmony_ci        return MIMETYPE_TEXT_HTML;
386bc03f14fSopenharmony_ci    }
387bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::FILE_URI)) {
388bc03f14fSopenharmony_ci        return MIMETYPE_TEXT_URI;
389bc03f14fSopenharmony_ci    }
390bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::SYSTEM_DEFINED_PIXEL_MAP)) {
391bc03f14fSopenharmony_ci        return MIMETYPE_PIXELMAP;
392bc03f14fSopenharmony_ci    }
393bc03f14fSopenharmony_ci    if (utdId == UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::OPENHARMONY_WANT)) {
394bc03f14fSopenharmony_ci        return MIMETYPE_TEXT_WANT;
395bc03f14fSopenharmony_ci    }
396bc03f14fSopenharmony_ci    return utdId;
397bc03f14fSopenharmony_ci}
398bc03f14fSopenharmony_ci
399bc03f14fSopenharmony_ci// other is appdefined-types
400bc03f14fSopenharmony_cistd::string CommonUtils::Convert2UtdId(int32_t uDType, const std::string &mimeType)
401bc03f14fSopenharmony_ci{
402bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_PLAIN && uDType == UDMF::HYPERLINK) {
403bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HYPERLINK);
404bc03f14fSopenharmony_ci    }
405bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_PLAIN) {
406bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::PLAIN_TEXT);
407bc03f14fSopenharmony_ci    }
408bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_URI) {
409bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::FILE_URI);
410bc03f14fSopenharmony_ci    }
411bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_HTML) {
412bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::HTML);
413bc03f14fSopenharmony_ci    }
414bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_WANT) {
415bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::OPENHARMONY_WANT);
416bc03f14fSopenharmony_ci    }
417bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_PIXELMAP) {
418bc03f14fSopenharmony_ci        return UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDType::SYSTEM_DEFINED_PIXEL_MAP);
419bc03f14fSopenharmony_ci    }
420bc03f14fSopenharmony_ci    return mimeType;
421bc03f14fSopenharmony_ci}
422bc03f14fSopenharmony_ci
423bc03f14fSopenharmony_ciUDMF::UDType CommonUtils::Convert(int32_t uDType, const std::string &mimeType)
424bc03f14fSopenharmony_ci{
425bc03f14fSopenharmony_ci    if (uDType != UDMF::UD_BUTT) {
426bc03f14fSopenharmony_ci        return static_cast<UDType>(uDType);
427bc03f14fSopenharmony_ci    }
428bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_URI) {
429bc03f14fSopenharmony_ci        return UDMF::FILE_URI;
430bc03f14fSopenharmony_ci    }
431bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_PLAIN) {
432bc03f14fSopenharmony_ci        return UDMF::PLAIN_TEXT;
433bc03f14fSopenharmony_ci    }
434bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_HTML) {
435bc03f14fSopenharmony_ci        return UDMF::HTML;
436bc03f14fSopenharmony_ci    }
437bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_WANT) {
438bc03f14fSopenharmony_ci        return UDMF::OPENHARMONY_WANT;
439bc03f14fSopenharmony_ci    }
440bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_PIXELMAP) {
441bc03f14fSopenharmony_ci        return UDMF::SYSTEM_DEFINED_PIXEL_MAP;
442bc03f14fSopenharmony_ci    }
443bc03f14fSopenharmony_ci    auto type = UDMF::UtdUtils::GetUtdEnumFromUtdId(mimeType);
444bc03f14fSopenharmony_ci    if (type != UDMF::UD_BUTT) {
445bc03f14fSopenharmony_ci        return static_cast<UDType>(type);
446bc03f14fSopenharmony_ci    }
447bc03f14fSopenharmony_ci    return UDMF::APPLICATION_DEFINED_RECORD;
448bc03f14fSopenharmony_ci}
449bc03f14fSopenharmony_ci} // namespace MiscServices
450bc03f14fSopenharmony_ci} // namespace OHOS