• 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 #include "location_napi_system.h"
16 #include "locator_callback_host.h"
17 #include "location_log.h"
18 #include "locator.h"
19 #include "system_ability_definition.h"
20 #include "napi_util.h"
21 
22 namespace OHOS {
23 namespace Location {
24 sptr<LocatorCallbackHost> g_systemSingleLocatorCallbackHost =
25     sptr<LocatorCallbackHost>(new (std::nothrow)LocatorCallbackHost());
26 sptr<LocatorCallbackHost> g_systemSubcribeCallbackHost =
27     sptr<LocatorCallbackHost>(new (std::nothrow)LocatorCallbackHost());;
28 std::unique_ptr<Locator> g_locatorImpl = Locator::GetInstance(LOCATION_LOCATOR_SA_ID);
29 
GetLocationOnce(const napi_env & env,const napi_ref & successHandlerRef,const napi_ref & failHandlerRef,const napi_ref & completeHandlerRef,int fixNumber)30 napi_value GetLocationOnce(const napi_env& env,
31                            const napi_ref& successHandlerRef,
32                            const napi_ref& failHandlerRef,
33                            const napi_ref& completeHandlerRef,
34                            int fixNumber)
35 {
36     NAPI_ASSERT(env, g_locatorImpl != nullptr, "get locator SA failed");
37     auto requestConfig = std::make_unique<RequestConfig>();
38     auto locatorCallback = sptr<ILocatorCallback>(g_systemSingleLocatorCallbackHost);
39     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
40     requestConfig->SetScenario(SCENE_UNSET);
41     requestConfig->SetTimeInterval(0);
42     requestConfig->SetDistanceInterval(0);
43     requestConfig->SetMaxAccuracy(0);
44     requestConfig->SetFixNumber(fixNumber);
45     if (g_systemSingleLocatorCallbackHost->m_successHandlerCb != nullptr ||
46         g_systemSingleLocatorCallbackHost->m_failHandlerCb != nullptr ||
47         g_systemSingleLocatorCallbackHost->m_completeHandlerCb != nullptr) {
48         LBSLOGI(LOCATION_NAPI, "handlers is not nullptr, stop locating first");
49         g_locatorImpl->StopLocating(locatorCallback);
50         if (env == g_systemSingleLocatorCallbackHost->m_env) {
51             g_systemSingleLocatorCallbackHost->DeleteAllCallbacks();
52         }
53     }
54     g_systemSingleLocatorCallbackHost->m_env = env;
55     g_systemSingleLocatorCallbackHost->m_fixNumber = fixNumber;
56     g_systemSingleLocatorCallbackHost->m_successHandlerCb = successHandlerRef;
57     g_systemSingleLocatorCallbackHost->m_failHandlerCb = failHandlerRef;
58     g_systemSingleLocatorCallbackHost->m_completeHandlerCb = completeHandlerRef;
59     g_locatorImpl->StartLocating(requestConfig, locatorCallback);
60     return UndefinedNapiValue(env);
61 }
62 
GetAllCallback(const napi_env & env,const napi_value & argv,napi_ref & successHandlerRef,napi_ref & failHandlerRef,napi_ref & completeHandlerRef)63 void GetAllCallback(const napi_env &env, const napi_value &argv, napi_ref &successHandlerRef,
64     napi_ref &failHandlerRef, napi_ref &completeHandlerRef)
65 {
66     bool hasProperty = false;
67     napi_value nVsuccessCallback = nullptr, nVfailCallback = nullptr, nVcompleteCallback = nullptr;
68     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, argv, "success", &hasProperty));
69     if (hasProperty) {
70         NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv, "success", &nVsuccessCallback));
71         NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, nVsuccessCallback, 1, &successHandlerRef));
72     }
73     hasProperty = false;
74     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, argv, "fail", &hasProperty));
75     if (hasProperty) {
76         NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv, "fail", &nVfailCallback));
77         NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, nVfailCallback, 1, &failHandlerRef));
78     }
79     hasProperty = false;
80     NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, argv, "complete", &hasProperty));
81     if (hasProperty) {
82         NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, argv, "complete", &nVcompleteCallback));
83         NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, nVcompleteCallback, 1, &completeHandlerRef));
84     }
85 }
86 
GetLocation(napi_env env,napi_callback_info cbinfo)87 napi_value GetLocation(napi_env env, napi_callback_info cbinfo)
88 {
89     size_t argc = 1;
90     napi_value argv[1] = {0}, thisVar = 0, result = nullptr;
91     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr));
92     napi_valuetype valueType = napi_undefined;
93     NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
94     NAPI_ASSERT(env, argc == 1, "number of parameters is error");
95     NAPI_ASSERT(env, valueType == napi_object, "type of parameters is error");
96     NAPI_ASSERT(env, g_locatorImpl != nullptr, "get locator SA failed");
97     napi_value nVtimeout, nVcoordType;
98     int32_t timeout = 0;
99     std::string coordType = "";
100     napi_ref successHandlerRef = nullptr, failHandlerRef = nullptr, completeHandlerRef = nullptr;
101     bool hasProperty = false;
102     NAPI_CALL(env, napi_has_named_property(env, argv[0], "timeout", &hasProperty));
103     if (hasProperty) {
104         NAPI_CALL(env, napi_get_named_property(env, argv[0], "timeout", &nVtimeout));
105         NAPI_CALL(env, napi_get_value_int32(env, nVtimeout, &timeout));
106     }
107     hasProperty = false;
108     NAPI_CALL(env, napi_has_named_property(env, argv[0], "coordType", &hasProperty));
109     if (hasProperty) {
110         NAPI_CALL(env, napi_get_named_property(env, argv[0], "coordType", &nVcoordType));
111         char type[64] = {0};
112         size_t typeLen = 0;
113         NAPI_CALL(env, napi_get_value_string_utf8(env, nVcoordType, type, sizeof(type), &typeLen));
114         coordType = type;
115         if (coordType != "wgs84") {
116             NAPI_CALL(env, napi_get_undefined(env, &result));
117             return result;
118         }
119     }
120     GetAllCallback(env, argv[0], successHandlerRef, failHandlerRef, completeHandlerRef);
121     int fixnumber = 1;
122     GetLocationOnce(env, successHandlerRef, failHandlerRef, completeHandlerRef, fixnumber);
123     NAPI_CALL(env, napi_get_undefined(env, &result));
124     return result;
125 }
126 
EmitSyncCallbackWork(const napi_env & env,const napi_value & successHandler,const napi_value & failHandler,const napi_value & completeHandler)127 bool EmitSyncCallbackWork(const napi_env& env,
128                           const napi_value& successHandler,
129                           const napi_value& failHandler,
130                           const napi_value& completeHandler)
131 {
132     napi_value jsEvent = nullptr;
133     napi_value arrString = nullptr;
134     napi_value value;
135     int arrIndex = 0;
136     NAPI_CALL_BASE(env, napi_create_array(env, &arrString), false);
137     NAPI_CALL_BASE(env, napi_create_string_utf8(env, "gps", NAPI_AUTO_LENGTH, &value), false);
138     NAPI_CALL_BASE(env, napi_set_element(env, arrString, arrIndex, value), false);
139     arrIndex++;
140     NAPI_CALL_BASE(env, napi_create_string_utf8(env, "network", NAPI_AUTO_LENGTH, &value), false);
141     NAPI_CALL_BASE(env, napi_set_element(env, arrString, arrIndex, value), false);
142     NAPI_CALL_BASE(env, napi_create_object(env, &jsEvent), false);
143     NAPI_CALL_BASE(env, napi_set_named_property(env, jsEvent, "types", arrString), false);
144     napi_value undefine;
145     NAPI_CALL_BASE(env, napi_get_undefined(env, &undefine), false);
146     NAPI_CALL_BASE(env, napi_call_function(env, nullptr, successHandler, 1, &jsEvent, &undefine), false);
147     return true;
148 }
149 
GetLocationType(napi_env env,napi_callback_info cbinfo)150 napi_value GetLocationType(napi_env env, napi_callback_info cbinfo)
151 {
152     size_t argc = 1;
153     napi_value argv[1] = {0};
154     napi_value thisVar = 0;
155     napi_value result = nullptr;
156     napi_valuetype valueType = napi_undefined;
157     NAPI_CALL(env,  napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr));
158     NAPI_ASSERT(env, argc == 1, "number of parameters is error");
159     NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
160     NAPI_ASSERT(env, valueType == napi_object, "type of parameters is error");
161     napi_value nVsuccessCallback;
162     napi_value nVfailCallback;
163     napi_value nVcompleteCallback;
164     bool hasProperty = false;
165     NAPI_CALL(env, napi_has_named_property(env, argv[0], "success", &hasProperty));
166     if (hasProperty) {
167         NAPI_CALL(env, napi_get_named_property(env, argv[0], "success", &nVsuccessCallback));
168     }
169     hasProperty = false;
170     NAPI_CALL(env, napi_has_named_property(env, argv[0], "fail", &hasProperty));
171     if (hasProperty) {
172         NAPI_CALL(env, napi_get_named_property(env, argv[0], "fail", &nVfailCallback));
173     }
174     hasProperty = false;
175     NAPI_CALL(env, napi_has_named_property(env, argv[0], "complete", &hasProperty));
176     if (hasProperty) {
177         NAPI_CALL(env, napi_get_named_property(env, argv[0], "complete", &nVcompleteCallback));
178     }
179     EmitSyncCallbackWork(env, nVsuccessCallback, nVfailCallback, nVcompleteCallback);
180     NAPI_CALL(env, napi_get_undefined(env, &result));
181     return result;
182 }
183 
SubscribeSystemLocationChange(napi_env env,napi_ref & successHandlerRef,napi_ref & failHandlerRef,int fixNumber,sptr<LocatorCallbackHost> & locatorCallbackHost)184 void SubscribeSystemLocationChange(napi_env env,
185                                    napi_ref& successHandlerRef,
186                                    napi_ref& failHandlerRef,
187                                    int fixNumber,
188                                    sptr<LocatorCallbackHost>& locatorCallbackHost)
189 {
190     auto locatorCallback = sptr<ILocatorCallback>(locatorCallbackHost);
191     if (locatorCallbackHost->m_successHandlerCb != nullptr ||
192         locatorCallbackHost->m_failHandlerCb != nullptr) {
193         LBSLOGI(LOCATION_NAPI, "GetHandlerCb() != nullptr, UnSubscribeLocationChange");
194         g_locatorImpl->StopLocating(locatorCallback);
195         if (env == locatorCallbackHost->m_env) {
196             locatorCallbackHost->DeleteAllCallbacks();
197         }
198     }
199     locatorCallbackHost->m_env = env;
200     locatorCallbackHost->m_fixNumber = fixNumber;
201     locatorCallbackHost->m_successHandlerCb = successHandlerRef;
202     locatorCallbackHost->m_failHandlerCb = failHandlerRef;
203     std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
204     requestConfig->SetPriority(PRIORITY_FAST_FIRST_FIX);
205     requestConfig->SetScenario(SCENE_UNSET);
206     requestConfig->SetTimeInterval(0);
207     requestConfig->SetDistanceInterval(0);
208     requestConfig->SetMaxAccuracy(0);
209     requestConfig->SetFixNumber(fixNumber);
210     g_locatorImpl->StartLocating(requestConfig, locatorCallback);
211 }
212 
Subscribe(napi_env env,napi_callback_info cbinfo)213 napi_value Subscribe(napi_env env, napi_callback_info cbinfo)
214 {
215     size_t argc = 1;
216     napi_value argv[1] = {0}, thisVar = 0, result = nullptr;
217     napi_value nVcoordType;
218     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr));
219     napi_valuetype valueType = napi_undefined;
220     NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
221     NAPI_ASSERT(env, argc == 1, "number of parameters is error");
222     NAPI_ASSERT(env, valueType == napi_object, "type of parameters is error");
223     NAPI_ASSERT(env, g_locatorImpl != nullptr, "get locator SA failed");
224     std::string coordType = "";
225     napi_ref successHandlerRef = nullptr, failHandlerRef = nullptr, completeHandlerRef = nullptr;
226     bool hasProperty = false;
227     NAPI_CALL(env, napi_has_named_property(env, argv[0], "coordType", &hasProperty));
228     if (hasProperty) {
229         NAPI_CALL(env, napi_get_named_property(env, argv[0], "coordType", &nVcoordType));
230         char type[64] = {0};
231         size_t typeLen = 0;
232         NAPI_CALL(env, napi_get_value_string_utf8(env, nVcoordType, type, sizeof(type), &typeLen));
233         coordType = type;
234         if (coordType != "wgs84") {
235             NAPI_CALL(env, napi_get_undefined(env, &result));
236             return result;
237         }
238     }
239     GetAllCallback(env, argv[0], successHandlerRef, failHandlerRef, completeHandlerRef);
240     SubscribeSystemLocationChange(env, successHandlerRef, failHandlerRef, 0, g_systemSubcribeCallbackHost);
241     NAPI_CALL(env, napi_get_undefined(env, &result));
242     return result;
243 }
244 
Unsubscribe(napi_env env,napi_callback_info cbinfo)245 napi_value Unsubscribe(napi_env env, napi_callback_info cbinfo)
246 {
247     NAPI_ASSERT(env, g_locatorImpl != nullptr, "get locator SA failed");
248     napi_value result = nullptr;
249     auto locatorCallback = sptr<ILocatorCallback>(g_systemSubcribeCallbackHost);
250     g_locatorImpl->StopLocating(locatorCallback);
251     if (env == g_systemSubcribeCallbackHost->m_env) {
252         g_systemSubcribeCallbackHost->DeleteAllCallbacks();
253     }
254     NAPI_CALL(env, napi_get_undefined(env, &result));
255     return result;
256 }
257 
GetSupportedCoordTypes(napi_env env,napi_callback_info cbinfo)258 napi_value GetSupportedCoordTypes(napi_env env, napi_callback_info cbinfo)
259 {
260     size_t argc = 1;
261     napi_value argv[1] = {0};
262     napi_value thisVar = 0;
263     NAPI_CALL(env, napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr));
264     NAPI_ASSERT(env, argc == 0, "number of parameters is error");
265     napi_value arrString = nullptr;
266     napi_value value;
267     int arrIndex = 0;
268     NAPI_CALL(env, napi_create_array(env, &arrString));
269     NAPI_CALL(env, napi_create_string_utf8(env, "wgs84", NAPI_AUTO_LENGTH, &value));
270     NAPI_CALL(env, napi_set_element(env, arrString, arrIndex, value));
271     return arrString;
272 }
273 }  // namespace Location
274 }  // namespace OHOS
275