1 /* 2 * Copyright (c) 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 16 #ifndef FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H 17 #define FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H 18 19 #include <string_ex.h> 20 #include <memory> 21 22 #include "standby_service_errors.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS { 27 namespace DevStandbyMgr { 28 struct AsyncWorkData { 29 explicit AsyncWorkData(napi_env napiEnv); 30 virtual ~AsyncWorkData(); 31 napi_env env; 32 napi_async_work asyncWork = nullptr; 33 napi_deferred deferred = nullptr; 34 napi_ref callback = nullptr; 35 bool isCallback = false; 36 int32_t errCode = 0; 37 }; 38 39 class Common { 40 public: 41 static napi_value NapiGetNull(napi_env env); 42 43 static void PaddingAsyncWorkData( 44 const napi_env& env, const napi_ref& callback, AsyncWorkData& info, napi_value& promise); 45 46 static void ReturnCallbackPromise(const napi_env& env, const AsyncWorkData& info, 47 const napi_value& result); 48 49 static void SetCallback(const napi_env& env, const napi_ref& callbackIn, 50 const int32_t& errCode, const napi_value& result); 51 52 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg); 53 54 static napi_value SetPromise(const napi_env& env, const AsyncWorkData& info, const napi_value& result); 55 56 static void HandleErrCode(const napi_env &env, int32_t errCode); 57 58 static std::string FindErrMsg(const napi_env& env, const int32_t errCode); 59 60 static int32_t FindErrCode(const napi_env& env, const int32_t errCodeIn); 61 62 static bool HandleParamErr(const napi_env& env, int32_t errCode); 63 64 static void SetInt32Value(const napi_env& env, const std::string& fieldStr, 65 const int32_t intValue, napi_value& result); 66 67 static void SetUint32Value(const napi_env& env, const std::string& fieldStr, 68 const uint32_t uintValue, napi_value& result); 69 70 static void SetStringValue(const napi_env& env, const std::string& fieldStr, 71 const std::string& stringValue, napi_value& result); 72 73 static napi_value GetStringValue(const napi_env &env, const napi_value &value, std::string &result); 74 75 static napi_value GetUint32Value(const napi_env& env, const napi_value& value, uint32_t& result); 76 77 static napi_value GetInt32Value(const napi_env& env, const napi_value& value, int32_t& result); 78 79 static napi_value GetNamedInt32Value(const napi_env &env, napi_value &object, const char* utf8name, 80 int32_t& result); 81 82 static napi_value GetNamedStringValue(const napi_env &env, napi_value &object, const char* utf8name, 83 std::string& result); 84 }; 85 } // namespace DevStandbyMgr 86 } // namespace OHOS 87 #endif // FOUNDATION_RESOURCESCHEDULE_DEVICE_STANDBY_INTERFACES_KITS_NAPI_INCLUDE_COMMON_H