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