15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "napi_utils.h" 175ccb8f90Sopenharmony_ci#include "power_log.h" 185ccb8f90Sopenharmony_ci 195ccb8f90Sopenharmony_cinamespace OHOS { 205ccb8f90Sopenharmony_cinamespace PowerMgr { 215ccb8f90Sopenharmony_cinamespace { 225ccb8f90Sopenharmony_ciconstexpr uint32_t MAX_SIZE = 255; 235ccb8f90Sopenharmony_ci} 245ccb8f90Sopenharmony_cinapi_value NapiUtils::GetCallbackInfo(napi_env& env, napi_callback_info& info, size_t& argc, napi_value argv[]) 255ccb8f90Sopenharmony_ci{ 265ccb8f90Sopenharmony_ci napi_value thisVar = nullptr; 275ccb8f90Sopenharmony_ci void* data = nullptr; 285ccb8f90Sopenharmony_ci if (napi_ok != napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)) { 295ccb8f90Sopenharmony_ci POWER_HILOGW(COMP_FWK, "Failed to get the input parameter"); 305ccb8f90Sopenharmony_ci } 315ccb8f90Sopenharmony_ci return thisVar; 325ccb8f90Sopenharmony_ci} 335ccb8f90Sopenharmony_ci 345ccb8f90Sopenharmony_cistd::string NapiUtils::GetStringFromNapi(napi_env& env, napi_value& napiStr) 355ccb8f90Sopenharmony_ci{ 365ccb8f90Sopenharmony_ci char str[MAX_SIZE] = {0}; 375ccb8f90Sopenharmony_ci size_t strLen; 385ccb8f90Sopenharmony_ci if (napi_ok != napi_get_value_string_utf8(env, napiStr, str, MAX_SIZE - 1, &strLen)) { 395ccb8f90Sopenharmony_ci POWER_HILOGW(COMP_FWK, "napi_get_value_string_utf8 failed"); 405ccb8f90Sopenharmony_ci return ""; 415ccb8f90Sopenharmony_ci } 425ccb8f90Sopenharmony_ci return std::string(str); 435ccb8f90Sopenharmony_ci} 445ccb8f90Sopenharmony_ci 455ccb8f90Sopenharmony_cinapi_ref NapiUtils::CreateReference(napi_env& env, napi_value& value) 465ccb8f90Sopenharmony_ci{ 475ccb8f90Sopenharmony_ci napi_ref refVal = nullptr; 485ccb8f90Sopenharmony_ci if (napi_ok != napi_create_reference(env, value, 1, &refVal)) { 495ccb8f90Sopenharmony_ci POWER_HILOGW(COMP_FWK, "Failed to create a value reference"); 505ccb8f90Sopenharmony_ci return refVal; 515ccb8f90Sopenharmony_ci } 525ccb8f90Sopenharmony_ci return refVal; 535ccb8f90Sopenharmony_ci} 545ccb8f90Sopenharmony_ci 555ccb8f90Sopenharmony_civoid NapiUtils::ReleaseReference(napi_env& env, napi_ref& ref) 565ccb8f90Sopenharmony_ci{ 575ccb8f90Sopenharmony_ci if (ref != nullptr) { 585ccb8f90Sopenharmony_ci napi_delete_reference(env, ref); 595ccb8f90Sopenharmony_ci ref = nullptr; 605ccb8f90Sopenharmony_ci } 615ccb8f90Sopenharmony_ci} 625ccb8f90Sopenharmony_ci 635ccb8f90Sopenharmony_cibool NapiUtils::CheckValueType(napi_env& env, napi_value& value, napi_valuetype checkType) 645ccb8f90Sopenharmony_ci{ 655ccb8f90Sopenharmony_ci napi_valuetype valueType = napi_undefined; 665ccb8f90Sopenharmony_ci napi_typeof(env, value, &valueType); 675ccb8f90Sopenharmony_ci if (valueType != checkType) { 685ccb8f90Sopenharmony_ci POWER_HILOGW(COMP_FWK, "Parameter type error"); 695ccb8f90Sopenharmony_ci return false; 705ccb8f90Sopenharmony_ci } 715ccb8f90Sopenharmony_ci return true; 725ccb8f90Sopenharmony_ci} 735ccb8f90Sopenharmony_ci} // namespace PowerMgr 745ccb8f90Sopenharmony_ci} // namespace OHOS 75