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