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 16 #ifndef NAPI_UTIL_H 17 #define NAPI_UTIL_H 18 19 #include <list> 20 #include <string> 21 22 #include "message_parcel.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "uv.h" 26 27 #include "location_async_context.h" 28 #include "async_context.h" 29 #include "constant_definition.h" 30 #include "geo_coding_mock_info.h" 31 #include "geo_address.h" 32 #include "locating_required_data.h" 33 #include "locating_required_data_config.h" 34 #include "location.h" 35 #include "location_log.h" 36 #include "request_config.h" 37 #include "satellite_status.h" 38 #include "bluetooth_scan_result.h" 39 40 namespace OHOS { 41 namespace Location { 42 napi_value UndefinedNapiValue(const napi_env& env); 43 void LocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result); 44 void BluetoohScanResultToJs(const napi_env& env, const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult, 45 napi_value& result); 46 void LocationsToJs(const napi_env& env, const std::vector<std::unique_ptr<Location>>& locations, napi_value& result); 47 void SatelliteStatusToJs(const napi_env& env, const std::shared_ptr<SatelliteStatus>& statusInfo, napi_value& result); 48 void SystemLocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result); 49 bool GeoAddressesToJsObj(const napi_env& env, 50 std::list<std::shared_ptr<GeoAddress>>& replyList, napi_value& arrayResult); 51 void JsObjToLocationRequest(const napi_env& env, const napi_value& object, 52 std::unique_ptr<RequestConfig>& requestConfig); 53 void JsObjToCurrentLocationRequest(const napi_env& env, const napi_value& object, 54 std::unique_ptr<RequestConfig>& requestConfig); 55 void JsObjToCachedLocationRequest(const napi_env& env, const napi_value& object, 56 std::unique_ptr<CachedGnssLocationsRequest>& request); 57 int JsObjToCommand(const napi_env& env, const napi_value& object, 58 std::unique_ptr<LocationCommand>& commandConfig); 59 int JsObjToGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel); 60 int JsObjToReverseGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel); 61 int JsObjectToString(const napi_env& env, const napi_value& object, 62 const char* fieldStr, const int bufLen, std::string& fieldRef); 63 int JsObjectToDouble(const napi_env& env, const napi_value& object, const char* fieldStr, double& fieldRef); 64 int JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef); 65 int JsObjectToInt64(const napi_env& env, const napi_value& object, const char* fieldStr, int64_t& fieldRef); 66 int JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef); 67 napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result); 68 napi_status SetValueStringArray(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result); 69 napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); 70 napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result); 71 napi_status SetValueDouble(const napi_env& env, const char* fieldStr, const double doubleValue, napi_value& result); 72 napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolvalue, napi_value& result); 73 napi_status SetValueArrayBuffer( 74 const napi_env& env, const char* fieldStr, const std::vector<uint8_t> vectorValue, napi_value& result); 75 napi_value DoAsyncWork(const napi_env& env, AsyncContext* asyncContext, 76 const size_t argc, const napi_value* argv, const size_t objectArgsNum); 77 bool JsObjToRevGeocodeMock(const napi_env& env, const napi_value& object, 78 std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo); 79 void JsObjToLocatingRequiredDataConfig(const napi_env& env, const napi_value& object, 80 std::unique_ptr<LocatingRequiredDataConfig>& config); 81 bool LocatingRequiredDataToJsObj(const napi_env& env, 82 std::vector<std::shared_ptr<LocatingRequiredData>>& replyList, napi_value& arrayResult); 83 std::string GetErrorMsgByCode(int code); 84 int ConvertErrorCode(int errorCode); 85 std::map<int, std::string> GetErrorCodeMap(); 86 void CreateFailCallBackParams(AsyncContext& context, const std::string& msg, int32_t errorCode); 87 void CountryCodeToJs(const napi_env& env, const std::shared_ptr<CountryCode>& country, napi_value& result); 88 void GetLocationArray(const napi_env& env, LocationMockAsyncContext *asyncContext, const napi_value& object); 89 void DeleteQueueWork(AsyncContext* context); 90 void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work); 91 napi_value GetErrorObject(napi_env env, const int32_t errCode, const std::string& errMsg); 92 bool CheckIfParamIsFunctionType(napi_env env, napi_value param); 93 napi_value SetEnumPropertyByInteger(napi_env env, napi_value dstObj, int32_t enumValue, const char *enumName); 94 bool CheckIfParamIsObjectType(napi_env env, napi_value param); 95 napi_value GetNapiValueByKey(napi_env env, const std::string& keyChar, napi_value object); 96 napi_value CreateJsMap(napi_env env, const std::map<std::string, std::string>& additionsMap); 97 napi_status SetValueStringMap(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result); 98 napi_value GetArrayProperty(const napi_env& env, const napi_value& object, std::string propertyName); 99 napi_value CreateError(napi_env env, int32_t err, const std::string &msg); 100 101 #define CHK_NAPIOK_CONTINUE(env, state, message) \ 102 { \ 103 if ((state) != napi_ok) { \ 104 LBSLOGE("(%{public}s) fail", #message); \ 105 GET_AND_THROW_LAST_ERROR((env)); \ 106 continue; \ 107 } \ 108 } 109 110 #define CHK_ERROR_CODE(type, theCall, isRequired) \ 111 { \ 112 int errorCode = (theCall); \ 113 if (errorCode == COMMON_ERROR || errorCode == INPUT_PARAMS_ERROR) { \ 114 LBSLOGE(LOCATOR_STANDARD, "Js Object to other types failed."); \ 115 return errorCode; \ 116 } \ 117 if ((isRequired) && errorCode == PARAM_IS_EMPTY) { \ 118 LBSLOGE(LOCATOR_STANDARD, "The required #%{public}s field should not be empty.", (type)); \ 119 return INPUT_PARAMS_ERROR; \ 120 } \ 121 } 122 123 #define DELETE_SCOPE_CONTEXT_WORK(env, scope, context, work) \ 124 { \ 125 if ((env) != nullptr && (scope) != nullptr) { \ 126 napi_close_handle_scope((env), (scope)); \ 127 } \ 128 if ((context) != nullptr) { \ 129 delete (context); \ 130 } \ 131 if ((work) != nullptr) { \ 132 delete (work); \ 133 } \ 134 } 135 136 #define CHK_NAPI_ERR_CLOSE_SCOPE(env, state, scope, context, work) \ 137 { \ 138 if ((state) != napi_ok) { \ 139 DELETE_SCOPE_CONTEXT_WORK((env), (scope), (context), (work)); \ 140 return; \ 141 } \ 142 } 143 } // namespace Location 144 } // namespace OHOS 145 #endif // NAPI_UTIL_H 146