• 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 #include "locator_skeleton.h"
17 
18 #include <file_ex.h>
19 #include "ipc_skeleton.h"
20 #include "common_utils.h"
21 #include "i_locator_callback.h"
22 #include "location.h"
23 #include "locator_ability.h"
24 #include "location_dumper.h"
25 #include "request_config.h"
26 #include "system_ability_definition.h"
27 #include "location_sa_load_manager.h"
28 #include "locationhub_ipc_interface_code.h"
29 #include "locating_required_data_config.h"
30 #include "locator_required_data_manager.h"
31 
32 namespace OHOS {
33 namespace Location {
InitLocatorHandleMap()34 void LocatorAbilityStub::InitLocatorHandleMap()
35 {
36     if (locatorHandleMap_.size() != 0) {
37         return;
38     }
39     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GET_SWITCH_STATE)] =
40         &LocatorAbilityStub::PreGetSwitchState;
41     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_SWITCH_CALLBACK)] =
42         &LocatorAbilityStub::PreRegisterSwitchCallback;
43     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_SWITCH_CALLBACK)] =
44         &LocatorAbilityStub::PreUnregisterSwitchCallback;
45     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::START_LOCATING)] =
46         &LocatorAbilityStub::PreStartLocating;
47     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::STOP_LOCATING)] =
48         &LocatorAbilityStub::PreStopLocating;
49     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GET_CACHE_LOCATION)] =
50         &LocatorAbilityStub::PreGetCacheLocation;
51     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::ENABLE_ABILITY)] =
52         &LocatorAbilityStub::PreEnableAbility;
53     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UPDATE_SA_ABILITY)] =
54         &LocatorAbilityStub::PreUpdateSaAbility;
55     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::IS_PRIVACY_COMFIRMED)] =
56         &LocatorAbilityStub::PreIsLocationPrivacyConfirmed;
57     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::SET_PRIVACY_COMFIRM_STATUS)] =
58         &LocatorAbilityStub::PreSetLocationPrivacyConfirmStatus;
59     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::ENABLE_LOCATION_MOCK)] =
60         &LocatorAbilityStub::PreEnableLocationMock;
61     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK)] =
62         &LocatorAbilityStub::PreDisableLocationMock;
63     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::SET_MOCKED_LOCATIONS)] =
64         &LocatorAbilityStub::PreSetMockedLocations;
65     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::PROXY_UID_FOR_FREEZE)] =
66         &LocatorAbilityStub::PreProxyUidForFreeze;
67     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::RESET_ALL_PROXY)] =
68         &LocatorAbilityStub::PreResetAllProxy;
69     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REPORT_LOCATION)] =
70         &LocatorAbilityStub::PreReportLocation;
71     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_LOCATING_REQUIRED_DATA_CALLBACK)] =
72         &LocatorAbilityStub::PreRegisterLocatingRequiredDataCallback;
73     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_LOCATING_REQUIRED_DATA_CALLBACK)] =
74         &LocatorAbilityStub::PreUnregisterLocatingRequiredDataCallback;
75 #ifdef FEATURE_GEOCODE_SUPPORT
76     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GEO_IS_AVAILABLE)] =
77         &LocatorAbilityStub::PreIsGeoConvertAvailable;
78     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GET_FROM_COORDINATE)] =
79         &LocatorAbilityStub::PreGetAddressByCoordinate;
80     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GET_FROM_LOCATION_NAME)] =
81         &LocatorAbilityStub::PreGetAddressByLocationName;
82     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK)] =
83         &LocatorAbilityStub::PreEnableReverseGeocodingMock;
84     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK)] =
85         &LocatorAbilityStub::PreDisableReverseGeocodingMock;
86     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO)] =
87         &LocatorAbilityStub::PreSetReverseGeocodingMockInfo;
88 #endif
89 #ifdef FEATURE_GNSS_SUPPORT
90     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_GNSS_STATUS_CALLBACK)] =
91         &LocatorAbilityStub::PreRegisterGnssStatusCallback;
92     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_GNSS_STATUS_CALLBACK)] =
93         &LocatorAbilityStub::PreUnregisterGnssStatusCallback;
94     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_NMEA_CALLBACK)] =
95         &LocatorAbilityStub::PreRegisterNmeaMessageCallback;
96     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_NMEA_CALLBACK)] =
97         &LocatorAbilityStub::PreUnregisterNmeaMessageCallback;
98     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_CACHED_CALLBACK)] =
99         &LocatorAbilityStub::PreStartCacheLocating;
100     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_CACHED_CALLBACK)] =
101         &LocatorAbilityStub::PreStopCacheLocating;
102     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::GET_CACHED_LOCATION_SIZE)] =
103         &LocatorAbilityStub::PreGetCachedGnssLocationsSize;
104     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::FLUSH_CACHED_LOCATIONS)] =
105         &LocatorAbilityStub::PreFlushCachedGnssLocations;
106     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::SEND_COMMAND)] =
107         &LocatorAbilityStub::PreSendCommand;
108     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::ADD_FENCE)] =
109         &LocatorAbilityStub::PreAddFence;
110     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REMOVE_FENCE)] =
111         &LocatorAbilityStub::PreRemoveFence;
112     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::REG_NMEA_CALLBACK_v9)] =
113         &LocatorAbilityStub::PreRegisterNmeaMessageCallbackV9;
114     locatorHandleMap_[static_cast<int>(LocatorInterfaceCode::UNREG_NMEA_CALLBACK_v9)] =
115         &LocatorAbilityStub::PreUnregisterNmeaMessageCallbackV9;
116 #endif
117 }
118 
119 
LocatorAbilityStub()120 LocatorAbilityStub::LocatorAbilityStub()
121 {
122     InitLocatorHandleMap();
123 }
124 
PreGetSwitchState(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)125 int LocatorAbilityStub::PreGetSwitchState(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
126 {
127     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
128     if (locatorAbility == nullptr) {
129         LBSLOGE(LOCATOR, "PreGetSwitchState: LocatorAbility is nullptr.");
130         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
131         return ERRCODE_SERVICE_UNAVAILABLE;
132     }
133     int state = DISABLED;
134     LocationErrCode errorCode = locatorAbility->GetSwitchState(state);
135     reply.WriteInt32(errorCode);
136     if (errorCode == ERRCODE_SUCCESS) {
137         reply.WriteInt32(state);
138     }
139     return errorCode;
140 }
141 
PreRegisterSwitchCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)142 int LocatorAbilityStub::PreRegisterSwitchCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
143 {
144     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
145     if (locatorAbility == nullptr) {
146         LBSLOGE(LOCATOR, "PreRegisterSwitchCallback: LocatorAbility is nullptr.");
147         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
148         return ERRCODE_SERVICE_UNAVAILABLE;
149     }
150     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
151     reply.WriteInt32(locatorAbility->RegisterSwitchCallback(client, identity.GetUid()));
152     return ERRCODE_SUCCESS;
153 }
154 
155 
PreStartLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)156 int LocatorAbilityStub::PreStartLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
157 {
158     if (!CheckLocationSwitchState(reply)) {
159         return ERRCODE_SWITCH_OFF;
160     }
161     if (!CheckLocationPermission(reply, identity)) {
162         return ERRCODE_PERMISSION_DENIED;
163     }
164     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
165     if (locatorAbility == nullptr) {
166         LBSLOGE(LOCATOR, "PreStartLocating: LocatorAbility is nullptr.");
167         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
168         return ERRCODE_SERVICE_UNAVAILABLE;
169     }
170     std::unique_ptr<RequestConfig> requestConfig = RequestConfig::Unmarshalling(data);
171     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
172     if (remoteObject == nullptr) {
173         LBSLOGE(LOCATOR, "StartLocating remote object nullptr");
174         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
175         return ERRCODE_SERVICE_UNAVAILABLE;
176     }
177 
178     sptr<IRemoteObject::DeathRecipient> death(new (std::nothrow) LocatorCallbackDeathRecipient());
179     remoteObject->AddDeathRecipient(death);
180     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
181     reply.WriteInt32(locatorAbility->StartLocating(requestConfig, callback, identity));
182     return ERRCODE_SUCCESS;
183 }
184 
PreStopLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)185 int LocatorAbilityStub::PreStopLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
186 {
187     if (!CheckLocationPermission(reply, identity)) {
188         return ERRCODE_PERMISSION_DENIED;
189     }
190     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
191     if (locatorAbility == nullptr) {
192         LBSLOGE(LOCATOR, "PreStopLocating: LocatorAbility is nullptr.");
193         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
194         return ERRCODE_SERVICE_UNAVAILABLE;
195     }
196     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
197     if (remoteObject == nullptr) {
198         LBSLOGE(LOCATOR, "LocatorAbility::StopLocating remote object nullptr");
199         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
200         return ERRCODE_SERVICE_UNAVAILABLE;
201     }
202     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
203     reply.WriteInt32(locatorAbility->StopLocating(callback));
204     return ERRCODE_SUCCESS;
205 }
206 
PreGetCacheLocation(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)207 int LocatorAbilityStub::PreGetCacheLocation(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
208 {
209     if (!CheckLocationSwitchState(reply)) {
210         return ERRCODE_SWITCH_OFF;
211     }
212     if (!CheckLocationPermission(reply, identity)) {
213         return ERRCODE_PERMISSION_DENIED;
214     }
215     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
216     if (locatorAbility == nullptr) {
217         LBSLOGE(LOCATOR, "PreGetCacheLocation: LocatorAbility is nullptr.");
218         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
219         return ERRCODE_SERVICE_UNAVAILABLE;
220     }
221     std::unique_ptr<Location> loc;
222     reply.WriteInt32(locatorAbility->GetCacheLocation(loc, identity));
223     if (loc != nullptr) {
224         loc->Marshalling(reply);
225     }
226     return ERRCODE_SUCCESS;
227 }
228 
PreEnableAbility(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)229 int LocatorAbilityStub::PreEnableAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
230 {
231     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
232         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
233             identity.ToString().c_str());
234         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
235         return ERRCODE_SYSTEM_PERMISSION_DENIED;
236     }
237     if (!CheckSettingsPermission(reply, identity)) {
238         return ERRCODE_PERMISSION_DENIED;
239     }
240     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
241     if (locatorAbility == nullptr) {
242         LBSLOGE(LOCATOR, "PreEnableAbility: LocatorAbility is nullptr.");
243         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
244         return ERRCODE_SERVICE_UNAVAILABLE;
245     }
246     bool isEnabled = data.ReadBool();
247     reply.WriteInt32(locatorAbility->EnableAbility(isEnabled));
248     return ERRCODE_SUCCESS;
249 }
250 
PreUpdateSaAbility(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)251 int LocatorAbilityStub::PreUpdateSaAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
252 {
253     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
254         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
255             identity.ToString().c_str());
256         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
257         return ERRCODE_SYSTEM_PERMISSION_DENIED;
258     }
259     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
260     if (locatorAbility == nullptr) {
261         LBSLOGE(LOCATOR, "PreUpdateSaAbility: LocatorAbility is nullptr.");
262         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
263         return ERRCODE_SERVICE_UNAVAILABLE;
264     }
265     reply.WriteInt32(locatorAbility->UpdateSaAbility());
266     return ERRCODE_SUCCESS;
267 }
268 
269 #ifdef FEATURE_GEOCODE_SUPPORT
PreIsGeoConvertAvailable(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)270 int LocatorAbilityStub::PreIsGeoConvertAvailable(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
271 {
272     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
273     if (locatorAbility == nullptr) {
274         LBSLOGE(LOCATOR, "PreIsGeoConvertAvailable: LocatorAbility is nullptr.");
275         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
276         return ERRCODE_SERVICE_UNAVAILABLE;
277     }
278     bool isAvailable = false;
279     reply.WriteInt32(locatorAbility->IsGeoConvertAvailable(isAvailable));
280     reply.WriteBool(isAvailable);
281     return ERRCODE_SUCCESS;
282 }
283 #endif
284 
285 #ifdef FEATURE_GEOCODE_SUPPORT
PreGetAddressByCoordinate(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)286 int LocatorAbilityStub::PreGetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
287 {
288     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
289     if (locatorAbility == nullptr) {
290         LBSLOGE(LOCATOR, "PreGetAddressByCoordinate: LocatorAbility is nullptr.");
291         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
292         return ERRCODE_SERVICE_UNAVAILABLE;
293     }
294     locatorAbility->GetAddressByCoordinate(data, reply, identity.GetBundleName());
295     return ERRCODE_SUCCESS;
296 }
297 #endif
298 
299 #ifdef FEATURE_GEOCODE_SUPPORT
PreGetAddressByLocationName(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)300 int LocatorAbilityStub::PreGetAddressByLocationName(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
301 {
302     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
303     if (locatorAbility == nullptr) {
304         LBSLOGE(LOCATOR, "PreGetAddressByLocationName: LocatorAbility is nullptr.");
305         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
306         return ERRCODE_SERVICE_UNAVAILABLE;
307     }
308     locatorAbility->GetAddressByLocationName(data, reply, identity.GetBundleName());
309     return ERRCODE_SUCCESS;
310 }
311 #endif
312 
PreUnregisterSwitchCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)313 int LocatorAbilityStub::PreUnregisterSwitchCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
314 {
315     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
316     if (locatorAbility == nullptr) {
317         LBSLOGE(LOCATOR, "PreUnregisterSwitchCallback: LocatorAbility is nullptr.");
318         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
319         return ERRCODE_SERVICE_UNAVAILABLE;
320     }
321     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
322     reply.WriteInt32(locatorAbility->UnregisterSwitchCallback(client));
323     return ERRCODE_SUCCESS;
324 }
325 
326 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterGnssStatusCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)327 int LocatorAbilityStub::PreRegisterGnssStatusCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
328 {
329     if (!CheckLocationPermission(reply, identity)) {
330         return ERRCODE_PERMISSION_DENIED;
331     }
332     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
333     if (locatorAbility == nullptr) {
334         LBSLOGE(LOCATOR, "PreRegisterGnssStatusCallback: LocatorAbility is nullptr.");
335         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
336         return ERRCODE_SERVICE_UNAVAILABLE;
337     }
338     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
339     reply.WriteInt32(locatorAbility->RegisterGnssStatusCallback(client, identity.GetUid()));
340     return ERRCODE_SUCCESS;
341 }
342 #endif
343 
344 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterGnssStatusCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)345 int LocatorAbilityStub::PreUnregisterGnssStatusCallback(MessageParcel &data,
346     MessageParcel &reply, AppIdentity &identity)
347 {
348     if (!CheckLocationPermission(reply, identity)) {
349         return ERRCODE_PERMISSION_DENIED;
350     }
351     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
352     if (locatorAbility == nullptr) {
353         LBSLOGE(LOCATOR, "PreUnregisterGnssStatusCallback: LocatorAbility is nullptr.");
354         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
355         return ERRCODE_SERVICE_UNAVAILABLE;
356     }
357     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
358     reply.WriteInt32(locatorAbility->UnregisterGnssStatusCallback(client));
359     return ERRCODE_SUCCESS;
360 }
361 #endif
362 
363 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterNmeaMessageCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)364 int LocatorAbilityStub::PreRegisterNmeaMessageCallback(MessageParcel &data,
365     MessageParcel &reply, AppIdentity &identity)
366 {
367     if (!CheckLocationPermission(reply, identity)) {
368         return ERRCODE_PERMISSION_DENIED;
369     }
370     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
371     if (locatorAbility == nullptr) {
372         LBSLOGE(LOCATOR, "PreRegisterNmeaMessageCallback: LocatorAbility is nullptr.");
373         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
374         return ERRCODE_SERVICE_UNAVAILABLE;
375     }
376     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
377     reply.WriteInt32(locatorAbility->RegisterNmeaMessageCallback(client, identity.GetUid()));
378     return ERRCODE_SUCCESS;
379 }
380 #endif
381 
382 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterNmeaMessageCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)383 int LocatorAbilityStub::PreUnregisterNmeaMessageCallback(MessageParcel &data,
384     MessageParcel &reply, AppIdentity &identity)
385 {
386     if (!CheckLocationPermission(reply, identity)) {
387         return ERRCODE_PERMISSION_DENIED;
388     }
389     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
390     if (locatorAbility == nullptr) {
391         LBSLOGE(LOCATOR, "PreUnregisterNmeaMessageCallback: LocatorAbility is nullptr.");
392         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
393         return ERRCODE_SERVICE_UNAVAILABLE;
394     }
395     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
396     reply.WriteInt32(locatorAbility->UnregisterNmeaMessageCallback(client));
397     return ERRCODE_SUCCESS;
398 }
399 #endif
400 
401 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterNmeaMessageCallbackV9(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)402 int LocatorAbilityStub::PreRegisterNmeaMessageCallbackV9(MessageParcel &data,
403     MessageParcel &reply, AppIdentity &identity)
404 {
405     if (!CheckPreciseLocationPermissions(reply, identity)) {
406         return ERRCODE_PERMISSION_DENIED;
407     }
408     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
409     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
410     if (locatorAbility == nullptr) {
411         LBSLOGE(LOCATOR, "PreRegisterNmeaMessageCallbackV9: LocatorAbility is nullptr.");
412         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
413         return ERRCODE_SERVICE_UNAVAILABLE;
414     }
415     reply.WriteInt32(locatorAbility->RegisterNmeaMessageCallback(client, identity.GetUid()));
416     return ERRCODE_SUCCESS;
417 }
418 #endif
419 
420 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterNmeaMessageCallbackV9(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)421 int LocatorAbilityStub::PreUnregisterNmeaMessageCallbackV9(MessageParcel &data,
422     MessageParcel &reply, AppIdentity &identity)
423 {
424     if (!CheckPreciseLocationPermissions(reply, identity)) {
425         return ERRCODE_PERMISSION_DENIED;
426     }
427     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
428     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
429     if (locatorAbility == nullptr) {
430         LBSLOGE(LOCATOR, "PreUnregisterNmeaMessageCallbackV9: LocatorAbility is nullptr.");
431         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
432         return ERRCODE_SERVICE_UNAVAILABLE;
433     }
434     reply.WriteInt32(locatorAbility->UnregisterNmeaMessageCallback(client));
435     return ERRCODE_SUCCESS;
436 }
437 #endif
438 
PreIsLocationPrivacyConfirmed(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)439 int LocatorAbilityStub::PreIsLocationPrivacyConfirmed(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
440 {
441     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
442         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
443             identity.ToString().c_str());
444         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
445         return ERRCODE_SYSTEM_PERMISSION_DENIED;
446     }
447     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
448     if (locatorAbility == nullptr) {
449         LBSLOGE(LOCATOR, "PreIsLocationPrivacyConfirmed: LocatorAbility is nullptr.");
450         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
451         return ERRCODE_SERVICE_UNAVAILABLE;
452     }
453     bool isConfirmed = false;
454     reply.WriteInt32(locatorAbility->IsLocationPrivacyConfirmed(data.ReadInt32(), isConfirmed));
455     reply.WriteBool(isConfirmed);
456     return ERRCODE_SUCCESS;
457 }
458 
PreSetLocationPrivacyConfirmStatus(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)459 int LocatorAbilityStub::PreSetLocationPrivacyConfirmStatus(MessageParcel &data,
460     MessageParcel &reply, AppIdentity &identity)
461 {
462     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
463         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
464             identity.ToString().c_str());
465         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
466         return ERRCODE_SYSTEM_PERMISSION_DENIED;
467     }
468     if (!CheckSettingsPermission(reply, identity)) {
469         return ERRCODE_PERMISSION_DENIED;
470     }
471 
472     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
473     if (locatorAbility == nullptr) {
474         LBSLOGE(LOCATOR, "PreSetLocationPrivacyConfirmStatus: LocatorAbility is nullptr.");
475         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
476         return ERRCODE_SERVICE_UNAVAILABLE;
477     }
478     reply.WriteInt32(locatorAbility->SetLocationPrivacyConfirmStatus(data.ReadInt32(),
479         data.ReadBool()));
480     return ERRCODE_SUCCESS;
481 }
482 
483 #ifdef FEATURE_GNSS_SUPPORT
PreStartCacheLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)484 int LocatorAbilityStub::PreStartCacheLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
485 {
486     if (!CheckLocationPermission(reply, identity)) {
487         return ERRCODE_PERMISSION_DENIED;
488     }
489     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
490     if (locatorAbility == nullptr) {
491         LBSLOGE(LOCATOR, "PreStartCacheLocating: LocatorAbility is nullptr.");
492         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
493         return ERRCODE_SERVICE_UNAVAILABLE;
494     }
495     if (!CheckLocationSwitchState(reply)) {
496         return ERRCODE_SWITCH_OFF;
497     }
498     std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
499     requestConfig->reportingPeriodSec = data.ReadInt32();
500     requestConfig->wakeUpCacheQueueFull = data.ReadBool();
501     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
502     std::string bundleName = Str16ToStr8(data.ReadString16());
503     if (remoteObject == nullptr) {
504         LBSLOGE(LOCATOR, "ParseDataAndStartCacheLocating remote object nullptr");
505         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
506         return ERRCODE_SERVICE_UNAVAILABLE;
507     }
508     if (bundleName.empty()) {
509         LBSLOGE(LOCATOR, "ParseDataAndStartCacheLocating get empty bundle name");
510         reply.WriteInt32(ERRCODE_INVALID_PARAM);
511         return ERRCODE_INVALID_PARAM;
512     }
513     sptr<ICachedLocationsCallback> callback = iface_cast<ICachedLocationsCallback>(remoteObject);
514     reply.WriteInt32(locatorAbility->RegisterCachedLocationCallback(requestConfig, callback, bundleName));
515     return ERRCODE_SUCCESS;
516 }
517 #endif
518 
519 #ifdef FEATURE_GNSS_SUPPORT
PreStopCacheLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)520 int LocatorAbilityStub::PreStopCacheLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
521 {
522     if (!CheckLocationPermission(reply, identity)) {
523         return ERRCODE_PERMISSION_DENIED;
524     }
525     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
526     if (locatorAbility == nullptr) {
527         LBSLOGE(LOCATOR, "PreStopCacheLocating: LocatorAbility is nullptr.");
528         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
529         return ERRCODE_SERVICE_UNAVAILABLE;
530     }
531     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
532     if (remoteObject == nullptr) {
533         LBSLOGE(LOCATOR, "LocatorAbility::ParseDataAndStopCacheLocating remote object nullptr");
534         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
535         return ERRCODE_SERVICE_UNAVAILABLE;
536     }
537     sptr<ICachedLocationsCallback> callback = iface_cast<ICachedLocationsCallback>(remoteObject);
538     reply.WriteInt32(locatorAbility->UnregisterCachedLocationCallback(callback));
539     return ERRCODE_SUCCESS;
540 }
541 #endif
542 
543 #ifdef FEATURE_GNSS_SUPPORT
PreGetCachedGnssLocationsSize(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)544 int LocatorAbilityStub::PreGetCachedGnssLocationsSize(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
545 {
546     if (!CheckLocationSwitchState(reply)) {
547         return ERRCODE_SWITCH_OFF;
548     }
549     if (!CheckLocationPermission(reply, identity)) {
550         return ERRCODE_PERMISSION_DENIED;
551     }
552     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
553     if (locatorAbility == nullptr) {
554         LBSLOGE(LOCATOR, "PreGetCachedGnssLocationsSize: LocatorAbility is nullptr.");
555         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
556         return ERRCODE_SERVICE_UNAVAILABLE;
557     }
558     int size = -1;
559     reply.WriteInt32(locatorAbility->GetCachedGnssLocationsSize(size));
560     reply.WriteInt32(size);
561     return ERRCODE_SUCCESS;
562 }
563 #endif
564 
565 #ifdef FEATURE_GNSS_SUPPORT
PreFlushCachedGnssLocations(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)566 int LocatorAbilityStub::PreFlushCachedGnssLocations(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
567 {
568     if (!CheckLocationSwitchState(reply)) {
569         return ERRCODE_SWITCH_OFF;
570     }
571     if (!CheckLocationPermission(reply, identity)) {
572         return ERRCODE_PERMISSION_DENIED;
573     }
574     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
575     if (locatorAbility == nullptr) {
576         LBSLOGE(LOCATOR, "PreFlushCachedGnssLocations: LocatorAbility is nullptr.");
577         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
578         return ERRCODE_SERVICE_UNAVAILABLE;
579     }
580     reply.WriteInt32(locatorAbility->FlushCachedGnssLocations());
581     return ERRCODE_SUCCESS;
582 }
583 #endif
584 
585 #ifdef FEATURE_GNSS_SUPPORT
PreSendCommand(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)586 int LocatorAbilityStub::PreSendCommand(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
587 {
588     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
589     if (locatorAbility == nullptr) {
590         LBSLOGE(LOCATOR, "PreSendCommand: LocatorAbility is nullptr.");
591         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
592         return ERRCODE_SERVICE_UNAVAILABLE;
593     }
594     std::unique_ptr<LocationCommand> locationCommand = std::make_unique<LocationCommand>();
595     locationCommand->scenario =  data.ReadInt32();
596     locationCommand->command = data.ReadBool();
597     reply.WriteInt32(locatorAbility->SendCommand(locationCommand));
598     return ERRCODE_SUCCESS;
599 }
600 #endif
601 
602 #ifdef FEATURE_GNSS_SUPPORT
PreAddFence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)603 int LocatorAbilityStub::PreAddFence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
604 {
605     if (!CheckLocationSwitchState(reply)) {
606         return ERRCODE_SWITCH_OFF;
607     }
608     if (!CheckLocationPermission(reply, identity)) {
609         return ERRCODE_PERMISSION_DENIED;
610     }
611     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
612     if (locatorAbility == nullptr) {
613         LBSLOGE(LOCATOR, "PreAddFence: LocatorAbility is nullptr.");
614         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
615         return ERRCODE_SERVICE_UNAVAILABLE;
616     }
617     std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
618     request->scenario = data.ReadInt32();
619     request->geofence.latitude = data.ReadDouble();
620     request->geofence.longitude = data.ReadDouble();
621     request->geofence.radius = data.ReadDouble();
622     request->geofence.expiration = data.ReadDouble();
623     reply.WriteInt32(locatorAbility->AddFence(request));
624     return ERRCODE_SUCCESS;
625 }
626 #endif
627 
628 #ifdef FEATURE_GNSS_SUPPORT
PreRemoveFence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)629 int LocatorAbilityStub::PreRemoveFence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
630 {
631     if (!CheckLocationSwitchState(reply)) {
632         return ERRCODE_SWITCH_OFF;
633     }
634     if (!CheckLocationPermission(reply, identity)) {
635         return ERRCODE_PERMISSION_DENIED;
636     }
637     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
638     if (locatorAbility == nullptr) {
639         LBSLOGE(LOCATOR, "PreRemoveFence: LocatorAbility is nullptr.");
640         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
641         return ERRCODE_SERVICE_UNAVAILABLE;
642     }
643     std::unique_ptr<GeofenceRequest> request = std::make_unique<GeofenceRequest>();
644     request->scenario = data.ReadInt32();
645     request->geofence.latitude = data.ReadDouble();
646     request->geofence.longitude = data.ReadDouble();
647     request->geofence.radius = data.ReadDouble();
648     request->geofence.expiration = data.ReadDouble();
649     reply.WriteInt32(locatorAbility->RemoveFence(request));
650     return ERRCODE_SUCCESS;
651 }
652 #endif
653 
PreEnableLocationMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)654 int LocatorAbilityStub::PreEnableLocationMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
655 {
656     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
657         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
658             identity.ToString().c_str());
659         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
660         return ERRCODE_SYSTEM_PERMISSION_DENIED;
661     }
662     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
663     if (locatorAbility == nullptr) {
664         LBSLOGE(LOCATOR, "PreEnableLocationMock: LocatorAbility is nullptr.");
665         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
666         return ERRCODE_SERVICE_UNAVAILABLE;
667     }
668     reply.WriteInt32(locatorAbility->EnableLocationMock());
669     return ERRCODE_SUCCESS;
670 }
671 
PreDisableLocationMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)672 int LocatorAbilityStub::PreDisableLocationMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
673 {
674     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
675         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
676             identity.ToString().c_str());
677         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
678         return ERRCODE_SYSTEM_PERMISSION_DENIED;
679     }
680     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
681     if (locatorAbility == nullptr) {
682         LBSLOGE(LOCATOR, "PreDisableLocationMock: LocatorAbility is nullptr.");
683         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
684         return ERRCODE_SERVICE_UNAVAILABLE;
685     }
686     reply.WriteInt32(locatorAbility->DisableLocationMock());
687     return ERRCODE_SUCCESS;
688 }
689 
PreSetMockedLocations(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)690 int LocatorAbilityStub::PreSetMockedLocations(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
691 {
692     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
693         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
694             identity.ToString().c_str());
695         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
696         return ERRCODE_SYSTEM_PERMISSION_DENIED;
697     }
698     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
699     if (locatorAbility == nullptr) {
700         LBSLOGE(LOCATOR, "PreSetMockedLocations: LocatorAbility is nullptr.");
701         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
702         return ERRCODE_SERVICE_UNAVAILABLE;
703     }
704     int timeInterval = data.ReadInt32();
705     int locationSize = data.ReadInt32();
706     timeInterval = timeInterval < 0 ? 1 : timeInterval;
707     locationSize = locationSize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX : locationSize;
708     std::vector<std::shared_ptr<Location>> vcLoc;
709     for (int i = 0; i < locationSize; i++) {
710         vcLoc.push_back(Location::UnmarshallingShared(data));
711     }
712     reply.WriteInt32(locatorAbility->SetMockedLocations(timeInterval, vcLoc));
713     return ERRCODE_SUCCESS;
714 }
715 
716 #ifdef FEATURE_GEOCODE_SUPPORT
PreEnableReverseGeocodingMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)717 int LocatorAbilityStub::PreEnableReverseGeocodingMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
718 {
719     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
720         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
721             identity.ToString().c_str());
722         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
723         return ERRCODE_SYSTEM_PERMISSION_DENIED;
724     }
725     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
726     if (locatorAbility == nullptr) {
727         LBSLOGE(LOCATOR, "PreEnableReverseGeocodingMock: LocatorAbility is nullptr.");
728         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
729         return ERRCODE_SERVICE_UNAVAILABLE;
730     }
731     reply.WriteInt32(locatorAbility->EnableReverseGeocodingMock());
732     return ERRCODE_SUCCESS;
733 }
734 #endif
735 
736 #ifdef FEATURE_GEOCODE_SUPPORT
PreDisableReverseGeocodingMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)737 int LocatorAbilityStub::PreDisableReverseGeocodingMock(MessageParcel &data,
738     MessageParcel &reply, AppIdentity &identity)
739 {
740     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
741         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
742             identity.ToString().c_str());
743         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
744         return ERRCODE_SYSTEM_PERMISSION_DENIED;
745     }
746     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
747     if (locatorAbility == nullptr) {
748         LBSLOGE(LOCATOR, "PreDisableReverseGeocodingMock: LocatorAbility is nullptr.");
749         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
750         return ERRCODE_SERVICE_UNAVAILABLE;
751     }
752     reply.WriteInt32(locatorAbility->DisableReverseGeocodingMock());
753     return ERRCODE_SUCCESS;
754 }
755 #endif
756 
757 #ifdef FEATURE_GEOCODE_SUPPORT
PreSetReverseGeocodingMockInfo(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)758 int LocatorAbilityStub::PreSetReverseGeocodingMockInfo(MessageParcel &data,
759     MessageParcel &reply, AppIdentity &identity)
760 {
761     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
762         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
763             identity.ToString().c_str());
764         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
765         return ERRCODE_SYSTEM_PERMISSION_DENIED;
766     }
767     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
768     if (locatorAbility == nullptr) {
769         LBSLOGE(LOCATOR, "PreSetReverseGeocodingMockInfo: LocatorAbility is nullptr.");
770         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
771         return ERRCODE_SERVICE_UNAVAILABLE;
772     }
773     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
774     int arraySize = data.ReadInt32();
775     arraySize = arraySize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX : arraySize;
776     for (int i = 0; i < arraySize; i++) {
777         std::shared_ptr<GeocodingMockInfo> info = std::make_shared<GeocodingMockInfo>();
778         info->ReadFromParcel(data);
779         mockInfo.push_back(info);
780     }
781     reply.WriteInt32(locatorAbility->SetReverseGeocodingMockInfo(mockInfo));
782     return ERRCODE_SUCCESS;
783 }
784 #endif
785 
PreProxyUidForFreeze(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)786 int LocatorAbilityStub::PreProxyUidForFreeze(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
787 {
788     if (!CheckLocationPermission(reply, identity)) {
789         return ERRCODE_PERMISSION_DENIED;
790     }
791     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
792     if (locatorAbility == nullptr) {
793         LBSLOGE(LOCATOR, "PreProxyUidForFreeze: LocatorAbility is nullptr.");
794         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
795         return ERRCODE_SERVICE_UNAVAILABLE;
796     }
797     if (identity.GetUid() != ROOT_UID) {
798         LBSLOGE(LOCATOR, "check root permission failed, [%{public}s]",
799             identity.ToString().c_str());
800         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
801         return ERRCODE_PERMISSION_DENIED;
802     }
803     int32_t uid = data.ReadInt32();
804     bool isProxy = data.ReadBool();
805     reply.WriteInt32(locatorAbility->ProxyUidForFreeze(uid, isProxy));
806     return ERRCODE_SUCCESS;
807 }
808 
PreResetAllProxy(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)809 int LocatorAbilityStub::PreResetAllProxy(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
810 {
811     if (!CheckLocationPermission(reply, identity)) {
812         return ERRCODE_PERMISSION_DENIED;
813     }
814     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
815     if (locatorAbility == nullptr) {
816         LBSLOGE(LOCATOR, "PreResetAllProxy: LocatorAbility is nullptr.");
817         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
818         return ERRCODE_SERVICE_UNAVAILABLE;
819     }
820     if (identity.GetUid() != ROOT_UID) {
821         LBSLOGE(LOCATOR, "check root permission failed, [%{public}s]",
822             identity.ToString().c_str());
823         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
824         return ERRCODE_PERMISSION_DENIED;
825     }
826     reply.WriteInt32(locatorAbility->ResetAllProxy());
827     return ERRCODE_SUCCESS;
828 }
829 
PreReportLocation(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)830 int LocatorAbilityStub::PreReportLocation(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
831 {
832     pid_t callingPid = IPCSkeleton::GetCallingPid();
833     pid_t callingUid = IPCSkeleton::GetCallingUid();
834     if (callingUid != static_cast<pid_t>(getuid()) || callingPid != getpid()) {
835         LBSLOGE(LOCATOR, "check system permission failed, [%{public}s]",
836             identity.ToString().c_str());
837         return ERRCODE_PERMISSION_DENIED;
838     }
839     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
840     if (locatorAbility == nullptr) {
841         LBSLOGE(LOCATOR, "PreReportLocation: LocatorAbility is nullptr.");
842         return ERRCODE_SERVICE_UNAVAILABLE;
843     }
844     std::string systemAbility = data.ReadString();
845     std::unique_ptr<Location> location = Location::Unmarshalling(data);
846     locatorAbility->ReportLocation(location, systemAbility);
847     return ERRCODE_SUCCESS;
848 }
849 
PreRegisterLocatingRequiredDataCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)850 int LocatorAbilityStub::PreRegisterLocatingRequiredDataCallback(MessageParcel &data,
851     MessageParcel &reply, AppIdentity &identity)
852 {
853     if (!CheckPreciseLocationPermissions(reply, identity)) {
854         return ERRCODE_PERMISSION_DENIED;
855     }
856     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
857         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
858             identity.ToString().c_str());
859         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
860         return ERRCODE_SYSTEM_PERMISSION_DENIED;
861     }
862     std::shared_ptr<LocatingRequiredDataConfig> dataConfig = LocatingRequiredDataConfig::Unmarshalling(data);
863     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
864     if (client == nullptr) {
865         LBSLOGE(LOCATOR, "%{public}s: client is nullptr.", __func__);
866         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
867         return ERRCODE_SERVICE_UNAVAILABLE;
868     }
869     auto locatorDataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
870     if (locatorDataManager == nullptr) {
871         LBSLOGE(LOCATOR, "%{public}s: locatorDataManager is nullptr.", __func__);
872         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
873         return ERRCODE_SERVICE_UNAVAILABLE;
874     }
875     LocationErrCode errorCode = locatorDataManager->RegisterCallback(dataConfig, client);
876 
877     reply.WriteInt32(errorCode);
878     return ERRCODE_SUCCESS;
879 }
880 
PreUnregisterLocatingRequiredDataCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)881 int LocatorAbilityStub::PreUnregisterLocatingRequiredDataCallback(MessageParcel &data,
882     MessageParcel &reply, AppIdentity &identity)
883 {
884     if (!CheckPreciseLocationPermissions(reply, identity)) {
885         return ERRCODE_PERMISSION_DENIED;
886     }
887     if (!CommonUtils::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
888         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
889             identity.ToString().c_str());
890         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
891         return ERRCODE_SYSTEM_PERMISSION_DENIED;
892     }
893     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
894     if (client == nullptr) {
895         LBSLOGE(LOCATOR, "%{public}s: client is nullptr.", __func__);
896         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
897         return ERRCODE_SERVICE_UNAVAILABLE;
898     }
899     auto locatorDataManager = DelayedSingleton<LocatorRequiredDataManager>::GetInstance();
900     if (locatorDataManager == nullptr) {
901         LBSLOGE(LOCATOR, "%{public}s: locatorDataManager is nullptr.", __func__);
902         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
903         return ERRCODE_SERVICE_UNAVAILABLE;
904     }
905     LocationErrCode errorCode = locatorDataManager->UnregisterCallback(client);
906 
907     reply.WriteInt32(errorCode);
908     return ERRCODE_SUCCESS;
909 }
910 
CheckLocationPermission(MessageParcel & reply,AppIdentity & identity)911 bool LocatorAbilityStub::CheckLocationPermission(MessageParcel &reply, AppIdentity &identity)
912 {
913     uint32_t callingTokenId = identity.GetTokenId();
914     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
915     if (!CommonUtils::CheckLocationPermission(callingTokenId, callingFirstTokenid) &&
916         !CommonUtils::CheckApproximatelyPermission(callingTokenId, callingFirstTokenid)) {
917         LBSLOGE(LOCATOR, "CheckLocationPermission return false.");
918         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
919         return false;
920     } else {
921         return true;
922     }
923 }
924 
CheckPreciseLocationPermissions(MessageParcel & reply,AppIdentity & identity)925 bool LocatorAbilityStub::CheckPreciseLocationPermissions(MessageParcel &reply, AppIdentity &identity)
926 {
927     uint32_t callingTokenId = identity.GetTokenId();
928     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
929     if (!CommonUtils::CheckLocationPermission(callingTokenId, callingFirstTokenid) ||
930         !CommonUtils::CheckApproximatelyPermission(callingTokenId, callingFirstTokenid)) {
931         LBSLOGE(LOCATOR, "CheckPreciseLocationPermissions return false.");
932         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
933         return false;
934     } else {
935         return true;
936     }
937 }
938 
CheckSettingsPermission(MessageParcel & reply,AppIdentity & identity)939 bool LocatorAbilityStub::CheckSettingsPermission(MessageParcel &reply, AppIdentity &identity)
940 {
941     uint32_t callingTokenId = identity.GetTokenId();
942     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
943     if (!CommonUtils::CheckSecureSettings(callingTokenId, callingFirstTokenid)) {
944         LBSLOGE(LOCATOR, "has no access permission, CheckSecureSettings return false");
945         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
946         return false;
947     } else {
948         return true;
949     }
950 }
951 
CheckLocationSwitchState(MessageParcel & reply)952 bool LocatorAbilityStub::CheckLocationSwitchState(MessageParcel &reply)
953 {
954     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
955     if (locatorAbility == nullptr) {
956         LBSLOGE(LOCATOR, "CheckLocationSwitchState: LocatorAbility is nullptr.");
957         return false;
958     }
959     int state = DISABLED;
960     locatorAbility->GetSwitchState(state);
961     if (state == DISABLED) {
962         LBSLOGE(LOCATOR, "switch state is off.");
963         reply.WriteInt32(ERRCODE_SWITCH_OFF);
964         return false;
965     }
966     return true;
967 }
968 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)969 int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
970     MessageParcel &data, MessageParcel &reply, MessageOption &option)
971 {
972     int ret = ERRCODE_SUCCESS;
973     pid_t callingPid = IPCSkeleton::GetCallingPid();
974     pid_t callingUid = IPCSkeleton::GetCallingUid();
975     uint32_t callingTokenId = IPCSkeleton::GetCallingTokenID();
976     uint64_t callingTokenIdEx = IPCSkeleton::GetCallingFullTokenID();
977     uint32_t callingFirstTokenid = IPCSkeleton::GetFirstTokenID();
978 
979     AppIdentity identity;
980     identity.SetPid(callingPid);
981     identity.SetUid(callingUid);
982     identity.SetTokenId(callingTokenId);
983     identity.SetTokenIdEx(callingTokenIdEx);
984     identity.SetFirstTokenId(callingFirstTokenid);
985     std::string bundleName = "";
986     if (!CommonUtils::GetBundleNameByUid(callingUid, bundleName)) {
987         LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", callingUid);
988     }
989     identity.SetBundleName(bundleName);
990     LBSLOGI(LOCATOR, "OnReceived cmd = %{public}u, flags= %{public}d, identity= [%{public}s]",
991         code, option.GetFlags(), identity.ToString().c_str());
992     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
993 
994     if (data.ReadInterfaceToken() != GetDescriptor()) {
995         LBSLOGE(LOCATOR, "invalid token.");
996         IPCSkeleton::SetCallingIdentity(callingIdentity);
997         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
998         return ERRCODE_SERVICE_UNAVAILABLE;
999     }
1000 
1001     auto handleFunc = locatorHandleMap_.find(code);
1002     if (handleFunc != locatorHandleMap_.end() && handleFunc->second != nullptr) {
1003         auto memberFunc = handleFunc->second;
1004         ret = (this->*memberFunc)(data, reply, identity);
1005     } else {
1006         LBSLOGE(LOCATOR, "OnReceived cmd = %{public}u, unsupport service.", code);
1007 #if !defined(FEATURE_GNSS_SUPPORT) || !defined(FEATURE_GEOCODE_SUPPORT)
1008         reply.WriteInt32(ERRCODE_NOT_SUPPORTED);
1009 #endif
1010         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
1011     }
1012     IPCSkeleton::SetCallingIdentity(callingIdentity);
1013     UnloadLocatorSa();
1014     return ret;
1015 }
1016 
SaDumpInfo(std::string & result)1017 void LocatorAbilityStub::SaDumpInfo(std::string& result)
1018 {
1019     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
1020     if (locatorAbility == nullptr) {
1021         LBSLOGE(LOCATOR, "SaDumpInfo: LocatorAbility is nullptr.");
1022         return;
1023     }
1024     int state = DISABLED;
1025     locatorAbility->GetSwitchState(state);
1026     result += "Location switch state: ";
1027     std::string status = state ? "on" : "off";
1028     result += status + "\n";
1029 }
1030 
Dump(int32_t fd,const std::vector<std::u16string> & args)1031 int32_t LocatorAbilityStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
1032 {
1033     std::vector<std::string> vecArgs;
1034     std::transform(args.begin(), args.end(), std::back_inserter(vecArgs), [](const std::u16string &arg) {
1035         return Str16ToStr8(arg);
1036     });
1037 
1038     LocationDumper dumper;
1039     std::string result;
1040     dumper.LocatorDump(SaDumpInfo, vecArgs, result);
1041     if (!SaveStringToFd(fd, result)) {
1042         LBSLOGE(LOCATOR, "Gnss save string to fd failed!");
1043         return ERR_OK;
1044     }
1045     return ERR_OK;
1046 }
1047 
UnloadLocatorSa()1048 bool LocatorAbilityStub::UnloadLocatorSa()
1049 {
1050     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
1051     if (locatorAbility == nullptr) {
1052         LBSLOGE(LOCATOR, "%{public}s: LocatorAbility is nullptr.", __func__);
1053         return false;
1054     }
1055     locatorAbility->UnloadSaAbility();
1056     return true;
1057 }
1058 
LocatorCallbackDeathRecipient()1059 LocatorCallbackDeathRecipient::LocatorCallbackDeathRecipient()
1060 {
1061 }
1062 
~LocatorCallbackDeathRecipient()1063 LocatorCallbackDeathRecipient::~LocatorCallbackDeathRecipient()
1064 {
1065 }
1066 
OnRemoteDied(const wptr<IRemoteObject> & remote)1067 void LocatorCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1068 {
1069     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remote.promote());
1070     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
1071     if (locatorAbility != nullptr) {
1072         locatorAbility->StopLocating(callback);
1073         locatorAbility->UnloadSaAbility();
1074         LBSLOGI(LOCATOR, "locator callback OnRemoteDied");
1075     }
1076 }
1077 
SwitchCallbackDeathRecipient()1078 SwitchCallbackDeathRecipient::SwitchCallbackDeathRecipient()
1079 {
1080 }
1081 
~SwitchCallbackDeathRecipient()1082 SwitchCallbackDeathRecipient::~SwitchCallbackDeathRecipient()
1083 {
1084 }
1085 
OnRemoteDied(const wptr<IRemoteObject> & remote)1086 void SwitchCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1087 {
1088     auto locatorAbility = DelayedSingleton<LocatorAbility>::GetInstance();
1089     if (locatorAbility != nullptr) {
1090         locatorAbility->UnregisterSwitchCallback(remote.promote());
1091         locatorAbility->UnloadSaAbility();
1092         LBSLOGI(LOCATOR, "switch callback OnRemoteDied");
1093     }
1094 }
1095 } // namespace Location
1096 } // namespace OHOS