1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "ScreenLockJsUtil"
16
17 #include "screenlock_js_util.h"
18
19 namespace OHOS::ScreenLock {
Convert2String(const napi_env env,napi_value jsString)20 std::string ScreenLockJsUtil::Convert2String(const napi_env env, napi_value jsString)
21 {
22 size_t maxLen = ScreenLockJsUtil::MAX_LEN;
23 napi_status status = napi_get_value_string_utf8(env, jsString, NULL, 0, &maxLen);
24 if (status != napi_ok) {
25 GET_AND_THROW_LAST_ERROR((env));
26 maxLen = ScreenLockJsUtil::MAX_LEN;
27 }
28 if (maxLen == 0) {
29 return std::string();
30 }
31 char *buf = new char[maxLen + 1];
32 if (buf == nullptr) {
33 return std::string();
34 }
35 size_t len = 0;
36 status = napi_get_value_string_utf8(env, jsString, buf, maxLen + 1, &len);
37 if (status != napi_ok) {
38 GET_AND_THROW_LAST_ERROR((env));
39 }
40 buf[len] = 0;
41 std::string value(buf);
42 delete[] buf;
43 return value;
44 }
45 } // namespace OHOS::ScreenLock