15c735df2Sopenharmony_ci/*
25c735df2Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
35c735df2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45c735df2Sopenharmony_ci * you may not use this file except in compliance with the License.
55c735df2Sopenharmony_ci * You may obtain a copy of the License at
65c735df2Sopenharmony_ci *
75c735df2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85c735df2Sopenharmony_ci *
95c735df2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105c735df2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115c735df2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125c735df2Sopenharmony_ci * See the License for the specific language governing permissions and
135c735df2Sopenharmony_ci * limitations under the License.
145c735df2Sopenharmony_ci */
155c735df2Sopenharmony_ci
165c735df2Sopenharmony_ci#ifndef NAPI_UTIL_H
175c735df2Sopenharmony_ci#define NAPI_UTIL_H
185c735df2Sopenharmony_ci
195c735df2Sopenharmony_ci#include <list>
205c735df2Sopenharmony_ci#include <string>
215c735df2Sopenharmony_ci
225c735df2Sopenharmony_ci#include "message_parcel.h"
235c735df2Sopenharmony_ci#include "napi/native_api.h"
245c735df2Sopenharmony_ci#include "napi/native_node_api.h"
255c735df2Sopenharmony_ci#include "uv.h"
265c735df2Sopenharmony_ci
275c735df2Sopenharmony_ci#include "location_async_context.h"
285c735df2Sopenharmony_ci#include "async_context.h"
295c735df2Sopenharmony_ci#include "constant_definition.h"
305c735df2Sopenharmony_ci#include "geo_coding_mock_info.h"
315c735df2Sopenharmony_ci#include "geo_address.h"
325c735df2Sopenharmony_ci#include "locating_required_data.h"
335c735df2Sopenharmony_ci#include "locating_required_data_config.h"
345c735df2Sopenharmony_ci#include "location.h"
355c735df2Sopenharmony_ci#include "location_log.h"
365c735df2Sopenharmony_ci#include "request_config.h"
375c735df2Sopenharmony_ci#include "satellite_status.h"
385c735df2Sopenharmony_ci
395c735df2Sopenharmony_cinamespace OHOS {
405c735df2Sopenharmony_cinamespace Location {
415c735df2Sopenharmony_cinapi_value UndefinedNapiValue(const napi_env& env);
425c735df2Sopenharmony_civoid LocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
435c735df2Sopenharmony_civoid LocationsToJs(const napi_env& env, const std::vector<std::unique_ptr<Location>>& locations, napi_value& result);
445c735df2Sopenharmony_civoid SatelliteStatusToJs(const napi_env& env, const std::shared_ptr<SatelliteStatus>& statusInfo, napi_value& result);
455c735df2Sopenharmony_civoid SystemLocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
465c735df2Sopenharmony_cibool GeoAddressesToJsObj(const napi_env& env,
475c735df2Sopenharmony_ci    std::list<std::shared_ptr<GeoAddress>>& replyList, napi_value& arrayResult);
485c735df2Sopenharmony_civoid JsObjToLocationRequest(const napi_env& env, const napi_value& object,
495c735df2Sopenharmony_ci    std::unique_ptr<RequestConfig>& requestConfig);
505c735df2Sopenharmony_civoid JsObjToCurrentLocationRequest(const napi_env& env, const napi_value& object,
515c735df2Sopenharmony_ci    std::unique_ptr<RequestConfig>& requestConfig);
525c735df2Sopenharmony_civoid JsObjToCachedLocationRequest(const napi_env& env, const napi_value& object,
535c735df2Sopenharmony_ci    std::unique_ptr<CachedGnssLocationsRequest>& request);
545c735df2Sopenharmony_ciint JsObjToCommand(const napi_env& env, const napi_value& object,
555c735df2Sopenharmony_ci    std::unique_ptr<LocationCommand>& commandConfig);
565c735df2Sopenharmony_ciint JsObjToGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel);
575c735df2Sopenharmony_cibool JsObjToReverseGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel);
585c735df2Sopenharmony_ciint JsObjectToString(const napi_env& env, const napi_value& object,
595c735df2Sopenharmony_ci    const char* fieldStr, const int bufLen, std::string& fieldRef);
605c735df2Sopenharmony_ciint JsObjectToDouble(const napi_env& env, const napi_value& object, const char* fieldStr, double& fieldRef);
615c735df2Sopenharmony_ciint JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef);
625c735df2Sopenharmony_ciint JsObjectToInt64(const napi_env& env, const napi_value& object, const char* fieldStr, int64_t& fieldRef);
635c735df2Sopenharmony_ciint JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef);
645c735df2Sopenharmony_cinapi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result);
655c735df2Sopenharmony_cinapi_status SetValueStringArray(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result);
665c735df2Sopenharmony_cinapi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result);
675c735df2Sopenharmony_cinapi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result);
685c735df2Sopenharmony_cinapi_status SetValueDouble(const napi_env& env, const char* fieldStr, const double doubleValue, napi_value& result);
695c735df2Sopenharmony_cinapi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolvalue, napi_value& result);
705c735df2Sopenharmony_cinapi_value DoAsyncWork(const napi_env& env, AsyncContext* asyncContext,
715c735df2Sopenharmony_ci    const size_t argc, const napi_value* argv, const size_t objectArgsNum);
725c735df2Sopenharmony_cibool JsObjToRevGeocodeMock(const napi_env& env, const napi_value& object,
735c735df2Sopenharmony_ci    std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo);
745c735df2Sopenharmony_civoid JsObjToLocatingRequiredDataConfig(const napi_env& env, const napi_value& object,
755c735df2Sopenharmony_ci    std::unique_ptr<LocatingRequiredDataConfig>& config);
765c735df2Sopenharmony_cibool LocatingRequiredDataToJsObj(const napi_env& env,
775c735df2Sopenharmony_ci    std::vector<std::shared_ptr<LocatingRequiredData>>& replyList, napi_value& arrayResult);
785c735df2Sopenharmony_cistd::string GetErrorMsgByCode(int code);
795c735df2Sopenharmony_civoid CreateFailCallBackParams(AsyncContext& context, const std::string& msg, int32_t errorCode);
805c735df2Sopenharmony_civoid CountryCodeToJs(const napi_env& env, const std::shared_ptr<CountryCode>& country, napi_value& result);
815c735df2Sopenharmony_civoid GetLocationArray(const napi_env& env, LocationMockAsyncContext *asyncContext, const napi_value& object);
825c735df2Sopenharmony_civoid DeleteQueueWork(AsyncContext* context);
835c735df2Sopenharmony_civoid DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work);
845c735df2Sopenharmony_cinapi_value GetErrorObject(napi_env env, const int32_t errCode, const std::string& errMsg);
855c735df2Sopenharmony_cibool CheckIfParamIsFunctionType(napi_env env, napi_value param);
865c735df2Sopenharmony_cinapi_value SetEnumPropertyByInteger(napi_env env, napi_value dstObj, int32_t enumValue, const char *enumName);
875c735df2Sopenharmony_cibool CheckIfParamIsObjectType(napi_env env, napi_value param);
885c735df2Sopenharmony_cinapi_value GetNapiValueByKey(napi_env env, const std::string& keyChar, napi_value object);
895c735df2Sopenharmony_cinapi_value CreateJsMap(napi_env env, const std::map<std::string, std::string>& additionsMap);
905c735df2Sopenharmony_cinapi_status SetValueStringMap(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result);
915c735df2Sopenharmony_cinapi_value GetArrayProperty(const napi_env& env, const napi_value& object, std::string propertyName);
925c735df2Sopenharmony_ci
935c735df2Sopenharmony_ci#define CHK_NAPIOK_CONTINUE(env, state, message) \
945c735df2Sopenharmony_ci{                                                \
955c735df2Sopenharmony_ci    if ((state) != napi_ok) {                    \
965c735df2Sopenharmony_ci        LBSLOGE("(%{public}s) fail", #message);  \
975c735df2Sopenharmony_ci        GET_AND_THROW_LAST_ERROR((env));         \
985c735df2Sopenharmony_ci        continue;                                \
995c735df2Sopenharmony_ci    }                                            \
1005c735df2Sopenharmony_ci}
1015c735df2Sopenharmony_ci
1025c735df2Sopenharmony_ci#define CHK_ERROR_CODE(type, theCall, isRequired)                                                 \
1035c735df2Sopenharmony_ci{                                                                                                 \
1045c735df2Sopenharmony_ci    int errorCode = (theCall);                                                                    \
1055c735df2Sopenharmony_ci    if (errorCode == COMMON_ERROR || errorCode == INPUT_PARAMS_ERROR) {                           \
1065c735df2Sopenharmony_ci        LBSLOGE(LOCATOR_STANDARD, "Js Object to other types failed.");                            \
1075c735df2Sopenharmony_ci        return errorCode;                                                                         \
1085c735df2Sopenharmony_ci    }                                                                                             \
1095c735df2Sopenharmony_ci    if ((isRequired) && errorCode == PARAM_IS_EMPTY) {                                            \
1105c735df2Sopenharmony_ci        LBSLOGE(LOCATOR_STANDARD, "The required #%{public}s field should not be empty.", (type)); \
1115c735df2Sopenharmony_ci        return INPUT_PARAMS_ERROR;                                                                \
1125c735df2Sopenharmony_ci    }                                                                                             \
1135c735df2Sopenharmony_ci}
1145c735df2Sopenharmony_ci
1155c735df2Sopenharmony_ci#define DELETE_SCOPE_CONTEXT_WORK(env, scope, context, work) \
1165c735df2Sopenharmony_ci{                                                            \
1175c735df2Sopenharmony_ci    if ((env) != nullptr && (scope) != nullptr) {            \
1185c735df2Sopenharmony_ci        napi_close_handle_scope((env), (scope));             \
1195c735df2Sopenharmony_ci    }                                                        \
1205c735df2Sopenharmony_ci    if ((context) != nullptr) {                              \
1215c735df2Sopenharmony_ci        delete (context);                                    \
1225c735df2Sopenharmony_ci    }                                                        \
1235c735df2Sopenharmony_ci    if ((work) != nullptr) {                                 \
1245c735df2Sopenharmony_ci        delete (work);                                       \
1255c735df2Sopenharmony_ci    }                                                        \
1265c735df2Sopenharmony_ci}
1275c735df2Sopenharmony_ci
1285c735df2Sopenharmony_ci#define CHK_NAPI_ERR_CLOSE_SCOPE(env, state, scope, context, work)    \
1295c735df2Sopenharmony_ci{                                                                     \
1305c735df2Sopenharmony_ci    if ((state) != napi_ok) {                                         \
1315c735df2Sopenharmony_ci        DELETE_SCOPE_CONTEXT_WORK((env), (scope), (context), (work)); \
1325c735df2Sopenharmony_ci        return;                                                       \
1335c735df2Sopenharmony_ci    }                                                                 \
1345c735df2Sopenharmony_ci}
1355c735df2Sopenharmony_ci}  // namespace Location
1365c735df2Sopenharmony_ci}  // namespace OHOS
1375c735df2Sopenharmony_ci#endif // NAPI_UTIL_H
138