1 /*
2 * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "pasteboard_common.h"
16
17 #include "napi_common_want.h"
18 #include "paste_data_record.h"
19 #include "pasteboard_hilog.h"
20 #include "pasteboard_js_err.h"
21 #include "pixel_map_napi.h"
22
23 namespace OHOS {
24 namespace MiscServicesNapi {
25 using namespace OHOS::MiscServices;
26 const size_t ARGC_TYPE_SET2 = 2;
27 constexpr size_t STR_TAIL_LENGTH = 1;
28 constexpr int32_t MIMETYPE_MAX_SIZE = 1024;
29
GetCallbackErrorValue(napi_env env,int32_t errorCode)30 napi_value GetCallbackErrorValue(napi_env env, int32_t errorCode)
31 {
32 napi_value result = nullptr;
33 napi_value eCode = nullptr;
34 NAPI_CALL(env, napi_create_int32(env, errorCode, &eCode));
35 NAPI_CALL(env, napi_create_object(env, &result));
36 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
37 return result;
38 }
39
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value * results)40 void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value *results)
41 {
42 if (results == nullptr) {
43 return;
44 }
45 napi_value callback = nullptr;
46 napi_value resultout = nullptr;
47 napi_get_reference_value(env, callbackIn, &callback);
48 napi_call_function(env, nullptr, callback, ARGC_TYPE_SET2, results, &resultout);
49 }
50
NapiGetNull(napi_env env)51 napi_value NapiGetNull(napi_env env)
52 {
53 napi_value result = nullptr;
54 napi_get_null(env, &result);
55 return result;
56 }
57
CreateNapiNumber(napi_env env,int32_t num)58 napi_value CreateNapiNumber(napi_env env, int32_t num)
59 {
60 napi_value value = nullptr;
61 napi_create_int32(env, num, &value);
62 return value;
63 }
64
CreateNapiString(napi_env env,std::string str)65 napi_value CreateNapiString(napi_env env, std::string str)
66 {
67 napi_value value = nullptr;
68 napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &value);
69 return value;
70 }
71
72 /* napi_value <-> std::string */
GetValue(napi_env env,napi_value in,std::string & out)73 bool GetValue(napi_env env, napi_value in, std::string &out)
74 {
75 napi_valuetype type = napi_undefined;
76 NAPI_CALL_BASE(env, napi_typeof(env, in, &type), false);
77 NAPI_ASSERT_BASE(env, type == napi_string, "Wrong argument type. String expected.", false);
78
79 size_t len = 0;
80 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, in, nullptr, 0, &len), false);
81 if (len < 0) {
82 return false;
83 }
84
85 size_t length = 0;
86 out.resize(len + STR_TAIL_LENGTH, 0);
87 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, in, out.data(), len + STR_TAIL_LENGTH, &length), false);
88 out.resize(len);
89
90 return true;
91 }
92
CheckArgsType(napi_env env,napi_value in,napi_valuetype expectedType,const char * message)93 bool CheckArgsType(napi_env env, napi_value in, napi_valuetype expectedType, const char *message)
94 {
95 napi_valuetype type = napi_undefined;
96 NAPI_CALL_BASE(env, napi_typeof(env, in, &type), false);
97 int32_t errCode = static_cast<int32_t>(JSErrorCode::INVALID_PARAMETERS);
98 if (type != expectedType) {
99 napi_throw_error(env, std::to_string(errCode).c_str(), message);
100 return false;
101 }
102 return true;
103 }
104
CheckExpression(napi_env env,bool flag,MiscServices::JSErrorCode errCode,const char * message)105 bool CheckExpression(napi_env env, bool flag, MiscServices::JSErrorCode errCode, const char *message)
106 {
107 if (!flag) {
108 NAPI_CALL_BASE(
109 env, napi_throw_error(env, std::to_string(static_cast<int32_t>(errCode)).c_str(), message), false);
110 return false;
111 }
112 return true;
113 }
114
115 // Check Parameters of CreateData, CreateRecord and AddRecord
CheckArgs(napi_env env,napi_value * argv,size_t argc,std::string & mimeType)116 bool CheckArgs(napi_env env, napi_value *argv, size_t argc, std::string &mimeType)
117 {
118 // 2: CreateRecord, CreateRecord and AddRecord has 2 args.
119 if (!CheckExpression(env, argc >= ARGC_TYPE_SET2, JSErrorCode::INVALID_PARAMETERS,
120 "Parameter error. Wrong number of arguments.") ||
121 !CheckArgsType(env, argv[0], napi_string, "Parameter error. The type of mimeType must be string.")) {
122 return false;
123 }
124
125 bool ret = GetValue(env, argv[0], mimeType);
126 if (!ret) {
127 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "GetValue failed");
128 return false;
129 }
130 if (!CheckExpression(
131 env, mimeType != "", JSErrorCode::INVALID_PARAMETERS, "Parameter error. mimeType cannot be empty.") ||
132 !CheckExpression(env, mimeType.size() <= MIMETYPE_MAX_SIZE, JSErrorCode::INVALID_PARAMETERS,
133 "Parameter error. The length of mimeType cannot be greater than 1024 bytes.")) {
134 return false;
135 }
136 const char *message = "Parameter error. The value does not match mimeType correctly.";
137
138 if (mimeType == MIMETYPE_TEXT_URI || mimeType == MIMETYPE_TEXT_PLAIN || mimeType == MIMETYPE_TEXT_HTML) {
139 if (!CheckArgsType(env, argv[1], napi_string, message)) {
140 return false;
141 }
142 } else if (mimeType == MIMETYPE_PIXELMAP) {
143 if (!CheckExpression(env, Media::PixelMapNapi::GetPixelMap(env, argv[1]) != nullptr,
144 JSErrorCode::INVALID_PARAMETERS, message)) {
145 return false;
146 }
147 } else if (mimeType == MIMETYPE_TEXT_WANT) {
148 AAFwk::Want want;
149 ret = OHOS::AppExecFwk::UnwrapWant(env, argv[1], want);
150 if (!CheckExpression(env, ret, JSErrorCode::INVALID_PARAMETERS, message)) {
151 return false;
152 }
153 } else {
154 bool isArrayBuffer = false;
155 NAPI_CALL_BASE(env, napi_is_arraybuffer(env, argv[1], &isArrayBuffer), false);
156 if (!CheckExpression(env, isArrayBuffer, JSErrorCode::INVALID_PARAMETERS, message)) {
157 return false;
158 }
159 }
160 return true;
161 }
162
163 } // namespace MiscServicesNapi
164 } // namespace OHOS