• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "geocoding_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 struct NativeContext {
43     napi_env env_ = nullptr;
44     napi_ref ref_ = nullptr;
45 };
46 
47 napi_value UndefinedNapiValue(const napi_env& env);
48 void LocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
49 void PoiToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
50 void SetUndefinedResult(const napi_env& env, napi_value& result);
51 void BluetoothScanResultToJs(const napi_env& env, const std::unique_ptr<BluetoothScanResult>& bluetoothScanResult,
52     napi_value& result);
53 void LocationsToJs(const napi_env& env, const std::vector<std::unique_ptr<Location>>& locations, napi_value& result);
54 void SatelliteStatusToJs(const napi_env& env, const std::shared_ptr<SatelliteStatus>& statusInfo, napi_value& result);
55 void SystemLocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
56 bool GeoAddressesToJsObj(const napi_env& env,
57     std::list<std::shared_ptr<GeoAddress>>& replyList, napi_value& arrayResult);
58 void JsObjToLocationRequest(const napi_env& env, const napi_value& object,
59     std::unique_ptr<RequestConfig>& requestConfig);
60 bool JsObjToLocation(const napi_env& env, const napi_value& object, Location& location);
61 void JsObjToCurrentLocationRequest(const napi_env& env, const napi_value& object,
62     std::unique_ptr<RequestConfig>& requestConfig);
63 void JsObjToCachedLocationRequest(const napi_env& env, const napi_value& object,
64     std::unique_ptr<CachedGnssLocationsRequest>& request);
65 int JsObjToCommand(const napi_env& env, const napi_value& object,
66     std::unique_ptr<LocationCommand>& commandConfig);
67 int JsObjToGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel);
68 int JsObjToReverseGeoCodeRequest(const napi_env& env, const napi_value& object, MessageParcel& dataParcel);
69 int JsObjectToString(const napi_env& env, const napi_value& object,
70     const char* fieldStr, const int bufLen, std::string& fieldRef);
71 int JsObjectToDouble(const napi_env& env, const napi_value& object, const char* fieldStr, double& fieldRef);
72 int JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef);
73 int JsObjectToInt64(const napi_env& env, const napi_value& object, const char* fieldStr, int64_t& fieldRef);
74 int JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef);
75 napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result);
76 napi_status SetValueStringArray(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result);
77 napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result);
78 napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result);
79 napi_status SetValueDouble(const napi_env& env, const char* fieldStr, const double doubleValue, napi_value& result);
80 napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolvalue, napi_value& result);
81 napi_status SetValueArrayBuffer(
82     const napi_env& env, const char* fieldStr, const std::vector<uint8_t> vectorValue, napi_value& result);
83 napi_value DoAsyncWork(const napi_env& env, AsyncContext* asyncContext,
84     const size_t argc, const napi_value* argv, const size_t objectArgsNum);
85 bool JsObjToRevGeocodeMock(const napi_env& env, const napi_value& object,
86     std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo);
87 void JsObjToLocatingRequiredDataConfig(const napi_env& env, const napi_value& object,
88     std::unique_ptr<LocatingRequiredDataConfig>& config);
89 bool LocatingRequiredDataToJsObj(const napi_env& env,
90     std::vector<std::shared_ptr<LocatingRequiredData>>& replyList, napi_value& arrayResult);
91 std::string GetErrorMsgByCode(int code);
92 int ConvertErrorCode(int errorCode);
93 std::map<int, std::string> GetErrorCodeMap();
94 void GetErrorCodeMapExt(std::map<int, std::string>& errorCodeMap);
95 void CreateFailCallBackParams(AsyncContext& context, const std::string& msg, int32_t errorCode);
96 void CountryCodeToJs(const napi_env& env, const std::shared_ptr<CountryCode>& country, napi_value& result);
97 void GetLocationArray(const napi_env& env, LocationMockAsyncContext *asyncContext, const napi_value& object);
98 void DeleteQueueWork(AsyncContext* context);
99 void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work);
100 napi_value GetErrorObject(napi_env env, const int32_t errCode, const std::string& errMsg);
101 bool CheckIfParamIsFunctionType(napi_env env, napi_value param);
102 napi_value SetEnumPropertyByInteger(napi_env env, napi_value dstObj, int32_t enumValue, const char *enumName);
103 bool CheckIfParamIsObjectType(napi_env env, napi_value param);
104 napi_value GetNapiValueByKey(napi_env env, const std::string& keyChar, napi_value object);
105 napi_value CreateJsMap(napi_env env, const std::map<std::string, std::string>& additionsMap);
106 napi_status SetValueStringMap(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result);
107 napi_value GetArrayProperty(const napi_env& env, const napi_value& object, std::string propertyName);
108 napi_value CreateError(napi_env env, int32_t err, const std::string &msg);
109 napi_value CreatePoiInfoJsObj(const napi_env& env, const PoiInfo& poiInfo);
110 napi_status SetValuePoi(const napi_env& env, const char* fieldStr, napi_value& value, napi_value& result);
111 
112 #define CHK_NAPIOK_CONTINUE(env, state, message) \
113 {                                                \
114     if ((state) != napi_ok) {                    \
115         LBSLOGE("(%{public}s) fail", #message);  \
116         GET_AND_THROW_LAST_ERROR((env));         \
117         continue;                                \
118     }                                            \
119 }
120 
121 #define CHK_ERROR_CODE(type, theCall, isRequired)                                                 \
122 {                                                                                                 \
123     int errorCode = (theCall);                                                                    \
124     if (errorCode == COMMON_ERROR || errorCode == INPUT_PARAMS_ERROR) {                           \
125         LBSLOGE(LOCATOR_STANDARD, "Js Object to other types failed.");                            \
126         return errorCode;                                                                         \
127     }                                                                                             \
128     if ((isRequired) && errorCode == PARAM_IS_EMPTY) {                                            \
129         LBSLOGE(LOCATOR_STANDARD, "The required #%{public}s field should not be empty.", (type)); \
130         return INPUT_PARAMS_ERROR;                                                                \
131     }                                                                                             \
132 }
133 
134 #define DELETE_SCOPE_CONTEXT_WORK(env, scope, context, work) \
135 {                                                            \
136     if ((env) != nullptr && (scope) != nullptr) {            \
137         napi_close_handle_scope((env), (scope));             \
138     }                                                        \
139     if ((context) != nullptr) {                              \
140         delete (context);                                    \
141     }                                                        \
142     if ((work) != nullptr) {                                 \
143         delete (work);                                       \
144     }                                                        \
145 }
146 
147 #define CHK_NAPI_ERR_CLOSE_SCOPE(env, state, scope, context, work)    \
148 {                                                                     \
149     if ((state) != napi_ok) {                                         \
150         DELETE_SCOPE_CONTEXT_WORK((env), (scope), (context), (work)); \
151         return;                                                       \
152     }                                                                 \
153 }
154 }  // namespace Location
155 }  // namespace OHOS
156 #endif // NAPI_UTIL_H
157