• 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 "geo_coding_mock_info.h"
31 #include "geo_address.h"
32 #include "location.h"
33 #include "location_log.h"
34 #include "request_config.h"
35 #include "satellite_status.h"
36 
37 namespace OHOS {
38 namespace Location {
39 napi_value UndefinedNapiValue(const napi_env& env);
40 void LocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
41 void LocationsToJs(const napi_env& env, const std::vector<std::shared_ptr<Location>>& locations, napi_value& result);
42 void SatelliteStatusToJs(const napi_env& env, const std::shared_ptr<SatelliteStatus>& statusInfo, napi_value& result);
43 void SystemLocationToJs(const napi_env& env, const std::unique_ptr<Location>& locationInfo, napi_value& result);
44 bool GeoAddressesToJsObj(const napi_env& env,
45     std::list<std::shared_ptr<GeoAddress>>& replyList, napi_value& arrayResult);
46 void JsObjToLocationRequest(const napi_env& env, const napi_value& object,
47     std::unique_ptr<RequestConfig>& requestConfig);
48 void JsObjToCurrentLocationRequest(const napi_env& env, const napi_value& object,
49     std::unique_ptr<RequestConfig>& requestConfig);
50 void JsObjToCachedLocationRequest(const napi_env& env, const napi_value& object,
51     std::unique_ptr<CachedGnssLocationsRequest>& request);
52 int JsObjToCommand(const napi_env& env, const napi_value& object,
53     std::unique_ptr<LocationCommand>& commandConfig);
54 void JsObjToGeoFenceRequest(const napi_env& env, const napi_value& object,
55     const std::unique_ptr<GeofenceRequest>& request);
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 std::string GetErrorMsgByCode(int code);
75 void CreateFailCallBackParams(AsyncContext& context, const std::string& msg, int32_t errorCode);
76 void CountryCodeToJs(const napi_env& env, const std::shared_ptr<CountryCode>& country, napi_value& result);
77 void GetLocationArray(const napi_env& env, LocationMockAsyncContext *asyncContext, const napi_value& object);
78 void DeleteQueueWork(AsyncContext* context);
79 void DeleteCallbackHandler(uv_loop_s *&loop, uv_work_t *&work);
80 napi_value GetErrorObject(napi_env env, const int32_t errCode, const std::string& errMsg);
81 bool CheckIfParamIsFunctionType(napi_env env, napi_value param);
82 napi_value SetEnumPropertyByInteger(napi_env env, napi_value dstObj, int32_t enumValue, const char *enumName);
83 
84 #define CHK_NAPIOK_CONTINUE(env, state, message) \
85 { \
86     if ((state) != napi_ok) { \
87         LBSLOGE("(%{public}s) fail", #message); \
88         GET_AND_THROW_LAST_ERROR((env)); \
89         continue; \
90     } \
91 }
92 
93 #define CHK_ERROR_CODE(type, theCall, isRequired)                                                     \
94     do {                                                                                              \
95         int errorCode = (theCall);                                                                    \
96         if (errorCode == COMMON_ERROR || errorCode == INPUT_PARAMS_ERROR) {                           \
97             LBSLOGE(LOCATOR_STANDARD, "Js Object to other types failed.");                            \
98             return errorCode;                                                                         \
99         }                                                                                             \
100         if ((isRequired) && errorCode == PARAM_IS_EMPTY) {                                            \
101             LBSLOGE(LOCATOR_STANDARD, "The required #%{public}s field should not be empty.", (type)); \
102             return INPUT_PARAMS_ERROR;                                                                \
103         }                                                                                             \
104     } while (0)
105 
106 #define CHK_NAPI_ERR_CLOSE_SCOPE(env, state, scope, context, work)             \
107     do {                                                                       \
108         if ((state) != napi_ok && (context) != nullptr && (work) != nullptr) { \
109             napi_close_handle_scope((env), (scope));                           \
110             delete (context);                                                  \
111             delete (work);                                                     \
112             return;                                                            \
113         }                                                                      \
114     } while (0)
115 }  // namespace Location
116 }  // namespace OHOS
117 #endif // NAPI_UTIL_H
118