1 /*
2 * Copyright (c) 2021-2022 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 #if (defined(SYSTEM_CLIPBOARD_SUPPORTED))
17 #include "pasteboard_client.h"
18 #endif
19
20 #include "core/common/clipboard/clipboard_impl.h"
21 namespace OHOS::Ace {
SetData(const std::string & data)22 void ClipboardImpl::SetData(const std::string& data)
23 {
24 #if (defined(SYSTEM_CLIPBOARD_SUPPORTED))
25 if (taskExecutor_) {
26 auto pasteData = OHOS::MiscServices::PasteboardClient::GetInstance()->CreatePlainTextData(data);
27 if (!pasteData) {
28 LOGE("create SystemKeyboardData fail from MiscServices");
29 return;
30 }
31 taskExecutor_->PostTask(
32 [pasteData]() { OHOS::MiscServices::PasteboardClient::GetInstance()->SetPasteData(*pasteData); },
33 TaskExecutor::TaskType::IO, "ArkUIClipboardSetPasteData");
34 }
35 #endif
36 }
37
GetData(const std::function<void (const std::string &)> & callback)38 void ClipboardImpl::GetData(const std::function<void(const std::string&)>& callback)
39 {
40 #if (defined(SYSTEM_CLIPBOARD_SUPPORTED))
41 if (taskExecutor_) {
42 auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData();
43 if (!has) {
44 LOGE("SystemKeyboardData is not exist from MiscServices");
45 return;
46 }
47 OHOS::MiscServices::PasteData pasteData;
48 auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData);
49 if (!ok) {
50 LOGE("Get SystemKeyboardData fail from MiscServices");
51 return;
52 }
53 auto textData = pasteData.GetPrimaryText();
54 if (!textData) {
55 LOGE("Get SystemKeyboardTextData fail from MiscServices");
56 return;
57 }
58 taskExecutor_->PostTask(
59 [callback, taskExecutor = WeakClaim(RawPtr(taskExecutor_)), textData]() { callback(*textData); },
60 TaskExecutor::TaskType::IO, "ArkUIClipboardGetPasteData");
61 }
62 #endif
63 }
Clear()64 void ClipboardImpl::Clear() {}
65 } // namespace OHOS::Ace