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 <cstddef>
16bc03f14fSopenharmony_ci
17bc03f14fSopenharmony_ci#include "napi_common_want.h"
18bc03f14fSopenharmony_ci#include "pasteboard_common.h"
19bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
20bc03f14fSopenharmony_ci#include "pasteboard_js_err.h"
21bc03f14fSopenharmony_ci#include "pastedata_napi.h"
22bc03f14fSopenharmony_ci#include "pastedata_record_napi.h"
23bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices;
24bc03f14fSopenharmony_ciusing namespace OHOS::Media;
25bc03f14fSopenharmony_ci
26bc03f14fSopenharmony_cinamespace OHOS {
27bc03f14fSopenharmony_cinamespace MiscServicesNapi {
28bc03f14fSopenharmony_cinamespace {
29bc03f14fSopenharmony_ciconstexpr int ARGC_TYPE_SET0 = 0;
30bc03f14fSopenharmony_ciconstexpr int ARGC_TYPE_SET1 = 1;
31bc03f14fSopenharmony_ciconstexpr int ARGC_TYPE_SET2 = 2;
32bc03f14fSopenharmony_ciconst int32_t STR_MAX_SIZE = 256;
33bc03f14fSopenharmony_ciconstexpr int32_t MIMETYPE_MAX_SIZE = 1024;
34bc03f14fSopenharmony_ciconstexpr int32_t MAX_TEXT_LEN = 20 * 1024 * 1024;
35bc03f14fSopenharmony_ciconstexpr size_t STR_TAIL_LENGTH = 1;
36bc03f14fSopenharmony_ci} // namespace
37bc03f14fSopenharmony_cistatic thread_local napi_ref g_pasteData = nullptr;
38bc03f14fSopenharmony_ci
39bc03f14fSopenharmony_ciPasteDataNapi::PasteDataNapi() : env_(nullptr)
40bc03f14fSopenharmony_ci{
41bc03f14fSopenharmony_ci    value_ = std::make_shared<PasteData>();
42bc03f14fSopenharmony_ci}
43bc03f14fSopenharmony_ci
44bc03f14fSopenharmony_ciPasteDataNapi::~PasteDataNapi() {}
45bc03f14fSopenharmony_ci
46bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddHtmlRecord(napi_env env, napi_callback_info info)
47bc03f14fSopenharmony_ci{
48bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddHtmlRecord is called!");
49bc03f14fSopenharmony_ci    size_t argc = 1;
50bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
51bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
52bc03f14fSopenharmony_ci
53bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
54bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET0, "Wrong number of arguments");
55bc03f14fSopenharmony_ci
56bc03f14fSopenharmony_ci    std::string str;
57bc03f14fSopenharmony_ci    bool ret = GetValue(env, argv[0], str);
58bc03f14fSopenharmony_ci    if (!ret) {
59bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
60bc03f14fSopenharmony_ci        return nullptr;
61bc03f14fSopenharmony_ci    }
62bc03f14fSopenharmony_ci
63bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
64bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
65bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
66bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddHtmlRecord object failed");
67bc03f14fSopenharmony_ci        return nullptr;
68bc03f14fSopenharmony_ci    }
69bc03f14fSopenharmony_ci    obj->value_->AddHtmlRecord(str);
70bc03f14fSopenharmony_ci    return nullptr;
71bc03f14fSopenharmony_ci}
72bc03f14fSopenharmony_ci
73bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddPixelMapRecord(napi_env env, napi_callback_info info)
74bc03f14fSopenharmony_ci{
75bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddPixelMapRecord is called begin!");
76bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET1;
77bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET1] = { 0 };
78bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
79bc03f14fSopenharmony_ci
80bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
81bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET0, "Wrong number of arguments");
82bc03f14fSopenharmony_ci
83bc03f14fSopenharmony_ci    napi_valuetype valueType = napi_undefined;
84bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
85bc03f14fSopenharmony_ci    NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
86bc03f14fSopenharmony_ci
87bc03f14fSopenharmony_ci    std::shared_ptr<PixelMap> pixelMap = PixelMapNapi::GetPixelMap(env, argv[0]);
88bc03f14fSopenharmony_ci    if (pixelMap == nullptr) {
89bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetPixelMap!");
90bc03f14fSopenharmony_ci        return nullptr;
91bc03f14fSopenharmony_ci    }
92bc03f14fSopenharmony_ci
93bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
94bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
95bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
96bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "unwrap failed");
97bc03f14fSopenharmony_ci        return nullptr;
98bc03f14fSopenharmony_ci    }
99bc03f14fSopenharmony_ci    obj->value_->AddPixelMapRecord(pixelMap);
100bc03f14fSopenharmony_ci    return nullptr;
101bc03f14fSopenharmony_ci}
102bc03f14fSopenharmony_ci
103bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddTextRecord(napi_env env, napi_callback_info info)
104bc03f14fSopenharmony_ci{
105bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddTextRecord is called!");
106bc03f14fSopenharmony_ci    size_t argc = 1;
107bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
108bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
109bc03f14fSopenharmony_ci
110bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
111bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET0, "Wrong number of arguments");
112bc03f14fSopenharmony_ci
113bc03f14fSopenharmony_ci    std::string str;
114bc03f14fSopenharmony_ci    bool ret = GetValue(env, argv[0], str);
115bc03f14fSopenharmony_ci    if (!ret) {
116bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
117bc03f14fSopenharmony_ci        return nullptr;
118bc03f14fSopenharmony_ci    }
119bc03f14fSopenharmony_ci
120bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
121bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
122bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
123bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddTextRecord object failed");
124bc03f14fSopenharmony_ci        return nullptr;
125bc03f14fSopenharmony_ci    }
126bc03f14fSopenharmony_ci    obj->value_->AddTextRecord(str);
127bc03f14fSopenharmony_ci    return nullptr;
128bc03f14fSopenharmony_ci}
129bc03f14fSopenharmony_ci
130bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddUriRecord(napi_env env, napi_callback_info info)
131bc03f14fSopenharmony_ci{
132bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddUriRecord is called!");
133bc03f14fSopenharmony_ci    size_t argc = 1;
134bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
135bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
136bc03f14fSopenharmony_ci
137bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
138bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET0, "Wrong number of arguments");
139bc03f14fSopenharmony_ci
140bc03f14fSopenharmony_ci    std::string str;
141bc03f14fSopenharmony_ci    bool ret = GetValue(env, argv[0], str);
142bc03f14fSopenharmony_ci    if (!ret) {
143bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
144bc03f14fSopenharmony_ci        return nullptr;
145bc03f14fSopenharmony_ci    }
146bc03f14fSopenharmony_ci
147bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
148bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
149bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
150bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddUriRecord object failed");
151bc03f14fSopenharmony_ci        return nullptr;
152bc03f14fSopenharmony_ci    }
153bc03f14fSopenharmony_ci    obj->value_->AddUriRecord(OHOS::Uri(str));
154bc03f14fSopenharmony_ci    return nullptr;
155bc03f14fSopenharmony_ci}
156bc03f14fSopenharmony_ci
157bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryHtml(napi_env env, napi_callback_info info)
158bc03f14fSopenharmony_ci{
159bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryHtml is called!");
160bc03f14fSopenharmony_ci    size_t argc = 1;
161bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
162bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
163bc03f14fSopenharmony_ci
164bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
165bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
166bc03f14fSopenharmony_ci
167bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
168bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
169bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
170bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryHtml object failed");
171bc03f14fSopenharmony_ci        return nullptr;
172bc03f14fSopenharmony_ci    }
173bc03f14fSopenharmony_ci
174bc03f14fSopenharmony_ci    std::shared_ptr<std::string> p = obj->value_->GetPrimaryHtml();
175bc03f14fSopenharmony_ci    if (p == nullptr) {
176bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryHtml failed");
177bc03f14fSopenharmony_ci        return nullptr;
178bc03f14fSopenharmony_ci    }
179bc03f14fSopenharmony_ci
180bc03f14fSopenharmony_ci    napi_value result = nullptr;
181bc03f14fSopenharmony_ci    napi_create_string_utf8(env, p->c_str(), NAPI_AUTO_LENGTH, &result);
182bc03f14fSopenharmony_ci    return result;
183bc03f14fSopenharmony_ci}
184bc03f14fSopenharmony_ci
185bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryPixelMap(napi_env env, napi_callback_info info)
186bc03f14fSopenharmony_ci{
187bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryPixelMap is called!");
188bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET1;
189bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET1] = { 0 };
190bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
191bc03f14fSopenharmony_ci
192bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
193bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
194bc03f14fSopenharmony_ci
195bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
196bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
197bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
198bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "unwrap failed");
199bc03f14fSopenharmony_ci        return nullptr;
200bc03f14fSopenharmony_ci    }
201bc03f14fSopenharmony_ci
202bc03f14fSopenharmony_ci    std::shared_ptr<PixelMap> pixelMap = obj->value_->GetPrimaryPixelMap();
203bc03f14fSopenharmony_ci    if (!pixelMap) {
204bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "pixelMap is nullptr");
205bc03f14fSopenharmony_ci        return nullptr;
206bc03f14fSopenharmony_ci    }
207bc03f14fSopenharmony_ci
208bc03f14fSopenharmony_ci    napi_value jsPixelMap = PixelMapNapi::CreatePixelMap(env, pixelMap);
209bc03f14fSopenharmony_ci    return jsPixelMap;
210bc03f14fSopenharmony_ci}
211bc03f14fSopenharmony_ci
212bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryText(napi_env env, napi_callback_info info)
213bc03f14fSopenharmony_ci{
214bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryText is called!");
215bc03f14fSopenharmony_ci    size_t argc = 1;
216bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
217bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
218bc03f14fSopenharmony_ci
219bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
220bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
221bc03f14fSopenharmony_ci
222bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
223bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
224bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
225bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryText object failed");
226bc03f14fSopenharmony_ci        return nullptr;
227bc03f14fSopenharmony_ci    }
228bc03f14fSopenharmony_ci
229bc03f14fSopenharmony_ci    std::shared_ptr<std::string> p = obj->value_->GetPrimaryText();
230bc03f14fSopenharmony_ci    if (p == nullptr) {
231bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryText failed");
232bc03f14fSopenharmony_ci        return nullptr;
233bc03f14fSopenharmony_ci    }
234bc03f14fSopenharmony_ci
235bc03f14fSopenharmony_ci    napi_value result = nullptr;
236bc03f14fSopenharmony_ci    napi_create_string_utf8(env, p->c_str(), NAPI_AUTO_LENGTH, &result);
237bc03f14fSopenharmony_ci    return result;
238bc03f14fSopenharmony_ci}
239bc03f14fSopenharmony_ci
240bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryUri(napi_env env, napi_callback_info info)
241bc03f14fSopenharmony_ci{
242bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryUri is called!");
243bc03f14fSopenharmony_ci    size_t argc = 1;
244bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
245bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
246bc03f14fSopenharmony_ci
247bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
248bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
249bc03f14fSopenharmony_ci
250bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
251bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
252bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
253bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryUri object failed");
254bc03f14fSopenharmony_ci        return nullptr;
255bc03f14fSopenharmony_ci    }
256bc03f14fSopenharmony_ci
257bc03f14fSopenharmony_ci    std::shared_ptr<OHOS::Uri> p = obj->value_->GetPrimaryUri();
258bc03f14fSopenharmony_ci    if (p == nullptr) {
259bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryUri failed");
260bc03f14fSopenharmony_ci        return nullptr;
261bc03f14fSopenharmony_ci    }
262bc03f14fSopenharmony_ci
263bc03f14fSopenharmony_ci    std::string text = p->ToString();
264bc03f14fSopenharmony_ci    napi_value result = nullptr;
265bc03f14fSopenharmony_ci    napi_create_string_utf8(env, text.c_str(), NAPI_AUTO_LENGTH, &result);
266bc03f14fSopenharmony_ci    return result;
267bc03f14fSopenharmony_ci}
268bc03f14fSopenharmony_ci
269bc03f14fSopenharmony_cinapi_value PasteDataNapi::HasMimeType(napi_env env, napi_callback_info info)
270bc03f14fSopenharmony_ci{
271bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "HasMimeType is called!");
272bc03f14fSopenharmony_ci    size_t argc = 1;
273bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
274bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
275bc03f14fSopenharmony_ci
276bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
277bc03f14fSopenharmony_ci    if ((!CheckExpression(env, argc > ARGC_TYPE_SET0, JSErrorCode::INVALID_PARAMETERS,
278bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be greater than zero.")) ||
279bc03f14fSopenharmony_ci        (!CheckArgsType(env, argv[0], napi_string, "Parameter error. The type of mimeType must be string."))) {
280bc03f14fSopenharmony_ci        return nullptr;
281bc03f14fSopenharmony_ci    }
282bc03f14fSopenharmony_ci
283bc03f14fSopenharmony_ci    std::string mimeType;
284bc03f14fSopenharmony_ci    if (!GetValue(env, argv[0], mimeType)) {
285bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
286bc03f14fSopenharmony_ci        return nullptr;
287bc03f14fSopenharmony_ci    }
288bc03f14fSopenharmony_ci
289bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
290bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
291bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
292bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddHtmlRecord object failed");
293bc03f14fSopenharmony_ci        return nullptr;
294bc03f14fSopenharmony_ci    }
295bc03f14fSopenharmony_ci
296bc03f14fSopenharmony_ci    bool ret = obj->value_->HasMimeType(mimeType);
297bc03f14fSopenharmony_ci    napi_value result = nullptr;
298bc03f14fSopenharmony_ci    napi_get_boolean(env, ret, &result);
299bc03f14fSopenharmony_ci    return result;
300bc03f14fSopenharmony_ci}
301bc03f14fSopenharmony_ci
302bc03f14fSopenharmony_cinapi_value PasteDataNapi::HasType(napi_env env, napi_callback_info info)
303bc03f14fSopenharmony_ci{
304bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "HasType is called!");
305bc03f14fSopenharmony_ci    return HasMimeType(env, info);
306bc03f14fSopenharmony_ci}
307bc03f14fSopenharmony_ci
308bc03f14fSopenharmony_ciPasteDataNapi *PasteDataNapi::RemoveAndGetRecordCommon(napi_env env, napi_callback_info info, uint32_t &index)
309bc03f14fSopenharmony_ci{
310bc03f14fSopenharmony_ci    size_t argc = 1;
311bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
312bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
313bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
314bc03f14fSopenharmony_ci
315bc03f14fSopenharmony_ci    if ((!CheckExpression(env, argc > ARGC_TYPE_SET0, JSErrorCode::INVALID_PARAMETERS,
316bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be greater than zero.")) ||
317bc03f14fSopenharmony_ci        (!CheckArgsType(env, argv[0], napi_number, "Parameter error. The type of mimeType must be number."))) {
318bc03f14fSopenharmony_ci        return nullptr;
319bc03f14fSopenharmony_ci    }
320bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
321bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
322bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
323bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get RemoveRecord object failed");
324bc03f14fSopenharmony_ci        return nullptr;
325bc03f14fSopenharmony_ci    }
326bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_value_uint32(env, argv[0], &index));
327bc03f14fSopenharmony_ci    return obj;
328bc03f14fSopenharmony_ci}
329bc03f14fSopenharmony_ci
330bc03f14fSopenharmony_cinapi_value PasteDataNapi::RemoveRecordAt(napi_env env, napi_callback_info info)
331bc03f14fSopenharmony_ci{
332bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "RemoveRecordAt is called!");
333bc03f14fSopenharmony_ci    uint32_t index = 0;
334bc03f14fSopenharmony_ci    PasteDataNapi *obj = RemoveAndGetRecordCommon(env, info, index);
335bc03f14fSopenharmony_ci    if (obj == nullptr) {
336bc03f14fSopenharmony_ci        return nullptr;
337bc03f14fSopenharmony_ci    }
338bc03f14fSopenharmony_ci    bool ret = obj->value_->RemoveRecordAt(index);
339bc03f14fSopenharmony_ci    napi_value result = nullptr;
340bc03f14fSopenharmony_ci    napi_get_boolean(env, ret, &result);
341bc03f14fSopenharmony_ci    return result;
342bc03f14fSopenharmony_ci}
343bc03f14fSopenharmony_ci
344bc03f14fSopenharmony_cinapi_value PasteDataNapi::RemoveRecord(napi_env env, napi_callback_info info)
345bc03f14fSopenharmony_ci{
346bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "RemoveRecord is called!");
347bc03f14fSopenharmony_ci    uint32_t index = 0;
348bc03f14fSopenharmony_ci    PasteDataNapi *obj = RemoveAndGetRecordCommon(env, info, index);
349bc03f14fSopenharmony_ci    if (obj == nullptr || !CheckExpression(env, index < obj->value_->GetRecordCount(), JSErrorCode::OUT_OF_RANGE,
350bc03f14fSopenharmony_ci        "index out of range.")) {
351bc03f14fSopenharmony_ci        return nullptr;
352bc03f14fSopenharmony_ci    }
353bc03f14fSopenharmony_ci    obj->value_->RemoveRecordAt(index);
354bc03f14fSopenharmony_ci    return nullptr;
355bc03f14fSopenharmony_ci}
356bc03f14fSopenharmony_ci
357bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryMimeType(napi_env env, napi_callback_info info)
358bc03f14fSopenharmony_ci{
359bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryMimeType is called!");
360bc03f14fSopenharmony_ci    size_t argc = 1;
361bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
362bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
363bc03f14fSopenharmony_ci
364bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
365bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
366bc03f14fSopenharmony_ci
367bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
368bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
369bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
370bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryMimeType object failed");
371bc03f14fSopenharmony_ci        return nullptr;
372bc03f14fSopenharmony_ci    }
373bc03f14fSopenharmony_ci    std::shared_ptr<std::string> mimeType = obj->value_->GetPrimaryMimeType();
374bc03f14fSopenharmony_ci    if (mimeType == nullptr) {
375bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryMimeType failed");
376bc03f14fSopenharmony_ci        return nullptr;
377bc03f14fSopenharmony_ci    }
378bc03f14fSopenharmony_ci    napi_value result = nullptr;
379bc03f14fSopenharmony_ci    napi_create_string_utf8(env, mimeType->c_str(), NAPI_AUTO_LENGTH, &result);
380bc03f14fSopenharmony_ci
381bc03f14fSopenharmony_ci    return result;
382bc03f14fSopenharmony_ci}
383bc03f14fSopenharmony_ci
384bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetRecordCount(napi_env env, napi_callback_info info)
385bc03f14fSopenharmony_ci{
386bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetRecordCount is called!");
387bc03f14fSopenharmony_ci    size_t argc = 1;
388bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
389bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
390bc03f14fSopenharmony_ci
391bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
392bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
393bc03f14fSopenharmony_ci
394bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
395bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
396bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
397bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetRecordCount object failed");
398bc03f14fSopenharmony_ci        return nullptr;
399bc03f14fSopenharmony_ci    }
400bc03f14fSopenharmony_ci
401bc03f14fSopenharmony_ci    size_t count = obj->value_->GetRecordCount();
402bc03f14fSopenharmony_ci    napi_value result = nullptr;
403bc03f14fSopenharmony_ci    napi_create_int64(env, count, &result);
404bc03f14fSopenharmony_ci
405bc03f14fSopenharmony_ci    return result;
406bc03f14fSopenharmony_ci}
407bc03f14fSopenharmony_ci
408bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetTag(napi_env env, napi_callback_info info)
409bc03f14fSopenharmony_ci{
410bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetTag is called!");
411bc03f14fSopenharmony_ci    size_t argc = 1;
412bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
413bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
414bc03f14fSopenharmony_ci
415bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
416bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
417bc03f14fSopenharmony_ci
418bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
419bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
420bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
421bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetTag object failed");
422bc03f14fSopenharmony_ci        return nullptr;
423bc03f14fSopenharmony_ci    }
424bc03f14fSopenharmony_ci    std::string tag = obj->value_->GetTag();
425bc03f14fSopenharmony_ci    napi_value result = nullptr;
426bc03f14fSopenharmony_ci    napi_create_string_utf8(env, tag.c_str(), NAPI_AUTO_LENGTH, &result);
427bc03f14fSopenharmony_ci
428bc03f14fSopenharmony_ci    return result;
429bc03f14fSopenharmony_ci}
430bc03f14fSopenharmony_ci
431bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetMimeTypes(napi_env env, napi_callback_info info)
432bc03f14fSopenharmony_ci{
433bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetMimeTypes is called!");
434bc03f14fSopenharmony_ci    size_t argc = 1;
435bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
436bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
437bc03f14fSopenharmony_ci
438bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
439bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
440bc03f14fSopenharmony_ci
441bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
442bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
443bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
444bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetMimeTypes object failed");
445bc03f14fSopenharmony_ci        return nullptr;
446bc03f14fSopenharmony_ci    }
447bc03f14fSopenharmony_ci    std::vector<std::string> mimeTypes = obj->value_->GetMimeTypes();
448bc03f14fSopenharmony_ci    if (mimeTypes.size() == 0) {
449bc03f14fSopenharmony_ci        return nullptr;
450bc03f14fSopenharmony_ci    }
451bc03f14fSopenharmony_ci
452bc03f14fSopenharmony_ci    napi_value nMimeTypes = nullptr;
453bc03f14fSopenharmony_ci    if (napi_create_array(env, &nMimeTypes) != napi_ok) {
454bc03f14fSopenharmony_ci        return nullptr;
455bc03f14fSopenharmony_ci    }
456bc03f14fSopenharmony_ci    size_t index = 0;
457bc03f14fSopenharmony_ci    napi_value value = nullptr;
458bc03f14fSopenharmony_ci    for (auto type : mimeTypes) {
459bc03f14fSopenharmony_ci        napi_create_string_utf8(env, type.c_str(), NAPI_AUTO_LENGTH, &value);
460bc03f14fSopenharmony_ci        napi_set_element(env, nMimeTypes, index, value);
461bc03f14fSopenharmony_ci        index++;
462bc03f14fSopenharmony_ci    }
463bc03f14fSopenharmony_ci    return nMimeTypes;
464bc03f14fSopenharmony_ci}
465bc03f14fSopenharmony_ci
466bc03f14fSopenharmony_civoid PasteDataNapi::AddRecord(napi_env env, napi_value *argv, size_t argc, PasteDataNapi *obj)
467bc03f14fSopenharmony_ci{
468bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddRecordV9!");
469bc03f14fSopenharmony_ci    std::string mimeType;
470bc03f14fSopenharmony_ci    if (!CheckArgs(env, argv, argc, mimeType)) {
471bc03f14fSopenharmony_ci        return;
472bc03f14fSopenharmony_ci    }
473bc03f14fSopenharmony_ci    bool isArrayBuffer = false;
474bc03f14fSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_is_arraybuffer(env, argv[1], &isArrayBuffer));
475bc03f14fSopenharmony_ci    if (isArrayBuffer) {
476bc03f14fSopenharmony_ci        void *data = nullptr;
477bc03f14fSopenharmony_ci        size_t dataLen = 0;
478bc03f14fSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_get_arraybuffer_info(env, argv[1], &data, &dataLen));
479bc03f14fSopenharmony_ci        obj->value_->AddKvRecord(mimeType,
480bc03f14fSopenharmony_ci            std::vector<uint8_t>(reinterpret_cast<uint8_t *>(data), reinterpret_cast<uint8_t *>(data) + dataLen));
481bc03f14fSopenharmony_ci        return;
482bc03f14fSopenharmony_ci    }
483bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_PIXELMAP) {
484bc03f14fSopenharmony_ci        std::shared_ptr<PixelMap> pixelMap = PixelMapNapi::GetPixelMap(env, argv[1]);
485bc03f14fSopenharmony_ci        obj->value_->AddPixelMapRecord(pixelMap);
486bc03f14fSopenharmony_ci        return;
487bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_WANT) {
488bc03f14fSopenharmony_ci        OHOS::AAFwk::Want want;
489bc03f14fSopenharmony_ci        AppExecFwk::UnwrapWant(env, argv[1], want);
490bc03f14fSopenharmony_ci        obj->value_->AddWantRecord(std::make_shared<OHOS::AAFwk::Want>(want));
491bc03f14fSopenharmony_ci        return;
492bc03f14fSopenharmony_ci    }
493bc03f14fSopenharmony_ci
494bc03f14fSopenharmony_ci    std::string str;
495bc03f14fSopenharmony_ci    bool ret = GetValue(env, argv[1], str);
496bc03f14fSopenharmony_ci    if (!ret) {
497bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to GetValue!");
498bc03f14fSopenharmony_ci        return;
499bc03f14fSopenharmony_ci    }
500bc03f14fSopenharmony_ci    if (mimeType == MIMETYPE_TEXT_HTML) {
501bc03f14fSopenharmony_ci        obj->value_->AddHtmlRecord(str);
502bc03f14fSopenharmony_ci    } else if (mimeType == MIMETYPE_TEXT_PLAIN) {
503bc03f14fSopenharmony_ci        obj->value_->AddTextRecord(str);
504bc03f14fSopenharmony_ci    } else {
505bc03f14fSopenharmony_ci        obj->value_->AddUriRecord(OHOS::Uri(str));
506bc03f14fSopenharmony_ci    }
507bc03f14fSopenharmony_ci}
508bc03f14fSopenharmony_ci
509bc03f14fSopenharmony_cibool PasteDataNapi::SetStringProp(
510bc03f14fSopenharmony_ci    napi_env env, const std::string &propName, napi_value &propValueNapi, PasteDataRecord::Builder &builder)
511bc03f14fSopenharmony_ci{
512bc03f14fSopenharmony_ci    std::string propValue;
513bc03f14fSopenharmony_ci    bool ret = GetValue(env, propValueNapi, propValue);
514bc03f14fSopenharmony_ci    if (!ret) {
515bc03f14fSopenharmony_ci        return false;
516bc03f14fSopenharmony_ci    }
517bc03f14fSopenharmony_ci    if ((propName == "mimeType") && (propValue.size() <= MIMETYPE_MAX_SIZE)) {
518bc03f14fSopenharmony_ci        builder.SetMimeType(propValue);
519bc03f14fSopenharmony_ci    } else if ((propName == "htmlText") && (propValue.size() <= MAX_TEXT_LEN)) {
520bc03f14fSopenharmony_ci        builder.SetHtmlText(std::make_shared<std::string>(propValue));
521bc03f14fSopenharmony_ci    } else if ((propName == "plainText") && (propValue.size() <= MAX_TEXT_LEN)) {
522bc03f14fSopenharmony_ci        builder.SetPlainText(std::make_shared<std::string>(propValue));
523bc03f14fSopenharmony_ci    } else if (propName == "uri") {
524bc03f14fSopenharmony_ci        builder.SetUri(std::make_shared<OHOS::Uri>(Uri(propValue)));
525bc03f14fSopenharmony_ci    } else {
526bc03f14fSopenharmony_ci        return false;
527bc03f14fSopenharmony_ci    }
528bc03f14fSopenharmony_ci    return true;
529bc03f14fSopenharmony_ci}
530bc03f14fSopenharmony_ci
531bc03f14fSopenharmony_cistd::shared_ptr<MiscServices::PasteDataRecord> PasteDataNapi::ParseRecord(napi_env env, napi_value &recordNapi)
532bc03f14fSopenharmony_ci{
533bc03f14fSopenharmony_ci    napi_value propNames = nullptr;
534bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_property_names(env, recordNapi, &propNames));
535bc03f14fSopenharmony_ci    uint32_t propNameNums = 0;
536bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_array_length(env, propNames, &propNameNums));
537bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "propNameNums = %{public}d", propNameNums);
538bc03f14fSopenharmony_ci    PasteDataRecord::Builder builder("");
539bc03f14fSopenharmony_ci    for (uint32_t i = 0; i < propNameNums; i++) {
540bc03f14fSopenharmony_ci        napi_value propNameNapi = nullptr;
541bc03f14fSopenharmony_ci        NAPI_CALL(env, napi_get_element(env, propNames, i, &propNameNapi));
542bc03f14fSopenharmony_ci        size_t len = 0;
543bc03f14fSopenharmony_ci        char str[STR_MAX_SIZE] = { 0 };
544bc03f14fSopenharmony_ci        NAPI_CALL(env, napi_get_value_string_utf8(env, propNameNapi, str, STR_MAX_SIZE - STR_TAIL_LENGTH, &len));
545bc03f14fSopenharmony_ci        napi_value propValueNapi = nullptr;
546bc03f14fSopenharmony_ci        NAPI_CALL(env, napi_get_named_property(env, recordNapi, str, &propValueNapi));
547bc03f14fSopenharmony_ci        std::string propName = str;
548bc03f14fSopenharmony_ci        PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "propName = %{public}s,", propName.c_str());
549bc03f14fSopenharmony_ci
550bc03f14fSopenharmony_ci        if (propName == "mimeType" || propName == "htmlText" || propName == "plainText" || propName == "uri") {
551bc03f14fSopenharmony_ci            if (!SetStringProp(env, propName, propValueNapi, builder)) {
552bc03f14fSopenharmony_ci                return nullptr;
553bc03f14fSopenharmony_ci            }
554bc03f14fSopenharmony_ci        } else if (propName == "want") {
555bc03f14fSopenharmony_ci            AAFwk::Want want;
556bc03f14fSopenharmony_ci            if (OHOS::AppExecFwk::UnwrapWant(env, propValueNapi, want) != true) {
557bc03f14fSopenharmony_ci                return nullptr;
558bc03f14fSopenharmony_ci            }
559bc03f14fSopenharmony_ci            builder.SetWant(std::make_shared<AAFwk::Want>(want));
560bc03f14fSopenharmony_ci        } else if (propName == "pixelMap") {
561bc03f14fSopenharmony_ci            std::shared_ptr<PixelMap> pixelMap = PixelMapNapi::GetPixelMap(env, propValueNapi);
562bc03f14fSopenharmony_ci            if (pixelMap == nullptr) {
563bc03f14fSopenharmony_ci                return nullptr;
564bc03f14fSopenharmony_ci            }
565bc03f14fSopenharmony_ci            builder.SetPixelMap(pixelMap);
566bc03f14fSopenharmony_ci        } else if (propName == "data") {
567bc03f14fSopenharmony_ci            std::shared_ptr<MineCustomData> customData = PasteDataRecordNapi::GetNativeKvData(env, propValueNapi);
568bc03f14fSopenharmony_ci            if (customData == nullptr) {
569bc03f14fSopenharmony_ci                return nullptr;
570bc03f14fSopenharmony_ci            }
571bc03f14fSopenharmony_ci            builder.SetCustomData(customData);
572bc03f14fSopenharmony_ci        }
573bc03f14fSopenharmony_ci    }
574bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> result = builder.Build();
575bc03f14fSopenharmony_ci
576bc03f14fSopenharmony_ci    PasteDataRecordNapi *record = nullptr;
577bc03f14fSopenharmony_ci    napi_unwrap(env, recordNapi, reinterpret_cast<void **>(&record));
578bc03f14fSopenharmony_ci    if (record != nullptr && record->value_->GetEntryGetter() != nullptr) {
579bc03f14fSopenharmony_ci        result->SetEntryGetter(record->value_->GetEntryGetter());
580bc03f14fSopenharmony_ci        result->SetDelayRecordFlag(true);
581bc03f14fSopenharmony_ci    }
582bc03f14fSopenharmony_ci    if (record != nullptr && !record->value_->GetEntries().empty()) {
583bc03f14fSopenharmony_ci        for (const auto& pasteDataEntry : record->value_->GetEntries()) {
584bc03f14fSopenharmony_ci            result->AddEntry(pasteDataEntry->GetUtdId(), pasteDataEntry);
585bc03f14fSopenharmony_ci        }
586bc03f14fSopenharmony_ci    }
587bc03f14fSopenharmony_ci
588bc03f14fSopenharmony_ci    return result;
589bc03f14fSopenharmony_ci}
590bc03f14fSopenharmony_ci
591bc03f14fSopenharmony_civoid PasteDataNapi::AddRecord(napi_env env, napi_value argv, PasteDataNapi *obj)
592bc03f14fSopenharmony_ci{
593bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddPasteDataRecord!");
594bc03f14fSopenharmony_ci
595bc03f14fSopenharmony_ci    napi_valuetype valueType = napi_undefined;
596bc03f14fSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_typeof(env, argv, &valueType));
597bc03f14fSopenharmony_ci    NAPI_ASSERT_RETURN_VOID(env, valueType == napi_object, "Wrong argument type. Object expected.");
598bc03f14fSopenharmony_ci
599bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> pasteDataRecord = ParseRecord(env, argv);
600bc03f14fSopenharmony_ci    if (pasteDataRecord == nullptr) {
601bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "ParseRecord failed!");
602bc03f14fSopenharmony_ci        return;
603bc03f14fSopenharmony_ci    }
604bc03f14fSopenharmony_ci    obj->value_->AddRecord(*pasteDataRecord);
605bc03f14fSopenharmony_ci}
606bc03f14fSopenharmony_ci
607bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddRecord(napi_env env, napi_callback_info info)
608bc03f14fSopenharmony_ci{
609bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddRecord is called!");
610bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET2;
611bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET2] = { 0 };
612bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
613bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
614bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");
615bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
616bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
617bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
618bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddRecord object failed");
619bc03f14fSopenharmony_ci        return nullptr;
620bc03f14fSopenharmony_ci    }
621bc03f14fSopenharmony_ci
622bc03f14fSopenharmony_ci    if (argc == ARGC_TYPE_SET1) {
623bc03f14fSopenharmony_ci        AddRecord(env, argv[0], obj);
624bc03f14fSopenharmony_ci        return nullptr;
625bc03f14fSopenharmony_ci    }
626bc03f14fSopenharmony_ci    AddRecord(env, argv, argc, obj);
627bc03f14fSopenharmony_ci    return nullptr;
628bc03f14fSopenharmony_ci}
629bc03f14fSopenharmony_ci
630bc03f14fSopenharmony_cinapi_value PasteDataNapi::ReplaceRecordAt(napi_env env, napi_callback_info info)
631bc03f14fSopenharmony_ci{
632bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "ReplaceRecordAt is called!");
633bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET2;
634bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET2] = { 0 };
635bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
636bc03f14fSopenharmony_ci    napi_value result = nullptr;
637bc03f14fSopenharmony_ci    napi_get_boolean(env, false, &result);
638bc03f14fSopenharmony_ci
639bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL), result);
640bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET1, "Wrong number of arguments");
641bc03f14fSopenharmony_ci    napi_valuetype valueType = napi_undefined;
642bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_typeof(env, argv[0], &valueType), result);
643bc03f14fSopenharmony_ci    NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. number expected.");
644bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_typeof(env, argv[1], &valueType), result);
645bc03f14fSopenharmony_ci    NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
646bc03f14fSopenharmony_ci
647bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
648bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
649bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
650bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get ReplaceRecordAt object failed");
651bc03f14fSopenharmony_ci        return result;
652bc03f14fSopenharmony_ci    }
653bc03f14fSopenharmony_ci
654bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> pasteDataRecord = ParseRecord(env, argv[1]);
655bc03f14fSopenharmony_ci    if (pasteDataRecord == nullptr) {
656bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "ParseRecord failed!");
657bc03f14fSopenharmony_ci        return result;
658bc03f14fSopenharmony_ci    }
659bc03f14fSopenharmony_ci
660bc03f14fSopenharmony_ci    int64_t number = 0;
661bc03f14fSopenharmony_ci    napi_get_value_int64(env, argv[0], &number);
662bc03f14fSopenharmony_ci    bool ret = obj->value_->ReplaceRecordAt(number, pasteDataRecord);
663bc03f14fSopenharmony_ci    napi_get_boolean(env, ret, &result);
664bc03f14fSopenharmony_ci
665bc03f14fSopenharmony_ci    return result;
666bc03f14fSopenharmony_ci}
667bc03f14fSopenharmony_ci
668bc03f14fSopenharmony_cinapi_value PasteDataNapi::ReplaceRecord(napi_env env, napi_callback_info info)
669bc03f14fSopenharmony_ci{
670bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "ReplaceRecord is called!");
671bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET2;
672bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET2] = { 0 };
673bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
674bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
675bc03f14fSopenharmony_ci    if (!CheckExpression(env, argc > ARGC_TYPE_SET1, JSErrorCode::INVALID_PARAMETERS,
676bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be greater than one.") ||
677bc03f14fSopenharmony_ci        !CheckArgsType(env, argv[0], napi_number, "The type of mimeType must be number.")) {
678bc03f14fSopenharmony_ci        return nullptr;
679bc03f14fSopenharmony_ci    }
680bc03f14fSopenharmony_ci
681bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
682bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
683bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
684bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get ReplaceRecord object failed");
685bc03f14fSopenharmony_ci        return nullptr;
686bc03f14fSopenharmony_ci    }
687bc03f14fSopenharmony_ci    uint32_t index = 0;
688bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_value_uint32(env, argv[0], &index));
689bc03f14fSopenharmony_ci    if (!CheckExpression(env, index < obj->value_->GetRecordCount(), JSErrorCode::OUT_OF_RANGE,
690bc03f14fSopenharmony_ci        "index out of range.")) {
691bc03f14fSopenharmony_ci        return nullptr;
692bc03f14fSopenharmony_ci    }
693bc03f14fSopenharmony_ci
694bc03f14fSopenharmony_ci    if (!CheckArgsType(env, argv[1], napi_object, "The type of record must be PasteDataRecord.")) {
695bc03f14fSopenharmony_ci        return nullptr;
696bc03f14fSopenharmony_ci    }
697bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> pasteDataRecord = ParseRecord(env, argv[1]);
698bc03f14fSopenharmony_ci    if (!CheckExpression(env, pasteDataRecord != nullptr, JSErrorCode::INVALID_PARAMETERS,
699bc03f14fSopenharmony_ci        "Parameter error. The type of PasteDataRecord cannot be nullptr.")) {
700bc03f14fSopenharmony_ci        return nullptr;
701bc03f14fSopenharmony_ci    }
702bc03f14fSopenharmony_ci    obj->value_->ReplaceRecordAt(index, pasteDataRecord);
703bc03f14fSopenharmony_ci    return nullptr;
704bc03f14fSopenharmony_ci}
705bc03f14fSopenharmony_ci
706bc03f14fSopenharmony_cinapi_value PasteDataNapi::AddWantRecord(napi_env env, napi_callback_info info)
707bc03f14fSopenharmony_ci{
708bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "AddWantRecord is called!");
709bc03f14fSopenharmony_ci    size_t argc = 1;
710bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
711bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
712bc03f14fSopenharmony_ci
713bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
714bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc > ARGC_TYPE_SET0, "Wrong number of arguments");
715bc03f14fSopenharmony_ci
716bc03f14fSopenharmony_ci    napi_valuetype valueType = napi_undefined;
717bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
718bc03f14fSopenharmony_ci    NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
719bc03f14fSopenharmony_ci    OHOS::AAFwk::Want want;
720bc03f14fSopenharmony_ci    if (!OHOS::AppExecFwk::UnwrapWant(env, argv[0], want)) {
721bc03f14fSopenharmony_ci        return nullptr;
722bc03f14fSopenharmony_ci    }
723bc03f14fSopenharmony_ci
724bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
725bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
726bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
727bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get AddWantRecord object failed");
728bc03f14fSopenharmony_ci        return nullptr;
729bc03f14fSopenharmony_ci    }
730bc03f14fSopenharmony_ci
731bc03f14fSopenharmony_ci    obj->value_->AddWantRecord(std::make_shared<OHOS::AAFwk::Want>(want));
732bc03f14fSopenharmony_ci    return nullptr;
733bc03f14fSopenharmony_ci}
734bc03f14fSopenharmony_ci
735bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetPrimaryWant(napi_env env, napi_callback_info info)
736bc03f14fSopenharmony_ci{
737bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetPrimaryWant is called!");
738bc03f14fSopenharmony_ci    size_t argc = 1;
739bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
740bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
741bc03f14fSopenharmony_ci
742bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
743bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
744bc03f14fSopenharmony_ci
745bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
746bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
747bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
748bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetPrimaryWant object failed");
749bc03f14fSopenharmony_ci        return nullptr;
750bc03f14fSopenharmony_ci    }
751bc03f14fSopenharmony_ci
752bc03f14fSopenharmony_ci    std::shared_ptr<OHOS::AAFwk::Want> want = obj->value_->GetPrimaryWant();
753bc03f14fSopenharmony_ci    if (!want) {
754bc03f14fSopenharmony_ci        return nullptr;
755bc03f14fSopenharmony_ci    }
756bc03f14fSopenharmony_ci    return OHOS::AppExecFwk::WrapWant(env, *want);
757bc03f14fSopenharmony_ci}
758bc03f14fSopenharmony_ci
759bc03f14fSopenharmony_cibool PasteDataNapi::SetNapiProperty(napi_env env, const PasteDataProperty &property, napi_value &nProperty)
760bc03f14fSopenharmony_ci{
761bc03f14fSopenharmony_ci    napi_value value = nullptr;
762bc03f14fSopenharmony_ci    napi_value arr = nullptr;
763bc03f14fSopenharmony_ci    int count = 0;
764bc03f14fSopenharmony_ci
765bc03f14fSopenharmony_ci    // additions : {[key: string]: object}
766bc03f14fSopenharmony_ci    value = OHOS::AppExecFwk::WrapWantParams(env, property.additions);
767bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "additions", value);
768bc03f14fSopenharmony_ci
769bc03f14fSopenharmony_ci    // mimeTypes: Array<string>
770bc03f14fSopenharmony_ci    napi_create_array(env, &arr);
771bc03f14fSopenharmony_ci    for (auto vec : property.mimeTypes) {
772bc03f14fSopenharmony_ci        napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &value);
773bc03f14fSopenharmony_ci        napi_set_element(env, arr, count, value);
774bc03f14fSopenharmony_ci        count++;
775bc03f14fSopenharmony_ci    }
776bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "mimeTypes", arr);
777bc03f14fSopenharmony_ci
778bc03f14fSopenharmony_ci    // tag: string
779bc03f14fSopenharmony_ci    napi_create_string_utf8(env, property.tag.c_str(), NAPI_AUTO_LENGTH, &value);
780bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "tag", value);
781bc03f14fSopenharmony_ci
782bc03f14fSopenharmony_ci    // timestamp: number
783bc03f14fSopenharmony_ci    napi_create_int64(env, property.timestamp, &value);
784bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "timestamp", value);
785bc03f14fSopenharmony_ci
786bc03f14fSopenharmony_ci    // localOnly: boolean
787bc03f14fSopenharmony_ci    napi_get_boolean(env, property.localOnly, &value);
788bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "localOnly", value);
789bc03f14fSopenharmony_ci
790bc03f14fSopenharmony_ci    napi_create_int32(env, static_cast<int32_t>(property.shareOption), &value);
791bc03f14fSopenharmony_ci    napi_set_named_property(env, nProperty, "shareOption", value);
792bc03f14fSopenharmony_ci    return true;
793bc03f14fSopenharmony_ci}
794bc03f14fSopenharmony_ci
795bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetProperty(napi_env env, napi_callback_info info)
796bc03f14fSopenharmony_ci{
797bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetProperty is called!");
798bc03f14fSopenharmony_ci    size_t argc = 1;
799bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
800bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
801bc03f14fSopenharmony_ci
802bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
803bc03f14fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_TYPE_SET0, "Wrong number of arguments");
804bc03f14fSopenharmony_ci
805bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
806bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
807bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
808bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Get GetProperty object failed");
809bc03f14fSopenharmony_ci        return nullptr;
810bc03f14fSopenharmony_ci    }
811bc03f14fSopenharmony_ci    PasteDataProperty property = obj->value_->GetProperty();
812bc03f14fSopenharmony_ci    napi_value nProperty = nullptr;
813bc03f14fSopenharmony_ci    napi_create_object(env, &nProperty);
814bc03f14fSopenharmony_ci    if (!SetNapiProperty(env, property, nProperty)) {
815bc03f14fSopenharmony_ci        return nullptr;
816bc03f14fSopenharmony_ci    }
817bc03f14fSopenharmony_ci    return nProperty;
818bc03f14fSopenharmony_ci}
819bc03f14fSopenharmony_ci
820bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetRecordAt(napi_env env, napi_callback_info info)
821bc03f14fSopenharmony_ci{
822bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetRecordAt is called!");
823bc03f14fSopenharmony_ci    uint32_t index = 0;
824bc03f14fSopenharmony_ci    PasteDataNapi *obj = RemoveAndGetRecordCommon(env, info, index);
825bc03f14fSopenharmony_ci    if (obj == nullptr) {
826bc03f14fSopenharmony_ci        return nullptr;
827bc03f14fSopenharmony_ci    }
828bc03f14fSopenharmony_ci
829bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> record = obj->value_->GetRecordAt(index);
830bc03f14fSopenharmony_ci    napi_value instance = nullptr;
831bc03f14fSopenharmony_ci    PasteDataRecordNapi::NewInstanceByRecord(env, instance, record);
832bc03f14fSopenharmony_ci    return instance;
833bc03f14fSopenharmony_ci}
834bc03f14fSopenharmony_ci
835bc03f14fSopenharmony_cinapi_value PasteDataNapi::GetRecord(napi_env env, napi_callback_info info)
836bc03f14fSopenharmony_ci{
837bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "GetRecord is called!");
838bc03f14fSopenharmony_ci    uint32_t index = 0;
839bc03f14fSopenharmony_ci    PasteDataNapi *obj = RemoveAndGetRecordCommon(env, info, index);
840bc03f14fSopenharmony_ci    if (obj == nullptr || !CheckExpression(env, index < obj->value_->GetRecordCount(), JSErrorCode::OUT_OF_RANGE,
841bc03f14fSopenharmony_ci        "index out of range.")) {
842bc03f14fSopenharmony_ci        return nullptr;
843bc03f14fSopenharmony_ci    }
844bc03f14fSopenharmony_ci
845bc03f14fSopenharmony_ci    std::shared_ptr<PasteDataRecord> record = obj->value_->GetRecordAt(index);
846bc03f14fSopenharmony_ci    napi_value instance = nullptr;
847bc03f14fSopenharmony_ci    PasteDataRecordNapi::NewInstanceByRecord(env, instance, record);
848bc03f14fSopenharmony_ci    return instance;
849bc03f14fSopenharmony_ci}
850bc03f14fSopenharmony_ci
851bc03f14fSopenharmony_civoid PasteDataNapi::SetProperty(napi_env env, napi_value in, PasteDataNapi *obj)
852bc03f14fSopenharmony_ci{
853bc03f14fSopenharmony_ci    napi_value propertyNames = nullptr;
854bc03f14fSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_get_property_names(env, in, &propertyNames));
855bc03f14fSopenharmony_ci    uint32_t propertyNamesNum = 0;
856bc03f14fSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_get_array_length(env, propertyNames, &propertyNamesNum));
857bc03f14fSopenharmony_ci    bool localOnlyValue = false;
858bc03f14fSopenharmony_ci    int32_t shareOptionValue = ShareOption::CrossDevice;
859bc03f14fSopenharmony_ci    for (uint32_t i = 0; i < propertyNamesNum; i++) {
860bc03f14fSopenharmony_ci        napi_value propertyNameNapi = nullptr;
861bc03f14fSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_get_element(env, propertyNames, i, &propertyNameNapi));
862bc03f14fSopenharmony_ci        size_t len = 0;
863bc03f14fSopenharmony_ci        char str[STR_MAX_SIZE] = { 0 };
864bc03f14fSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_get_value_string_utf8(env, propertyNameNapi, str, STR_MAX_SIZE, &len));
865bc03f14fSopenharmony_ci        std::string propertyName = str;
866bc03f14fSopenharmony_ci        napi_value propertyNameValueNapi = nullptr;
867bc03f14fSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, in, str, &propertyNameValueNapi));
868bc03f14fSopenharmony_ci        if (propertyName == "localOnly") {
869bc03f14fSopenharmony_ci            NAPI_CALL_RETURN_VOID(env, napi_get_value_bool(env, propertyNameValueNapi, &localOnlyValue));
870bc03f14fSopenharmony_ci            PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "localOnlyValue = %{public}d", localOnlyValue);
871bc03f14fSopenharmony_ci        }
872bc03f14fSopenharmony_ci        if (propertyName == "shareOption") {
873bc03f14fSopenharmony_ci            NAPI_CALL_RETURN_VOID(env, napi_get_value_int32(env, propertyNameValueNapi, &shareOptionValue));
874bc03f14fSopenharmony_ci            PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "shareOptionValue = %{public}d", shareOptionValue);
875bc03f14fSopenharmony_ci        }
876bc03f14fSopenharmony_ci        if (propertyName == "tag") {
877bc03f14fSopenharmony_ci            char tagValue[STR_MAX_SIZE] = { 0 };
878bc03f14fSopenharmony_ci            size_t tagValueLen = 0;
879bc03f14fSopenharmony_ci            NAPI_CALL_RETURN_VOID(
880bc03f14fSopenharmony_ci                env, napi_get_value_string_utf8(env, propertyNameValueNapi, tagValue, STR_MAX_SIZE, &tagValueLen));
881bc03f14fSopenharmony_ci            PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "tagValue = %{public}s", tagValue);
882bc03f14fSopenharmony_ci            std::string tagValueStr = tagValue;
883bc03f14fSopenharmony_ci            obj->value_->SetTag(tagValueStr);
884bc03f14fSopenharmony_ci        }
885bc03f14fSopenharmony_ci        if (propertyName == "additions") {
886bc03f14fSopenharmony_ci            AAFwk::WantParams additions;
887bc03f14fSopenharmony_ci            bool ret = OHOS::AppExecFwk::UnwrapWantParams(env, propertyNameValueNapi, additions);
888bc03f14fSopenharmony_ci            PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "additions ret = %{public}d", ret);
889bc03f14fSopenharmony_ci            obj->value_->SetAdditions(additions);
890bc03f14fSopenharmony_ci        }
891bc03f14fSopenharmony_ci    }
892bc03f14fSopenharmony_ci    localOnlyValue = shareOptionValue == ShareOption::CrossDevice ? false : true;
893bc03f14fSopenharmony_ci    PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "localOnly final Value = %{public}d", localOnlyValue);
894bc03f14fSopenharmony_ci    obj->value_->SetLocalOnly(localOnlyValue);
895bc03f14fSopenharmony_ci    obj->value_->SetShareOption(static_cast<ShareOption>(shareOptionValue));
896bc03f14fSopenharmony_ci}
897bc03f14fSopenharmony_ci
898bc03f14fSopenharmony_cibool PasteDataNapi::IsProperty(napi_env env, napi_value in)
899bc03f14fSopenharmony_ci{
900bc03f14fSopenharmony_ci    napi_valuetype valueType = napi_undefined;
901bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_typeof(env, in, &valueType), false);
902bc03f14fSopenharmony_ci    if (valueType != napi_object) {
903bc03f14fSopenharmony_ci        return false;
904bc03f14fSopenharmony_ci    }
905bc03f14fSopenharmony_ci    napi_value propertyNames = nullptr;
906bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_get_property_names(env, in, &propertyNames), false);
907bc03f14fSopenharmony_ci    uint32_t propertyNamesNum = 0;
908bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_get_array_length(env, propertyNames, &propertyNamesNum), false);
909bc03f14fSopenharmony_ci    bool hasProperty = false;
910bc03f14fSopenharmony_ci    const char *key[] = { "additions", "mimeTypes", "tag", "timestamp", "localOnly", "shareOption" };
911bc03f14fSopenharmony_ci    const napi_valuetype type[] = { napi_object, napi_object, napi_string, napi_number, napi_boolean, napi_number };
912bc03f14fSopenharmony_ci    napi_value propertyValue = nullptr;
913bc03f14fSopenharmony_ci    for (uint32_t i = 0; i < propertyNamesNum; i++) {
914bc03f14fSopenharmony_ci        NAPI_CALL_BASE(env, napi_has_named_property(env, in, key[i], &hasProperty), false);
915bc03f14fSopenharmony_ci        if (!hasProperty) {
916bc03f14fSopenharmony_ci            return false;
917bc03f14fSopenharmony_ci        }
918bc03f14fSopenharmony_ci        NAPI_CALL_BASE(env, napi_get_named_property(env, in, key[i], &propertyValue), false);
919bc03f14fSopenharmony_ci        if (i == ARGC_TYPE_SET1) {
920bc03f14fSopenharmony_ci            bool isArray = false;
921bc03f14fSopenharmony_ci            NAPI_CALL_BASE(env, napi_is_array(env, propertyValue, &isArray), false);
922bc03f14fSopenharmony_ci            if (!isArray) {
923bc03f14fSopenharmony_ci                return false;
924bc03f14fSopenharmony_ci            }
925bc03f14fSopenharmony_ci            continue;
926bc03f14fSopenharmony_ci        }
927bc03f14fSopenharmony_ci        NAPI_CALL_BASE(env, napi_typeof(env, propertyValue, &valueType), false);
928bc03f14fSopenharmony_ci        if (valueType != type[i]) {
929bc03f14fSopenharmony_ci            return false;
930bc03f14fSopenharmony_ci        }
931bc03f14fSopenharmony_ci    }
932bc03f14fSopenharmony_ci    return true;
933bc03f14fSopenharmony_ci}
934bc03f14fSopenharmony_ci
935bc03f14fSopenharmony_cinapi_value PasteDataNapi::SetProperty(napi_env env, napi_callback_info info)
936bc03f14fSopenharmony_ci{
937bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "SetProperty is called!");
938bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET1;
939bc03f14fSopenharmony_ci    napi_value argv[ARGC_TYPE_SET1] = { 0 };
940bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
941bc03f14fSopenharmony_ci
942bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
943bc03f14fSopenharmony_ci    if (!CheckExpression(env, argc > ARGC_TYPE_SET0, JSErrorCode::INVALID_PARAMETERS,
944bc03f14fSopenharmony_ci        "Parameter error. The number of arguments must be greater than zero.") ||
945bc03f14fSopenharmony_ci        !CheckExpression(env, IsProperty(env, argv[0]), JSErrorCode::INVALID_PARAMETERS,
946bc03f14fSopenharmony_ci            "Parameter error. The type of property must be PasteDataProperty.")) {
947bc03f14fSopenharmony_ci        return nullptr;
948bc03f14fSopenharmony_ci    }
949bc03f14fSopenharmony_ci
950bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
951bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
952bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr)) {
953bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_unwrap failed");
954bc03f14fSopenharmony_ci        return nullptr;
955bc03f14fSopenharmony_ci    }
956bc03f14fSopenharmony_ci    SetProperty(env, argv[0], obj);
957bc03f14fSopenharmony_ci    return nullptr;
958bc03f14fSopenharmony_ci}
959bc03f14fSopenharmony_ci
960bc03f14fSopenharmony_cinapi_value PasteDataNapi::PasteDataInit(napi_env env, napi_value exports)
961bc03f14fSopenharmony_ci{
962bc03f14fSopenharmony_ci    napi_status status = napi_ok;
963bc03f14fSopenharmony_ci    napi_property_descriptor descriptors[] = { DECLARE_NAPI_FUNCTION("addHtmlRecord", AddHtmlRecord),
964bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("addWantRecord", AddWantRecord), DECLARE_NAPI_FUNCTION("addRecord", AddRecord),
965bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("addTextRecord", AddTextRecord), DECLARE_NAPI_FUNCTION("addUriRecord", AddUriRecord),
966bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("addPixelMapRecord", AddPixelMapRecord),
967bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getMimeTypes", GetMimeTypes), DECLARE_NAPI_FUNCTION("getPrimaryHtml", GetPrimaryHtml),
968bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getPrimaryWant", GetPrimaryWant),
969bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getPrimaryMimeType", GetPrimaryMimeType),
970bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getPrimaryText", GetPrimaryText), DECLARE_NAPI_FUNCTION("getPrimaryUri", GetPrimaryUri),
971bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getPrimaryPixelMap", GetPrimaryPixelMap),
972bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getProperty", GetProperty), DECLARE_NAPI_FUNCTION("getRecordAt", GetRecordAt),
973bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getRecord", GetRecord), DECLARE_NAPI_FUNCTION("getRecordCount", GetRecordCount),
974bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getTag", GetTag), DECLARE_NAPI_FUNCTION("hasMimeType", HasMimeType),
975bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("hasType", HasType), DECLARE_NAPI_FUNCTION("removeRecordAt", RemoveRecordAt),
976bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("removeRecord", RemoveRecord), DECLARE_NAPI_FUNCTION("replaceRecordAt", ReplaceRecordAt),
977bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("replaceRecord", ReplaceRecord), DECLARE_NAPI_FUNCTION("setProperty", SetProperty),
978bc03f14fSopenharmony_ci        DECLARE_NAPI_FUNCTION("pasteStart", PasteStart), DECLARE_NAPI_FUNCTION("pasteComplete", PasteComplete) };
979bc03f14fSopenharmony_ci
980bc03f14fSopenharmony_ci    napi_value constructor;
981bc03f14fSopenharmony_ci    napi_define_class(env, "PasteData", NAPI_AUTO_LENGTH, New, nullptr,
982bc03f14fSopenharmony_ci        sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &constructor);
983bc03f14fSopenharmony_ci    if (status != napi_ok) {
984bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "Failed to define class at Init");
985bc03f14fSopenharmony_ci        return nullptr;
986bc03f14fSopenharmony_ci    }
987bc03f14fSopenharmony_ci
988bc03f14fSopenharmony_ci    status = napi_create_reference(env, constructor, 1, &g_pasteData);
989bc03f14fSopenharmony_ci    if (status != napi_ok) {
990bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "PasteDataNapi Init create reference failed");
991bc03f14fSopenharmony_ci        return nullptr;
992bc03f14fSopenharmony_ci    }
993bc03f14fSopenharmony_ci    napi_set_named_property(env, exports, "PasteData", constructor);
994bc03f14fSopenharmony_ci    return exports;
995bc03f14fSopenharmony_ci}
996bc03f14fSopenharmony_ci
997bc03f14fSopenharmony_civoid PasteDataNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
998bc03f14fSopenharmony_ci{
999bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "Destructor");
1000bc03f14fSopenharmony_ci    PasteDataNapi *obj = static_cast<PasteDataNapi *>(nativeObject);
1001bc03f14fSopenharmony_ci    delete obj;
1002bc03f14fSopenharmony_ci}
1003bc03f14fSopenharmony_ci
1004bc03f14fSopenharmony_cinapi_value PasteDataNapi::New(napi_env env, napi_callback_info info)
1005bc03f14fSopenharmony_ci{
1006bc03f14fSopenharmony_ci    size_t argc = ARGC_TYPE_SET1;
1007bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
1008bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
1009bc03f14fSopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1010bc03f14fSopenharmony_ci
1011bc03f14fSopenharmony_ci    // get native object
1012bc03f14fSopenharmony_ci    PasteDataNapi *obj = new PasteDataNapi();
1013bc03f14fSopenharmony_ci    obj->env_ = env;
1014bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_wrap(env, thisVar, obj, PasteDataNapi::Destructor,
1015bc03f14fSopenharmony_ci                       nullptr, // finalize_hint
1016bc03f14fSopenharmony_ci                       nullptr));
1017bc03f14fSopenharmony_ci    return thisVar;
1018bc03f14fSopenharmony_ci}
1019bc03f14fSopenharmony_ci
1020bc03f14fSopenharmony_cinapi_status PasteDataNapi::NewInstance(napi_env env, napi_value &instance)
1021bc03f14fSopenharmony_ci{
1022bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "enter");
1023bc03f14fSopenharmony_ci    napi_status status;
1024bc03f14fSopenharmony_ci    napi_value constructor;
1025bc03f14fSopenharmony_ci    status = napi_get_reference_value(env, g_pasteData, &constructor);
1026bc03f14fSopenharmony_ci    if (status != napi_ok) {
1027bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "get reference failed");
1028bc03f14fSopenharmony_ci        return status;
1029bc03f14fSopenharmony_ci    }
1030bc03f14fSopenharmony_ci
1031bc03f14fSopenharmony_ci    status = napi_new_instance(env, constructor, 0, nullptr, &instance);
1032bc03f14fSopenharmony_ci    if (status != napi_ok) {
1033bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "new instance failed");
1034bc03f14fSopenharmony_ci        return status;
1035bc03f14fSopenharmony_ci    }
1036bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "new instance ok");
1037bc03f14fSopenharmony_ci    return napi_ok;
1038bc03f14fSopenharmony_ci}
1039bc03f14fSopenharmony_ci
1040bc03f14fSopenharmony_cibool PasteDataNapi::IsPasteData(napi_env env, napi_value in)
1041bc03f14fSopenharmony_ci{
1042bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "enter");
1043bc03f14fSopenharmony_ci    napi_valuetype type = napi_undefined;
1044bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_typeof(env, in, &type), false);
1045bc03f14fSopenharmony_ci    if (type != napi_object) {
1046bc03f14fSopenharmony_ci        return false;
1047bc03f14fSopenharmony_ci    }
1048bc03f14fSopenharmony_ci    napi_value constructor;
1049bc03f14fSopenharmony_ci    bool isPasteData = false;
1050bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_get_reference_value(env, g_pasteData, &constructor), false);
1051bc03f14fSopenharmony_ci    NAPI_CALL_BASE(env, napi_instanceof(env, in, constructor, &isPasteData), false);
1052bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "isPasteData is [%{public}d]", isPasteData);
1053bc03f14fSopenharmony_ci    return isPasteData;
1054bc03f14fSopenharmony_ci}
1055bc03f14fSopenharmony_ci
1056bc03f14fSopenharmony_cinapi_value PasteDataNapi::PasteStart(napi_env env, napi_callback_info info)
1057bc03f14fSopenharmony_ci{
1058bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
1059bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
1060bc03f14fSopenharmony_ci    size_t argc = 1;
1061bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
1062bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1063bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
1064bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr) || (obj->value_ == nullptr)) {
1065bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_unwrap failed");
1066bc03f14fSopenharmony_ci        return nullptr;
1067bc03f14fSopenharmony_ci    }
1068bc03f14fSopenharmony_ci    std::string pasteId = obj->value_->GetPasteId();
1069bc03f14fSopenharmony_ci    PasteboardClient::GetInstance()->PasteStart(pasteId);
1070bc03f14fSopenharmony_ci    return nullptr;
1071bc03f14fSopenharmony_ci}
1072bc03f14fSopenharmony_ci
1073bc03f14fSopenharmony_cinapi_value PasteDataNapi::PasteComplete(napi_env env, napi_callback_info info)
1074bc03f14fSopenharmony_ci{
1075bc03f14fSopenharmony_ci    napi_value thisVar = nullptr;
1076bc03f14fSopenharmony_ci    PasteDataNapi *obj = nullptr;
1077bc03f14fSopenharmony_ci    size_t argc = 1;
1078bc03f14fSopenharmony_ci    napi_value argv[1] = { 0 };
1079bc03f14fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1080bc03f14fSopenharmony_ci    napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
1081bc03f14fSopenharmony_ci    if ((status != napi_ok) || (obj == nullptr) || (obj->value_ == nullptr)) {
1082bc03f14fSopenharmony_ci        PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_unwrap failed");
1083bc03f14fSopenharmony_ci        return nullptr;
1084bc03f14fSopenharmony_ci    }
1085bc03f14fSopenharmony_ci    std::string deviceId = obj->value_->GetDeviceId();
1086bc03f14fSopenharmony_ci    std::string pasteId = obj->value_->GetPasteId();
1087bc03f14fSopenharmony_ci    PasteboardClient::GetInstance()->PasteComplete(deviceId, pasteId);
1088bc03f14fSopenharmony_ci    return nullptr;
1089bc03f14fSopenharmony_ci}
1090bc03f14fSopenharmony_ci} // namespace MiscServicesNapi
1091bc03f14fSopenharmony_ci} // namespace OHOS
1092