199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H
1799552fe9Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H
1899552fe9Sopenharmony_ci
1999552fe9Sopenharmony_ci#include <string_ex.h>
2099552fe9Sopenharmony_ci#include <memory>
2199552fe9Sopenharmony_ci
2299552fe9Sopenharmony_ci#include "standby_service_errors.h"
2399552fe9Sopenharmony_ci#include "napi/native_api.h"
2499552fe9Sopenharmony_ci#include "napi/native_node_api.h"
2599552fe9Sopenharmony_ci
2699552fe9Sopenharmony_cinamespace OHOS {
2799552fe9Sopenharmony_cinamespace DevStandbyMgr {
2899552fe9Sopenharmony_cistruct AsyncWorkData {
2999552fe9Sopenharmony_ci    explicit AsyncWorkData(napi_env napiEnv);
3099552fe9Sopenharmony_ci    virtual ~AsyncWorkData();
3199552fe9Sopenharmony_ci    napi_env env;
3299552fe9Sopenharmony_ci    napi_async_work asyncWork = nullptr;
3399552fe9Sopenharmony_ci    napi_deferred deferred = nullptr;
3499552fe9Sopenharmony_ci    napi_ref callback = nullptr;
3599552fe9Sopenharmony_ci    bool isCallback = false;
3699552fe9Sopenharmony_ci    int32_t errCode = 0;
3799552fe9Sopenharmony_ci};
3899552fe9Sopenharmony_ci
3999552fe9Sopenharmony_ciclass Common {
4099552fe9Sopenharmony_cipublic:
4199552fe9Sopenharmony_ci    static napi_value NapiGetNull(napi_env env);
4299552fe9Sopenharmony_ci
4399552fe9Sopenharmony_ci    static void PaddingAsyncWorkData(
4499552fe9Sopenharmony_ci        const napi_env& env, const napi_ref& callback, AsyncWorkData& info, napi_value& promise);
4599552fe9Sopenharmony_ci
4699552fe9Sopenharmony_ci    static void ReturnCallbackPromise(const napi_env& env, const AsyncWorkData& info,
4799552fe9Sopenharmony_ci        const napi_value& result);
4899552fe9Sopenharmony_ci
4999552fe9Sopenharmony_ci    static void SetCallback(const napi_env& env, const napi_ref& callbackIn,
5099552fe9Sopenharmony_ci        const int32_t& errCode, const napi_value& result);
5199552fe9Sopenharmony_ci
5299552fe9Sopenharmony_ci    static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
5399552fe9Sopenharmony_ci
5499552fe9Sopenharmony_ci    static napi_value SetPromise(const napi_env& env, const AsyncWorkData& info, const napi_value& result);
5599552fe9Sopenharmony_ci
5699552fe9Sopenharmony_ci    static void HandleErrCode(const napi_env &env, int32_t errCode);
5799552fe9Sopenharmony_ci
5899552fe9Sopenharmony_ci    static std::string FindErrMsg(const napi_env& env, const int32_t errCode);
5999552fe9Sopenharmony_ci
6099552fe9Sopenharmony_ci    static int32_t FindErrCode(const napi_env& env, const int32_t errCodeIn);
6199552fe9Sopenharmony_ci
6299552fe9Sopenharmony_ci    static bool HandleParamErr(const napi_env& env, int32_t errCode);
6399552fe9Sopenharmony_ci
6499552fe9Sopenharmony_ci    static void SetInt32Value(const napi_env& env, const std::string& fieldStr,
6599552fe9Sopenharmony_ci        const int32_t intValue, napi_value& result);
6699552fe9Sopenharmony_ci
6799552fe9Sopenharmony_ci    static void SetUint32Value(const napi_env& env, const std::string& fieldStr,
6899552fe9Sopenharmony_ci        const uint32_t uintValue, napi_value& result);
6999552fe9Sopenharmony_ci
7099552fe9Sopenharmony_ci    static void SetStringValue(const napi_env& env, const std::string& fieldStr,
7199552fe9Sopenharmony_ci        const std::string& stringValue, napi_value& result);
7299552fe9Sopenharmony_ci
7399552fe9Sopenharmony_ci    static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result);
7499552fe9Sopenharmony_ci
7599552fe9Sopenharmony_ci    static napi_value GetUint32Value(const napi_env& env, const napi_value& value, uint32_t& result);
7699552fe9Sopenharmony_ci
7799552fe9Sopenharmony_ci    static napi_value GetInt32Value(const napi_env& env, const napi_value& value, int32_t& result);
7899552fe9Sopenharmony_ci
7999552fe9Sopenharmony_ci    static napi_value GetNamedInt32Value(const napi_env &env, napi_value &object, const char* utf8name,
8099552fe9Sopenharmony_ci        int32_t& result);
8199552fe9Sopenharmony_ci
8299552fe9Sopenharmony_ci    static napi_value GetNamedStringValue(const napi_env &env, napi_value &object, const char* utf8name,
8399552fe9Sopenharmony_ci        std::string& result);
8499552fe9Sopenharmony_ci};
8599552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
8699552fe9Sopenharmony_ci}  // namespace OHOS
8799552fe9Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H