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