123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/ohos/capability/clipboard/clipboard_impl.h"
1723b3eb3cSopenharmony_ci#include <vector>
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "adapter/ohos/osal/pixel_map_ohos.h"
2023b3eb3cSopenharmony_ci#include "adapter/ohos/capability/html/html_to_span.h"
2123b3eb3cSopenharmony_ci#include "base/log/log_wrapper.h"
2223b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2323b3eb3cSopenharmony_ci#include "core/components_ng/pattern/text/span/span_string.h"
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_cinamespace OHOS::Ace {
2623b3eb3cSopenharmony_ci#ifndef SYSTEM_CLIPBOARD_SUPPORTED
2723b3eb3cSopenharmony_cinamespace {
2823b3eb3cSopenharmony_cistd::string g_clipboard;
2923b3eb3cSopenharmony_ciRefPtr<PixelMap> g_pixmap;
3023b3eb3cSopenharmony_ci} // namespace
3123b3eb3cSopenharmony_ci#endif
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
3423b3eb3cSopenharmony_ciMiscServices::ShareOption TransitionCopyOption(CopyOptions copyOption)
3523b3eb3cSopenharmony_ci{
3623b3eb3cSopenharmony_ci    auto shareOption = MiscServices::ShareOption::InApp;
3723b3eb3cSopenharmony_ci    switch (copyOption) {
3823b3eb3cSopenharmony_ci        case CopyOptions::InApp:
3923b3eb3cSopenharmony_ci            shareOption = MiscServices::ShareOption::InApp;
4023b3eb3cSopenharmony_ci            break;
4123b3eb3cSopenharmony_ci        case CopyOptions::Local:
4223b3eb3cSopenharmony_ci            shareOption = MiscServices::ShareOption::LocalDevice;
4323b3eb3cSopenharmony_ci            break;
4423b3eb3cSopenharmony_ci        case CopyOptions::Distributed:
4523b3eb3cSopenharmony_ci            shareOption = MiscServices::ShareOption::CrossDevice;
4623b3eb3cSopenharmony_ci            break;
4723b3eb3cSopenharmony_ci        default:
4823b3eb3cSopenharmony_ci            break;
4923b3eb3cSopenharmony_ci    }
5023b3eb3cSopenharmony_ci    return shareOption;
5123b3eb3cSopenharmony_ci}
5223b3eb3cSopenharmony_ci
5323b3eb3cSopenharmony_ciconst std::string SPAN_STRING_TAG = "openharmony.styled-string";
5423b3eb3cSopenharmony_ci#endif
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_civoid ClipboardImpl::HasData(const std::function<void(bool hasData)>& callback)
5723b3eb3cSopenharmony_ci{
5823b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
5923b3eb3cSopenharmony_ci    bool hasData = false;
6023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
6123b3eb3cSopenharmony_ci    taskExecutor_->PostSyncTask(
6223b3eb3cSopenharmony_ci        [&hasData]() { hasData = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData(); },
6323b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardHasData");
6423b3eb3cSopenharmony_ci    callback(hasData);
6523b3eb3cSopenharmony_ci#endif
6623b3eb3cSopenharmony_ci}
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_civoid ClipboardImpl::HasDataType(
6923b3eb3cSopenharmony_ci    const std::function<void(bool hasData)>& callback, const std::vector<std::string>& mimeTypes)
7023b3eb3cSopenharmony_ci{
7123b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
7223b3eb3cSopenharmony_ci    bool hasData = false;
7323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
7423b3eb3cSopenharmony_ci    taskExecutor_->PostSyncTask(
7523b3eb3cSopenharmony_ci        [&hasData, mimeTypes]() {
7623b3eb3cSopenharmony_ci            for (auto mimeType = mimeTypes.begin(); mimeType != mimeTypes.end(); ++mimeType) {
7723b3eb3cSopenharmony_ci                hasData = OHOS::MiscServices::PasteboardClient::GetInstance()->HasDataType(*mimeType);
7823b3eb3cSopenharmony_ci                TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "Clipboard data mimeType %{public}s available ? %{public}d",
7923b3eb3cSopenharmony_ci                    mimeType->c_str(), hasData);
8023b3eb3cSopenharmony_ci                if (hasData) {
8123b3eb3cSopenharmony_ci                    break;
8223b3eb3cSopenharmony_ci                }
8323b3eb3cSopenharmony_ci            }
8423b3eb3cSopenharmony_ci        },
8523b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardHasDataType");
8623b3eb3cSopenharmony_ci    callback(hasData);
8723b3eb3cSopenharmony_ci#endif
8823b3eb3cSopenharmony_ci}
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_civoid ClipboardImpl::SetData(const std::string& data, CopyOptions copyOption, bool isDragData)
9123b3eb3cSopenharmony_ci{
9223b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
9323b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
9423b3eb3cSopenharmony_ci    auto shareOption = TransitionCopyOption(copyOption);
9523b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
9623b3eb3cSopenharmony_ci        [data, shareOption, isDragData]() {
9723b3eb3cSopenharmony_ci            auto pasteData = OHOS::MiscServices::PasteboardClient::GetInstance()->CreatePlainTextData(data);
9823b3eb3cSopenharmony_ci            CHECK_NULL_VOID(pasteData);
9923b3eb3cSopenharmony_ci            pasteData->SetShareOption(shareOption);
10023b3eb3cSopenharmony_ci            pasteData->SetDraggedDataFlag(isDragData);
10123b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteboardClient::GetInstance()->SetPasteData(*pasteData);
10223b3eb3cSopenharmony_ci        },
10323b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardSetDataWithCopyOption");
10423b3eb3cSopenharmony_ci#else
10523b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
10623b3eb3cSopenharmony_ci        [data]() { g_clipboard = data; }, TaskExecutor::TaskType::UI, "ArkUIClipboardSetTextPasteData");
10723b3eb3cSopenharmony_ci#endif
10823b3eb3cSopenharmony_ci}
10923b3eb3cSopenharmony_ci
11023b3eb3cSopenharmony_civoid ClipboardImpl::SetPixelMapData(const RefPtr<PixelMap>& pixmap, CopyOptions copyOption)
11123b3eb3cSopenharmony_ci{
11223b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
11323b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
11423b3eb3cSopenharmony_ci    auto shareOption = TransitionCopyOption(copyOption);
11523b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
11623b3eb3cSopenharmony_ci        [pixmap, shareOption]() {
11723b3eb3cSopenharmony_ci            CHECK_NULL_VOID(pixmap);
11823b3eb3cSopenharmony_ci            auto pixmapOhos = AceType::DynamicCast<PixelMapOhos>(pixmap);
11923b3eb3cSopenharmony_ci            CHECK_NULL_VOID(pixmapOhos);
12023b3eb3cSopenharmony_ci            auto pasteData = OHOS::MiscServices::PasteboardClient::GetInstance()->CreatePixelMapData(
12123b3eb3cSopenharmony_ci                pixmapOhos->GetPixelMapSharedPtr());
12223b3eb3cSopenharmony_ci            CHECK_NULL_VOID(pasteData);
12323b3eb3cSopenharmony_ci            pasteData->SetShareOption(shareOption);
12423b3eb3cSopenharmony_ci            TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "Set pixmap to system clipboard");
12523b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteboardClient::GetInstance()->SetPasteData(*pasteData);
12623b3eb3cSopenharmony_ci        },
12723b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardSetPixelMapWithCopyOption");
12823b3eb3cSopenharmony_ci#else
12923b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
13023b3eb3cSopenharmony_ci        [pixmap]() { g_pixmap = pixmap; }, TaskExecutor::TaskType::UI, "ArkUIClipboardSetImagePasteData");
13123b3eb3cSopenharmony_ci#endif
13223b3eb3cSopenharmony_ci}
13323b3eb3cSopenharmony_ci
13423b3eb3cSopenharmony_civoid ClipboardImpl::GetData(const std::function<void(const std::string&)>& callback, bool syncMode)
13523b3eb3cSopenharmony_ci{
13623b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
13723b3eb3cSopenharmony_ci    if (!taskExecutor_ || !callback) {
13823b3eb3cSopenharmony_ci        return;
13923b3eb3cSopenharmony_ci    }
14023b3eb3cSopenharmony_ci
14123b3eb3cSopenharmony_ci    if (syncMode) {
14223b3eb3cSopenharmony_ci        GetDataSync(callback);
14323b3eb3cSopenharmony_ci    } else {
14423b3eb3cSopenharmony_ci        GetDataAsync(callback);
14523b3eb3cSopenharmony_ci    }
14623b3eb3cSopenharmony_ci#else
14723b3eb3cSopenharmony_ci    if (syncMode) {
14823b3eb3cSopenharmony_ci        callback(g_clipboard);
14923b3eb3cSopenharmony_ci        return;
15023b3eb3cSopenharmony_ci    }
15123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
15223b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
15323b3eb3cSopenharmony_ci        [callback, taskExecutor = WeakClaim(RawPtr(taskExecutor_)), textData = g_clipboard]() {
15423b3eb3cSopenharmony_ci            callback(textData);
15523b3eb3cSopenharmony_ci        },
15623b3eb3cSopenharmony_ci        TaskExecutor::TaskType::UI, "ArkUIClipboardTextDataCallback");
15723b3eb3cSopenharmony_ci#endif
15823b3eb3cSopenharmony_ci}
15923b3eb3cSopenharmony_ci
16023b3eb3cSopenharmony_civoid ClipboardImpl::GetPixelMapData(const std::function<void(const RefPtr<PixelMap>&)>& callback, bool syncMode)
16123b3eb3cSopenharmony_ci{
16223b3eb3cSopenharmony_ci    if (!taskExecutor_ || !callback) {
16323b3eb3cSopenharmony_ci        return;
16423b3eb3cSopenharmony_ci    }
16523b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
16623b3eb3cSopenharmony_ci    if (syncMode) {
16723b3eb3cSopenharmony_ci        GetPixelMapDataSync(callback);
16823b3eb3cSopenharmony_ci    } else {
16923b3eb3cSopenharmony_ci        GetPixelMapDataAsync(callback);
17023b3eb3cSopenharmony_ci    }
17123b3eb3cSopenharmony_ci#else
17223b3eb3cSopenharmony_ci    if (syncMode) {
17323b3eb3cSopenharmony_ci        callback(g_pixmap);
17423b3eb3cSopenharmony_ci    } else {
17523b3eb3cSopenharmony_ci        taskExecutor_->PostTask([callback, taskExecutor = WeakClaim(RawPtr(taskExecutor_)),
17623b3eb3cSopenharmony_ci                                    imageData = g_pixmap]() { callback(imageData); },
17723b3eb3cSopenharmony_ci            TaskExecutor::TaskType::UI, "ArkUIClipboardImageDataCallback");
17823b3eb3cSopenharmony_ci    }
17923b3eb3cSopenharmony_ci#endif
18023b3eb3cSopenharmony_ci}
18123b3eb3cSopenharmony_ci
18223b3eb3cSopenharmony_ciRefPtr<PasteDataMix> ClipboardImpl::CreatePasteDataMix()
18323b3eb3cSopenharmony_ci{
18423b3eb3cSopenharmony_ci    return AceType::MakeRefPtr<PasteDataImpl>();
18523b3eb3cSopenharmony_ci}
18623b3eb3cSopenharmony_ci
18723b3eb3cSopenharmony_civoid ClipboardImpl::AddMultiTypeRecord(
18823b3eb3cSopenharmony_ci    const RefPtr<PasteDataMix>& pasteData, const RefPtr<MultiTypeRecordMix>& multiTypeRecord)
18923b3eb3cSopenharmony_ci{
19023b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
19123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
19223b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
19323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
19423b3eb3cSopenharmony_ci    auto multiTypeRecordImpl = AceType::DynamicCast<MultiTypeRecordImpl>(multiTypeRecord);
19523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(multiTypeRecordImpl);
19623b3eb3cSopenharmony_ci
19723b3eb3cSopenharmony_ci    std::map<std::string, std::shared_ptr<OHOS::MiscServices::EntryValue>> multiTypeDataMap;
19823b3eb3cSopenharmony_ci    if (!multiTypeRecordImpl->GetPlainText().empty()) {
19923b3eb3cSopenharmony_ci        multiTypeDataMap[OHOS::MiscServices::MIMETYPE_TEXT_PLAIN] =
20023b3eb3cSopenharmony_ci            std::make_shared<OHOS::MiscServices::EntryValue>(multiTypeRecordImpl->GetPlainText());
20123b3eb3cSopenharmony_ci    }
20223b3eb3cSopenharmony_ci    if (!multiTypeRecordImpl->GetUri().empty()) {
20323b3eb3cSopenharmony_ci        multiTypeDataMap[OHOS::MiscServices::MIMETYPE_TEXT_URI] =
20423b3eb3cSopenharmony_ci            std::make_shared<OHOS::MiscServices::EntryValue>(multiTypeRecordImpl->GetUri());
20523b3eb3cSopenharmony_ci    }
20623b3eb3cSopenharmony_ci    if (multiTypeRecordImpl->GetPixelMap()) {
20723b3eb3cSopenharmony_ci        multiTypeDataMap[OHOS::MiscServices::MIMETYPE_PIXELMAP] =
20823b3eb3cSopenharmony_ci            std::make_shared<OHOS::MiscServices::EntryValue>(multiTypeRecordImpl->GetPixelMap());
20923b3eb3cSopenharmony_ci    }
21023b3eb3cSopenharmony_ci    if (!multiTypeRecordImpl->GetSpanStringBuffer().empty()) {
21123b3eb3cSopenharmony_ci        multiTypeDataMap[SPAN_STRING_TAG] =
21223b3eb3cSopenharmony_ci            std::make_shared<OHOS::MiscServices::EntryValue>(multiTypeRecordImpl->GetSpanStringBuffer());
21323b3eb3cSopenharmony_ci    }
21423b3eb3cSopenharmony_ci
21523b3eb3cSopenharmony_ci    auto entry =
21623b3eb3cSopenharmony_ci        std::make_shared<std::map<std::string, std::shared_ptr<OHOS::MiscServices::EntryValue>>>(multiTypeDataMap);
21723b3eb3cSopenharmony_ci    peData->GetPasteDataData()->AddRecord(
21823b3eb3cSopenharmony_ci        MiscServices::PasteDataRecord::NewMultiTypeRecord(entry, GetMimeType(multiTypeDataMap)));
21923b3eb3cSopenharmony_ci#endif
22023b3eb3cSopenharmony_ci}
22123b3eb3cSopenharmony_ci
22223b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
22323b3eb3cSopenharmony_ciconst std::string ClipboardImpl::GetMimeType(
22423b3eb3cSopenharmony_ci    std::map<std::string, std::shared_ptr<OHOS::MiscServices::EntryValue>> multiTypeDataMap)
22523b3eb3cSopenharmony_ci{
22623b3eb3cSopenharmony_ci    std::string mimeType;
22723b3eb3cSopenharmony_ci    if (multiTypeDataMap.find(SPAN_STRING_TAG) != multiTypeDataMap.end()) {
22823b3eb3cSopenharmony_ci        mimeType = SPAN_STRING_TAG;
22923b3eb3cSopenharmony_ci    }
23023b3eb3cSopenharmony_ci    if (multiTypeDataMap.find(OHOS::MiscServices::MIMETYPE_PIXELMAP) != multiTypeDataMap.end()) {
23123b3eb3cSopenharmony_ci        mimeType = OHOS::MiscServices::MIMETYPE_PIXELMAP;
23223b3eb3cSopenharmony_ci    }
23323b3eb3cSopenharmony_ci    if (multiTypeDataMap.find(OHOS::MiscServices::MIMETYPE_TEXT_URI) != multiTypeDataMap.end()) {
23423b3eb3cSopenharmony_ci        mimeType = OHOS::MiscServices::MIMETYPE_TEXT_URI;
23523b3eb3cSopenharmony_ci    }
23623b3eb3cSopenharmony_ci    if (multiTypeDataMap.find(OHOS::MiscServices::MIMETYPE_TEXT_PLAIN) != multiTypeDataMap.end()) {
23723b3eb3cSopenharmony_ci        mimeType = OHOS::MiscServices::MIMETYPE_TEXT_PLAIN;
23823b3eb3cSopenharmony_ci    }
23923b3eb3cSopenharmony_ci    return mimeType;
24023b3eb3cSopenharmony_ci}
24123b3eb3cSopenharmony_ci#endif
24223b3eb3cSopenharmony_ci
24323b3eb3cSopenharmony_civoid MultiTypeRecordImpl::SetPlainText(const std::string plainText)
24423b3eb3cSopenharmony_ci{
24523b3eb3cSopenharmony_ci    plainText_ = plainText;
24623b3eb3cSopenharmony_ci}
24723b3eb3cSopenharmony_civoid MultiTypeRecordImpl::SetUri(const std::string uri)
24823b3eb3cSopenharmony_ci{
24923b3eb3cSopenharmony_ci    uri_ = uri;
25023b3eb3cSopenharmony_ci}
25123b3eb3cSopenharmony_civoid MultiTypeRecordImpl::SetPixelMap(RefPtr<PixelMap> pixelMap)
25223b3eb3cSopenharmony_ci{
25323b3eb3cSopenharmony_ci    pixelMap_ = pixelMap;
25423b3eb3cSopenharmony_ci}
25523b3eb3cSopenharmony_ciconst RefPtr<PixelMap> MultiTypeRecordImpl::GetPixelMap()
25623b3eb3cSopenharmony_ci{
25723b3eb3cSopenharmony_ci    return pixelMap_;
25823b3eb3cSopenharmony_ci}
25923b3eb3cSopenharmony_ciconst std::string MultiTypeRecordImpl::GetPlainText()
26023b3eb3cSopenharmony_ci{
26123b3eb3cSopenharmony_ci    return plainText_;
26223b3eb3cSopenharmony_ci}
26323b3eb3cSopenharmony_ciconst std::string MultiTypeRecordImpl::GetUri()
26423b3eb3cSopenharmony_ci{
26523b3eb3cSopenharmony_ci    return uri_;
26623b3eb3cSopenharmony_ci}
26723b3eb3cSopenharmony_cistd::vector<uint8_t>& MultiTypeRecordImpl::GetSpanStringBuffer()
26823b3eb3cSopenharmony_ci{
26923b3eb3cSopenharmony_ci    return spanStringBuffer_;
27023b3eb3cSopenharmony_ci}
27123b3eb3cSopenharmony_ci
27223b3eb3cSopenharmony_civoid ClipboardImpl::AddPixelMapRecord(const RefPtr<PasteDataMix>& pasteData, const RefPtr<PixelMap>& pixmap)
27323b3eb3cSopenharmony_ci{
27423b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
27523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
27623b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
27723b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
27823b3eb3cSopenharmony_ci    auto pixmapOhos = AceType::DynamicCast<PixelMapOhos>(pixmap);
27923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(pixmapOhos);
28023b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "add pixelMap record to pasteData");
28123b3eb3cSopenharmony_ci    peData->GetPasteDataData()->AddPixelMapRecord(pixmapOhos->GetPixelMapSharedPtr());
28223b3eb3cSopenharmony_ci#endif
28323b3eb3cSopenharmony_ci}
28423b3eb3cSopenharmony_ci
28523b3eb3cSopenharmony_civoid ClipboardImpl::AddImageRecord(const RefPtr<PasteDataMix>& pasteData, const std::string& uri)
28623b3eb3cSopenharmony_ci{
28723b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
28823b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
28923b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
29023b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
29123b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "add url %{private}s record to pasteData", uri.c_str());
29223b3eb3cSopenharmony_ci    peData->GetPasteDataData()->AddUriRecord(OHOS::Uri(uri));
29323b3eb3cSopenharmony_ci#endif
29423b3eb3cSopenharmony_ci}
29523b3eb3cSopenharmony_ci
29623b3eb3cSopenharmony_civoid ClipboardImpl::AddTextRecord(const RefPtr<PasteDataMix>& pasteData, const std::string& selectedStr)
29723b3eb3cSopenharmony_ci{
29823b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
29923b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
30023b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
30123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
30223b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "add text record to pasteData, length: %{public}d",
30323b3eb3cSopenharmony_ci        static_cast<int32_t>(StringUtils::ToWstring(selectedStr).length()));
30423b3eb3cSopenharmony_ci    peData->GetPasteDataData()->AddTextRecord(selectedStr);
30523b3eb3cSopenharmony_ci#endif
30623b3eb3cSopenharmony_ci}
30723b3eb3cSopenharmony_ci
30823b3eb3cSopenharmony_civoid ClipboardImpl::AddSpanStringRecord(const RefPtr<PasteDataMix>& pasteData, std::vector<uint8_t>& data)
30923b3eb3cSopenharmony_ci{
31023b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
31123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(taskExecutor_);
31223b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
31323b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
31423b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "add spanstring record to pasteData, length: %{public}d",
31523b3eb3cSopenharmony_ci        static_cast<int32_t>(data.size()));
31623b3eb3cSopenharmony_ci    peData->GetPasteDataData()->AddKvRecord(SPAN_STRING_TAG, data);
31723b3eb3cSopenharmony_ci#endif
31823b3eb3cSopenharmony_ci}
31923b3eb3cSopenharmony_ci
32023b3eb3cSopenharmony_civoid ClipboardImpl::SetData(const RefPtr<PasteDataMix>& pasteData, CopyOptions copyOption)
32123b3eb3cSopenharmony_ci{
32223b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
32323b3eb3cSopenharmony_ci    auto shareOption = TransitionCopyOption(copyOption);
32423b3eb3cSopenharmony_ci    auto peData = AceType::DynamicCast<PasteDataImpl>(pasteData);
32523b3eb3cSopenharmony_ci    CHECK_NULL_VOID(peData);
32623b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
32723b3eb3cSopenharmony_ci        [peData, shareOption]() {
32823b3eb3cSopenharmony_ci            auto pasteData = peData->GetPasteDataData();
32923b3eb3cSopenharmony_ci            pasteData->SetShareOption(shareOption);
33023b3eb3cSopenharmony_ci            TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "add pasteData to clipboard, shareOption:  %{public}d", shareOption);
33123b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteboardClient::GetInstance()->SetPasteData(*pasteData);
33223b3eb3cSopenharmony_ci        },
33323b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardSetMixDataWithCopyOption");
33423b3eb3cSopenharmony_ci#endif
33523b3eb3cSopenharmony_ci}
33623b3eb3cSopenharmony_ci
33723b3eb3cSopenharmony_civoid ClipboardImpl::GetData(const std::function<void(const std::string&, bool isLastRecord)>& textCallback,
33823b3eb3cSopenharmony_ci    const std::function<void(const RefPtr<PixelMap>&, bool isLastRecord)>& pixelMapCallback,
33923b3eb3cSopenharmony_ci    const std::function<void(const std::string&, bool isLastRecord)>& urlCallback, bool syncMode)
34023b3eb3cSopenharmony_ci{
34123b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
34223b3eb3cSopenharmony_ci    if (!taskExecutor_ || !textCallback || !pixelMapCallback || !urlCallback) {
34323b3eb3cSopenharmony_ci        return;
34423b3eb3cSopenharmony_ci    }
34523b3eb3cSopenharmony_ci
34623b3eb3cSopenharmony_ci    if (syncMode) {
34723b3eb3cSopenharmony_ci        GetDataSync(textCallback, pixelMapCallback, urlCallback);
34823b3eb3cSopenharmony_ci    } else {
34923b3eb3cSopenharmony_ci        GetDataAsync(textCallback, pixelMapCallback, urlCallback);
35023b3eb3cSopenharmony_ci    }
35123b3eb3cSopenharmony_ci#endif
35223b3eb3cSopenharmony_ci}
35323b3eb3cSopenharmony_ci
35423b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
35523b3eb3cSopenharmony_cistd::shared_ptr<MiscServices::PasteData> PasteDataImpl::GetPasteDataData()
35623b3eb3cSopenharmony_ci{
35723b3eb3cSopenharmony_ci    if (pasteData_ == nullptr) {
35823b3eb3cSopenharmony_ci        pasteData_ = std::make_shared<MiscServices::PasteData>();
35923b3eb3cSopenharmony_ci    }
36023b3eb3cSopenharmony_ci    return pasteData_;
36123b3eb3cSopenharmony_ci}
36223b3eb3cSopenharmony_civoid PasteDataImpl::SetUnifiedData(std::shared_ptr<MiscServices::PasteData> pasteData)
36323b3eb3cSopenharmony_ci{
36423b3eb3cSopenharmony_ci    pasteData_ = pasteData;
36523b3eb3cSopenharmony_ci}
36623b3eb3cSopenharmony_ci
36723b3eb3cSopenharmony_civoid ClipboardImpl::GetDataSync(const std::function<void(const std::string&)>& callback)
36823b3eb3cSopenharmony_ci{
36923b3eb3cSopenharmony_ci    std::string result;
37023b3eb3cSopenharmony_ci    taskExecutor_->PostSyncTask(
37123b3eb3cSopenharmony_ci        [&result]() {
37223b3eb3cSopenharmony_ci            auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
37323b3eb3cSopenharmony_ci            CHECK_NULL_VOID(has);
37423b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteData pasteData;
37523b3eb3cSopenharmony_ci            auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
37623b3eb3cSopenharmony_ci            CHECK_NULL_VOID(ok);
37723b3eb3cSopenharmony_ci            for (const auto& pasteDataRecord : pasteData.AllRecords()) {
37823b3eb3cSopenharmony_ci                if (pasteDataRecord == nullptr) {
37923b3eb3cSopenharmony_ci                    continue;
38023b3eb3cSopenharmony_ci                }
38123b3eb3cSopenharmony_ci                if (pasteDataRecord->GetCustomData() != nullptr) {
38223b3eb3cSopenharmony_ci                    auto customData = pasteDataRecord->GetCustomData();
38323b3eb3cSopenharmony_ci                    auto itemData = customData->GetItemData();
38423b3eb3cSopenharmony_ci                    if (itemData.find(SPAN_STRING_TAG) == itemData.end()) {
38523b3eb3cSopenharmony_ci                        continue;
38623b3eb3cSopenharmony_ci                    }
38723b3eb3cSopenharmony_ci                    auto spanStr = SpanString::DecodeTlv(itemData[SPAN_STRING_TAG]);
38823b3eb3cSopenharmony_ci                    if (spanStr) {
38923b3eb3cSopenharmony_ci                        result = spanStr->GetString();
39023b3eb3cSopenharmony_ci                        break;
39123b3eb3cSopenharmony_ci                    }
39223b3eb3cSopenharmony_ci                }
39323b3eb3cSopenharmony_ci                if (pasteDataRecord->GetHtmlText() != nullptr) {
39423b3eb3cSopenharmony_ci                    auto htmlText = pasteDataRecord->GetHtmlText();
39523b3eb3cSopenharmony_ci                    HtmlToSpan toSpan;
39623b3eb3cSopenharmony_ci                    auto spanStr = toSpan.ToSpanString(*htmlText, false);
39723b3eb3cSopenharmony_ci                    if (spanStr) {
39823b3eb3cSopenharmony_ci                        result = spanStr->GetString();
39923b3eb3cSopenharmony_ci                        break;
40023b3eb3cSopenharmony_ci                    }
40123b3eb3cSopenharmony_ci                }
40223b3eb3cSopenharmony_ci            }
40323b3eb3cSopenharmony_ci            if (result.empty()) {
40423b3eb3cSopenharmony_ci                auto textData = pasteData.GetPrimaryText();
40523b3eb3cSopenharmony_ci                CHECK_NULL_VOID(textData);
40623b3eb3cSopenharmony_ci                result = *textData;
40723b3eb3cSopenharmony_ci            }
40823b3eb3cSopenharmony_ci        },
40923b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardGetTextDataSync");
41023b3eb3cSopenharmony_ci    callback(result);
41123b3eb3cSopenharmony_ci}
41223b3eb3cSopenharmony_ci
41323b3eb3cSopenharmony_civoid ClipboardImpl::GetDataAsync(const std::function<void(const std::string&)>& callback)
41423b3eb3cSopenharmony_ci{
41523b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
41623b3eb3cSopenharmony_ci        [callback, weakExecutor = WeakClaim(RawPtr(taskExecutor_)), weak = WeakClaim(this)]() {
41723b3eb3cSopenharmony_ci            auto clip = weak.Upgrade();
41823b3eb3cSopenharmony_ci            auto taskExecutor = weakExecutor.Upgrade();
41923b3eb3cSopenharmony_ci            CHECK_NULL_VOID(taskExecutor);
42023b3eb3cSopenharmony_ci            if (!OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData()) {
42123b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "SystemKeyboardData is not exist from MiscServices");
42223b3eb3cSopenharmony_ci                taskExecutor->PostTask(
42323b3eb3cSopenharmony_ci                    [callback]() { callback(""); }, TaskExecutor::TaskType::UI, "ArkUIClipboardHasDataFailed");
42423b3eb3cSopenharmony_ci                return;
42523b3eb3cSopenharmony_ci            }
42623b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteData pasteData;
42723b3eb3cSopenharmony_ci            if (!OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData)) {
42823b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "Get SystemKeyboardData fail from MiscServices");
42923b3eb3cSopenharmony_ci                taskExecutor->PostTask(
43023b3eb3cSopenharmony_ci                    [callback]() { callback(""); }, TaskExecutor::TaskType::UI, "ArkUIClipboardGetDataFailed");
43123b3eb3cSopenharmony_ci                return;
43223b3eb3cSopenharmony_ci            }
43323b3eb3cSopenharmony_ci            std::string resText;
43423b3eb3cSopenharmony_ci            for (const auto& pasteDataRecord : pasteData.AllRecords()) {
43523b3eb3cSopenharmony_ci                if (clip->ProcessPasteDataRecord(pasteDataRecord, resText)) {
43623b3eb3cSopenharmony_ci                    break;
43723b3eb3cSopenharmony_ci                }
43823b3eb3cSopenharmony_ci            }
43923b3eb3cSopenharmony_ci            if (resText.empty()) {
44023b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "Get SystemKeyboardTextData fail from MiscServices");
44123b3eb3cSopenharmony_ci                taskExecutor->PostTask(
44223b3eb3cSopenharmony_ci                    [callback]() { callback(""); }, TaskExecutor::TaskType::UI, "ArkUIClipboardGetTextDataFailed");
44323b3eb3cSopenharmony_ci                return;
44423b3eb3cSopenharmony_ci            }
44523b3eb3cSopenharmony_ci            TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "resText len:%{public}d", static_cast<int32_t>(resText.length()));
44623b3eb3cSopenharmony_ci            auto result = resText;
44723b3eb3cSopenharmony_ci            taskExecutor->PostTask(
44823b3eb3cSopenharmony_ci                [callback, result]() { callback(result); },
44923b3eb3cSopenharmony_ci                TaskExecutor::TaskType::UI, "ArkUIClipboardGetTextDataCallback");
45023b3eb3cSopenharmony_ci        },
45123b3eb3cSopenharmony_ci        TaskExecutor::TaskType::BACKGROUND, "ArkUIClipboardGetTextDataAsync");
45223b3eb3cSopenharmony_ci}
45323b3eb3cSopenharmony_ci
45423b3eb3cSopenharmony_cibool ClipboardImpl::ProcessPasteDataRecord(const std::shared_ptr<MiscServices::PasteDataRecord>& pasteDataRecord,
45523b3eb3cSopenharmony_ci    std::string& resText)
45623b3eb3cSopenharmony_ci{
45723b3eb3cSopenharmony_ci    if (pasteDataRecord == nullptr) {
45823b3eb3cSopenharmony_ci        return false;
45923b3eb3cSopenharmony_ci    }
46023b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "mimeType:%{public}s", pasteDataRecord->GetMimeType().c_str());
46123b3eb3cSopenharmony_ci    if (pasteDataRecord->GetHtmlText() != nullptr) {
46223b3eb3cSopenharmony_ci        auto htmlText = pasteDataRecord->GetHtmlText();
46323b3eb3cSopenharmony_ci        TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "htmlText:%{private}s, length=%{public}zu", htmlText->c_str(),
46423b3eb3cSopenharmony_ci            htmlText->length());
46523b3eb3cSopenharmony_ci        HtmlToSpan toSpan;
46623b3eb3cSopenharmony_ci        auto spanStr = toSpan.ToSpanString(*htmlText);
46723b3eb3cSopenharmony_ci        if (spanStr) {
46823b3eb3cSopenharmony_ci            resText = spanStr->GetString();
46923b3eb3cSopenharmony_ci            return true;
47023b3eb3cSopenharmony_ci        }
47123b3eb3cSopenharmony_ci    }
47223b3eb3cSopenharmony_ci    if (pasteDataRecord->GetCustomData() != nullptr) {
47323b3eb3cSopenharmony_ci        auto itemData = pasteDataRecord->GetCustomData()->GetItemData();
47423b3eb3cSopenharmony_ci        if (itemData.find(SPAN_STRING_TAG) != itemData.end()) {
47523b3eb3cSopenharmony_ci            auto spanStr = SpanString::DecodeTlv(itemData[SPAN_STRING_TAG]);
47623b3eb3cSopenharmony_ci            if (spanStr) {
47723b3eb3cSopenharmony_ci                resText = spanStr->GetString();
47823b3eb3cSopenharmony_ci                return true;
47923b3eb3cSopenharmony_ci            }
48023b3eb3cSopenharmony_ci        }
48123b3eb3cSopenharmony_ci    }
48223b3eb3cSopenharmony_ci    if (pasteDataRecord->GetPlainText() != nullptr) {
48323b3eb3cSopenharmony_ci        auto textData = pasteDataRecord->GetPlainText();
48423b3eb3cSopenharmony_ci        TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "textData:%{private}s, length:%{public}zu", textData->c_str(),
48523b3eb3cSopenharmony_ci            textData->length());
48623b3eb3cSopenharmony_ci        resText.append(*textData);
48723b3eb3cSopenharmony_ci    }
48823b3eb3cSopenharmony_ci    return false;
48923b3eb3cSopenharmony_ci}
49023b3eb3cSopenharmony_ci
49123b3eb3cSopenharmony_civoid ClipboardImpl::GetDataSync(const std::function<void(const std::string&, bool isLastRecord)>& textCallback,
49223b3eb3cSopenharmony_ci    const std::function<void(const RefPtr<PixelMap>&, bool isLastRecord)>& pixelMapCallback,
49323b3eb3cSopenharmony_ci    const std::function<void(const std::string&, bool isLastRecord)>& urlCallback)
49423b3eb3cSopenharmony_ci{
49523b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "get data from clipboard, sync");
49623b3eb3cSopenharmony_ci    OHOS::MiscServices::PasteData pasteData;
49723b3eb3cSopenharmony_ci    taskExecutor_->PostSyncTask(
49823b3eb3cSopenharmony_ci        [&pasteData]() {
49923b3eb3cSopenharmony_ci            auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
50023b3eb3cSopenharmony_ci            CHECK_NULL_VOID(has);
50123b3eb3cSopenharmony_ci            auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
50223b3eb3cSopenharmony_ci            CHECK_NULL_VOID(ok);
50323b3eb3cSopenharmony_ci        },
50423b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardGetPasteDataSync");
50523b3eb3cSopenharmony_ci
50623b3eb3cSopenharmony_ci    auto count = pasteData.GetRecordCount();
50723b3eb3cSopenharmony_ci    size_t index = 0;
50823b3eb3cSopenharmony_ci    for (const auto& pasteDataRecord : pasteData.AllRecords()) {
50923b3eb3cSopenharmony_ci        index++;
51023b3eb3cSopenharmony_ci        if (pasteDataRecord == nullptr) {
51123b3eb3cSopenharmony_ci            continue;
51223b3eb3cSopenharmony_ci        }
51323b3eb3cSopenharmony_ci        bool isLastRecord = index == count;
51423b3eb3cSopenharmony_ci        if (pasteDataRecord->GetPlainText() != nullptr) {
51523b3eb3cSopenharmony_ci            auto textData = pasteDataRecord->GetPlainText();
51623b3eb3cSopenharmony_ci            auto result = *textData;
51723b3eb3cSopenharmony_ci            textCallback(result, isLastRecord);
51823b3eb3cSopenharmony_ci        } else if (pasteDataRecord->GetPixelMap() != nullptr) {
51923b3eb3cSopenharmony_ci            auto imageData = pasteDataRecord->GetPixelMap();
52023b3eb3cSopenharmony_ci            auto result = AceType::MakeRefPtr<PixelMapOhos>(imageData);
52123b3eb3cSopenharmony_ci            pixelMapCallback(result, isLastRecord);
52223b3eb3cSopenharmony_ci        } else if (pasteDataRecord->GetUri() != nullptr) {
52323b3eb3cSopenharmony_ci            auto textData = pasteDataRecord->GetUri();
52423b3eb3cSopenharmony_ci            auto result = (*textData).ToString();
52523b3eb3cSopenharmony_ci            urlCallback(result, isLastRecord);
52623b3eb3cSopenharmony_ci        }
52723b3eb3cSopenharmony_ci    }
52823b3eb3cSopenharmony_ci}
52923b3eb3cSopenharmony_ci
53023b3eb3cSopenharmony_civoid ClipboardImpl::GetDataAsync(const std::function<void(const std::string&, bool isLastRecord)>& textCallback,
53123b3eb3cSopenharmony_ci    const std::function<void(const RefPtr<PixelMap>&, bool isLastRecord)>& pixelMapCallback,
53223b3eb3cSopenharmony_ci    const std::function<void(const std::string&, bool isLastRecord)>& urlCallback)
53323b3eb3cSopenharmony_ci{
53423b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_CLIPBOARD, "get data from clipboard, async");
53523b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
53623b3eb3cSopenharmony_ci        [textCallback, pixelMapCallback, urlCallback, weakExecutor = WeakClaim(RawPtr(taskExecutor_))]() {
53723b3eb3cSopenharmony_ci            auto taskExecutor = weakExecutor.Upgrade();
53823b3eb3cSopenharmony_ci            auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
53923b3eb3cSopenharmony_ci            CHECK_NULL_VOID(has);
54023b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteData pasteData;
54123b3eb3cSopenharmony_ci            auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
54223b3eb3cSopenharmony_ci            CHECK_NULL_VOID(ok);
54323b3eb3cSopenharmony_ci            auto count = pasteData.GetRecordCount();
54423b3eb3cSopenharmony_ci            size_t index = 0;
54523b3eb3cSopenharmony_ci            for (const auto& pasteDataRecord : pasteData.AllRecords()) {
54623b3eb3cSopenharmony_ci                index++;
54723b3eb3cSopenharmony_ci                if (pasteDataRecord == nullptr) {
54823b3eb3cSopenharmony_ci                    continue;
54923b3eb3cSopenharmony_ci                }
55023b3eb3cSopenharmony_ci                bool isLastRecord = index == count;
55123b3eb3cSopenharmony_ci                if (pasteDataRecord->GetPlainText() != nullptr) {
55223b3eb3cSopenharmony_ci                    auto textData = pasteDataRecord->GetPlainText();
55323b3eb3cSopenharmony_ci                    auto result = *textData;
55423b3eb3cSopenharmony_ci                    taskExecutor->PostTask(
55523b3eb3cSopenharmony_ci                        [textCallback, result, isLastRecord]() { textCallback(result, isLastRecord); },
55623b3eb3cSopenharmony_ci                        TaskExecutor::TaskType::UI, "ArkUIClipboardGetTextCallback");
55723b3eb3cSopenharmony_ci                } else if (pasteDataRecord->GetPixelMap() != nullptr) {
55823b3eb3cSopenharmony_ci                    auto imageData = pasteDataRecord->GetPixelMap();
55923b3eb3cSopenharmony_ci                    auto result = AceType::MakeRefPtr<PixelMapOhos>(imageData);
56023b3eb3cSopenharmony_ci                    taskExecutor->PostTask(
56123b3eb3cSopenharmony_ci                        [pixelMapCallback, result, isLastRecord]() { pixelMapCallback(result, isLastRecord); },
56223b3eb3cSopenharmony_ci                        TaskExecutor::TaskType::UI, "ArkUIClipboardGetImageCallback");
56323b3eb3cSopenharmony_ci                } else if (pasteDataRecord->GetUri() != nullptr) {
56423b3eb3cSopenharmony_ci                    auto textData = pasteDataRecord->GetUri();
56523b3eb3cSopenharmony_ci                    auto result = (*textData).ToString();
56623b3eb3cSopenharmony_ci                    taskExecutor->PostTask([urlCallback, result, isLastRecord]() { urlCallback(result, isLastRecord); },
56723b3eb3cSopenharmony_ci                        TaskExecutor::TaskType::UI, "ArkUIClipboardGetUrlCallback");
56823b3eb3cSopenharmony_ci                }
56923b3eb3cSopenharmony_ci            }
57023b3eb3cSopenharmony_ci        },
57123b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardGetDataAsync");
57223b3eb3cSopenharmony_ci}
57323b3eb3cSopenharmony_ci
57423b3eb3cSopenharmony_civoid ClipboardImpl::GetSpanStringData(
57523b3eb3cSopenharmony_ci    const std::function<void(std::vector<std::vector<uint8_t>>&, const std::string&, bool&)>& callback, bool syncMode)
57623b3eb3cSopenharmony_ci{
57723b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
57823b3eb3cSopenharmony_ci    if (!taskExecutor_ || !callback) {
57923b3eb3cSopenharmony_ci        return;
58023b3eb3cSopenharmony_ci    }
58123b3eb3cSopenharmony_ci
58223b3eb3cSopenharmony_ci    GetSpanStringDataHelper(callback, syncMode);
58323b3eb3cSopenharmony_ci#endif
58423b3eb3cSopenharmony_ci}
58523b3eb3cSopenharmony_ci
58623b3eb3cSopenharmony_civoid ClipboardImpl::GetSpanStringDataHelper(
58723b3eb3cSopenharmony_ci    const std::function<void(std::vector<std::vector<uint8_t>>&, const std::string&, bool&)>& callback, bool syncMode)
58823b3eb3cSopenharmony_ci{
58923b3eb3cSopenharmony_ci    auto task = [callback, weakExecutor = WeakClaim(RawPtr(taskExecutor_)), weak = WeakClaim(this)]() {
59023b3eb3cSopenharmony_ci        auto clip = weak.Upgrade();
59123b3eb3cSopenharmony_ci        CHECK_NULL_VOID(clip);
59223b3eb3cSopenharmony_ci        auto taskExecutor = weakExecutor.Upgrade();
59323b3eb3cSopenharmony_ci        CHECK_NULL_VOID(taskExecutor);
59423b3eb3cSopenharmony_ci        auto hasData = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
59523b3eb3cSopenharmony_ci        CHECK_NULL_VOID(hasData);
59623b3eb3cSopenharmony_ci        OHOS::MiscServices::PasteData pasteData;
59723b3eb3cSopenharmony_ci        auto getDataRes = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
59823b3eb3cSopenharmony_ci        CHECK_NULL_VOID(getDataRes);
59923b3eb3cSopenharmony_ci        std::vector<std::vector<uint8_t>> arrays;
60023b3eb3cSopenharmony_ci        std::string text;
60123b3eb3cSopenharmony_ci        bool isMultiTypeRecord = false;
60223b3eb3cSopenharmony_ci        clip->ProcessSpanStringData(arrays, pasteData, text, isMultiTypeRecord);
60323b3eb3cSopenharmony_ci        auto textData = pasteData.GetPrimaryText();
60423b3eb3cSopenharmony_ci        if (textData && text.empty()) {
60523b3eb3cSopenharmony_ci            text.append(*textData);
60623b3eb3cSopenharmony_ci        }
60723b3eb3cSopenharmony_ci        auto result = text;
60823b3eb3cSopenharmony_ci        taskExecutor->PostTask(
60923b3eb3cSopenharmony_ci            [callback, arrays, result, isMultiTypeRecord]() mutable { callback(arrays, result, isMultiTypeRecord); },
61023b3eb3cSopenharmony_ci            TaskExecutor::TaskType::UI, "ArkUIClipboardGetSpanStringDataCallback");
61123b3eb3cSopenharmony_ci    };
61223b3eb3cSopenharmony_ci    if (syncMode) {
61323b3eb3cSopenharmony_ci        taskExecutor_->PostSyncTask(task, TaskExecutor::TaskType::BACKGROUND, "ArkUIClipboardGetSpanStringDataSync");
61423b3eb3cSopenharmony_ci    } else {
61523b3eb3cSopenharmony_ci        taskExecutor_->PostTask(task, TaskExecutor::TaskType::BACKGROUND, "ArkUIClipboardGetSpanStringDataAsync");
61623b3eb3cSopenharmony_ci    }
61723b3eb3cSopenharmony_ci}
61823b3eb3cSopenharmony_ci
61923b3eb3cSopenharmony_civoid ClipboardImpl::ProcessSpanStringData(std::vector<std::vector<uint8_t>>& arrays,
62023b3eb3cSopenharmony_ci    const OHOS::MiscServices::PasteData& pasteData, std::string& text, bool& isMultiTypeRecord)
62123b3eb3cSopenharmony_ci{
62223b3eb3cSopenharmony_ci    for (const auto& pasteDataRecord : pasteData.AllRecords()) {
62323b3eb3cSopenharmony_ci        if (pasteDataRecord == nullptr) {
62423b3eb3cSopenharmony_ci            continue;
62523b3eb3cSopenharmony_ci        }
62623b3eb3cSopenharmony_ci#ifdef SYSTEM_CLIPBOARD_SUPPORTED
62723b3eb3cSopenharmony_ci        std::vector<std::string> types = { SPAN_STRING_TAG, OHOS::MiscServices::MIMETYPE_TEXT_URI,
62823b3eb3cSopenharmony_ci            OHOS::MiscServices::MIMETYPE_PIXELMAP, OHOS::MiscServices::MIMETYPE_TEXT_PLAIN };
62923b3eb3cSopenharmony_ci        auto validTypes = pasteDataRecord->GetValidMimeTypes(types);
63023b3eb3cSopenharmony_ci        if (validTypes.size() > 1) {
63123b3eb3cSopenharmony_ci            isMultiTypeRecord = true;
63223b3eb3cSopenharmony_ci        }
63323b3eb3cSopenharmony_ci#endif
63423b3eb3cSopenharmony_ci        auto hasSpanString = false;
63523b3eb3cSopenharmony_ci        auto entryPtr = pasteDataRecord->GetEntryByMimeType(SPAN_STRING_TAG);
63623b3eb3cSopenharmony_ci        if (entryPtr) {
63723b3eb3cSopenharmony_ci            // entryValue InstanceOf OHOS::MiscServices::EntryValue.
63823b3eb3cSopenharmony_ci            auto entryValue = entryPtr->GetValue();
63923b3eb3cSopenharmony_ci            auto spanStringBuffer = std::get_if<std::vector<uint8_t>>(&entryValue);
64023b3eb3cSopenharmony_ci            arrays.emplace_back(*spanStringBuffer);
64123b3eb3cSopenharmony_ci            hasSpanString = true;
64223b3eb3cSopenharmony_ci        }
64323b3eb3cSopenharmony_ci        if (pasteDataRecord->GetHtmlText() != nullptr && hasSpanString) {
64423b3eb3cSopenharmony_ci            auto htmlText = pasteDataRecord->GetHtmlText();
64523b3eb3cSopenharmony_ci            HtmlToSpan toSpan;
64623b3eb3cSopenharmony_ci            auto spanStr = toSpan.ToSpanString(*htmlText);
64723b3eb3cSopenharmony_ci            if (spanStr) {
64823b3eb3cSopenharmony_ci                std::vector<uint8_t> arr;
64923b3eb3cSopenharmony_ci                spanStr->EncodeTlv(arr);
65023b3eb3cSopenharmony_ci                arrays.emplace_back(arr);
65123b3eb3cSopenharmony_ci            }
65223b3eb3cSopenharmony_ci        }
65323b3eb3cSopenharmony_ci        if (pasteDataRecord->GetPlainText() != nullptr) {
65423b3eb3cSopenharmony_ci            auto textData = pasteDataRecord->GetPlainText();
65523b3eb3cSopenharmony_ci            text.append(*textData);
65623b3eb3cSopenharmony_ci        }
65723b3eb3cSopenharmony_ci    }
65823b3eb3cSopenharmony_ci}
65923b3eb3cSopenharmony_ci
66023b3eb3cSopenharmony_civoid ClipboardImpl::GetPixelMapDataSync(const std::function<void(const RefPtr<PixelMap>&)>& callback)
66123b3eb3cSopenharmony_ci{
66223b3eb3cSopenharmony_ci    RefPtr<PixelMap> pixmap;
66323b3eb3cSopenharmony_ci    taskExecutor_->PostSyncTask(
66423b3eb3cSopenharmony_ci        [&pixmap]() {
66523b3eb3cSopenharmony_ci            auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
66623b3eb3cSopenharmony_ci            CHECK_NULL_VOID(has);
66723b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteData pasteData;
66823b3eb3cSopenharmony_ci            auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
66923b3eb3cSopenharmony_ci            CHECK_NULL_VOID(ok);
67023b3eb3cSopenharmony_ci            auto imageData = pasteData.GetPrimaryPixelMap();
67123b3eb3cSopenharmony_ci            CHECK_NULL_VOID(imageData);
67223b3eb3cSopenharmony_ci            pixmap = AceType::MakeRefPtr<PixelMapOhos>(imageData);
67323b3eb3cSopenharmony_ci        },
67423b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardGetImageDataSync");
67523b3eb3cSopenharmony_ci    callback(pixmap);
67623b3eb3cSopenharmony_ci}
67723b3eb3cSopenharmony_ci
67823b3eb3cSopenharmony_civoid ClipboardImpl::GetPixelMapDataAsync(const std::function<void(const RefPtr<PixelMap>&)>& callback)
67923b3eb3cSopenharmony_ci{
68023b3eb3cSopenharmony_ci    taskExecutor_->PostTask(
68123b3eb3cSopenharmony_ci        [callback, weakExecutor = WeakClaim(RawPtr(taskExecutor_))]() {
68223b3eb3cSopenharmony_ci            auto taskExecutor = weakExecutor.Upgrade();
68323b3eb3cSopenharmony_ci            CHECK_NULL_VOID(taskExecutor);
68423b3eb3cSopenharmony_ci            auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
68523b3eb3cSopenharmony_ci            if (!has) {
68623b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "SystemKeyboardData is not exist from MiscServices");
68723b3eb3cSopenharmony_ci                taskExecutor->PostTask(
68823b3eb3cSopenharmony_ci                    [callback]() { callback(nullptr); }, TaskExecutor::TaskType::UI, "ArkUIClipboardHasDataFailed");
68923b3eb3cSopenharmony_ci                return;
69023b3eb3cSopenharmony_ci            }
69123b3eb3cSopenharmony_ci            OHOS::MiscServices::PasteData pasteData;
69223b3eb3cSopenharmony_ci            auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
69323b3eb3cSopenharmony_ci            if (!ok) {
69423b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "Get SystemKeyboardData fail from MiscServices");
69523b3eb3cSopenharmony_ci                taskExecutor->PostTask(
69623b3eb3cSopenharmony_ci                    [callback]() { callback(nullptr); }, TaskExecutor::TaskType::UI, "ArkUIClipboardGetDataFailed");
69723b3eb3cSopenharmony_ci                return;
69823b3eb3cSopenharmony_ci            }
69923b3eb3cSopenharmony_ci            auto imageData = pasteData.GetPrimaryPixelMap();
70023b3eb3cSopenharmony_ci            if (!imageData) {
70123b3eb3cSopenharmony_ci                TAG_LOGW(AceLogTag::ACE_CLIPBOARD, "Get SystemKeyboardImageData fail from MiscServices");
70223b3eb3cSopenharmony_ci                taskExecutor->PostTask(
70323b3eb3cSopenharmony_ci                    [callback]() { callback(nullptr); },
70423b3eb3cSopenharmony_ci                    TaskExecutor::TaskType::UI, "ArkUIClipboardGetImageDataFailed");
70523b3eb3cSopenharmony_ci                return;
70623b3eb3cSopenharmony_ci            }
70723b3eb3cSopenharmony_ci            auto result = AceType::MakeRefPtr<PixelMapOhos>(imageData);
70823b3eb3cSopenharmony_ci            taskExecutor->PostTask(
70923b3eb3cSopenharmony_ci                [callback, result]() { callback(result); },
71023b3eb3cSopenharmony_ci                TaskExecutor::TaskType::UI, "ArkUIClipboardGetImageDataCallback");
71123b3eb3cSopenharmony_ci        },
71223b3eb3cSopenharmony_ci        TaskExecutor::TaskType::PLATFORM, "ArkUIClipboardGetImageDataAsync");
71323b3eb3cSopenharmony_ci}
71423b3eb3cSopenharmony_ci#endif
71523b3eb3cSopenharmony_ci
71623b3eb3cSopenharmony_civoid ClipboardImpl::Clear() {}
71723b3eb3cSopenharmony_ci
71823b3eb3cSopenharmony_ci} // namespace OHOS::Ace
719