1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci#include <memory>
16bc03f14fSopenharmony_ci#include <thread>
17bc03f14fSopenharmony_ci#include <uv.h>
18bc03f14fSopenharmony_ci
19bc03f14fSopenharmony_ci#include "entry_getter.h"
20bc03f14fSopenharmony_ci#include "napi_common_want.h"
21bc03f14fSopenharmony_ci#include "pasteboard_common.h"
22bc03f14fSopenharmony_ci#include "pasteboard_error.h"
23bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
24bc03f14fSopenharmony_ci#include "pasteboard_js_err.h"
25bc03f14fSopenharmony_ci#include "systempasteboard_napi.h"
26bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices;
27bc03f14fSopenharmony_ciusing namespace OHOS::Media;
28bc03f14fSopenharmony_ci
29bc03f14fSopenharmony_cinamespace OHOS {
30bc03f14fSopenharmony_cinamespace MiscServicesNapi {
31bc03f14fSopenharmony_cistatic thread_local napi_ref g_systemPasteboard = nullptr;
32bc03f14fSopenharmony_cistatic thread_local napi_ref g_systemPasteboard_instance = nullptr;
33bc03f14fSopenharmony_cithread_local std::map<napi_ref, std::shared_ptr<PasteboardObserverInstance>> SystemPasteboardNapi::observers_;
34bc03f14fSopenharmony_cistd::shared_ptr<PasteboardDelayGetterInstance> SystemPasteboardNapi::delayGetter_;
35bc03f14fSopenharmony_cistd::mutex SystemPasteboardNapi::delayMutex_;
36bc03f14fSopenharmony_ciconstexpr int ARGC_TYPE_SET1 = 1;
37bc03f14fSopenharmony_ciconstexpr size_t MAX_ARGS = 6;
38bc03f14fSopenharmony_ciconstexpr size_t SYNC_TIMEOUT = 3500;
39bc03f14fSopenharmony_ciconstexpr size_t DELAY_TIMEOUT = 2;
40bc03f14fSopenharmony_ciconst std::string STRING_UPDATE = "update";
41bc03f14fSopenharmony_ciPasteboardObserverInstance::PasteboardObserverInstance(const napi_env &env, const napi_ref &ref) : env_(env), ref_(ref)
42bc03f14fSopenharmony_ci{
43bc03f14fSopenharmony_ci    stub_ = new (std::nothrow) PasteboardObserverInstance::PasteboardObserverImpl();
44bc03f14fSopenharmony_ci}
45bc03f14fSopenharmony_ci
46bc03f14fSopenharmony_ciPasteboardObserverInstance::~PasteboardObserverInstance()
47bc03f14fSopenharmony_ci{
48bc03f14fSopenharmony_ci    napi_delete_reference(env_, ref_);
49bc03f14fSopenharmony_ci}
50bc03f14fSopenharmony_ci
51bc03f14fSopenharmony_cisptr<PasteboardObserverInstance::PasteboardObserverImpl> PasteboardObserverInstance::GetStub()
52bc03f14fSopenharmony_ci{
53bc03f14fSopenharmony_ci    return stub_;
54bc03f14fSopenharmony_ci}
55bc03f14fSopenharmony_ci
56bc03f14fSopenharmony_civoid UvQueueWorkOnPasteboardChanged(uv_work_t *work, int status)
57bc03f14fSopenharmony_ci{
58bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "UvQueueWorkOnPasteboardChanged start");
59bc03f14fSopenharmony_ci    if (UV_ECANCELED == status || work == nullptr || work->data == nullptr) {
60bc03f14fSopenharmony_ci        return;
61bc03f14fSopenharmony_ci    }
62bc03f14fSopenharmony_ci    PasteboardDataWorker *pasteboardDataWorker = (PasteboardDataWorker *)work->data;
63bc03f14fSopenharmony_ci    if (pasteboardDataWorker == nullptr || pasteboardDataWorker->observer == nullptr) {
64bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "pasteboardDataWorker or ref is null");
65bc03f14fSopenharmony_ci        delete work;
66bc03f14fSopenharmony_ci        work = nullptr;
67bc03f14fSopenharmony_ci        return;
68bc03f14fSopenharmony_ci    }
69bc03f14fSopenharmony_ci
70bc03f14fSopenharmony_ci    auto env = pasteboardDataWorker->observer->GetEnv();
71bc03f14fSopenharmony_ci    auto ref = pasteboardDataWorker->observer->GetRef();
72bc03f14fSopenharmony_ci
73bc03f14fSopenharmony_ci    napi_handle_scope scope = nullptr;
74bc03f14fSopenharmony_ci    napi_open_handle_scope(env, &scope);
75bc03f14fSopenharmony_ci    if (scope == nullptr) {
76bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "scope null");
77bc03f14fSopenharmony_ci        return;
78bc03f14fSopenharmony_ci    }
79bc03f14fSopenharmony_ci    napi_value undefined = nullptr;
80bc03f14fSopenharmony_ci    napi_get_undefined(env, &undefined);
81bc03f14fSopenharmony_ci
82bc03f14fSopenharmony_ci    napi_value callback = nullptr;
83bc03f14fSopenharmony_ci    napi_value resultOut = nullptr;
84bc03f14fSopenharmony_ci    napi_get_reference_value(env, ref, &callback);
85bc03f14fSopenharmony_ci    napi_value result = NapiGetNull(env);
86bc03f14fSopenharmony_ci    napi_call_function(env, undefined, callback, 0, &result, &resultOut);
87bc03f14fSopenharmony_ci
88bc03f14fSopenharmony_ci    napi_close_handle_scope(env, scope);
89bc03f14fSopenharmony_ci    delete pasteboardDataWorker;
90bc03f14fSopenharmony_ci    pasteboardDataWorker = nullptr;
91bc03f14fSopenharmony_ci    delete work;
92bc03f14fSopenharmony_ci}
93bc03f14fSopenharmony_ci
94bc03f14fSopenharmony_civoid PasteboardObserverInstance::OnPasteboardChanged()
95bc03f14fSopenharmony_ci{
96bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "OnPasteboardChanged is called!");
97bc03f14fSopenharmony_ci    uv_loop_s *loop = nullptr;
98bc03f14fSopenharmony_ci    napi_get_uv_event_loop(env_, &loop);
99bc03f14fSopenharmony_ci    if (loop == nullptr) {
100bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "loop instance is nullptr");
101bc03f14fSopenharmony_ci        return;
102bc03f14fSopenharmony_ci    }
103bc03f14fSopenharmony_ci
104bc03f14fSopenharmony_ci    uv_work_t *work = new (std::nothrow) uv_work_t;
105bc03f14fSopenharmony_ci    if (work == nullptr) {
106bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "work is null");
107bc03f14fSopenharmony_ci        return;
108bc03f14fSopenharmony_ci    }
109bc03f14fSopenharmony_ci    PasteboardDataWorker *pasteboardDataWorker = new (std::nothrow) PasteboardDataWorker();
110bc03f14fSopenharmony_ci    if (pasteboardDataWorker == nullptr) {
111bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "pasteboardDataWorker is null");
112bc03f14fSopenharmony_ci        delete work;
113bc03f14fSopenharmony_ci        work = nullptr;
114bc03f14fSopenharmony_ci        return;
115bc03f14fSopenharmony_ci    }
116bc03f14fSopenharmony_ci    pasteboardDataWorker->observer = shared_from_this();
117bc03f14fSopenharmony_ci
118bc03f14fSopenharmony_ci    work->data = (void *)pasteboardDataWorker;
119bc03f14fSopenharmony_ci
120bc03f14fSopenharmony_ci    int ret = uv_queue_work(
121bc03f14fSopenharmony_ci        loop, work, [](uv_work_t *work) {}, UvQueueWorkOnPasteboardChanged);
122bc03f14fSopenharmony_ci    if (ret != 0) {
123bc03f14fSopenharmony_ci        delete pasteboardDataWorker;
124bc03f14fSopenharmony_ci        pasteboardDataWorker = nullptr;
125bc03f14fSopenharmony_ci        delete work;
126bc03f14fSopenharmony_ci        work = nullptr;
127bc03f14fSopenharmony_ci    }
128bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "OnPasteboardChanged end");
129bc03f14fSopenharmony_ci}
130bc03f14fSopenharmony_ci
131bc03f14fSopenharmony_ciPasteboardDelayGetterInstance::PasteboardDelayGetterInstance(const napi_env &env, const napi_ref &ref)
132bc03f14fSopenharmony_ci    : env_(env), ref_(ref)
133bc03f14fSopenharmony_ci{
134bc03f14fSopenharmony_ci    stub_ = std::make_shared<PasteboardDelayGetterInstance::PasteboardDelayGetterImpl>();
135bc03f14fSopenharmony_ci}
136bc03f14fSopenharmony_ci
137bc03f14fSopenharmony_ciPasteboardDelayGetterInstance::~PasteboardDelayGetterInstance()
138bc03f14fSopenharmony_ci{
139bc03f14fSopenharmony_ci    ref_ = nullptr;
140bc03f14fSopenharmony_ci}
141bc03f14fSopenharmony_ci
142bc03f14fSopenharmony_civoid UvQueueWorkGetDelayPasteData(uv_work_t *work, int status)
143bc03f14fSopenharmony_ci{
144bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "UvQueueWorkGetDelayPasteData start");
145bc03f14fSopenharmony_ci    if (UV_ECANCELED == status || work == nullptr || work->data == nullptr) {
146bc03f14fSopenharmony_ci        return;
147bc03f14fSopenharmony_ci    }
148bc03f14fSopenharmony_ci    PasteboardDelayWorker *pasteboardDelayWorker = (PasteboardDelayWorker *)work->data;
149bc03f14fSopenharmony_ci    if (pasteboardDelayWorker == nullptr || pasteboardDelayWorker->delayGetter == nullptr) {
150bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "pasteboardDataWorker or delayGetter is null");
151bc03f14fSopenharmony_ci        delete work;
152bc03f14fSopenharmony_ci        work = nullptr;
153bc03f14fSopenharmony_ci        return;
154bc03f14fSopenharmony_ci    }
155bc03f14fSopenharmony_ci    auto env = pasteboardDelayWorker->delayGetter->GetEnv();
156bc03f14fSopenharmony_ci    auto ref = pasteboardDelayWorker->delayGetter->GetRef();
157bc03f14fSopenharmony_ci    napi_handle_scope scope = nullptr;
158bc03f14fSopenharmony_ci    napi_open_handle_scope(env, &scope);
159bc03f14fSopenharmony_ci    if (scope == nullptr) {
160bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "scope is null");
161bc03f14fSopenharmony_ci        return;
162bc03f14fSopenharmony_ci    }
163bc03f14fSopenharmony_ci    napi_value undefined = nullptr;
164bc03f14fSopenharmony_ci    napi_get_undefined(env, &undefined);
165bc03f14fSopenharmony_ci    napi_value argv[1] = { CreateNapiString(env, pasteboardDelayWorker->dataType) };
166bc03f14fSopenharmony_ci    napi_value callback = nullptr;
167bc03f14fSopenharmony_ci    napi_value resultOut = nullptr;
168bc03f14fSopenharmony_ci    napi_get_reference_value(env, ref, &callback);
169bc03f14fSopenharmony_ci    {
170bc03f14fSopenharmony_ci        std::unique_lock<std::mutex> lock(pasteboardDelayWorker->mutex);
171bc03f14fSopenharmony_ci        auto ret = napi_call_function(env, undefined, callback, 1, argv, &resultOut);
172bc03f14fSopenharmony_ci        if (ret == napi_ok) {
173bc03f14fSopenharmony_ci            PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "get delay data success");
174bc03f14fSopenharmony_ci            UDMF::UnifiedDataNapi *unifiedDataNapi = nullptr;
175bc03f14fSopenharmony_ci            napi_unwrap(env, resultOut, reinterpret_cast<void **>(&unifiedDataNapi));
176bc03f14fSopenharmony_ci            if (unifiedDataNapi != nullptr) {
177bc03f14fSopenharmony_ci                pasteboardDelayWorker->unifiedData = unifiedDataNapi->value_;
178bc03f14fSopenharmony_ci            }
179bc03f14fSopenharmony_ci        }
180bc03f14fSopenharmony_ci        napi_close_handle_scope(env, scope);
181bc03f14fSopenharmony_ci        pasteboardDelayWorker->complete = true;
182bc03f14fSopenharmony_ci        if (!pasteboardDelayWorker->clean) {
183bc03f14fSopenharmony_ci            pasteboardDelayWorker->cv.notify_all();
184bc03f14fSopenharmony_ci            return;
185bc03f14fSopenharmony_ci        }
186bc03f14fSopenharmony_ci    }
187bc03f14fSopenharmony_ci    delete pasteboardDelayWorker;
188bc03f14fSopenharmony_ci    pasteboardDelayWorker = nullptr;
189bc03f14fSopenharmony_ci    delete work;
190bc03f14fSopenharmony_ci    work = nullptr;
191bc03f14fSopenharmony_ci}
192bc03f14fSopenharmony_ci
193bc03f14fSopenharmony_civoid PasteboardDelayGetterInstance::GetUnifiedData(const std::string &type, UDMF::UnifiedData &data)
194bc03f14fSopenharmony_ci{
195bc03f14fSopenharmony_ci    uv_loop_s *loop = nullptr;
196bc03f14fSopenharmony_ci    napi_get_uv_event_loop(env_, &loop);
197bc03f14fSopenharmony_ci    if (loop == nullptr) {
198bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "loop instance is nullptr");
199bc03f14fSopenharmony_ci        return;
200bc03f14fSopenharmony_ci    }
201bc03f14fSopenharmony_ci    uv_work_t *work = new (std::nothrow) uv_work_t;
202bc03f14fSopenharmony_ci    if (work == nullptr) {
203bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "work is null");
204bc03f14fSopenharmony_ci        return;
205bc03f14fSopenharmony_ci    }
206bc03f14fSopenharmony_ci    PasteboardDelayWorker *pasteboardDelayWorker = new (std::nothrow) PasteboardDelayWorker();
207bc03f14fSopenharmony_ci    if (pasteboardDelayWorker == nullptr) {
208bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "pasteboardDataWorker is null");
209bc03f14fSopenharmony_ci        delete work;
210bc03f14fSopenharmony_ci        work = nullptr;
211bc03f14fSopenharmony_ci        return;
212bc03f14fSopenharmony_ci    }
213bc03f14fSopenharmony_ci    pasteboardDelayWorker->delayGetter = shared_from_this();
214bc03f14fSopenharmony_ci    pasteboardDelayWorker->dataType = type;
215bc03f14fSopenharmony_ci    work->data = (void *)pasteboardDelayWorker;
216bc03f14fSopenharmony_ci    bool noNeedClean = false;
217bc03f14fSopenharmony_ci    {
218bc03f14fSopenharmony_ci        std::unique_lock<std::mutex> lock(pasteboardDelayWorker->mutex);
219bc03f14fSopenharmony_ci        int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkGetDelayPasteData);
220bc03f14fSopenharmony_ci        if (ret != 0) {
221bc03f14fSopenharmony_ci            delete pasteboardDelayWorker;
222bc03f14fSopenharmony_ci            pasteboardDelayWorker = nullptr;
223bc03f14fSopenharmony_ci            delete work;
224bc03f14fSopenharmony_ci            work = nullptr;
225bc03f14fSopenharmony_ci            return;
226bc03f14fSopenharmony_ci        }
227bc03f14fSopenharmony_ci        if (pasteboardDelayWorker->cv.wait_for(lock, std::chrono::seconds(DELAY_TIMEOUT),
228bc03f14fSopenharmony_ci            [pasteboardDelayWorker] { return pasteboardDelayWorker->complete; }) &&
229bc03f14fSopenharmony_ci            pasteboardDelayWorker->unifiedData != nullptr) {
230bc03f14fSopenharmony_ci            data = *(pasteboardDelayWorker->unifiedData);
231bc03f14fSopenharmony_ci        }
232bc03f14fSopenharmony_ci        if (!pasteboardDelayWorker->complete && uv_cancel((uv_req_t*)work) != 0) {
233bc03f14fSopenharmony_ci            pasteboardDelayWorker->clean = true;
234bc03f14fSopenharmony_ci            noNeedClean = true;
235bc03f14fSopenharmony_ci        }
236bc03f14fSopenharmony_ci    }
237bc03f14fSopenharmony_ci    if (!noNeedClean) {
238bc03f14fSopenharmony_ci        delete pasteboardDelayWorker;
239bc03f14fSopenharmony_ci        pasteboardDelayWorker = nullptr;
240bc03f14fSopenharmony_ci        delete work;
241bc03f14fSopenharmony_ci        work = nullptr;
242bc03f14fSopenharmony_ci    }
243bc03f14fSopenharmony_ci}
244bc03f14fSopenharmony_ci
245bc03f14fSopenharmony_cibool SystemPasteboardNapi::CheckAgrsOfOnAndOff(napi_env env, bool checkArgsCount, napi_value *argv, size_t argc)
246bc03f14fSopenharmony_ci{
247bc03f14fSopenharmony_ci    if (!CheckExpression(
248bc03f14fSopenharmony_ci        env, checkArgsCount, JSErrorCode::INVALID_PARAMETERS, "Parameter error. The number of arguments is wrong.") ||
249bc03f14fSopenharmony_ci        !CheckArgsType(env, argv[0], napi_string, "Parameter error. The type of mimeType must be string.")) {
250bc03f14fSopenharmony_ci        return false;
251bc03f14fSopenharmony_ci    }
252bc03f14fSopenharmony_ci    std::string mimeType;
253bc03f14fSopenharmony_ci    bool ret = GetValue(env, argv[0], mimeType);
254bc03f14fSopenharmony_ci    if (!ret) {
255bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "GetValue failed");
256bc03f14fSopenharmony_ci        return false;
257bc03f14fSopenharmony_ci    }
258bc03f14fSopenharmony_ci    if (!CheckExpression(env, mimeType == STRING_UPDATE, JSErrorCode::INVALID_PARAMETERS,
259bc03f14fSopenharmony_ci        "Parameter error. The value of type must be update")) {
260bc03f14fSopenharmony_ci        return false;
261bc03f14fSopenharmony_ci    }
262bc03f14fSopenharmony_ci    return true;
263bc03f14fSopenharmony_ci}
264bc03f14fSopenharmony_ci
265bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::On(napi_env env, napi_callback_info info)
266bc03f14fSopenharmony_ci{
267bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi on() is called!");
268bc03f14fSopenharmony_ci    size_t argc = MAX_ARGS;
269bc03f14fSopenharmony_ci    napi_value argv[MAX_ARGS] = { 0 };
270bc03f14fSopenharmony_ci    napi_value thisVar = 0;
271bc03f14fSopenharmony_ci    void *data = nullptr;
272bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
273bc03f14fSopenharmony_ci    // on(type: 'update', callback: () => void) has 2 args
274bc03f14fSopenharmony_ci    if (!CheckAgrsOfOnAndOff(env, argc >= 2, argv, argc) ||
275bc03f14fSopenharmony_ci        !CheckArgsType(env, argv[1], napi_function, "Parameter error. The type of callback must be function.")) {
276bc03f14fSopenharmony_ci        return nullptr;
277bc03f14fSopenharmony_ci    }
278bc03f14fSopenharmony_ci
279bc03f14fSopenharmony_ci    napi_value result = nullptr;
280bc03f14fSopenharmony_ci    napi_get_undefined(env, &result);
281bc03f14fSopenharmony_ci    auto observer = GetObserver(env, argv[1]);
282bc03f14fSopenharmony_ci    if (observer != nullptr) {
283bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "observer exist.");
284bc03f14fSopenharmony_ci        return result;
285bc03f14fSopenharmony_ci    }
286bc03f14fSopenharmony_ci    napi_ref ref = nullptr;
287bc03f14fSopenharmony_ci    napi_create_reference(env, argv[1], 1, &ref);
288bc03f14fSopenharmony_ci    observer = std::make_shared<PasteboardObserverInstance>(env, ref);
289bc03f14fSopenharmony_ci    observer->GetStub()->SetObserverWrapper(observer);
290bc03f14fSopenharmony_ci    PasteboardClient::GetInstance()->Subscribe(PasteboardObserverType::OBSERVER_LOCAL, observer->GetStub());
291bc03f14fSopenharmony_ci    observers_[ref] = observer;
292bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi on() is end!");
293bc03f14fSopenharmony_ci    return result;
294bc03f14fSopenharmony_ci}
295bc03f14fSopenharmony_ci
296bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::Off(napi_env env, napi_callback_info info)
297bc03f14fSopenharmony_ci{
298bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi off () is called!");
299bc03f14fSopenharmony_ci    size_t argc = MAX_ARGS;
300bc03f14fSopenharmony_ci    napi_value argv[MAX_ARGS] = { 0 };
301bc03f14fSopenharmony_ci    napi_value thisVar = 0;
302bc03f14fSopenharmony_ci    void *data = nullptr;
303bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
304bc03f14fSopenharmony_ci    // off(type: 'update', callback?: () => void) has at least 1 arg
305bc03f14fSopenharmony_ci    if (!CheckAgrsOfOnAndOff(env, argc >= 1, argv, argc)) {
306bc03f14fSopenharmony_ci        return nullptr;
307bc03f14fSopenharmony_ci    }
308bc03f14fSopenharmony_ci
309bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardObserverInstance> observer = nullptr;
310bc03f14fSopenharmony_ci    // 1: is the observer parameter
311bc03f14fSopenharmony_ci    if (argc > 1) {
312bc03f14fSopenharmony_ci        if (!CheckArgsType(env, argv[1], napi_function, "Parameter error. The type of callback must be function.")) {
313bc03f14fSopenharmony_ci            return nullptr;
314bc03f14fSopenharmony_ci        }
315bc03f14fSopenharmony_ci        observer = GetObserver(env, argv[1]);
316bc03f14fSopenharmony_ci    }
317bc03f14fSopenharmony_ci
318bc03f14fSopenharmony_ci    DeleteObserver(observer);
319bc03f14fSopenharmony_ci    napi_value result = nullptr;
320bc03f14fSopenharmony_ci    napi_get_undefined(env, &result);
321bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi off () is called!");
322bc03f14fSopenharmony_ci    return result;
323bc03f14fSopenharmony_ci}
324bc03f14fSopenharmony_ci
325bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::Clear(napi_env env, napi_callback_info info)
326bc03f14fSopenharmony_ci{
327bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "Clear is called!");
328bc03f14fSopenharmony_ci    auto context = std::make_shared<AsyncCall::Context>();
329bc03f14fSopenharmony_ci    auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
330bc03f14fSopenharmony_ci        // clear has 0 or 1 args
331bc03f14fSopenharmony_ci        if (argc > 0 &&
332bc03f14fSopenharmony_ci            !CheckArgsType(env, argv[0], napi_function, "Parameter error. The type of callback must be function.")) {
333bc03f14fSopenharmony_ci            return napi_invalid_arg;
334bc03f14fSopenharmony_ci        }
335bc03f14fSopenharmony_ci        return napi_ok;
336bc03f14fSopenharmony_ci    };
337bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
338bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec Clear");
339bc03f14fSopenharmony_ci        PasteboardClient::GetInstance()->Clear();
340bc03f14fSopenharmony_ci    };
341bc03f14fSopenharmony_ci    context->SetAction(std::move(input));
342bc03f14fSopenharmony_ci    // 0: the AsyncCall at the first position;
343bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 0);
344bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
345bc03f14fSopenharmony_ci}
346bc03f14fSopenharmony_ci
347bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::ClearData(napi_env env, napi_callback_info info)
348bc03f14fSopenharmony_ci{
349bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "ClearData is called!");
350bc03f14fSopenharmony_ci    return Clear(env, info);
351bc03f14fSopenharmony_ci}
352bc03f14fSopenharmony_ci
353bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::HasPasteData(napi_env env, napi_callback_info info)
354bc03f14fSopenharmony_ci{
355bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "HasPasteData is called!");
356bc03f14fSopenharmony_ci    auto context = std::make_shared<HasContextInfo>();
357bc03f14fSopenharmony_ci    auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
358bc03f14fSopenharmony_ci        // hasPasteData has 0 or 1 args
359bc03f14fSopenharmony_ci        if (argc > 0 &&
360bc03f14fSopenharmony_ci            !CheckArgsType(env, argv[0], napi_function, "Parameter error. The type of callback must be function.")) {
361bc03f14fSopenharmony_ci            return napi_invalid_arg;
362bc03f14fSopenharmony_ci        }
363bc03f14fSopenharmony_ci        return napi_ok;
364bc03f14fSopenharmony_ci    };
365bc03f14fSopenharmony_ci    auto output = [context](napi_env env, napi_value *result) -> napi_status {
366bc03f14fSopenharmony_ci        napi_status status = napi_get_boolean(env, context->hasPasteData, result);
367bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_get_boolean status = %{public}d", status);
368bc03f14fSopenharmony_ci        return status;
369bc03f14fSopenharmony_ci    };
370bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
371bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec HasPasteData");
372bc03f14fSopenharmony_ci        context->hasPasteData = PasteboardClient::GetInstance()->HasPasteData();
373bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "HasPasteData result = %{public}d", context->hasPasteData);
374bc03f14fSopenharmony_ci        context->status = napi_ok;
375bc03f14fSopenharmony_ci    };
376bc03f14fSopenharmony_ci    context->SetAction(std::move(input), std::move(output));
377bc03f14fSopenharmony_ci    // 0: the AsyncCall at the first position;
378bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 0);
379bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
380bc03f14fSopenharmony_ci}
381bc03f14fSopenharmony_ci
382bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::HasData(napi_env env, napi_callback_info info)
383bc03f14fSopenharmony_ci{
384bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "HasData is called!");
385bc03f14fSopenharmony_ci    return HasPasteData(env, info);
386bc03f14fSopenharmony_ci}
387bc03f14fSopenharmony_ci
388bc03f14fSopenharmony_civoid SystemPasteboardNapi::GetDataCommon(std::shared_ptr<GetContextInfo> &context)
389bc03f14fSopenharmony_ci{
390bc03f14fSopenharmony_ci    auto input = [](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
391bc03f14fSopenharmony_ci        // 1: GetPasteData has 0 or 1 args
392bc03f14fSopenharmony_ci        if (argc > 0 &&
393bc03f14fSopenharmony_ci            !CheckArgsType(env, argv[0], napi_function, "Parameter error. The type of callback must be function.")) {
394bc03f14fSopenharmony_ci            return napi_invalid_arg;
395bc03f14fSopenharmony_ci        }
396bc03f14fSopenharmony_ci        return napi_ok;
397bc03f14fSopenharmony_ci    };
398bc03f14fSopenharmony_ci
399bc03f14fSopenharmony_ci    auto output = [context](napi_env env, napi_value *result) -> napi_status {
400bc03f14fSopenharmony_ci        napi_value instance = nullptr;
401bc03f14fSopenharmony_ci        PasteDataNapi::NewInstance(env, instance);
402bc03f14fSopenharmony_ci        PasteDataNapi *obj = nullptr;
403bc03f14fSopenharmony_ci        napi_status ret = napi_unwrap(env, instance, reinterpret_cast<void **>(&obj));
404bc03f14fSopenharmony_ci        if ((ret == napi_ok) || (obj != nullptr)) {
405bc03f14fSopenharmony_ci            obj->value_ = context->pasteData;
406bc03f14fSopenharmony_ci        } else {
407bc03f14fSopenharmony_ci            return napi_generic_failure;
408bc03f14fSopenharmony_ci        }
409bc03f14fSopenharmony_ci        *result = instance;
410bc03f14fSopenharmony_ci        return napi_ok;
411bc03f14fSopenharmony_ci    };
412bc03f14fSopenharmony_ci    context->SetAction(std::move(input), std::move(output));
413bc03f14fSopenharmony_ci}
414bc03f14fSopenharmony_ci
415bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetPasteData(napi_env env, napi_callback_info info)
416bc03f14fSopenharmony_ci{
417bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPasteData is called!");
418bc03f14fSopenharmony_ci
419bc03f14fSopenharmony_ci    auto context = std::make_shared<GetContextInfo>();
420bc03f14fSopenharmony_ci    context->pasteData = std::make_shared<PasteData>();
421bc03f14fSopenharmony_ci    GetDataCommon(context);
422bc03f14fSopenharmony_ci
423bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
424bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPasteData Begin");
425bc03f14fSopenharmony_ci        PasteboardClient::GetInstance()->GetPasteData(*context->pasteData);
426bc03f14fSopenharmony_ci        context->status = napi_ok;
427bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPasteData End");
428bc03f14fSopenharmony_ci    };
429bc03f14fSopenharmony_ci
430bc03f14fSopenharmony_ci    // 0: the AsyncCall at the first position;
431bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 0);
432bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
433bc03f14fSopenharmony_ci}
434bc03f14fSopenharmony_ci
435bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetData(napi_env env, napi_callback_info info)
436bc03f14fSopenharmony_ci{
437bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetData is called!");
438bc03f14fSopenharmony_ci
439bc03f14fSopenharmony_ci    auto context = std::make_shared<GetContextInfo>();
440bc03f14fSopenharmony_ci    context->pasteData = std::make_shared<PasteData>();
441bc03f14fSopenharmony_ci    GetDataCommon(context);
442bc03f14fSopenharmony_ci
443bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
444bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetData Begin");
445bc03f14fSopenharmony_ci        int32_t ret = PasteboardClient::GetInstance()->GetPasteData(*context->pasteData);
446bc03f14fSopenharmony_ci        if (ret == static_cast<int32_t>(PasteboardError::TASK_PROCESSING)) {
447bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "Another getData is being processed");
448bc03f14fSopenharmony_ci        } else {
449bc03f14fSopenharmony_ci            context->status = napi_ok;
450bc03f14fSopenharmony_ci        }
451bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetData End");
452bc03f14fSopenharmony_ci    };
453bc03f14fSopenharmony_ci    // 0: the AsyncCall at the first position;
454bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 0);
455bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
456bc03f14fSopenharmony_ci}
457bc03f14fSopenharmony_ci
458bc03f14fSopenharmony_civoid SystemPasteboardNapi::SetDataCommon(std::shared_ptr<SetContextInfo> &context)
459bc03f14fSopenharmony_ci{
460bc03f14fSopenharmony_ci    auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
461bc03f14fSopenharmony_ci        // setData has 1 or 2 args
462bc03f14fSopenharmony_ci        if (!CheckExpression(
463bc03f14fSopenharmony_ci            env, argc > 0, JSErrorCode::INVALID_PARAMETERS,
464bc03f14fSopenharmony_ci            "Parameter error. The number of arguments must be greater than zero.") ||
465bc03f14fSopenharmony_ci            !CheckExpression(env, PasteDataNapi::IsPasteData(env, argv[0]), JSErrorCode::INVALID_PARAMETERS,
466bc03f14fSopenharmony_ci            "Parameter error. The Type of data must be pasteData.")) {
467bc03f14fSopenharmony_ci            return napi_invalid_arg;
468bc03f14fSopenharmony_ci        }
469bc03f14fSopenharmony_ci        if (argc > 1 &&
470bc03f14fSopenharmony_ci            !CheckArgsType(env, argv[1], napi_function, "Parameter error. The type of callback must be function.")) {
471bc03f14fSopenharmony_ci            return napi_invalid_arg;
472bc03f14fSopenharmony_ci        }
473bc03f14fSopenharmony_ci        PasteDataNapi *pasteData = nullptr;
474bc03f14fSopenharmony_ci        napi_unwrap(env, argv[0], reinterpret_cast<void **>(&pasteData));
475bc03f14fSopenharmony_ci        if (pasteData != nullptr) {
476bc03f14fSopenharmony_ci            context->obj = pasteData->value_;
477bc03f14fSopenharmony_ci        }
478bc03f14fSopenharmony_ci        return napi_ok;
479bc03f14fSopenharmony_ci    };
480bc03f14fSopenharmony_ci    context->SetAction(std::move(input));
481bc03f14fSopenharmony_ci}
482bc03f14fSopenharmony_ci
483bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetPasteData(napi_env env, napi_callback_info info)
484bc03f14fSopenharmony_ci{
485bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SetPasteData is called!");
486bc03f14fSopenharmony_ci    auto context = std::make_shared<SetContextInfo>();
487bc03f14fSopenharmony_ci    SetDataCommon(context);
488bc03f14fSopenharmony_ci
489bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
490bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec SetPasteData");
491bc03f14fSopenharmony_ci        if (context->obj != nullptr) {
492bc03f14fSopenharmony_ci            PasteboardClient::GetInstance()->SetPasteData(*(context->obj));
493bc03f14fSopenharmony_ci            context->obj = nullptr;
494bc03f14fSopenharmony_ci        }
495bc03f14fSopenharmony_ci        context->status = napi_ok;
496bc03f14fSopenharmony_ci    };
497bc03f14fSopenharmony_ci    // 1: the AsyncCall at the second position
498bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 1);
499bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
500bc03f14fSopenharmony_ci}
501bc03f14fSopenharmony_ci
502bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetData(napi_env env, napi_callback_info info)
503bc03f14fSopenharmony_ci{
504bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SetData is called!");
505bc03f14fSopenharmony_ci    auto context = std::make_shared<SetContextInfo>();
506bc03f14fSopenharmony_ci    SetDataCommon(context);
507bc03f14fSopenharmony_ci
508bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
509bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec SetPasteData");
510bc03f14fSopenharmony_ci        int32_t ret = static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
511bc03f14fSopenharmony_ci        if (context->obj != nullptr) {
512bc03f14fSopenharmony_ci            std::map<uint32_t, std::shared_ptr<UDMF::EntryGetter>> entryGetters;
513bc03f14fSopenharmony_ci            for (auto record : context->obj->AllRecords()) {
514bc03f14fSopenharmony_ci                if (record != nullptr && record->GetEntryGetter() != nullptr) {
515bc03f14fSopenharmony_ci                    entryGetters.emplace(record->GetRecordId(), record->GetEntryGetter());
516bc03f14fSopenharmony_ci                }
517bc03f14fSopenharmony_ci            }
518bc03f14fSopenharmony_ci            ret = PasteboardClient::GetInstance()->SetPasteData(*(context->obj), nullptr, entryGetters);
519bc03f14fSopenharmony_ci            context->obj = nullptr;
520bc03f14fSopenharmony_ci        }
521bc03f14fSopenharmony_ci        if (ret == static_cast<int>(PasteboardError::E_OK)) {
522bc03f14fSopenharmony_ci            context->status = napi_ok;
523bc03f14fSopenharmony_ci        } else if (ret == static_cast<int>(PasteboardError::PROHIBIT_COPY)) {
524bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "The system prohibits copying");
525bc03f14fSopenharmony_ci        } else if (ret == static_cast<int>(PasteboardError::TASK_PROCESSING)) {
526bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "Another setData is being processed");
527bc03f14fSopenharmony_ci        }
528bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec context->status[%{public}d]", context->status);
529bc03f14fSopenharmony_ci    };
530bc03f14fSopenharmony_ci    // 1: the AsyncCall at the second position
531bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 1);
532bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
533bc03f14fSopenharmony_ci}
534bc03f14fSopenharmony_ci
535bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetUnifiedData(napi_env env, napi_callback_info info)
536bc03f14fSopenharmony_ci{
537bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SetUnifiedData is called!");
538bc03f14fSopenharmony_ci    auto context = std::make_shared<SetUnifiedContextInfo>();
539bc03f14fSopenharmony_ci    SetDataCommon(context);
540bc03f14fSopenharmony_ci
541bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context* ctx) {
542bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec SetPasteData");
543bc03f14fSopenharmony_ci        int32_t ret = static_cast<int32_t>(PasteboardError::INVALID_DATA_ERROR);
544bc03f14fSopenharmony_ci        if (context->obj != nullptr) {
545bc03f14fSopenharmony_ci            if (context->isDelay && context->delayGetter != nullptr) {
546bc03f14fSopenharmony_ci                ret = PasteboardClient::GetInstance()->SetUnifiedData(*(context->obj), context->delayGetter->GetStub());
547bc03f14fSopenharmony_ci            } else {
548bc03f14fSopenharmony_ci                ret = PasteboardClient::GetInstance()->SetUnifiedData(*(context->obj));
549bc03f14fSopenharmony_ci            }
550bc03f14fSopenharmony_ci            context->obj = nullptr;
551bc03f14fSopenharmony_ci        }
552bc03f14fSopenharmony_ci        if (ret == static_cast<int>(PasteboardError::E_OK)) {
553bc03f14fSopenharmony_ci            context->status = napi_ok;
554bc03f14fSopenharmony_ci        } else if (ret == static_cast<int>(PasteboardError::PROHIBIT_COPY)) {
555bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "The system prohibits copying");
556bc03f14fSopenharmony_ci        } else if (ret == static_cast<int>(PasteboardError::TASK_PROCESSING)) {
557bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "Another setData is being processed");
558bc03f14fSopenharmony_ci        }
559bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "exec context->status[%{public}d]", context->status);
560bc03f14fSopenharmony_ci    };
561bc03f14fSopenharmony_ci    // 1: the AsyncCall at the second position
562bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 1);
563bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
564bc03f14fSopenharmony_ci}
565bc03f14fSopenharmony_ci
566bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetUnifiedData(napi_env env, napi_callback_info info)
567bc03f14fSopenharmony_ci{
568bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetUnifiedData is called!");
569bc03f14fSopenharmony_ci
570bc03f14fSopenharmony_ci    auto context = std::make_shared<GetUnifiedContextInfo>();
571bc03f14fSopenharmony_ci    context->unifiedData = std::make_shared<UDMF::UnifiedData>();
572bc03f14fSopenharmony_ci    GetDataCommon(context);
573bc03f14fSopenharmony_ci
574bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context* ctx) {
575bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetUnifiedData Begin");
576bc03f14fSopenharmony_ci        int32_t ret = PasteboardClient::GetInstance()->GetUnifiedData(*context->unifiedData);
577bc03f14fSopenharmony_ci        if (ret == static_cast<int32_t>(PasteboardError::TASK_PROCESSING)) {
578bc03f14fSopenharmony_ci            context->SetErrInfo(ret, "Another getData is being processed");
579bc03f14fSopenharmony_ci        } else {
580bc03f14fSopenharmony_ci            context->status = napi_ok;
581bc03f14fSopenharmony_ci        }
582bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetUnifiedData End");
583bc03f14fSopenharmony_ci    };
584bc03f14fSopenharmony_ci    // 0: the AsyncCall at the first position;
585bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 0);
586bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
587bc03f14fSopenharmony_ci}
588bc03f14fSopenharmony_ci
589bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetUnifiedDataSync(napi_env env, napi_callback_info info)
590bc03f14fSopenharmony_ci{
591bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi GetUnifiedDataSync is called!");
592bc03f14fSopenharmony_ci    napi_value instance = nullptr;
593bc03f14fSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> unifiedData = std::make_shared<UDMF::UnifiedData>();
594bc03f14fSopenharmony_ci
595bc03f14fSopenharmony_ci    NAPI_CALL(env, UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, instance));
596bc03f14fSopenharmony_ci    UDMF::UnifiedDataNapi* obj = nullptr;
597bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, instance, reinterpret_cast<void**>(&obj));
598bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr) || obj->value_ == nullptr) {
599bc03f14fSopenharmony_ci        return nullptr;
600bc03f14fSopenharmony_ci    }
601bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int32_t>>>(SYNC_TIMEOUT);
602bc03f14fSopenharmony_ci    std::thread thread([block, unifiedData = obj->value_]() mutable {
603bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->GetUnifiedData(*unifiedData);
604bc03f14fSopenharmony_ci        std::shared_ptr<int32_t> value = std::make_shared<int32_t>(ret);
605bc03f14fSopenharmony_ci        block->SetValue(value);
606bc03f14fSopenharmony_ci    });
607bc03f14fSopenharmony_ci    thread.detach();
608bc03f14fSopenharmony_ci    auto value = block->GetValue();
609bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "request timed out.")) {
610bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, GetUnifiedDataSync failed.");
611bc03f14fSopenharmony_ci    }
612bc03f14fSopenharmony_ci    return instance;
613bc03f14fSopenharmony_ci}
614bc03f14fSopenharmony_ci
615bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetUnifiedDataSync(napi_env env, napi_callback_info info)
616bc03f14fSopenharmony_ci{
617bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi SetUnifiedDataSync is called!");
618bc03f14fSopenharmony_ci    size_t argc = 1;
619bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
620bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
621bc03f14fSopenharmony_ci
622bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
623bc03f14fSopenharmony_ci    if (!CheckExpression(
624bc03f14fSopenharmony_ci        env, argc > 0, JSErrorCode::INVALID_PARAMETERS, "Parameter error. Wrong number of arguments.")) {
625bc03f14fSopenharmony_ci        return nullptr;
626bc03f14fSopenharmony_ci    }
627bc03f14fSopenharmony_ci    UDMF::UnifiedDataNapi* unifiedDataNapi = nullptr;
628bc03f14fSopenharmony_ci    napi_unwrap(env, argv[0], reinterpret_cast<void**>(&unifiedDataNapi));
629bc03f14fSopenharmony_ci    if (!CheckExpression(env, (unifiedDataNapi != nullptr && unifiedDataNapi->value_ != nullptr),
630bc03f14fSopenharmony_ci        JSErrorCode::INVALID_PARAMETERS, "Parameter error. The Type of data must be unifiedData.")) {
631bc03f14fSopenharmony_ci        return nullptr;
632bc03f14fSopenharmony_ci    }
633bc03f14fSopenharmony_ci    auto properties = unifiedDataNapi->GetPropertiesNapi(env);
634bc03f14fSopenharmony_ci    bool isDelay = false;
635bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterInstance> delayGetter = nullptr;
636bc03f14fSopenharmony_ci    if (properties != nullptr && properties->delayDataRef_ != nullptr) {
637bc03f14fSopenharmony_ci        delayGetter = std::make_shared<PasteboardDelayGetterInstance>(env, properties->delayDataRef_);
638bc03f14fSopenharmony_ci        delayGetter->GetStub()->SetDelayGetterWrapper(delayGetter);
639bc03f14fSopenharmony_ci        isDelay = true;
640bc03f14fSopenharmony_ci    }
641bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
642bc03f14fSopenharmony_ci    std::shared_ptr<UDMF::UnifiedData> unifiedData = unifiedDataNapi->value_;
643bc03f14fSopenharmony_ci    std::thread thread([block, unifiedData, isDelay, delayGetter]() mutable {
644bc03f14fSopenharmony_ci        int32_t ret = isDelay ?
645bc03f14fSopenharmony_ci            PasteboardClient::GetInstance()->SetUnifiedData(*unifiedData, delayGetter->GetStub()) :
646bc03f14fSopenharmony_ci            PasteboardClient::GetInstance()->SetUnifiedData(*unifiedData);
647bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(static_cast<int>(ret));
648bc03f14fSopenharmony_ci        block->SetValue(value);
649bc03f14fSopenharmony_ci    });
650bc03f14fSopenharmony_ci    thread.detach();
651bc03f14fSopenharmony_ci    auto value = block->GetValue();
652bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "request timed out.")) {
653bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, SetUnifiedDataSync failed.");
654bc03f14fSopenharmony_ci        return nullptr;
655bc03f14fSopenharmony_ci    }
656bc03f14fSopenharmony_ci    if (*value != static_cast<int32_t>(PasteboardError::E_OK)) {
657bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "operate invalid, SetUnifiedDataSync failed");
658bc03f14fSopenharmony_ci        return nullptr;
659bc03f14fSopenharmony_ci    }
660bc03f14fSopenharmony_ci    {
661bc03f14fSopenharmony_ci        std::lock_guard<std::mutex> lck(delayMutex_);
662bc03f14fSopenharmony_ci        delayGetter_ = delayGetter;
663bc03f14fSopenharmony_ci    }
664bc03f14fSopenharmony_ci    return nullptr;
665bc03f14fSopenharmony_ci}
666bc03f14fSopenharmony_ci
667bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetAppShareOptions(napi_env env, napi_callback_info info)
668bc03f14fSopenharmony_ci{
669bc03f14fSopenharmony_ci    size_t argc = 1;
670bc03f14fSopenharmony_ci    napi_value argv[1] = {0};
671bc03f14fSopenharmony_ci    napi_value thisArg = nullptr;
672bc03f14fSopenharmony_ci    void *data = nullptr;
673bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
674bc03f14fSopenharmony_ci    if (!CheckExpression(env, argc > 0, JSErrorCode::INVALID_PARAMETERS,
675bc03f14fSopenharmony_ci        "Parameter error. Mandatory parameters are left unspecified.")) {
676bc03f14fSopenharmony_ci        return nullptr;
677bc03f14fSopenharmony_ci    }
678bc03f14fSopenharmony_ci    int32_t shareOptions;
679bc03f14fSopenharmony_ci    auto status = napi_get_value_int32(env, argv[0], &shareOptions);
680bc03f14fSopenharmony_ci    if (!CheckExpression(env, status == napi_ok, JSErrorCode::INVALID_PARAMETERS,
681bc03f14fSopenharmony_ci        "Parameter error. Incorrect parameter types.")) {
682bc03f14fSopenharmony_ci        return nullptr;
683bc03f14fSopenharmony_ci    }
684bc03f14fSopenharmony_ci    if (!CheckExpression(env, shareOptions >= ShareOption::InApp && shareOptions <= ShareOption::CrossDevice,
685bc03f14fSopenharmony_ci        JSErrorCode::INVALID_PARAMETERS, "Parameter error. Parameter verification failed.")) {
686bc03f14fSopenharmony_ci        return nullptr;
687bc03f14fSopenharmony_ci    }
688bc03f14fSopenharmony_ci    auto result = PasteboardClient::GetInstance()->SetAppShareOptions(static_cast<ShareOption>(shareOptions));
689bc03f14fSopenharmony_ci    if (!CheckExpression(env, result != static_cast<int32_t>(PasteboardError::PERMISSION_VERIFICATION_ERROR),
690bc03f14fSopenharmony_ci        JSErrorCode::NO_SYSTEM_PERMISSION,
691bc03f14fSopenharmony_ci        "Permission verification failed. A non-system application calls a system API.")) {
692bc03f14fSopenharmony_ci        return nullptr;
693bc03f14fSopenharmony_ci    }
694bc03f14fSopenharmony_ci    if (!CheckExpression(env, result != static_cast<int32_t>(PasteboardError::INVALID_OPERATION_ERROR),
695bc03f14fSopenharmony_ci        JSErrorCode::SETTINGS_ALREADY_EXIST, "Settings already exist.")) {
696bc03f14fSopenharmony_ci        return nullptr;
697bc03f14fSopenharmony_ci    }
698bc03f14fSopenharmony_ci    return nullptr;
699bc03f14fSopenharmony_ci}
700bc03f14fSopenharmony_ci
701bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::RemoveAppShareOptions(napi_env env, napi_callback_info info)
702bc03f14fSopenharmony_ci{
703bc03f14fSopenharmony_ci    auto result = PasteboardClient::GetInstance()->RemoveAppShareOptions();
704bc03f14fSopenharmony_ci    if (CheckExpression(env, result != static_cast<int32_t>(PasteboardError::PERMISSION_VERIFICATION_ERROR),
705bc03f14fSopenharmony_ci        JSErrorCode::NO_SYSTEM_PERMISSION,
706bc03f14fSopenharmony_ci        "Permission verification failed. A non-system application calls a system API.")) {
707bc03f14fSopenharmony_ci        return nullptr;
708bc03f14fSopenharmony_ci    }
709bc03f14fSopenharmony_ci    return nullptr;
710bc03f14fSopenharmony_ci}
711bc03f14fSopenharmony_ci
712bc03f14fSopenharmony_civoid SystemPasteboardNapi::SetDataCommon(std::shared_ptr<SetUnifiedContextInfo>& context)
713bc03f14fSopenharmony_ci{
714bc03f14fSopenharmony_ci    auto input = [context](napi_env env, size_t argc, napi_value* argv, napi_value self) -> napi_status {
715bc03f14fSopenharmony_ci        // setData has 1 arg
716bc03f14fSopenharmony_ci        if (!CheckExpression(
717bc03f14fSopenharmony_ci            env, argc > 0, JSErrorCode::INVALID_PARAMETERS, "Parameter error. Wrong number of arguments.")) {
718bc03f14fSopenharmony_ci            return napi_invalid_arg;
719bc03f14fSopenharmony_ci        }
720bc03f14fSopenharmony_ci        UDMF::UnifiedDataNapi* unifiedDataNapi = nullptr;
721bc03f14fSopenharmony_ci        context->status = napi_unwrap(env, argv[0], reinterpret_cast<void**>(&unifiedDataNapi));
722bc03f14fSopenharmony_ci        if (!CheckExpression(env, unifiedDataNapi != nullptr,
723bc03f14fSopenharmony_ci            JSErrorCode::INVALID_PARAMETERS, "Parameter error. The Type of data must be unifiedData.")) {
724bc03f14fSopenharmony_ci            return napi_invalid_arg;
725bc03f14fSopenharmony_ci        }
726bc03f14fSopenharmony_ci        context->obj = unifiedDataNapi->value_;
727bc03f14fSopenharmony_ci        auto properties = unifiedDataNapi->GetPropertiesNapi(env);
728bc03f14fSopenharmony_ci        if (properties != nullptr && properties->delayDataRef_ != nullptr) {
729bc03f14fSopenharmony_ci            context->delayGetter = std::make_shared<PasteboardDelayGetterInstance>(env, properties->delayDataRef_);
730bc03f14fSopenharmony_ci            context->delayGetter->GetStub()->SetDelayGetterWrapper(context->delayGetter);
731bc03f14fSopenharmony_ci            context->isDelay = true;
732bc03f14fSopenharmony_ci        }
733bc03f14fSopenharmony_ci        return napi_ok;
734bc03f14fSopenharmony_ci    };
735bc03f14fSopenharmony_ci    auto output = [context](napi_env env, napi_value *result) -> napi_status {
736bc03f14fSopenharmony_ci        if (context->status == napi_ok) {
737bc03f14fSopenharmony_ci            std::lock_guard<std::mutex> lck(delayMutex_);
738bc03f14fSopenharmony_ci            delayGetter_ = std::move(context->delayGetter);
739bc03f14fSopenharmony_ci        }
740bc03f14fSopenharmony_ci        return napi_ok;
741bc03f14fSopenharmony_ci    };
742bc03f14fSopenharmony_ci    context->SetAction(std::move(input), std::move(output));
743bc03f14fSopenharmony_ci}
744bc03f14fSopenharmony_ci
745bc03f14fSopenharmony_civoid SystemPasteboardNapi::GetDataCommon(std::shared_ptr<GetUnifiedContextInfo>& context)
746bc03f14fSopenharmony_ci{
747bc03f14fSopenharmony_ci    auto input = [](napi_env env, size_t argc, napi_value* argv, napi_value self) -> napi_status {
748bc03f14fSopenharmony_ci        // 1: GetPasteData has 0 or 1 args
749bc03f14fSopenharmony_ci        if (argc > 0 &&
750bc03f14fSopenharmony_ci            !CheckArgsType(env, argv[0], napi_function, "Parameter error. The type of callback must be function.")) {
751bc03f14fSopenharmony_ci            return napi_invalid_arg;
752bc03f14fSopenharmony_ci        }
753bc03f14fSopenharmony_ci        return napi_ok;
754bc03f14fSopenharmony_ci    };
755bc03f14fSopenharmony_ci
756bc03f14fSopenharmony_ci    auto output = [context](napi_env env, napi_value* result) -> napi_status {
757bc03f14fSopenharmony_ci        napi_value instance = nullptr;
758bc03f14fSopenharmony_ci        std::shared_ptr<UDMF::UnifiedData> unifiedData = std::make_shared<UDMF::UnifiedData>();
759bc03f14fSopenharmony_ci        UDMF::UnifiedDataNapi::NewInstance(env, unifiedData, instance);
760bc03f14fSopenharmony_ci
761bc03f14fSopenharmony_ci        UDMF::UnifiedDataNapi* obj = nullptr;
762bc03f14fSopenharmony_ci        napi_status ret = napi_unwrap(env, instance, reinterpret_cast<void**>(&obj));
763bc03f14fSopenharmony_ci        if ((ret == napi_ok) || (obj != nullptr)) {
764bc03f14fSopenharmony_ci            obj->value_ = context->unifiedData;
765bc03f14fSopenharmony_ci        } else {
766bc03f14fSopenharmony_ci            return napi_generic_failure;
767bc03f14fSopenharmony_ci        }
768bc03f14fSopenharmony_ci        *result = instance;
769bc03f14fSopenharmony_ci        return napi_ok;
770bc03f14fSopenharmony_ci    };
771bc03f14fSopenharmony_ci    context->SetAction(std::move(input), std::move(output));
772bc03f14fSopenharmony_ci}
773bc03f14fSopenharmony_ci
774bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::IsRemoteData(napi_env env, napi_callback_info info)
775bc03f14fSopenharmony_ci{
776bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi IsRemoteData() is called!");
777bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
778bc03f14fSopenharmony_ci    std::thread thread([block]() {
779bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->IsRemoteData();
780bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "value=%{public}d", ret);
781bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(static_cast<int>(ret));
782bc03f14fSopenharmony_ci        block->SetValue(value);
783bc03f14fSopenharmony_ci    });
784bc03f14fSopenharmony_ci    thread.detach();
785bc03f14fSopenharmony_ci    auto value = block->GetValue();
786bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "Request timed out.")) {
787bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, IsRemoteData failed.");
788bc03f14fSopenharmony_ci        return nullptr;
789bc03f14fSopenharmony_ci    }
790bc03f14fSopenharmony_ci    napi_value result = nullptr;
791bc03f14fSopenharmony_ci    napi_get_boolean(env, *value, &result);
792bc03f14fSopenharmony_ci    return result;
793bc03f14fSopenharmony_ci}
794bc03f14fSopenharmony_ci
795bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetDataSource(napi_env env, napi_callback_info info)
796bc03f14fSopenharmony_ci{
797bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi GetDataSource() is called!");
798bc03f14fSopenharmony_ci    std::string bundleName;
799bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
800bc03f14fSopenharmony_ci    std::thread thread([block, &bundleName]() mutable {
801bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->GetDataSource(bundleName);
802bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(ret);
803bc03f14fSopenharmony_ci        block->SetValue(value);
804bc03f14fSopenharmony_ci    });
805bc03f14fSopenharmony_ci    thread.detach();
806bc03f14fSopenharmony_ci    auto value = block->GetValue();
807bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "Request timed out.")) {
808bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, GetDataSource failed.");
809bc03f14fSopenharmony_ci        return nullptr;
810bc03f14fSopenharmony_ci    }
811bc03f14fSopenharmony_ci
812bc03f14fSopenharmony_ci    if (*value != static_cast<int>(PasteboardError::E_OK)) {
813bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetDataSource, failed, ret = %{public}d", *value);
814bc03f14fSopenharmony_ci        return nullptr;
815bc03f14fSopenharmony_ci    }
816bc03f14fSopenharmony_ci    napi_value result = nullptr;
817bc03f14fSopenharmony_ci    napi_create_string_utf8(env, bundleName.c_str(), NAPI_AUTO_LENGTH, &result);
818bc03f14fSopenharmony_ci    return result;
819bc03f14fSopenharmony_ci}
820bc03f14fSopenharmony_ci
821bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::HasDataType(napi_env env, napi_callback_info info)
822bc03f14fSopenharmony_ci{
823bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi HasDataType() is called!");
824bc03f14fSopenharmony_ci    size_t argc = 1;
825bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
826bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
827bc03f14fSopenharmony_ci
828bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
829bc03f14fSopenharmony_ci    if ((!CheckExpression(env, argc >= ARGC_TYPE_SET1, JSErrorCode::INVALID_PARAMETERS,
830bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be grater than zero.")) ||
831bc03f14fSopenharmony_ci        (!CheckArgsType(env, argv[0], napi_string, "Parameter error. The type of mimeType must be string."))) {
832bc03f14fSopenharmony_ci        return nullptr;
833bc03f14fSopenharmony_ci    }
834bc03f14fSopenharmony_ci
835bc03f14fSopenharmony_ci    std::string mimeType;
836bc03f14fSopenharmony_ci    if (!GetValue(env, argv[0], mimeType)) {
837bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
838bc03f14fSopenharmony_ci        return nullptr;
839bc03f14fSopenharmony_ci    }
840bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
841bc03f14fSopenharmony_ci    std::thread thread([block, mimeType]() {
842bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->HasDataType(mimeType);
843bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "ret = %{public}d", ret);
844bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(static_cast<int>(ret));
845bc03f14fSopenharmony_ci        block->SetValue(value);
846bc03f14fSopenharmony_ci    });
847bc03f14fSopenharmony_ci    thread.detach();
848bc03f14fSopenharmony_ci    auto value = block->GetValue();
849bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "Request timed out.")) {
850bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, HasDataType failed.");
851bc03f14fSopenharmony_ci        return nullptr;
852bc03f14fSopenharmony_ci    }
853bc03f14fSopenharmony_ci    napi_value result = nullptr;
854bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "value = %{public}d", *value);
855bc03f14fSopenharmony_ci    napi_get_boolean(env, *value, &result);
856bc03f14fSopenharmony_ci    return result;
857bc03f14fSopenharmony_ci}
858bc03f14fSopenharmony_ci
859bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::DetectPatterns(napi_env env, napi_callback_info info)
860bc03f14fSopenharmony_ci{
861bc03f14fSopenharmony_ci    auto context = std::make_shared<DetectPatternsContextInfo>();
862bc03f14fSopenharmony_ci    auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
863bc03f14fSopenharmony_ci        if (!CheckExpression(env, argc == ARGC_TYPE_SET1, JSErrorCode::INVALID_PARAMETERS,
864bc03f14fSopenharmony_ci            "Parameter error. The number of arguments must be one.")) {
865bc03f14fSopenharmony_ci            return napi_invalid_arg;
866bc03f14fSopenharmony_ci        }
867bc03f14fSopenharmony_ci        bool getValueRes = GetValue(env, argv[0], context->patternsToCheck);
868bc03f14fSopenharmony_ci        if (!CheckExpression(env, getValueRes, JSErrorCode::INVALID_PARAMETERS,
869bc03f14fSopenharmony_ci            "Parameter error. Array<Pattern> expected.")) {
870bc03f14fSopenharmony_ci            PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue.");
871bc03f14fSopenharmony_ci            return napi_invalid_arg;
872bc03f14fSopenharmony_ci        }
873bc03f14fSopenharmony_ci        return napi_ok;
874bc03f14fSopenharmony_ci    };
875bc03f14fSopenharmony_ci    auto output = [context](napi_env env, napi_value *result) -> napi_status {
876bc03f14fSopenharmony_ci        napi_status status = SetValue(env, context->patternsDetect, *result);
877bc03f14fSopenharmony_ci        return status;
878bc03f14fSopenharmony_ci    };
879bc03f14fSopenharmony_ci    auto exec = [context](AsyncCall::Context *ctx) {
880bc03f14fSopenharmony_ci        context->patternsDetect = PasteboardClient::GetInstance()->DetectPatterns(context->patternsToCheck);
881bc03f14fSopenharmony_ci        context->status = napi_ok;
882bc03f14fSopenharmony_ci    };
883bc03f14fSopenharmony_ci    context->SetAction(std::move(input), std::move(output));
884bc03f14fSopenharmony_ci    AsyncCall asyncCall(env, info, context, 1);
885bc03f14fSopenharmony_ci    return asyncCall.Call(env, exec);
886bc03f14fSopenharmony_ci}
887bc03f14fSopenharmony_ci
888bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::ClearDataSync(napi_env env, napi_callback_info info)
889bc03f14fSopenharmony_ci{
890bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi ClearDataSync() is called!");
891bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
892bc03f14fSopenharmony_ci    std::thread thread([block]() {
893bc03f14fSopenharmony_ci        PasteboardClient::GetInstance()->Clear();
894bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(0);
895bc03f14fSopenharmony_ci        block->SetValue(value);
896bc03f14fSopenharmony_ci    });
897bc03f14fSopenharmony_ci    thread.detach();
898bc03f14fSopenharmony_ci    auto value = block->GetValue();
899bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "Request timed out.")) {
900bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, ClearDataSync failed.");
901bc03f14fSopenharmony_ci    }
902bc03f14fSopenharmony_ci    return nullptr;
903bc03f14fSopenharmony_ci}
904bc03f14fSopenharmony_ci
905bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::GetDataSync(napi_env env, napi_callback_info info)
906bc03f14fSopenharmony_ci{
907bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi GetDataSync() is called!");
908bc03f14fSopenharmony_ci    napi_value instance = nullptr;
909bc03f14fSopenharmony_ci    NAPI_CALL(env, PasteDataNapi::NewInstance(env, instance));
910bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
911bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, instance, reinterpret_cast<void **>(&obj));
912bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr) || obj->value_ == nullptr) {
913bc03f14fSopenharmony_ci        return nullptr;
914bc03f14fSopenharmony_ci    }
915bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int32_t>>>(SYNC_TIMEOUT);
916bc03f14fSopenharmony_ci    std::thread thread([block, pasteData = obj->value_]() mutable {
917bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->GetPasteData(*pasteData);
918bc03f14fSopenharmony_ci        std::shared_ptr<int32_t> value = std::make_shared<int32_t>(ret);
919bc03f14fSopenharmony_ci        block->SetValue(value);
920bc03f14fSopenharmony_ci    });
921bc03f14fSopenharmony_ci    thread.detach();
922bc03f14fSopenharmony_ci    auto value = block->GetValue();
923bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "request timed out.")) {
924bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, GetDataSync failed.");
925bc03f14fSopenharmony_ci    }
926bc03f14fSopenharmony_ci    return instance;
927bc03f14fSopenharmony_ci}
928bc03f14fSopenharmony_ci
929bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SetDataSync(napi_env env, napi_callback_info info)
930bc03f14fSopenharmony_ci{
931bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi SetDataSync() is called!");
932bc03f14fSopenharmony_ci    size_t argc = 1;
933bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
934bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
935bc03f14fSopenharmony_ci
936bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
937bc03f14fSopenharmony_ci    if (!CheckExpression(env, argc > 0, JSErrorCode::INVALID_PARAMETERS,
938bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be one.") ||
939bc03f14fSopenharmony_ci        !CheckExpression(env, PasteDataNapi::IsPasteData(env, argv[0]), JSErrorCode::INVALID_PARAMETERS,
940bc03f14fSopenharmony_ci        "Parameter error. The Type of data must be pasteData.")) {
941bc03f14fSopenharmony_ci        return nullptr;
942bc03f14fSopenharmony_ci    }
943bc03f14fSopenharmony_ci
944bc03f14fSopenharmony_ci    PasteDataNapi *pasteData = nullptr;
945bc03f14fSopenharmony_ci    napi_unwrap(env, argv[0], reinterpret_cast<void **>(&pasteData));
946bc03f14fSopenharmony_ci    if (pasteData == nullptr || pasteData->value_ == nullptr) {
947bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
948bc03f14fSopenharmony_ci        return nullptr;
949bc03f14fSopenharmony_ci    }
950bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
951bc03f14fSopenharmony_ci    std::shared_ptr<PasteData> data = pasteData->value_;
952bc03f14fSopenharmony_ci    std::thread thread([block, data]() {
953bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->SetPasteData(*data);
954bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(static_cast<int>(ret));
955bc03f14fSopenharmony_ci        block->SetValue(value);
956bc03f14fSopenharmony_ci    });
957bc03f14fSopenharmony_ci    thread.detach();
958bc03f14fSopenharmony_ci    auto value = block->GetValue();
959bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "request timed out.")) {
960bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, SetDataSync failed.");
961bc03f14fSopenharmony_ci        return nullptr;
962bc03f14fSopenharmony_ci    }
963bc03f14fSopenharmony_ci
964bc03f14fSopenharmony_ci    if (*value != static_cast<int32_t>(PasteboardError::E_OK)) {
965bc03f14fSopenharmony_ci        PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "operate invalid, SetDataSync failed");
966bc03f14fSopenharmony_ci        return nullptr;
967bc03f14fSopenharmony_ci    }
968bc03f14fSopenharmony_ci    return nullptr;
969bc03f14fSopenharmony_ci}
970bc03f14fSopenharmony_ci
971bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::HasDataSync(napi_env env, napi_callback_info info)
972bc03f14fSopenharmony_ci{
973bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SystemPasteboardNapi HasDataSync() is called!");
974bc03f14fSopenharmony_ci    auto block = std::make_shared<BlockObject<std::shared_ptr<int>>>(SYNC_TIMEOUT);
975bc03f14fSopenharmony_ci    std::thread thread([block]() {
976bc03f14fSopenharmony_ci        auto ret = PasteboardClient::GetInstance()->HasPasteData();
977bc03f14fSopenharmony_ci        std::shared_ptr<int> value = std::make_shared<int>(static_cast<int>(ret));
978bc03f14fSopenharmony_ci        block->SetValue(value);
979bc03f14fSopenharmony_ci    });
980bc03f14fSopenharmony_ci    thread.detach();
981bc03f14fSopenharmony_ci    auto value = block->GetValue();
982bc03f14fSopenharmony_ci    if (!CheckExpression(env, value != nullptr, JSErrorCode::REQUEST_TIME_OUT, "request timed out.")) {
983bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "time out, HasDataSync failed.");
984bc03f14fSopenharmony_ci        return nullptr;
985bc03f14fSopenharmony_ci    }
986bc03f14fSopenharmony_ci    napi_value result = nullptr;
987bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "value=%{public}d", *value);
988bc03f14fSopenharmony_ci    napi_get_boolean(env, *value, &result);
989bc03f14fSopenharmony_ci    return result;
990bc03f14fSopenharmony_ci}
991bc03f14fSopenharmony_ci
992bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::SystemPasteboardInit(napi_env env, napi_value exports)
993bc03f14fSopenharmony_ci{
994bc03f14fSopenharmony_ci    napi_status status = napi_ok;
995bc03f14fSopenharmony_ci    napi_property_descriptor descriptors[] = {
996bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("on", On),
997bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("off", Off),
998bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("clear", Clear),
999bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getPasteData", GetPasteData),
1000bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("hasPasteData", HasPasteData),
1001bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setPasteData", SetPasteData),
1002bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("clearData", ClearData),
1003bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getData", GetData),
1004bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("hasData", HasData),
1005bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setData", SetData),
1006bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("isRemoteData", IsRemoteData),
1007bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getDataSource", GetDataSource),
1008bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("hasDataType", HasDataType),
1009bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("detectPatterns", DetectPatterns),
1010bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("clearDataSync", ClearDataSync),
1011bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getDataSync", GetDataSync),
1012bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("hasDataSync", HasDataSync),
1013bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setDataSync", SetDataSync),
1014bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setUnifiedData", SetUnifiedData),
1015bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getUnifiedData", GetUnifiedData),
1016bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setUnifiedDataSync", SetUnifiedDataSync),
1017bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getUnifiedDataSync", GetUnifiedDataSync),
1018bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setAppShareOptions", SetAppShareOptions),
1019bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("removeAppShareOptions", RemoveAppShareOptions),
1020bc03f14fSopenharmony_ci
1021bc03f14fSopenharmony_ci    };
1022bc03f14fSopenharmony_ci    napi_value constructor;
1023bc03f14fSopenharmony_ci    napi_define_class(env, "SystemPasteboard", NAPI_AUTO_LENGTH, New, nullptr,
1024bc03f14fSopenharmony_ci        sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &constructor);
1025bc03f14fSopenharmony_ci    if (status != napi_ok) {
1026bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to define class at SystemPasteboardInit");
1027bc03f14fSopenharmony_ci        return nullptr;
1028bc03f14fSopenharmony_ci    }
1029bc03f14fSopenharmony_ci    napi_create_reference(env, constructor, 1, &g_systemPasteboard);
1030bc03f14fSopenharmony_ci    status = napi_set_named_property(env, exports, "SystemPasteboard", constructor);
1031bc03f14fSopenharmony_ci    if (status != napi_ok) {
1032bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Set property failed when SystemPasteboardInit");
1033bc03f14fSopenharmony_ci        return nullptr;
1034bc03f14fSopenharmony_ci    }
1035bc03f14fSopenharmony_ci    return exports;
1036bc03f14fSopenharmony_ci}
1037bc03f14fSopenharmony_ci
1038bc03f14fSopenharmony_ciSystemPasteboardNapi::SystemPasteboardNapi() : env_(nullptr)
1039bc03f14fSopenharmony_ci{
1040bc03f14fSopenharmony_ci    value_ = std::make_shared<PasteDataNapi>();
1041bc03f14fSopenharmony_ci}
1042bc03f14fSopenharmony_ci
1043bc03f14fSopenharmony_ciSystemPasteboardNapi::~SystemPasteboardNapi()
1044bc03f14fSopenharmony_ci{
1045bc03f14fSopenharmony_ci}
1046bc03f14fSopenharmony_ci
1047bc03f14fSopenharmony_civoid SystemPasteboardNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
1048bc03f14fSopenharmony_ci{
1049bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "Destructor");
1050bc03f14fSopenharmony_ci    SystemPasteboardNapi *obj = static_cast<SystemPasteboardNapi *>(nativeObject);
1051bc03f14fSopenharmony_ci    delete obj;
1052bc03f14fSopenharmony_ci}
1053bc03f14fSopenharmony_ci
1054bc03f14fSopenharmony_cinapi_value SystemPasteboardNapi::New(napi_env env, napi_callback_info info)
1055bc03f14fSopenharmony_ci{
1056bc03f14fSopenharmony_ci    size_t argc = MAX_ARGS;
1057bc03f14fSopenharmony_ci    napi_value argv[MAX_ARGS] = { 0 };
1058bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
1059bc03f14fSopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1060bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "proc.");
1061bc03f14fSopenharmony_ci    // get native object
1062bc03f14fSopenharmony_ci    SystemPasteboardNapi *obj = new (std::nothrow) SystemPasteboardNapi();
1063bc03f14fSopenharmony_ci    if (!obj) {
1064bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "New obj is null");
1065bc03f14fSopenharmony_ci        return nullptr;
1066bc03f14fSopenharmony_ci    }
1067bc03f14fSopenharmony_ci    obj->env_ = env;
1068bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_wrap(env, thisVar, obj, SystemPasteboardNapi::Destructor,
1069bc03f14fSopenharmony_ci                       nullptr, // finalize_hint
1070bc03f14fSopenharmony_ci                       nullptr));
1071bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "end.");
1072bc03f14fSopenharmony_ci    return thisVar;
1073bc03f14fSopenharmony_ci}
1074bc03f14fSopenharmony_ci
1075bc03f14fSopenharmony_cinapi_status SystemPasteboardNapi::NewInstance(napi_env env, napi_value &instance)
1076bc03f14fSopenharmony_ci{
1077bc03f14fSopenharmony_ci    napi_status status;
1078bc03f14fSopenharmony_ci    if (g_systemPasteboard_instance != nullptr) {
1079bc03f14fSopenharmony_ci        status = napi_get_reference_value(env, g_systemPasteboard_instance, &instance);
1080bc03f14fSopenharmony_ci        if (status != napi_ok) {
1081bc03f14fSopenharmony_ci            PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "get instance failed");
1082bc03f14fSopenharmony_ci            return status;
1083bc03f14fSopenharmony_ci        }
1084bc03f14fSopenharmony_ci        return napi_ok;
1085bc03f14fSopenharmony_ci    }
1086bc03f14fSopenharmony_ci
1087bc03f14fSopenharmony_ci    napi_value constructor;
1088bc03f14fSopenharmony_ci    status = napi_get_reference_value(env, g_systemPasteboard, &constructor);
1089bc03f14fSopenharmony_ci    if (status != napi_ok) {
1090bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "get reference failed");
1091bc03f14fSopenharmony_ci        return status;
1092bc03f14fSopenharmony_ci    }
1093bc03f14fSopenharmony_ci
1094bc03f14fSopenharmony_ci    status = napi_new_instance(env, constructor, 0, nullptr, &instance);
1095bc03f14fSopenharmony_ci    if (status != napi_ok) {
1096bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "new instance failed");
1097bc03f14fSopenharmony_ci        return status;
1098bc03f14fSopenharmony_ci    }
1099bc03f14fSopenharmony_ci    napi_create_reference(env, instance, 1, &g_systemPasteboard_instance);
1100bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "new instance ok");
1101bc03f14fSopenharmony_ci
1102bc03f14fSopenharmony_ci    return napi_ok;
1103bc03f14fSopenharmony_ci}
1104bc03f14fSopenharmony_ci
1105bc03f14fSopenharmony_cistd::shared_ptr<PasteboardObserverInstance> SystemPasteboardNapi::GetObserver(napi_env env, napi_value observer)
1106bc03f14fSopenharmony_ci{
1107bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetObserver start");
1108bc03f14fSopenharmony_ci    for (auto &[refKey, observerValue] : observers_) {
1109bc03f14fSopenharmony_ci        napi_value callback = nullptr;
1110bc03f14fSopenharmony_ci        napi_get_reference_value(env, refKey, &callback);
1111bc03f14fSopenharmony_ci        bool isEqual = false;
1112bc03f14fSopenharmony_ci        napi_strict_equals(env, observer, callback, &isEqual);
1113bc03f14fSopenharmony_ci        if (isEqual) {
1114bc03f14fSopenharmony_ci            return observerValue;
1115bc03f14fSopenharmony_ci        }
1116bc03f14fSopenharmony_ci    }
1117bc03f14fSopenharmony_ci    return nullptr;
1118bc03f14fSopenharmony_ci}
1119bc03f14fSopenharmony_ci
1120bc03f14fSopenharmony_civoid SystemPasteboardNapi::DeleteObserver(const std::shared_ptr<PasteboardObserverInstance> &observer)
1121bc03f14fSopenharmony_ci{
1122bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "observer == null: %{public}d, size: %{public}zu",
1123bc03f14fSopenharmony_ci        observer == nullptr, observers_.size());
1124bc03f14fSopenharmony_ci    std::vector<std::shared_ptr<PasteboardObserverInstance>> observers;
1125bc03f14fSopenharmony_ci    {
1126bc03f14fSopenharmony_ci        for (auto it = observers_.begin(); it != observers_.end();) {
1127bc03f14fSopenharmony_ci            if (it->second == observer) {
1128bc03f14fSopenharmony_ci                observers.push_back(observer);
1129bc03f14fSopenharmony_ci                observers_.erase(it++);
1130bc03f14fSopenharmony_ci                break;
1131bc03f14fSopenharmony_ci            }
1132bc03f14fSopenharmony_ci            if (observer == nullptr) {
1133bc03f14fSopenharmony_ci                observers.push_back(it->second);
1134bc03f14fSopenharmony_ci                observers_.erase(it++);
1135bc03f14fSopenharmony_ci            } else {
1136bc03f14fSopenharmony_ci                it++;
1137bc03f14fSopenharmony_ci            }
1138bc03f14fSopenharmony_ci        }
1139bc03f14fSopenharmony_ci    }
1140bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "delete observer size: %{public}zu", observers.size());
1141bc03f14fSopenharmony_ci    for (auto &delObserver : observers) {
1142bc03f14fSopenharmony_ci        PasteboardClient::GetInstance()->Unsubscribe(PasteboardObserverType::OBSERVER_LOCAL,
1143bc03f14fSopenharmony_ci            delObserver->GetStub());
1144bc03f14fSopenharmony_ci    }
1145bc03f14fSopenharmony_ci}
1146bc03f14fSopenharmony_ci
1147bc03f14fSopenharmony_civoid PasteboardObserverInstance::PasteboardObserverImpl::OnPasteboardChanged()
1148bc03f14fSopenharmony_ci{
1149bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardObserverInstance> observerInstance(wrapper_.lock());
1150bc03f14fSopenharmony_ci    if (observerInstance == nullptr) {
1151bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "expired callback");
1152bc03f14fSopenharmony_ci        return;
1153bc03f14fSopenharmony_ci    }
1154bc03f14fSopenharmony_ci    observerInstance->OnPasteboardChanged();
1155bc03f14fSopenharmony_ci}
1156bc03f14fSopenharmony_ci
1157bc03f14fSopenharmony_civoid PasteboardObserverInstance::PasteboardObserverImpl::SetObserverWrapper(
1158bc03f14fSopenharmony_ci    const std::shared_ptr<PasteboardObserverInstance> &observerInstance)
1159bc03f14fSopenharmony_ci{
1160bc03f14fSopenharmony_ci    wrapper_ = observerInstance;
1161bc03f14fSopenharmony_ci}
1162bc03f14fSopenharmony_ci
1163bc03f14fSopenharmony_civoid PasteboardDelayGetterInstance::PasteboardDelayGetterImpl::GetUnifiedData(
1164bc03f14fSopenharmony_ci    const std::string &type, UDMF::UnifiedData &data)
1165bc03f14fSopenharmony_ci{
1166bc03f14fSopenharmony_ci    std::shared_ptr<PasteboardDelayGetterInstance> delayGetterInstance(wrapper_.lock());
1167bc03f14fSopenharmony_ci    if (delayGetterInstance == nullptr) {
1168bc03f14fSopenharmony_ci        PASTEBOARD_HILOGW(PASTEBOARD_MODULE_JS_NAPI, "no delay getter");
1169bc03f14fSopenharmony_ci        return;
1170bc03f14fSopenharmony_ci    }
1171bc03f14fSopenharmony_ci    delayGetterInstance->GetUnifiedData(type, data);
1172bc03f14fSopenharmony_ci}
1173bc03f14fSopenharmony_ci
1174bc03f14fSopenharmony_civoid PasteboardDelayGetterInstance::PasteboardDelayGetterImpl::GetPasteData(
1175bc03f14fSopenharmony_ci    const std::string &type, MiscServices::PasteData &data)
1176bc03f14fSopenharmony_ci{
1177bc03f14fSopenharmony_ci}
1178bc03f14fSopenharmony_ci
1179bc03f14fSopenharmony_civoid PasteboardDelayGetterInstance::PasteboardDelayGetterImpl::SetDelayGetterWrapper(
1180bc03f14fSopenharmony_ci    const std::shared_ptr<PasteboardDelayGetterInstance> delayGetterInstance)
1181bc03f14fSopenharmony_ci{
1182bc03f14fSopenharmony_ci    wrapper_ = delayGetterInstance;
1183bc03f14fSopenharmony_ci}
1184bc03f14fSopenharmony_ci} // namespace MiscServicesNapi
1185bc03f14fSopenharmony_ci} // namespace OHOS