1 /* 2 * Copyright (c) 2021 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 "bridge/declarative_frontend/jsview/js_clipboard.h" 17 18 #include <functional> 19 20 #include "base/log/ace_trace.h" 21 #include "core/common/clipboard/clipboard_proxy.h" 22 #include "core/common/container.h" 23 #include "frameworks/bridge/declarative_frontend/engine/functions/js_clipboard_function.h" 24 25 namespace OHOS::Ace::Framework { 26 JSBind(BindingTarget globalObj)27void JSClipboard::JSBind(BindingTarget globalObj) 28 { 29 JSClass<JSClipboard>::Declare("JSClipboard"); 30 JSClass<JSClipboard>::StaticMethod("get", &JSClipboard::Get); 31 JSClass<JSClipboard>::StaticMethod("set", &JSClipboard::Set); 32 JSClass<JSClipboard>::StaticMethod("clear", &JSClipboard::Clear); 33 JSClass<JSClipboard>::Bind(globalObj); 34 } 35 Get(const JSCallbackInfo & info)36void JSClipboard::Get(const JSCallbackInfo& info) 37 { 38 auto container = Container::Current(); 39 if (!container) { 40 LOGW("container is null"); 41 return; 42 } 43 auto executor = container->GetTaskExecutor(); 44 auto clipboard = ClipboardProxy::GetInstance()->GetClipboard(executor); 45 if (info[0]->IsFunction()) { 46 RefPtr<JsClipboardFunction> callback = AceType::MakeRefPtr<JsClipboardFunction>(JSRef<JSFunc>::Cast(info[0])); 47 auto function = [execCtx = info.GetExecutionContext(), callback](const std::string& str) { 48 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); 49 auto func = std::move(callback); 50 ACE_SCORING_EVENT("clipboard.get"); 51 func->Execute(str); 52 }; 53 if (clipboard) { 54 clipboard->GetData(function); 55 } 56 } 57 } 58 Set(const std::string & data)59void JSClipboard::Set(const std::string& data) 60 { 61 auto container = Container::Current(); 62 if (!container) { 63 LOGW("container is null"); 64 return; 65 } 66 auto executor = container->GetTaskExecutor(); 67 auto clipboard = ClipboardProxy::GetInstance()->GetClipboard(executor); 68 if (clipboard) { 69 clipboard->SetData(data); 70 } 71 } 72 Clear()73void JSClipboard::Clear() 74 { 75 auto container = Container::Current(); 76 if (!container) { 77 LOGW("container is null"); 78 return; 79 } 80 auto executor = container->GetTaskExecutor(); 81 auto clipboard = ClipboardProxy::GetInstance()->GetClipboard(executor); 82 if (clipboard) { 83 clipboard->Clear(); 84 } 85 } 86 87 } // namespace OHOS::Ace::Framework