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