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