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