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