1 /*
2  * Copyright (C) 2022-2024 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 "common_hisysevent.h"
23 #include "location.h"
24 #include "locator_ability.h"
25 #include "location_dumper.h"
26 #include "location_config_manager.h"
27 #include "request_config.h"
28 #include "system_ability_definition.h"
29 #include "location_sa_load_manager.h"
30 
31 #include "locating_required_data_config.h"
32 #include "locator_required_data_manager.h"
33 #include "location_log_event_ids.h"
34 #include "geofence_request.h"
35 #include "permission_manager.h"
36 #include "hook_utils.h"
37 #include "location_data_rdb_manager.h"
38 
39 namespace OHOS {
40 namespace Location {
41 const int DEFAULT_USERID = 100;
42 
InitLocatorHandleMap()43 void LocatorAbilityStub::InitLocatorHandleMap()
44 {
45     if (locatorHandleMap_.size() != 0) {
46         return;
47     }
48     ConstructLocatorHandleMap();
49     ConstructLocatorEnhanceHandleMap();
50     ConstructLocatorMockHandleMap();
51     ConstructGeocodeHandleMap();
52     ConstructGnssHandleMap();
53     ConstructGnssEnhanceHandleMap();
54 }
55 
ConstructLocatorHandleMap()56 void LocatorAbilityStub::ConstructLocatorHandleMap()
57 {
58     locatorHandleMap_[LocatorInterfaceCode::GET_SWITCH_STATE] =
59         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
60         return PreGetSwitchState(data, reply, identity);
61         };
62     locatorHandleMap_[LocatorInterfaceCode::REG_SWITCH_CALLBACK] =
63         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
64         return PreRegisterSwitchCallback(data, reply, identity);
65         };
66     locatorHandleMap_[LocatorInterfaceCode::UNREG_SWITCH_CALLBACK] =
67         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
68         return PreUnregisterSwitchCallback(data, reply, identity);
69         };
70     locatorHandleMap_[LocatorInterfaceCode::START_LOCATING] =
71         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
72         return PreStartLocating(data, reply, identity);
73         };
74     locatorHandleMap_[LocatorInterfaceCode::STOP_LOCATING] =
75         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
76         return PreStopLocating(data, reply, identity);
77         };
78     locatorHandleMap_[LocatorInterfaceCode::GET_CACHE_LOCATION] =
79         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
80         return PreGetCacheLocation(data, reply, identity);
81         };
82     locatorHandleMap_[LocatorInterfaceCode::ENABLE_ABILITY] =
83         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
84         return PreEnableAbility(data, reply, identity);
85         };
86     locatorHandleMap_[LocatorInterfaceCode::ENABLE_ABILITY_BY_USERID] =
87         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
88         return PreEnableAbilityForUser(data, reply, identity);
89         };
90 }
91 
ConstructLocatorEnhanceHandleMap()92 void LocatorAbilityStub::ConstructLocatorEnhanceHandleMap()
93 {
94     locatorHandleMap_[LocatorInterfaceCode::UPDATE_SA_ABILITY] =
95         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
96         return PreUpdateSaAbility(data, reply, identity);
97         };
98     locatorHandleMap_[LocatorInterfaceCode::IS_PRIVACY_COMFIRMED] =
99         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
100         return PreIsLocationPrivacyConfirmed(data, reply, identity);
101         };
102     locatorHandleMap_[LocatorInterfaceCode::SET_PRIVACY_COMFIRM_STATUS] =
103         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
104         return PreSetLocationPrivacyConfirmStatus(data, reply, identity);
105         };
106     locatorHandleMap_[LocatorInterfaceCode::PROXY_PID_FOR_FREEZE] =
107         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
108         return PreProxyForFreeze(data, reply, identity);
109         };
110     locatorHandleMap_[LocatorInterfaceCode::RESET_ALL_PROXY] =
111         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
112         return PreResetAllProxy(data, reply, identity);
113         };
114     locatorHandleMap_[LocatorInterfaceCode::REPORT_LOCATION] =
115         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
116         return PreReportLocation(data, reply, identity);
117         };
118     locatorHandleMap_[LocatorInterfaceCode::REG_LOCATING_REQUIRED_DATA_CALLBACK] =
119         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
120         return PreRegisterLocatingRequiredDataCallback(data, reply, identity);
121         };
122     locatorHandleMap_[LocatorInterfaceCode::UNREG_LOCATING_REQUIRED_DATA_CALLBACK] =
123         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
124         return PreUnregisterLocatingRequiredDataCallback(data, reply, identity);
125         };
126     locatorHandleMap_[LocatorInterfaceCode::REG_LOCATION_ERROR] =
127         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
128         return PreRegisterLocationError(data, reply, identity);
129         };
130     locatorHandleMap_[LocatorInterfaceCode::UNREG_LOCATION_ERROR] =
131         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
132         return PreUnregisterLocationError(data, reply, identity);
133         };
134     locatorHandleMap_[LocatorInterfaceCode::REPORT_LOCATION_ERROR] =
135         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
136         return PreReportLocationError(data, reply, identity);
137         };
138     locatorHandleMap_[LocatorInterfaceCode::GET_CURRENT_WIFI_BSSID_FOR_LOCATING] =
139         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
140         return PreGetCurrentWifiBssidForLocating(data, reply, identity);
141         };
142 }
143 
ConstructLocatorMockHandleMap()144 void LocatorAbilityStub::ConstructLocatorMockHandleMap()
145 {
146     locatorHandleMap_[LocatorInterfaceCode::ENABLE_LOCATION_MOCK] =
147         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
148         return PreEnableLocationMock(data, reply, identity);
149         };
150     locatorHandleMap_[LocatorInterfaceCode::DISABLE_LOCATION_MOCK] =
151         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
152         return PreDisableLocationMock(data, reply, identity);
153         };
154     locatorHandleMap_[LocatorInterfaceCode::SET_MOCKED_LOCATIONS] =
155         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
156         return PreSetMockedLocations(data, reply, identity);
157         };
158 }
159 
ConstructGeocodeHandleMap()160 void LocatorAbilityStub::ConstructGeocodeHandleMap()
161 {
162 #ifdef FEATURE_GEOCODE_SUPPORT
163     locatorHandleMap_[LocatorInterfaceCode::GEO_IS_AVAILABLE] =
164         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
165         return PreIsGeoConvertAvailable(data, reply, identity);
166         };
167     locatorHandleMap_[LocatorInterfaceCode::GET_FROM_COORDINATE] =
168         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
169         return PreGetAddressByCoordinate(data, reply, identity);
170             };
171     locatorHandleMap_[LocatorInterfaceCode::GET_FROM_LOCATION_NAME] =
172         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
173         return PreGetAddressByLocationName(data, reply, identity);
174     };
175     locatorHandleMap_[LocatorInterfaceCode::ENABLE_REVERSE_GEOCODE_MOCK] =
176         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
177         return PreEnableReverseGeocodingMock(data, reply, identity);
178         };
179     locatorHandleMap_[LocatorInterfaceCode::DISABLE_REVERSE_GEOCODE_MOCK] =
180         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
181         return PreDisableReverseGeocodingMock(data, reply, identity);
182         };
183     locatorHandleMap_[LocatorInterfaceCode::SET_REVERSE_GEOCODE_MOCKINFO] =
184         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
185         return PreSetReverseGeocodingMockInfo(data, reply, identity);
186         };
187 #endif
188 }
189 
ConstructGnssHandleMap()190 void LocatorAbilityStub::ConstructGnssHandleMap()
191 {
192 #ifdef FEATURE_GNSS_SUPPORT
193     locatorHandleMap_[LocatorInterfaceCode::REG_GNSS_STATUS_CALLBACK] =
194         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
195         return PreRegisterGnssStatusCallback(data, reply, identity);
196         };
197     locatorHandleMap_[LocatorInterfaceCode::UNREG_GNSS_STATUS_CALLBACK] =
198         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
199         return PreUnregisterGnssStatusCallback(data, reply, identity);
200         };
201     locatorHandleMap_[LocatorInterfaceCode::REG_NMEA_CALLBACK] =
202         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
203         return PreRegisterNmeaMessageCallback(data, reply, identity);
204         };
205     locatorHandleMap_[LocatorInterfaceCode::UNREG_NMEA_CALLBACK] =
206         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
207         return PreUnregisterNmeaMessageCallback(data, reply, identity);
208         };
209     locatorHandleMap_[LocatorInterfaceCode::REG_CACHED_CALLBACK] =
210         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
211         return PreStartCacheLocating(data, reply, identity);
212         };
213     locatorHandleMap_[LocatorInterfaceCode::UNREG_CACHED_CALLBACK] =
214         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
215         return PreStopCacheLocating(data, reply, identity);
216         };
217     locatorHandleMap_[LocatorInterfaceCode::GET_CACHED_LOCATION_SIZE] =
218         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
219         return PreGetCachedGnssLocationsSize(data, reply, identity);
220         };
221     locatorHandleMap_[LocatorInterfaceCode::FLUSH_CACHED_LOCATIONS] =
222         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
223         return PreFlushCachedGnssLocations(data, reply, identity);
224         };
225     locatorHandleMap_[LocatorInterfaceCode::SEND_COMMAND] =
226         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
227         return PreSendCommand(data, reply, identity);
228         };
229     locatorHandleMap_[LocatorInterfaceCode::REG_NMEA_CALLBACK_V9] =
230         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
231         return PreRegisterNmeaMessageCallbackV9(data, reply, identity);
232         };
233     locatorHandleMap_[LocatorInterfaceCode::UNREG_NMEA_CALLBACK_V9] =
234         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
235         return PreUnregisterNmeaMessageCallbackV9(data, reply, identity);
236         };
237 #endif
238 }
239 
ConstructGnssEnhanceHandleMap()240 void LocatorAbilityStub::ConstructGnssEnhanceHandleMap()
241 {
242 #ifdef FEATURE_GNSS_SUPPORT
243     locatorHandleMap_[LocatorInterfaceCode::ADD_FENCE] =
244         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
245         return PreAddFence(data, reply, identity);
246         };
247     locatorHandleMap_[LocatorInterfaceCode::REMOVE_FENCE] =
248         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
249         return PreRemoveFence(data, reply, identity);
250         };
251     locatorHandleMap_[LocatorInterfaceCode::ADD_GNSS_GEOFENCE] =
252         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
253         return PreAddGnssGeofence(data, reply, identity);
254         };
255     locatorHandleMap_[LocatorInterfaceCode::REMOVE_GNSS_GEOFENCE] =
256         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
257         return PreRemoveGnssGeofence(data, reply, identity);
258         };
259     locatorHandleMap_[LocatorInterfaceCode::GET_GEOFENCE_SUPPORT_COORDINATE_SYSTEM_TYPE] =
260         [this](MessageParcel &data, MessageParcel &reply, AppIdentity &identity) {
261         return PreQuerySupportCoordinateSystemType(data, reply, identity);
262         };
263 #endif
264 }
265 
LocatorAbilityStub()266 LocatorAbilityStub::LocatorAbilityStub()
267 {
268     InitLocatorHandleMap();
269 }
270 
PreGetSwitchState(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)271 int LocatorAbilityStub::PreGetSwitchState(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
272 {
273     int state = DEFAULT_SWITCH_STATE;
274     LocationErrCode errorCode =  LocatorAbility::GetInstance()->GetSwitchState(state);
275     reply.WriteInt32(errorCode);
276     if (errorCode == ERRCODE_SUCCESS) {
277         reply.WriteInt32(state);
278     }
279     return errorCode;
280 }
281 
PreRegisterSwitchCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)282 int LocatorAbilityStub::PreRegisterSwitchCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
283 {
284     auto locatorAbility = LocatorAbility::GetInstance();
285     if (locatorAbility == nullptr) {
286         LBSLOGE(LOCATOR, "PreRegisterSwitchCallback: LocatorAbility is nullptr.");
287         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
288         return ERRCODE_SERVICE_UNAVAILABLE;
289     }
290     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
291     reply.WriteInt32(locatorAbility->RegisterSwitchCallback(client, identity.GetUid()));
292     return ERRCODE_SUCCESS;
293 }
294 
295 
PreStartLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)296 int LocatorAbilityStub::PreStartLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
297 {
298     if (!CheckLocationSwitchState(reply)) {
299         return ERRCODE_SWITCH_OFF;
300     }
301     if (!CheckLocationPermission(reply, identity)) {
302         return ERRCODE_PERMISSION_DENIED;
303     }
304     bool res = HookUtils::ExecuteHookWhenPreStartLocating(identity.GetBundleName());
305     auto reportManager = ReportManager::GetInstance();
306     if (reportManager != nullptr && res) {
307         if (reportManager->IsAppBackground(identity.GetBundleName(), identity.GetTokenId(),
308             identity.GetTokenIdEx(), identity.GetUid(), identity.GetPid()) &&
309             !PermissionManager::CheckBackgroundPermission(identity.GetTokenId(), identity.GetFirstTokenId())) {
310             reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
311             return ERRCODE_PERMISSION_DENIED;
312         }
313     }
314     auto locatorAbility = LocatorAbility::GetInstance();
315     if (locatorAbility == nullptr) {
316         LBSLOGE(LOCATOR, "PreStartLocating: LocatorAbility is nullptr.");
317         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
318         return ERRCODE_SERVICE_UNAVAILABLE;
319     }
320     std::unique_ptr<RequestConfig> requestConfig = RequestConfig::Unmarshalling(data);
321     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
322     if (remoteObject == nullptr) {
323         LBSLOGE(LOCATOR, "StartLocating remote object nullptr");
324         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
325         return ERRCODE_SERVICE_UNAVAILABLE;
326     }
327 
328     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
329     reply.WriteInt32(locatorAbility->StartLocating(requestConfig, callback, identity));
330     return ERRCODE_SUCCESS;
331 }
332 
PreStopLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)333 int LocatorAbilityStub::PreStopLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
334 {
335     if (!CheckLocationPermission(reply, identity)) {
336         return ERRCODE_PERMISSION_DENIED;
337     }
338     auto locatorAbility = LocatorAbility::GetInstance();
339     if (locatorAbility == nullptr) {
340         LBSLOGE(LOCATOR, "PreStopLocating: LocatorAbility is nullptr.");
341         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
342         return ERRCODE_SERVICE_UNAVAILABLE;
343     }
344     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
345     if (remoteObject == nullptr) {
346         LBSLOGE(LOCATOR, "LocatorAbility::StopLocating remote object nullptr");
347         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
348         return ERRCODE_SERVICE_UNAVAILABLE;
349     }
350     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
351     reply.WriteInt32(locatorAbility->StopLocating(callback));
352     return ERRCODE_SUCCESS;
353 }
354 
PreGetCacheLocation(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)355 int LocatorAbilityStub::PreGetCacheLocation(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
356 {
357     if (!CheckLocationSwitchState(reply)) {
358         return ERRCODE_SWITCH_OFF;
359     }
360     if (!CheckLocationPermission(reply, identity)) {
361         return ERRCODE_PERMISSION_DENIED;
362     }
363     auto locatorAbility = LocatorAbility::GetInstance();
364     if (locatorAbility == nullptr) {
365         LBSLOGE(LOCATOR, "PreGetCacheLocation: LocatorAbility is nullptr.");
366         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
367         return ERRCODE_SERVICE_UNAVAILABLE;
368     }
369     std::unique_ptr<Location> loc;
370     reply.WriteInt32(locatorAbility->GetCacheLocation(loc, identity));
371     if (loc != nullptr) {
372         loc->Marshalling(reply);
373     }
374     return ERRCODE_SUCCESS;
375 }
376 
PreEnableAbility(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)377 int LocatorAbilityStub::PreEnableAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
378 {
379     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
380         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
381             identity.ToString().c_str());
382         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
383         return ERRCODE_SYSTEM_PERMISSION_DENIED;
384     }
385     if (!CheckSettingsPermission(reply, identity)) {
386         return ERRCODE_PERMISSION_DENIED;
387     }
388     auto locatorAbility = LocatorAbility::GetInstance();
389     if (locatorAbility == nullptr) {
390         LBSLOGE(LOCATOR, "PreEnableAbility: LocatorAbility is nullptr.");
391         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
392         return ERRCODE_SERVICE_UNAVAILABLE;
393     }
394     bool isEnabled = data.ReadBool();
395     bool privacyState = false;
396     LocationErrCode code =
397         LocationConfigManager::GetInstance()->GetPrivacyTypeState(PRIVACY_TYPE_STARTUP, privacyState);
398     if (code == ERRCODE_SUCCESS && isEnabled && !privacyState && identity.GetBundleName() == "com.ohos.sceneboard") {
399         LocationConfigManager::GetInstance()->OpenPrivacyDialog();
400         LBSLOGE(LOCATOR, "OpenPrivacyDialog");
401         return ERRCODE_SERVICE_UNAVAILABLE;
402     }
403     int userId = 0;
404     if (!CommonUtils::GetCurrentUserId(userId)) {
405         userId = DEFAULT_USERID;
406     }
407     if (!HookUtils::ExecuteHookEnableAbility(
408         identity.GetBundleName().size() == 0 ? std::to_string(identity.GetUid()) : identity.GetBundleName(),
409         isEnabled, userId)) {
410         return ERRCODE_SUCCESS;
411     }
412     LocationErrCode errCode = locatorAbility->EnableAbility(isEnabled);
413     std::string bundleName;
414     bool result = LocationConfigManager::GetInstance()->GetSettingsBundleName(bundleName);
415     // settings first enable location, need to update privacy state
416     if (code == ERRCODE_SUCCESS && errCode == ERRCODE_SUCCESS && isEnabled && !privacyState &&
417         result && !bundleName.empty() && identity.GetBundleName() == bundleName) {
418         LocationConfigManager::GetInstance()->SetPrivacyTypeState(PRIVACY_TYPE_STARTUP, true);
419     }
420     reply.WriteInt32(errCode);
421     return ERRCODE_SUCCESS;
422 }
423 
PreEnableAbilityForUser(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)424 int LocatorAbilityStub::PreEnableAbilityForUser(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
425 {
426     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
427         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
428             identity.ToString().c_str());
429         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
430         return ERRCODE_SYSTEM_PERMISSION_DENIED;
431     }
432     if (!CheckSettingsPermission(reply, identity)) {
433         return ERRCODE_PERMISSION_DENIED;
434     }
435     bool isEnabled = data.ReadBool();
436     int32_t userId = data.ReadInt32();
437     bool privacyState = false;
438     int currentUserId = 0;
439     LocationErrCode code =
440         LocationConfigManager::GetInstance()->GetPrivacyTypeState(PRIVACY_TYPE_STARTUP, privacyState);
441     if (code == ERRCODE_SUCCESS && isEnabled && !privacyState && identity.GetBundleName() == "com.ohos.sceneboard" &&
442         (CommonUtils::GetCurrentUserId(currentUserId) && userId == currentUserId)) {
443         LocationConfigManager::GetInstance()->OpenPrivacyDialog();
444         LBSLOGE(LOCATOR, "OpenPrivacyDialog");
445         return ERRCODE_SERVICE_UNAVAILABLE;
446     }
447     if (!HookUtils::ExecuteHookEnableAbility(
448         identity.GetBundleName().size() == 0 ? std::to_string(identity.GetUid()) : identity.GetBundleName(),
449         isEnabled, userId)) {
450         return ERRCODE_SUCCESS;
451     }
452     LocationErrCode errCode = LocatorAbility::GetInstance()->EnableAbilityForUser(isEnabled, userId);
453     std::string bundleName;
454     bool result = LocationConfigManager::GetInstance()->GetSettingsBundleName(bundleName);
455     // settings first enable location, need to update privacy state
456     if (code == ERRCODE_SUCCESS && errCode == ERRCODE_SUCCESS && isEnabled && !privacyState &&
457         result && !bundleName.empty() && identity.GetBundleName() == bundleName &&
458         (CommonUtils::GetCurrentUserId(currentUserId) && userId == currentUserId)) {
459         LocationConfigManager::GetInstance()->SetPrivacyTypeState(PRIVACY_TYPE_STARTUP, true);
460     }
461     reply.WriteInt32(errCode);
462     return ERRCODE_SUCCESS;
463 }
464 
PreUpdateSaAbility(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)465 int LocatorAbilityStub::PreUpdateSaAbility(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
466 {
467     pid_t callingPid = IPCSkeleton::GetCallingPid();
468     pid_t callingUid = IPCSkeleton::GetCallingUid();
469     if (callingUid != static_cast<pid_t>(getuid()) || callingPid != getpid()) {
470         LBSLOGE(LOCATOR, "check permission failed, [%{public}s]", identity.ToString().c_str());
471         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
472         return ERRCODE_PERMISSION_DENIED;
473     }
474     auto locatorAbility = LocatorAbility::GetInstance();
475     if (locatorAbility == nullptr) {
476         LBSLOGE(LOCATOR, "PreUpdateSaAbility: LocatorAbility is nullptr.");
477         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
478         return ERRCODE_SERVICE_UNAVAILABLE;
479     }
480     reply.WriteInt32(locatorAbility->UpdateSaAbility());
481     return ERRCODE_SUCCESS;
482 }
483 
484 #ifdef FEATURE_GEOCODE_SUPPORT
PreIsGeoConvertAvailable(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)485 int LocatorAbilityStub::PreIsGeoConvertAvailable(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
486 {
487     auto locatorAbility = LocatorAbility::GetInstance();
488     if (locatorAbility == nullptr) {
489         LBSLOGE(LOCATOR, "PreIsGeoConvertAvailable: LocatorAbility is nullptr.");
490         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
491         return ERRCODE_SERVICE_UNAVAILABLE;
492     }
493     bool isAvailable = false;
494     reply.WriteInt32(locatorAbility->IsGeoConvertAvailable(isAvailable));
495     reply.WriteBool(isAvailable);
496     return ERRCODE_SUCCESS;
497 }
498 #endif
499 
500 #ifdef FEATURE_GEOCODE_SUPPORT
PreGetAddressByCoordinate(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)501 int LocatorAbilityStub::PreGetAddressByCoordinate(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
502 {
503     auto locatorAbility = LocatorAbility::GetInstance();
504     if (locatorAbility == nullptr) {
505         LBSLOGE(LOCATOR, "PreGetAddressByCoordinate: LocatorAbility is nullptr.");
506         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
507         return ERRCODE_SERVICE_UNAVAILABLE;
508     }
509     locatorAbility->GetAddressByCoordinate(data, reply, identity.GetBundleName());
510     return ERRCODE_SUCCESS;
511 }
512 #endif
513 
514 #ifdef FEATURE_GEOCODE_SUPPORT
PreGetAddressByLocationName(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)515 int LocatorAbilityStub::PreGetAddressByLocationName(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
516 {
517     auto locatorAbility = LocatorAbility::GetInstance();
518     if (locatorAbility == nullptr) {
519         LBSLOGE(LOCATOR, "PreGetAddressByLocationName: LocatorAbility is nullptr.");
520         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
521         return ERRCODE_SERVICE_UNAVAILABLE;
522     }
523     locatorAbility->GetAddressByLocationName(data, reply, identity.GetBundleName());
524     return ERRCODE_SUCCESS;
525 }
526 #endif
527 
PreUnregisterSwitchCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)528 int LocatorAbilityStub::PreUnregisterSwitchCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
529 {
530     auto locatorAbility = LocatorAbility::GetInstance();
531     if (locatorAbility == nullptr) {
532         LBSLOGE(LOCATOR, "PreUnregisterSwitchCallback: LocatorAbility is nullptr.");
533         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
534         return ERRCODE_SERVICE_UNAVAILABLE;
535     }
536     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
537     reply.WriteInt32(locatorAbility->UnregisterSwitchCallback(client));
538     return ERRCODE_SUCCESS;
539 }
540 
541 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterGnssStatusCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)542 int LocatorAbilityStub::PreRegisterGnssStatusCallback(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
543 {
544     if (!CheckLocationSwitchState(reply)) {
545         return ERRCODE_SWITCH_OFF;
546     }
547     if (!CheckLocationPermission(reply, identity)) {
548         return ERRCODE_PERMISSION_DENIED;
549     }
550     auto locatorAbility = LocatorAbility::GetInstance();
551     if (locatorAbility == nullptr) {
552         LBSLOGE(LOCATOR, "PreRegisterGnssStatusCallback: LocatorAbility is nullptr.");
553         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
554         return ERRCODE_SERVICE_UNAVAILABLE;
555     }
556     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
557 
558     reply.WriteInt32(locatorAbility->RegisterGnssStatusCallback(client, identity));
559     return ERRCODE_SUCCESS;
560 }
561 #endif
562 
563 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterGnssStatusCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)564 int LocatorAbilityStub::PreUnregisterGnssStatusCallback(MessageParcel &data,
565     MessageParcel &reply, AppIdentity &identity)
566 {
567     if (!CheckLocationPermission(reply, identity)) {
568         return ERRCODE_PERMISSION_DENIED;
569     }
570     auto locatorAbility = LocatorAbility::GetInstance();
571     if (locatorAbility == nullptr) {
572         LBSLOGE(LOCATOR, "PreUnregisterGnssStatusCallback: LocatorAbility is nullptr.");
573         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
574         return ERRCODE_SERVICE_UNAVAILABLE;
575     }
576     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
577     reply.WriteInt32(locatorAbility->UnregisterGnssStatusCallback(client));
578     return ERRCODE_SUCCESS;
579 }
580 #endif
581 
582 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterNmeaMessageCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)583 int LocatorAbilityStub::PreRegisterNmeaMessageCallback(MessageParcel &data,
584     MessageParcel &reply, AppIdentity &identity)
585 {
586     if (!CheckLocationSwitchState(reply)) {
587         return ERRCODE_SWITCH_OFF;
588     }
589     if (!CheckLocationPermission(reply, identity)) {
590         return ERRCODE_PERMISSION_DENIED;
591     }
592     auto locatorAbility = LocatorAbility::GetInstance();
593     if (locatorAbility == nullptr) {
594         LBSLOGE(LOCATOR, "PreRegisterNmeaMessageCallback: LocatorAbility is nullptr.");
595         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
596         return ERRCODE_SERVICE_UNAVAILABLE;
597     }
598     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
599     reply.WriteInt32(locatorAbility->RegisterNmeaMessageCallback(client, identity));
600     return ERRCODE_SUCCESS;
601 }
602 #endif
603 
604 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterNmeaMessageCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)605 int LocatorAbilityStub::PreUnregisterNmeaMessageCallback(MessageParcel &data,
606     MessageParcel &reply, AppIdentity &identity)
607 {
608     if (!CheckLocationPermission(reply, identity)) {
609         return ERRCODE_PERMISSION_DENIED;
610     }
611     auto locatorAbility = LocatorAbility::GetInstance();
612     if (locatorAbility == nullptr) {
613         LBSLOGE(LOCATOR, "PreUnregisterNmeaMessageCallback: LocatorAbility is nullptr.");
614         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
615         return ERRCODE_SERVICE_UNAVAILABLE;
616     }
617     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
618     reply.WriteInt32(locatorAbility->UnregisterNmeaMessageCallback(client));
619     return ERRCODE_SUCCESS;
620 }
621 #endif
622 
623 #ifdef FEATURE_GNSS_SUPPORT
PreRegisterNmeaMessageCallbackV9(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)624 int LocatorAbilityStub::PreRegisterNmeaMessageCallbackV9(MessageParcel &data,
625     MessageParcel &reply, AppIdentity &identity)
626 {
627     if (!CheckLocationSwitchState(reply)) {
628         return ERRCODE_SWITCH_OFF;
629     }
630     if (!CheckPreciseLocationPermissions(reply, identity)) {
631         return ERRCODE_PERMISSION_DENIED;
632     }
633     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
634     auto locatorAbility = LocatorAbility::GetInstance();
635     if (locatorAbility == nullptr) {
636         LBSLOGE(LOCATOR, "PreRegisterNmeaMessageCallbackV9: LocatorAbility is nullptr.");
637         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
638         return ERRCODE_SERVICE_UNAVAILABLE;
639     }
640     reply.WriteInt32(locatorAbility->RegisterNmeaMessageCallback(client, identity));
641     return ERRCODE_SUCCESS;
642 }
643 #endif
644 
645 #ifdef FEATURE_GNSS_SUPPORT
PreUnregisterNmeaMessageCallbackV9(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)646 int LocatorAbilityStub::PreUnregisterNmeaMessageCallbackV9(MessageParcel &data,
647     MessageParcel &reply, AppIdentity &identity)
648 {
649     if (!CheckPreciseLocationPermissions(reply, identity)) {
650         return ERRCODE_PERMISSION_DENIED;
651     }
652     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
653     auto locatorAbility = LocatorAbility::GetInstance();
654     if (locatorAbility == nullptr) {
655         LBSLOGE(LOCATOR, "PreUnregisterNmeaMessageCallbackV9: LocatorAbility is nullptr.");
656         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
657         return ERRCODE_SERVICE_UNAVAILABLE;
658     }
659     reply.WriteInt32(locatorAbility->UnregisterNmeaMessageCallback(client));
660     return ERRCODE_SUCCESS;
661 }
662 #endif
663 
PreIsLocationPrivacyConfirmed(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)664 int LocatorAbilityStub::PreIsLocationPrivacyConfirmed(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
665 {
666     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
667         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
668             identity.ToString().c_str());
669         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
670         return ERRCODE_SYSTEM_PERMISSION_DENIED;
671     }
672     auto locatorAbility = LocatorAbility::GetInstance();
673     if (locatorAbility == nullptr) {
674         LBSLOGE(LOCATOR, "PreIsLocationPrivacyConfirmed: LocatorAbility is nullptr.");
675         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
676         return ERRCODE_SERVICE_UNAVAILABLE;
677     }
678     bool isConfirmed = false;
679     reply.WriteInt32(locatorAbility->IsLocationPrivacyConfirmed(data.ReadInt32(), isConfirmed));
680     reply.WriteBool(isConfirmed);
681     return ERRCODE_SUCCESS;
682 }
683 
PreSetLocationPrivacyConfirmStatus(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)684 int LocatorAbilityStub::PreSetLocationPrivacyConfirmStatus(MessageParcel &data,
685     MessageParcel &reply, AppIdentity &identity)
686 {
687     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
688         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
689             identity.ToString().c_str());
690         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
691         return ERRCODE_SYSTEM_PERMISSION_DENIED;
692     }
693     if (!CheckSettingsPermission(reply, identity)) {
694         return ERRCODE_PERMISSION_DENIED;
695     }
696 
697     auto locatorAbility = LocatorAbility::GetInstance();
698     if (locatorAbility == nullptr) {
699         LBSLOGE(LOCATOR, "PreSetLocationPrivacyConfirmStatus: LocatorAbility is nullptr.");
700         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
701         return ERRCODE_SERVICE_UNAVAILABLE;
702     }
703     reply.WriteInt32(locatorAbility->SetLocationPrivacyConfirmStatus(data.ReadInt32(),
704         data.ReadBool()));
705     return ERRCODE_SUCCESS;
706 }
707 
708 #ifdef FEATURE_GNSS_SUPPORT
PreStartCacheLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)709 int LocatorAbilityStub::PreStartCacheLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
710 {
711     if (!CheckLocationSwitchState(reply)) {
712         return ERRCODE_SWITCH_OFF;
713     }
714     if (!CheckLocationPermission(reply, identity)) {
715         return ERRCODE_PERMISSION_DENIED;
716     }
717     auto locatorAbility = LocatorAbility::GetInstance();
718     if (locatorAbility == nullptr) {
719         LBSLOGE(LOCATOR, "PreStartCacheLocating: LocatorAbility is nullptr.");
720         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
721         return ERRCODE_SERVICE_UNAVAILABLE;
722     }
723     std::unique_ptr<CachedGnssLocationsRequest> requestConfig = std::make_unique<CachedGnssLocationsRequest>();
724     requestConfig->reportingPeriodSec = data.ReadInt32();
725     requestConfig->wakeUpCacheQueueFull = data.ReadBool();
726     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
727     std::string bundleName = Str16ToStr8(data.ReadString16());
728     if (remoteObject == nullptr) {
729         LBSLOGE(LOCATOR, "ParseDataAndStartCacheLocating remote object nullptr");
730         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
731         return ERRCODE_SERVICE_UNAVAILABLE;
732     }
733     if (bundleName.empty()) {
734         LBSLOGE(LOCATOR, "ParseDataAndStartCacheLocating get empty bundle name");
735         reply.WriteInt32(ERRCODE_INVALID_PARAM);
736         return ERRCODE_INVALID_PARAM;
737     }
738     sptr<ICachedLocationsCallback> callback = iface_cast<ICachedLocationsCallback>(remoteObject);
739     reply.WriteInt32(locatorAbility->RegisterCachedLocationCallback(requestConfig, callback, bundleName));
740     return ERRCODE_SUCCESS;
741 }
742 #endif
743 
744 #ifdef FEATURE_GNSS_SUPPORT
PreStopCacheLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)745 int LocatorAbilityStub::PreStopCacheLocating(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
746 {
747     if (!CheckLocationPermission(reply, identity)) {
748         return ERRCODE_PERMISSION_DENIED;
749     }
750     auto locatorAbility = LocatorAbility::GetInstance();
751     if (locatorAbility == nullptr) {
752         LBSLOGE(LOCATOR, "PreStopCacheLocating: LocatorAbility is nullptr.");
753         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
754         return ERRCODE_SERVICE_UNAVAILABLE;
755     }
756     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
757     if (remoteObject == nullptr) {
758         LBSLOGE(LOCATOR, "LocatorAbility::ParseDataAndStopCacheLocating remote object nullptr");
759         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
760         return ERRCODE_SERVICE_UNAVAILABLE;
761     }
762     sptr<ICachedLocationsCallback> callback = iface_cast<ICachedLocationsCallback>(remoteObject);
763     reply.WriteInt32(locatorAbility->UnregisterCachedLocationCallback(callback));
764     return ERRCODE_SUCCESS;
765 }
766 #endif
767 
768 #ifdef FEATURE_GNSS_SUPPORT
PreGetCachedGnssLocationsSize(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)769 int LocatorAbilityStub::PreGetCachedGnssLocationsSize(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
770 {
771     if (!CheckLocationSwitchState(reply)) {
772         return ERRCODE_SWITCH_OFF;
773     }
774     if (!CheckLocationPermission(reply, identity)) {
775         return ERRCODE_PERMISSION_DENIED;
776     }
777     auto locatorAbility = LocatorAbility::GetInstance();
778     if (locatorAbility == nullptr) {
779         LBSLOGE(LOCATOR, "PreGetCachedGnssLocationsSize: LocatorAbility is nullptr.");
780         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
781         return ERRCODE_SERVICE_UNAVAILABLE;
782     }
783     int size = -1;
784     reply.WriteInt32(locatorAbility->GetCachedGnssLocationsSize(size));
785     reply.WriteInt32(size);
786     return ERRCODE_SUCCESS;
787 }
788 #endif
789 
790 #ifdef FEATURE_GNSS_SUPPORT
PreFlushCachedGnssLocations(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)791 int LocatorAbilityStub::PreFlushCachedGnssLocations(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
792 {
793     if (!CheckLocationSwitchState(reply)) {
794         return ERRCODE_SWITCH_OFF;
795     }
796     if (!CheckLocationPermission(reply, identity)) {
797         return ERRCODE_PERMISSION_DENIED;
798     }
799     auto locatorAbility = LocatorAbility::GetInstance();
800     if (locatorAbility == nullptr) {
801         LBSLOGE(LOCATOR, "PreFlushCachedGnssLocations: LocatorAbility is nullptr.");
802         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
803         return ERRCODE_SERVICE_UNAVAILABLE;
804     }
805     reply.WriteInt32(locatorAbility->FlushCachedGnssLocations());
806     return ERRCODE_SUCCESS;
807 }
808 #endif
809 
810 #ifdef FEATURE_GNSS_SUPPORT
PreSendCommand(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)811 int LocatorAbilityStub::PreSendCommand(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
812 {
813     if (!CheckLocationPermission(reply, identity)) {
814         return ERRCODE_PERMISSION_DENIED;
815     }
816     auto locatorAbility = LocatorAbility::GetInstance();
817     if (locatorAbility == nullptr) {
818         LBSLOGE(LOCATOR, "PreSendCommand: LocatorAbility is nullptr.");
819         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
820         return ERRCODE_SERVICE_UNAVAILABLE;
821     }
822     std::unique_ptr<LocationCommand> locationCommand = std::make_unique<LocationCommand>();
823     locationCommand->scenario =  data.ReadInt32();
824     locationCommand->command = Str16ToStr8(data.ReadString16());
825     reply.WriteInt32(locatorAbility->SendCommand(locationCommand));
826     return ERRCODE_SUCCESS;
827 }
828 #endif
829 
830 #ifdef FEATURE_GNSS_SUPPORT
PreAddFence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)831 int LocatorAbilityStub::PreAddFence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
832 {
833     return DoProcessFenceRequest(LocatorInterfaceCode::ADD_FENCE, data, reply, identity);
834 }
835 #endif
836 
837 #ifdef FEATURE_GNSS_SUPPORT
PreRemoveFence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)838 int LocatorAbilityStub::PreRemoveFence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
839 {
840     return DoProcessFenceRequest(LocatorInterfaceCode::REMOVE_FENCE, data, reply, identity);
841 }
842 #endif
843 
844 #ifdef FEATURE_GNSS_SUPPORT
DoProcessFenceRequest(LocatorInterfaceCode code,MessageParcel & data,MessageParcel & reply,AppIdentity & identity)845 int LocatorAbilityStub::DoProcessFenceRequest(
846     LocatorInterfaceCode code, MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
847 {
848     if (!CheckLocationSwitchState(reply)) {
849         return ERRCODE_SWITCH_OFF;
850     }
851     if (!CheckLocationPermission(reply, identity)) {
852         return ERRCODE_PERMISSION_DENIED;
853     }
854     auto locatorAbility = LocatorAbility::GetInstance();
855     if (locatorAbility == nullptr) {
856         LBSLOGE(LOCATOR, "PreRemoveFence: LocatorAbility is nullptr.");
857         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
858         return ERRCODE_SERVICE_UNAVAILABLE;
859     }
860     auto request = GeofenceRequest::Unmarshalling(data);
861     if (code == LocatorInterfaceCode::ADD_FENCE) {
862         reply.WriteInt32(locatorAbility->AddFence(request));
863     } else if (code == LocatorInterfaceCode::REMOVE_FENCE) {
864         reply.WriteInt32(locatorAbility->RemoveFence(request));
865     }
866     return ERRCODE_SUCCESS;
867 }
868 #endif
869 
870 #ifdef FEATURE_GNSS_SUPPORT
PreAddGnssGeofence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)871 int LocatorAbilityStub::PreAddGnssGeofence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
872 {
873     LBSLOGI(LOCATOR, "PreAddGnssGeofence enter.");
874     if (!CheckLocationSwitchState(reply)) {
875         return ERRCODE_SWITCH_OFF;
876     }
877     if (!CheckPreciseLocationPermissions(reply, identity)) {
878         return ERRCODE_PERMISSION_DENIED;
879     }
880     auto locatorAbility = LocatorAbility::GetInstance();
881     if (locatorAbility == nullptr) {
882         LBSLOGE(LOCATOR, "LocatorAbility is nullptr.");
883         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
884         return ERRCODE_SERVICE_UNAVAILABLE;
885     }
886     auto request = GeofenceRequest::Unmarshalling(data);
887     request->SetBundleName(identity.GetBundleName());
888     request->SetUid(identity.GetUid());
889     reply.WriteInt32(locatorAbility->AddGnssGeofence(request));
890     return ERRCODE_SUCCESS;
891 }
892 #endif
893 
894 #ifdef FEATURE_GNSS_SUPPORT
PreRemoveGnssGeofence(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)895 int LocatorAbilityStub::PreRemoveGnssGeofence(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
896 {
897     LBSLOGI(LOCATOR, "PreRemoveGnssGeofence enter.");
898     if (!CheckLocationSwitchState(reply)) {
899         return ERRCODE_SWITCH_OFF;
900     }
901     if (!CheckPreciseLocationPermissions(reply, identity)) {
902         return ERRCODE_PERMISSION_DENIED;
903     }
904     auto locatorAbility = LocatorAbility::GetInstance();
905     if (locatorAbility == nullptr) {
906         LBSLOGE(LOCATOR, "LocatorAbility is nullptr.");
907         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
908         return ERRCODE_SERVICE_UNAVAILABLE;
909     }
910     std::shared_ptr<GeofenceRequest> request = std::make_shared<GeofenceRequest>();
911     request->SetFenceId(data.ReadInt32());
912     request->SetBundleName(identity.GetBundleName());
913     reply.WriteInt32(locatorAbility->RemoveGnssGeofence(request));
914     return ERRCODE_SUCCESS;
915 }
916 #endif
917 
PreEnableLocationMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)918 int LocatorAbilityStub::PreEnableLocationMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
919 {
920     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
921         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
922             identity.ToString().c_str());
923         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
924         return ERRCODE_SYSTEM_PERMISSION_DENIED;
925     }
926     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
927         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
928             identity.ToString().c_str());
929         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
930         return ERRCODE_PERMISSION_DENIED;
931     }
932     auto locatorAbility = LocatorAbility::GetInstance();
933     if (locatorAbility == nullptr) {
934         LBSLOGE(LOCATOR, "PreEnableLocationMock: LocatorAbility is nullptr.");
935         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
936         return ERRCODE_SERVICE_UNAVAILABLE;
937     }
938     reply.WriteInt32(locatorAbility->EnableLocationMock());
939     return ERRCODE_SUCCESS;
940 }
941 
PreDisableLocationMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)942 int LocatorAbilityStub::PreDisableLocationMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
943 {
944     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
945         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
946             identity.ToString().c_str());
947         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
948         return ERRCODE_SYSTEM_PERMISSION_DENIED;
949     }
950     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
951         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
952             identity.ToString().c_str());
953         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
954         return ERRCODE_PERMISSION_DENIED;
955     }
956     auto locatorAbility = LocatorAbility::GetInstance();
957     if (locatorAbility == nullptr) {
958         LBSLOGE(LOCATOR, "PreDisableLocationMock: LocatorAbility is nullptr.");
959         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
960         return ERRCODE_SERVICE_UNAVAILABLE;
961     }
962     reply.WriteInt32(locatorAbility->DisableLocationMock());
963     return ERRCODE_SUCCESS;
964 }
965 
PreSetMockedLocations(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)966 int LocatorAbilityStub::PreSetMockedLocations(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
967 {
968     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
969         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
970             identity.ToString().c_str());
971         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
972         return ERRCODE_SYSTEM_PERMISSION_DENIED;
973     }
974     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
975         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
976             identity.ToString().c_str());
977         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
978         return ERRCODE_PERMISSION_DENIED;
979     }
980     auto locatorAbility = LocatorAbility::GetInstance();
981     if (locatorAbility == nullptr) {
982         LBSLOGE(LOCATOR, "PreSetMockedLocations: LocatorAbility is nullptr.");
983         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
984         return ERRCODE_SERVICE_UNAVAILABLE;
985     }
986     int timeInterval = data.ReadInt32();
987     int locationSize = data.ReadInt32();
988     timeInterval = timeInterval < 0 ? 1 : timeInterval;
989     locationSize = locationSize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX : locationSize;
990     std::vector<std::shared_ptr<Location>> vcLoc;
991     for (int i = 0; i < locationSize; i++) {
992         vcLoc.push_back(Location::UnmarshallingShared(data));
993     }
994     reply.WriteInt32(locatorAbility->SetMockedLocations(timeInterval, vcLoc));
995     return ERRCODE_SUCCESS;
996 }
997 
998 #ifdef FEATURE_GEOCODE_SUPPORT
PreEnableReverseGeocodingMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)999 int LocatorAbilityStub::PreEnableReverseGeocodingMock(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1000 {
1001     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1002         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
1003             identity.ToString().c_str());
1004         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
1005         return ERRCODE_SYSTEM_PERMISSION_DENIED;
1006     }
1007     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1008         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
1009             identity.ToString().c_str());
1010         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1011         return ERRCODE_PERMISSION_DENIED;
1012     }
1013     auto locatorAbility = LocatorAbility::GetInstance();
1014     if (locatorAbility == nullptr) {
1015         LBSLOGE(LOCATOR, "PreEnableReverseGeocodingMock: LocatorAbility is nullptr.");
1016         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1017         return ERRCODE_SERVICE_UNAVAILABLE;
1018     }
1019     reply.WriteInt32(locatorAbility->EnableReverseGeocodingMock());
1020     return ERRCODE_SUCCESS;
1021 }
1022 #endif
1023 
1024 #ifdef FEATURE_GEOCODE_SUPPORT
PreDisableReverseGeocodingMock(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1025 int LocatorAbilityStub::PreDisableReverseGeocodingMock(MessageParcel &data,
1026     MessageParcel &reply, AppIdentity &identity)
1027 {
1028     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1029         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
1030             identity.ToString().c_str());
1031         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
1032         return ERRCODE_SYSTEM_PERMISSION_DENIED;
1033     }
1034     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1035         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
1036             identity.ToString().c_str());
1037         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1038         return ERRCODE_PERMISSION_DENIED;
1039     }
1040     auto locatorAbility = LocatorAbility::GetInstance();
1041     if (locatorAbility == nullptr) {
1042         LBSLOGE(LOCATOR, "PreDisableReverseGeocodingMock: LocatorAbility is nullptr.");
1043         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1044         return ERRCODE_SERVICE_UNAVAILABLE;
1045     }
1046     reply.WriteInt32(locatorAbility->DisableReverseGeocodingMock());
1047     return ERRCODE_SUCCESS;
1048 }
1049 #endif
1050 
1051 #ifdef FEATURE_GEOCODE_SUPPORT
PreSetReverseGeocodingMockInfo(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1052 int LocatorAbilityStub::PreSetReverseGeocodingMockInfo(MessageParcel &data,
1053     MessageParcel &reply, AppIdentity &identity)
1054 {
1055     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1056         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
1057             identity.ToString().c_str());
1058         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
1059         return ERRCODE_SYSTEM_PERMISSION_DENIED;
1060     }
1061     if (!PermissionManager::CheckMockLocationPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1062         LBSLOGE(LOCATOR, "CheckMockLocationPermission return false, [%{public}s]",
1063             identity.ToString().c_str());
1064         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1065         return ERRCODE_PERMISSION_DENIED;
1066     }
1067     auto locatorAbility = LocatorAbility::GetInstance();
1068     if (locatorAbility == nullptr) {
1069         LBSLOGE(LOCATOR, "PreSetReverseGeocodingMockInfo: LocatorAbility is nullptr.");
1070         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1071         return ERRCODE_SERVICE_UNAVAILABLE;
1072     }
1073     std::vector<std::shared_ptr<GeocodingMockInfo>> mockInfo;
1074     int arraySize = data.ReadInt32();
1075     arraySize = arraySize > INPUT_ARRAY_LEN_MAX ? INPUT_ARRAY_LEN_MAX : arraySize;
1076     for (int i = 0; i < arraySize; i++) {
1077         std::shared_ptr<GeocodingMockInfo> info = std::make_shared<GeocodingMockInfo>();
1078         info->ReadFromParcel(data);
1079         mockInfo.push_back(info);
1080     }
1081     reply.WriteInt32(locatorAbility->SetReverseGeocodingMockInfo(mockInfo));
1082     return ERRCODE_SUCCESS;
1083 }
1084 #endif
1085 
PreProxyForFreeze(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1086 int LocatorAbilityStub::PreProxyForFreeze(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1087 {
1088     if (!CheckRssProcessName(reply, identity)) {
1089         return ERRCODE_PERMISSION_DENIED;
1090     }
1091     auto locatorAbility = LocatorAbility::GetInstance();
1092     if (locatorAbility == nullptr) {
1093         LBSLOGE(LOCATOR, "PreProxyForFreeze: LocatorAbility is nullptr.");
1094         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1095         return ERRCODE_SERVICE_UNAVAILABLE;
1096     }
1097     std::set<int> pidList;
1098     int size = data.ReadInt32();
1099     if (size > MAX_BUFF_SIZE) {
1100         size = MAX_BUFF_SIZE;
1101     }
1102     for (int i = 0; i < size; i++) {
1103         pidList.insert(data.ReadInt32());
1104     }
1105     bool isProxy = data.ReadBool();
1106     reply.WriteInt32(locatorAbility->ProxyForFreeze(pidList, isProxy));
1107     return ERRCODE_SUCCESS;
1108 }
1109 
PreResetAllProxy(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1110 int LocatorAbilityStub::PreResetAllProxy(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1111 {
1112     if (!CheckRssProcessName(reply, identity)) {
1113         return ERRCODE_PERMISSION_DENIED;
1114     }
1115     auto locatorAbility = LocatorAbility::GetInstance();
1116     if (locatorAbility == nullptr) {
1117         LBSLOGE(LOCATOR, "PreResetAllProxy: LocatorAbility is nullptr.");
1118         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1119         return ERRCODE_SERVICE_UNAVAILABLE;
1120     }
1121     reply.WriteInt32(locatorAbility->ResetAllProxy());
1122     return ERRCODE_SUCCESS;
1123 }
1124 
PreReportLocation(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1125 int LocatorAbilityStub::PreReportLocation(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1126 {
1127     pid_t callingPid = IPCSkeleton::GetCallingPid();
1128     pid_t callingUid = IPCSkeleton::GetCallingUid();
1129     if (callingUid != static_cast<pid_t>(getuid()) || callingPid != getpid()) {
1130         LBSLOGE(LOCATOR, "check system permission failed, [%{public}s]",
1131             identity.ToString().c_str());
1132         return ERRCODE_PERMISSION_DENIED;
1133     }
1134     auto locatorAbility = LocatorAbility::GetInstance();
1135     if (locatorAbility == nullptr) {
1136         LBSLOGE(LOCATOR, "PreReportLocation: LocatorAbility is nullptr.");
1137         return ERRCODE_SERVICE_UNAVAILABLE;
1138     }
1139     std::string systemAbility = data.ReadString();
1140     std::unique_ptr<Location> location = Location::Unmarshalling(data);
1141     locatorAbility->ReportLocation(location, systemAbility, identity);
1142 #ifdef FEATURE_GNSS_SUPPORT
1143     if (systemAbility == NETWORK_ABILITY) {
1144         locatorAbility->SendNetworkLocation(location);
1145     }
1146 #endif
1147     return ERRCODE_SUCCESS;
1148 }
1149 
PreRegisterLocatingRequiredDataCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1150 int LocatorAbilityStub::PreRegisterLocatingRequiredDataCallback(MessageParcel &data,
1151     MessageParcel &reply, AppIdentity &identity)
1152 {
1153     if (!CheckPreciseLocationPermissions(reply, identity)) {
1154         return ERRCODE_PERMISSION_DENIED;
1155     }
1156     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1157         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
1158             identity.ToString().c_str());
1159         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
1160         return ERRCODE_SYSTEM_PERMISSION_DENIED;
1161     }
1162     std::shared_ptr<LocatingRequiredDataConfig> dataConfig = LocatingRequiredDataConfig::Unmarshalling(data);
1163     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
1164     if (client == nullptr) {
1165         LBSLOGE(LOCATOR, "%{public}s: client is nullptr.", __func__);
1166         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1167         return ERRCODE_SERVICE_UNAVAILABLE;
1168     }
1169     auto locatorDataManager = LocatorRequiredDataManager::GetInstance();
1170     if (locatorDataManager == nullptr) {
1171         LBSLOGE(LOCATOR, "%{public}s: locatorDataManager is nullptr.", __func__);
1172         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1173         return ERRCODE_SERVICE_UNAVAILABLE;
1174     }
1175     client->AddDeathRecipient(scanRecipient_);
1176     LocationErrCode errorCode = locatorDataManager->RegisterCallback(identity, dataConfig, client);
1177 
1178     reply.WriteInt32(errorCode);
1179     return ERRCODE_SUCCESS;
1180 }
1181 
PreUnregisterLocatingRequiredDataCallback(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1182 int LocatorAbilityStub::PreUnregisterLocatingRequiredDataCallback(MessageParcel &data,
1183     MessageParcel &reply, AppIdentity &identity)
1184 {
1185     if (!CheckPreciseLocationPermissions(reply, identity)) {
1186         return ERRCODE_PERMISSION_DENIED;
1187     }
1188     if (!PermissionManager::CheckSystemPermission(identity.GetTokenId(), identity.GetTokenIdEx())) {
1189         LBSLOGE(LOCATOR, "CheckSystemPermission return false, [%{public}s]",
1190             identity.ToString().c_str());
1191         reply.WriteInt32(ERRCODE_SYSTEM_PERMISSION_DENIED);
1192         return ERRCODE_SYSTEM_PERMISSION_DENIED;
1193     }
1194     sptr<IRemoteObject> client = data.ReadObject<IRemoteObject>();
1195     if (client == nullptr) {
1196         LBSLOGE(LOCATOR, "%{public}s: client is nullptr.", __func__);
1197         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1198         return ERRCODE_SERVICE_UNAVAILABLE;
1199     }
1200     auto locatorDataManager = LocatorRequiredDataManager::GetInstance();
1201     if (locatorDataManager == nullptr) {
1202         LBSLOGE(LOCATOR, "%{public}s: locatorDataManager is nullptr.", __func__);
1203         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1204         return ERRCODE_SERVICE_UNAVAILABLE;
1205     }
1206     client->RemoveDeathRecipient(scanRecipient_);
1207     LocationErrCode errorCode = locatorDataManager->UnregisterCallback(client);
1208 
1209     reply.WriteInt32(errorCode);
1210     return ERRCODE_SUCCESS;
1211 }
1212 
1213 #ifdef FEATURE_GNSS_SUPPORT
PreQuerySupportCoordinateSystemType(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1214 int LocatorAbilityStub::PreQuerySupportCoordinateSystemType(MessageParcel &data,
1215     MessageParcel &reply, AppIdentity &identity)
1216 {
1217     auto locatorAbility = LocatorAbility::GetInstance();
1218     if (locatorAbility == nullptr) {
1219         LBSLOGE(LOCATOR, "LocatorAbility is nullptr.");
1220         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1221         return ERRCODE_SERVICE_UNAVAILABLE;
1222     }
1223     std::vector<CoordinateSystemType> coordinateSystemTypes;
1224     auto errCode = locatorAbility->QuerySupportCoordinateSystemType(coordinateSystemTypes);
1225     reply.WriteInt32(errCode);
1226     if (errCode != ERRCODE_SUCCESS) {
1227         return errCode;
1228     }
1229     int size = static_cast<int>(coordinateSystemTypes.size()) > COORDINATE_SYSTEM_TYPE_SIZE ?
1230         COORDINATE_SYSTEM_TYPE_SIZE : static_cast<int>(coordinateSystemTypes.size());
1231     reply.WriteInt32(size);
1232     for (int i = 0; i < size; i++) {
1233         reply.WriteInt32(static_cast<int>(coordinateSystemTypes[i]));
1234     }
1235     return ERRCODE_SUCCESS;
1236 }
1237 #endif
1238 
CheckLocationPermission(MessageParcel & reply,AppIdentity & identity)1239 bool LocatorAbilityStub::CheckLocationPermission(MessageParcel &reply, AppIdentity &identity)
1240 {
1241     uint32_t callingTokenId = identity.GetTokenId();
1242     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
1243     if (!PermissionManager::CheckLocationPermission(callingTokenId, callingFirstTokenid) &&
1244         !PermissionManager::CheckApproximatelyPermission(callingTokenId, callingFirstTokenid)) {
1245         LBSLOGE(LOCATOR, "%{public}d %{public}s failed", callingTokenId, __func__);
1246         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1247         return false;
1248     } else {
1249         return true;
1250     }
1251 }
1252 
CheckRssProcessName(MessageParcel & reply,AppIdentity & identity)1253 bool LocatorAbilityStub::CheckRssProcessName(MessageParcel &reply, AppIdentity &identity)
1254 {
1255     uint32_t callingTokenId = identity.GetTokenId();
1256     if (!PermissionManager::CheckRssProcessName(callingTokenId)) {
1257         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1258         return false;
1259     } else {
1260         return true;
1261     }
1262 }
1263 
CheckPreciseLocationPermissions(MessageParcel & reply,AppIdentity & identity)1264 bool LocatorAbilityStub::CheckPreciseLocationPermissions(MessageParcel &reply, AppIdentity &identity)
1265 {
1266     uint32_t callingTokenId = identity.GetTokenId();
1267     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
1268     if (!PermissionManager::CheckLocationPermission(callingTokenId, callingFirstTokenid) ||
1269         !PermissionManager::CheckApproximatelyPermission(callingTokenId, callingFirstTokenid)) {
1270         LBSLOGE(LOCATOR, "%{public}d %{public}s failed", callingTokenId, __func__);
1271         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1272         return false;
1273     } else {
1274         return true;
1275     }
1276 }
1277 
CheckSettingsPermission(MessageParcel & reply,AppIdentity & identity)1278 bool LocatorAbilityStub::CheckSettingsPermission(MessageParcel &reply, AppIdentity &identity)
1279 {
1280     uint32_t callingTokenId = identity.GetTokenId();
1281     uint32_t callingFirstTokenid = identity.GetFirstTokenId();
1282     if (!PermissionManager::CheckSecureSettings(callingTokenId, callingFirstTokenid)) {
1283         LBSLOGE(LOCATOR, "has no access permission, CheckSecureSettings return false");
1284         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1285         return false;
1286     } else {
1287         return true;
1288     }
1289 }
1290 
CheckLocationSwitchState(MessageParcel & reply)1291 bool LocatorAbilityStub::CheckLocationSwitchState(MessageParcel &reply)
1292 {
1293     auto locatorAbility = LocatorAbility::GetInstance();
1294     if (locatorAbility == nullptr) {
1295         LBSLOGE(LOCATOR, "CheckLocationSwitchState: LocatorAbility is nullptr.");
1296         return false;
1297     }
1298     int state = DISABLED;
1299     LocationErrCode errorCode = locatorAbility->GetSwitchState(state);
1300     if (errorCode != ERRCODE_SUCCESS) {
1301         LBSLOGE(LOCATOR, "GetSwitchState failed errCode = %{public}d.", errorCode);
1302     }
1303     if (state != ENABLED) {
1304         LBSLOGE(LOCATOR, "switch state is off.");
1305         reply.WriteInt32(ERRCODE_SWITCH_OFF);
1306         return false;
1307     }
1308     return true;
1309 }
1310 
PreRegisterLocationError(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1311 int LocatorAbilityStub::PreRegisterLocationError(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1312 {
1313     if (!CheckLocationSwitchState(reply)) {
1314         return ERRCODE_SWITCH_OFF;
1315     }
1316     if (!CheckLocationPermission(reply, identity)) {
1317         return ERRCODE_PERMISSION_DENIED;
1318     }
1319     auto locatorAbility = LocatorAbility::GetInstance();
1320     if (locatorAbility == nullptr) {
1321         LBSLOGE(LOCATOR, "PreStartLocating: LocatorAbility is nullptr.");
1322         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1323         return ERRCODE_SERVICE_UNAVAILABLE;
1324     }
1325     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
1326     if (remoteObject == nullptr) {
1327         LBSLOGE(LOCATOR, "StartLocating remote object nullptr");
1328         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1329         return ERRCODE_SERVICE_UNAVAILABLE;
1330     }
1331     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
1332     reply.WriteInt32(locatorAbility->RegisterLocationError(callback, identity));
1333     return ERRCODE_SUCCESS;
1334 }
1335 
PreUnregisterLocationError(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1336 int LocatorAbilityStub::PreUnregisterLocationError(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1337 {
1338     if (!CheckLocationPermission(reply, identity)) {
1339         return ERRCODE_PERMISSION_DENIED;
1340     }
1341     auto locatorAbility = LocatorAbility::GetInstance();
1342     if (locatorAbility == nullptr) {
1343         LBSLOGE(LOCATOR, "PreStopLocating: LocatorAbility is nullptr.");
1344         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1345         return ERRCODE_SERVICE_UNAVAILABLE;
1346     }
1347     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
1348     if (remoteObject == nullptr) {
1349         LBSLOGE(LOCATOR, "LocatorAbility::StopLocating remote object nullptr");
1350         reply.WriteInt32(ERRCODE_SERVICE_UNAVAILABLE);
1351         return ERRCODE_SERVICE_UNAVAILABLE;
1352     }
1353     sptr<ILocatorCallback> callback = iface_cast<ILocatorCallback>(remoteObject);
1354     reply.WriteInt32(locatorAbility->UnregisterLocationError(callback, identity));
1355     return ERRCODE_SUCCESS;
1356 }
1357 
PreReportLocationError(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1358 int LocatorAbilityStub::PreReportLocationError(MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1359 {
1360     pid_t callingPid = IPCSkeleton::GetCallingPid();
1361     pid_t callingUid = IPCSkeleton::GetCallingUid();
1362     if (callingUid != static_cast<pid_t>(getuid()) || callingPid != getpid()) {
1363         LBSLOGE(LOCATOR, "check system permission failed, [%{public}s]",
1364             identity.ToString().c_str());
1365         return ERRCODE_PERMISSION_DENIED;
1366     }
1367     auto locatorAbility = LocatorAbility::GetInstance();
1368     if (locatorAbility == nullptr) {
1369         LBSLOGE(LOCATOR, "PreReportLocation: LocatorAbility is nullptr.");
1370         return ERRCODE_SERVICE_UNAVAILABLE;
1371     }
1372     int32_t errCode = data.ReadInt32();
1373     std::string errMsg = data.ReadString();
1374     std::string uuid = data.ReadString();
1375     locatorAbility->ReportLocationError(uuid, errCode);
1376     return ERRCODE_SUCCESS;
1377 }
1378 
PreGetCurrentWifiBssidForLocating(MessageParcel & data,MessageParcel & reply,AppIdentity & identity)1379 int LocatorAbilityStub::PreGetCurrentWifiBssidForLocating(
1380     MessageParcel &data, MessageParcel &reply, AppIdentity &identity)
1381 {
1382     if (!CheckLocationSwitchState(reply)) {
1383         return ERRCODE_SWITCH_OFF;
1384     }
1385     if (!CheckPreciseLocationPermissions(reply, identity)) {
1386         return ERRCODE_PERMISSION_DENIED;
1387     }
1388     auto locatorDataManager = LocatorRequiredDataManager::GetInstance();
1389     std::string bssid;
1390     LocationErrCode errorCode = locatorDataManager->GetCurrentWifiBssidForLocating(bssid);
1391     reply.WriteInt32(errorCode);
1392     reply.WriteString16(Str8ToStr16(bssid));
1393     return ERRCODE_SUCCESS;
1394 }
1395 
IsStopAction(uint32_t code)1396 bool LocatorAbilityStub::IsStopAction(uint32_t code)
1397 {
1398     if (code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_SWITCH_CALLBACK) ||
1399         code == static_cast<uint32_t>(LocatorInterfaceCode::STOP_LOCATING) ||
1400         code == static_cast<uint32_t>(LocatorInterfaceCode::STOP_LOCATING) ||
1401         code == static_cast<uint32_t>(LocatorInterfaceCode::DISABLE_LOCATION_MOCK) ||
1402         code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_LOCATION_ERROR) ||
1403         code == static_cast<uint32_t>(LocatorInterfaceCode::UNREG_LOCATING_REQUIRED_DATA_CALLBACK)) {
1404         return true;
1405     }
1406     return false;
1407 }
1408 
CheckRequestAvailable(uint32_t code,AppIdentity & identity)1409 bool LocatorAbilityStub::CheckRequestAvailable(uint32_t code, AppIdentity &identity)
1410 {
1411     if (IsStopAction(code)) {
1412         return true;
1413     }
1414     if (CommonUtils::IsAppBelongCurrentAccount(identity)) {
1415         return true;
1416     }
1417     LBSLOGD(LOCATOR, "CheckRequestAvailable fail uid:%{public}d", identity.GetUid());
1418     return false;
1419 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1420 int32_t LocatorAbilityStub::OnRemoteRequest(uint32_t code,
1421     MessageParcel &data, MessageParcel &reply, MessageOption &option)
1422 {
1423     int ret = ERRCODE_SUCCESS;
1424     AppIdentity identity;
1425     identity.SetPid(IPCSkeleton::GetCallingPid());
1426     identity.SetUid(IPCSkeleton::GetCallingUid());
1427     identity.SetTokenId(IPCSkeleton::GetCallingTokenID());
1428     identity.SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
1429     identity.SetFirstTokenId(IPCSkeleton::GetFirstTokenID());
1430 
1431     // first token id is invalid
1432     if (identity.GetUid() == static_cast<pid_t>(identity.GetFirstTokenId()) &&
1433         identity.GetUid() == static_cast<pid_t>(getuid()) && identity.GetPid() == getpid()) {
1434         identity.SetFirstTokenId(0);
1435     }
1436 
1437     std::string bundleName = "";
1438     if (!CommonUtils::GetBundleNameByUid(identity.GetUid(), bundleName)) {
1439         LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", identity.GetUid());
1440     }
1441     identity.SetBundleName(bundleName);
1442     if (code != static_cast<uint32_t>(LocatorInterfaceCode::PROXY_PID_FOR_FREEZE)) {
1443         LBSLOGI(LOCATOR,
1444             "OnReceived cmd = %{public}u, flags= %{public}d, identity= [%{public}s], timestamp = %{public}s",
1445             code, option.GetFlags(), identity.ToString().c_str(),
1446             std::to_string(CommonUtils::GetCurrentTimeStamp()).c_str());
1447     }
1448     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
1449     if (data.ReadInterfaceToken() != GetDescriptor()) {
1450         LBSLOGE(LOCATOR, "invalid token.");
1451         IPCSkeleton::SetCallingIdentity(callingIdentity);
1452         return ERRCODE_SERVICE_UNAVAILABLE;
1453     }
1454     if (!CheckRequestAvailable(code, identity)) {
1455         IPCSkeleton::SetCallingIdentity(callingIdentity);
1456         reply.WriteInt32(ERRCODE_PERMISSION_DENIED);
1457         return ERRCODE_PERMISSION_DENIED;
1458     }
1459     CancelIdleState();
1460     RemoveUnloadTask(code);
1461     auto handleFunc = locatorHandleMap_.find(static_cast<LocatorInterfaceCode>(code));
1462     if (handleFunc != locatorHandleMap_.end() && handleFunc->second != nullptr) {
1463         auto memberFunc = handleFunc->second;
1464         ret = memberFunc(data, reply, identity);
1465     } else {
1466         LBSLOGE(LOCATOR, "OnReceived cmd = %{public}u, unsupport service.", code);
1467         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
1468     }
1469     IPCSkeleton::SetCallingIdentity(callingIdentity);
1470     WriteLocationDenyReportEvent(code, ret, data, identity);
1471     PostUnloadTask(code);
1472     return ret;
1473 }
1474 
SaDumpInfo(std::string & result)1475 void LocatorAbilityStub::SaDumpInfo(std::string& result)
1476 {
1477     auto locatorAbility = LocatorAbility::GetInstance();
1478     if (locatorAbility == nullptr) {
1479         LBSLOGE(LOCATOR, "SaDumpInfo: LocatorAbility is nullptr.");
1480         return;
1481     }
1482     int state = DISABLED;
1483     LocationErrCode errorCode = locatorAbility->GetSwitchState(state);
1484     if (errorCode != ERRCODE_SUCCESS) {
1485         LBSLOGE(LOCATOR, "GetSwitchState failed errCode = %{public}d.", errorCode);
1486     }
1487     result += "Location switch state: ";
1488     std::string status = state ? "on" : "off";
1489     result += status + "\n";
1490 }
1491 
Dump(int32_t fd,const std::vector<std::u16string> & args)1492 int32_t LocatorAbilityStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
1493 {
1494     std::vector<std::string> vecArgs;
1495     std::transform(args.begin(), args.end(), std::back_inserter(vecArgs), [](const std::u16string &arg) {
1496         return Str16ToStr8(arg);
1497     });
1498 
1499     LocationDumper dumper;
1500     std::string result;
1501     dumper.LocatorDump(SaDumpInfo, vecArgs, result);
1502     if (!SaveStringToFd(fd, result)) {
1503         LBSLOGE(LOCATOR, "Gnss save string to fd failed!");
1504         return ERR_OK;
1505     }
1506     return ERR_OK;
1507 }
1508 
WriteLocationDenyReportEvent(uint32_t code,int errCode,MessageParcel & data,AppIdentity & identity)1509 void LocatorAbilityStub::WriteLocationDenyReportEvent(uint32_t code, int errCode,
1510     MessageParcel &data, AppIdentity &identity)
1511 {
1512     if (code == static_cast<uint32_t>(LocatorInterfaceCode::START_LOCATING) && errCode != ERRCODE_SUCCESS) {
1513         std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
1514         requestConfig->ReadFromParcel(data);
1515         auto requestInfo = requestConfig->ToString();
1516         WriteLocationInnerEvent(LOCATION_REQUEST_DENY, {"errorCode", std::to_string(errCode),
1517             "requestAppName", identity.GetBundleName(), "requestInfo", requestInfo});
1518     }
1519 }
1520 
SwitchCallbackDeathRecipient()1521 SwitchCallbackDeathRecipient::SwitchCallbackDeathRecipient()
1522 {
1523 }
1524 
~SwitchCallbackDeathRecipient()1525 SwitchCallbackDeathRecipient::~SwitchCallbackDeathRecipient()
1526 {
1527 }
1528 
OnRemoteDied(const wptr<IRemoteObject> & remote)1529 void SwitchCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1530 {
1531     auto locatorAbility = LocatorAbility::GetInstance();
1532     if (locatorAbility != nullptr) {
1533         locatorAbility->RemoveUnloadTask(DEFAULT_CODE);
1534         locatorAbility->UnregisterSwitchCallback(remote.promote());
1535         locatorAbility->PostUnloadTask(DEFAULT_CODE);
1536         LBSLOGI(LOCATOR, "switch callback OnRemoteDied");
1537     }
1538 }
1539 
ScanCallbackDeathRecipient()1540 ScanCallbackDeathRecipient::ScanCallbackDeathRecipient()
1541 {
1542 }
1543 
~ScanCallbackDeathRecipient()1544 ScanCallbackDeathRecipient::~ScanCallbackDeathRecipient()
1545 {
1546 }
1547 
OnRemoteDied(const wptr<IRemoteObject> & remote)1548 void ScanCallbackDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
1549 {
1550     auto locatorDataManager = LocatorRequiredDataManager::GetInstance();
1551     if (locatorDataManager != nullptr) {
1552         locatorDataManager->UnregisterCallback(remote.promote());
1553         LBSLOGI(LOCATOR, "scan callback OnRemoteDied");
1554     }
1555 }
1556 } // namespace Location
1557 } // namespace OHOS