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)22void 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); 34 } 35 #else 36 LOGI("Current device doesn't support system clipboard"); 37 #endif 38 } 39 GetData(const std::function<void (const std::string &)> & callback)40void ClipboardImpl::GetData(const std::function<void(const std::string&)>& callback) 41 { 42 #if (defined(SYSTEM_CLIPBOARD_SUPPORTED)) 43 if (taskExecutor_) { 44 auto has = OHOS::MiscServices::PasteboardClient::GetInstance()->HasPasteData(); 45 if (!has) { 46 LOGE("SystemKeyboardData is not exist from MiscServices"); 47 return; 48 } 49 OHOS::MiscServices::PasteData pasteData; 50 auto ok = OHOS::MiscServices::PasteboardClient::GetInstance()->GetPasteData(pasteData); 51 if (!ok) { 52 LOGE("Get SystemKeyboardData fail from MiscServices"); 53 return; 54 } 55 auto textData = pasteData.GetPrimaryText(); 56 if (!textData) { 57 LOGE("Get SystemKeyboardTextData fail from MiscServices"); 58 return; 59 } 60 taskExecutor_->PostTask( 61 [callback, taskExecutor = WeakClaim(RawPtr(taskExecutor_)), textData]() { callback(*textData); }, 62 TaskExecutor::TaskType::IO); 63 } 64 #else 65 LOGI("Current device doesn't support system clipboard"); 66 #endif 67 } Clear()68void ClipboardImpl::Clear() {} 69 } // namespace OHOS::Ace